def _pub_keyinfo_from_xpub(xpub: BIP32Key, network: Optional[str] = None, compressed: Optional[bool] = None) -> PubkeyInfo: """Return the pub_key tuple (SEC-bytes, network) from a BIP32 xpub. BIP32Key is always compressed and includes network information: here the 'network, compressed' input parameters are passed only to allow consistency checks. """ compressed = True if compressed is None else compressed if not compressed: raise BTClibValueError("Uncompressed SEC / compressed BIP32 mismatch") if isinstance(xpub, BIP32KeyData): xpub.assert_valid() else: xpub = BIP32KeyData.b58decode(xpub) if xpub.key[0] not in (2, 3): err_msg = f"not a public key: {xpub.b58encode()}" raise BTClibValueError(err_msg) if network is None: return xpub.key, network_from_xkeyversion(xpub.version) allowed_versions = xpubversions_from_network(network) if xpub.version not in allowed_versions: err_msg = f"Not a {network} key: " err_msg += f"{xpub.b58encode()}" raise BTClibValueError(err_msg) return xpub.key, network
def _prv_keyinfo_from_xprv(xprv: BIP32Key, network: Optional[str] = None, compressed: Optional[bool] = None) -> PrvkeyInfo: """Return prv_key tuple (int, compressed, network) from BIP32 xprv. BIP32Key is always compressed and includes network information: here the 'network, compressed' input parameters are passed only to allow consistency checks. """ compressed = True if compressed is None else compressed if not compressed: raise BTClibValueError("uncompressed SEC / compressed BIP32 mismatch") if isinstance(xprv, BIP32KeyData): xprv.assert_valid() else: xprv = BIP32KeyData.b58decode(xprv) if xprv.key[0] != 0: err_msg = f"not a private key: {xprv.b58encode()}" raise BTClibValueError(err_msg) if network is None: network = network_from_xkeyversion(xprv.version) allowed_versions = xprvversions_from_network(network) if xprv.version not in allowed_versions: err_msg = f"not a {network} key: " err_msg += f"{xprv.b58encode()}" raise BTClibValueError(err_msg) q = int.from_bytes(xprv.key[1:], byteorder="big") return q, network, True
def test_curve_from_xkeyversion() -> None: for net in NETWORKS: all_versions = xpubversions_from_network(net) + xprvversions_from_network(net) for version in all_versions: # unfortunately 'regtest' shares same versions with 'testnet' if net != "regtest": assert net == network_from_xkeyversion(version) assert NETWORKS[net]["curve"] == curve_from_xkeyversion(version)
def _helper_checks(xkey: BIP32Key, check_root_xkey: bool) -> Tuple[BIP32KeyData, Network]: if not isinstance(xkey, BIP32KeyData): xkey = BIP32KeyData.b58decode(xkey) if check_root_xkey and not xkey.is_root: raise BTClibValueError(f"not a root key: {xkey.b58encode()}") network = NETWORKS[network_from_xkeyversion(xkey.version)] return xkey, network
def test_curve_from_xkeyversion() -> None: for net_str, net in NETWORKS.items(): all_versions = xpubversions_from_network(net_str) all_versions += xprvversions_from_network(net_str) for version in all_versions: # unfortunately 'regtest' shares same versions with 'testnet' if net_str != "regtest": assert net_str == network_from_xkeyversion(version) assert net.curve == curve_from_xkeyversion(version)