<blockquote>
<p>hashOutputs = sha256²(20120a0000000000a91417b9a9afddaae527d25788bce2202563d4ab0d058784ea1100000000000020701a8d401c84fb13e6baf169d59684e17abd9fa216c8cc5b9fc63d622ff8c58d)</p>
</blockquote>
<p>This value is incorrect. The following values are correct.</p>
<p>hashOutputs = sha256²(20120a000000000017a91417b9a9afddaae527d25788bce2202563d4ab0d058784ea110000000000220020701a8d401c84fb13e6baf169d59684e17abd9fa216c8cc5b9fc63d622ff8c58d)</p>
<p>hashOutputs: 787ea14bb2276614a291caeadbadb8155b4491f454988aab8919ffbfd6fc83a5</p>
<p>As a result, the message hash is:</p>
<p>989f5760931156cf50fba49c7d9e7b79e785cf340b9775d1a8931bc5e13197f7</p>

CC BY-SA 4.0
2021-01-13T05:53:15.323Z
2021-01-13T05:53:15.323Z
Shigeyuki Azuchi
<p>Also, for the script code, i believe you need to preceed the scriptcode with a byte which indicates the length of the script code,as per <a href="https://github.com/bitcoin/bips/blob/master/bip-0143.mediawiki" rel="nofollow noreferrer">BIP143</a></p>

CC BY-SA 4.0
2024-04-21T23:31:44.440Z
2024-04-21T23:31:44.440Z
Alain. Jr
<p>To test my understanding of the signing and verifying process i tried to verify some random Bitcoin transactions by hand. Sadly i got stuck with a P2WSH transaction, i choose the following transaction with Hash <a href="https://www.blockchain.com/btc/tx/c53b99c9fdba60fd47c6026177d3f6e1ed6d3abde8f433364619aa7d437dad26" rel="nofollow noreferrer">c53b99c9fdba60fd47c6026177d3f6e1ed6d3abde8f433364619aa7d437dad26</a></p>
<p>With the raw transaction:</p>
<blockquote>
<p>010000000001012198e5bc0860a4fcf420e2b909fce47e746357457b060c63571e12bd84fec4a70100000000ffffffff0220120a000000000017a91417b9a9afddaae527d25788bce2202563d4ab0d058784ea110000000000220020701a8d401c84fb13e6baf169d59684e17abd9fa216c8cc5b9fc63d622ff8c58d0400473044022014cd600863ad3c9f6802383fe814a693a77144117cf7694f63b558b8c02d801c02201c3ad9901f659742668caf770f3d7f89a3633f9ccd2bfdd6a7c6f7529fe7b43101473044022047e4ad9788da6b764e723dd71d9fe07a217e48871129a672ef2788eb0d331a7a02206107b10c23e8720df9f1e5f609c471726066c5b72c56ee774615498f8fe62e8d016952210375e00eb72e29da82b89367947f29ef34afb75e8654f6ea368e0acdfd92976b7c2103a1b26313f430c4b15bb1fdce663207659d8cac749a0e53d70eff01874496feff2103c96d495bfdd5ba4145e3e046fee45e84a8a48ad05bd8dbb395c011a32cf9f88053ae00000000</p>
</blockquote>
<p>I can deserialize this to:</p>
<hr />
<p>TX version number = 00000001<br />
Inputs count = 01<br />
TX from hash = a7c4fe84bd121e57630c067b455763747ee4fc09b9e220f4fca46008bce59821<br />
TX outpoint Index = 00000001<br />
Input script =<br />
Sequence number = ffffffff<br />
Outputs count = 02<br />
Value(base 10) = 660000<br />
Output script = a91417b9a9afddaae527d25788bce2202563d4ab0d0587<br />
Value(base 10) = 1174148<br />
Output script = 0020701a8d401c84fb13e6baf169d59684e17abd9fa216c8cc5b9fc63d622ff8c58d<br />
Witness 0 0 = 00<br />
Witness 0 1 = 473044022014cd600863ad3c9f6802383fe814a693a77144117cf7694f63b558b8c02d801c02201c3ad9901f659742668caf770f3d7f89a3633f9ccd2bfdd6a7c6f7529fe7b43101<br />
Witness 0 2 = 473044022047e4ad9788da6b764e723dd71d9fe07a217e48871129a672ef2788eb0d331a7a02206107b10c23e8720df9f1e5f609c471726066c5b72c56ee774615498f8fe62e8d01<br />
Witness 0 3 = 6952210375e00eb72e29da82b89367947f29ef34afb75e8654f6ea368e0acdfd92976b7c2103a1b26313f430c4b15bb1fdce663207659d8cac749a0e53d70eff01874496feff2103c96d495bfdd5ba4145e3e046fee45e84a8a48ad05bd8dbb395c011a32cf9f88053ae<br />
Lock time = 00000000</p>
<hr />
<p>Now i try to compute the MessageHash that gets signed:</p>
<p>with:<br />
hashPrevouts = sha256²(2198e5bc0860a4fcf420e2b909fce47e746357457b060c63571e12bd84fec4a701000000)<br />
hashSequence = sha256²(ffffffff)<br />
hashOutputs = sha256²(20120a0000000000a91417b9a9afddaae527d25788bce2202563d4ab0d058784ea1100000000000020701a8d401c84fb13e6baf169d59684e17abd9fa216c8cc5b9fc63d622ff8c58d)</p>
<p>Version:  01000000<br />
hashPrevouts:  65aa319c96c046e8589c758adf7441d335682d3fc76df649180c4f94a1c4a731<br />
hashSequence:  3bb13029ce7b1f559ef5e747fcac439f1455a2ec7c5f09b72290795e70665044<br />
outPoint:  2198e5bc0860a4fcf420e2b909fce47e746357457b060c63571e12bd84fec4a701000000<br />
scriptCode:  6952210375e00eb72e29da82b89367947f29ef34afb75e8654f6ea368e0acdfd92976b7c2103a1b26313f430c4b15bb1fdce663207659d8cac749a0e53d70eff01874496feff2103c96d495bfdd5ba4145e3e046fee45e84a8a48ad05bd8dbb395c011a32cf9f88053ae<br />
value:  345c1d0000000000<br />
nSequence:  ffffffff<br />
hashOutputs:  58d6a86eb99a911735d2f262a9fc13fcf0f8422d75e25ba04db120f65cf573d6<br />
nLocktime:  00000000<br />
sighash:  01000000</p>
<hr />
<p>This will give me the MessageHash = 8560e61d0167f784d0cdaf9d45ba0e1be752e94d30a34339d976b01e50f0e366</p>
<p>But if i try to verify this MessageHash with the first Signature(473044022014cd600863ad3c9f6802383fe814a693a77144117cf7694f63b558b8c02d801c02201c3ad9901f659742668caf770f3d7f89a3633f9ccd2bfdd6a7c6f7529fe7b43101), which has     R = 14cd600863ad3c9f6802383fe814a693a77144117cf7694f63b558b8c02d801c and S = 1c3ad9901f659742668caf770f3d7f89a3633f9ccd2bfdd6a7c6f7529fe7b431 against any of the 3 PubKeys in Witness 0 3, i always get the the Signature is not valid.</p>
<p>Can someone tell me where i got wrong ?</p>

<bitcoin-core><transaction-verification><bech32-address><p2wsh>
Verifying a P2WSH by hand
ASP
# Verifying a P2WSH by hand

(Asked by: username_0 on 2021-01-13)

To test my understanding of the signing and verifying process i tried to verify some random Bitcoin transactions by hand. Sadly i got stuck with a P2WSH transaction, i choose the following transaction with Hash [c53b99c9fdba60fd47c6026177d3f6e1ed6d3abde8f433364619aa7d437dad26](https://www.blockchain.com/btc/tx/c53b99c9fdba60fd47c6026177d3f6e1ed6d3abde8f433364619aa7d437dad26)

With the raw transaction:

> 010000000001012198e5bc0860a4fcf420e2b909fce47e746357457b060c63571e12bd84fec4a70100000000ffffffff0220120a000000000017a91417b9a9afddaae527d25788bce2202563d4ab0d058784ea110000000000220020701a8d401c84fb13e6baf169d59684e17abd9fa216c8cc5b9fc63d622ff8c58d0400473044022014cd600863ad3c9f6802383fe814a693a77144117cf7694f63b558b8c02d801c02201c3ad9901f659742668caf770f3d7f89a3633f9ccd2bfdd6a7c6f7529fe7b43101473044022047e4ad9788da6b764e723dd71d9fe07a217e48871129a672ef2788eb0d331a7a02206107b10c23e8720df9f1e5f609c471726066c5b72c56ee774615498f8fe62e8d016952210375e00eb72e29da82b89367947f29ef34afb75e8654f6ea368e0acdfd92976b7c2103a1b26313f430c4b15bb1fdce663207659d8cac749a0e53d70eff01874496feff2103c96d495bfdd5ba4145e3e046fee45e84a8a48ad05bd8dbb395c011a32cf9f88053ae00000000

I can deserialize this to:

---

TX version number = 00000001  
Inputs count = 01  
TX from hash = a7c4fe84bd121e57630c067b455763747ee4fc09b9e220f4fca46008bce59821  
TX outpoint Index = 00000001  
Input script =  
Sequence number = ffffffff  
Outputs count = 02  
Value(base 10) = 660000  
Output script = a91417b9a9afddaae527d25788bce2202563d4ab0d0587  
Value(base 10) = 1174148  
Output script = 0020701a8d401c84fb13e6baf169d59684e17abd9fa216c8cc5b9fc63d622ff8c58d  
Witness 0 0 = 00  
Witness 0 1 = 473044022014cd600863ad3c9f6802383fe814a693a77144117cf7694f63b558b8c02d801c02201c3ad9901f659742668caf770f3d7f89a3633f9ccd2bfdd6a7c6f7529fe7b43101  
Witness 0 2 = 473044022047e4ad9788da6b764e723dd71d9fe07a217e48871129a672ef2788eb0d331a7a02206107b10c23e8720df9f1e5f609c471726066c5b72c56ee774615498f8fe62e8d01  
Witness 0 3 = 6952210375e00eb72e29da82b89367947f29ef34afb75e8654f6ea368e0acdfd92976b7c2103a1b26313f430c4b15bb1fdce663207659d8cac749a0e53d70eff01874496feff2103c96d495bfdd5ba4145e3e046fee45e84a8a48ad05bd8dbb395c011a32cf9f88053ae  
Lock time = 00000000

---

Now i try to compute the MessageHash that gets signed:

with:  
hashPrevouts = sha256²(2198e5bc0860a4fcf420e2b909fce47e746357457b060c63571e12bd84fec4a701000000)  
hashSequence = sha256²(ffffffff)  
hashOutputs = sha256²(20120a0000000000a91417b9a9afddaae527d25788bce2202563d4ab0d058784ea1100000000000020701a8d401c84fb13e6baf169d59684e17abd9fa216c8cc5b9fc63d622ff8c58d)

Version: 01000000  
hashPrevouts: 65aa319c96c046e8589c758adf7441d335682d3fc76df649180c4f94a1c4a731  
hashSequence: 3bb13029ce7b1f559ef5e747fcac439f1455a2ec7c5f09b72290795e70665044  
outPoint: 2198e5bc0860a4fcf420e2b909fce47e746357457b060c63571e12bd84fec4a701000000  
scriptCode: 6952210375e00eb72e29da82b89367947f29ef34afb75e8654f6ea368e0acdfd92976b7c2103a1b26313f430c4b15bb1fdce663207659d8cac749a0e53d70eff01874496feff2103c96d495bfdd5ba4145e3e046fee45e84a8a48ad05bd8dbb395c011a32cf9f88053ae  
value: 345c1d0000000000  
nSequence: ffffffff  
hashOutputs: 58d6a86eb99a911735d2f262a9fc13fcf0f8422d75e25ba04db120f65cf573d6  
nLocktime: 00000000  
sighash: 01000000

---

This will give me the MessageHash = 8560e61d0167f784d0cdaf9d45ba0e1be752e94d30a34339d976b01e50f0e366

But if i try to verify this MessageHash with the first Signature(473044022014cd600863ad3c9f6802383fe814a693a77144117cf7694f63b558b8c02d801c02201c3ad9901f659742668caf770f3d7f89a3633f9ccd2bfdd6a7c6f7529fe7b43101), which has R = 14cd600863ad3c9f6802383fe814a693a77144117cf7694f63b558b8c02d801c and S = 1c3ad9901f659742668caf770f3d7f89a3633f9ccd2bfdd6a7c6f7529fe7b431 against any of the 3 PubKeys in Witness 0 3, i always get the the Signature is not valid.

Can someone tell me where i got wrong ?

**Tags:** bitcoin-core, transaction-verification, bech32-address, p2wsh


---

## 2 answers

### Answer by username_1 on 2021-01-13. Score: 0 | Accepted answer

> hashOutputs = sha256²(20120a0000000000a91417b9a9afddaae527d25788bce2202563d4ab0d058784ea1100000000000020701a8d401c84fb13e6baf169d59684e17abd9fa216c8cc5b9fc63d622ff8c58d)

This value is incorrect. The following values are correct.

hashOutputs = sha256²(20120a000000000017a91417b9a9afddaae527d25788bce2202563d4ab0d058784ea110000000000220020701a8d401c84fb13e6baf169d59684e17abd9fa216c8cc5b9fc63d622ff8c58d)

hashOutputs: 787ea14bb2276614a291caeadbadb8155b4491f454988aab8919ffbfd6fc83a5

As a result, the message hash is:

989f5760931156cf50fba49c7d9e7b79e785cf340b9775d1a8931bc5e13197f7


---

### Answer by username_2 on 2024-04-21. Score: 0

Also, for the script code, i believe you need to preceed the scriptcode with a byte which indicates the length of the script code,as per [BIP143](https://github.com/bitcoin/bips/blob/master/bip-0143.mediawiki)


---

