From 941eb475e38a7e67bd42eb1ef3ff885791207cee Mon Sep 17 00:00:00 2001 From: callebtc <93376500+callebtc@users.noreply.github.com> Date: Thu, 14 Mar 2024 13:01:03 +0100 Subject: [PATCH] tests: mint restore api test --- cashu/mint/ledger.py | 6 ++--- cashu/mint/router.py | 4 ++-- tests/test_mint_api.py | 38 ++++++++++++++++++++++++++++--- tests/test_mint_api_deprecated.py | 32 ++++++++++++++++++++++++++ 4 files changed, 72 insertions(+), 8 deletions(-) diff --git a/cashu/mint/ledger.py b/cashu/mint/ledger.py index 0035f24a..b62072dd 100644 --- a/cashu/mint/ledger.py +++ b/cashu/mint/ledger.py @@ -799,7 +799,7 @@ async def split( async def restore( self, outputs: List[BlindedMessage] ) -> Tuple[List[BlindedMessage], List[BlindedSignature]]: - promises: List[BlindedSignature] = [] + signatures: List[BlindedSignature] = [] return_outputs: List[BlindedMessage] = [] async with self.db.connect() as conn: for output in outputs: @@ -814,10 +814,10 @@ async def restore( if not promise.id and len(self.keysets) == 1: promise.id = self.keyset.id # END backwards compatibility - promises.append(promise) + signatures.append(promise) return_outputs.append(output) logger.trace(f"promise found: {promise}") - return return_outputs, promises + return return_outputs, signatures # ------- BLIND SIGNATURES ------- diff --git a/cashu/mint/router.py b/cashu/mint/router.py index 4c96d162..58af4203 100644 --- a/cashu/mint/router.py +++ b/cashu/mint/router.py @@ -354,5 +354,5 @@ async def check_state( ) async def restore(payload: PostMintRequest) -> PostRestoreResponse: assert payload.outputs, Exception("no outputs provided.") - outputs, promises = await ledger.restore(payload.outputs) - return PostRestoreResponse(outputs=outputs, signatures=promises) + outputs, signatures = await ledger.restore(payload.outputs) + return PostRestoreResponse(outputs=outputs, signatures=signatures) diff --git a/tests/test_mint_api.py b/tests/test_mint_api.py index 5a571416..6dd65b91 100644 --- a/tests/test_mint_api.py +++ b/tests/test_mint_api.py @@ -6,6 +6,8 @@ from cashu.core.base import ( PostCheckStateRequest, PostCheckStateResponse, + PostMintRequest, + PostRestoreResponse, SpentState, ) from cashu.core.settings import settings @@ -183,11 +185,11 @@ async def test_mint_quote(ledger: Ledger): assert result["request"] invoice = bolt11.decode(result["request"]) assert invoice.amount_msat == 100 * 1000 - + expiry = None if invoice.expiry is not None: expiry = invoice.date + invoice.expiry - + assert result["expiry"] == expiry # get mint quote again from api @@ -251,7 +253,7 @@ async def test_melt_quote_internal(ledger: Ledger, wallet: Wallet): # TODO: internal invoice, fee should be 0 assert result["fee_reserve"] == 0 invoice_obj = bolt11.decode(request) - + expiry = None if invoice_obj.expiry is not None: expiry = invoice_obj.date + invoice_obj.expiry @@ -399,3 +401,33 @@ async def test_api_check_state(ledger: Ledger): assert response assert len(response.states) == 2 assert response.states[0].state == SpentState.unspent + + +@pytest.mark.asyncio +@pytest.mark.skipif( + settings.debug_mint_only_deprecated, + reason="settings.debug_mint_only_deprecated is set", +) +async def test_api_restore(ledger: Ledger, wallet: Wallet): + invoice = await wallet.request_mint(64) + pay_if_regtest(invoice.bolt11) + await wallet.mint(64, id=invoice.id) + assert wallet.balance == 64 + secrets, rs, derivation_paths = await wallet.generate_secrets_from_to(0, 0) + outputs, rs = wallet._construct_outputs([64], secrets, rs) + + payload = PostMintRequest(outputs=outputs, quote="placeholder") + response = httpx.post( + f"{BASE_URL}/v1/restore", + json=payload.dict(), + ) + data = response.json() + assert "signatures" in data + assert "outputs" in data + assert response.status_code == 200, f"{response.url} {response.status_code}" + response = PostRestoreResponse.parse_obj(response.json()) + assert response + assert response + assert len(response.signatures) == 1 + assert len(response.outputs) == 1 + assert response.outputs == outputs diff --git a/tests/test_mint_api_deprecated.py b/tests/test_mint_api_deprecated.py index fcdcf8cd..86a2ffd6 100644 --- a/tests/test_mint_api_deprecated.py +++ b/tests/test_mint_api_deprecated.py @@ -5,6 +5,8 @@ from cashu.core.base import ( CheckSpendableRequest_deprecated, CheckSpendableResponse_deprecated, + PostMintRequest, + PostRestoreResponse, Proof, ) from cashu.core.settings import settings @@ -325,3 +327,33 @@ async def test_api_check_state(ledger: Ledger): assert len(states.spendable) == 2 assert states.pending assert len(states.pending) == 2 + + +@pytest.mark.asyncio +@pytest.mark.skipif( + settings.debug_mint_only_deprecated, + reason="settings.debug_mint_only_deprecated is set", +) +async def test_api_restore(ledger: Ledger, wallet: Wallet): + invoice = await wallet.request_mint(64) + pay_if_regtest(invoice.bolt11) + await wallet.mint(64, id=invoice.id) + assert wallet.balance == 64 + secrets, rs, derivation_paths = await wallet.generate_secrets_from_to(0, 0) + outputs, rs = wallet._construct_outputs([64], secrets, rs) + + payload = PostMintRequest(outputs=outputs, quote="placeholder") + response = httpx.post( + f"{BASE_URL}/restore", + json=payload.dict(), + ) + data = response.json() + assert "promises" in data + assert "outputs" in data + assert response.status_code == 200, f"{response.url} {response.status_code}" + response = PostRestoreResponse.parse_obj(response.json()) + assert response + assert response.promises + assert len(response.promises) == 1 + assert len(response.outputs) == 1 + assert response.outputs == outputs