ledger/utils.py
def make_child_xpub(
        derivation: str,
        parent_or_none: Optional[LedgerXPub],
        child: LedgerXPub,
        mainnet: bool = True) -> str:
    '''
    Builds an xpub for a derived child using its parent and path
    Args:
        derivation      (str): the m-prefixed derivation path e.g. m/44h/0h/0h
        parent (LedgerPubkey): the parent public key
        child  (LedgerPubkey): the child public key
        mainnet        (bool): whether to use mainnet prefixes
    '''
    indices = parse_derivation(derivation)

    # determine appropriate xpub version bytes
    if not mainnet:
        prefix = VERSION_BYTES['testnet']['public']
    else:
        prefix = VERSION_BYTES['mainnet']['public']

    if parent_or_none is not None:
        # xpubs include the parent fingerprint
        parent = cast(LedgerPubkey, parent_or_none)
        compressed_parent_key = compress_pubkey(parent['pubkey'])
        parent_fingerprint = rutils.hash160(compressed_parent_key)[:4]
        child_index = indices[-1].to_bytes(4, byteorder='big')
        depth = len(indices)
    else:
        # this means it's a master key
        parent_fingerprint = b'\x00' * 4
        child_index = b'\x00' * 4
        depth = 0

    # xpubs always use compressed pubkeys
    compressed_pubkey = compress_pubkey(child['pubkey'])

    # build the xpub
    xpub = bytearray()
    xpub.extend(prefix)                      # xpub prefix
    xpub.extend([depth])                     # depth
    xpub.extend(parent_fingerprint)          # paren't fingerprint
    xpub.extend(child_index)                 # index
    xpub.extend(child['chain_code'])         # chain_code
    xpub.extend(compressed_pubkey)           # pubkey (comp)
    return base58.encode(xpub)

    Builds an xpub for a derived child using its parent and path
    Args:
        derivation      (str): the m-prefixed derivation path e.g. m/44h/0h/0h
        parent (LedgerPubkey): the parent public key
        child  (LedgerPubkey): the child public key
        mainnet        (bool): whether to use mainnet prefixes
    
Builds an xpub for a derived child using its parent and path
Builds an xpub for a derived child using its parent and path
def make_child_xpub(
        derivation: str,
        parent_or_none: Optional[LedgerXPub],
        child: LedgerXPub,
        mainnet: bool = True) -> str:
    indices = parse_derivation(derivation)
    if not mainnet:
        prefix = VERSION_BYTES['testnet']['public']
    else:
        prefix = VERSION_BYTES['mainnet']['public']
    if parent_or_none is not None:
        parent = cast(LedgerPubkey, parent_or_none)
        compressed_parent_key = compress_pubkey(parent['pubkey'])
        parent_fingerprint = rutils.hash160(compressed_parent_key)[:4]
        child_index = indices[-1].to_bytes(4, byteorder='big')
        depth = len(indices)
    else:
        parent_fingerprint = b'\x00' * 4
        child_index = b'\x00' * 4
        depth = 0
    compressed_pubkey = compress_pubkey(child['pubkey'])
    xpub = bytearray()
    xpub.extend(prefix)                      
    xpub.extend([depth])                     
    xpub.extend(parent_fingerprint)          
    xpub.extend(child_index)                 
    xpub.extend(child['chain_code'])         
    xpub.extend(compressed_pubkey)           
    return base58.encode(xpub)
def make_child_xpub ( derivation : str , parent_or_none : Optional [ LedgerXPub ] , child : LedgerXPub , mainnet : bool = True ) -> str : indices = parse_derivation ( derivation ) if not mainnet : prefix = VERSION_BYTES [ 'testnet' ] [ 'public' ] else : prefix = VERSION_BYTES [ 'mainnet' ] [ 'public' ] if parent_or_none is not None : parent = cast ( LedgerPubkey , parent_or_none ) compressed_parent_key = compress_pubkey ( parent [ 'pubkey' ] ) parent_fingerprint = rutils . hash160 ( compressed_parent_key ) [ : 4 ] child_index = indices [ - 1 ] . to_bytes ( 4 , byteorder = 'big' ) depth = len ( indices ) else : parent_fingerprint = b'\x00' * 4 child_index = b'\x00' * 4 depth = 0 compressed_pubkey = compress_pubkey ( child [ 'pubkey' ] ) xpub = bytearray ( ) xpub . extend ( prefix ) xpub . extend ( [ depth ] ) xpub . extend ( parent_fingerprint ) xpub . extend ( child_index ) xpub . extend ( child [ 'chain_code' ] ) xpub . extend ( compressed_pubkey ) return base58 . encode ( xpub )
Builds an xpub for a derived child using its parent and path
Builds an xpub for a derived child using its parent and path
'''
    Builds an xpub for a derived child using its parent and path
    Args:
        derivation      (str): the m-prefixed derivation path e.g. m/44h/0h/0h
        parent (LedgerPubkey): the parent public key
        child  (LedgerPubkey): the child public key
        mainnet        (bool): whether to use mainnet prefixes
    ''' # determine appropriate xpub version bytes # xpubs include the parent fingerprint # this means it's a master key # xpubs always use compressed pubkeys # build the xpub # xpub prefix # depth # paren't fingerprint # index # chain_code # pubkey (comp)
derivation
str
the m-prefixed derivation path e.g.
the m - prefixed derivation path e . g .
parent_or_none
Optional[LedgerXPub]
child
LedgerXPub
the child public key
the child public key
mainnet
bool
whether to use mainnet prefixes
whether to use mainnet prefixes
parent
the parent public key
the parent public key