mirror of
https://github.com/aljazceru/python-nostr.git
synced 2025-12-19 07:14:23 +01:00
add from npub to publickey (#22)
This commit is contained in:
12
nostr/key.py
12
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
|
||||
return 1
|
||||
|
||||
Reference in New Issue
Block a user