From 04d710b1b808c8b0637cdcb98b68f0bc46b5b6d2 Mon Sep 17 00:00:00 2001 From: "P. Reis" Date: Wed, 19 Feb 2025 11:14:05 -0300 Subject: [PATCH] fix: return mints from the wallet, not just the ones from kind 7375 --- packages/ditto/controllers/api/cashu.ts | 15 ++++++++++++++- .../ditto/middleware/swapNutzapsMiddleware.ts | 2 +- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/packages/ditto/controllers/api/cashu.ts b/packages/ditto/controllers/api/cashu.ts index e003b00a..122fc667 100644 --- a/packages/ditto/controllers/api/cashu.ts +++ b/packages/ditto/controllers/api/cashu.ts @@ -2,6 +2,7 @@ import { CashuMint, CashuWallet, MintQuoteState, Proof } from '@cashu/cashu-ts'; import { confRequiredMw } from '@ditto/api/middleware'; import { Hono } from '@hono/hono'; import { generateSecretKey, getPublicKey } from 'nostr-tools'; +import { NSchema as n } from '@nostrify/nostrify'; import { bytesToString, stringToBytes } from '@scure/base'; import { z } from 'zod'; @@ -246,7 +247,12 @@ app.get('/wallet', requireNip44Signer, swapNutzapsMiddleware, async (c) => { return c.json({ error: 'Wallet not found' }, 404); } - const decryptedContent: string[][] = JSON.parse(await signer.nip44.decrypt(pubkey, event.content)); + const { data: decryptedContent, success } = n.json().pipe(z.string().array().array()).safeParse( + await signer.nip44.decrypt(pubkey, event.content), + ); + if (!success) { + return c.json({ error: 'Could not decrypt wallet content' }, 422); + } const privkey = decryptedContent.find(([value]) => value === 'privkey')?.[1]; if (!privkey || !isNostrId(privkey)) { @@ -258,6 +264,13 @@ app.get('/wallet', requireNip44Signer, swapNutzapsMiddleware, async (c) => { let balance = 0; const mints: string[] = []; + for (const tag of decryptedContent) { + const isMint = tag[0] === 'mint'; + if (isMint) { + mints.push(tag[1]); + } + } + const tokens = await store.query([{ authors: [pubkey], kinds: [7375] }], { signal }); for (const token of tokens) { try { diff --git a/packages/ditto/middleware/swapNutzapsMiddleware.ts b/packages/ditto/middleware/swapNutzapsMiddleware.ts index c81ebdba..c70682fb 100644 --- a/packages/ditto/middleware/swapNutzapsMiddleware.ts +++ b/packages/ditto/middleware/swapNutzapsMiddleware.ts @@ -7,11 +7,11 @@ import { NostrEvent, NostrFilter, NostrSigner, NSchema as n, NStore } from '@nos import { SetRequired } from 'type-fest'; import { stringToBytes } from '@scure/base'; import { logi } from '@soapbox/logi'; +import { z } from 'zod'; import { isNostrId } from '@/utils.ts'; import { errorJson } from '@/utils/log.ts'; import { createEvent } from '@/utils/api.ts'; -import { z } from 'zod'; /** * Swap nutzaps into wallet (create new events) if the user has a wallet, otheriwse, just fallthrough.