Error model

This commit is contained in:
callebtc
2022-10-05 19:44:08 +02:00
parent dcc72b3961
commit befd12102c
2 changed files with 45 additions and 20 deletions

View File

@@ -4,6 +4,11 @@ from typing import List
from pydantic import BaseModel from pydantic import BaseModel
class CashuError(BaseModel):
code = "000"
error = "CashuError"
class P2SHScript(BaseModel): class P2SHScript(BaseModel):
script: str script: str
signature: str signature: str
@@ -110,6 +115,16 @@ class MintRequest(BaseModel):
blinded_messages: List[BlindedMessage] = [] blinded_messages: List[BlindedMessage] = []
class GetMintResponse(BaseModel):
pr: str
hash: str
class GetMeltResponse(BaseModel):
paid: str
preimage: str
class SplitRequest(BaseModel): class SplitRequest(BaseModel):
proofs: List[Proof] proofs: List[Proof]
amount: int amount: int
@@ -127,6 +142,11 @@ class SplitRequest(BaseModel):
self.output_data = None self.output_data = None
class PostSplitResponse(BaseModel):
fst: List[BlindedSignature]
snd: List[BlindedSignature]
class CheckRequest(BaseModel): class CheckRequest(BaseModel):
proofs: List[Proof] proofs: List[Proof]

View File

@@ -3,7 +3,16 @@ from typing import Union
from fastapi import APIRouter from fastapi import APIRouter
from secp256k1 import PublicKey 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 from cashu.mint import ledger
router: APIRouter = APIRouter() router: APIRouter = APIRouter()
@@ -17,10 +26,16 @@ def keys():
@router.get("/mint") @router.get("/mint")
async def request_mint(amount: int = 0): 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) payment_request, payment_hash = await ledger.request_mint(amount)
print(f"Lightning invoice: {payment_request}") 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") @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. Requests the minting of tokens belonging to a paid payment request.
Parameters: Call this endpoint after `GET /mint`.
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.
""" """
amounts = [] amounts = []
B_s = [] 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) promises = await ledger.mint(B_s, amounts, payment_hash=payment_hash)
return promises return promises
except Exception as exc: except Exception as exc:
return {"error": str(exc)} return CashuError(error=str(exc))
@router.post("/melt") @router.post("/melt")
@@ -60,7 +63,8 @@ async def melt(payload: MeltRequest):
Requests tokens to be destroyed and sent out via Lightning. Requests tokens to be destroyed and sent out via Lightning.
""" """
ok, preimage = await ledger.melt(payload.proofs, payload.amount, payload.invoice) 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") @router.post("/check")
@@ -80,8 +84,9 @@ async def split(payload: SplitRequest):
try: try:
split_return = await ledger.split(proofs, amount, outputs) split_return = await ledger.split(proofs, amount, outputs)
except Exception as exc: except Exception as exc:
return {"error": str(exc)} return CashuError(error=str(exc))
if not split_return: if not split_return:
return {"error": "there was a problem with the split."} return {"error": "there was a problem with the split."}
fst_promises, snd_promises = split_return fst_promises, snd_promises = split_return
return {"fst": fst_promises, "snd": snd_promises} resp = PostSplitResponse(fst=fst_promises, snd=snd_promises)
return resp