Verifying Spends with Script Intrepreter
The SDK comes with a script interpreter that allows you to verify the chain of spends within Bitcoin. When coins are spent from one transaction to another, the process is carried out between a particular output of the source transaction and a particular input of the spending transaction. The Spend
class sets up the necessary contextual information for this process, and then evaluates the scripts to determine if the transfer is legitimate.
This guide will walk you through the verification of a real spend, with real data. You can apply this code to your own transactions to ensure the transfer of coins from one state into the next is legitimate.
Pre-requisites
We will need two transactions: a source transaction and a spending transaction, in order to set up the Spend
context in which the transfer of coins occurs between them. When you construct an instance of the Spend
class, you'll need to provide this information in the correct format. In a new file, let's set up som basic things we'll need:
Once you've provided the context and constructed the Spend, you should have a new spend instance ready for verification.
Validating the Spend
You can use the validate()
method to run the scripts and validate the spend, as follows:
The result will be a boolean indicating the result of the script. If there is an error thrown, or if the boolean is false, the script is not valid. If the boolean is true, the spend is considered valid.
Errors from the spend will contain useful information, such as the specific opcode and context in which the error occurred (in either the locking or unlocking script).
Last updated