Skip to content

Commit

Permalink
nutxx -> nut20
Browse files Browse the repository at this point in the history
  • Loading branch information
callebtc committed Dec 4, 2024
1 parent 0d1e69a commit 10ab918
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 19 deletions.
9 changes: 4 additions & 5 deletions cashu/core/nuts/nutxx.py → cashu/core/nuts/nut20.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@

def construct_message(quote_id: str, outputs: List[BlindedMessage]) -> bytes:
serialized_outputs = b"".join([o.B_.encode("utf-8") for o in outputs])
msgbytes = sha256(
quote_id.encode("utf-8")
+ serialized_outputs
).digest()
msgbytes = sha256(quote_id.encode("utf-8") + serialized_outputs).digest()
return msgbytes


def sign_mint_quote(
quote_id: str,
outputs: List[BlindedMessage],
Expand All @@ -23,6 +21,7 @@ def sign_mint_quote(
sig = privkey.schnorr_sign(msgbytes, None, raw=True)
return sig.hex()


def verify_mint_quote(
quote_id: str,
outputs: List[BlindedMessage],
Expand All @@ -32,4 +31,4 @@ def verify_mint_quote(
pubkey = PublicKey(bytes.fromhex(public_key), raw=True)
msgbytes = construct_message(quote_id, outputs)
sig = bytes.fromhex(signature)
return pubkey.schnorr_verify(msgbytes, sig, None, raw=True)
return pubkey.schnorr_verify(msgbytes, sig, None, raw=True)
9 changes: 6 additions & 3 deletions cashu/mint/verification.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
TransactionError,
TransactionUnitError,
)
from ..core.nuts import nutxx
from ..core.nuts import nut20
from ..core.settings import settings
from ..lightning.base import LightningBackend
from ..mint.crud import LedgerCrud
Expand Down Expand Up @@ -281,11 +281,14 @@ def _verify_and_get_unit_method(
return unit, method

def _verify_mint_quote_witness(
self, quote: MintQuote, outputs: List[BlindedMessage], witness: Optional[str],
self,
quote: MintQuote,
outputs: List[BlindedMessage],
witness: Optional[str],
) -> bool:
"""Verify signature on quote id and outputs"""
if not quote.pubkey:
return True
if not witness:
return False
return nutxx.verify_mint_quote(quote.quote, outputs, quote.pubkey, witness)
return nut20.verify_mint_quote(quote.quote, outputs, quote.pubkey, witness)
6 changes: 4 additions & 2 deletions cashu/wallet/api/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ async def swap(
if incoming_wallet.url == outgoing_wallet.url:
raise Exception("mints for swap have to be different")

# get keypair to lock the quote with (NUTXX)
# get keypair to lock the quote with (NUT-20)
keypair = await incoming_wallet.get_quote_ephemeral_keypair()
# request invoice from incoming mint
mint_quote = await incoming_wallet.request_mint(amount, keypair=keypair)
Expand All @@ -209,7 +209,9 @@ async def swap(
)

# mint token in incoming mint
await incoming_wallet.mint(amount, quote_id=mint_quote.quote, quote_privkey=mint_quote.privkey)
await incoming_wallet.mint(
amount, quote_id=mint_quote.quote, quote_privkey=mint_quote.privkey
)
await incoming_wallet.load_proofs(reload=True)
mint_balances = await incoming_wallet.balance_per_minturl()
return SwapResponse(
Expand Down
6 changes: 3 additions & 3 deletions cashu/wallet/mint_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def supports_websocket_mint_quote(self, method: Method, unit: Unit) -> bool:
def supports_mint_quote_signature(self) -> bool:
if not self.nuts:
return False
nutxx = self.nuts.get(MINT_QUOTE_SIGNATURE_NUT, None)
if nutxx:
return nutxx["supported"]
nut20 = self.nuts.get(MINT_QUOTE_SIGNATURE_NUT, None)
if nut20:
return nut20["supported"]
return False
16 changes: 10 additions & 6 deletions cashu/wallet/wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
PostCheckStateResponse,
PostMeltQuoteResponse,
)
from ..core.nuts import nutxx
from ..core.nuts import nut20
from ..core.p2pk import Secret
from ..core.settings import settings
from ..core.split import amount_split
Expand Down Expand Up @@ -387,7 +387,11 @@ async def _check_used_secrets(self, secrets):
logger.trace("Secret check complete.")

async def request_mint_with_callback(
self, amount: int, callback: Callable, memo: Optional[str] = None, keypair: Optional[Tuple[str, str]] = None
self,
amount: int,
callback: Callable,
memo: Optional[str] = None,
keypair: Optional[Tuple[str, str]] = None,
) -> Tuple[MintQuote, SubscriptionManager]:
"""Request a quote invoice for minting tokens.
Expand Down Expand Up @@ -417,7 +421,8 @@ async def request_mint_with_callback(

return quote, subscriptions

async def request_mint(self,
async def request_mint(
self,
amount: int,
memo: Optional[str] = None,
keypair: Optional[Tuple[str, str]] = None,
Expand All @@ -444,8 +449,7 @@ async def request_mint(self,

# TODO: generate secret with BIP39 (seed and specific derivation + counter)
async def get_quote_ephemeral_keypair(self) -> Optional[Tuple[str, str]]:
"""Creates a keypair for a quote IF the mint supports NUT-19
"""
"""Creates a keypair for a quote IF the mint supports NUT-19"""
if not self.mint_info:
await self.load_mint_info()
if self.mint_info.supports_mint_quote_signature():
Expand Down Expand Up @@ -549,7 +553,7 @@ async def mint(

witness: Optional[str] = None
if quote_privkey:
witness = nutxx.sign_mint_quote(quote_id, outputs, quote_privkey)
witness = nut20.sign_mint_quote(quote_id, outputs, quote_privkey)

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

0 comments on commit 10ab918

Please sign in to comment.