petertodd/python-bitcoinlib

Example for P2WSH wrong?

weskerfoot opened this issue · 4 comments

Forgive me if I am missing something, but should this line not include the size of the hash?

txin_redeemScript = CScript([OP_0, txin_scriptHash])

I.e.

 txin_redeemScript = CScript([OP_0, 0x20, txin_scriptHash])

BIP141 says

The '0' in scriptPubKey indicates the following push is a version 0 witness program. The length of the witness program indicates that it is a P2WSH type. The last item in the witness (the "witnessScript") is popped off, hashed with SHA256, compared against the 32-byte-hash in scriptPubKey, and deserialized:

See here

When I had tested the library, it did not add this length byte automatically.

dgpv commented
>>> from bitcoin.core.script import CScript
>>> CScript([b'X'*20])[0]
20

relevant line:

other = CScriptOp.encode_op_pushdata(other)

Thank you, I see what's going on there now! I didn't realize it was using isinstance to behave differently.

python-bitcoinlib is forked from bitcoin core's test harness, and as such its semantics and syntax are designed to precisely match the semantics and syntax of classes with the same name in the C++ codebase. Over there, the second argument is a variable width byte array, with the length encoded when you do a pushdata.

You can see some examples in its test harness here:

https://github.com/bitcoin/bitcoin/blob/3f1966ead6b2324d863ae6c5d9956cf4b9e4b148/test/functional/feature_segwit.py#L433

So as the example matches the C++ codebase, I'd say it's correct. ;-)