mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 11:29:46 +00:00
refactor: create getLastRedeemedNutzap function
This commit is contained in:
parent
839aaca179
commit
8890311e2d
1 changed files with 24 additions and 6 deletions
|
|
@ -3,7 +3,7 @@ import { type DittoConf } from '@ditto/conf';
|
||||||
import { MiddlewareHandler } from '@hono/hono';
|
import { MiddlewareHandler } from '@hono/hono';
|
||||||
import { HTTPException } from '@hono/hono/http-exception';
|
import { HTTPException } from '@hono/hono/http-exception';
|
||||||
import { getPublicKey } from 'nostr-tools';
|
import { getPublicKey } from 'nostr-tools';
|
||||||
import { NostrFilter, NostrSigner, NSchema as n, NStore } from '@nostrify/nostrify';
|
import { NostrEvent, NostrFilter, NostrSigner, NSchema as n, NStore } from '@nostrify/nostrify';
|
||||||
import { SetRequired } from 'type-fest';
|
import { SetRequired } from 'type-fest';
|
||||||
import { stringToBytes } from '@scure/base';
|
import { stringToBytes } from '@scure/base';
|
||||||
import { logi } from '@soapbox/logi';
|
import { logi } from '@soapbox/logi';
|
||||||
|
|
@ -59,7 +59,7 @@ export const swapNutzapsMiddleware: MiddlewareHandler<
|
||||||
try {
|
try {
|
||||||
contentTags = z.string().array().array().parse(decryptedContent);
|
contentTags = z.string().array().array().parse(decryptedContent);
|
||||||
} catch {
|
} catch {
|
||||||
return c.json({ error: 'Could not JSON parse the decrypted wallet content.' }, 400);
|
return c.json({ error: 'Could not parse the decrypted wallet content.' }, 400);
|
||||||
}
|
}
|
||||||
|
|
||||||
const privkey = contentTags.find(([value]) => value === 'privkey')?.[1];
|
const privkey = contentTags.find(([value]) => value === 'privkey')?.[1];
|
||||||
|
|
@ -88,12 +88,12 @@ export const swapNutzapsMiddleware: MiddlewareHandler<
|
||||||
|
|
||||||
const nutzapsFilter: NostrFilter = { kinds: [9321], '#p': [pubkey], '#u': mints };
|
const nutzapsFilter: NostrFilter = { kinds: [9321], '#p': [pubkey], '#u': mints };
|
||||||
|
|
||||||
const [nutzapHistory] = await store.query([{ kinds: [7376], authors: [pubkey] }], { signal });
|
const lastRedeemedNutzap = await getLastRedeemedNutzap(store, pubkey, { signal });
|
||||||
if (nutzapHistory) {
|
if (lastRedeemedNutzap) {
|
||||||
nutzapsFilter.since = nutzapHistory.created_at;
|
nutzapsFilter.since = lastRedeemedNutzap.created_at;
|
||||||
}
|
}
|
||||||
|
|
||||||
const mintsToProofs: { [key: string]: { proofs: Proof[]; redeemed: string[][] } } = {};
|
const mintsToProofs: { [key: string]: { proofs: Proof[]; redeemed: string[][] } } = {}; // 'key' is the mint url
|
||||||
|
|
||||||
const nutzaps = await store.query([nutzapsFilter], { signal });
|
const nutzaps = await store.query([nutzapsFilter], { signal });
|
||||||
|
|
||||||
|
|
@ -187,3 +187,21 @@ export const swapNutzapsMiddleware: MiddlewareHandler<
|
||||||
|
|
||||||
await next();
|
await next();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/** Returns a spending history event that contains the last redeemed nutzap. */
|
||||||
|
async function getLastRedeemedNutzap(
|
||||||
|
store: NStore,
|
||||||
|
pubkey: string,
|
||||||
|
opts?: { signal?: AbortSignal },
|
||||||
|
): Promise<NostrEvent | undefined> {
|
||||||
|
const events = await store.query([{ kinds: [7376], authors: [pubkey] }], { signal: opts?.signal });
|
||||||
|
|
||||||
|
for (const event of events) {
|
||||||
|
const nutzap = event.tags.find(([name]) => name === 'e');
|
||||||
|
const redeemed = nutzap?.[3];
|
||||||
|
if (redeemed === 'redeemed') {
|
||||||
|
return event;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
async function getMintsToProofs() {}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue