def deserialize(xpubstr): xpub_bytes = b58check_decode(xpubstr) data = XpubStruct.parse(xpub_bytes) node = HDNodeType( depth=data.depth, fingerprint=data.fingerprint, child_num=data.child_num, chain_code=data.chain_code, ) if data.key[0] == 0: node.private_key = data.key[1:] else: node.public_key = data.key return data.version, node
def get_subnode(node, i): # Public Child key derivation (CKD) algorithm of BIP32 i_as_bytes = struct.pack(">L", i) if i & HARDENED_FLAG: raise ValueError("Prime derivation not supported") # Public derivation data = node.public_key + i_as_bytes I64 = hmac.HMAC(key=node.chain_code, msg=data, digestmod=hashlib.sha512).digest() I_left_as_exponent = int.from_bytes(I64[:32], "big") # BIP32 magic converts old public key to new public point point = SEC1Encoder.decode_public_key(node.public_key, secp256k1) result = I_left_as_exponent * secp256k1.G + point if point == Point.IDENTITY_ELEMENT: raise ValueError("Point cannot be INFINITY") # Convert public point to compressed public key public_key = SEC1Encoder.encode_public_key(result) return HDNodeType( depth=node.depth + 1, child_num=i, chain_code=I64[32:], fingerprint=hash_160(node.public_key)[:4], public_key=public_key, )
def _make_node_path(self, xpub, address_n): bip32node = BIP32Node.from_xkey(xpub) node = HDNodeType( depth=bip32node.depth, fingerprint=int.from_bytes(bip32node.fingerprint, 'big'), child_num=int.from_bytes(bip32node.child_number, 'big'), chain_code=bip32node.chaincode, public_key=bip32node.eckey.get_public_key_bytes(compressed=True), ) return HDNodePathType(node=node, address_n=address_n)
def _make_node_path(self, xpub, address_n): _, depth, fingerprint, child_num, chain_code, key = deserialize_xpub(xpub) node = HDNodeType( depth=depth, fingerprint=int.from_bytes(fingerprint, 'big'), child_num=int.from_bytes(child_num, 'big'), chain_code=chain_code, public_key=key, ) return HDNodePathType(node=node, address_n=address_n)
def _make_node_path(self, xpub, address_n): pubkey = bip32_key_from_string(xpub) derivation = pubkey.derivation() node = HDNodeType( depth=derivation.depth, fingerprint=be_bytes_to_int(pubkey.fingerprint()), child_num=derivation.n, chain_code=derivation.chain_code, public_key=pubkey.to_bytes(), ) return HDNodePathType(node=node, address_n=address_n)