Files
nutshell/tests/test_mint_api_cache.py
lollerfirst d98d166df1 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>
2024-12-15 00:39:53 +01:00

129 lines
4.5 KiB
Python

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
from tests.helpers import pay_if_regtest
BASE_URL = "http://localhost:3337"
invoice_32sat = "lnbc320n1pnsuamsdqqxqrrsssp5w3tlpw2zss396qh28l3a07u35zdx8nmknzryk89ackn23eywdu2spp5ckt298t835ejzh2xepyxlg57f54q27ffc2zjsjh3t5pmx4wghpcqne0vycw5dfalx5y45d2jtwqfwz437hduyccn9nxk2feay0ytxldjpf3fcjrcf5k2s56q3erj86ymlqdp703y89vt4lr4lun5z5duulcqwuwutn"
@pytest_asyncio.fixture(scope="function")
async def wallet(ledger: Ledger):
wallet1 = await Wallet.with_db(
url=BASE_URL,
db="test_data/wallet_mint_api",
name="wallet_mint_api",
)
await wallet1.load_mint()
yield wallet1
@pytest.mark.asyncio
@pytest.mark.skipif(
not settings.mint_redis_cache_enabled,
reason="settings.mint_redis_cache_enabled is False",
)
async def test_api_mint_cached_responses(wallet: Wallet):
# Testing mint
mint_quote = await wallet.request_mint(64)
await pay_if_regtest(mint_quote.request)
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, "signature": signature},
timeout=None,
)
response1 = httpx.post(
f"{BASE_URL}/v1/mint/bolt11",
json={"quote": quote_id, "outputs": outputs_payload, "signature": signature},
timeout=None,
)
assert response.status_code == 200, f"{response.status_code = }"
assert response1.status_code == 200, f"{response1.status_code = }"
assert response.text == response1.text
@pytest.mark.asyncio
@pytest.mark.skipif(
not settings.mint_redis_cache_enabled,
reason="settings.mint_redis_cache_enabled is False",
)
async def test_api_swap_cached_responses(wallet: Wallet):
mint_quote = await wallet.request_mint(64)
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, 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,
"signature": signature,
},
timeout=None,
)
response1 = httpx.post(
f"{BASE_URL}/v1/swap",
json={"inputs": inputs_payload, "outputs": outputs_payload},
timeout=None,
)
assert response.status_code == 200, f"{response.status_code = }"
assert response1.status_code == 200, f"{response1.status_code = }"
assert response.text == response1.text
@pytest.mark.asyncio
@pytest.mark.skipif(
not settings.mint_redis_cache_enabled,
reason="settings.mint_redis_cache_enabled is False",
)
async def test_api_melt_cached_responses(wallet: Wallet):
mint_quote = await wallet.request_mint(64)
melt_quote = await wallet.melt_quote(invoice_32sat)
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(
f"{BASE_URL}/v1/melt/bolt11",
json={
"quote": melt_quote.quote,
"inputs": inputs_payload,
"outputs": outputs_payload,
},
timeout=None,
)
response1 = httpx.post(
f"{BASE_URL}/v1/melt/bolt11",
json={
"quote": melt_quote.quote,
"inputs": inputs_payload,
"outputs": outputs_payload,
},
timeout=None,
)
assert response.status_code == 200, f"{response.status_code = }"
assert response1.status_code == 200, f"{response1.status_code = }"
assert response.text == response1.text