Bitcoin: Getting error “non-mandatory-script-verify-flag (Witness program hash mismatch)” when trying to send raw signed transaction
Here is an article that explains what the error “non-mandatory-script-verify-flag (Witness program hash mismatch)” means and why you might be seeing it when trying to send a raw signed transaction using Bitcoinlib:
Understanding the Error: Non-Mandatory Script Verify Flag
When sending raw transactions in Bitcoin, you need to ensure that all required flags are included. One of these mandatory flags is the script-verify
flag, which allows the transaction script (the code that performs various operations on the input) to be verified. However, there’s a catch – some scripts rely on specific witness program hashes being present.
What Causes the Error?
The error “non-mandatory-script-verify-flag (Witness program hash mismatch)” typically occurs when the script-verify
flag is not included in the transaction script. This can happen if your code doesn’t properly handle witness programs or if there are dependencies between scripts that aren’t met.
What Does it Mean?
When a transaction is sent, Bitcoin checks several flags to ensure everything is valid and functional. The script-verify
flag is crucial for this process because certain transactions rely on specific witness program hashes being present in the script. If these hashes don’t match, the transaction may be rejected.
Simplified Example Code:
Here’s a simplified example of how you might create a transaction using Bitcoinlib:
from bitcoinlib.transactions import Output, Key

Create a key for the sender
sender_key = Key.from_str("my-sender-key", "hex")
Define the output script (a simple example)
output_script = "0c6f1d2c6e8b76e42f8..."
Create an input script that relies on a specific hash program witness
input_script = "0b95fa7f3e4daeb5d34..."
def slim():
Create the output and input scripts
output_output = Output.from_str("0c6f1d2c6e8b76e42f8...", sender_key, "hex", None)
No script-verify flag
input_input = Input.from_str(input_script, sender_key, "hex")
Send the transaction
tx_hash = bitcoinlib.utils.hash(output_output)
print(f"Transaction hash: {tx_hash}")
if __name__ == "__main__":
slim()
Conclusion
In this example, we created a simple transaction with multiple outputs and inputs. However, when sending this transaction using Bitcoinlib, the error “non-mandatory-script-verify-flag (Witness program hash mismatch)” occurred because the script-verify
flag was missing from one of the input scripts.
Best Practices:
To avoid similar issues in the future:
- Always include the
script-verify
flag in your transaction script.
- Ensure that all witness program hashes are present and correct.
- Consider adding dependencies between scripts to minimize the risk of errors.
By following these best practices, you can ensure that your transactions pass validation and avoid receiving any error messages like “non-mandatory-script-verify-flag (Witness program hash mismatch)”.
Bir yanıt yazın