Skip to main content

BitcoinTx

Git Source

Allows to reference Bitcoin raw transaction in Solidity.

See https://developer.bitcoin.org/reference/transactions.html#raw-transaction-format

Functions

validateProof

Validates the SPV proof of the Bitcoin transaction. Reverts in case the validation or proof verification fail.

function validateProof(IRelay relay, uint256 txProofDifficultyFactor, Info memory txInfo, Proof memory proof)
internal
view
returns (bytes32 txHash);

Parameters

NameTypeDescription
relayIRelayBitcoin relay providing the current Bitcoin network difficulty.
txProofDifficultyFactoruint256The number of confirmations required on the Bitcoin chain.
txInfoInfoBitcoin transaction data.
proofProofBitcoin proof data.

Returns

NameTypeDescription
txHashbytes32Proven 32-byte transaction hash.

evaluateProofDifficulty

Evaluates the given Bitcoin proof difficulty against the actual Bitcoin chain difficulty provided by the relay oracle. Reverts in case the evaluation fails.

function evaluateProofDifficulty(IRelay relay, uint256 txProofDifficultyFactor, bytes memory bitcoinHeaders)
internal
view;

Parameters

NameTypeDescription
relayIRelayBitcoin relay providing the current Bitcoin network difficulty.
txProofDifficultyFactoruint256The number of confirmations required on the Bitcoin chain.
bitcoinHeadersbytesBitcoin headers chain being part of the SPV proof. Used to extract the observed proof difficulty.

processTxOutputs

Processes the Bitcoin transaction output vector.

function processTxOutputs(bytes memory txOutputVector, bytes32 scriptPubKeyHash)
internal
pure
returns (TxOutputsInfo memory resultInfo);

Parameters

NameTypeDescription
txOutputVectorbytesBitcoin transaction output vector. This function assumes vector's structure is valid so it must be validated using e.g. BTCUtils.validateVout function before it is passed here.
scriptPubKeyHashbytes32Expected Bitcoin scriptPubKey keccak256 hash.

Returns

NameTypeDescription
resultInfoTxOutputsInfoOutcomes of the processing.

processTxOutputs

Processes all outputs from the transaction.

function processTxOutputs(
bytes memory txOutputVector,
bytes32 scriptPubKeyHash,
TxOutputsProcessingInfo memory processInfo
) internal pure returns (TxOutputsInfo memory resultInfo);

Parameters

NameTypeDescription
txOutputVectorbytesBitcoin transaction output vector. This function assumes vector's structure is valid so it must be validated using e.g. BTCUtils.validateVout function before it is passed here.
scriptPubKeyHashbytes32Expected Bitcoin scriptPubKey keccak256 hash.
processInfoTxOutputsProcessingInfoTxOutputsProcessingInfo identifying output starting index and the number of outputs.

extractEvmAddressFromOutput

function extractEvmAddressFromOutput(bytes memory _output, uint256 _at) internal pure returns (address evmAddress);

reverseEndianness

function reverseEndianness(bytes32 b) internal pure returns (bytes32 txHash);

ensureTxInputSpendsUtxo

function ensureTxInputSpendsUtxo(bytes memory _vin, BitcoinTx.UTXO memory utxo) internal pure;

Structs

Info

Represents Bitcoin transaction data.

struct Info {
bytes4 version;
bytes inputVector;
bytes outputVector;
bytes4 locktime;
}

Proof

Represents data needed to perform a Bitcoin SPV proof.

struct Proof {
bytes merkleProof;
uint256 txIndexInBlock;
bytes bitcoinHeaders;
}

UTXO

Represents info about an unspent transaction output.

struct UTXO {
bytes32 txHash;
uint32 txOutputIndex;
uint64 txOutputValue;
}

TxOutputsProcessingInfo

Represents temporary information needed during the processing of the Bitcoin transaction outputs. This structure is an internal one and should not be exported outside of the transaction processing code.

Allows to mitigate "stack too deep" errors on EVM.

struct TxOutputsProcessingInfo {
uint256 outputStartingIndex;
uint256 outputsCount;
}

TxOutputsInfo

Represents an outcome of the Bitcoin transaction outputs processing.

struct TxOutputsInfo {
uint64 value;
address evmAddress;
}