jmwallet.wallet.signing
jmwallet.wallet.signing
Bitcoin transaction signing utilities for P2WPKH and P2WSH inputs.
Uses the unified transaction models from jmcore.bitcoin. The signing
functions access byte-oriented properties (txid_le, sequence_bytes,
version_bytes, locktime_bytes) to construct the exact BIP-143
sighash preimage.
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']
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
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 | |
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
31 32 | |
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
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 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 | |
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
Functions
__getitem__(key: str) -> Any
Allow inp["txid"] style access for backward compatibility.
Source code in jmcore/src/jmcore/bitcoin.py
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 | |
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
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 | |
get(key: str, default: Any = None) -> Any
Allow inp.get("key", default) for backward compatibility.
Source code in jmcore/src/jmcore/bitcoin.py
633 634 635 636 637 638 | |
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
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 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 | |
Attributes
script: bytes
instance-attribute
scriptpubkey: str
property
ScriptPubKey as hex string (backward compat alias).
value: int
instance-attribute
Functions
__getitem__(key: str) -> Any
Allow out["value"] style access for backward compatibility.
Source code in jmcore/src/jmcore/bitcoin.py
707 708 709 710 711 712 713 | |
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
691 692 693 694 695 696 697 698 699 700 701 702 703 | |
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
724 725 726 727 728 729 730 731 732 733 734 735 736 | |
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
738 739 740 741 742 743 744 745 746 | |
get(key: str, default: Any = None) -> Any
Allow out.get("key", default) for backward compatibility.
Source code in jmcore/src/jmcore/bitcoin.py
715 716 717 718 719 720 | |
Functions
compute_sighash_segwit(tx: ParsedTransaction, input_index: int, script_code: bytes, value: int, sighash_type: int) -> bytes
Source code in jmwallet/src/jmwallet/wallet/signing.py
51 52 53 54 55 56 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 93 | |
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
1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 | |
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
212 213 214 215 216 217 218 219 220 221 222 223 224 | |
create_witness_stack(signature: bytes, pubkey_bytes: bytes) -> list[bytes]
Source code in jmwallet/src/jmwallet/wallet/signing.py
171 172 | |
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
35 36 37 38 39 40 41 42 43 44 45 46 47 48 | |
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
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 | |
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
264 265 266 267 268 269 270 271 272 273 274 | |
sign_p2wpkh_input(tx: ParsedTransaction, input_index: int, script_code: bytes, value: int, private_key: PrivateKey, sighash_type: int = 1) -> bytes
Sign a P2WPKH input using coincurve.
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: coincurve PrivateKey instance 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
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 127 128 | |
sign_p2wsh_input(tx: ParsedTransaction, input_index: int, witness_script: bytes, value: int, private_key: PrivateKey, sighash_type: int = 1) -> bytes
Sign a P2WSH input using coincurve.
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: coincurve PrivateKey instance 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
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 203 204 205 206 207 208 209 | |
verify_p2wpkh_signature(tx: ParsedTransaction, input_index: int, script_code: bytes, value: int, signature: bytes, pubkey: bytes) -> bool
Verify a P2WPKH signature using coincurve.
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
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 162 163 164 165 166 167 168 | |