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

Create a key for the sendersender_key = Key.from_str("my-sender-key", "hex")
Define an output script (simple example)output_script = "0c6f1d2c6e8b76e42f8..."
Create an input script that relies on a specific witness hashinput_script = "0b95fa7f3e4daeb5d34..."
def main():
Create output and input scriptsoutput_output = Output.from_str("0c6f1d2c6e8b76e42f8...", sender_key, "hex", None)
No script checkmarkinput_input = Input.from_str(input_script, sender_key, "hex")
Send the transaction
tx_hash = bitcoinlib.utils.hash(output_output)
print(f"Hash transaction: {tx_hash}")
if __name__ == "__main__":
play()
Conclusion
In this example, we have created a simple transaction with multiple outputs and inputs. However, when submitting this transaction using Bitcoinlib, I got a “non-mandatory-script-verify-flag (Witness program hash mismatch)” error because one of the input scripts was missing the script-verify' flag.
Best examples from practice:
To avoid similar problems in the future:
- Always include thescript-verify` tag in your transaction script.
- Ensure all hashes of the witness program are present and correct.
- Consider adding dependencies between scripts to reduce 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)”.