From befd12102c70f9fc582d3feb122e33cfe0f81f7c Mon Sep 17 00:00:00 2001 From: callebtc <93376500+callebtc@users.noreply.github.com> Date: Wed, 5 Oct 2022 19:44:08 +0200 Subject: [PATCH] Error model --- cashu/core/base.py | 20 ++++++++++++++++++++ cashu/mint/router.py | 45 ++++++++++++++++++++++++-------------------- 2 files changed, 45 insertions(+), 20 deletions(-) diff --git a/cashu/core/base.py b/cashu/core/base.py index 4f7e7dd..b54e26f 100644 --- a/cashu/core/base.py +++ b/cashu/core/base.py @@ -4,6 +4,11 @@ from typing import List from pydantic import BaseModel +class CashuError(BaseModel): + code = "000" + error = "CashuError" + + class P2SHScript(BaseModel): script: str signature: str @@ -110,6 +115,16 @@ class MintRequest(BaseModel): blinded_messages: List[BlindedMessage] = [] +class GetMintResponse(BaseModel): + pr: str + hash: str + + +class GetMeltResponse(BaseModel): + paid: str + preimage: str + + class SplitRequest(BaseModel): proofs: List[Proof] amount: int @@ -127,6 +142,11 @@ class SplitRequest(BaseModel): self.output_data = None +class PostSplitResponse(BaseModel): + fst: List[BlindedSignature] + snd: List[BlindedSignature] + + class CheckRequest(BaseModel): proofs: List[Proof] diff --git a/cashu/mint/router.py b/cashu/mint/router.py index 807fbf4..43696ec 100644 --- a/cashu/mint/router.py +++ b/cashu/mint/router.py @@ -3,7 +3,16 @@ from typing import Union from fastapi import APIRouter from secp256k1 import PublicKey -from cashu.core.base import CheckRequest, MeltRequest, MintRequest, SplitRequest +from cashu.core.base import ( + CashuError, + CheckRequest, + MeltRequest, + MintRequest, + SplitRequest, + GetMintResponse, + GetMeltResponse, + PostSplitResponse, +) from cashu.mint import ledger router: APIRouter = APIRouter() @@ -17,10 +26,16 @@ def keys(): @router.get("/mint") async def request_mint(amount: int = 0): - """Request minting of tokens. Server responds with a Lightning invoice.""" + """ + Request minting of new tokens. The mint responds with a Lightning invoice. + This endpoint can be used for a Lightning invoice UX flow. + + Call `POST /mint` after paying the invoice. + """ payment_request, payment_hash = await ledger.request_mint(amount) print(f"Lightning invoice: {payment_request}") - return {"pr": payment_request, "hash": payment_hash} + resp = GetMintResponse(pr=payment_request, hash=payment_hash) + return resp @router.post("/mint") @@ -28,19 +43,7 @@ async def mint(payloads: MintRequest, payment_hash: Union[str, None] = None): """ Requests the minting of tokens belonging to a paid payment request. - Parameters: - pr: payment_request of the Lightning paid invoice. - - Body (JSON): - payloads: contains a list of blinded messages waiting to be signed. - - NOTE: - - This needs to be replaced by the preimage otherwise someone knowing - the payment_request can request the tokens instead of the rightful - owner. - - The blinded message should ideally be provided to the server *before* payment - in the GET /mint endpoint so that the server knows to sign only these tokens - when the invoice is paid. + Call this endpoint after `GET /mint`. """ amounts = [] B_s = [] @@ -51,7 +54,7 @@ async def mint(payloads: MintRequest, payment_hash: Union[str, None] = None): promises = await ledger.mint(B_s, amounts, payment_hash=payment_hash) return promises except Exception as exc: - return {"error": str(exc)} + return CashuError(error=str(exc)) @router.post("/melt") @@ -60,7 +63,8 @@ async def melt(payload: MeltRequest): Requests tokens to be destroyed and sent out via Lightning. """ ok, preimage = await ledger.melt(payload.proofs, payload.amount, payload.invoice) - return {"paid": ok, "preimage": preimage} + resp = GetMeltResponse(paid=ok, preimage=preimage) + return resp @router.post("/check") @@ -80,8 +84,9 @@ async def split(payload: SplitRequest): try: split_return = await ledger.split(proofs, amount, outputs) except Exception as exc: - return {"error": str(exc)} + return CashuError(error=str(exc)) if not split_return: return {"error": "there was a problem with the split."} fst_promises, snd_promises = split_return - return {"fst": fst_promises, "snd": snd_promises} + resp = PostSplitResponse(fst=fst_promises, snd=snd_promises) + return resp