Expand description
§Bitcoin script types
Bitcoin doesn’t make a distinction between Bitcoin script coming from
different sources, like scriptPubKey in transaction output or witness
and scriptSig in transaction input. There are many other possible script
containers for Bitcoin script: redeem script, witness script, taproot leaf
bitcoin_scripts of different versions. In fact, any “script” of
bitcoin::Script type can be used for inputs and outputs. What is a valid
script for in one context will not be a valid script for some other. That
would mean that in principle with existing bitcoin::Script type every
input script can be used as an output script, leading to potentially harmful
code coming from an unaware developer.
While all bitcoin::Scripts have the same parsing rules converting byte
string into a set of instructions (i.e. the same syntax), there are
multiple ways how the consensus meaning of these instructions will be
interpreted under different contexts (different semantics). Moreover,
the bitcoin_scripts may be nested - or to be committed into some other
Bitcoin script – in a nested structures like in several layers, like
redeemScript inside of scriptSig used for P2SH, or tapScript within
witnessScript coming from witness field for Taproot. These nested layers
do distinguish on the information they contain, since some of them only
commit to the hashes of the nested bitcoin_scripts (bitcoin::ScriptHash,
WitnessProgram) or public keys (bitcoin::PubkeyHash,
bitcoin::WPubkeyHash), while other contain the full source of the
script.
The present type system represents a solution to the problem: it distinguish
different logical types by introducing Script wrapper types. It defines
LockScript as bottom layer of a script, containing no other script
commitments (in form of their hashes). It also defines types above on it:
PubkeyScript (for whatever is there in scriptPubkey field
of a bitcoin::TxOut), SigScript (for whatever comes from scriptSig
field of bitcoin::TxIn), RedeemScript and WitnessScript.
For taproot, we define LeafScript as a top level of specific script
branch (see bitcoin::util::psbt::TapTree) and crate::TapScript as a
type specific for the current 0xC0 tapleaf version semantics, defined in
BIP-342.
There are conversion functions, which, for instance, can analyse
PubkeyScript and if it is a custom script or P2PK return a
LockScript type - or otherwise fail with error. These conversions
functions reside in convert module. So with this type system one is
always sure which semantic information it does contain.
§Type conversion
LockScript -+-> (PubkeyScript + RedeemScript) -+-> SigScript
| +-> WitnessScript
+-> PubkeyScript
|
TapScript ----> LeafScript
PubkeyScript --?--> LockScriptRe-exports§
pub use convert::ConvertInfo;
Modules§
- address
- Address-related types for detailed payload analysis and memory-efficient processing.
- convert
- Helper traits and supplementary types for converting different types of bitcoin_scripts and keys into each other.
- hlc
- Hash-locked contract supporting data structures.
- taproot
- Taproot script tree implementation allowing arbitrary tree processing/
modification (see
TaprootScriptTreestructure).
Structs§
- Leaf
Script - Any valid branch of taproot script spending
- Lock
Script - Script whose knowledge and satisfaction is required for spending some
specific transaction output. This is the deepest nested version of Bitcoin
script containing no hashes of other bitcoin_scripts, including P2SH
redeemScripthashes orwitnessProgram(hash or witness script), or public key hashes. It is also used for representing specific spending branch of the taproot script tree. - Pubkey
Script - A content of
scriptPubkeyfrom a transaction output - Redeem
Script - Redeem script as part of the
witnessorscriptSigstructure; it is hashed for P2(W)SH output. - Script
Code - A representation of
scriptPubkeydata used during SegWit signing procedure - Script
Set - Scripting data for both transaction output and spending transaction input
parts that can be generated from some complete bitcoin Script
(
LockScript) or public key using particularcrate::ConvertInfo - SigScript
- A content of
scriptSigfrom a transaction input - TapScript
- Script at specific taproot script spend path for
0xC0tapleaf version, which semantics are defined in BIP-342. - Witness
Program - Witness program: a part of post-segwit
scriptPubkey; a data pushed to the stack following witness version - Witness
Script - A content of the script from
witnessstructure; en equivalent ofredeemScriptfor witness-based transaction inputs. However, unlikeRedeemScript,WitnessScriptproduce SHA256-based hashes ofWScriptHashtype.
Enums§
- Taproot
Witness - Parsed witness stack for Taproot inputs
- Taproot
Witness Error - Errors for
TaprootWitnessconstruction fromWitnessand byte representations
Traits§
- Into
Node Hash - Marker trait for all forms of hashes which may participate in the construction of taproot script tree.
Type Aliases§
- TapNode
Hash - The hash value of a taptree node which may be a leaf node, branch node or a hidden node.