-
Notifications
You must be signed in to change notification settings - Fork 380
Script Verification
This section describes the verification of input and output scripts with the relevant non-script arguments (witnesses, lock-time, previous output amounts etc). Note that script verification does not verify any chain state information. 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.
auto p2pkh_output_script = script::to_pay_key_hash_pattern(
bitcoin_short_hash(pubkey0));
// Previous output amount.
std::string previous_btc_amount = "1.0";
uint64_t previous_output_amount;
decode_base10(previous_output_amount, previous_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 {
operation(sig_0),
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.
witness empty_witness;
auto ec = script::verify(p2pkh_transaction, 0, rule_fork::all_rules,
p2pkh_input_script, empty_witness, p2pkh_output_script,
previous_output_amount);
// Prints success
std::cout << ec.message() << std::endl;
You can find the complete ready-to-compile example code from this chapter here.
Note that we have also passed in non-script arguments into script::verify()
, such as the witness and the previous output amount, which are 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 std::error_code object, with a value from the Libbitcoin error code enum.
Note: Changes to verify method in upcoming version 4:
Note that the function signature for the verify function will change for the upcoming version 4 of the Libbitcoin library. Input script and witness will be moved into the transaction parameter. Optionally, the previous output point can also be extracted from the transaction metadata. The transaction verification step in the previous example would be expressed as the following.
// Libbitcoin version4: Changes to script::verify().
// Input script and witness parameters are moved into tx.
auto ec1 = script::verify(p2pkh_transaction, input0_index,
rule_fork::all_rules, p2pkh_output_script, previous_output_amount);
// Libbitcoin version4: Alternative script::verify() signature.
// Prevout script and amount can be moved into tx metadata.
p2pkh_transaction.inputs()[input0_index]
.previous_output().metadata.cache.set_script(p2pkh_output_script);
p2pkh_transaction.inputs()[input0_index]
.previous_output().metadata.cache.set_value(previous_output_amount);
auto ec2 = script::verify(p2pkh_transaction, input0_index,
rule_fork::all_rules);
You can find the complete example code from this chapter here
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