Support NUT-XX (signatures on quotes) for mint and wallet side (#670)

* 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>
This commit is contained in:
lollerfirst
2024-12-15 00:39:53 +01:00
committed by GitHub
parent 399c201552
commit d98d166df1
30 changed files with 505 additions and 243 deletions

View File

@@ -2,6 +2,7 @@ import httpx
import pytest
import pytest_asyncio
from cashu.core.nuts import nut20
from cashu.core.settings import settings
from cashu.mint.ledger import Ledger
from cashu.wallet.wallet import Wallet
@@ -29,21 +30,23 @@ async def wallet(ledger: Ledger):
)
async def test_api_mint_cached_responses(wallet: Wallet):
# Testing mint
invoice = await wallet.request_mint(64)
await pay_if_regtest(invoice.request)
mint_quote = await wallet.request_mint(64)
await pay_if_regtest(mint_quote.request)
quote_id = invoice.quote
quote_id = mint_quote.quote
secrets, rs, derivation_paths = await wallet.generate_secrets_from_to(10010, 10011)
outputs, rs = wallet._construct_outputs([32, 32], secrets, rs)
assert mint_quote.privkey
signature = nut20.sign_mint_quote(quote_id, outputs, mint_quote.privkey)
outputs_payload = [o.dict() for o in outputs]
response = httpx.post(
f"{BASE_URL}/v1/mint/bolt11",
json={"quote": quote_id, "outputs": outputs_payload},
json={"quote": quote_id, "outputs": outputs_payload, "signature": signature},
timeout=None,
)
response1 = httpx.post(
f"{BASE_URL}/v1/mint/bolt11",
json={"quote": quote_id, "outputs": outputs_payload},
json={"quote": quote_id, "outputs": outputs_payload, "signature": signature},
timeout=None,
)
assert response.status_code == 200, f"{response.status_code = }"
@@ -57,17 +60,23 @@ async def test_api_mint_cached_responses(wallet: Wallet):
reason="settings.mint_redis_cache_enabled is False",
)
async def test_api_swap_cached_responses(wallet: Wallet):
quote = await wallet.request_mint(64)
await pay_if_regtest(quote.request)
mint_quote = await wallet.request_mint(64)
await pay_if_regtest(mint_quote.request)
minted = await wallet.mint(64, quote.quote)
minted = await wallet.mint(64, mint_quote.quote)
secrets, rs, derivation_paths = await wallet.generate_secrets_from_to(10010, 10011)
outputs, rs = wallet._construct_outputs([32, 32], secrets, rs)
assert mint_quote.privkey
signature = nut20.sign_mint_quote(mint_quote.quote, outputs, mint_quote.privkey)
inputs_payload = [i.dict() for i in minted]
outputs_payload = [o.dict() for o in outputs]
response = httpx.post(
f"{BASE_URL}/v1/swap",
json={"inputs": inputs_payload, "outputs": outputs_payload},
json={
"inputs": inputs_payload,
"outputs": outputs_payload,
"signature": signature,
},
timeout=None,
)
response1 = httpx.post(
@@ -86,15 +95,14 @@ async def test_api_swap_cached_responses(wallet: Wallet):
reason="settings.mint_redis_cache_enabled is False",
)
async def test_api_melt_cached_responses(wallet: Wallet):
quote = await wallet.request_mint(64)
mint_quote = await wallet.request_mint(64)
melt_quote = await wallet.melt_quote(invoice_32sat)
await pay_if_regtest(quote.request)
minted = await wallet.mint(64, quote.quote)
await pay_if_regtest(mint_quote.request)
minted = await wallet.mint(64, mint_quote.quote)
secrets, rs, derivation_paths = await wallet.generate_secrets_from_to(10010, 10010)
outputs, rs = wallet._construct_outputs([32], secrets, rs)
inputs_payload = [i.dict() for i in minted]
outputs_payload = [o.dict() for o in outputs]
response = httpx.post(