python-bitcoin-utils icon indicating copy to clipboard operation
python-bitcoin-utils copied to clipboard

Add Signature Hash Annex Support (BIP-341)

Open JAGADISHSUNILPEDNEKAR opened this issue 9 months ago • 1 comments

Description

This PR implements support for the signature hash annex as defined in BIP-341 (Taproot). The annex feature allows for future extensions to the signature validation system by providing a way to include additional data in transaction signature hashes.

Key Changes

  • Added an optional annex parameter to the get_transaction_taproot_digest method
  • Implemented the annex prefix and hashing logic per BIP-341 specifications
  • Fixed an index out of range error in the from_raw method by adding proper boundary checking during transaction parsing
  • Added comprehensive unit tests and integration tests for annex functionality

Implementation Details

  • The signature hash calculation now includes the annex when provided, with the appropriate bit set in the spend type byte
  • The annex is formatted according to BIP-341 with a 0x50 prefix and proper length encoding
  • Transaction parsing is now more robust with checks to prevent reading beyond data boundaries

Testing

  • Added unit tests that verify the transaction digests change correctly with different annexes
  • Created integration tests that verify:
    • Transactions with annexes can be created and broadcast to the network
    • Transactions with annexes can be spent
    • Transactions with annexes can be imported and parsed correctly
  • Integration tests require a running Bitcoin Core instance in regtest mode

Notes

  • All existing tests continue to pass
  • The integration test automatically skips when Bitcoin Core isn't available
  • Docstrings have been updated to document the annex parameter for Sphinx auto-documentation

This implementation enhances compatibility with the Taproot soft fork and is a prerequisite for full Partially Signed Bitcoin Transaction (PSBT) support.

JAGADISHSUNILPEDNEKAR avatar Apr 03 '25 16:04 JAGADISHSUNILPEDNEKAR

This is the extension of PR #128 I had some issue while working with the previous branch so shifted to a new branch with the implementation of the same thing covering your feedback :)

JAGADISHSUNILPEDNEKAR avatar Apr 05 '25 14:04 JAGADISHSUNILPEDNEKAR