Skip to content

Commit

Permalink
move nut19 to nuts directory
Browse files Browse the repository at this point in the history
  • Loading branch information
lollerfirst committed Nov 15, 2024
1 parent 432b39e commit 219d878
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 10 deletions.
Empty file added cashu/core/nuts/__init__.py
Empty file.
10 changes: 6 additions & 4 deletions cashu/core/crypto/nut19.py → cashu/core/nuts/nut19.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
from typing import List

from ..base import BlindedMessage
from .secp import PrivateKey, PublicKey
from ..crypto.secp import PrivateKey, PublicKey


def construct_message(quote_id: str, outputs: List[BlindedMessage]) -> bytes:
serialized_outputs = bytes.fromhex("".join([o.B_ for o in outputs]))
serialized_outputs = b"".join([o.B_.encode("utf-8") for o in outputs])
msgbytes = sha256(
quote_id.encode("utf-8")
+ serialized_outputs
Expand All @@ -16,18 +16,20 @@ def construct_message(quote_id: str, outputs: List[BlindedMessage]) -> bytes:
def sign_mint_quote(
quote_id: str,
outputs: List[BlindedMessage],
privkey: PrivateKey,
private_key: str,
) -> str:
privkey = PrivateKey(bytes.fromhex(private_key), raw=True)
msgbytes = construct_message(quote_id, outputs)
sig = privkey.schnorr_sign(msgbytes)
return sig.hex()

def verify_mint_quote(
quote_id: str,
outputs: List[BlindedMessage],
pubkey: PublicKey,
public_key: str,
signature: str,
) -> bool:
pubkey = PublicKey(bytes.fromhex(public_key), raw=True)
msgbytes = construct_message(quote_id, outputs)
sig = bytes.fromhex(signature)
return pubkey.schnorr_verify(msgbytes, sig)
6 changes: 3 additions & 3 deletions cashu/mint/verification.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
Proof,
Unit,
)
from ..core.crypto import b_dhke, nut19
from ..core.crypto import b_dhke
from ..core.nuts import nut19
from ..core.crypto.secp import PublicKey
from ..core.db import Connection, Database
from ..core.errors import (
Expand Down Expand Up @@ -287,5 +288,4 @@ def _verify_mint_quote_witness(
return True
if not witness:
return False
pubkey = PublicKey(bytes.fromhex(quote.key), raw=True)
return nut19.verify_mint_quote(quote.quote, outputs, pubkey, witness)
return nut19.verify_mint_quote(quote.quote, outputs, quote.key, witness)
6 changes: 3 additions & 3 deletions cashu/wallet/wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
Unit,
WalletKeyset,
)
from ..core.crypto import b_dhke, nut19
from ..core.crypto import b_dhke
from ..core.nuts import nut19
from ..core.crypto.keys import derive_keyset_id
from ..core.crypto.secp import PrivateKey, PublicKey
from ..core.db import Database
Expand Down Expand Up @@ -546,8 +547,7 @@ async def mint(

witness: Optional[str] = None
if quote_key:
privkey = PrivateKey(bytes.fromhex(quote_key), raw=True)
witness = nut19.sign_mint_quote(quote_id, outputs, privkey)
witness = nut19.sign_mint_quote(quote_id, outputs, quote_key)

# will raise exception if mint is unsuccessful
promises = await super().mint(outputs, quote_id, witness)
Expand Down

0 comments on commit 219d878

Please sign in to comment.