The main function is about verifying that a transaction was mined in a certain bitcoin block. The verification happens in two steps:
- compare the provided block header information with the actual chain
- compare the merkle root from the provided merkle proof with the merkle root of the provided block header
These are the main functions that can be used to verify that a tx was mined in a given bitcoin block.
The block header can be provided as an object with the header details or as a buffer.
- was-tx-mined-compact (header as a buffer)
- was-tx-mined (header as an object)
Both functions take the following arguments in the same order:
- Bitcoin block height
- Raw tx hex
- Bitcoin block header either as hex or as a tuple
- Merkle proof
The header object has the following properties using the reverse hex of the shown values in the bitcoin explorer:
- version: (buff 4)
- parent: (buff 32)
- merkle-root: (buff 32)
- timestamp: (buff 4)
- nbits: (buff 4)
- nonce: (buff 4)
The verification happens in two steps:
- verify that the hash of the given header is equal to the header hash of the given block height
- verify that the given merkle proof for the given transaction id results in the merkle root contained in the header
- verify-block-header
- verify-merkle-proof
Once the tx id was confirmed to be mined in the given block, the inputs and outputs of the tx can be used to trigger certain actions in a smart contract. To verify e.g. that an input is indeed an input of the verified tx id, the hash of a transaction buffer must match the tx id. Then the inputs and outputs of the transaction can be used either
- by parsing the transaction buffer into an object with inputs, outputs, timelock, etc. or
- by concatinating the transaction object to a buffer with the correct hash.
As requirements, clarinet
and deno
needs to be installed.
This example sends an amount of STX to the sender of a bitcoin transaction using p2pkh addresses. It exists in two version, one using the header object, the other the header buffer (compact).
- Deploy all contracts
clarinet integrate
- Call deployment plan to send 0.1 BTC
clarinet deployments apply -p deployments/send-btc.devnet-plan.yaml --no-dashboard
- Confirm to continue
- Copy the tx hex from the Transaction
- Press N to mine the block in the clarinet dashboard
- Generate deployment plan for the stacks transaction by running the following command with the copied tx hex (replace
01..txhex
). (The generation script takes care of reversing the properties of the block header.)
deno run --allow-net ./src/generatePlan.ts 01..txhex > deployments/send-to-first-input-plan.yaml
- Call deployment plan to send STX to the bitcoin sender
clarinet deployments apply -p deployments/sent-to-first-input-plan.yaml
- Check the stacks explorer at localhost: 8001 about the result for the transactions of the two versions