def serialize_xprv(xtype, c, k, depth=0, fingerprint=b'\x00' * 4, child_number=b'\x00' * 4, *, net=None): if not ecc.is_secret_within_curve_range(k): raise BitcoinException('Impossible xprv (not within curve order)') xprv = xprv_header(xtype, net=net) \ + bytes([depth]) + fingerprint + child_number + c + bytes([0]) + k return EncodeBase58Check(xprv)
def deserialize_xkey(xkey, prv, *, net=None): if net is None: net = constants.net xkey = DecodeBase58Check(xkey) if len(xkey) != 78: raise BitcoinException('Invalid length for extended key: {}'.format( len(xkey))) depth = xkey[4] fingerprint = xkey[5:9] child_number = xkey[9:13] c = xkey[13:13 + 32] header = int('0x' + bh2u(xkey[0:4]), 16) headers = net.XPRV_HEADERS if prv else net.XPUB_HEADERS if header not in headers.values(): raise BitcoinException('Invalid extended key format: {}'.format( hex(header))) xtype = list(headers.keys())[list(headers.values()).index(header)] n = 33 if prv else 32 K_or_k = xkey[13 + n:] if prv and not ecc.is_secret_within_curve_range(K_or_k): raise BitcoinException('Impossible xprv (not within curve order)') return xtype, depth, fingerprint, child_number, c, K_or_k
def deserialize_drk(xkey, prv, *, net=None): if net is None: net = constants.net xkey = DecodeBase58Check(xkey) if len(xkey) != 78: raise BitcoinException('Invalid length for extended key: {}'.format( len(xkey))) depth = xkey[4] fingerprint = xkey[5:9] child_number = xkey[9:13] c = xkey[13:13 + 32] header = int('0x' + bh2u(xkey[0:4]), 16) if prv and header != net.DRKV_HEADER: raise BitcoinException('Invalid extended key format: {}'.format( hex(header))) if not prv and header != net.DRKP_HEADER: raise BitcoinException('Invalid extended key format: {}'.format( hex(header))) xtype = 'standard' n = 33 if prv else 32 K_or_k = xkey[13 + n:] if prv and not ecc.is_secret_within_curve_range(K_or_k): raise BitcoinException('Impossible drkv (not within curve order)') return xtype, depth, fingerprint, child_number, c, K_or_k