Skip to content

jmwallet.wallet.address

jmwallet.wallet.address

Bitcoin address utilities.

Re-exports address helpers from jmcore.bitcoin.

Attributes

__all__ = ['hash160', 'pubkey_to_p2wpkh_address', 'pubkey_to_p2wpkh_script', 'script_to_p2wsh_address', 'script_to_p2wsh_scriptpubkey'] module-attribute

Functions

hash160(data: bytes) -> bytes

RIPEMD160(SHA256(data)) - Used for Bitcoin addresses.

Args: data: Input data to hash

Returns: 20-byte hash

Source code in jmcore/src/jmcore/bitcoin.py
233
234
235
236
237
238
239
240
241
242
243
def hash160(data: bytes) -> bytes:
    """
    RIPEMD160(SHA256(data)) - Used for Bitcoin addresses.

    Args:
        data: Input data to hash

    Returns:
        20-byte hash
    """
    return hashlib.new("ripemd160", hashlib.sha256(data).digest()).digest()

pubkey_to_p2wpkh_address(pubkey: bytes | str, network: str | NetworkType = 'mainnet') -> str

Convert compressed public key to P2WPKH (native SegWit) address.

Args: pubkey: 33-byte compressed public key (bytes or hex string) network: Network type

Returns: Bech32 P2WPKH address

Source code in jmcore/src/jmcore/bitcoin.py
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
@validate_call
def pubkey_to_p2wpkh_address(pubkey: bytes | str, network: str | NetworkType = "mainnet") -> str:
    """
    Convert compressed public key to P2WPKH (native SegWit) address.

    Args:
        pubkey: 33-byte compressed public key (bytes or hex string)
        network: Network type

    Returns:
        Bech32 P2WPKH address
    """
    if isinstance(pubkey, str):
        pubkey = bytes.fromhex(pubkey)

    if len(pubkey) != 33:
        raise ValueError(f"Invalid compressed pubkey length: {len(pubkey)}")

    pubkey_hash = hash160(pubkey)
    hrp = get_hrp(network)

    result = bech32_lib.encode(hrp, 0, pubkey_hash)
    if result is None:
        raise ValueError("Failed to encode bech32 address")
    return result

pubkey_to_p2wpkh_script(pubkey: bytes | str) -> bytes

Create P2WPKH scriptPubKey from public key.

Args: pubkey: 33-byte compressed public key (bytes or hex string)

Returns: 22-byte P2WPKH scriptPubKey (OP_0 <20-byte-hash>)

Source code in jmcore/src/jmcore/bitcoin.py
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
def pubkey_to_p2wpkh_script(pubkey: bytes | str) -> bytes:
    """
    Create P2WPKH scriptPubKey from public key.

    Args:
        pubkey: 33-byte compressed public key (bytes or hex string)

    Returns:
        22-byte P2WPKH scriptPubKey (OP_0 <20-byte-hash>)
    """
    if isinstance(pubkey, str):
        pubkey = bytes.fromhex(pubkey)

    pubkey_hash = hash160(pubkey)
    return bytes([0x00, 0x14]) + pubkey_hash

script_to_p2wsh_address(script: bytes, network: str | NetworkType = 'mainnet') -> str

Convert witness script to P2WSH address.

Args: script: Witness script bytes network: Network type

Returns: Bech32 P2WSH address

Source code in jmcore/src/jmcore/bitcoin.py
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
@validate_call
def script_to_p2wsh_address(script: bytes, network: str | NetworkType = "mainnet") -> str:
    """
    Convert witness script to P2WSH address.

    Args:
        script: Witness script bytes
        network: Network type

    Returns:
        Bech32 P2WSH address
    """
    script_hash = sha256(script)
    hrp = get_hrp(network)

    result = bech32_lib.encode(hrp, 0, script_hash)
    if result is None:
        raise ValueError("Failed to encode bech32 address")
    return result

script_to_p2wsh_scriptpubkey(script: bytes) -> bytes

Create P2WSH scriptPubKey from witness script.

Args: script: Witness script bytes

Returns: 34-byte P2WSH scriptPubKey (OP_0 <32-byte-hash>)

Source code in jmcore/src/jmcore/bitcoin.py
404
405
406
407
408
409
410
411
412
413
414
415
def script_to_p2wsh_scriptpubkey(script: bytes) -> bytes:
    """
    Create P2WSH scriptPubKey from witness script.

    Args:
        script: Witness script bytes

    Returns:
        34-byte P2WSH scriptPubKey (OP_0 <32-byte-hash>)
    """
    script_hash = sha256(script)
    return bytes([0x00, 0x20]) + script_hash