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

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 Error: Non-Mandatory Script Verify Flag

When sending unprocessed transactions to Bitcoin, you need to make sure that all the required flags are turned on. One such mandatory flag is the script-verify' flag, which allows you to verify the transaction script (code that performs various operations on input data). However, there is one catch - some scripts rely on the presence of certain hashes of the witness program.

What is causing the error?

The "non-mandatory-script-verify-flag (Witness program hash mismatch)" error usually occurs when thescript-verify’ flag is not included in the transaction script. This can happen if your code doesn’t handle witness programs properly, or if there are dependencies between scripts that aren’t respected.

What does that mean?

When a transaction is sent, Bitcoin checks several flags to make sure everything is valid and functional. The script-verify' flag is critical to this process, as some transactions rely on specific witness hashes 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 to 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")


Determine the output script (simple example)

output_script = "0c6f1d2c6e8b76e42f8..."


Create an input script based on a specific hash of the witness program

input_script = "0b95fa7f3e4daeb5d34..."

def main():


Create output and input scripts

output_output = Output.from_str("0c6f1d2c6e8b76e42f8...", sender_key, "hex", None)

No script check flag

input_input = Input.from_str(input_script, sender_key, "hex")


Sending transaction

tx_hash = bitcoinlib.utils.hash(output_output)

print(f"Transaction hash: {tx_hash}")

if __name__ == "__main__":

main()

Conclusion

In this example, we have created a simple transaction with several outputs and inputs. However, when sending this transaction with Bitcoinlib, an error "non-mandatory-script-verify-flag (witness program hash mismatch)" occurred because one of the input scripts was missing the script-verify’ flag.

Recommendations:

Bitcoin: Getting error

To avoid such problems in the future:

  • Always include the `script-verify’ flag in your transaction script.
  • Verify that all hashes of the witness program are present and correct.
  • Consider adding dependencies between scripts to minimize the risk of errors.

By following these guidelines, you can ensure that your transactions pass verification and avoid receiving non-mandatory-script-verify-flag (witness program hash mismatch) error messages.

ETHEREUM WALLET FROM LINE

Leave a Reply

Your email address will not be published. Required fields are marked *