BIP 0141 + 0143
Why does a Bitcoin transaction have two identifiers?
SegWit separates what a transaction does from the proof that it is allowed, and gives the transaction a second name that also covers the proof. Signing and block space changed along the way.
Every transaction is known by a hash of its bytes. Before SegWit, those bytes included the signatures, so a change in how a transaction was signed could change its identifier without changing anything it did.12
BIP 141, a consensus soft fork assigned in 2015 and recorded as deployed, separates the data that decides what a transaction does from the data that proves it is allowed. Each transaction now has two identifiers. BIP 143, its companion, changes what a SegWit signature commits to. This chapter takes a transaction published in BIP 143 apart to see both.345
Source details
This chapter is an independent explanation, not the proposal itself. Preamble fields are shown as the pinned files record them, without author e-mail addresses. Status is the proposal’s own field. It is not an endorsement or a sign of consensus.
BIP 141: Segregated Witness (Consensus layer)
- BIP
- 141
- Layer
- Consensus (soft fork)
- Title
- Segregated Witness (Consensus layer)
- Authors
- Eric Lombrozo
Johnson Lau
Pieter Wuille - Status
- Deployed
- Type
- Specification
- Assigned
- 2015-12-21
- License
- PD
BIP 143: Transaction Signature Verification for Version 0 Witness Program
- BIP
- 143
- Layer
- Consensus (soft fork)
- Title
- Transaction Signature Verification for Version 0 Witness Program
- Authors
- Johnson Lau
Pieter Wuille - Status
- Deployed
- Type
- Specification
- Assigned
- 2016-01-03
- License
- PD
Fig. A03.1 One transaction, two serializations
The whole serialization, 343 bytes, hashed twice with SHA-256: the wtxid.
Cut out the marker and flag (00 01), the 2 bytes after nVersion.
Cut out the witness, the 108 bytes just before nLockTime.
The 233 bytes left are the old serialization; hashed the same way, they give the txid.
Exact identifiers (byte order as computed)
- wtxid, 343 bytes hashed
62b709c9126ae7782ea86576b338f39211199d149dced92423df070937386cc3- txid, 233 bytes hashed
09462d604a3102fae083ada369c795855a28db4bdd3d05358a361cf32a1a15e8
What a transaction does, and what proves it
BIP 141 starts from a distinction. A transaction’s effects are fully determined by which outputs it spends and which new outputs it creates. Signatures, and the other data used to check that a spend is allowed, are needed to validate the chain, not to determine its resulting state.1
SegWit moves that validation data, in particular scripts and signatures, into a new structure called the witness. Blocks commit to it separately from the transaction tree, through a commitment placed in the coinbase transaction.48
Two serializations, two hashes
The txid is defined exactly as before: the double SHA-256 of nVersion, the inputs, the outputs and nLockTime. The wtxid is the double SHA-256 of a longer serialization. It has the same fields, plus a marker byte 0x00 and a flag byte 0x01 after the version, and the witness data just before nLockTime.56
Each input gets one witness field: a count of stack items, then each item preceded by its length. An input that does not spend a witness program still gets a field, an empty one written as the single byte 0x00. When no input has witness data, the wtxid equals the txid.1011
Fig. A03.2 The witness section, byte by byte
Exact witness items
- Input 1, item 0 (71 bytes)
304402203609e17b84f6a7d30c80bfa610b5b4542f32a8a0d5447a12fb1366d7f01cc44a0220573a954c4518331561406f90300e8f3358f51928d43c212a8caed02de67eebee01- Input 1, item 1 (33 bytes)
025476c2e83188368da1ff3e292e7acafcdb3566bb0ad253f62fc70f07aeee6357- HASH160 of the key = the 20-byte program
1d0f172a0ecb48aee1be1f2687d2963ae33f71a1
Why have a second identifier at all? Because blocks that contain witness data must commit to it too. BIP 141 adds a rule requiring a commitment to the wtxids of a block’s transactions, built like the existing merkle root and recorded in an output of the coinbase transaction. The coinbase’s own wtxid is simply taken to be all zeros.13
Fig. A03.3 Two trees in one block
The example's identifiers (byte order as computed)
- txid
09462d604a3102fae083ada369c795855a28db4bdd3d05358a361cf32a1a15e8- wtxid
62b709c9126ae7782ea86576b338f39211199d149dced92423df070937386cc3- The coinbase's wtxid, as BIP 141 defines it
0000000000000000000000000000000000000000000000000000000000000000
The example below has an ordinary input and a SegWit input. Switch between txid and wtxid. Watch the witness bytes leave or enter the highlighted hash input, while the transaction still describes the same payment.15714
Fig. A03.4 Which bytes each hash covers
Static view: the first example through the txid lens. With JavaScript you can switch to the wtxid and BIP 143 lenses and to the second example.
txid: double SHA-256 of the 233 bytes a pre-SegWit node sees. The marker, flag and witness (110 bytes) are left out.
Exact values for this view
- txid (byte order as computed)
09462d604a3102fae083ada369c795855a28db4bdd3d05358a361cf32a1a15e8- Base size · total size
- 233 bytes · 343 bytes
BIP 143 line 190 (Example: Native P2WPKH). Parsed and hashed by the tested model; the preimage and sighash match the published ones. Serialization only: nothing here checks signatures.
wtxid: double SHA-256 of all 343 bytes, marker, flag and witness included.
Exact values for this view
- wtxid (byte order as computed)
62b709c9126ae7782ea86576b338f39211199d149dced92423df070937386cc3- Base size · total size
- 233 bytes · 343 bytes
BIP 143 line 190 (Example: Native P2WPKH). Parsed and hashed by the tested model; the preimage and sighash match the published ones. Serialization only: nothing here checks signatures.
BIP 143 preimage for input 1: ten items, 182 bytes. Numbered bytes feed the item with that number; scriptCode, amount and sighash type are not fields of the transaction.
Exact values for this view
- 1. nVersion (copied from the transaction)
01000000- 2. hashPrevouts (double SHA-256 of every input’s outpoint)
96b827c8483d4e9b96712b6713a7b68d6e8003a781feba36c31143470b4efd37- 3. hashSequence (double SHA-256 of every input’s nSequence)
52b0a642eea2fb7ae638c36f6252b6750293dbe574a806984b8e4d8548339a3b- 4. outpoint (the input being signed)
ef51e1b804cc89d182d279655c3aa89e815b1b309fe287d9b2b55d57b90ec68a01000000- 5. scriptCode (built from the spent output’s 20-byte program, which is not in this transaction (BIP 143 line 180))
1976a9141d0f172a0ecb48aee1be1f2687d2963ae33f71a188ac- 6. amount (not in this transaction: the value of the output being spent (BIP 143 line 156))
0046c32300000000- 7. nSequence (the input being signed)
ffffffff- 8. hashOutputs (double SHA-256 of every output)
863ef3e1a92afbfdb97f31ad0fc7683ee943e9abcf2501590ff8f6551f47e5e5- 9. nLockTime (copied from the transaction)
11000000- 10. sighash type (SIGHASH_ALL, chosen by the signer)
01000000- Sighash (double SHA-256 of the preimage)
c37af31116d1b27caf68aae9e3ac82f1477929014d5b917657d0eb49478cb670
BIP 143 line 190 (Example: Native P2WPKH). Parsed and hashed by the tested model; the preimage and sighash match the published ones. Serialization only: nothing here checks signatures.
txid: double SHA-256 of the 142 bytes a pre-SegWit node sees. The marker, flag and witness (109 bytes) are left out.
Exact values for this view
- txid (byte order as computed)
230c7acd5e6e84f4f306f2818a386a0be5a5f725f8dc8c0f2e0595f5d0d948ef- Base size · total size
- 142 bytes · 251 bytes
BIP 143 line 250 (Example: P2SH-P2WPKH). Parsed and hashed by the tested model; the preimage and sighash match the published ones. Serialization only: nothing here checks signatures.
wtxid: double SHA-256 of all 251 bytes, marker, flag and witness included.
Exact values for this view
- wtxid (byte order as computed)
bbfaa949fc3cecafb0cf7020e9a5418a24ba85e8691e11bfdcc5f62b3b480f68- Base size · total size
- 142 bytes · 251 bytes
BIP 143 line 250 (Example: P2SH-P2WPKH). Parsed and hashed by the tested model; the preimage and sighash match the published ones. Serialization only: nothing here checks signatures.
BIP 143 preimage for input 0: ten items, 182 bytes. Numbered bytes feed the item with that number; amount and sighash type are not fields of the transaction.
Exact values for this view
- 1. nVersion (copied from the transaction)
01000000- 2. hashPrevouts (double SHA-256 of every input’s outpoint)
b0287b4a252ac05af83d2dcef00ba313af78a3e9c329afa216eb3aa2a7b4613a- 3. hashSequence (double SHA-256 of every input’s nSequence)
18606b350cd8bf565266bc352f0caddcf01e8fa789dd8a15386327cf8cabe198- 4. outpoint (the input being signed)
db6b1b20aa0fd7b23880be2ecbd4a98130974cf4748fb66092ac4d3ceb1a547701000000- 5. scriptCode (built from the 20-byte witness program inside this input’s redeemScript (BIP 143 line 240))
1976a91479091972186c449eb1ded22b78e40d009bdf008988ac- 6. amount (not in this transaction: the value of the output being spent (BIP 143 line 215))
00ca9a3b00000000- 7. nSequence (the input being signed)
feffffff- 8. hashOutputs (double SHA-256 of every output)
de984f44532e2173ca0d64314fcefe6d30da6f8cf27bafa706da61df8a226c83- 9. nLockTime (copied from the transaction)
92040000- 10. sighash type (SIGHASH_ALL, chosen by the signer)
01000000- Sighash (double SHA-256 of the preimage)
64f3b0f4dd2bb3aa1ce8566d220cc74dda9df97d8490cc81d89d735c92e59fb6
BIP 143 line 250 (Example: P2SH-P2WPKH). Parsed and hashed by the tested model; the preimage and sighash match the published ones. Serialization only: nothing here checks signatures.
Why a stable name matters
Because the signature data of SegWit inputs is no longer part of the txid, changing how such an input was signed no longer changes the transaction’s identifier. BIP 141 presents this as the end of nonintentional malleability, under conditions. Every input must be a witness input signed with at least one CHECKSIG or CHECKMULTISIG. A multisig spend can still be changed if enough of its key holders agree.2
Stable identifiers matter when one transaction spends another before either is confirmed. BIP 141 names the benefit directly: chains of unconfirmed transactions without counterparty risk, which it calls important for off-chain protocols such as the Lightning Network.19
The mixed example also shows where the protection stops. Its first input is an ordinary pay-to-public-key spend, so that input’s signature is still in the scriptSig and still part of the txid.514
Not every witness item is a signature
For a P2WPKH input, the witness must be exactly two items: a signature, and a public key whose HASH160 matches the 20-byte program. A P2WSH input’s witness is different again: the stack items its script needs, followed by the script itself. BIP 141 says witness data is not script: the items are data, even when one of them is a serialized witness script. Nor are they all signatures.101220
The second example wraps a witness program inside an older P2SH script. Its scriptSig carries the redeem script that contains the program, so that single input has both a scriptSig and a witness, as drawn below.1415
Fig. A03.5 One input with a scriptSig and a witness
Exact scriptSig and witness
- scriptSig (23 bytes)
16001479091972186c449eb1ded22b78e40d009bdf0089- Witness item 0 (71 bytes)
3044022047ac8e878352d3ebbde1c94ce3a10d057c24175747116f8288e5d794d12d482f0220217f36a485cae903c713331d877c1f64677e3622ad4010726870540656fe9dcb01- Witness item 1 (33 bytes)
03ad1d8e89212f0b92c74d23bb710c00662ad1470198ac48c43f7d6f93a2a26873
Weight instead of bytes
Separating the witness had a second purpose. BIP 141’s motivation notes that moving data into a structure older nodes do not know about lets a soft fork discount it when calculating block size.21
When BIP 141 was written, blocks were limited to 1,000,000 bytes. It replaced that rule with block weight: three times the base size plus the total size, at most 4,000,000. Base size counts the bytes a pre-SegWit node would see; total size counts everything, witness included.2223
Written another way, every base byte costs four weight units, and every witness-related byte, including the marker and flag, costs one. BIP 141 explains why it chose a single combined limit rather than two separate ones: separate limits would make mining and fee estimation nearly impossible.2425
Weight is therefore not a byte count. Since base bytes count four times, no more than 1,000,000 of them fit in a block, as before. A block can exceed that many bytes in total only through its witness data.2326
Fig. A03.6 Bytes into weight
BIP 141 defines transaction weight the same way, and virtual size as weight divided by four, rounded up. For the mixed example that is 233 base bytes and 343 bytes in total: 3 × 233 + 343 = 1,042 weight units, or 261 virtual bytes. These per-transaction terms are suggested vocabulary; the consensus limit applies to blocks.232728
Going deeper: what a SegWit signature signs
BIP 143 changes what a signature in a version 0 witness program commits to. Under the original algorithm, each signature hashed data in proportion to the size of the whole transaction, so total hashing grew with the square of the number of signature checks. BIP 143 instead builds a preimage with a fixed structure of ten items, three of which are summary hashes that can be reused across inputs.16303132
Fig. A03.7 Hashing once, or once per input
For SIGHASH_ALL, the three summary hashes cover every input’s outpoint, every input’s nSequence and every output. They can be computed once and reused for each input that is signed, which is where the saving comes from.1832
Fig. A03.8 What a SegWit signature signs
Exact preimage items
- 1. nVersion: copied from the transaction
01000000- 2. hashPrevouts: double SHA-256 of every input’s outpoint
96b827c8483d4e9b96712b6713a7b68d6e8003a781feba36c31143470b4efd37- 3. hashSequence: double SHA-256 of every input’s nSequence
52b0a642eea2fb7ae638c36f6252b6750293dbe574a806984b8e4d8548339a3b- 4. outpoint: the input being signed
ef51e1b804cc89d182d279655c3aa89e815b1b309fe287d9b2b55d57b90ec68a01000000- 5. scriptCode: built from the spent output’s 20-byte program, which is not in this transaction (BIP 143 line 180)
1976a9141d0f172a0ecb48aee1be1f2687d2963ae33f71a188ac- 6. amount: not in this transaction: the value of the output being spent (BIP 143 line 156)
0046c32300000000- 7. nSequence: the input being signed
ffffffff- 8. hashOutputs: double SHA-256 of every output
863ef3e1a92afbfdb97f31ad0fc7683ee943e9abcf2501590ff8f6551f47e5e5- 9. nLockTime: copied from the transaction
11000000- 10. sighash type: SIGHASH_ALL, chosen by the signer
01000000
The sixth item matters most to small signing devices: the amount of the output being spent. That amount is not in the transaction at all. Without it, an offline signer cannot work out how much is being spent or what fee is being paid. BIP 143 makes each signature commit to its own input’s amount. If a device is told a wrong amount for that input, the signature is therefore invalid and, in the BIP’s words, no funding might be lost. That protects only the input being signed: knowing the fee needs a trustworthy amount for every input.1734
Fig. A03.9 Why the signature covers the amount
The offline signer sees input 1’s outpoint, a reference to an earlier output. The amount it spends is not in the transaction.
Told the amount, 6.00000000 BTC as published with the example, the signer puts it into item 6 and hashes the preimage.
Told any other amount, it would hash a different preimage. Nodes rebuild the preimage with the real amount, so that signature would fail.
Exact values
- Input 1: previous txid (serialized byte order)
ef51e1b804cc89d182d279655c3aa89e815b1b309fe287d9b2b55d57b90ec68a- Item 6: amount, 8 bytes little-endian (6.00000000 BTC)
0046c32300000000- Sighash with the published amount
c37af31116d1b27caf68aae9e3ac82f1477929014d5b917657d0eb49478cb670
Use the BIP 143 lens of Fig. A03.4 to see where each item comes from. For a P2WPKH input the scriptCode is a standard pay-to-public-key-hash script built from the 20-byte program. The page reproduces the published preimage and digest of both examples byte for byte.72933
All of this applies only to version 0 witness programs. Each signature covers its own input’s amount. In the mixed example, the first input is an ordinary spend signed with the original algorithm, which does not involve the amount at all.14173536
Two names, two kinds of content
Every transaction has two identifiers, equal when it carries no witness data. The txid names what the transaction does, what it spends and what it creates, plus any proof still kept in scriptSigs. The wtxid adds the witness: the rest of the evidence that the spending was allowed.15
Evidence
Each numbered marker in the text points here, and the ↩ links lead back to the text. Quotes are verbatim
from the BIP files at commit 3a10b5b, wiki markup
included. Links open the exact lines. Labels say what kind of statement each entry is: a rule, the
author’s rationale, history, a test vector, or our own inference.
-
↩ a b c d Author’s rationale A transaction's effects are determined by the outputs it spends and creates; signatures are only needed to validate the chain state, not to determine it.
Quoted source text (1)
The entirety of the transaction's effects are determined by output consumption (spends) and new output creation. Other transaction data, and signatures in particular, are only required to validate the blockchain state, not to determine it.
-
↩ a b Author’s rationale Signature data in the witness no longer affects the txid; BIP141 states this prevents involuntary malleability when all inputs are signed with at least one CHECKSIG or CHECKMULTISIG, while an m-of-n multisig spend remains malleable with agreement of m key holders.
BIP 141 L26 BIP 141 L27 BIP 141 L28
Quoted source text (3)
Since signature data is no longer part of the transaction hash, changes to how the transaction was signed are no longer relevant to transaction identification.
as long as all inputs are signed (with at least one CHECKSIG or CHECKMULTISIG operation)
In the case of an m-of-n CHECKMULTISIG script, a transaction is malleable only with agreement of m private key holders
-
↩ History BIP141 (assigned 2015) and BIP143 (assigned 2016) are consensus soft-fork proposals whose preambles record the status Deployed.
BIP 141 L10 BIP 141 L3 BIP 141 L8 BIP 143 L9 BIP 143 L7
Quoted source text (5)
Assigned: 2015-12-21
Layer: Consensus (soft fork)
Status: Deployed
Assigned: 2016-01-03
Status: Deployed
-
↩ a b Rule BIP141 defines a witness structure, committed to blocks separately from the transaction merkle tree, holding data needed to check validity but not to determine effects; scripts and signatures move into it.
Quoted source text (1)
This BIP defines a new structure called a "witness" that is committed to blocks separately from the transaction merkle tree. This structure contains data required to check transaction validity but not required to determine transaction effects. In particular, scripts and signatures are moved into this new structure.
-
↩ a b c d e f g h Rule Each transaction has two IDs: the txid is the double SHA256 of [nVersion][txins][txouts][nLockTime]; the wtxid is the double SHA256 of [nVersion][marker][flag][txins][txouts][witness][nLockTime].
BIP 141 L41 BIP 141 L43 BIP 141 L45 BIP 141 L47 BIP 141 L49
Quoted source text (5)
Each transaction will have 2 IDs.
Definition of <code>txid</code> remains unchanged: the double SHA256 of the traditional serialization format:
[nVersion][txins][txouts][nLockTime]
A new <code>wtxid</code> is defined: the double SHA256 of the new serialization with witness data:
[nVersion][marker][flag][txins][txouts][witness][nLockTime]
-
↩ a b Rule The marker must be 0x00 and the flag a non-zero byte, currently 0x01.
Quoted source text (2)
The <code>marker</code> MUST be a 1-byte zero value: <code>0x00</code>.
The <code>flag</code> MUST be a 1-byte non-zero value. Currently, <code>0x01</code> MUST be used.
-
↩ a b c d Test vector BIP143 publishes complete example transactions with their preimages and sighashes, and asks developers to test against them.
BIP 143 L137 BIP 143 L190 BIP 143 L250
Quoted source text (3)
developers should test their implementations against all the tests below.
The serialized signed transaction is:
The serialized signed transaction is:
-
↩ a b c Rule The witness is committed in a tree nested into the block's merkle root via the coinbase; the commitment is required when a block has witness data and optional otherwise.
BIP 141 L18 BIP 141 L63 BIP 141 L80
Quoted source text (3)
The witness is committed in a tree that is nested into the block's existing merkle root via the coinbase transaction
A new block rule is added which requires a commitment to the <code>wtxid</code>.
If all transactions in a block do not have witness data, the commitment is optional.
-
↩ Author’s rationale Transmitting witness data is needed only for a peer that validates a transaction, not one that only checks its existence.
Quoted source text (1)
It is needed only if a peer is trying to validate a transaction instead of just checking its existence.
-
↩ a b c Rule Each input has a witness field: a var_int count of stack items, then each item with a var_int length. Witness data is not script.
Quoted source text (1)
Each txin is associated with a witness field. A witness field starts with a <code>var_int</code> to indicate the number of stack items for the txin. It is followed by stack items, with each item starts with a <code>var_int</code> to indicate the length. Witness data is NOT script.
-
↩ a b c Rule A non-witness-program input must have an empty witness field, written 0x00; if no input is a witness program, wtxid equals txid.
Quoted source text (1)
A non-witness program (defined hereinafter) txin MUST be associated with an empty witness field, represented by a <code>0x00</code>. If all txins are not witness program, a transaction's <code>wtxid</code> is equal to its <code>txid</code>.
-
↩ a b c d Rule A version 0, 20-byte program is P2WPKH: the witness is exactly two items, a signature and a public key whose HASH160 matches the program.
BIP 141 L95 BIP 141 L96 BIP 141 L97
Quoted source text (3)
It is interpreted as a pay-to-witness-public-key-hash (P2WPKH) program.
The witness must consist of exactly 2 items (≤ 520 bytes each). The first one a signature, and the second one a public key.
The HASH160 of the public key must match the 20-byte witness program.
-
↩ a b Rule Blocks containing witness data must commit to the wtxids; the witness root is built like the merkle root, the commitment hash is the double SHA-256 of that root and a witness reserved value (carried in the coinbase input's witness), it is recorded in a coinbase output, and the coinbase's own wtxid is taken as all zeros.
BIP 141 L63 BIP 141 L65 BIP 141 L67 BIP 141 L72 BIP 141 L76 BIP 141 L80
Quoted source text (6)
A new block rule is added which requires a commitment to the <code>wtxid</code>. The <code>wtxid</code> of coinbase transaction is assumed to be <code>0x0000....0000</code>.
A <code>witness root hash</code> is calculated with all those <code>wtxid</code> as leaves, in a way similar to the <code>hashMerkleRoot</code> in the block header.
The commitment is recorded in a <code>scriptPubKey</code> of the coinbase transaction.
32-byte - Commitment hash: Double-SHA256(witness root hash|witness reserved value)
the coinbase's input's witness must consist of a single 32-byte array for the <code>witness reserved value</code>.
If all transactions in a block do not have witness data, the commitment is optional.
-
↩ a b c d e f Test vector The native P2WPKH example spends one ordinary P2PK output and one P2WPKH output; the P2SH-P2WPKH example spends one P2SH-wrapped witness program.
BIP 143 L151 BIP 143 L155 BIP 143 L214
Quoted source text (3)
The first input comes from an ordinary P2PK:
The second input comes from a P2WPKH witness program:
The input comes from a P2SH-P2WPKH witness program:
-
↩ a b c Rule Witness validation is also triggered when a P2SH redeemScript pushed in the scriptSig consists exactly of a version push opcode, a one-byte push-length opcode and the witness-program bytes.
Quoted source text (2)
Triggered when a <code>scriptPubKey</code> is a P2SH script, and the BIP16 <code>redeemScript</code> pushed in the <code>scriptSig</code> is exactly a push of a version byte plus a push of a witness program.
* First, byte 0x00 (<code>OP_0</code>) or any byte between 0x51 (<code>OP_1</code>) and 0x60 (<code>OP_16</code>) inclusive (the version byte). * Then, a byte ''L'' between 0x02 (push of 2 bytes) and 0x28 (push of 40 bytes) inclusive. * Finally, ''L'' arbitrary bytes (the witness program).
-
↩ a b c Rule The BIP143 preimage has ten items: nVersion, hashPrevouts, hashSequence, outpoint, scriptCode, amount, nSequence, hashOutputs, nLocktime and sighash type.
BIP 143 L29 BIP 143 L34 BIP 143 L38
Quoted source text (3)
1. nVersion of the transaction (4-byte little endian)
6. value of the output spent by this input (8-byte little endian)
10. sighash type of the signature (4-byte little endian)
-
↩ a b c d e Rule Every BIP143 sighash type commits to the amount spent by the signed input; item 6 is that 8-byte amount.
Quoted source text (2)
All sighash types commit to the amount being spent by the signed input;
The item 6 is a 8-byte value of the amount of bitcoin spent in this input.
-
↩ a b c Rule Without ANYONECANPAY, hashPrevouts is the double SHA256 of all input outpoints; for SIGHASH_ALL, hashSequence covers all nSequence values and hashOutputs all outputs.
BIP 143 L58 BIP 143 L62 BIP 143 L66
Quoted source text (3)
If the <code>ANYONECANPAY</code> flag is not set, <code>hashPrevouts</code> is the double SHA256 of the serialization of all input outpoints;
If none of the <code>ANYONECANPAY</code>, <code>SINGLE</code>, <code>NONE</code> sighash type is set, <code>hashSequence</code> is the double SHA256 of the serialization of <code>nSequence</code> of all inputs;
If the sighash type is neither <code>SINGLE</code> nor <code>NONE</code>, <code>hashOutputs</code> is the double SHA256 of the serialization of all output amount (8-byte little endian) with <code>scriptPubKey</code> (serialized as scripts inside CTxOuts);
-
↩ Author’s rationale BIP141 says this allows unconfirmed transaction dependency chains without counterparty risk, important for off-chain protocols such as the Lightning Network.
Quoted source text (1)
It allows creation of unconfirmed transaction dependency chains without counterparty risk, an important feature for offchain protocols such as the Lightning Network
-
↩ Rule A version 0, 32-byte program is P2WSH: the witness is an input stack for the script followed by the serialized script itself.
Quoted source text (2)
It is interpreted as a pay-to-witness-script-hash (P2WSH) program.
The witness must consist of an input stack to feed to the script, followed by a serialized script (<code>witnessScript</code>).
-
↩ Author’s rationale BIP141's motivation lists that moving data into a structure unknown to old nodes lets a soft fork discount witness size when calculating block size.
Quoted source text (1)
'''Some constraints could be bypassed with a soft fork''' by moving part of the transaction data to a structure unknown to current protocol, for example: #* Size of witness could be ignored / discounted when calculating the block size, effectively increasing the block size to some extent
-
↩ History When BIP141 was written, blocks were limited to 1,000,000 bytes total size.
Quoted source text (1)
Blocks are currently limited to 1,000,000 bytes (1MB) total size.
-
↩ a b c Rule Block weight is base size × 3 + total size, and must be at most 4,000,000; base size excludes witness-related data as a non-upgraded node sees it.
BIP 141 L117 BIP 141 L119 BIP 141 L123
Quoted source text (3)
''Block weight'' is defined as ''Base size'' * 3 + ''Total size''.
''Base size'' is the block size in bytes with the original transaction serialization without any witness-related data, as seen by a non-upgraded node.
The new rule is ''block weight'' ≤ 4,000,000.
-
↩ a b Editorial inference Since weight = 3 × base + total = 4 × base + (total − base), each base byte adds 4 weight units and each witness-related byte (marker, flag, witness) adds 1.
Quoted source text (2)
''Block weight'' is defined as ''Base size'' * 3 + ''Total size''.
including base data and witness data.
-
↩ Author’s rationale BIP141 chose one composite limit because two separate limits would make mining and fee estimation nearly impossible.
Quoted source text (1)
Using two separate limits would make mining and fee estimation nearly impossible.
-
↩ Editorial inference Because weight = 4 × base + witness ≤ 4,000,000, a block's base data cannot exceed 1,000,000 bytes; only witness data lets its total size exceed that.
Quoted source text (2)
''Block weight'' is defined as ''Base size'' * 3 + ''Total size''.
The new rule is ''block weight'' ≤ 4,000,000.
-
↩ a b Rule Transaction weight is defined the same way, and virtual size is weight / 4 rounded up; these are suggested terms, not consensus limits.
BIP 141 L136 BIP 141 L140 BIP 141 L142
Quoted source text (3)
The following definitions are not used for consensus limits, but are suggested to provide language consistent with the terminology introduced above.
''Transaction weight'' is defined as ''Base transaction size'' * 3 + ''Total transaction size''
''Virtual transaction size'' is defined as ''Transaction weight'' / 4 (rounded up to the next integer).
-
↩ a b Test vector Parsed, the native P2WPKH example has 233 base bytes and 343 bytes in total: weight 1,042, virtual size 261.
Quoted source text (2)
The serialized signed transaction is:
''Transaction weight'' is defined as ''Base transaction size'' * 3 + ''Total transaction size''
-
↩ a b c Test vector The tested model reproduces both published examples' serializations, preimages and sighashes byte for byte, and computes their sizes and weights.
Quoted source text (2)
hash preimage:
sigHash:
-
↩ Rule BIP143 defines a new digest for signatures in version 0 witness programs, to minimize redundant hashing and to cover the input value.
Quoted source text (1)
This proposal defines a new transaction digest algorithm for signature verification in version 0 witness program, in order to minimize redundant data hashing in verification, and to cover the input value by the signature.
-
↩ a b Author’s rationale With the original digest, hashing per signature grows with transaction size, so total hashing grows as O(n^2) in the number of sigops.
Quoted source text (1)
For the verification of each signature, the amount of data hashing is proportional to the size of the transaction. Therefore, data hashing grows in O(n2) as the number of sigops in a transaction increases.
-
↩ a b c Rule hashPrevouts, hashSequence and hashOutputs can be reused across inputs, reducing total hashing from O(n^2) to O(n).
Quoted source text (1)
may be reused in other inputs of the same transaction, so that the time complexity of the whole hashing process reduces from O(n2) to O(n).
-
↩ a b Rule For P2WPKH the scriptCode is 0x1976a914{20-byte-pubkey-hash}88ac.
Quoted source text (1)
For <code>P2WPKH</code> witness program, the <code>scriptCode</code> is <code>0x1976a914{20-byte-pubkey-hash}88ac</code>.
-
↩ a b Author’s rationale Without the amount, an offline signer cannot compute the amount spent or the fee; with the input's amount in its digest, a wrong value makes that signature invalid and no funding might be lost.
Quoted source text (2)
For an offline transaction signing device ("cold wallet"), however, the unknowing of input amount makes it impossible to calculate the exact amount being spent and the transaction fee.
In the case that a wrong value is provided and signed, the signature would be invalid and no funding might be lost.
-
↩ Rule The BIP143 digest applies only to sigops in version 0 witness programs.
Quoted source text (2)
A new transaction digest algorithm is defined, but only applicable to sigops in version 0 witness program:
a new transaction digest algorithm for signature verification in version 0 witness program.
-
↩ Author’s rationale The original signature digest does not involve the amount being spent by the input.
Quoted source text (1)
The algorithm does not involve the amount of Bitcoin being spent by the input.