refactor: create walletSchema and use it where required

This commit is contained in:
P. Reis 2025-02-11 11:29:58 -03:00
parent 1ff6511b39
commit 89840eb279
3 changed files with 20 additions and 17 deletions

View file

@ -1,13 +1,13 @@
import { Env as HonoEnv, Hono } from '@hono/hono';
import { NostrSigner, NSchema as n, NSecSigner, NStore } from '@nostrify/nostrify';
import { NostrSigner, NSecSigner, NStore } from '@nostrify/nostrify';
import { generateSecretKey, getPublicKey } from 'nostr-tools';
import { bytesToString, stringToBytes } from '@scure/base';
import { assertEquals, assertExists } from '@std/assert';
import { z } from 'zod';
import { createTestDB } from '@/test.ts';
import cashuApp from '@/controllers/api/cashu.ts';
import { walletSchema } from '@/schema.ts';
interface AppEnv extends HonoEnv {
Variables: {
@ -29,13 +29,6 @@ Deno.test('PUT /wallet must be successful', {
const signer = new NSecSigner(sk);
const nostrPrivateKey = bytesToString('hex', sk);
const expectedResponseSchema = z.object({
pubkey_p2pk: n.id(),
mints: z.array(z.string()).nonempty(),
relays: z.array(z.string()).nonempty(),
balance: z.number(),
});
const app = new Hono<AppEnv>().use(
async (c, next) => {
c.set('signer', signer);
@ -68,7 +61,7 @@ Deno.test('PUT /wallet must be successful', {
assertExists(wallet);
assertEquals(wallet.kind, 17375);
const { data, success } = expectedResponseSchema.safeParse(await response.json());
const { data, success } = walletSchema.safeParse(await response.json());
assertEquals(success, true);
if (!data) return; // get rid of typescript error possibly undefined

View file

@ -13,6 +13,9 @@ import { errorJson } from '@/utils/log.ts';
import { signerMiddleware } from '@/middleware/signerMiddleware.ts';
import { requireNip44Signer } from '@/middleware/requireSigner.ts';
import { storeMiddleware } from '@/middleware/storeMiddleware.ts';
import { walletSchema } from '@/schema.ts';
type Wallet = z.infer<typeof walletSchema>;
const app = new Hono().use('*', storeMiddleware, signerMiddleware);
@ -34,13 +37,6 @@ const app = new Hono().use('*', storeMiddleware, signerMiddleware);
/* PUT /api/v1/ditto/cashu/wallet -> Wallet */
/* DELETE /api/v1/ditto/cashu/wallet -> 204 */
export interface Wallet {
pubkey_p2pk: string;
mints: string[];
relays: string[];
balance: number;
}
interface Nutzap {
amount: number;
event_id?: string;

View file

@ -1,4 +1,5 @@
import ISO6391, { LanguageCode } from 'iso-639-1';
import { NSchema as n } from '@nostrify/nostrify';
import { z } from 'zod';
/** Validates individual items in an array, dropping any that aren't valid. */
@ -80,6 +81,18 @@ const sizesSchema = z.string().refine((value) =>
value.split(' ').every((v) => /^[1-9]\d{0,3}[xX][1-9]\d{0,3}$/.test(v))
);
/** Ditto Cashu wallet */
const walletSchema = z.object({
pubkey_p2pk: n.id(),
mints: z.array(z.string().url()).nonempty().transform((val) => {
return [...new Set(val)];
}),
relays: z.array(z.string()).nonempty().transform((val) => {
return [...new Set(val)];
}),
balance: z.number(),
});
export {
booleanParamSchema,
decode64Schema,
@ -91,5 +104,6 @@ export {
percentageSchema,
safeUrlSchema,
sizesSchema,
walletSchema,
wsUrlSchema,
};