Move from ed25519 to PyNaCl

This commit is contained in:
Gergely Polonkai 2022-05-06 11:39:23 +02:00
parent 280652d5a7
commit 1299acd463
No known key found for this signature in database
GPG Key ID: 2D2885533B869ED4
9 changed files with 136 additions and 100 deletions

View File

@ -4,7 +4,9 @@ import re
from typing import Optional
import bip39
from ed25519 import BadSignatureError, SigningKey, VerifyingKey, create_keypair
from nacl.encoding import RawEncoder
from nacl.exceptions import BadSignatureError
from nacl.signing import SigningKey, VerifyKey
from .base32 import base32_bytes_to_string, base32_string_to_bytes
from .exc import ValidationError
@ -29,21 +31,17 @@ class Identity:
def __init__(
self,
name: str,
verify_key: Optional[VerifyingKey] = None,
verify_key: Optional[VerifyKey] = None,
sign_key: Optional[SigningKey] = None,
) -> None:
if not self.valid_name(name):
raise ValidationError(f'Invalid name: {name}')
if (
sign_key
and verify_key
and sign_key.get_verifying_key().to_bytes() != verify_key.to_bytes()
):
if sign_key and verify_key and sign_key.verify_key != verify_key:
raise ValidationError('Signing and verifying keys dont match')
if sign_key and not verify_key:
verify_key = sign_key.get_verifying_key()
verify_key = sign_key.verify_key
if not verify_key:
raise ValidationError('At least verify_key must be present')
@ -53,7 +51,9 @@ class Identity:
self.sign_key = sign_key
def __str__(self) -> str:
return f'@{self.name}.{base32_bytes_to_string(self.verify_key.to_bytes())}'
key = base32_bytes_to_string(self.verify_key.encode(RawEncoder))
return f'@{self.name}.{key}'
def __repr__(self) -> str:
if self.sign_key:
@ -81,9 +81,8 @@ class Identity:
address = address[1:]
name, verify_key_data = address.split('.')
verify_key_bytes = base32_string_to_bytes(verify_key_data)
verify_key = VerifyingKey(verify_key_bytes)
verify_key = VerifyKey(verify_key_bytes)
return cls(name, verify_key=verify_key)
@ -109,7 +108,7 @@ class Identity:
try:
verify_key_bytes = base32_string_to_bytes(verify_key_data)
VerifyingKey(verify_key_bytes)
VerifyKey(verify_key_bytes)
except BaseException: # pylint: disable=broad-except
return False
@ -119,7 +118,8 @@ class Identity:
def generate(cls, name: str) -> 'Identity':
"""Generate a new entity"""
sign_key, verify_key = create_keypair()
sign_key = SigningKey.generate()
verify_key = sign_key.verify_key
return cls(name, verify_key=verify_key, sign_key=sign_key)
@ -142,7 +142,7 @@ class Identity:
if self.sign_key is None:
return None
seed = self.sign_key.to_seed()
seed = self.sign_key.encode()
mnemonic = bip39.encode_bytes(seed)
return f'{self.name} {mnemonic}'
@ -153,7 +153,9 @@ class Identity:
if not self.sign_key:
raise TypeError('This identity doesnt have a signing key')
return base32_bytes_to_string(self.sign_key.sign(data.encode('utf-8')))
signed_message = self.sign_key.sign(data.encode('utf-8'))
return base32_bytes_to_string(signed_message.signature)
def verify(self, data: str, signature: str) -> bool:
"""Verify if data is signed by us"""
@ -161,7 +163,7 @@ class Identity:
signature_bytes = base32_string_to_bytes(signature)
try:
self.verify_key.verify(signature_bytes, data.encode('utf-8'))
self.verify_key.verify(data.encode('utf-8'), signature_bytes)
except BadSignatureError:
return False

View File

@ -1,22 +0,0 @@
from .keys import (
BadSignatureError,
BadPrefixError,
create_keypair,
SigningKey,
VerifyingKey,
remove_prefix,
to_ascii,
from_ascii,
)
__all__ = (
'BadSignatureError',
'BadPrefixError',
'create_keypair',
'SigningKey',
'VerifyingKey',
'remove_prefix',
'to_ascii',
'from_ascii',
'__version__'
)

View File

@ -1,3 +0,0 @@
version_json: str
def get_versions(): ...

View File

@ -1,32 +0,0 @@
from typing import Any, Callable, Optional, Tuple
BadSignatureError: Any
def create_keypair(entropy: Callable[[int], bytes] = ...) -> Tuple[SigningKey, VerifyingKey]: ...
class BadPrefixError(Exception): ...
def remove_prefix(s_bytes: bytes, prefix: bytes) -> bytes: ...
def to_ascii(s_bytes: bytes, prefix: str = ..., encoding: str = ...) -> str: ...
def from_ascii(s_ascii: str, prefix: str = ..., encoding: str = ...) -> bytes: ...
class SigningKey:
sk_s: bytes
vk_s: bytes
def __init__(self, sk_s: bytes, prefix: str = ..., encoding: Optional[str] = ...) -> None: ...
def to_bytes(self, prefix: str = ...) -> bytes: ...
def to_ascii(self, prefix: str = ..., encoding: Optional[str] = ...) -> str: ...
def to_seed(self, prefix: str = ...) -> bytes: ...
def __eq__(self, them: object) -> bool: ...
def get_verifying_key(self) -> VerifyingKey: ...
def sign(self, msg: bytes, prefix: str = ..., encoding: Optional[str] = ...) -> bytes: ...
class VerifyingKey:
vk_s: bytes
def __init__(self, vk_s: bytes, prefix: str = ..., encoding: Optional[str] = ...) -> None: ...
def to_bytes(self, prefix: str = ...) -> bytes: ...
def to_ascii(self, prefix: str = ..., encoding: Optional[str] = ...) -> str: ...
def __eq__(self, them: object) -> bool: ...
def verify(self, sig: bytes, msg: bytes, prefix: str = ..., encoding: Optional[str] = ...) -> bool: ...
def selftest() -> None: ...

115
poetry.lock generated
View File

@ -64,6 +64,17 @@ d = ["aiohttp (>=3.7.4)"]
jupyter = ["ipython (>=7.8.0)", "tokenize-rt (>=3.2.0)"]
uvloop = ["uvloop (>=0.15.2)"]
[[package]]
name = "cffi"
version = "1.15.0"
description = "Foreign Function Interface for Python calling C code."
category = "main"
optional = false
python-versions = "*"
[package.dependencies]
pycparser = "*"
[[package]]
name = "click"
version = "8.1.2"
@ -108,14 +119,6 @@ python-versions = ">=2.7, !=3.0.*"
[package.extras]
graph = ["objgraph (>=1.7.2)"]
[[package]]
name = "ed25519"
version = "1.5"
description = "Ed25519 public-key signatures"
category = "main"
optional = false
python-versions = "*"
[[package]]
name = "fastjsonschema"
version = "2.15.3"
@ -241,6 +244,14 @@ category = "dev"
optional = false
python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*"
[[package]]
name = "pycparser"
version = "2.21"
description = "C parser in Python"
category = "main"
optional = false
python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*"
[[package]]
name = "pylint"
version = "2.13.5"
@ -261,6 +272,21 @@ tomli = {version = ">=1.1.0", markers = "python_version < \"3.11\""}
[package.extras]
testutil = ["gitpython (>3)"]
[[package]]
name = "pynacl"
version = "1.5.0"
description = "Python binding to the Networking and Cryptography (NaCl) library"
category = "main"
optional = false
python-versions = ">=3.6"
[package.dependencies]
cffi = ">=1.4.1"
[package.extras]
docs = ["sphinx (>=1.6.5)", "sphinx-rtd-theme"]
tests = ["pytest (>=3.2.1,!=3.3.0)", "hypothesis (>=3.27.0)"]
[[package]]
name = "pyparsing"
version = "3.0.8"
@ -349,7 +375,7 @@ python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,>=2.7"
[metadata]
lock-version = "1.1"
python-versions = "^3.10"
content-hash = "97d56a47bf36410214965fb0aa60352f05af7eb2e2d0b51f1919a42e05f677df"
content-hash = "9d4e957c25769c1ade80ea509b8228362a5c6e69a16b9b48adb94d09c3dd896a"
[metadata.files]
astroid = [
@ -393,6 +419,58 @@ black = [
{file = "black-22.3.0-py3-none-any.whl", hash = "sha256:bc58025940a896d7e5356952228b68f793cf5fcb342be703c3a2669a1488cb72"},
{file = "black-22.3.0.tar.gz", hash = "sha256:35020b8886c022ced9282b51b5a875b6d1ab0c387b31a065b84db7c33085ca79"},
]
cffi = [
{file = "cffi-1.15.0-cp27-cp27m-macosx_10_9_x86_64.whl", hash = "sha256:c2502a1a03b6312837279c8c1bd3ebedf6c12c4228ddbad40912d671ccc8a962"},
{file = "cffi-1.15.0-cp27-cp27m-manylinux1_i686.whl", hash = "sha256:23cfe892bd5dd8941608f93348c0737e369e51c100d03718f108bf1add7bd6d0"},
{file = "cffi-1.15.0-cp27-cp27m-manylinux1_x86_64.whl", hash = "sha256:41d45de54cd277a7878919867c0f08b0cf817605e4eb94093e7516505d3c8d14"},
{file = "cffi-1.15.0-cp27-cp27m-win32.whl", hash = "sha256:4a306fa632e8f0928956a41fa8e1d6243c71e7eb59ffbd165fc0b41e316b2474"},
{file = "cffi-1.15.0-cp27-cp27m-win_amd64.whl", hash = "sha256:e7022a66d9b55e93e1a845d8c9eba2a1bebd4966cd8bfc25d9cd07d515b33fa6"},
{file = "cffi-1.15.0-cp27-cp27mu-manylinux1_i686.whl", hash = "sha256:14cd121ea63ecdae71efa69c15c5543a4b5fbcd0bbe2aad864baca0063cecf27"},
{file = "cffi-1.15.0-cp27-cp27mu-manylinux1_x86_64.whl", hash = "sha256:d4d692a89c5cf08a8557fdeb329b82e7bf609aadfaed6c0d79f5a449a3c7c023"},
{file = "cffi-1.15.0-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:0104fb5ae2391d46a4cb082abdd5c69ea4eab79d8d44eaaf79f1b1fd806ee4c2"},
{file = "cffi-1.15.0-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:91ec59c33514b7c7559a6acda53bbfe1b283949c34fe7440bcf917f96ac0723e"},
{file = "cffi-1.15.0-cp310-cp310-manylinux_2_12_i686.manylinux2010_i686.whl", hash = "sha256:f5c7150ad32ba43a07c4479f40241756145a1f03b43480e058cfd862bf5041c7"},
{file = "cffi-1.15.0-cp310-cp310-manylinux_2_12_x86_64.manylinux2010_x86_64.whl", hash = "sha256:00c878c90cb53ccfaae6b8bc18ad05d2036553e6d9d1d9dbcf323bbe83854ca3"},
{file = "cffi-1.15.0-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:abb9a20a72ac4e0fdb50dae135ba5e77880518e742077ced47eb1499e29a443c"},
{file = "cffi-1.15.0-cp310-cp310-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:a5263e363c27b653a90078143adb3d076c1a748ec9ecc78ea2fb916f9b861962"},
{file = "cffi-1.15.0-cp310-cp310-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:f54a64f8b0c8ff0b64d18aa76675262e1700f3995182267998c31ae974fbc382"},
{file = "cffi-1.15.0-cp310-cp310-win32.whl", hash = "sha256:c21c9e3896c23007803a875460fb786118f0cdd4434359577ea25eb556e34c55"},
{file = "cffi-1.15.0-cp310-cp310-win_amd64.whl", hash = "sha256:5e069f72d497312b24fcc02073d70cb989045d1c91cbd53979366077959933e0"},
{file = "cffi-1.15.0-cp36-cp36m-macosx_10_9_x86_64.whl", hash = "sha256:64d4ec9f448dfe041705426000cc13e34e6e5bb13736e9fd62e34a0b0c41566e"},
{file = "cffi-1.15.0-cp36-cp36m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:2756c88cbb94231c7a147402476be2c4df2f6078099a6f4a480d239a8817ae39"},
{file = "cffi-1.15.0-cp36-cp36m-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:3b96a311ac60a3f6be21d2572e46ce67f09abcf4d09344c49274eb9e0bf345fc"},
{file = "cffi-1.15.0-cp36-cp36m-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:75e4024375654472cc27e91cbe9eaa08567f7fbdf822638be2814ce059f58032"},
{file = "cffi-1.15.0-cp36-cp36m-manylinux_2_5_i686.manylinux1_i686.whl", hash = "sha256:59888172256cac5629e60e72e86598027aca6bf01fa2465bdb676d37636573e8"},
{file = "cffi-1.15.0-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.whl", hash = "sha256:27c219baf94952ae9d50ec19651a687b826792055353d07648a5695413e0c605"},
{file = "cffi-1.15.0-cp36-cp36m-win32.whl", hash = "sha256:4958391dbd6249d7ad855b9ca88fae690783a6be9e86df65865058ed81fc860e"},
{file = "cffi-1.15.0-cp36-cp36m-win_amd64.whl", hash = "sha256:f6f824dc3bce0edab5f427efcfb1d63ee75b6fcb7282900ccaf925be84efb0fc"},
{file = "cffi-1.15.0-cp37-cp37m-macosx_10_9_x86_64.whl", hash = "sha256:06c48159c1abed75c2e721b1715c379fa3200c7784271b3c46df01383b593636"},
{file = "cffi-1.15.0-cp37-cp37m-manylinux_2_12_i686.manylinux2010_i686.whl", hash = "sha256:c2051981a968d7de9dd2d7b87bcb9c939c74a34626a6e2f8181455dd49ed69e4"},
{file = "cffi-1.15.0-cp37-cp37m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl", hash = "sha256:fd8a250edc26254fe5b33be00402e6d287f562b6a5b2152dec302fa15bb3e997"},
{file = "cffi-1.15.0-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:91d77d2a782be4274da750752bb1650a97bfd8f291022b379bb8e01c66b4e96b"},
{file = "cffi-1.15.0-cp37-cp37m-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:45db3a33139e9c8f7c09234b5784a5e33d31fd6907800b316decad50af323ff2"},
{file = "cffi-1.15.0-cp37-cp37m-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:263cc3d821c4ab2213cbe8cd8b355a7f72a8324577dc865ef98487c1aeee2bc7"},
{file = "cffi-1.15.0-cp37-cp37m-win32.whl", hash = "sha256:17771976e82e9f94976180f76468546834d22a7cc404b17c22df2a2c81db0c66"},
{file = "cffi-1.15.0-cp37-cp37m-win_amd64.whl", hash = "sha256:3415c89f9204ee60cd09b235810be700e993e343a408693e80ce7f6a40108029"},
{file = "cffi-1.15.0-cp38-cp38-macosx_10_9_x86_64.whl", hash = "sha256:4238e6dab5d6a8ba812de994bbb0a79bddbdf80994e4ce802b6f6f3142fcc880"},
{file = "cffi-1.15.0-cp38-cp38-manylinux_2_12_i686.manylinux2010_i686.whl", hash = "sha256:0808014eb713677ec1292301ea4c81ad277b6cdf2fdd90fd540af98c0b101d20"},
{file = "cffi-1.15.0-cp38-cp38-manylinux_2_12_x86_64.manylinux2010_x86_64.whl", hash = "sha256:57e9ac9ccc3101fac9d6014fba037473e4358ef4e89f8e181f8951a2c0162024"},
{file = "cffi-1.15.0-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:8b6c2ea03845c9f501ed1313e78de148cd3f6cad741a75d43a29b43da27f2e1e"},
{file = "cffi-1.15.0-cp38-cp38-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:10dffb601ccfb65262a27233ac273d552ddc4d8ae1bf93b21c94b8511bffe728"},
{file = "cffi-1.15.0-cp38-cp38-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:786902fb9ba7433aae840e0ed609f45c7bcd4e225ebb9c753aa39725bb3e6ad6"},
{file = "cffi-1.15.0-cp38-cp38-win32.whl", hash = "sha256:da5db4e883f1ce37f55c667e5c0de439df76ac4cb55964655906306918e7363c"},
{file = "cffi-1.15.0-cp38-cp38-win_amd64.whl", hash = "sha256:181dee03b1170ff1969489acf1c26533710231c58f95534e3edac87fff06c443"},
{file = "cffi-1.15.0-cp39-cp39-macosx_10_9_x86_64.whl", hash = "sha256:45e8636704eacc432a206ac7345a5d3d2c62d95a507ec70d62f23cd91770482a"},
{file = "cffi-1.15.0-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:31fb708d9d7c3f49a60f04cf5b119aeefe5644daba1cd2a0fe389b674fd1de37"},
{file = "cffi-1.15.0-cp39-cp39-manylinux_2_12_i686.manylinux2010_i686.whl", hash = "sha256:6dc2737a3674b3e344847c8686cf29e500584ccad76204efea14f451d4cc669a"},
{file = "cffi-1.15.0-cp39-cp39-manylinux_2_12_x86_64.manylinux2010_x86_64.whl", hash = "sha256:74fdfdbfdc48d3f47148976f49fab3251e550a8720bebc99bf1483f5bfb5db3e"},
{file = "cffi-1.15.0-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:ffaa5c925128e29efbde7301d8ecaf35c8c60ffbcd6a1ffd3a552177c8e5e796"},
{file = "cffi-1.15.0-cp39-cp39-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:3f7d084648d77af029acb79a0ff49a0ad7e9d09057a9bf46596dac9514dc07df"},
{file = "cffi-1.15.0-cp39-cp39-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:ef1f279350da2c586a69d32fc8733092fd32cc8ac95139a00377841f59a3f8d8"},
{file = "cffi-1.15.0-cp39-cp39-win32.whl", hash = "sha256:2a23af14f408d53d5e6cd4e3d9a24ff9e05906ad574822a10563efcef137979a"},
{file = "cffi-1.15.0-cp39-cp39-win_amd64.whl", hash = "sha256:3773c4d81e6e818df2efbc7dd77325ca0dcb688116050fb2b3011218eda36139"},
{file = "cffi-1.15.0.tar.gz", hash = "sha256:920f0d66a896c2d99f0adbb391f990a84091179542c205fa53ce5787aff87954"},
]
click = [
{file = "click-8.1.2-py3-none-any.whl", hash = "sha256:24e1a4a9ec5bf6299411369b208c1df2188d9eb8d916302fe6bf03faed227f1e"},
{file = "click-8.1.2.tar.gz", hash = "sha256:479707fe14d9ec9a0757618b7a100a0ae4c4e236fac5b7f80ca68028141a1a72"},
@ -448,9 +526,6 @@ dill = [
{file = "dill-0.3.4-py2.py3-none-any.whl", hash = "sha256:7e40e4a70304fd9ceab3535d36e58791d9c4a776b38ec7f7ec9afc8d3dca4d4f"},
{file = "dill-0.3.4.zip", hash = "sha256:9f9734205146b2b353ab3fec9af0070237b6ddae78452af83d2fca84d739e675"},
]
ed25519 = [
{file = "ed25519-1.5.tar.gz", hash = "sha256:02053ee019ceef0df97294be2d4d5a8fc120fc86e81e08bec1245fc0f9403358"},
]
fastjsonschema = [
{file = "fastjsonschema-2.15.3-py3-none-any.whl", hash = "sha256:ddb0b1d8243e6e3abb822bd14e447a89f4ab7439342912d590444831fa00b6a0"},
{file = "fastjsonschema-2.15.3.tar.gz", hash = "sha256:0a572f0836962d844c1fc435e200b2e4f4677e4e6611a2e3bdd01ba697c275ec"},
@ -555,10 +630,26 @@ py = [
{file = "py-1.11.0-py2.py3-none-any.whl", hash = "sha256:607c53218732647dff4acdfcd50cb62615cedf612e72d1724fb1a0cc6405b378"},
{file = "py-1.11.0.tar.gz", hash = "sha256:51c75c4126074b472f746a24399ad32f6053d1b34b68d2fa41e558e6f4a98719"},
]
pycparser = [
{file = "pycparser-2.21-py2.py3-none-any.whl", hash = "sha256:8ee45429555515e1f6b185e78100aea234072576aa43ab53aefcae078162fca9"},
{file = "pycparser-2.21.tar.gz", hash = "sha256:e644fdec12f7872f86c58ff790da456218b10f863970249516d60a5eaca77206"},
]
pylint = [
{file = "pylint-2.13.5-py3-none-any.whl", hash = "sha256:c149694cfdeaee1aa2465e6eaab84c87a881a7d55e6e93e09466be7164764d1e"},
{file = "pylint-2.13.5.tar.gz", hash = "sha256:dab221658368c7a05242e673c275c488670144123f4bd262b2777249c1c0de9b"},
]
pynacl = [
{file = "PyNaCl-1.5.0-cp36-abi3-macosx_10_10_universal2.whl", hash = "sha256:401002a4aaa07c9414132aaed7f6836ff98f59277a234704ff66878c2ee4a0d1"},
{file = "PyNaCl-1.5.0-cp36-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.manylinux_2_24_aarch64.whl", hash = "sha256:52cb72a79269189d4e0dc537556f4740f7f0a9ec41c1322598799b0bdad4ef92"},
{file = "PyNaCl-1.5.0-cp36-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:a36d4a9dda1f19ce6e03c9a784a2921a4b726b02e1c736600ca9c22029474394"},
{file = "PyNaCl-1.5.0-cp36-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_24_x86_64.whl", hash = "sha256:0c84947a22519e013607c9be43706dd42513f9e6ae5d39d3613ca1e142fba44d"},
{file = "PyNaCl-1.5.0-cp36-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:06b8f6fa7f5de8d5d2f7573fe8c863c051225a27b61e6860fd047b1775807858"},
{file = "PyNaCl-1.5.0-cp36-abi3-musllinux_1_1_aarch64.whl", hash = "sha256:a422368fc821589c228f4c49438a368831cb5bbc0eab5ebe1d7fac9dded6567b"},
{file = "PyNaCl-1.5.0-cp36-abi3-musllinux_1_1_x86_64.whl", hash = "sha256:61f642bf2378713e2c2e1de73444a3778e5f0a38be6fee0fe532fe30060282ff"},
{file = "PyNaCl-1.5.0-cp36-abi3-win32.whl", hash = "sha256:e46dae94e34b085175f8abb3b0aaa7da40767865ac82c928eeb9e57e1ea8a543"},
{file = "PyNaCl-1.5.0-cp36-abi3-win_amd64.whl", hash = "sha256:20f42270d27e1b6a29f54032090b972d97f0a1b0948cc52392041ef7831fee93"},
{file = "PyNaCl-1.5.0.tar.gz", hash = "sha256:8ac7448f09ab85811607bdd21ec2464495ac8b7c66d146bf545b0f08fb9220ba"},
]
pyparsing = [
{file = "pyparsing-3.0.8-py3-none-any.whl", hash = "sha256:ef7b523f6356f763771559412c0d7134753f037822dad1b16945b7b846f7ad06"},
{file = "pyparsing-3.0.8.tar.gz", hash = "sha256:7bf433498c016c4314268d95df76c81b842a4cb2b276fa3312cfb1e1d85f6954"},

View File

@ -6,9 +6,9 @@ authors = ["Gergely Polonkai <gergely@polonkai.eu>"]
[tool.poetry.dependencies]
python = "^3.10"
ed25519 = "^1.5"
bip39 = "^0.0.2"
fastjsonschema = "^2.15.3"
PyNaCl = "^1.5.0"
[tool.poetry.dev-dependencies]
pytest = "^7.1.1"

View File

@ -1,7 +1,7 @@
"""Test configuration and global fixtures
"""
from ed25519 import SigningKey
from nacl.signing import SigningKey
import pytest
from _pytest.fixtures import SubRequest

View File

@ -3,7 +3,7 @@
from datetime import datetime, timedelta
from typing import Any, Dict
from ed25519 import SigningKey
from nacl.signing import SigningKey
import pytest
from pytest_mock.plugin import MockerFixture
@ -213,11 +213,11 @@ def test_sign_different_author(identity: Identity) -> None:
def test_sign_new_identity() -> None:
"""Test if signing a document with a different entity also sets the author"""
key_seed = (
other_key_seed = (
b'`_\x8dm\x18\xeem\xe3\\\xeb_\x1aw)\xcd\xb7\xd8\xd9\xdd\xad\x86\x9a#wQ"F\x95\xa1\x178r'
)
key = SigningKey(key_seed)
identity = Identity('name', sign_key=key)
identity = Identity('name', sign_key=SigningKey(other_key_seed))
assert str(identity) == '@name.bu5seaewd4p7cx7ot4ue3m6wpigfa5hmowxgeophe2so72roao5wq'
document = Es4Document.from_json(VALID_DOCUMENT)
document.author = identity

View File

@ -2,7 +2,7 @@
"""
import bip39
from ed25519 import SigningKey, create_keypair
from nacl.signing import SigningKey
import pytest
from pytest_mock.plugin import MockerFixture
@ -27,7 +27,8 @@ TEST_MNEMONIC = bip39.encode_bytes(TEST_SEED)
def test_init_bad_name(name: str) -> None:
"""Test if initialisation is not possible with an invalid name"""
sign, verify = create_keypair()
sign = SigningKey.generate()
verify = sign.verify_key
with pytest.raises(ValidationError) as ctx:
Identity(name, verify_key=verify, sign_key=sign)
@ -38,8 +39,8 @@ def test_init_bad_name(name: str) -> None:
def test_init_key_mismatch() -> None:
"""Test if initialisation fails if the signing and verifying keys dont match"""
sign1, _ = create_keypair()
_, verify2 = create_keypair()
sign1 = SigningKey.generate()
verify2 = SigningKey.generate().verify_key
with pytest.raises(ValidationError) as ctx:
Identity('name', verify_key=verify2, sign_key=sign1)
@ -89,12 +90,12 @@ def test_from_address() -> None:
"""Test loading an identity from an author address"""
skey = SigningKey(TEST_SEED)
vkey = skey.get_verifying_key()
vkey = skey.verify_key
identity = Identity.from_address('@test.bcz76z52y5dlpohtkmpuj3jsdcvfmebzpcgfmtmhu4u7hlexzreya')
assert identity.name == 'test'
assert identity.sign_key is None
assert identity.verify_key.to_bytes() == vkey.to_bytes()
assert identity.verify_key == vkey
def test_from_invalid_address() -> None:
@ -110,9 +111,8 @@ def test_generate(mocker: MockerFixture) -> None:
"""Test the generate property"""
skey = SigningKey(TEST_SEED)
vkey = skey.get_verifying_key()
mocker.patch('earthsnake.identity.create_keypair', return_value=(skey, vkey))
mocker.patch('earthsnake.identity.SigningKey.generate', return_value=skey)
identity = Identity.generate('test')
assert str(identity) == '@test.bcz76z52y5dlpohtkmpuj3jsdcvfmebzpcgfmtmhu4u7hlexzreya'
@ -145,7 +145,7 @@ def test_from_mnemonic() -> None:
assert identity.name == name
assert identity.sign_key
assert identity.sign_key.to_seed() == TEST_SEED
assert identity.sign_key.encode() == TEST_SEED
assert str(identity) == f'@{name}.bcz76z52y5dlpohtkmpuj3jsdcvfmebzpcgfmtmhu4u7hlexzreya'