refactor: use validateAndParseWallet function in GET /wallet

This commit is contained in:
P. Reis 2025-02-20 19:33:25 -03:00
parent 36640dd400
commit d98a4bd263

View file

@ -2,19 +2,22 @@ import { CashuMint, CashuWallet, MintQuoteState, Proof } from '@cashu/cashu-ts';
import { confRequiredMw } from '@ditto/api/middleware'; import { confRequiredMw } from '@ditto/api/middleware';
import { Hono } from '@hono/hono'; import { Hono } from '@hono/hono';
import { generateSecretKey, getPublicKey } from 'nostr-tools'; import { generateSecretKey, getPublicKey } from 'nostr-tools';
import { NSchema as n } from '@nostrify/nostrify'; import { NostrEvent, NSchema as n } from '@nostrify/nostrify';
import { bytesToString, stringToBytes } from '@scure/base'; import { bytesToString, stringToBytes } from '@scure/base';
import { logi } from '@soapbox/logi';
import { z } from 'zod'; import { z } from 'zod';
import { createEvent, parseBody } from '@/utils/api.ts'; import { createEvent, parseBody } from '@/utils/api.ts';
import { requireNip44Signer } from '@/middleware/requireSigner.ts'; import { requireNip44Signer } from '@/middleware/requireSigner.ts';
import { requireStore } from '@/middleware/storeMiddleware.ts'; import { requireStore } from '@/middleware/storeMiddleware.ts';
import { walletSchema } from '@/schema.ts';
import { swapNutzapsMiddleware } from '@/middleware/swapNutzapsMiddleware.ts'; import { swapNutzapsMiddleware } from '@/middleware/swapNutzapsMiddleware.ts';
import { walletSchema } from '@/schema.ts';
import { hydrateEvents } from '@/storages/hydrate.ts';
import { isNostrId, nostrNow } from '@/utils.ts'; import { isNostrId, nostrNow } from '@/utils.ts';
import { logi } from '@soapbox/logi';
import { errorJson } from '@/utils/log.ts'; import { errorJson } from '@/utils/log.ts';
import { getAmount } from '@/utils/bolt11.ts'; import { getAmount } from '@/utils/bolt11.ts';
import { DittoEvent } from '@/interfaces/DittoEvent.ts';
import { validateAndParseWallet } from '@/utils/cashu.ts';
type Wallet = z.infer<typeof walletSchema>; type Wallet = z.infer<typeof walletSchema>;
@ -107,7 +110,7 @@ app.post('/mint/:quote_id', requireNip44Signer, async (c) => {
const now = nostrNow(); const now = nostrNow();
try { try {
if (mintUrl && (expiration > now) && (quote_id === decryptedQuoteId)) { if (mintUrl && (expiration > now) && (quote_id === decryptedQuoteId)) { // TODO: organize order of operations of deleting expired quote ids
const mint = new CashuMint(mintUrl); const mint = new CashuMint(mintUrl);
const wallet = new CashuWallet(mint); const wallet = new CashuWallet(mint);
await wallet.loadMint(); await wallet.loadMint();
@ -198,7 +201,7 @@ app.put('/wallet', requireNip44Signer, async (c) => {
const sk = generateSecretKey(); const sk = generateSecretKey();
const privkey = bytesToString('hex', sk); const privkey = bytesToString('hex', sk);
const p2pk = getPublicKey(stringToBytes('hex', privkey)); const p2pk = getPublicKey(sk);
walletContentTags.push(['privkey', privkey]); walletContentTags.push(['privkey', privkey]);
@ -242,34 +245,14 @@ app.get('/wallet', requireNip44Signer, swapNutzapsMiddleware, async (c) => {
const pubkey = await signer.getPublicKey(); const pubkey = await signer.getPublicKey();
const { signal } = c.req.raw; const { signal } = c.req.raw;
const [event] = await store.query([{ authors: [pubkey], kinds: [17375] }], { signal }); const { data, error } = await validateAndParseWallet(store, signer, pubkey, { signal });
if (!event) { if (error) {
return c.json({ error: 'Wallet not found' }, 404); return c.json({ error: error.message }, 404);
} }
const { data: decryptedContent, success } = n.json().pipe(z.string().array().array()).safeParse( const { p2pk, mints } = data;
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)) {
return c.json({ error: 'Wallet does not contain privkey or privkey is not a valid nostr id.' }, 422);
}
const p2pk = getPublicKey(stringToBytes('hex', privkey));
let balance = 0; 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 }); const tokens = await store.query([{ authors: [pubkey], kinds: [7375] }], { signal });
for (const token of tokens) { for (const token of tokens) {
@ -286,7 +269,7 @@ app.get('/wallet', requireNip44Signer, swapNutzapsMiddleware, async (c) => {
return accumulator + current.amount; return accumulator + current.amount;
}, 0); }, 0);
} catch (e) { } catch (e) {
logi({ level: 'error', ns: 'ditto.api.cashu.wallet.swap', error: errorJson(e) }); logi({ level: 'error', ns: 'ditto.api.cashu.wallet', error: errorJson(e) });
} }
} }