jmwallet.wallet.signing
jmwallet.wallet.signing
Bitcoin transaction signing utilities for P2WPKH and P2WSH inputs.
BIP-143 sighash computation delegates to python-bitcointx's
SignatureHash so consensus-critical preimage construction lives in
an audited library. The transaction is serialized via the in-tree
ParsedTransaction and handed to bitcointx's CTransaction.
Attributes
Transaction = ParsedTransaction
module-attribute
__all__ = ['ParsedTransaction', 'Transaction', 'TransactionSigningError', 'TxInput', 'TxOutput', 'compute_sighash_segwit', 'create_p2wpkh_script_code', 'create_p2wsh_witness_stack', 'create_witness_stack', 'deserialize_transaction', 'encode_varint', 'hash256', 'read_varint', 'sign_p2wpkh_input', 'sign_p2wsh_input', 'verify_p2wpkh_signature', 'verify_p2wsh_signature']
module-attribute
read_varint = decode_varint
module-attribute
Classes
ParsedTransaction
Parsed Bitcoin transaction with typed inputs and outputs.
Provides dual accessors for int and bytes representations of version and locktime (needed by BIP-143 sighash construction).
Source code in jmcore/src/jmcore/bitcoin.py
799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 | |
Attributes
has_witness: bool
instance-attribute
inputs: list[TxInput]
instance-attribute
locktime: int
instance-attribute
locktime_bytes: bytes
property
Locktime as 4-byte little-endian bytes.
outputs: list[TxOutput]
instance-attribute
version: int
instance-attribute
version_bytes: bytes
property
Version as 4-byte little-endian bytes.
witnesses: list[list[bytes]]
instance-attribute
TransactionSigningError
Bases: Exception
Source code in jmwallet/src/jmwallet/wallet/signing.py
37 38 | |
TxInput
Unified transaction input model.
Stores data in canonical byte form internally. Provides dual accessors for the two dominant usage patterns in the codebase:
- String pattern (RPC / human-readable):
txid(big-endian hex),scriptsig_hex,scriptpubkey_hex,sequence(int). - Bytes pattern (BIP-143 signing):
txid_le(little-endian bytes),scriptsig(bytes),sequence_bytes(4-byte LE bytes).
Construction helpers
TxInput.from_hex(txid_hex, vout, ...)— build from big-endian hex txid (the format returned by Bitcoin Core RPC).- Direct
TxInput(txid_le=..., vout=..., ...)— build from raw LE bytes (the format found inside serialised transactions).
Source code in jmcore/src/jmcore/bitcoin.py
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 | |
Attributes
scriptpubkey: bytes = b''
class-attribute
instance-attribute
scriptpubkey_hex: str
property
ScriptPubKey of the prevout as hex string.
scriptsig: bytes = b''
class-attribute
instance-attribute
scriptsig_hex: str
property
ScriptSig as hex string.
sequence: int = 4294967295
class-attribute
instance-attribute
sequence_bytes: bytes
property
Sequence as 4-byte little-endian bytes (for BIP-143 preimage).
txid: str
property
Transaction ID as big-endian hex (RPC / display format).
txid_le: bytes
instance-attribute
value: int = 0
class-attribute
instance-attribute
vout: int
instance-attribute
Methods:
__getitem__(key: str) -> Any
Allow inp["txid"] style access for backward compatibility.
Source code in jmcore/src/jmcore/bitcoin.py
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 | |
from_hex(txid: str, vout: int, *, scriptsig: str = '', sequence: int = 4294967295, value: int = 0, scriptpubkey: str = '') -> TxInput
classmethod
Create from big-endian hex txid (the RPC / display format).
Args: txid: 64-char hex string (big-endian, as returned by RPC) vout: Output index scriptsig: ScriptSig hex (default empty) sequence: Sequence number (default 0xFFFFFFFF) value: UTXO value in satoshis (optional, for tx builder) scriptpubkey: Prevout scriptPubKey hex (optional)
Source code in jmcore/src/jmcore/bitcoin.py
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 | |
get(key: str, default: Any = None) -> Any
Allow inp.get("key", default) for backward compatibility.
Source code in jmcore/src/jmcore/bitcoin.py
683 684 685 686 687 688 | |
TxOutput
Unified transaction output model.
Stores value and script (scriptPubKey) in canonical byte form.
Provides convenience accessors for hex and address representations.
Source code in jmcore/src/jmcore/bitcoin.py
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 | |
Attributes
script: bytes
instance-attribute
scriptpubkey: str
property
ScriptPubKey as hex string (backward compat alias).
value: int
instance-attribute
Methods:
__getitem__(key: str) -> Any
Allow out["value"] style access for backward compatibility.
Source code in jmcore/src/jmcore/bitcoin.py
757 758 759 760 761 762 763 | |
address(network: str | NetworkType = 'mainnet') -> str
Derive address from scriptPubKey.
Args: network: Network type for bech32/base58 encoding.
Returns: Address string.
Raises: ValueError: If scriptPubKey is an unsupported type.
Source code in jmcore/src/jmcore/bitcoin.py
741 742 743 744 745 746 747 748 749 750 751 752 753 | |
from_address(address: str, value: int) -> TxOutput
classmethod
Create from address string (resolves to scriptPubKey).
Args: address: Bitcoin address (any supported format) value: Output value in satoshis
Source code in jmcore/src/jmcore/bitcoin.py
774 775 776 777 778 779 780 781 782 783 784 785 786 | |
from_hex(scriptpubkey: str, value: int) -> TxOutput
classmethod
Create from hex scriptPubKey.
Args: scriptpubkey: ScriptPubKey as hex string value: Output value in satoshis
Source code in jmcore/src/jmcore/bitcoin.py
788 789 790 791 792 793 794 795 796 | |
get(key: str, default: Any = None) -> Any
Allow out.get("key", default) for backward compatibility.
Source code in jmcore/src/jmcore/bitcoin.py
765 766 767 768 769 770 | |
Functions:
compute_sighash_segwit(tx: ParsedTransaction, input_index: int, script_code: bytes, value: int, sighash_type: int) -> bytes
Compute the BIP-143 sighash for a segwit input.
Delegates to :func:bitcointx.core.script.SignatureHash with
SIGVERSION_WITNESS_V0. The ParsedTransaction is re-serialized
and parsed by bitcointx so we never re-implement the BIP-143 preimage
layout in-tree.
Source code in jmwallet/src/jmwallet/wallet/signing.py
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | |
create_p2wpkh_script_code(pubkey: bytes | str) -> bytes
Create scriptCode for P2WPKH signing (BIP143).
For P2WPKH, the scriptCode is the P2PKH script: OP_DUP OP_HASH160 <20-byte-pubkeyhash> OP_EQUALVERIFY OP_CHECKSIG
Args: pubkey: Public key bytes or hex
Returns: 25-byte scriptCode
Source code in jmcore/src/jmcore/bitcoin.py
1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 | |
create_p2wsh_witness_stack(signature: bytes, witness_script: bytes) -> list[bytes]
Create witness stack for P2WSH input.
For timelocked scripts (CLTV), the witness is: [signature, witness_script]
Args: signature: DER signature with sighash byte witness_script: The witness script (e.g., freeze script)
Returns: Witness stack: [signature, witness_script]
Source code in jmwallet/src/jmwallet/wallet/signing.py
224 225 226 227 228 229 230 231 232 233 234 235 236 | |
create_witness_stack(signature: bytes, pubkey_bytes: bytes) -> list[bytes]
Source code in jmwallet/src/jmwallet/wallet/signing.py
164 165 | |
deserialize_transaction(tx_bytes: bytes) -> ParsedTransaction
Deserialize a raw transaction for signing.
Delegates to :func:jmcore.bitcoin.parse_transaction_bytes which now
returns typed TxInput / TxOutput objects with the dual-accessor
API required by the signing code.
Raises: TransactionSigningError: If the transaction bytes cannot be parsed.
Source code in jmwallet/src/jmwallet/wallet/signing.py
41 42 43 44 45 46 47 48 49 50 51 52 53 54 | |
encode_varint(n: int) -> bytes
Encode integer as Bitcoin varint.
Args: n: Integer to encode
Returns: Encoded bytes
Source code in jmcore/src/jmcore/bitcoin.py
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 | |
hash256(data: bytes) -> bytes
SHA256(SHA256(data)) - Used for Bitcoin txids and block hashes.
Args: data: Input data to hash
Returns: 32-byte hash
Source code in jmcore/src/jmcore/bitcoin.py
282 283 284 285 286 287 288 289 290 291 292 | |
sign_p2wpkh_input(tx: ParsedTransaction, input_index: int, script_code: bytes, value: int, private_key: CKey, sighash_type: int = 1) -> bytes
Sign a P2WPKH input.
Args: tx: The transaction to sign input_index: Index of the input to sign script_code: The scriptCode for signing (P2PKH script for P2WPKH) value: The value of the input being spent (in satoshis) private_key: Signing private key sighash_type: Sighash type (default SIGHASH_ALL = 1)
Returns: DER-encoded signature with sighash type byte appended
Source code in jmwallet/src/jmwallet/wallet/signing.py
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | |
sign_p2wsh_input(tx: ParsedTransaction, input_index: int, witness_script: bytes, value: int, private_key: CKey, sighash_type: int = 1) -> bytes
Sign a P2WSH input.
For P2WSH, the scriptCode in BIP143 signing is the witness script itself.
Args: tx: The transaction to sign input_index: Index of the input to sign witness_script: The witness script (e.g., timelocked freeze script) value: The value of the input being spent (in satoshis) private_key: Signing private key sighash_type: Sighash type (default SIGHASH_ALL = 1)
Returns: DER-encoded signature with sighash type byte appended
Source code in jmwallet/src/jmwallet/wallet/signing.py
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | |
verify_p2wpkh_signature(tx: ParsedTransaction, input_index: int, script_code: bytes, value: int, signature: bytes, pubkey: bytes) -> bool
Verify a P2WPKH signature.
Args: tx: The transaction containing the input input_index: Index of the input to verify script_code: The scriptCode (P2PKH script for P2WPKH) value: The value of the input being spent (in satoshis) signature: DER-encoded signature with sighash type byte appended pubkey: Public key bytes (compressed or uncompressed)
Returns: True if signature is valid, False otherwise
Source code in jmwallet/src/jmwallet/wallet/signing.py
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | |
verify_p2wsh_signature(tx: ParsedTransaction, input_index: int, witness_script: bytes, value: int, signature: bytes, pubkey: bytes) -> bool
Verify a P2WSH BIP143 signature against its witness script.
Source code in jmwallet/src/jmwallet/wallet/signing.py
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 | |