Buckle up for today's #DecodingBitcoin post. It's a long one but we promise it's worth your time. Today we're going to break down how to sign a #bitcoin segwit transaction using a real example from the BIP-143 test vectors (thatโ€™s one of the segwit BIPs!) image ๐‘๐‘œ๐‘ก๐‘’: ๐‘กโ„Ž๐‘–๐‘  ๐‘’๐‘ฅ๐‘Ž๐‘š๐‘๐‘™๐‘’ ๐‘–๐‘  ๐‘“๐‘œ๐‘Ÿ ๐‘Ž ๐‘ ๐‘’๐‘”๐‘ค๐‘–๐‘ก ๐‘ฃ0 ๐‘ก๐‘Ÿ๐‘Ž๐‘›๐‘ ๐‘Ž๐‘๐‘ก๐‘–๐‘œ๐‘› ๐‘ ๐‘œ ๐‘ ๐‘œ๐‘š๐‘’ ๐‘œ๐‘“ ๐‘กโ„Ž๐‘’ ๐‘ ๐‘๐‘’๐‘๐‘–๐‘“๐‘–๐‘๐‘  ๐‘Ž๐‘Ÿ๐‘’ ๐‘‘๐‘–๐‘“๐‘“๐‘’๐‘Ÿ๐‘’๐‘›๐‘ก ๐‘“๐‘œ๐‘Ÿ ๐‘™๐‘’๐‘”๐‘Ž๐‘๐‘ฆ ๐‘Ž๐‘›๐‘‘ ๐‘ก๐‘Ž๐‘๐‘Ÿ๐‘œ๐‘œ๐‘ก (๐‘ ๐‘’๐‘”๐‘ค๐‘–๐‘ก ๐‘ฃ1) ๐‘ก๐‘Ÿ๐‘Ž๐‘›๐‘ ๐‘Ž๐‘๐‘ก๐‘–๐‘œ๐‘›๐‘ . ๐ป๐‘œ๐‘ค๐‘’๐‘ฃ๐‘’๐‘Ÿ, ๐‘กโ„Ž๐‘’ ๐‘”๐‘’๐‘›๐‘’๐‘Ÿ๐‘Ž๐‘™ ๐‘๐‘œ๐‘›๐‘๐‘’๐‘๐‘ก๐‘  ๐‘Ž๐‘Ÿ๐‘’ ๐‘ ๐‘ก๐‘–๐‘™๐‘™ ๐‘กโ„Ž๐‘’ ๐‘ ๐‘Ž๐‘š๐‘’! The transaction we'll be working with has two inputs. The first is a legacy P2PK inputโ€“we wonโ€™t be covering that today. Instead, weโ€™re going to focus on the second input, the P2WPKH (native segwit) one. image Since this example came from one of the BIP-143 test vectors, we know what the final, signed transaction looks like. The goal is to recreate this: image First, we create the base transaction, the transaction without any signatures. Weโ€™ll start with the - version number - marker & flag fields (to indicate the tx is segwit) - locktime image ๐ด ๐‘›๐‘œ๐‘ก๐‘’ ๐‘œ๐‘› ๐‘ ๐‘’๐‘”๐‘ค๐‘–๐‘ก ๐‘ฃ๐‘ . ๐‘™๐‘’๐‘”๐‘Ž๐‘๐‘ฆ ๐‘ก๐‘Ÿ๐‘Ž๐‘›๐‘ ๐‘Ž๐‘๐‘ก๐‘–๐‘œ๐‘›๐‘ : ๐ต๐‘’๐‘๐‘Ž๐‘ข๐‘ ๐‘’ ๐‘Ž๐‘ก ๐‘™๐‘’๐‘Ž๐‘ ๐‘ก ๐‘œ๐‘›๐‘’ ๐‘œ๐‘“ ๐‘กโ„Ž๐‘’ ๐‘–๐‘›๐‘๐‘ข๐‘ก๐‘  ๐‘–๐‘  ๐‘ ๐‘’๐‘”๐‘ค๐‘–๐‘ก (๐‘›๐‘Ž๐‘ก๐‘–๐‘ฃ๐‘’ ๐‘œ๐‘Ÿ ๐‘ค๐‘Ÿ๐‘Ž๐‘๐‘๐‘’๐‘‘), ๐‘กโ„Ž๐‘’ ๐‘ก๐‘Ÿ๐‘Ž๐‘›๐‘ ๐‘Ž๐‘๐‘ก๐‘–๐‘œ๐‘› ๐‘–๐‘  ๐‘Ž๐‘™๐‘ ๐‘œ ๐‘๐‘œ๐‘›๐‘ ๐‘–๐‘‘๐‘’๐‘Ÿ๐‘’๐‘‘ ๐‘ ๐‘’๐‘”๐‘ค๐‘–๐‘ก. image Hereโ€™s what we have so far: image Letโ€™s add inputs! Recall that all inputs come from existing transactions. That means for each input, we need to find the transaction it came from and get: 1. that transactionโ€™s ID 2. the output index ๐–๐ก๐š๐ญโ€™๐ฌ ๐š๐ง ๐จ๐ฎ๐ญ๐ฉ๐ฎ๐ญ ๐ข๐ง๐๐ž๐ฑ? Every transaction has a list of outputs. The โ€œoutput indexโ€ is a way to reference a specific output from the list. We need to ask, "from a transactionโ€™s list of outputs, which one corresponds to the input I care about?โ€ For each input, two more things are needed: the ๐ฌ๐œ๐ซ๐ข๐ฉ๐ญ๐’๐ข๐  (placeholder for data required to spend the input), and a ๐ฌ๐ž๐ช๐ฎ๐ž๐ง๐œ๐ž ๐ง๐ฎ๐ฆ๐›๐ž๐ซ (usually 0xFFFFFFFF) image After adding all the inputs, the transaction looks like this: image Remember when we said the scriptSig would be a placeholder? Hereโ€™s why those fields are currently empty: image Time to add outputs! For each output, we include the - amount (in satoshis) - scriptPubKey: the locking script that defines the rules for how the output can be spent image Things are starting to come together! image A few more things are needed before we can get to signing. First is setting up the ๐ฐ๐ข๐ญ๐ง๐ž๐ฌ๐ฌ field. This is where the signature and corresponding public key go for segwit transactions. The witness field starts off empty. This is different from legacy transactions where signatures are placed directly in the scriptSig field. image The next thing thatโ€™s needed is the ๐ฌ๐œ๐ซ๐ข๐ฉ๐ญ๐‚๐จ๐๐ž. The scriptCode for a P2WPKH (pay-to-witness-public-key-hash) input is: image ๐‘Šโ„Ž๐‘Ž๐‘กโ€™๐‘  ๐‘กโ„Ž๐‘Ž๐‘ก 20 ๐‘๐‘ฆ๐‘ก๐‘’ ๐‘๐‘ข๐‘๐‘˜๐‘’๐‘ฆ โ„Ž๐‘Ž๐‘ โ„Ž? ๐ธ๐‘Ž๐‘Ÿ๐‘™๐‘–๐‘’๐‘Ÿ ๐‘ค๐‘’ ๐‘ ๐‘Ž๐‘ค ๐‘’๐‘Ž๐‘โ„Ž ๐‘œ๐‘ข๐‘ก๐‘๐‘ข๐‘ก โ„Ž๐‘Ž๐‘  ๐‘Ž ๐‘ ๐‘๐‘Ÿ๐‘–๐‘๐‘ก๐‘ƒ๐‘ข๐‘๐พ๐‘’๐‘ฆ (๐‘Ÿ๐‘ข๐‘™๐‘’๐‘  ๐‘“๐‘œ๐‘Ÿ โ„Ž๐‘œ๐‘ค ๐‘ก๐‘œ ๐‘ ๐‘๐‘’๐‘›๐‘‘ ๐‘กโ„Ž๐‘’ ๐‘œ๐‘ข๐‘ก๐‘๐‘ข๐‘ก). ๐ด๐‘™๐‘ ๐‘œ, ๐‘Ÿ๐‘’๐‘๐‘Ž๐‘™๐‘™ ๐‘กโ„Ž๐‘Ž๐‘ก ๐‘กโ„Ž๐‘’ ๐‘–๐‘›๐‘๐‘ข๐‘ก ๐‘ก๐‘œ ๐‘œ๐‘›๐‘’ ๐‘ก๐‘Ÿ๐‘Ž๐‘›๐‘ ๐‘Ž๐‘๐‘ก๐‘–๐‘œ๐‘› ๐‘–๐‘  ๐‘กโ„Ž๐‘’ ๐‘œ๐‘ข๐‘ก๐‘๐‘ข๐‘ก ๐‘“๐‘Ÿ๐‘œ๐‘š ๐‘Ž๐‘›๐‘œ๐‘กโ„Ž๐‘’๐‘Ÿ. ๐‘‡โ„Ž๐‘’ 20 ๐‘๐‘ฆ๐‘ก๐‘’ ๐‘๐‘ข๐‘๐‘˜๐‘’๐‘ฆ โ„Ž๐‘Ž๐‘ โ„Ž ๐‘–๐‘  ๐‘’๐‘ฅ๐‘ก๐‘Ÿ๐‘Ž๐‘๐‘ก๐‘’๐‘‘ ๐‘“๐‘Ÿ๐‘œ๐‘š ๐‘กโ„Ž๐‘’ ๐‘๐‘œ๐‘Ÿ๐‘Ÿ๐‘’๐‘ ๐‘๐‘œ๐‘›๐‘‘๐‘–๐‘›๐‘” ๐‘œ๐‘ข๐‘ก๐‘๐‘ข๐‘ก'๐‘  ๐‘ ๐‘๐‘Ÿ๐‘–๐‘๐‘ก๐‘ƒ๐‘ข๐‘๐พ๐‘’๐‘ฆ. Hereโ€™s the ๐ฌ๐œ๐ซ๐ข๐ฉ๐ญ๐‚๐จ๐๐ž for the example weโ€™re working on: image Lastly, three important hashes are required. The first is ๐ก๐š๐ฌ๐ก๐๐ซ๐ž๐ฏ๐จ๐ฎ๐ญ๐ฌ. Itโ€™s the double SHA256 hash of all input outpoints (outpoint = the transaction id + output index) The second is ๐ก๐š๐ฌ๐ก๐’๐ž๐ช๐ฎ๐ž๐ง๐œ๐ž, the double SHA256 hash of all input sequence numbers. The third is ๐ก๐š๐ฌ๐ก๐Ž๐ฎ๐ญ๐ฉ๐ฎ๐ญ๐ฌ, the double SHA256 hash of all outputs. Thatโ€™s everything (finally!). Letโ€™s put it all together into something that can be signed! When signing a transaction, the spender actually signs a hash of the transaction data, not the entire transaction itself. This hash is called the ๐ฌ๐ข๐ ๐ก๐š๐ฌ๐ก. The data used to create the sighash is called the ๐ฉ๐ซ๐ž๐ข๐ฆ๐š๐ ๐ž. For a transaction input, the preimage is made of these items: image The sighash_type indicates which parts of the transaction the signature is committing to. image After hashing the preimage twice with SHA-256, weโ€™re left with the sighash. At last! Itโ€™s time to do some signing! image There are a few steps for signing a segwit (v0) transaction. First, the signerโ€™s private key is used to create an ECDSA signature for the sighash. The resulting signature has two parts, ๐‘Ÿ and ๐‘ . In ECDSA, there are actually two valid s values for every signature: a "high" value and a "low" value. Both are mathematically valid, but bitcoin requires using the low s value to prevent transaction malleability (that means altering a transaction's ID!) After selecting the low s value, the signature must be encoded into DER format. This is how itโ€™s structured: image And hereโ€™s what the DER encoded signature looks like for our example: image The last step is to add a byte at the end for the sighash type. If we look back at the preimage made earlier we see this example is using SIGHASH_ALL (0x01). The full code for the signing step looks like this: image Remember the transaction witness field we set space aside for? Itโ€™s now time to put the signature in it ๐Ÿš€ image This is how the witness field is structured: image Which works out to be this for our example: image With the completion of the witness field, the transaction is now signed! image This is what the final signed transaction hex looks like broken down: image Bonus: You can use the Bitcoin Core CLI decoderawtransaction command to examine all the parts of the raw transaction hex image * ~ * ~ * ~ * ~ * ~ * ~ If you made it to the end, give yourself a pat on the back. If you enjoyed it, be sure to like this post so we know to make more like it! * ~ * ~ * ~ * ~ * ~ * ~ This material is from Decoding Bitcoin, your go-to resource for understanding bitcoin, #privacy, and #decentralization. You can visit for the full lesson with all the code examples, as well as more free, interactive content. For more of a challenge, play chapters 4, 5, and 6 of @Saving Satoshi () to learn about public-private key cryptography, digital signatures, and transaction building ๐Ÿ˜บ image Hope you learned something new about transaction signing. If you enjoyed this, share it with a friend and donโ€™t forget to follow us, @Bitcoin Dev Project for more content like this. Thanks for reading!