Exemple #1
0
def test_from_key() -> None:

    secp256r1 = CURVES["secp256r1"]
    m_c = bytes_from_point(Q, compressed=True), "mainnet"
    m_unc = bytes_from_point(Q, compressed=False), "mainnet"
    t_c = bytes_from_point(Q, compressed=True), "testnet"
    t_unc = bytes_from_point(Q, compressed=False), "testnet"
    for pubkey in [Q] + plain_pub_keys:
        assert Q == point_from_pubkey(pubkey)
        with pytest.raises(BTClibValueError):
            point_from_pubkey(pubkey, secp256r1)
        assert m_c == pubkeyinfo_from_pubkey(pubkey)
        assert m_c == pubkeyinfo_from_pubkey(pubkey, "mainnet")
        assert m_c == pubkeyinfo_from_pubkey(pubkey,
                                             "mainnet",
                                             compressed=True)
        assert m_c == pubkeyinfo_from_pubkey(pubkey, compressed=True)
        assert m_unc == pubkeyinfo_from_pubkey(pubkey,
                                               "mainnet",
                                               compressed=False)
        assert m_unc == pubkeyinfo_from_pubkey(pubkey, compressed=False)
        assert t_c == pubkeyinfo_from_pubkey(pubkey, "testnet")
        assert t_c == pubkeyinfo_from_pubkey(pubkey,
                                             "testnet",
                                             compressed=True)
        assert t_unc == pubkeyinfo_from_pubkey(pubkey,
                                               "testnet",
                                               compressed=False)

    for pubkey in [xpub_data] + compressed_pub_keys:
        assert Q == point_from_pubkey(pubkey)
        with pytest.raises(BTClibValueError):
            point_from_pubkey(pubkey, secp256r1)
        assert m_c == pubkeyinfo_from_pubkey(pubkey)
        assert m_c == pubkeyinfo_from_pubkey(pubkey, "mainnet")
        assert m_c == pubkeyinfo_from_pubkey(pubkey,
                                             "mainnet",
                                             compressed=True)
        assert m_c == pubkeyinfo_from_pubkey(pubkey, compressed=True)
        with pytest.raises(BTClibValueError):
            pubkeyinfo_from_pubkey(pubkey, "mainnet", compressed=False)
        with pytest.raises(BTClibValueError):
            pubkeyinfo_from_pubkey(pubkey, compressed=False)
        with pytest.raises(BTClibValueError):
            pubkeyinfo_from_pubkey(pubkey, "testnet", compressed=False)

    for pubkey in uncompressed_pub_keys:
        assert Q == point_from_pubkey(pubkey)
        with pytest.raises(BTClibValueError):
            point_from_pubkey(pubkey, secp256r1)
        assert m_unc == pubkeyinfo_from_pubkey(pubkey)
        assert m_unc == pubkeyinfo_from_pubkey(pubkey, "mainnet")
        with pytest.raises(BTClibValueError):
            pubkeyinfo_from_pubkey(pubkey, "mainnet", compressed=True)
        with pytest.raises(BTClibValueError):
            pubkeyinfo_from_pubkey(pubkey, compressed=True)
        assert m_unc == pubkeyinfo_from_pubkey(pubkey,
                                               "mainnet",
                                               compressed=False)
        assert m_unc == pubkeyinfo_from_pubkey(pubkey, compressed=False)
        with pytest.raises(BTClibValueError):
            pubkeyinfo_from_pubkey(pubkey, "testnet", compressed=True)

    for pubkey in [xpub_data] + net_aware_pub_keys:
        assert Q == point_from_pubkey(pubkey)
        with pytest.raises(BTClibValueError):
            point_from_pubkey(pubkey, secp256r1)
        assert pubkeyinfo_from_pubkey(pubkey) in (m_c, m_unc)
        assert pubkeyinfo_from_pubkey(pubkey, "mainnet") in (m_c, m_unc)
        with pytest.raises(BTClibValueError):
            pubkeyinfo_from_pubkey(pubkey, "testnet")

    for pubkey in net_unaware_pub_keys:
        assert Q == point_from_pubkey(pubkey)
        with pytest.raises(BTClibValueError):
            point_from_pubkey(pubkey, secp256r1)
        assert pubkeyinfo_from_pubkey(pubkey) in (m_c, m_unc)
        assert pubkeyinfo_from_pubkey(pubkey, "mainnet") in (m_c, m_unc)
        assert pubkeyinfo_from_pubkey(pubkey, "testnet") in (t_c, t_unc)

    for invalid_pub_key in [INF, INF_xpub_data] + invalid_pub_keys:
        with pytest.raises(BTClibValueError):
            point_from_pubkey(invalid_pub_key)
        with pytest.raises(BTClibValueError):
            pubkeyinfo_from_pubkey(invalid_pub_key)

    for not_a_pub_key in ([INF, INF_xpub_data] + not_a_pub_keys + [q, q0, qn] +
                          plain_prv_keys +
                          [xprv_data, xprv0_data, xprvn_data] +
                          compressed_prv_keys + uncompressed_prv_keys):
        with pytest.raises(BTClibValueError):
            point_from_pubkey(not_a_pub_key)
        with pytest.raises(BTClibValueError):
            pubkeyinfo_from_pubkey(not_a_pub_key)

    for key in [Q] + plain_pub_keys + [q] + plain_prv_keys:
        assert Q == point_from_key(key)
        assert m_c == pubkeyinfo_from_key(key)
        assert m_c == pubkeyinfo_from_key(key, "mainnet")
        assert m_c == pubkeyinfo_from_key(key, "mainnet", compressed=True)
        assert m_c == pubkeyinfo_from_key(key, compressed=True)
        assert m_unc == pubkeyinfo_from_key(key, "mainnet", compressed=False)
        assert m_unc == pubkeyinfo_from_key(key, compressed=False)
        assert t_c == pubkeyinfo_from_key(key, "testnet")
        assert t_c == pubkeyinfo_from_key(key, "testnet", compressed=True)
        assert t_unc == pubkeyinfo_from_key(key, "testnet", compressed=False)

    for key in compressed_pub_keys + [xpub_data, xprv_data
                                      ] + compressed_prv_keys:
        assert Q == point_from_key(key)
        with pytest.raises(BTClibValueError):
            point_from_key(key, secp256r1)
        assert m_c == pubkeyinfo_from_key(key)
        assert m_c == pubkeyinfo_from_key(key, "mainnet")
        assert m_c == pubkeyinfo_from_key(key, "mainnet", compressed=True)
        assert m_c == pubkeyinfo_from_key(key, compressed=True)
        with pytest.raises(BTClibValueError):
            pubkeyinfo_from_key(key, "mainnet", compressed=False)
        with pytest.raises(BTClibValueError):
            pubkeyinfo_from_key(key, compressed=False)

    for key in uncompressed_pub_keys + uncompressed_prv_keys:
        assert Q == point_from_key(key)
        with pytest.raises(BTClibValueError):
            point_from_key(key, secp256r1)
        assert m_unc == pubkeyinfo_from_key(key)
        assert m_unc == pubkeyinfo_from_key(key, "mainnet")
        with pytest.raises(BTClibValueError):
            pubkeyinfo_from_key(key, "mainnet", compressed=True)
        with pytest.raises(BTClibValueError):
            pubkeyinfo_from_key(key, compressed=True)
        assert m_unc == pubkeyinfo_from_key(key, "mainnet", compressed=False)
        assert m_unc == pubkeyinfo_from_key(key, compressed=False)

    for key in net_aware_pub_keys + [xpub_data, xprv_data
                                     ] + net_aware_prv_keys:
        assert Q == point_from_key(key)
        with pytest.raises(BTClibValueError):
            point_from_key(key, secp256r1)
        assert pubkeyinfo_from_key(key) in (m_c, m_unc)
        assert pubkeyinfo_from_key(key, "mainnet") in (m_c, m_unc)
        with pytest.raises(BTClibValueError):
            pubkeyinfo_from_key(key, "testnet")

    for key in [q] + net_unaware_prv_keys + net_unaware_pub_keys:
        assert Q == point_from_key(key)
        assert pubkeyinfo_from_key(key) in (m_c, m_unc)
        assert pubkeyinfo_from_key(key, "mainnet") in (m_c, m_unc)
        assert pubkeyinfo_from_key(key, "testnet") in (t_c, t_unc)

    for invalid_key in ([INF, INF_xpub_data] + invalid_pub_keys +
                        [q0, qn, xprv0_data, xprvn_data] + invalid_prv_keys):
        with pytest.raises(BTClibValueError):
            point_from_key(invalid_key)
        with pytest.raises(BTClibValueError):
            pubkeyinfo_from_key(invalid_key)

    for not_a_key in [
            q0,
            qn,
            xprv0_data,
            xprvn_data,
            INF,
            INF_xpub_data,
    ] + not_a_pub_keys:
        with pytest.raises(BTClibValueError):
            point_from_key(not_a_key)
        with pytest.raises(BTClibValueError):
            pubkeyinfo_from_key(not_a_key)
Exemple #2
0
    def test_pubkeyinfo_from_key(self):

        # prvkeys
        xprv = (
            "xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiC"
            "hkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi").encode()
        xprv_str = xprv.decode("ascii")
        xprv_dict = bip32.deserialize(xprv)
        q, network, _ = _prvkeyinfo_from_xprv(xprv)
        ec = NETWORKS[network]["curve"]
        q_bytes = q.to_bytes(ec.nsize, "big")
        wif1 = wif_from_prvkey(q, network, True)
        wif2 = wif_from_prvkey(q, network, False)

        # pubkeys
        Q = mult(q, ec.G, ec)
        xpub = bip32.xpub_from_xprv(xprv)
        xpub_str = xpub.decode("ascii")
        xpub_dict = bip32.deserialize(xpub)
        Q_compr = xpub_dict["key"]
        Q_compr_hexstr = Q_compr.hex()
        Q_uncompr = bytes_from_point(Q, ec, False)
        Q_uncompr_hexstr = Q_uncompr.hex()

        # int prvkey, compressed result
        self.assertEqual(pubkeyinfo_from_key(q)[0], Q_compr)
        self.assertEqual(pubkeyinfo_from_key(q_bytes)[0], Q_compr)

        # int prvkey, uncompressed result
        self.assertEqual(
            pubkeyinfo_from_key(q, compressed=False)[0], Q_uncompr)
        self.assertEqual(
            pubkeyinfo_from_key(q_bytes, compressed=False)[0], Q_uncompr)

        # compressed wif prvkey, both results
        self.assertEqual(pubkeyinfo_from_key(wif1)[0], Q_compr)
        self.assertRaises(ValueError,
                          pubkeyinfo_from_key,
                          wif1,
                          compressed=False)

        # uncompressed wif prvkey, both results
        self.assertRaises(ValueError,
                          pubkeyinfo_from_key,
                          wif2,
                          compressed=True)
        self.assertEqual(pubkeyinfo_from_key(wif2)[0], Q_uncompr)

        # (compressed) BIP32 prvkey, compressed results
        self.assertEqual(pubkeyinfo_from_key(xprv)[0], Q_compr)
        self.assertEqual(pubkeyinfo_from_key(xprv_str)[0], Q_compr)
        self.assertEqual(pubkeyinfo_from_key(" " + xprv_str + " ")[0], Q_compr)
        self.assertEqual(pubkeyinfo_from_key(xprv_dict)[0], Q_compr)

        # (compressed) BIP32 prvkey, uncompressed result
        self.assertRaises(ValueError,
                          pubkeyinfo_from_key,
                          xprv,
                          compressed=False)
        self.assertRaises(ValueError,
                          pubkeyinfo_from_key,
                          xprv_str,
                          compressed=False)
        self.assertRaises(ValueError,
                          pubkeyinfo_from_key,
                          " " + xprv_str + " ",
                          compressed=False)
        self.assertRaises(ValueError,
                          pubkeyinfo_from_key,
                          xprv_dict,
                          compressed=False)

        # (compressed) BIP32 pubkey, compressed results
        self.assertEqual(pubkeyinfo_from_key(xpub)[0], Q_compr)
        self.assertEqual(pubkeyinfo_from_key(xpub_str)[0], Q_compr)
        self.assertEqual(pubkeyinfo_from_key(" " + xpub_str + " ")[0], Q_compr)
        self.assertEqual(pubkeyinfo_from_key(xpub_dict)[0], Q_compr)

        # (compressed) BIP32 pubkey, uncompressed result
        self.assertRaises(ValueError,
                          pubkeyinfo_from_key,
                          xpub,
                          compressed=False)
        self.assertRaises(ValueError,
                          pubkeyinfo_from_key,
                          xpub_str,
                          compressed=False)
        self.assertRaises(ValueError,
                          pubkeyinfo_from_key,
                          " " + xpub_str + " ",
                          compressed=False)
        self.assertRaises(ValueError,
                          pubkeyinfo_from_key,
                          xpub_dict,
                          compressed=False)

        # compressed SEC Octets (pubkey), compressed results
        self.assertEqual(pubkeyinfo_from_key(Q_compr)[0], Q_compr)
        self.assertEqual(pubkeyinfo_from_key(Q_compr_hexstr)[0], Q_compr)
        self.assertEqual(
            pubkeyinfo_from_key(" " + Q_compr_hexstr + " ")[0], Q_compr)

        # compressed SEC Octets (pubkey), uncompressed results
        self.assertRaises(ValueError,
                          pubkeyinfo_from_key,
                          Q_compr,
                          compressed=False)
        self.assertRaises(ValueError,
                          pubkeyinfo_from_key,
                          Q_compr_hexstr,
                          compressed=False)
        self.assertRaises(
            ValueError,
            pubkeyinfo_from_key,
            " " + Q_compr_hexstr + " ",
            compressed=False,
        )
        self.assertRaises(ValueError, pubkeyinfo_from_key, b"\x00" + Q_compr)
        self.assertRaises(ValueError, pubkeyinfo_from_key,
                          Q_compr_hexstr + "00")

        # uncompressed SEC Octets (pubkey), uncompressed results
        self.assertEqual(pubkeyinfo_from_key(Q_uncompr)[0], Q_uncompr)
        self.assertEqual(pubkeyinfo_from_key(Q_uncompr_hexstr)[0], Q_uncompr)
        self.assertEqual(
            pubkeyinfo_from_key(" " + Q_uncompr_hexstr + " ")[0], Q_uncompr)

        # uncompressed SEC Octets (pubkey), compressed results
        self.assertRaises(ValueError,
                          pubkeyinfo_from_key,
                          Q_uncompr,
                          compressed=True)
        self.assertRaises(ValueError,
                          pubkeyinfo_from_key,
                          Q_uncompr_hexstr,
                          compressed=True)
        self.assertRaises(
            ValueError,
            pubkeyinfo_from_key,
            " " + Q_uncompr_hexstr + " ",
            compressed=True,
        )
        self.assertRaises(ValueError, pubkeyinfo_from_key, b"\x00" + Q_uncompr)
        self.assertRaises(ValueError, pubkeyinfo_from_key,
                          Q_uncompr_hexstr + "00")

        # native tuple input, both results
        self.assertEqual(pubkeyinfo_from_key(Q)[0], Q_compr)
        self.assertEqual(
            pubkeyinfo_from_key(Q, compressed=False)[0], Q_uncompr)

        # pubkeyinfo_from_pubkey does not accept prvkey inputs
        self.assertRaises(TypeError, pubkeyinfo_from_pubkey, q)
        self.assertRaises(ValueError, pubkeyinfo_from_pubkey, q_bytes)
        self.assertRaises(ValueError, pubkeyinfo_from_pubkey, xprv)
        self.assertRaises(ValueError, pubkeyinfo_from_pubkey, xprv_str)
        self.assertRaises(ValueError, pubkeyinfo_from_pubkey, xprv_dict)
        self.assertRaises(ValueError, pubkeyinfo_from_pubkey, wif1)
        self.assertRaises(ValueError, pubkeyinfo_from_pubkey, wif2)

        # Not a public key:
        xpub_dict_bad = copy.copy(xpub_dict)
        xpub_dict_bad["key"] = b"\x00" + xpub_dict["key"][1:]
        self.assertRaises(ValueError, _pubkeyinfo_from_xpub, xpub_dict_bad)
        # _pubkeyinfo_from_xpub(xpub_dict_bad)

        # Invalid point: 7 is not a field element
        Q = INF
        self.assertRaises(ValueError, pubkeyinfo_from_key, Q)
        Q_compr = b"\x02" + Q[0].to_bytes(ec.psize, "big")
        self.assertRaises(ValueError, pubkeyinfo_from_key, Q_compr)
        Q_uncompr = (b"\x04" + Q[0].to_bytes(ec.psize, "big") +
                     Q[1].to_bytes(ec.psize, "big"))
        self.assertRaises(ValueError, pubkeyinfo_from_key, Q_uncompr)
        Q_compr_hexstr = Q_compr.hex()
        self.assertRaises(ValueError, pubkeyinfo_from_key, Q_compr_hexstr)
        Q_uncompr_hexstr = Q_uncompr.hex()
        self.assertRaises(ValueError, pubkeyinfo_from_key, Q_uncompr_hexstr)
        t = xpub_dict["version"]
        t += xpub_dict["depth"].to_bytes(1, "big")
        t += xpub_dict["parent_fingerprint"]
        t += xpub_dict["index"]
        t += xpub_dict["chain_code"]
        t += Q_compr
        xpub = b58encode(t, 78)
        self.assertRaises(ValueError, pubkeyinfo_from_key, xpub)
        xpub_str = xpub.decode("ascii")
        self.assertRaises(ValueError, pubkeyinfo_from_key, xpub_str)