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] known_version = False for (testnet_header, public_header, d) in [ (False, False, XPRV_HEADERS_MAIN), (True, False, XPRV_HEADERS_TEST), (False, True, XPUB_HEADERS_MAIN), (True, True, XPUB_HEADERS_TEST), ]: for key, header in d.items(): if version == header: script_type = key is_testnet = testnet_header is_pubkey = public_header known_version = True break if not known_version: 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, script_type=script_type) 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
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: is_testnet = False is_pubkey = False elif version == EX_TEST_PRIVATE: is_testnet = True is_pubkey = False elif version == EX_MAIN_PUBLIC: is_testnet = False is_pubkey = True elif version == EX_TEST_PUBLIC: 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 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) 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
def pointFromCompressedPub(secret): # 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) return point
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, testnet=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 = check_decode(xkey, need_prefix=True) # Extract remaining fields # Python 2.x compatibility if isinstance(raw[5], int): depth = raw[5] else: depth = ord(raw[5]) fpr = raw[5:9] child = struct.unpack(">L", raw[9:13])[0] chain = raw[13:45] secret = raw[45:] # Extract private key or public key point if not public: secret = secret[1:] else: # Recover public curve point from compressed key # Python3 FIX lsb = secret[0] & 1 if isinstance(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=public, testnet=testnet) if 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 = check_decode(xkey) raw = unhexlify(base58CheckDecode(xkey, skip_first_bytes=False)) if len(raw) != 78: raise ValueError("extended key format wrong length") # Verify address version/type version = raw[:4] if version in EX_MAIN_PRIVATE: is_testnet = False is_pubkey = False elif version in EX_TEST_PRIVATE: is_testnet = True is_pubkey = False elif version in EX_MAIN_PUBLIC: is_testnet = False is_pubkey = True elif version in EX_TEST_PUBLIC: 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 = int.from_bytes(secret[1:], 'big') 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 = PointObject(SECP256k1.curve, x, y) secret = VerifyKey(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.SetPublic() return key