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
Example #2
0
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)
Example #3
0
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)