Bitcoin

Error “non-mandatory-script-verify-flag(Witness program hash mismatch)” when trying to send raw signed transaction

I’m trying to send a very simple segwit transaction with multiple inputs and outputs using bitcoinlib. Here is the simplified code:

from bitcoinlib.transactions import Output, Key, Transaction

# (prev_txid, output_n, address, value in satoshis)
inputs = (
    (u'4ab923ee400a3899dc0790c7658af9931e54fc40dc1a8a79281a989e6a56ddd2', 125, 'bc1p0val59xpljwfr52hdg667v0nnnjzusfmvgys6warv7hklrfjum7sjgj75j', 1077938),
)

outputs = ()
outputs.append(Output(15260, address="bc1pylrxedd66287hl0e000qy9c86u9tagg206yvyymp4j2c6c5npfjseyjpdu"))

 # 1000 = 10.00 sat/vB
fee_per_kb = 1000

t = Transaction(outputs=outputs, witness_type="segwit", version=2)

for ti in inputs:
    ki = Key("PRIVATE_KEY")
    t.add_input(prev_txid=ti(0), output_n=ti(1), address=ti(2), value=ti(3),  keys=ki.public(), witness_type="segwit")

fee = round((t.estimate_size(1) * fee_per_kb) / 100)

total_inputs = 0
for ti in inputs:
    total_inputs += ti(3)

total_output = 15260
change = total_inputs - total_output - fee

# Add change output
t.add_output(change, address="bc1pyr9x8f7z5t9cxykfzzx66y2xc3davshzmd3xmf9dcxdpkg4tjuusschhpq")

icount = 0
for ti in inputs:
    ki = Key("PRIVATE_KEY")
    t.sign(ki.private_byte, icount)
    icount += 1

t.update_totals()
t.verify()
t.info()
print()
print("Raw Signed Transaction: %s" % t.raw_hex())
Transaction b2b765f26c3d8f766e205f1ae4bd82950f6283013ed23c0dd6d337c15b30d97a
Date: None
Network: bitcoin
Version: 2
Witness type: segwit
Status: new
Verified: True
Inputs
- bc1p0val59xpljwfr52hdg667v0nnnjzusfmvgys6warv7hklrfjum7sjgj75j 0.01077938 BTC 4ab923ee400a3899dc0790c7658af9931e54fc40dc1a8a79281a989e6a56ddd2 125
  segwit sig_pubkey; sigs: 1 (1-of-1) valid
Outputs
- bc1pylrxedd66287hl0e000qy9c86u9tagg206yvyymp4j2c6c5npfjseyjpdu 0.00015260 BTC p2tr U
- bc1pyr9x8f7z5t9cxykfzzx66y2xc3davshzmd3xmf9dcxdpkg4tjuusschhpq 0.01061148 BTC p2tr U
Size: 236
Vsize: 153
Fee: 1530
Confirmations: None
Block: None

Raw Signed Transaction: 02000000000101d2dd566a9e981a28798a1adc40fc541e93f98a65c79007dc99380a40ee23b94a7d00000000ffffffff029c3b00000000000022512027c66cb5bad28febfdf97bde021707d70abea10a7e88c21361ac958d62930a651c3110000000000022512020ca63a7c2a2cb8312c9108dad1146c45bd642e2db626da4adc19a1b22ab973902473044022019d43d372c8d6967f377f76d20a24d31fb3cb046a5640e9a02197afa526738dd02201afdc3e421665b35397ef2adf40908aeb3031569e49aab367285a9015effbd1f0121023f5bdab1fb426681f1a020998ffc69c844f380fe6e0d67b73e7fc9bc3291018b00000000

When I run the script everything looks fine and even displays. Verified like True But when I try to send it through bitcoin-cli, I get the error “non-mandatory-script-verify-flag(Witness program hash mismatch)”.

I think I made a mistake signing my input, but I’ve checked the private key multiple times and I’m not sure how the current UTXO matches the stored address.

Can anyone advise what to do?

Related Articles

Back to top button