pub fn verify(
script_pubkey: &impl ScriptPubkeyExt,
amount: Option<i64>,
tx_to: &impl TransactionExt,
input_index: usize,
flags: Option<u32>,
spent_outputs: &[impl TxOutExt],
) -> Result<(), KernelError>Expand description
Verifies a transaction input against its corresponding output script.
This function checks that the transaction input at the specified index properly satisfies the spending conditions defined by the output script. The verification process depends on the script type and the consensus rules specified by the flags.
§Arguments
script_pubkey- The output script (locking script) to verify againstamount- The amount in satoshis of the output being spent. Required for SegWit and Taproot scripts (whenVERIFY_WITNESSorVERIFY_TAPROOTflags are set). Optional for pre-SegWit scripts.tx_to- The transaction containing the input to verify (the spending transaction)input_index- The zero-based index of the input withintx_toto verifyflags- Verification flags specifying which consensus rules to enforce. IfNone, defaults toVERIFY_ALL. Combine multiple flags using bitwise OR (|).spent_outputs- The outputs being spent by the transaction. For SegWit and Taproot, this should contain all outputs spent by all inputs in the transaction. For pre-SegWit, this can be empty or contain just the output being spent. The length must either be 0 or match the number of inputs in the transaction.
§Returns
Ok(())- Verification succeeded; the input properly spends the outputErr(KernelError::ScriptVerify(ScriptVerifyError::TxInputIndex))- Input index out of boundsErr(KernelError::ScriptVerify(ScriptVerifyError::SpentOutputsMismatch))- The spent_outputs length is non-zero but doesn’t match the number of inputsErr(KernelError::ScriptVerify(ScriptVerifyError::InvalidFlags))- Invalid verification flagsErr(KernelError::ScriptVerify(ScriptVerifyError::InvalidFlagsCombination))- Incompatible combination of flagsErr(KernelError::ScriptVerify(ScriptVerifyError::SpentOutputsRequired))- Spent outputs are required for this script type but were not providedErr(KernelError::ScriptVerify(ScriptVerifyError::Invalid))- Script verification failed; the input does not properly satisfy the output’s spending conditions
§Examples
§Verifying a P2PKH transaction
let prev_output = prev_tx.output(0).unwrap();
let result = verify(
&prev_output.script_pubkey(),
None,
&spending_tx,
0,
Some(VERIFY_ALL),
&[] as &[TxOut],
);§Using custom flags
// Only verify P2SH and DERSIG rules
let custom_flags = VERIFY_P2SH | VERIFY_DERSIG;
let result = verify(
&prev_output.script_pubkey(),
None,
&spending_tx,
0,
Some(custom_flags),
&[] as &[TxOut],
);§Panics
This function does not panic under normal circumstances. All error conditions
are returned as Result::Err.