pub fn reconstruct_script_pubkey(
leaf: &CompactLeafData,
input: &TxIn,
) -> Result<ScriptBuf, LeafErrorKind>Expand description
Reconstructs the output script, also called scriptPubKey, from a CompactLeafData and the spending tx input. Returns an error if we can’t reconstruct the script (the input doesn’t contain the required data).
The reconstructed output script is the hash of either a public key or a script (i.e., P2PKH, P2SH, P2WPKH, and P2WSH).
The logic behind is:
For some script types, the output script is just the hash of something that needs to be revealed at some later stage (e.g., pkh is the hash of a public key that will be revealed afterwards in the scriptSig, at spend time). Therefore, this information is redundant, as we have it inside the spending transaction. For types where reconstruction is possible, we just need to communicate the type with a single byte marker, and the rest can be built from that using the spending transaction.