Don't miss our holiday offer - up to 50% OFF!
Bitcoin: Getting error “non-mandatory-script-verify-flag (Witness program hash mismatch)” when trying to send raw signed transaction
Here is an article explaining what the “non-mandatory-script-verify-flag (Witness program hash mismatch)” error means and why you might see it when you try to send a signed raw transaction using Bitcoinlib:
Understanding the error: Optional 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 can be used to verify the transaction script (the code that performs various operations on the input). However, there is a catch – some scripts rely on the presence of certain witness program hashes.
What causes the error?
The non-mandatory-script-verify-flag (Witness program hash mismatch) error usually occurs when the script verification flag is not included in the transaction script. This can happen if your Code Witness programs are not working properly or if there are dependencies between scripts that are not being executed.
What does this mean?
When a transaction is submitted, Bitcoin checks several flags to make sure everything is valid and working. The script verification flag is critical to this process because certain transactions rely on specific witness program hashes present in the script. If these hashes do not match, the transaction may be rejected.
Simple sample code:
Here is a simple example of how you can create a transaction with Bitcoinlib:
from bitcoinlib.transactions import Output, Key
Create a key for the sendersender_key = Key.from_str("my-sender-key", "hex")
Define the output script (simple example)output_script = "0c6f1d2c6e8b76e42f8..."
Create an input script based on a specific hash of the witness programinput_script = "0b95fa7f3e4daeb5d34..."
def main():
Create output and input scriptsoutput_output = Output.from_str("0c6f1d2c6e8b76e42f8...", sender_key, "hex", None)
No script validation flaginput_input = Input.from_str(input_script, sender_key, "hex")
Send the transactiontx_hash = bitcoinlib.utils.hash(output_output)
print(f"Transaction hash: {tx_hash}")
if __name__ == "__main__":
Main ()
Conclusion
In this example, we created a simple transaction with multiple outputs and inputs. However, when sending this transaction using Bitcoinlib, a “non-mandatory-script-verify-flag (Witness hash mismatch)” error 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.
- Make sure that all witness 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 error messages like “optional-script-verification-flag (Witness hash mismatch)”.