Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: dbitcoin100k on November 06, 2019, 02:44:04 AM



Title: help generating "hashOutputs" for segwit tx
Post by: dbitcoin100k on November 06, 2019, 02:44:04 AM
I'm trying to sign a segwit transaction but I need help generating the signature. Specifically, I'm getting stuck on the bit where I have to generate hashOutputs.

Since my transaction has two outputs, what do I hash exactly?

I'm doing SIGHASH_ALL, so I believe this is the relevant bit from bip143, but I have no idea what it means:

"If the sighash type is neither SINGLE nor NONE, hashOutputs is the double SHA256 of the serialization of all output amount (8-byte little endian) with scriptPubKey (serialized as scripts inside CTxOuts);"

"all output amount" - so do I add up the total value of the outputs?
Do I just put one scriptPubKey after the other?

Thanks


Title: Re: help generating "hashOutputs" for segwit tx
Post by: achow101 on November 06, 2019, 03:41:54 AM
You serialize it as the outputs one after another, just like they are in the transaction. So it would be the first output's value, then it's scriptPubKey, then the second output value and it's scriptPubKey, and so on.


Title: Re: help generating "hashOutputs" for segwit tx
Post by: pooya87 on November 06, 2019, 03:43:54 AM
"all output amount" - so do I add up the total value of the outputs?
Do I just put one scriptPubKey after the other?

you have to "concatenate" the bytes together.
so your "hashOutputs" is hash of the serialized TxOuts concatenated together. so for your 2 outputs it will be:
Code:
SHA256_d(amount1 || scriptPub1 || amount2 || scrioptPub2);
from the first example of BIP-143 (https://github.com/bitcoin/bips/blob/master/bip-0143.mediawiki):
output 1:
    amount = 202cb20600000000
    scriptPub = 1976a9148280b37df378db99f66f85c95a783a76ac7a6d5988ac
output 2:
    amount = 9093510d00000000
    scriptPub = 1976a9143bde42dbee7e4dbe6a21b2d50ce2f0167faa815988ac
Code:
SHA256_d(202cb206000000001976a9148280b37df378db99f66f85c95a783a76ac7a6d5988ac9093510d000000001976a9143bde42dbee7e4dbe6a21b2d50ce2f0167faa815988ac);