From ea96fab9e380dddbbe63c10dc6ae878255d39b09 Mon Sep 17 00:00:00 2001 From: lollerfirst <43107113+lollerfirst@users.noreply.github.com> Date: Wed, 22 Jan 2025 00:36:22 +0100 Subject: [PATCH] [FIX] Specs conformant error codes (#693) * error codes must follow spec * make format --- cashu/core/errors.py | 18 ++++++++++++++++++ cashu/mint/ledger.py | 3 ++- cashu/mint/verification.py | 6 ++++-- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/cashu/core/errors.py b/cashu/core/errors.py index 13095c6..e0f3f94 100644 --- a/cashu/core/errors.py +++ b/cashu/core/errors.py @@ -18,6 +18,19 @@ class NotAllowedError(CashuError): def __init__(self, detail: Optional[str] = None, code: Optional[int] = None): super().__init__(detail or self.detail, code=code or self.code) +class OutputsAlreadySignedError(CashuError): + detail = "outputs have already been signed before." + code = 10002 + + def __init__(self, detail: Optional[str] = None, code: Optional[int] = None): + super().__init__(detail or self.detail, code=code or self.code) + +class InvalidProofsError(CashuError): + detail = "proofs could not be verified" + code = 10003 + + def __init__(self, detail: Optional[str] = None, code: Optional[int] = None): + super().__init__(detail or self.detail, code=code or self.code) class TransactionError(CashuError): detail = "transaction error" @@ -63,6 +76,11 @@ class TransactionUnitError(TransactionError): def __init__(self, detail): super().__init__(detail, code=self.code) +class TransactionAmountExceedsLimitError(TransactionError): + code = 11006 + + def __init__(self, detail): + super().__init__(detail, code=self.code) class KeysetError(CashuError): detail = "keyset error" diff --git a/cashu/mint/ledger.py b/cashu/mint/ledger.py index a070f8d..706a614 100644 --- a/cashu/mint/ledger.py +++ b/cashu/mint/ledger.py @@ -39,6 +39,7 @@ from ..core.errors import ( NotAllowedError, QuoteNotPaidError, QuoteSignatureInvalidError, + TransactionAmountExceedsLimitError, TransactionError, ) from ..core.helpers import sum_proofs @@ -403,7 +404,7 @@ class Ledger(LedgerVerification, LedgerSpendingConditions, LedgerTasks, LedgerFe if not quote_request.amount > 0: raise TransactionError("amount must be positive") if settings.mint_max_peg_in and quote_request.amount > settings.mint_max_peg_in: - raise NotAllowedError( + raise TransactionAmountExceedsLimitError( f"Maximum mint amount is {settings.mint_max_peg_in} sat." ) if settings.mint_peg_out_only: diff --git a/cashu/mint/verification.py b/cashu/mint/verification.py index 377779e..d726476 100644 --- a/cashu/mint/verification.py +++ b/cashu/mint/verification.py @@ -15,8 +15,10 @@ from ..core.crypto import b_dhke from ..core.crypto.secp import PublicKey from ..core.db import Connection, Database from ..core.errors import ( + InvalidProofsError, NoSecretInProofsError, NotAllowedError, + OutputsAlreadySignedError, SecretTooLongError, TransactionError, TransactionUnitError, @@ -79,7 +81,7 @@ class LedgerVerification( raise TransactionError("duplicate proofs.") # Verify ecash signatures if not all([self._verify_proof_bdhke(p) for p in proofs]): - raise TransactionError("could not verify proofs.") + raise InvalidProofsError() # Verify input spending conditions if not all([self._verify_input_spending_conditions(p) for p in proofs]): raise TransactionError("validation of input spending conditions failed.") @@ -141,7 +143,7 @@ class LedgerVerification( # verify that outputs have not been signed previously signed_before = await self._check_outputs_issued_before(outputs, conn) if any(signed_before): - raise TransactionError("outputs have already been signed before.") + raise OutputsAlreadySignedError() logger.trace(f"Verified {len(outputs)} outputs.") async def _check_outputs_issued_before(