python-bitcoin-utils
python-bitcoin-utils copied to clipboard
Add Signature Hash Annex Support (BIP-341)
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
annexparameter to theget_transaction_taproot_digestmethod - Implemented the annex prefix and hashing logic per BIP-341 specifications
- Fixed an index out of range error in the
from_rawmethod 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
0x50prefix 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.
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 :)