Skip to content
This repository was archived by the owner on Aug 12, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion aeternity/signing.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import uuid

from nacl.encoding import RawEncoder, HexEncoder
from nacl.signing import SigningKey
from nacl.signing import SigningKey, VerifyKey
from nacl.exceptions import CryptoError
from nacl.pwhash import argon2id
from nacl import secret, utils as nacl_utils
Expand Down Expand Up @@ -213,3 +213,20 @@ def keystore_open(k, password):
encrypted = bytes.fromhex(k.get("crypto", {}).get("ciphertext"))
private_key = box.decrypt(encrypted, nonce=nonce, encoder=RawEncoder)
return private_key


def is_signature_valid(account_id, signature, data: bytes) -> bool:
"""
Verify the signature of a message
:param account_id: the account id signing the message
:param signature: the signature of the messagfe
:param data: the message that has been signed
:return: true if the signature for the message is valid, false otherwise
"""
try:
id = hashing.decode(account_id) if isinstance(account_id, str) else account_id
sg = hashing.decode(signature) if isinstance(signature, str) else signature
VerifyKey(id).verify(data, sg)
return True
except Exception as e:
return False
26 changes: 25 additions & 1 deletion tests/test_signing.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from pytest import raises
from tests import ACCOUNT, NODE_CLI, tempdir, TEST_FEE, TEST_TTL
from aeternity.signing import Account
from aeternity.signing import Account, is_signature_valid
from aeternity.utils import is_valid_hash
from aeternity import hashing
import os


Expand Down Expand Up @@ -71,3 +72,26 @@ def test_signing_keystore_save_load_wrong_pwd():
with raises(ValueError):
a = Account.from_keystore(path, "nononon")
assert a.get_address() == ACCOUNT.get_address()


def test_signing_is_signature_valid():
sg_ae = "sg_6cXUU8rimh8B3byLHJA9SaG29uRggtyrpGi5YAFiL9cJUoVtMX4P4kpd4UPTjiGXYSaquSN3gidJ73U8CtfweQ14GFgsC"
account_id = "ak_axjxzUJpj9siJDQKZrBFNTvQLR2JwcZoVPjgdCQdnGUtwf66r"

sg_b64 = "KuZVJ8kK6xCmujLfd8AjU3IfENn1WwcQRA0hI/WWzyXp97zerFg9XRx/ICHcHRGmvxstsul/QEDma2uHf6DIAEcr8Vs="
account_b64 = "TRza0pA9oaZw7tltPULKlkRaGV2qXT9vJx9q2HGY4Lom4qR3"

msg = "aeternity".encode("utf-8")

assert is_signature_valid(account_id, sg_ae, msg)
assert is_signature_valid(
hashing._base64_decode(account_b64),
hashing._base64_decode(sg_b64),
msg)

msg = "wrong".encode("utf-8")
assert not is_signature_valid(account_id, sg_ae, msg)
assert not is_signature_valid(
hashing._base64_decode(account_b64),
hashing._base64_decode(sg_b64),
msg)