|
|
|
@ -4,7 +4,7 @@ import re |
|
|
|
|
from typing import Optional |
|
|
|
|
|
|
|
|
|
import bip39 |
|
|
|
|
from ed25519 import SigningKey, VerifyingKey, create_keypair |
|
|
|
|
from ed25519 import BadSignatureError, SigningKey, VerifyingKey, create_keypair |
|
|
|
|
|
|
|
|
|
from .base32 import base32_bytes_to_string, base32_string_to_bytes |
|
|
|
|
from .exc import ValidationError |
|
|
|
@ -23,8 +23,8 @@ class Identity: |
|
|
|
|
|
|
|
|
|
_KEY_PATTERN = f'b[{B32_CHAR}]{{52}}' |
|
|
|
|
_NAME_PATTERN = f'[{ALPHA_LOWER}][{ALPHA_LOWER_OR_DIGIT}]{{3}}' |
|
|
|
|
_PATTERN = f'^@{_NAME_PATTERN}\\.{_KEY_PATTERN}$' |
|
|
|
|
_SECRET_PATTERN = f'^{_KEY_PATTERN}$' |
|
|
|
|
PATTERN = f'^@{_NAME_PATTERN}\\.{_KEY_PATTERN}$' |
|
|
|
|
|
|
|
|
|
def __init__( |
|
|
|
|
self, |
|
|
|
@ -143,3 +143,23 @@ class Identity: |
|
|
|
|
mnemonic = bip39.encode_bytes(seed) |
|
|
|
|
|
|
|
|
|
return f'{self.name} {mnemonic}' |
|
|
|
|
|
|
|
|
|
def sign(self, data: str) -> str: |
|
|
|
|
"""Sign data""" |
|
|
|
|
|
|
|
|
|
if not self.sign_key: |
|
|
|
|
raise TypeError('This identity doesn’t have a signing key') |
|
|
|
|
|
|
|
|
|
return base32_bytes_to_string(self.sign_key.sign(data.encode('utf-8'))) |
|
|
|
|
|
|
|
|
|
def verify(self, data: str, signature: str) -> bool: |
|
|
|
|
"""Verify if data is signed by us""" |
|
|
|
|
|
|
|
|
|
signature_bytes = base32_string_to_bytes(signature) |
|
|
|
|
|
|
|
|
|
try: |
|
|
|
|
self.verify_key.verify(signature_bytes, data.encode('utf-8')) |
|
|
|
|
except BadSignatureError: |
|
|
|
|
return False |
|
|
|
|
|
|
|
|
|
return True |
|
|
|
|