karask / python-bitcoin-utils

Library to interact with the Bitcoin network. Ideal for low-level learning and experimenting.
MIT License
262 stars 99 forks source link

P2PWKH to P2PWKH [non-mandatory-script-verify-flag (Signature must be zero for failed CHECK(MULTI)SIG operation)] #57

Closed AlanLatte closed 6 months ago

AlanLatte commented 6 months ago

Hi, I'm new to Bitcoin and can't figure it out.

sender_priv_key = PrivateKey.from_wif(
    "92X8vyMDfHbip7K1N4ATFyHFsPxjx3GadmCi51wsmT4KXCpbc5q"
)
sender_pub_key = sender_priv_key.get_public_key()

receiver_addr_1 = P2wpkhAddress("tb1q3w2u9k99xfchpf2eu5ut8fnk7xhfn905h5s3lu")

script_code = Script(
    [
        "OP_DUP",
        "OP_HASH160",
        sender_pub_key.to_hash160(),
        "OP_EQUALVERIFY",
        "OP_CHECKSIG",
    ]
)

# 2000 satoshi
txin1 = TxInput("570ab331d50cf5b0a91b7dd4a8abfaee07dee23853083f32de981eed4a9f7f04", 1)

#  512 satoshi
txin2 = TxInput("1899c7e991676095152d8daf8820892fd8c1c401f64295882f311dfafaff01d9", 1)

# 420 satoshi left for transaction fee
txout_1 = TxOutput(2000 - 420, receiver_addr_1.to_script_pub_key())
txout_2 = TxOutput(512, receiver_addr_1.to_script_pub_key())

tx = Transaction([txin1, txin2], [txout_1, txout_2], has_segwit=True)

txsign1 = sender_priv_key.sign_segwit_input(tx, 0, script_code, 2000, SIGHASH_ALL)
tx.witnesses.append(TxWitnessInput([txsign1, sender_pub_key.to_hex()]))

txsign2 = sender_priv_key.sign_segwit_input(tx, 1, script_code, 512, SIGHASH_ALL)
tx.witnesses.append(TxWitnessInput([txsign2, sender_pub_key.to_hex()]))

print("Raw transaction size: ", tx.get_vsize())
print("TxId:", tx.get_txid())
print("Raw signed transaction:\n" + tx.serialize())

When running the script and sed raw tx to node, an error is thrown:

error code: -26
error message: non-mandatory-script-verify-flag (Signature must be zero for failed CHECK(MULTI)SIG operation)

Alternatively, I tried signing the transaction via cryptos and it worked fine. I would be very grateful if you help me with the answer!

rawtx (btcoin-utils): 02000000000102047f9f4aed1e98de323f085338e2de07eefaaba8d47d1ba9b0f50cd531b30a570100000000ffffffffd901fffafa1d312f889542f601c4c1d82f892088af8d2d1595606791e9c799180100000000ffffffff022c060000000000001600148b95c2d8a5327170a559e538b3a676f1ae9995f400020000000000001600148b95c2d8a5327170a559e538b3a676f1ae9995f40247304402207c810b47d37ceb5bea5594f19f6a679f6f988c11c0c28892ae452a3da2f0552502204f164ba03ca994a1dd45aaf574ccc17799c6bd16926f0b87bd00342ef9c444c801210399d9f0ad5fe28c36ce0c5534732e51677e0a40191555d352aba340c1f65916b8024730440220704bfca3000776bf754827bf8ed49daf8f30d8ec3f411fbc2b0b194f0bf6b04c02204ccdd8a019797c8b35ba1a266324ea9270371c418cba330f094cb83d87742e3d01210399d9f0ad5fe28c36ce0c5534732e51677e0a40191555d352aba340c1f65916b800000000

rawtx (cryptos) 01000000000102d901fffafa1d312f889542f601c4c1d82f892088af8d2d1595606791e9c799180100000000ffffffff047f9f4aed1e98de323f085338e2de07eefaaba8d47d1ba9b0f50cd531b30a570100000000ffffffff0262070000000000001600148b95c2d8a5327170a559e538b3a676f1ae9995f400020000000000001600148b95c2d8a5327170a559e538b3a676f1ae9995f402483045022100f4653182a8c556ed7fca5d9432cd6c1c690e9ada6862bbf63698a9bdff36627c02200c424fc1fc45dc061c36d4e40fa7622b76c2aa131208bd000bf694246c47a39e01210399d9f0ad5fe28c36ce0c5534732e51677e0a40191555d352aba340c1f65916b802463043021f309dd2483c14898b5a584320119131c27145108471cd3c3d4c831523bc5da702204c1decb34c4692b4f852f6977681c38f806e4fb1626564c27cd82eac29d8553801210399d9f0ad5fe28c36ce0c5534732e51677e0a40191555d352aba340c1f65916b800000000

karask commented 6 months ago

Hi @AlanLatte

I noticed that txin1 has 512 sats and txin2 has 2000 sats. So when you get the signatures (txsign1 and txsign2) the amounts provided should be the other way around.

That should be it so I did not look further.

Hope this helps