Beispiel #1
0
def test_encode_empty():
    addr = Address(None, empty=True)
    assert str(addr) == ''

    addr = Address(None, empty=True)
    addr.value = bytes([])
    assert str(addr) == ''
Beispiel #2
0
def test_error():
    with pytest.raises(AssertionError):
        Address(pubkey=None)

    with pytest.raises(TypeError):
        Address(pubkey=1234)

    with pytest.raises(ValueError):
        Address(pubkey="1234")

    with pytest.raises(TypeError):
        Address(pubkey=["1234"])
Beispiel #3
0
def test_import():
    acc = Account.decrypt_account(exported_key, '1234')
    assert str(
        acc.address) == "AmLnc5nhXjL3a3GUzv1Hb44LnGPvhZufDZ8s8oE9kazgbW6FgnUa"
    assert str(acc.private_key
               ) == "6i2n6TAtsKBaWSujYFzqkBCiP834j1u3nwmffZb8dxCxDcQaXAb"

    assert acc.address == acc.private_key.address
    assert acc.public_key == acc.private_key.public_key
    assert acc.public_key.point == acc.private_key.public_key.point
    assert acc.public_key != acc.address.public_key
    assert acc.public_key.point == acc.address.public_key.point
    assert acc.private_key.public_key != acc.address.public_key
    assert acc.private_key.public_key.point == acc.address.public_key.point

    assert bytes(acc.address) == convert_public_key_to_bytes(acc.public_key)
    assert bytes(acc.address) == convert_public_key_to_bytes(
        acc.private_key.public_key)
    assert bytes(acc.address) == convert_public_key_to_bytes(
        acc.address.public_key)
    assert bytes(acc.private_key.address) == convert_public_key_to_bytes(
        acc.public_key)

    private_key = PrivateKey(
        pk="6i2n6TAtsKBaWSujYFzqkBCiP834j1u3nwmffZb8dxCxDcQaXAb")
    assert acc.public_key != private_key.public_key
    assert acc.public_key.point == private_key.public_key.point
    assert private_key.public_key != acc.address.public_key
    assert private_key.public_key.point == acc.address.public_key.point

    assert bytes(acc.address) == convert_public_key_to_bytes(
        private_key.public_key)
    assert bytes(private_key.address) == convert_public_key_to_bytes(
        acc.public_key)

    address = Address(None, empty=True)
    address.value = "AmLnc5nhXjL3a3GUzv1Hb44LnGPvhZufDZ8s8oE9kazgbW6FgnUa"
    assert acc.address != address
    assert bytes(acc.address) == bytes(address)
    assert private_key.address != address
    assert bytes(private_key.address) == bytes(address)

    assert acc.public_key != address.public_key
    assert acc.public_key.point == address.public_key.point
    assert private_key.public_key != address.public_key
    assert private_key.public_key.point == address.public_key.point

    assert bytes(acc.address) == convert_public_key_to_bytes(
        address.public_key)
    assert bytes(private_key.address) == convert_public_key_to_bytes(
        address.public_key)
Beispiel #4
0
def test_compare_pubkey():
    private_key = "6i2n6TAtsKBaWSujYFzqkBCiP834j1u3nwmffZb8dxCxDcQaXAb"
    address = "AmLnc5nhXjL3a3GUzv1Hb44LnGPvhZufDZ8s8oE9kazgbW6FgnUa"

    pubkey_compressed = decode_address(address).hex()
    pubkey_uncompressed = uncompress_key(pubkey_compressed)
    pubkey_str = bytes.fromhex(pubkey_uncompressed)
    pubkey_bytes = bytes.fromhex(pubkey_uncompressed)[1:]
    pubkey_bytes_x = pubkey_bytes[:ecdsa.SECP256k1.baselen]
    pubkey_bytes_y = pubkey_bytes[ecdsa.SECP256k1.baselen:]
    pubkey_x = ecdsa.util.string_to_number(pubkey_bytes_x)
    pubkey_y = ecdsa.util.string_to_number(pubkey_bytes_y)
    assert ecdsa.ecdsa.point_is_valid(ecdsa.SECP256k1.generator, pubkey_x,
                                      pubkey_y)

    pubkey_point = ecdsa.ellipticcurve.Point(ecdsa.SECP256k1.curve, pubkey_x,
                                             pubkey_y, ecdsa.SECP256k1.order)
    pubkey = ecdsa.ecdsa.Public_key(ecdsa.SECP256k1.generator, pubkey_point)

    pk = PrivateKey(pk=private_key)
    pk_pubkey = pk.public_key
    pk_pubkey_str = convert_public_key_to_bytes(pk.public_key,
                                                compressed=False)
    pk_pubkey_c_str = convert_public_key_to_bytes(pk.public_key,
                                                  compressed=True)

    assert address == str(pk.address)
    assert pubkey.point == pk_pubkey.point
    assert pubkey_str == pk_pubkey_str
    assert address == encode_address(pk_pubkey_c_str)

    addr = Address(None, empty=True)
    addr.value = address
    addr_pubkey = addr.public_key
    addr_pubkey_str = convert_public_key_to_bytes(addr.public_key,
                                                  compressed=False)
    addr_pubkey_c_str = convert_public_key_to_bytes(addr.public_key,
                                                    compressed=True)

    assert pubkey.point == addr_pubkey.point
    assert pubkey_str == addr_pubkey_str
    assert address == encode_address(addr_pubkey_c_str)
Beispiel #5
0
def test_signature():
    msg = bytes("test", 'utf-8')
    h = hashlib.sha256(msg).digest()
    priv_key = PrivateKey(None)
    signature = priv_key.sign_msg(h)
    address = Address(priv_key.public_key).__str__()

    # check success
    assert verify_sig(h, signature, address)

    # check wrong signature from another key
    priv_key = PrivateKey(None)
    signature = priv_key.sign_msg(h)
    assert not verify_sig(h, signature, address)
Beispiel #6
0
def test_govname():
    addr = Address(None, empty=True)
    name = 'abcdetest'
    addr.value = name.encode()
    assert len(str(addr)) == len(name)

    addr = Address(None, empty=True)
    baddr = bytes([
        0x61, 0x65, 0x72, 0x67, 0x6f, 0x2e, 0x65, 0x6e, 0x74, 0x65, 0x72, 0x70,
        0x72, 0x69, 0x73, 0x65
    ])
    addr.value = baddr
    assert str(addr) == 'aergo.enterprise'
    assert str(addr) == Address.encode(baddr)
    assert bytes(addr) == Address.decode('aergo.enterprise')
    assert baddr == Address.decode('aergo.enterprise')

    addr = Address(None, empty=True)
    addr.value = 'aergo.system'
    assert str(addr) == 'aergo.system'
    assert bytes(addr) == Address.decode('aergo.system')
Beispiel #7
0
def test_success():
    sk = ecdsa.SigningKey.generate(curve=ecdsa.SECP256k1,
                                   hashfunc=hashlib.sha256)
    addr = Address(pubkey=sk.privkey.public_key)
    assert type(addr) is Address
    assert isinstance(str(addr), str)
    assert isinstance(bytes(addr), bytes)
    assert bytes(addr) == addr.value
    assert bytes(addr) == convert_public_key_to_bytes(pubkey=addr.public_key,
                                                      curve=addr.curve,
                                                      compressed=True)
    with pytest.raises(ValueError):
        addr.value = str(addr)

    addr_str = str(addr)

    addr2 = Address(None, empty=True)
    addr2.value = addr_str
    assert type(addr2) is Address
    assert isinstance(str(addr2), str)
    assert isinstance(bytes(addr2), bytes)
    assert bytes(addr2) == addr2.value
    assert bytes(addr2) == convert_public_key_to_bytes(pubkey=addr2.public_key,
                                                       curve=addr2.curve,
                                                       compressed=True)

    assert addr2 != addr
    assert addr2.value == addr.value
    assert str(addr2) == str(addr)
    assert bytes(addr2) == bytes(addr)
    assert addr2.public_key.point == addr.public_key.point
    assert convert_public_key_to_bytes(pubkey=addr.public_key,
                                       curve=addr.curve,
                                       compressed=False) == \
           convert_public_key_to_bytes(pubkey=addr2.public_key,
                                       curve=addr2.curve,
                                       compressed=False)

    pubkey3 = convert_public_key_to_bytes(pubkey=addr.public_key,
                                          curve=addr.curve,
                                          compressed=False)
    pubkey4 = convert_public_key_to_bytes(pubkey=addr2.public_key,
                                          curve=addr.curve,
                                          compressed=True)
    addr3 = Address(pubkey=pubkey3)
    assert addr3.value == addr.value
    assert bytes(addr3) == bytes(addr)
    addr4 = Address(pubkey=pubkey4)
    assert addr4.value == addr2.value
    assert bytes(addr4) == bytes(addr2)

    assert addr4 != addr3
    assert str(addr4) == str(addr3)
    assert bytes(addr4) == bytes(addr3)
    assert addr4.public_key.point == addr3.public_key.point

    addr5 = Address(None, empty=True)
    addr5.value = bytes(addr3)
    assert addr4 != addr5
    assert str(addr4) == str(addr5)
    assert bytes(addr4) == bytes(addr5)
    assert addr4.public_key.point == addr5.public_key.point