Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
callebtc committed Dec 13, 2024
1 parent 3c8609f commit b0a5287
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 11 deletions.
2 changes: 1 addition & 1 deletion cashu/core/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def __init__(self):
super().__init__(self.detail, code=2001)


class QuoteInvalidWitnessError(CashuError):
class QuoteSignatureInvalidError(CashuError):
detail = "Signature for mint request invalid"
code = 20008

Expand Down
4 changes: 2 additions & 2 deletions cashu/mint/ledger.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
KeysetNotFoundError,
LightningError,
NotAllowedError,
QuoteInvalidWitnessError,
QuoteNotPaidError,
QuoteSignatureInvalidError,
TransactionError,
)
from ..core.helpers import sum_proofs
Expand Down Expand Up @@ -562,7 +562,7 @@ async def mint(
if quote.expiry and quote.expiry > int(time.time()):
raise TransactionError("quote expired")
if not self._verify_mint_quote_witness(quote, outputs, signature):
raise QuoteInvalidWitnessError()
raise QuoteSignatureInvalidError()

promises = await self._generate_promises(outputs)
except Exception as e:
Expand Down
9 changes: 8 additions & 1 deletion tests/test_mint_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
PostRestoreRequest,
PostRestoreResponse,
)
from cashu.core.nuts import nut20
from cashu.core.nuts.nuts import MINT_NUT
from cashu.core.settings import settings
from cashu.mint.ledger import Ledger
Expand Down Expand Up @@ -244,10 +245,16 @@ async def test_mint(ledger: Ledger, wallet: Wallet):
await pay_if_regtest(mint_quote.request)
secrets, rs, derivation_paths = await wallet.generate_secrets_from_to(10000, 10001)
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)
outputs_payload = [o.dict() for o in outputs]
response = httpx.post(
f"{BASE_URL}/v1/mint/bolt11",
json={"quote": mint_quote.quote, "outputs": outputs_payload},
json={
"quote": mint_quote.quote,
"outputs": outputs_payload,
"signature": signature,
},
timeout=None,
)
assert response.status_code == 200, f"{response.url} {response.status_code}"
Expand Down
25 changes: 18 additions & 7 deletions tests/test_mint_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from cashu.core.base import MeltQuoteState
from cashu.core.helpers import sum_proofs
from cashu.core.models import PostMeltQuoteRequest, PostMintQuoteRequest
from cashu.core.nuts import nut20
from cashu.core.settings import settings
from cashu.mint.ledger import Ledger
from cashu.wallet.wallet import Wallet
Expand Down Expand Up @@ -119,9 +120,9 @@ async def test_melt_external(wallet1: Wallet, ledger: Ledger):
@pytest.mark.asyncio
@pytest.mark.skipif(is_regtest, reason="only works with FakeWallet")
async def test_mint_internal(wallet1: Wallet, ledger: Ledger):
mint_quote = await wallet1.request_mint(128)
await ledger.get_mint_quote(mint_quote.quote)
mint_quote = await ledger.get_mint_quote(mint_quote.quote)
wallet_mint_quote = await wallet1.request_mint(128)
await ledger.get_mint_quote(wallet_mint_quote.quote)
mint_quote = await ledger.get_mint_quote(wallet_mint_quote.quote)

assert mint_quote.paid, "mint quote should be paid"

Expand All @@ -136,7 +137,11 @@ async def test_mint_internal(wallet1: Wallet, ledger: Ledger):
len(output_amounts)
)
outputs, rs = wallet1._construct_outputs(output_amounts, secrets, rs)
await ledger.mint(outputs=outputs, quote_id=mint_quote.quote)
assert wallet_mint_quote.privkey
signature = nut20.sign_mint_quote(
mint_quote.quote, outputs, wallet_mint_quote.privkey
)
await ledger.mint(outputs=outputs, quote_id=mint_quote.quote, signature=signature)

await assert_err(
ledger.mint(outputs=outputs, quote_id=mint_quote.quote),
Expand Down Expand Up @@ -311,14 +316,18 @@ async def test_mint_with_same_outputs_twice(wallet1: Wallet, ledger: Ledger):
len(output_amounts)
)
outputs, rs = wallet1._construct_outputs(output_amounts, secrets, rs)
await ledger.mint(outputs=outputs, quote_id=mint_quote.quote)
assert mint_quote.privkey
signature = nut20.sign_mint_quote(mint_quote.quote, outputs, mint_quote.privkey)
await ledger.mint(outputs=outputs, quote_id=mint_quote.quote, signature=signature)

# now try to mint with the same outputs again
mint_quote_2 = await wallet1.request_mint(128)
await pay_if_regtest(mint_quote_2.request)

assert mint_quote_2.privkey
signature = nut20.sign_mint_quote(mint_quote_2.quote, outputs, mint_quote_2.privkey)
await assert_err(
ledger.mint(outputs=outputs, quote_id=mint_quote_2.quote),
ledger.mint(outputs=outputs, quote_id=mint_quote_2.quote, signature=signature),
"outputs have already been signed before.",
)

Expand All @@ -338,7 +347,9 @@ async def test_melt_with_same_outputs_twice(wallet1: Wallet, ledger: Ledger):
# we use the outputs once for minting
mint_quote_2 = await wallet1.request_mint(128)
await pay_if_regtest(mint_quote_2.request)
await ledger.mint(outputs=outputs, quote_id=mint_quote_2.quote)
assert mint_quote_2.privkey
signature = nut20.sign_mint_quote(mint_quote_2.quote, outputs, mint_quote_2.privkey)
await ledger.mint(outputs=outputs, quote_id=mint_quote_2.quote, signature=signature)

# use the same outputs for melting
mint_quote = await ledger.mint_quote(PostMintQuoteRequest(unit="sat", amount=128))
Expand Down

0 comments on commit b0a5287

Please sign in to comment.