From b9d813804edabdede82c1192f276a0aeb1d13ec2 Mon Sep 17 00:00:00 2001 From: "P. Reis" Date: Sun, 16 Feb 2025 20:00:14 -0300 Subject: [PATCH] feat: implement GET '/api/v1/ditto/cashu/quote/:quote_id' (not tested) --- packages/ditto/controllers/api/cashu.ts | 26 ++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/packages/ditto/controllers/api/cashu.ts b/packages/ditto/controllers/api/cashu.ts index 1c5af55f..1b8ce403 100644 --- a/packages/ditto/controllers/api/cashu.ts +++ b/packages/ditto/controllers/api/cashu.ts @@ -10,7 +10,7 @@ import { requireNip44Signer } from '@/middleware/requireSigner.ts'; import { requireStore } from '@/middleware/storeMiddleware.ts'; import { walletSchema } from '@/schema.ts'; import { swapNutzapsMiddleware } from '@/middleware/swapNutzapsMiddleware.ts'; -import { isNostrId } from '@/utils.ts'; +import { isNostrId, nostrNow } from '@/utils.ts'; import { logi } from '@soapbox/logi'; import { errorJson } from '@/utils/log.ts'; @@ -82,6 +82,30 @@ app.post('/quote', requireNip44Signer, async (c) => { * https://github.com/cashubtc/nuts/blob/main/04.md#check-mint-quote-state */ app.get('/quote/:quote_id', requireNip44Signer, async (c) => { + const signer = c.var.signer; + const { signal } = c.req.raw; + const store = c.get('store'); + const pubkey = await signer.getPublicKey(); + const quote_id = c.req.param('quote_id'); + + const events = await store.query([{ kinds: [7374], authors: [pubkey] }], { signal }); + for (const event of events) { + const decryptedQuoteId = await signer.nip44.decrypt(pubkey, event.content); + const mintUrl = event.tags.find(([name]) => name === 'mint')?.[1]; + const expiration = Number(event.tags.find(([name]) => name === 'expiration')?.[1]); + const now = nostrNow(); + + if (mintUrl && (expiration > now) && (quote_id === decryptedQuoteId)) { + const mint = new CashuMint(mintUrl); + const wallet = new CashuWallet(mint); + await wallet.loadMint(); + + const mintQuote = await wallet.checkMintQuote(quote_id); + return c.json(mintQuote, 200); + } + } + + return c.json({ error: 'Quote not found' }, 404); }); /**