def fromExtendedKey(xkey, public=False): """ Create a BIP32Key by importing from extended private or public key string If public is True, return a public-only key regardless of input type. """ # Sanity checks raw = Base58.check_decode(xkey) if len(raw) != 78: raise ValueError("extended key format wrong length") # Verify address version/type version = raw[:4] if version == EX_MAIN_PRIVATE: keytype = 'xprv' elif version == EX_MAIN_PUBLIC: keytype = 'xpub' else: raise ValueError("unknown extended key version") # Extract remaining fields depth = ord(raw[4]) fpr = raw[5:9] child = struct.unpack(">L", raw[9:13])[0] chain = raw[13:45] secret = raw[45:78] # Extract private key or public key point if keytype == 'xprv': secret = secret[1:] else: # Recover public curve point from compressed key lsb = ord(secret[0]) & 1 x = string_to_int(secret[1:]) ys = (x**3 + 7) % FIELD_ORDER # y^2 = x^3 + 7 mod p y = sqrt_mod(ys, FIELD_ORDER) if y & 1 != lsb: y = FIELD_ORDER - y point = ecdsa.ellipticcurve.Point(SECP256k1.curve, x, y) secret = ecdsa.VerifyingKey.from_public_point(point, curve=SECP256k1) is_pubkey = (keytype == 'xpub') key = BIP32Key(secret=secret, chain=chain, depth=depth, index=child, fpr=fpr, public=is_pubkey) if not is_pubkey and public: key = key.SetPublic() return key
def fromExtendedKey(xkey, public=False): """ Create a BIP32Key by importing from extended private or public key string If public is True, return a public-only key regardless of input type. """ # Sanity checks raw = Base58.check_decode(xkey) if len(raw) != 78: raise ValueError("extended key format wrong length") # Verify address version/type version = raw[:4] if version == EX_MAIN_PRIVATE: keytype = 'xprv' elif version == EX_MAIN_PUBLIC: keytype = 'xpub' else: raise ValueError("unknown extended key version") # Extract remaining fields depth = ord(raw[4]) fpr = raw[5:9] child = struct.unpack(">L", raw[9:13])[0] chain = raw[13:45] secret = raw[45:78] # Extract private key or public key point if keytype == 'xprv': secret = secret[1:] else: # Recover public curve point from compressed key lsb = ord(secret[0]) & 1 x = string_to_int(secret[1:]) ys = (x**3+7) % FIELD_ORDER # y^2 = x^3 + 7 mod p y = sqrt_mod(ys, FIELD_ORDER) if y & 1 != lsb: y = FIELD_ORDER-y point = ecdsa.ellipticcurve.Point(SECP256k1.curve, x, y) secret = ecdsa.VerifyingKey.from_public_point(point, curve=SECP256k1) is_pubkey = (keytype == 'xpub') key = BIP32Key(secret=secret, chain=chain, depth=depth, index=child, fpr=fpr, public=is_pubkey) if not is_pubkey and public: key = key.SetPublic() return key
def fromExtendedKey(xkey, public=False): """ Create a BIP32Key by importing from extended private or public key string If public is True, return a public-only key regardless of input type. """ # Sanity checks raw = Base58.check_decode(xkey) if len(raw) != 78: raise ValueError("extended key format wrong length") # Verify address version/type version = raw[:4] tversion = codecs.encode(version, "hex") if tversion in query_lsit(testnet=False): is_testnet = False is_pubkey = False elif tversion in query_lsit(testnet=True): is_testnet = True is_pubkey = False elif tversion in query_lsit(public=True): is_testnet = False is_pubkey = True elif tversion in query_lsit(public=True, testnet=True): is_testnet = True is_pubkey = True else: raise ValueError("unknown extended key version") # Extract remaining fields # Python 2.x compatibility if type(raw[4]) == int: depth = raw[4] else: depth = ord(raw[4]) fpr = raw[5:9] child = struct.unpack(">L", raw[9:13])[0] chain = raw[13:45] secret = raw[45:78] # Extract private key or public key point if not is_pubkey: secret = secret[1:] else: # Recover public curve point from compressed key # Python3 FIX lsb = secret[0] & 1 if type( secret[0]) == int else ord(secret[0]) & 1 x = string_to_int(secret[1:]) ys = (x**3 + 7) % FIELD_ORDER # y^2 = x^3 + 7 mod p y = sqrt_mod(ys, FIELD_ORDER) if y & 1 != lsb: y = FIELD_ORDER - y point = ecdsa.ellipticcurve.Point(SECP256k1.curve, x, y) secret = ecdsa.VerifyingKey.from_public_point(point, curve=SECP256k1) key = BIP32Key(secret=secret, chain=chain, depth=depth, index=child, fpr=fpr, public=is_pubkey, testnet=is_testnet) if not is_pubkey and public: key = key.SetPublic() return key