def test_wif_from_prvkey(self):
        q = 0xC28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D

        # compressed WIF
        wif = wif_from_prvkey(q, True)
        self.assertEqual(
            wif, b'KwdMAjGmerYanjeui5SHS7JkmpZvVipYvB2LJGU1ZxJwYvP98617')
        q2 = prvkey_from_wif(wif)
        self.assertEqual(q2[0], q)
        self.assertEqual(q2[1], True)

        # uncompressed WIF
        wif = wif_from_prvkey(q, False)
        self.assertEqual(
            wif, b'5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ')
        q3 = prvkey_from_wif(wif)
        self.assertEqual(q3[0], q)
        self.assertEqual(q3[1], False)

        # private key not in (0, n)
        badq = ec.n
        self.assertRaises(ValueError, wif_from_prvkey, badq, True)
        #wif = wif_from_prvkey(badq, True)

        # Not a private key WIF: missing leading 0x80
        payload = b'\x81' + octets_from_int(badq, ec.psize)
        badwif = base58.encode_check(payload)
        self.assertRaises(ValueError, prvkey_from_wif, badwif)
        #prvkey_from_wif(badwif)

        # Not a compressed WIF: missing trailing 0x01
        payload = b'\x80' + octets_from_int(badq, ec.psize) + b'\x00'
        badwif = base58.encode_check(payload)
        self.assertRaises(ValueError, prvkey_from_wif, badwif)
        # prvkey_from_wif(badwif)

        # Not a WIF: wrong size (35)
        payload = b'\x80' + octets_from_int(badq, ec.psize) + b'\x01\x00'
        badwif = base58.encode_check(payload)
        self.assertRaises(ValueError, prvkey_from_wif, badwif)
        #prvkey_from_wif(badwif)

        # Not a WIF: private key not in (0, n)
        payload = b'\x80' + octets_from_int(badq, ec.psize)
        badwif = base58.encode_check(payload)
        self.assertRaises(ValueError, prvkey_from_wif, badwif)
Exemple #2
0
    def test_wif_from_prvkey(self):
        p_num = 0xC28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D
        p_bytes = bytes_from_Scalar(ec, p_num)
        p_hex = p_bytes.hex()

        # private key as number
        wif = wif_from_prvkey(p_num)
        self.assertEqual(
            wif, b'KwdMAjGmerYanjeui5SHS7JkmpZvVipYvB2LJGU1ZxJwYvP98617')
        p2 = prvkey_from_wif(wif)
        self.assertEqual(p2[0], p_bytes)
        self.assertEqual(p2[1], True)
        wif = wif_from_prvkey(p_num, False)
        self.assertEqual(
            wif, b'5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ')
        p3 = prvkey_from_wif(wif)
        self.assertEqual(p3[0], p_bytes)
        self.assertEqual(p3[1], False)

        # private key as bytes, i.e. the preferred format
        wif = wif_from_prvkey(p_bytes)
        self.assertEqual(
            wif, b'KwdMAjGmerYanjeui5SHS7JkmpZvVipYvB2LJGU1ZxJwYvP98617')
        p4 = prvkey_from_wif(wif)
        self.assertEqual(p4[0], p_bytes)
        self.assertEqual(p4[1], True)
        wif = wif_from_prvkey(p_bytes, False)
        self.assertEqual(
            wif, b'5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ')
        p5 = prvkey_from_wif(wif)
        self.assertEqual(p5[0], p_bytes)
        self.assertEqual(p5[1], False)

        # private key as hex string
        wif = wif_from_prvkey(p_hex)
        self.assertEqual(
            wif, b'KwdMAjGmerYanjeui5SHS7JkmpZvVipYvB2LJGU1ZxJwYvP98617')
        p6 = prvkey_from_wif(wif)
        self.assertEqual(p6[0], p_bytes)
        self.assertEqual(p6[1], True)
        wif = wif_from_prvkey(p_hex, False)
        self.assertEqual(
            wif, b'5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ')
        p7 = prvkey_from_wif(wif)
        self.assertEqual(p7[0], p_bytes)
        self.assertEqual(p7[1], False)

        payload = b'\x80' + p_bytes + b'\x01\x01'
        wif = b58encode_check(payload)
        self.assertRaises(ValueError, prvkey_from_wif, wif)