1200wd / bitcoinlib

Bitcoin and other Cryptocurrencies Library for Python. Includes a fully functional wallet, Mnemonic key generation and management and connection with various service providers to receive and send blockchain and transaction information.
http://bitcoinlib.readthedocs.io/
GNU General Public License v3.0
606 stars 201 forks source link

verify fails on p2wpkh #303

Closed mrx23dot closed 1 year ago

mrx23dot commented 1 year ago

I have created this segwit transaction https://coinb.in/#verify accepts it, but this lib doesn't:

from bitcoinlib.transactions import Transaction

raw_transaction_hex = "010000000001014596DDE147995AE7485EBA2AC365DF99A67AB75F81990E4CC1163471CD5659950000000000FFFFFFFF0175070000000000001976A9146F303D16FC6680F171C087A7D41B6BDF215AEE5E88AC0247304402207F4FFD453D9680F81CCE725AA3C0FE912F4215A3686810883CA26D7880B76CB002200A7F0AAC28E9C20D6621E0F2F9F9FEBD1D5431AF29A156BBBF3F071123BD488D012102F629179B2A08F930EC72D5909B5C4B8A6D18E88FDB473D040ABFA90CC81745AA00000000"

transaction = Transaction.parse(raw_transaction_hex)
print("valid:", transaction.verify())
False

at least it could give reason.

mrx23dot commented 1 year ago

same transaction created with different lib, but verify still fails

020000000001014596dde147995ae7485eba2ac365df99a67ab75f81990e4cc1163471cd5659950000000000ffffffff0175070000000000001976a9146f303d16fc6680f171c087a7d41b6bdf215aee5e88ac024730440220475ef700c502af5c4e2d4bf6776997fb4adb7ff509991415fa42e343b09616220220464d1db3ca6b48f8fe01af37dfeedee629ba4d48d7710bea002a803620d52196012102f629179b2a08f930ec72d5909b5c4b8a6d18e88fdb473d040abfa90cc81745aa00000000

mccwdev commented 1 year ago

The raw transaction does not contain the value of the input, which is needed to verify it. To fix it try:

rawtx = '020000000001014596dde147995ae7485eba2ac365df99a67ab75f81990e4cc1163471cd5659950000000000ffffffff0175070000000000001976a9146f303d16fc6680f171c087a7d41b6bdf215aee5e88ac024730440220475ef700c502af5c4e2d4bf6776997fb4adb7ff509991415fa42e343b09616220220464d1db3ca6b48f8fe01af37dfeedee629ba4d48d7710bea002a803620d52196012102f629179b2a08f930ec72d5909b5c4b8a6d18e88fdb473d040abfa90cc81745aa00000000'
t = Transaction.parse(rawtx)
t.inputs[0].value = 2115
t.verify()
t.info()
mrx23dot commented 1 year ago

I see your point, but this was a valid transaction accepted by the chain https://mempool.space/tx/ce8419aa2a9d29a7965cf44645a535d6f5c37ee28ea933c5f60bfb38ef888826?showFlow=false#flow

raw hex: 020000000001014596dde147995ae7485eba2ac365df99a67ab75f81990e4cc1163471cd5659950000000000ffffffff0175070000000000001976a9146f303d16fc6680f171c087a7d41b6bdf215aee5e88ac024730440220475ef700c502af5c4e2d4bf6776997fb4adb7ff509991415fa42e343b09616220220464d1db3ca6b48f8fe01af37dfeedee629ba4d48d7710bea002a803620d52196012102f629179b2a08f930ec72d5909b5c4b8a6d18e88fdb473d040abfa90cc81745aa00000000

so it must know that the input was: 0.00002115 BTC

and output was: 0.00001909 BTC

Also why does it say that it's not signed?

Inputs

Or does it need the witness data for these?

mccwdev commented 1 year ago

The witness data is included but the input value is not included in the raw transaction hash, so to verify the transaction any client needs to look at the preceding transactions.