Skip to content
BIP ATLAS

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 revision
bitcoin/bips @ 3a10b5b
Source checked
1 October 2026
Review state
Awaiting review
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

Pinned source · Reader view on bips.dev
SHA-256 d72409335872a32494f75e7547589abf4b3b23b6beebe6bc6999c7a84b135e2c
Git blob 442d9dd8bfc9816414ebc129dacd7e462f5d8c8a

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

Pinned source · Reader view on bips.dev
SHA-256 62bc71351563e68baeb12643c68355d217953ae9eb6a6e68b2b0323275b6beec
Git blob 5d451c221fa9ef052325c42086b8a7097b942556

Fig. A03.1 One transaction, two serializations

  1. Two serializations, two hashes, step 1 of 4A tape of 343 bytes: nVersion 4 bytes, marker + flag 2 bytes, inputs 156 bytes, outputs 69 bytes, witness 108 bytes, nLockTime 4 bytes. Double SHA-256 of all of it is the wtxid, 62b709c9126ae7782ea86576b338f39211199d149dced92423df070937386cc3.INPUTSOUTPUTSWITNESSNVERSION · 4 BNLOCKTIME · 4 BEVERYTHING · 343 BSHA-256twiceWTXID62b709c9126a…

    The whole serialization, 343 bytes, hashed twice with SHA-256: the wtxid.

  2. Two serializations, two hashes, step 2 of 4The marker 00 and flag 01, 2 bytes right after nVersion, are lifted out of the tape.0001INPUTSOUTPUTSWITNESSNVERSION · 4 BNLOCKTIME · 4 BMARKER · FLAG · 2 BLEFT OUT OF THE TXID

    Cut out the marker and flag (00 01), the 2 bytes after nVersion.

  3. Two serializations, two hashes, step 3 of 4The witness, 108 bytes just before nLockTime, is lifted out as well.0001INPUTSOUTPUTSWITNESSNVERSION · 4 BNLOCKTIME · 4 BWITNESS · 108 B110 B LEFT OUT IN ALL

    Cut out the witness, the 108 bytes just before nLockTime.

  4. Two serializations, two hashes, step 4 of 4The remaining 233 bytes, nVersion, inputs, outputs and nLockTime, are what a pre-SegWit node sees. Double SHA-256 of them is the txid, 09462d604a3102fae083ada369c795855a28db4bdd3d05358a361cf32a1a15e8.INPUTSOUTPUTSNVERSION · 4 BNLOCKTIME · 4 BOLD SERIALIZATION · 233 BSHA-256twiceTXID09462d604a31…

    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
The native P2WPKH example from BIP 143, drawn to scale. All of it hashes to the wtxid; without the marker, flag and witness it hashes to the txid. Hashes are shown in the byte order they are computed.567

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

Caveat

The witness is not taken off the blockchain. Blocks with witness data commit to it, and any node that validates a transaction needs it. Only a peer that merely checks that a transaction exists can skip downloading it.89

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

The witness section, byte by byteThe witness section of the Legacy + SegWit inputs example, 108 bytes, one field per input. Input 0: empty, the single byte 00. Input 1: count 2, then a length byte and item 0 of 71 bytes, a length byte and item 1 of 33 bytes. The key hashes with HASH160 to the 20-byte program 1d0f172a0ecb48aee1be1f2687d2963ae33f71a1.WITNESS SECTION · 108 BYTES00INPUT 0EMPTY0247signature · 71 B21key 33 BINPUT 1 · 2 ITEMSCOUNTLEN 71LEN 33HASH160= THE 20-BYTE PROGRAM1d0f172a0ecb48ae…THE PROGRAM IS IN THE OUTPUT BEING SPENT
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
The mixed example’s witness section: an empty field for the ordinary input, then a count, two lengths, a signature and a key for the SegWit input. The key hashes to the program it spends.101112

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

Two trees in one blockA schematic block of four transactions, one of them the published example. The txids, 09462d60… among them, are hashed pairwise into the merkle root in the block header. The wtxids, 62b709c9… among them and with the coinbase's taken as 32 zero bytes, are hashed the same way into a witness root. The double SHA-256 of that root followed by a 32-byte reserved value, which sits in the coinbase input’s witness, is the commitment recorded in an output of the coinbase transaction, which is itself one of the txid leaves.BLOCK HEADERMERKLE ROOTTXIDSCOINBASEEXAMPLE09462d60…OTHER TXOTHER TXCOINBASE OUTPUTCOMMITMENT = SHA-256 TWICE OFWITNESS ROOT ‖ RESERVED VALUE(32 B, IN THE COINBASE’S WITNESS)WTXIDSCOINBASE00…00EXAMPLE62b709c9…OTHER TXOTHER TX
The example's identifiers (byte order as computed)
txid
09462d604a3102fae083ada369c795855a28db4bdd3d05358a361cf32a1a15e8
wtxid
62b709c9126ae7782ea86576b338f39211199d149dced92423df070937386cc3
The coinbase's wtxid, as BIP 141 defines it
0000000000000000000000000000000000000000000000000000000000000000
A schematic block of four transactions; the example is not in a real block. The txids feed the header as before; the wtxids, the coinbase’s taken as zeros, feed a commitment in a coinbase output, together with a reserved value.5813

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.

Which bytes each hash coversLegacy + SegWit inputs: a serialized transaction of 343 bytes. Fields in order: nVersion, 4 bytes; marker, 1 byte; flag, 1 byte; input count, 1 byte; input 0 · previous txid, 32 bytes; input 0 · output index, 4 bytes; input 0 · scriptSig length, 1 byte; input 0 · scriptSig push opcode, 1 byte; input 0 · scriptSig: the signature, 72 bytes; input 0 · nSequence, 4 bytes; input 1 · previous txid, 32 bytes; input 1 · output index, 4 bytes; input 1 · scriptSig length, 1 byte; input 1 · nSequence, 4 bytes; output count, 1 byte; output 0 · value, 8 bytes; output 0 · script length, 1 byte; output 0 · scriptPubKey, 25 bytes; output 1 · value, 8 bytes; output 1 · script length, 1 byte; output 1 · scriptPubKey, 25 bytes; witness 0 · item count, 1 byte; witness 1 · item count, 1 byte; witness 1 · item 0 length, 1 byte; witness 1 · signature, 71 bytes; witness 1 · item 1 length, 1 byte; witness 1 · public key, 33 bytes; nLockTime, 4 bytes. txid: double SHA-256 of the 233 bytes a pre-SegWit node sees. The marker, flag and witness (110 bytes) are left out. Result: 09462d604a3102fae083ada369c795855a28db4bdd3d05358a361cf32a1a15e8.LEGACY + SEGWIT INPUTS · 343 BYTES0001MARKER · FLAGNOT IN THE TXID0816243240previous txid…scriptSig · signature…scriptSig · signatureprevious txidindex…valuescriptPubKey…scriptPubKeysignature…signature…signaturepublic key…public keySHA-256twiceTXID09462d604a3102fa…233 OF 343 BYTESHASH · TXIDSIGNATUREPUBLIC KEYNSEQUENCE · NLOCKTIMENOT IN THE TXID
Which bytes each hash coversLegacy + SegWit inputs: a serialized transaction of 343 bytes. Fields in order: nVersion, 4 bytes; marker, 1 byte; flag, 1 byte; input count, 1 byte; input 0 · previous txid, 32 bytes; input 0 · output index, 4 bytes; input 0 · scriptSig length, 1 byte; input 0 · scriptSig push opcode, 1 byte; input 0 · scriptSig: the signature, 72 bytes; input 0 · nSequence, 4 bytes; input 1 · previous txid, 32 bytes; input 1 · output index, 4 bytes; input 1 · scriptSig length, 1 byte; input 1 · nSequence, 4 bytes; output count, 1 byte; output 0 · value, 8 bytes; output 0 · script length, 1 byte; output 0 · scriptPubKey, 25 bytes; output 1 · value, 8 bytes; output 1 · script length, 1 byte; output 1 · scriptPubKey, 25 bytes; witness 0 · item count, 1 byte; witness 1 · item count, 1 byte; witness 1 · item 0 length, 1 byte; witness 1 · signature, 71 bytes; witness 1 · item 1 length, 1 byte; witness 1 · public key, 33 bytes; nLockTime, 4 bytes. txid: double SHA-256 of the 233 bytes a pre-SegWit node sees. The marker, flag and witness (110 bytes) are left out. Result: 09462d604a3102fae083ada369c795855a28db4bdd3d05358a361cf32a1a15e8.LEGACY + SEGWIT INPUTS · 343 BYTES0001MARKER · FLAGNOT IN THE TXID081624previous txid…indexscriptSig · signature…scriptSig · signature…scriptSig · signatureprevious txid…indexvaluescriptPubKey…scriptPubKeyvalue…scriptPubKeysignature…signature…signature…public key…public keySHA-256twiceTXID09462d604a3102fa…233 OF 343 BYTESHASH · TXIDSIGNATUREPUBLIC KEYNSEQUENCE · NLOCKTIMENOT IN THE TXID

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.

Through the txid lens the marker, flag and witness are hatched out, and with them the SegWit input’s signature. In the first example the ordinary input’s signature sits in its scriptSig, so the txid still covers it. The BIP 143 lens numbers the bytes behind each preimage item.511121415161718

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

One input with a scriptSig and a witnessInput 0 of the SegWit inside P2SH example. Its scriptSig is 23 bytes: the push opcode 16 and the 22-byte redeem script 001479091972186c449eb1ded22b78e40d009bdf0089: the witness version byte 00, then a push of 20 bytes (14) of the witness program, a key hash. Its witness holds a 71-byte signature and a 33-byte public key, whose HASH160 is that key hash.INPUT 0 · SCRIPTSIG · 23 B160014program · 20 BREDEEM SCRIPT · 22 BPUSH OF 22 BYTESWITNESS VERSION 0PUSH OF THE 20-BYTE PROGRAMINPUT 0 · WITNESS · 2 ITEMS0247signature · 71 B21key 33 BCOUNTLEN 71LEN 33HASH160= THE 20-BYTE PROGRAM79091972186c449e…THE SAME KEY HASH THE SCRIPTSIG PUSHES
Exact scriptSig and witness
scriptSig (23 bytes)
16001479091972186c449eb1ded22b78e40d009bdf0089
Witness item 0 (71 bytes)
3044022047ac8e878352d3ebbde1c94ce3a10d057c24175747116f8288e5d794d12d482f0220217f36a485cae903c713331d877c1f64677e3622ad4010726870540656fe9dcb01
Witness item 1 (33 bytes)
03ad1d8e89212f0b92c74d23bb710c00662ad1470198ac48c43f7d6f93a2a26873
The P2SH-P2WPKH example’s only input. Its scriptSig pushes a 22-byte redeem script: OP_0 (hex 00), the one-byte push-length opcode (hex 14), and the 20-byte witness program. The witness holds the signature and the public key.121415

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

Bytes into weightLegacy + SegWit inputs: 233 base bytes count 4 each, 932 units; 110 witness-related bytes count 1 each, 110 units; weight 3 × 233 + 343 = 1042, virtual size 261. SegWit inside P2SH: 142 base bytes count 4 each, 568 units; 109 witness-related bytes count 1 each, 109 units; weight 3 × 142 + 251 = 677, virtual size 170.LEGACY + SEGWIT INPUTS · P2PK AND P2WPKHBASE 233 B + 110 B WITNESS-RELATEDEACH × 4× 1233 × 4 = 932110WEIGHT 3 × 233 + 343 = 1,042 WU · ⌈1,042 ÷ 4⌉ = 261 vBSEGWIT INSIDE P2SH · P2SH-P2WPKHBASE 142 B + 109 B WITNESS-RELATEDEACH × 4× 1142 × 4 = 568109WEIGHT 3 × 142 + 251 = 677 WU · ⌈677 ÷ 4⌉ = 170 vB
Both examples on one scale. Each base byte is stretched four times; each witness-related byte, marker and flag included, counts once. The totals are computed from the parsed transactions, not estimated.724272829

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

Hashing once, or once per inputSchematic, four inputs. Original digest: each of the four signature checks hashes its own copy of the whole transaction, so the hashing grows with the number of inputs times the size of the transaction. BIP 143: hashPrevouts, hashSequence and hashOutputs can each be computed once from all the outpoints, all the nSequence values and all the outputs; each input’s preimage combines those three summaries with that input’s own items, has the same fixed shape for every input, and is then hashed: one fixed-size hash per input.ORIGINAL DIGESTTHE WHOLE TRANSACTIONINPUT 0INPUT 1INPUT 2INPUT 3EACH CHECK HASHES A COPY OF THE WHOLE TXBIP 143THE WHOLE TRANSACTIONALL OUTPOINTShashPrevoutsALL NSEQUENCEShashSequenceALL OUTPUTShashOutputsPREIMAGEINPUT 0PREIMAGEINPUT 1PREIMAGEINPUT 2PREIMAGEINPUT 3
Schematic, with no values: four inputs under each digest. BIP 143’s three summary hashes can be made once and shared by every input’s preimage; each preimage is then hashed on its own.183132

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

What a SegWit signature signsThe BIP 143 preimage for input 1 of the Legacy + SegWit inputs example, SIGHASH_ALL, 182 bytes in 10 items: 1 nVersion, 4 bytes, copied from the transaction; 2 hashPrevouts, 32 bytes, double SHA-256 of every input’s outpoint; 3 hashSequence, 32 bytes, double SHA-256 of every input’s nSequence; 4 outpoint, 36 bytes, the input being signed; 5 scriptCode, 26 bytes, built from the spent output’s 20-byte program, which is not in this transaction (BIP 143 line 180); 6 amount, 8 bytes, not in this transaction: the value of the output being spent (BIP 143 line 156); 7 nSequence, 4 bytes, the input being signed; 8 hashOutputs, 32 bytes, double SHA-256 of every output; 9 nLockTime, 4 bytes, copied from the transaction; 10 sighash type, 4 bytes, SIGHASH_ALL, chosen by the signer. The amount is 6.00000000 BTC. The double SHA-256 of the preimage is the sighash c37af31116d1b27caf68aae9e3ac82f1477929014d5b917657d0eb49478cb670, the value BIP 143 publishes.INPUT 1 · SIGHASH_ALL · 10 ITEMS · 182 BYTES08162432hashPrevouts…hashSequence…outpoint…outpointscriptCode…amounthashOutputs…hashOutputs123456789101nVersion · 4 B2hashPrevouts · 32 B3hashSequence · 32 B4outpoint · 36 B5scriptCode · 26 B6amount · 8 B7nSequence · 4 B8hashOutputs · 32 B9nLockTime · 4 B10sighash type · 4 BDASHED: NOT A FIELD OF THE TRANSACTIONOUTLINED: AMOUNT SPENT, 6.00000000 BTCdouble SHA-256 of the preimage =c37af31116d1b27caf68aae9e3ac82f1477929014d5b917657d0eb49478cb670THE SIGHASH PUBLISHED AT BIP 143 LINE 187
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 ten items for the mixed example’s SegWit input, on a 32-byte ruler. Dashed items are not fields of the transaction; the amount is outlined. The digest matches the one BIP 143 publishes.16172933

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

  1. Why the signature covers the amount, step 1 of 3An offline signer holds the unsigned transaction. Input 1 names the output it spends by previous txid ef51e1b804cc89d182d279655c3aa89e815b1b309fe287d9b2b55d57b90ec68a and index; nothing in the transaction says how much that output holds.OFFLINE SIGNERprev txid ef51e1b8…idxAMOUNT: NOT GIVENINPUT 1 · OUTPOINT ONLY

    The offline signer sees input 1’s outpoint, a reference to an earlier output. The amount it spends is not in the transaction.

  2. Why the signature covers the amount, step 2 of 3The amount 6.00000000 BTC is supplied with the transaction and becomes item 6 of the BIP 143 preimage. The double SHA-256 of the preimage is c37af31116d1b27caf68aae9e3ac82f1477929014d5b917657d0eb49478cb670.OFFLINE SIGNER6.00000000 BTC12345678910PREIMAGE ITEMSSIGHASHc37af31116…

    Told the amount, 6.00000000 BTC as published with the example, the signer puts it into item 6 and hashes the preimage.

  3. Why the signature covers the amount, step 3 of 3With a wrong amount in item 6 the preimage differs, so its digest differs from c37af31116d1b27caf68aae9e3ac82f1477929014d5b917657d0eb49478cb670, the digest nodes compute from the real amount; a signature over the wrong digest is invalid.OFFLINE SIGNERANY OTHER AMOUNT12345?78910PREIMAGE ITEMSDIGEST≠ c37af311…✗ SIGNATUREFAILS

    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
An offline signer sees which output an input spends, not how much it holds. The amount it is given goes into the digest, so a wrong one cannot produce a valid signature.1734

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.

  1. ↩ 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.

    BIP 141, line 22
  2. ↩ 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.

    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.

    BIP 141, line 26

    as long as all inputs are signed (with at least one CHECKSIG or CHECKMULTISIG operation)

    BIP 141, line 27

    In the case of an m-of-n CHECKMULTISIG script, a transaction is malleable only with agreement of m private key holders

    BIP 141, line 28
  3. ↩ History BIP141 (assigned 2015) and BIP143 (assigned 2016) are consensus soft-fork proposals whose preambles record the status Deployed.

    Quoted source text (5)

    Assigned: 2015-12-21

    BIP 141, line 10

    Layer: Consensus (soft fork)

    BIP 141, line 3

    Status: Deployed

    BIP 141, line 8

    Assigned: 2016-01-03

    BIP 143, line 9

    Status: Deployed

    BIP 143, line 7
  4. ↩ 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.

    BIP 141, line 16
  5. ↩ 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].

    Quoted source text (5)

    Each transaction will have 2 IDs.

    BIP 141, line 41

    Definition of <code>txid</code> remains unchanged: the double SHA256 of the traditional serialization format:

    BIP 141, line 43

    [nVersion][txins][txouts][nLockTime]

    BIP 141, line 45

    A new <code>wtxid</code> is defined: the double SHA256 of the new serialization with witness data:

    BIP 141, line 47

    [nVersion][marker][flag][txins][txouts][witness][nLockTime]

    BIP 141, line 49
  6. ↩ 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>.

    BIP 141, line 53

    The <code>flag</code> MUST be a 1-byte non-zero value. Currently, <code>0x01</code> MUST be used.

    BIP 141, line 55
  7. ↩ a b c d Test vector BIP143 publishes complete example transactions with their preimages and sighashes, and asks developers to test against them.

    Quoted source text (3)

    developers should test their implementations against all the tests below.

    BIP 143, line 137

    The serialized signed transaction is:

    BIP 143, line 190

    The serialized signed transaction is:

    BIP 143, line 250
  8. ↩ 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.

    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

    BIP 141, line 18

    A new block rule is added which requires a commitment to the <code>wtxid</code>.

    BIP 141, line 63

    If all transactions in a block do not have witness data, the commitment is optional.

    BIP 141, line 80
  9. ↩ 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.

    BIP 141, line 31
  10. ↩ 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.

    BIP 141, line 57
  11. ↩ 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>.

    BIP 141, line 59
  12. ↩ 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.

    Quoted source text (3)

    It is interpreted as a pay-to-witness-public-key-hash (P2WPKH) program.

    BIP 141, line 95

    The witness must consist of exactly 2 items (≤ 520 bytes each). The first one a signature, and the second one a public key.

    BIP 141, line 96

    The HASH160 of the public key must match the 20-byte witness program.

    BIP 141, line 97
  13. ↩ 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.

    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>.

    BIP 141, line 63

    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.

    BIP 141, line 65

    The commitment is recorded in a <code>scriptPubKey</code> of the coinbase transaction.

    BIP 141, line 67

    32-byte - Commitment hash: Double-SHA256(witness root hash|witness reserved value)

    BIP 141, line 72

    the coinbase's input's witness must consist of a single 32-byte array for the <code>witness reserved value</code>.

    BIP 141, line 76

    If all transactions in a block do not have witness data, the commitment is optional.

    BIP 141, line 80
  14. ↩ 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.

    Quoted source text (3)

    The first input comes from an ordinary P2PK:

    BIP 143, line 151

    The second input comes from a P2WPKH witness program:

    BIP 143, line 155

    The input comes from a P2SH-P2WPKH witness program:

    BIP 143, line 214
  15. ↩ 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.

    BIP 141, line 92

    * 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).

    BIP 141, lines 86–88
  16. ↩ a b c Rule The BIP143 preimage has ten items: nVersion, hashPrevouts, hashSequence, outpoint, scriptCode, amount, nSequence, hashOutputs, nLocktime and sighash type.

    Quoted source text (3)

    1. nVersion of the transaction (4-byte little endian)

    BIP 143, line 29

    6. value of the output spent by this input (8-byte little endian)

    BIP 143, line 34

    10. sighash type of the signature (4-byte little endian)

    BIP 143, line 38
  17. ↩ 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;

    BIP 143, line 42

    The item 6 is a 8-byte value of the amount of bitcoin spent in this input.

    BIP 143, line 55
  18. ↩ 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.

    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;

    BIP 143, line 58

    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;

    BIP 143, line 62

    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);

    BIP 143, line 66
  19. ↩ 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

    BIP 141, line 30
  20. ↩ 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.

    BIP 141, line 101

    The witness must consist of an input stack to feed to the script, followed by a serialized script (<code>witnessScript</code>).

    BIP 141, line 102
  21. ↩ 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

    BIP 141, lines 32–33
  22. ↩ 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.

    BIP 141, line 115
  23. ↩ 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.

    Quoted source text (3)

    ''Block weight'' is defined as ''Base size'' * 3 + ''Total size''.

    BIP 141, line 117

    ''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.

    BIP 141, line 119

    The new rule is ''block weight'' ≤ 4,000,000.

    BIP 141, line 123
  24. ↩ 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''.

    BIP 141, line 117

    including base data and witness data.

    BIP 141, line 121
  25. ↩ 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.

    BIP 141, line 117
  26. ↩ 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''.

    BIP 141, line 117

    The new rule is ''block weight'' ≤ 4,000,000.

    BIP 141, line 123
  27. ↩ 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.

    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.

    BIP 141, line 136

    ''Transaction weight'' is defined as ''Base transaction size'' * 3 + ''Total transaction size''

    BIP 141, line 140

    ''Virtual transaction size'' is defined as ''Transaction weight'' / 4 (rounded up to the next integer).

    BIP 141, line 142
  28. ↩ 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:

    BIP 143, line 190

    ''Transaction weight'' is defined as ''Base transaction size'' * 3 + ''Total transaction size''

    BIP 141, line 140
  29. ↩ 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:

    BIP 143, line 174

    sigHash:

    BIP 143, line 187
  30. ↩ 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.

    BIP 143, line 14
  31. ↩ 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.

    BIP 143, line 21
  32. ↩ 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).

    BIP 143, line 70
  33. ↩ 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>.

    BIP 143, line 50
  34. ↩ 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.

    BIP 143, line 22

    In the case that a wrong value is provided and signed, the signature would be invalid and no funding might be lost.

    BIP 143, line 22
  35. ↩ 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:

    BIP 143, line 27

    a new transaction digest algorithm for signature verification in version 0 witness program.

    BIP 141, line 152
  36. ↩ 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.

    BIP 143, line 22