mirror of
https://github.com/aljazceru/nutshell.git
synced 2026-01-06 10:24:21 +01:00
* nut-19 sign mint quote * ephemeral key for quote * `mint` adjustments + crypto/nut19.py * wip: mint side working * fix import * post-merge fixups * more fixes * make format * move nut19 to nuts directory * `key` -> `privkey` and `pubkey` * make format * mint_info method for nut-19 support * fix tests imports * fix signature missing positional argument + fix db migration format not correctly escaped + pass in NUT-19 keypair to `request_mint` `request_mint_with_callback` * make format * fix `get_invoice_status` * rename to xx * nutxx -> nut20 * mypy * remove `mint_quote_signature_required` as per spec * wip edits * clean up * fix tests * fix deprecated api tests * fix redis tests * fix cache tests * fix regtest mint external * fix mint regtest * add test without signature * test pubkeys in quotes * wip * add compat --------- Co-authored-by: callebtc <93376500+callebtc@users.noreply.github.com>
53 lines
1.6 KiB
Python
53 lines
1.6 KiB
Python
import hashlib
|
|
from enum import Enum
|
|
from typing import Union
|
|
|
|
from .crypto.secp import PrivateKey, PublicKey
|
|
from .secret import Secret, SecretKind
|
|
|
|
|
|
class SigFlags(Enum):
|
|
# require signatures only on the inputs (default signature flag)
|
|
SIG_INPUTS = "SIG_INPUTS"
|
|
# require signatures on inputs and outputs
|
|
SIG_ALL = "SIG_ALL"
|
|
|
|
|
|
class P2PKSecret(Secret):
|
|
@classmethod
|
|
def from_secret(cls, secret: Secret):
|
|
assert SecretKind(secret.kind) == SecretKind.P2PK, "Secret is not a P2PK secret"
|
|
# NOTE: exclude tags in .dict() because it doesn't deserialize it properly
|
|
# need to add it back in manually with tags=secret.tags
|
|
return cls(**secret.dict(exclude={"tags"}), tags=secret.tags)
|
|
|
|
@property
|
|
def locktime(self) -> Union[None, int]:
|
|
locktime = self.tags.get_tag("locktime")
|
|
return int(locktime) if locktime else None
|
|
|
|
@property
|
|
def sigflag(self) -> Union[None, SigFlags]:
|
|
sigflag = self.tags.get_tag("sigflag")
|
|
return SigFlags(sigflag) if sigflag else None
|
|
|
|
@property
|
|
def n_sigs(self) -> Union[None, int]:
|
|
n_sigs = self.tags.get_tag("n_sigs")
|
|
return int(n_sigs) if n_sigs else None
|
|
|
|
|
|
def schnorr_sign(message: bytes, private_key: PrivateKey) -> bytes:
|
|
signature = private_key.schnorr_sign(
|
|
hashlib.sha256(message).digest(), None, raw=True
|
|
)
|
|
return signature
|
|
|
|
|
|
def verify_schnorr_signature(
|
|
message: bytes, pubkey: PublicKey, signature: bytes
|
|
) -> bool:
|
|
return pubkey.schnorr_verify(
|
|
hashlib.sha256(message).digest(), signature, None, raw=True
|
|
)
|