def test_change_password(self): rpc = Rpc("http://localhost:8080", True) passphrase1 = f"some passphrase {random()}" passphrase2 = f"another passphrase {random()}" secret = generate_private_key() account = rpc.account.import_raw( "0x" + binascii.hexlify(secret).decode("ascii"), passphrase1) message = "0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef" signature1 = rpc.account.sign(message, account, passphrase1) expected = sign_ecdsa(bytes.fromhex(message[2:]), secret) assert bytes.fromhex(signature1[2:]) == expected with pytest.raises(Exception): rpc.account.sign(message, account, passphrase2) rpc.account.change_password(account, passphrase1, passphrase2) with pytest.raises(Exception): rpc.account.sign(message, account, passphrase1) signature2 = rpc.account.sign(message, account, passphrase2) assert bytes.fromhex(signature2[2:]) == expected
def test_sign_verify(): message = bytes(32) priv = generate_private_key() pub = get_public_from_private(priv) sig = sign_ecdsa(message, priv) assert verify_ecdsa(message, sig, pub)
def test_sign_recover(): message = bytes(32) priv = generate_private_key() pub = get_public_from_private(priv) sig = sign_ecdsa(message, priv) assert recover_ecdsa(message, sig) == pub
def test_register_remote_key(self): rpc = Rpc("http://localhost:8080", True) address = self.random_ip() port = self.random_port() remote_public_key = get_public_from_private(generate_private_key()) local = rpc.net.register_remote_key_for( address, port, "0x" + binascii.hexlify(remote_public_key).decode("ascii")) assert rpc.net.locak_key_for(address, port) == local
def test_cannot_sign_without_passphrase(self): rpc = Rpc("http://localhost:8080", True) passphrase = f"some passphrase {random()}" secret = generate_private_key() account = rpc.account.import_raw( "0x" + binascii.hexlify(secret).decode("ascii"), passphrase) message = "0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef" with pytest.raises(Exception): rpc.account.sign(message, account, None)
def test_sign(self): rpc = Rpc("http://localhost:8080", True) passphrase = f"some passphrase {random()}" secret = generate_private_key() account = rpc.account.import_raw( "0x" + binascii.hexlify(secret).decode("ascii"), passphrase) message = "0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef" signature = rpc.account.sign(message, account, passphrase) expected = sign_ecdsa(bytes.fromhex(message[2:]), secret) assert bytes.fromhex(signature[2:]) == expected
def test_import_raw(self): rpc = Rpc("http://localhost:8080", True) before_list = rpc.account.get_list() passphrase = f"some passphrase {random()}" secret = generate_private_key() account = rpc.account.import_raw( "0x" + binascii.hexlify(secret).decode("ascii"), passphrase) after_list = rpc.account.get_list() before_list.append(account) before_list.sort() after_list.sort() assert before_list == after_list
def test_unlock_with_duration(self): rpc = Rpc("http://localhost:8080", True) passphrase = f"some passphrase {random()}" secret = generate_private_key() account = rpc.account.import_raw( "0x" + binascii.hexlify(secret).decode("ascii"), passphrase) message = "0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef" rpc.account.unlock(account, passphrase, 1) signature = rpc.account.sign(message, account, None) expected = sign_ecdsa(bytes.fromhex(message[2:]), secret) assert bytes.fromhex(signature[2:]) == expected time.sleep(2) with pytest.raises(Exception): rpc.account.sign(message, account, None)
def test_storage_encoding(): private_key = generate_private_key() storage = encode(private_key, KeyType.ASSET, "passphrase", "metadata") private = decode(storage, "passphrase") assert private_key == bytes.fromhex(private)
def test_generate_private_key(): priv = generate_private_key() assert (re.match("^[0-9a-fA-F]{64}$", binascii.hexlify(priv).decode("ascii")) is not None)
def test_get_public_from_private(): priv = generate_private_key() pub = get_public_from_private(priv) assert (re.match("^[0-9a-fA-F]{128}$", binascii.hexlify(pub).decode("ascii")) is not None)