From 181c1efa23f4335af9910764e176aab5852123f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?dni=20=E2=9A=A1?= Date: Sat, 31 Dec 2022 14:34:02 +0100 Subject: [PATCH] add from npub to publickey (#22) --- nostr/key.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/nostr/key.py b/nostr/key.py index d5ac2f0..f12c435 100644 --- a/nostr/key.py +++ b/nostr/key.py @@ -21,6 +21,14 @@ class PublicKey: pk = secp256k1.PublicKey(b"\x02" + self.raw_bytes, True) return pk.schnorr_verify(bytes.fromhex(hash), bytes.fromhex(sig), None, True) + @classmethod + def from_npub(cls, npub: str): + """ Load a PublicKey from its bech32/npub form """ + hrp, data, spec = bech32.bech32_decode(npub) + raw_public_key = bech32.convertbits(data, 5, 8)[:-1] + return cls(bytes(raw_public_key)) + + class PrivateKey: def __init__(self, raw_secret: bytes=None) -> None: if not raw_secret is None: @@ -85,7 +93,7 @@ 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 @@ -94,4 +102,4 @@ ffi = FFI() @ffi.callback("int (unsigned char *, const unsigned char *, const unsigned char *, void *)") def copy_x(output, x32, y32, data): ffi.memmove(output, x32, 32) - return 1 \ No newline at end of file + return 1