示例#1
0
    def test_from_bytes(self):
        pubkey = PublicKey.from_bytes(data['pubkey_bin'])

        assert pubkey.pair == data['pubkey_pair']
        assert pubkey.compressed is True
        assert pubkey.network is bitforge.networks.default

        with raises(PublicKey.InvalidBinary):
            PublicKey.from_bytes(b'a')

        with raises(PublicKey.InvalidBinary):
            PublicKey.from_bytes(b'a' * 70)
示例#2
0
    def test_from_bytes(self):
        pubkey = PublicKey.from_bytes(data['pubkey_bin'])

        assert pubkey.pair == data['pubkey_pair']
        assert pubkey.compressed is True
        assert pubkey.network is bitforge.networks.default

        with raises(PublicKey.InvalidBinary):
            PublicKey.from_bytes(b'a')

        with raises(PublicKey.InvalidBinary):
            PublicKey.from_bytes(b'a' * 70)
示例#3
0
    def test_from_private_key(self):
        privkey = PrivateKey.from_hex(data['privkey_hex'])
        pubkey  = PublicKey.from_private_key(privkey)

        assert pubkey.network is privkey.network
        assert pubkey.compressed == privkey.compressed
        assert pubkey.pair == data['pubkey_pair']
示例#4
0
    def test_from_private_key(self):
        privkey = PrivateKey.from_hex(data['privkey_hex'])
        pubkey = PublicKey.from_private_key(privkey)

        assert pubkey.network is privkey.network
        assert pubkey.compressed == privkey.compressed
        assert pubkey.pair == data['pubkey_pair']
示例#5
0
    def from_bytes(b):
        """ Generates either a HDPrivateKey or HDPublicKey from the underlying
            bytes.
            The serialization must conform to the description in:
            https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#serialization-format
        Args:
            b (bytes): A byte stream conforming to the above.
        Returns:
            HDPrivateKey or HDPublicKey: Either an HD private or
                public key object, depending on what was serialized.
        """
        if len(b) < 78:
            raise ValueError("b must be at least 78 bytes long.")

        version = int.from_bytes(b[:4], 'big')
        depth = b[4]
        parent_fingerprint = b[5:9]
        index = int.from_bytes(b[9:13], 'big')
        chain_code = b[13:45]
        key_bytes = b[45:78]

        rv = None
        if version == HDPrivateKey.MAINNET_VERSION or version == HDPrivateKey.TESTNET_VERSION:
            if key_bytes[0] != 0:
                raise ValueError("First byte of private key must be 0x00!")

            private_key = int.from_bytes(key_bytes[1:], 'big')
            rv = HDPrivateKey(key=private_key,
                              chain_code=chain_code,
                              index=index,
                              depth=depth,
                              parent_fingerprint=parent_fingerprint)
        elif version == HDPublicKey.MAINNET_VERSION or version == HDPublicKey.TESTNET_VERSION:
            if key_bytes[0] != 0x02 and key_bytes[0] != 0x03:
                raise ValueError("First byte of public key must be 0x02 or 0x03!")

            public_key = PublicKey.from_bytes(key_bytes)
            rv = HDPublicKey(x=public_key.point.x,
                             y=public_key.point.y,
                             chain_code=chain_code,
                             index=index,
                             depth=depth,
                             parent_fingerprint=parent_fingerprint)
        else:
            raise ValueError("incorrect encoding.")

        return (rv, b[78:])
示例#6
0
 def test_to_hex_uncompressed(self):
     string = data['pubkey_hex']['uncompressed']
     assert PublicKey.from_hex(string).to_hex() == string
示例#7
0
 def test_from_hex_errors(self):
     with raises(PublicKey.InvalidHex): PublicKey.from_hex('a')
     with raises(PublicKey.InvalidHex): PublicKey.from_hex('a@')
示例#8
0
    def test_from_hex_uncompressed(self):
        pubkey = PublicKey.from_hex(data['pubkey_hex']['uncompressed'])

        assert pubkey.pair == data['pubkey_pair']
        assert pubkey.compressed is False
        assert pubkey.network is bitforge.networks.default
示例#9
0
 def test_to_address_test_uncompress(self):
     pubkey = PublicKey.from_hex(data['pubkey_hex']['uncompressed'], bitforge.networks.testnet)
     assert pubkey.to_address().to_string() == data['address']['test_uncompressed']
示例#10
0
 def test_unknown_network(self):
     with raises(PublicKey.UnknownNetwork):
         PublicKey(data['pubkey_pair'], network='a')
示例#11
0
 def test_to_bytes(self):
     bytes = data['pubkey_bin']
     assert PublicKey.from_bytes(bytes).to_bytes() == bytes
示例#12
0
 def test_to_hex_uncompressed(self):
     string = data['pubkey_hex']['uncompressed']
     assert PublicKey.from_hex(string).to_hex() == string
示例#13
0
 def test_from_hex_errors(self):
     with raises(PublicKey.InvalidHex):
         PublicKey.from_hex('a')
     with raises(PublicKey.InvalidHex):
         PublicKey.from_hex('a@')
示例#14
0
    def test_from_hex_uncompressed(self):
        pubkey = PublicKey.from_hex(data['pubkey_hex']['uncompressed'])

        assert pubkey.pair == data['pubkey_pair']
        assert pubkey.compressed is False
        assert pubkey.network is bitforge.networks.default
示例#15
0
 def test_to_address_live_compress(self):
     pubkey = PublicKey.from_hex(data['pubkey_hex']['compressed'],
                                 bitforge.networks.livenet)
     assert pubkey.to_address().to_string(
     ) == data['address']['live_compressed']
示例#16
0
 def test_to_bytes(self):
     bytes = data['pubkey_bin']
     assert PublicKey.from_bytes(bytes).to_bytes() == bytes
示例#17
0
 def test_invalid_pair(self):
     with raises(PublicKey.InvalidPair):
         PublicKey((0, 0))