-
Notifications
You must be signed in to change notification settings - Fork 389
Script Verification
This section describes the verification of input and output scripts with the relevant off-stack arguments (witnesses, lock-time, unspent outputs etc). Note that script verification does not verify the chain state information passed on to the verification of the scripts. Script verification assesses whether input & output scripts evaluate to true on the script stack for a given a set of consensus rules.
In Libbitcoin, the script::verify()
function parses the input and output scripts through the script interpreter, in order to evaluate if the entire script evaluates to true.
The following example demonstrates the spending of a P2PKH output. We construct both P2PKH output and input scripts and then check what the two scripts evaluate to.
// Omitted for brevity:
// Construction of p2pkh_transaction object used in example below.
// Previous output script / Previous output amount.
//---------------------------------------------------------------------------
// Previous output script: P2PKH.
script p2pkh_output_script = script::to_pay_key_hash_pattern(
bitcoin_short_hash(pubkey0));
// Previous output amount.
std::string prev_btc_amount = "1.0";
uint64_t prev_output_amount;
decode_base10(prev_output_amount, prev_btc_amount, btc_decimal_places);
// Input script.
//---------------------------------------------------------------------------
// Signature.
endorsement sig_0;
uint8_t input0_index(0u);
script::create_endorsement(sig_0, my_secret0, p2pkh_output_script,
p2pkh_transaction, input0_index, sighash_algorithm::all);
// Input script operations.
operation::list input_operations;
input_operations.push_back(operation(sig_0));
input_operations.push_back(operation(to_chunk(pubkey0)));
script p2pkh_input_script(input_operations);
// Add input script to transaction.
p2pkh_transaction.inputs()[0].set_script(p2pkh_input_script);
// Verify input script, output script.
//---------------------------------------------------------------------------
// With all fork rules, no witness.
code ec;
witness empty_witness;
ec = script::verify(p2pkh_transaction, 0u, rule_fork::all_rules,
p2pkh_input_script, empty_witness, p2pkh_output_script,
prev_output_amount);
// Prints success (Bitcoin:0)
std::cout << ec << std::endl;
You can find the complete example script here.
Note that we have also passed in non-stack arguments into script::verify()
, such as the witness and the previous output amount, which is required for verifying BIP143 signatures. The rule_fork
argument tells the script interpreter which Bitcoin soft fork rules to apply during the verification of the script.
The script verify method returns a printable error code object.
Users | Developers | License | Copyright © 2011-2024 libbitcoin developers
- Home
- manifesto
- libbitcoin.info
- Libbitcoin Institute
- Freenode (IRC)
- Mailing List
- Slack Channel
- Build Libbitcoin
- Comprehensive Overview
- Developer Documentation
- Tutorials (aaronjaramillo)
- Bitcoin Unraveled
-
Cryptoeconomics
- Foreword by Amir Taaki
- Value Proposition
- Axiom of Resistance
- Money Taxonomy
- Pure Bank
- Production and Consumption
- Labor and Leisure
- Custodial Risk Principle
- Dedicated Cost Principle
- Depreciation Principle
- Expression Principle
- Inflation Principle
- Other Means Principle
- Patent Resistance Principle
- Risk Sharing Principle
- Reservation Principle
- Scalability Principle
- Subjective Inflation Principle
- Consolidation Principle
- Fragmentation Principle
- Permissionless Principle
- Public Data Principle
- Social Network Principle
- State Banking Principle
- Substitution Principle
- Cryptodynamic Principles
- Censorship Resistance Property
- Consensus Property
- Stability Property
- Utility Threshold Property
- Zero Sum Property
- Threat Level Paradox
- Miner Business Model
- Qualitative Security Model
- Proximity Premium Flaw
- Variance Discount Flaw
- Centralization Risk
- Pooling Pressure Risk
- ASIC Monopoly Fallacy
- Auditability Fallacy
- Balance of Power Fallacy
- Blockchain Fallacy
- Byproduct Mining Fallacy
- Causation Fallacy
- Cockroach Fallacy
- Credit Expansion Fallacy
- Debt Loop Fallacy
- Decoupled Mining Fallacy
- Dumping Fallacy
- Empty Block Fallacy
- Energy Exhaustion Fallacy
- Energy Store Fallacy
- Energy Waste Fallacy
- Fee Recovery Fallacy
- Genetic Purity Fallacy
- Full Reserve Fallacy
- Halving Fallacy
- Hoarding Fallacy
- Hybrid Mining Fallacy
- Ideal Money Fallacy
- Impotent Mining Fallacy
- Inflation Fallacy
- Inflationary Quality Fallacy
- Jurisdictional Arbitrage Fallacy
- Lunar Fallacy
- Network Effect Fallacy
- Prisoner's Dilemma Fallacy
- Private Key Fallacy
- Proof of Cost Fallacy
- Proof of Memory Façade
- Proof of Stake Fallacy
- Proof of Work Fallacy
- Regression Fallacy
- Relay Fallacy
- Replay Protection Fallacy
- Reserve Currency Fallacy
- Risk Free Return Fallacy
- Scarcity Fallacy
- Selfish Mining Fallacy
- Side Fee Fallacy
- Split Credit Expansion Fallacy
- Stock to Flow Fallacy
- Thin Air Fallacy
- Time Preference Fallacy
- Unlendable Money Fallacy
- Fedcoin Objectives
- Hearn Error
- Collectible Tautology
- Price Estimation
- Savings Relation
- Speculative Consumption
- Spam Misnomer
- Efficiency Paradox
- Split Speculator Dilemma
- Bitcoin Labels
- Brand Arrogation
- Reserve Definition
- Maximalism Definition
- Shitcoin Definition
- Glossary
- Console Applications
- Development Libraries
- Maintainer Information
- Miscellaneous Articles