mirror of
https://github.com/aljazceru/python-nostr.git
synced 2025-12-18 06:44:26 +01:00
Adds initial test framework; adds PrivateKey.from_nsec() (#13)
* Adds initial test framework; adds PrivateKey.from_nsec() * Update setup.py
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,2 +1,3 @@
|
||||
venv/
|
||||
nostr/__pycache__/
|
||||
__pycache__/
|
||||
nostr.egg-info/
|
||||
@@ -114,10 +114,13 @@ pip install -r requirements.txt
|
||||
|
||||
Note: I wrote this with Python 3.9.5.
|
||||
|
||||
## Test Suite
|
||||
See the [Test Suite README](test/README.md)
|
||||
|
||||
## Disclaimer
|
||||
- This library is in very early development and still a WIP.
|
||||
- It might have some bugs.
|
||||
- I need to add tests.
|
||||
- I need to add more tests.
|
||||
- I will try to publish this as a [PyPI](https://pypi.org/) package at some point.
|
||||
|
||||
Please feel free to add issues, add PRs, or provide any feedback!
|
||||
|
||||
11
nostr/key.py
11
nostr/key.py
@@ -31,6 +31,13 @@ class PrivateKey:
|
||||
sk = secp256k1.PrivateKey(self.raw_secret)
|
||||
self.public_key = PublicKey(sk.pubkey.serialize()[1:])
|
||||
|
||||
@classmethod
|
||||
def from_nsec(cls, nsec: str):
|
||||
""" Load a PrivateKey from its bech32/nsec form """
|
||||
hrp, data, spec = bech32.bech32_decode(nsec)
|
||||
raw_secret = bech32.convertbits(data, 5, 8)[:-1]
|
||||
return cls(bytes(raw_secret))
|
||||
|
||||
def bech32(self) -> str:
|
||||
converted_bits = bech32.convertbits(self.raw_secret, 8, 5)
|
||||
return bech32.bech32_encode("nsec", converted_bits, bech32.Encoding.BECH32)
|
||||
@@ -78,6 +85,10 @@ class PrivateKey:
|
||||
sk = secp256k1.PrivateKey(self.raw_secret)
|
||||
sig = sk.schnorr_sign(hash, None, raw=True)
|
||||
return sig.hex()
|
||||
|
||||
def __eq__(self, other):
|
||||
return self.raw_secret == other.raw_secret
|
||||
|
||||
|
||||
ffi = FFI()
|
||||
@ffi.callback("int (unsigned char *, const unsigned char *, const unsigned char *, void *)")
|
||||
|
||||
21
setup.py
Normal file
21
setup.py
Normal file
@@ -0,0 +1,21 @@
|
||||
from setuptools import setup, find_packages
|
||||
|
||||
|
||||
with open("README.md", "r") as f:
|
||||
long_description = f.read()
|
||||
|
||||
setup(
|
||||
name='nostr',
|
||||
version="0.0.1",
|
||||
packages=find_packages(include=['nostr']),
|
||||
python_requires='>3.6.0',
|
||||
url='https://github.com/jeffthibault/python-nostr',
|
||||
description="A Python library for making Nostr clients.",
|
||||
long_description=long_description,
|
||||
long_description_content_type="text/markdown",
|
||||
classifiers=[
|
||||
'Operating System :: POSIX :: Linux',
|
||||
'Operating System :: Microsoft :: Windows',
|
||||
'Operating System :: MacOS :: MacOS X',
|
||||
],
|
||||
)
|
||||
31
test/README.md
Normal file
31
test/README.md
Normal file
@@ -0,0 +1,31 @@
|
||||
# Testing python-nostr
|
||||
|
||||
## Set up the test environment
|
||||
|
||||
Install the test-runner dependencies:
|
||||
```
|
||||
pip3 install -r test/requirements.txt
|
||||
```
|
||||
|
||||
Then make the `nostr` python module visible/importable to the tests by installing the local dev dir as an editable module:
|
||||
```
|
||||
# from the repo root
|
||||
pip3 install -e .
|
||||
```
|
||||
|
||||
## Running the test suite
|
||||
Run the whole test suite:
|
||||
```
|
||||
# from the repo root
|
||||
pytest
|
||||
```
|
||||
|
||||
Run a specific test file:
|
||||
```
|
||||
pytest test/test_this_file.py
|
||||
```
|
||||
|
||||
Run a specific test:
|
||||
```
|
||||
pytest test/test_this_file.py::test_this_specific_test
|
||||
```
|
||||
1
test/requirements.txt
Normal file
1
test/requirements.txt
Normal file
@@ -0,0 +1 @@
|
||||
pytest>=7.2.0
|
||||
23
test/test_key.py
Normal file
23
test/test_key.py
Normal file
@@ -0,0 +1,23 @@
|
||||
from nostr.key import PrivateKey
|
||||
|
||||
|
||||
def test_eq_true():
|
||||
""" __eq__ should return True when PrivateKeys are equal """
|
||||
pk1 = PrivateKey()
|
||||
pk2 = PrivateKey(pk1.raw_secret)
|
||||
assert pk1 == pk2
|
||||
|
||||
|
||||
def test_eq_false():
|
||||
""" __eq__ should return False when PrivateKeys are not equal """
|
||||
pk1 = PrivateKey()
|
||||
pk2 = PrivateKey()
|
||||
assert pk1.raw_secret != pk2.raw_secret
|
||||
assert pk1 != pk2
|
||||
|
||||
|
||||
def test_from_nsec():
|
||||
""" PrivateKey.from_nsec should yield the source's raw_secret """
|
||||
pk1 = PrivateKey()
|
||||
pk2 = PrivateKey.from_nsec(pk1.bech32())
|
||||
assert pk1.raw_secret == pk2.raw_secret
|
||||
Reference in New Issue
Block a user