Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] Specs conformant error codes #693

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions cashu/core/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down
3 changes: 2 additions & 1 deletion cashu/mint/ledger.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
NotAllowedError,
QuoteNotPaidError,
QuoteSignatureInvalidError,
TransactionAmountExceedsLimitError,
TransactionError,
)
from ..core.helpers import sum_proofs
Expand Down Expand Up @@ -403,7 +404,7 @@ async def mint_quote(self, quote_request: PostMintQuoteRequest) -> MintQuote:
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:
Expand Down
6 changes: 4 additions & 2 deletions cashu/mint/verification.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
from ..core.crypto.secp import PublicKey
from ..core.db import Connection, Database
from ..core.errors import (
InvalidProofsError,
NoSecretInProofsError,
NotAllowedError,
OutputsAlreadySignedError,
SecretTooLongError,
TransactionError,
TransactionUnitError,
Expand Down Expand Up @@ -79,7 +81,7 @@ async def verify_inputs_and_outputs(
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.")
Expand Down Expand Up @@ -141,7 +143,7 @@ async def _verify_outputs(
# 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(
Expand Down
Loading