applesauce-wallet 0.0.0-next-20250610194602 → 0.0.0-next-20250718170433
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/__tests__/exports.test.js +1 -1
- package/dist/actions/index.d.ts +2 -0
- package/dist/actions/index.js +2 -0
- package/dist/actions/zap-info.d.ts +22 -0
- package/dist/actions/zap-info.js +83 -0
- package/dist/actions/zaps.d.ts +8 -0
- package/dist/actions/zaps.js +30 -0
- package/dist/blueprints/index.d.ts +3 -1
- package/dist/blueprints/index.js +3 -1
- package/dist/blueprints/zaps.d.ts +8 -0
- package/dist/blueprints/zaps.js +12 -0
- package/dist/helpers/__tests__/tokens.test.js +2 -2
- package/dist/helpers/index.d.ts +5 -3
- package/dist/helpers/index.js +5 -3
- package/dist/helpers/zap-info.d.ts +19 -0
- package/dist/helpers/zap-info.js +42 -0
- package/dist/helpers/zaps.d.ts +26 -0
- package/dist/helpers/zaps.js +62 -0
- package/dist/models/index.d.ts +2 -1
- package/dist/models/index.js +2 -1
- package/dist/models/zaps.d.ts +6 -0
- package/dist/models/zaps.js +16 -0
- package/dist/operations/event/index.d.ts +4 -2
- package/dist/operations/event/index.js +4 -2
- package/dist/operations/event/zap-info.d.ts +10 -0
- package/dist/operations/event/zap-info.js +17 -0
- package/dist/operations/event/zaps.d.ts +14 -0
- package/dist/operations/event/zaps.js +32 -0
- package/dist/operations/index.d.ts +1 -1
- package/dist/operations/index.js +1 -1
- package/package.json +5 -5
package/dist/actions/index.d.ts
CHANGED
package/dist/actions/index.js
CHANGED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Action } from "applesauce-actions";
|
|
2
|
+
/** An action to add a relay to the kind 10019 nutzap info event */
|
|
3
|
+
export declare function AddNutzapInfoRelay(relay: string | string[]): Action;
|
|
4
|
+
/** An action to remove a relay from the kind 10019 nutzap info event */
|
|
5
|
+
export declare function RemoveNutzapInfoRelay(relay: string | string[]): Action;
|
|
6
|
+
/** An action to add a mint to the kind 10019 nutzap info event */
|
|
7
|
+
export declare function AddNutzapInfoMint(mint: {
|
|
8
|
+
url: string;
|
|
9
|
+
units?: string[];
|
|
10
|
+
} | Array<{
|
|
11
|
+
url: string;
|
|
12
|
+
units?: string[];
|
|
13
|
+
}>): Action;
|
|
14
|
+
/** An action to remove a mint from the kind 10019 nutzap info event */
|
|
15
|
+
export declare function RemoveNutzapInfoMint(mint: string | string[]): Action;
|
|
16
|
+
/** An action to set the pubkey for the kind 10019 nutzap info event */
|
|
17
|
+
export declare function SetNutzapInfoPubkey(pubkey: string): Action;
|
|
18
|
+
/** An action to update the entire nutzap info event */
|
|
19
|
+
export declare function UpdateNutzapInfo(relays: string[], mints: Array<{
|
|
20
|
+
url: string;
|
|
21
|
+
units?: string[];
|
|
22
|
+
}>, pubkey: string): Action;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { modifyPublicTags } from "applesauce-factory/operations/event";
|
|
2
|
+
import { addNameValueTag, removeNameValueTag, setSingletonTag } from "applesauce-factory/operations/tag";
|
|
3
|
+
import { NUTZAP_INFO_KIND } from "../helpers/zap-info.js";
|
|
4
|
+
import { setNutzapInfoMints, setNutzapInfoPubkey, setNutzapInfoRelays } from "../operations/event/zap-info.js";
|
|
5
|
+
/** An action to add a relay to the kind 10019 nutzap info event */
|
|
6
|
+
export function AddNutzapInfoRelay(relay) {
|
|
7
|
+
return async function* ({ events, factory, self }) {
|
|
8
|
+
if (typeof relay === "string")
|
|
9
|
+
relay = [relay];
|
|
10
|
+
const operations = relay.map((r) => addNameValueTag(["relay", r], false));
|
|
11
|
+
const nutzapInfo = events.getReplaceable(NUTZAP_INFO_KIND, self);
|
|
12
|
+
const draft = nutzapInfo
|
|
13
|
+
? await factory.modifyTags(nutzapInfo, ...operations)
|
|
14
|
+
: await factory.build({ kind: NUTZAP_INFO_KIND }, modifyPublicTags(...operations));
|
|
15
|
+
const signed = await factory.sign(draft);
|
|
16
|
+
yield signed;
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
/** An action to remove a relay from the kind 10019 nutzap info event */
|
|
20
|
+
export function RemoveNutzapInfoRelay(relay) {
|
|
21
|
+
return async function* ({ events, factory, self }) {
|
|
22
|
+
if (typeof relay === "string")
|
|
23
|
+
relay = [relay];
|
|
24
|
+
const nutzapInfo = events.getReplaceable(NUTZAP_INFO_KIND, self);
|
|
25
|
+
if (!nutzapInfo)
|
|
26
|
+
return;
|
|
27
|
+
const draft = await factory.modifyTags(nutzapInfo, ...relay.map((r) => removeNameValueTag(["relay", r])));
|
|
28
|
+
const signed = await factory.sign(draft);
|
|
29
|
+
yield signed;
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
/** An action to add a mint to the kind 10019 nutzap info event */
|
|
33
|
+
export function AddNutzapInfoMint(mint) {
|
|
34
|
+
return async function* ({ events, factory, self }) {
|
|
35
|
+
const mints = Array.isArray(mint) ? mint : [mint];
|
|
36
|
+
const operations = mints.map((m) => {
|
|
37
|
+
const tag = m.units ? ["mint", m.url, ...m.units] : ["mint", m.url];
|
|
38
|
+
return addNameValueTag(tag, false);
|
|
39
|
+
});
|
|
40
|
+
const nutzapInfo = events.getReplaceable(NUTZAP_INFO_KIND, self);
|
|
41
|
+
const draft = nutzapInfo
|
|
42
|
+
? await factory.modifyTags(nutzapInfo, ...operations)
|
|
43
|
+
: await factory.build({ kind: NUTZAP_INFO_KIND }, modifyPublicTags(...operations));
|
|
44
|
+
const signed = await factory.sign(draft);
|
|
45
|
+
yield signed;
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
/** An action to remove a mint from the kind 10019 nutzap info event */
|
|
49
|
+
export function RemoveNutzapInfoMint(mint) {
|
|
50
|
+
return async function* ({ events, factory, self }) {
|
|
51
|
+
if (typeof mint === "string")
|
|
52
|
+
mint = [mint];
|
|
53
|
+
const nutzapInfo = events.getReplaceable(NUTZAP_INFO_KIND, self);
|
|
54
|
+
if (!nutzapInfo)
|
|
55
|
+
return;
|
|
56
|
+
const draft = await factory.modifyTags(nutzapInfo, ...mint.map((m) => removeNameValueTag(["mint", m])));
|
|
57
|
+
const signed = await factory.sign(draft);
|
|
58
|
+
yield signed;
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
/** An action to set the pubkey for the kind 10019 nutzap info event */
|
|
62
|
+
export function SetNutzapInfoPubkey(pubkey) {
|
|
63
|
+
return async function* ({ events, factory, self }) {
|
|
64
|
+
const nutzapInfo = events.getReplaceable(NUTZAP_INFO_KIND, self);
|
|
65
|
+
const draft = nutzapInfo
|
|
66
|
+
? await factory.modifyTags(nutzapInfo, setSingletonTag(["pubkey", pubkey], true))
|
|
67
|
+
: await factory.build({ kind: NUTZAP_INFO_KIND }, modifyPublicTags(setSingletonTag(["pubkey", pubkey], true)));
|
|
68
|
+
const signed = await factory.sign(draft);
|
|
69
|
+
yield signed;
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
/** An action to update the entire nutzap info event */
|
|
73
|
+
export function UpdateNutzapInfo(relays, mints, pubkey) {
|
|
74
|
+
return async function* ({ events, factory, self }) {
|
|
75
|
+
const operations = [setNutzapInfoRelays(relays), setNutzapInfoMints(mints), setNutzapInfoPubkey(pubkey)];
|
|
76
|
+
const nutzapInfo = events.getReplaceable(NUTZAP_INFO_KIND, self);
|
|
77
|
+
const draft = nutzapInfo
|
|
78
|
+
? await factory.modify(nutzapInfo, ...operations)
|
|
79
|
+
: await factory.build({ kind: NUTZAP_INFO_KIND }, ...operations);
|
|
80
|
+
const signed = await factory.sign(draft);
|
|
81
|
+
yield signed;
|
|
82
|
+
};
|
|
83
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Token } from "@cashu/cashu-ts";
|
|
2
|
+
import { Action } from "applesauce-actions";
|
|
3
|
+
import { NostrEvent } from "nostr-tools";
|
|
4
|
+
import { ProfilePointer } from "nostr-tools/nip19";
|
|
5
|
+
/** Creates a NIP-61 nutzap event for an event with a token */
|
|
6
|
+
export declare function NutzapEvent(event: NostrEvent, token: Token, comment?: string): Action;
|
|
7
|
+
/** Creates a NIP-61 nutzap event to a users profile */
|
|
8
|
+
export declare function NutzapProfile(user: string | ProfilePointer, token: Token, comment?: string): Action;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { NutzapBlueprint, ProfileNutzapBlueprint } from "../blueprints/zaps.js";
|
|
2
|
+
import { NUTZAP_INFO_KIND, verifyProofsLocked } from "../helpers/zap-info.js";
|
|
3
|
+
/** Creates a NIP-61 nutzap event for an event with a token */
|
|
4
|
+
export function NutzapEvent(event, token, comment) {
|
|
5
|
+
return async function* ({ events, factory }) {
|
|
6
|
+
const recipient = event.pubkey;
|
|
7
|
+
const info = events.getReplaceable(NUTZAP_INFO_KIND, recipient);
|
|
8
|
+
if (!info)
|
|
9
|
+
throw new Error("Nutzap info not found");
|
|
10
|
+
// Verify all tokens are p2pk locked
|
|
11
|
+
verifyProofsLocked(token.proofs, info);
|
|
12
|
+
// NOTE: Disabled because mints and units should be checked by the app before
|
|
13
|
+
// const mints = getNutzapInfoMints(info);
|
|
14
|
+
// if (!mints.some((m) => m.mint === token.mint)) throw new Error("Token mint not found in nutzap info");
|
|
15
|
+
const nutzap = await factory.sign(await factory.create(NutzapBlueprint, event, token, comment || token.memo));
|
|
16
|
+
yield nutzap;
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
/** Creates a NIP-61 nutzap event to a users profile */
|
|
20
|
+
export function NutzapProfile(user, token, comment) {
|
|
21
|
+
return async function* ({ events, factory }) {
|
|
22
|
+
const info = events.getReplaceable(NUTZAP_INFO_KIND, typeof user === "string" ? user : user.pubkey);
|
|
23
|
+
if (!info)
|
|
24
|
+
throw new Error("Nutzap info not found");
|
|
25
|
+
// Verify all tokens are p2pk locked
|
|
26
|
+
verifyProofsLocked(token.proofs, info);
|
|
27
|
+
const nutzap = await factory.sign(await factory.create(ProfileNutzapBlueprint, user, token, comment || token.memo));
|
|
28
|
+
yield nutzap;
|
|
29
|
+
};
|
|
30
|
+
}
|
package/dist/blueprints/index.js
CHANGED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Token } from "@cashu/cashu-ts";
|
|
2
|
+
import { EventBlueprint } from "applesauce-factory";
|
|
3
|
+
import { NostrEvent } from "nostr-tools";
|
|
4
|
+
import { ProfilePointer } from "nostr-tools/nip19";
|
|
5
|
+
/** A blueprint to create a NIP-61 nutzap event for an event */
|
|
6
|
+
export declare function NutzapBlueprint(event: NostrEvent, token: Token, comment?: string): EventBlueprint;
|
|
7
|
+
/** A blueprint to create a NIP-61 nutzap event for a user instead of an event */
|
|
8
|
+
export declare function ProfileNutzapBlueprint(user: string | ProfilePointer, token: Token, comment?: string): EventBlueprint;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { blueprint } from "applesauce-factory";
|
|
2
|
+
import { skip } from "applesauce-factory/helpers";
|
|
3
|
+
import { NUTZAP_KIND } from "../helpers/zaps.js";
|
|
4
|
+
import { setNutzapComment, setNutzapEvent, setNutzapMint, setNutzapProofs, setNutzapRecipient, } from "../operations/event/zaps.js";
|
|
5
|
+
/** A blueprint to create a NIP-61 nutzap event for an event */
|
|
6
|
+
export function NutzapBlueprint(event, token, comment) {
|
|
7
|
+
return blueprint(NUTZAP_KIND, setNutzapProofs(token.proofs), setNutzapMint(token.mint), setNutzapEvent(event), setNutzapRecipient(event.pubkey), comment ? setNutzapComment(comment) : skip());
|
|
8
|
+
}
|
|
9
|
+
/** A blueprint to create a NIP-61 nutzap event for a user instead of an event */
|
|
10
|
+
export function ProfileNutzapBlueprint(user, token, comment) {
|
|
11
|
+
return blueprint(NUTZAP_KIND, setNutzapProofs(token.proofs), setNutzapMint(token.mint), setNutzapRecipient(user), comment ? setNutzapComment(comment) : skip());
|
|
12
|
+
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { describe, expect, it } from "vitest";
|
|
2
2
|
import { EventFactory } from "applesauce-factory";
|
|
3
|
-
import { EncryptedContentSymbol, unixNow } from "applesauce-core/helpers";
|
|
4
3
|
import { FakeUser } from "../../__tests__/fake-user.js";
|
|
5
4
|
import { WalletTokenBlueprint } from "../../blueprints/tokens.js";
|
|
6
5
|
import { decodeTokenFromEmojiString, dumbTokenSelection, encodeTokenToEmoji, unlockTokenContent } from "../tokens.js";
|
|
6
|
+
import { HiddenContentSymbol, unixNow } from "applesauce-core/helpers";
|
|
7
7
|
const user = new FakeUser();
|
|
8
8
|
const factory = new EventFactory({ signer: user });
|
|
9
9
|
describe("dumbTokenSelection", () => {
|
|
@@ -58,7 +58,7 @@ describe("dumbTokenSelection", () => {
|
|
|
58
58
|
bDraft.created_at -= 60 * 60 * 7;
|
|
59
59
|
const b = await user.signEvent(bDraft);
|
|
60
60
|
// manually remove the hidden content to lock it again
|
|
61
|
-
Reflect.deleteProperty(b,
|
|
61
|
+
Reflect.deleteProperty(b, HiddenContentSymbol);
|
|
62
62
|
expect(dumbTokenSelection([a, b], 20).events).toEqual([a]);
|
|
63
63
|
});
|
|
64
64
|
it("should ignore duplicate proofs", async () => {
|
package/dist/helpers/index.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
export * from "./wallet.js";
|
|
2
|
-
export * from "./tokens.js";
|
|
3
|
-
export * from "./history.js";
|
|
4
1
|
export * from "./animated-qr.js";
|
|
2
|
+
export * from "./history.js";
|
|
3
|
+
export * from "./tokens.js";
|
|
4
|
+
export * from "./wallet.js";
|
|
5
|
+
export * from "./zap-info.js";
|
|
6
|
+
export * from "./zaps.js";
|
package/dist/helpers/index.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
export * from "./wallet.js";
|
|
2
|
-
export * from "./tokens.js";
|
|
3
|
-
export * from "./history.js";
|
|
4
1
|
export * from "./animated-qr.js";
|
|
2
|
+
export * from "./history.js";
|
|
3
|
+
export * from "./tokens.js";
|
|
4
|
+
export * from "./wallet.js";
|
|
5
|
+
export * from "./zap-info.js";
|
|
6
|
+
export * from "./zaps.js";
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Proof } from "@cashu/cashu-ts";
|
|
2
|
+
import { NostrEvent } from "nostr-tools";
|
|
3
|
+
export declare const NUTZAP_INFO_KIND = 10019;
|
|
4
|
+
export declare const NutzapMintsSymbol: unique symbol;
|
|
5
|
+
export declare const NutzapRelaysSymbol: unique symbol;
|
|
6
|
+
/** Returns the relay URLs from a kind:10019 nutzap info event */
|
|
7
|
+
export declare function getNutzapInfoRelays(event: NostrEvent): string[];
|
|
8
|
+
/** Returns the mint URLs from a kind:10019 nutzap info event */
|
|
9
|
+
export declare function getNutzapInfoMints(event: NostrEvent): {
|
|
10
|
+
mint: string;
|
|
11
|
+
units?: string[];
|
|
12
|
+
}[];
|
|
13
|
+
/** Returns the pubkey for P2PK-locking from a kind:10019 nutzap info event */
|
|
14
|
+
export declare function getNutzapInfoPubkey(event: NostrEvent): string | undefined;
|
|
15
|
+
/**
|
|
16
|
+
* verfies if proofs are locked to nutzap info
|
|
17
|
+
* @throws {Error} if proofs are not locked to nutzap info
|
|
18
|
+
*/
|
|
19
|
+
export declare function verifyProofsLocked(proofs: Proof[], info: NostrEvent): void;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { getOrComputeCachedValue, mergeRelaySets, safeParse } from "applesauce-core/helpers";
|
|
2
|
+
export const NUTZAP_INFO_KIND = 10019;
|
|
3
|
+
// Symbols for caching computed values
|
|
4
|
+
export const NutzapMintsSymbol = Symbol.for("nutzap-mints");
|
|
5
|
+
export const NutzapRelaysSymbol = Symbol.for("nutzap-relays");
|
|
6
|
+
/** Returns the relay URLs from a kind:10019 nutzap info event */
|
|
7
|
+
export function getNutzapInfoRelays(event) {
|
|
8
|
+
return getOrComputeCachedValue(event, NutzapRelaysSymbol, () => {
|
|
9
|
+
return mergeRelaySets(event.tags.filter((t) => t[0] === "relay").map((t) => t[1]));
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
/** Returns the mint URLs from a kind:10019 nutzap info event */
|
|
13
|
+
export function getNutzapInfoMints(event) {
|
|
14
|
+
return getOrComputeCachedValue(event, NutzapMintsSymbol, () => {
|
|
15
|
+
return event.tags.filter((t) => t[0] === "mint").map((t) => ({ mint: t[1], units: t.slice(2).filter(Boolean) }));
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
/** Returns the pubkey for P2PK-locking from a kind:10019 nutzap info event */
|
|
19
|
+
export function getNutzapInfoPubkey(event) {
|
|
20
|
+
return event.tags.find((t) => t[0] === "pubkey")?.[1];
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* verfies if proofs are locked to nutzap info
|
|
24
|
+
* @throws {Error} if proofs are not locked to nutzap info
|
|
25
|
+
*/
|
|
26
|
+
export function verifyProofsLocked(proofs, info) {
|
|
27
|
+
const pubkey = getNutzapInfoPubkey(info);
|
|
28
|
+
if (!pubkey)
|
|
29
|
+
throw new Error("Nutzap info must have a pubkey");
|
|
30
|
+
const fullPubkey = pubkey.length === 64 ? `02${pubkey}` : pubkey;
|
|
31
|
+
for (const proof of proofs) {
|
|
32
|
+
const secret = safeParse(proof.secret);
|
|
33
|
+
if (!secret)
|
|
34
|
+
throw new Error(`Cashu token must have a spending condition`);
|
|
35
|
+
if (!Array.isArray(secret))
|
|
36
|
+
throw new Error("Invalid spending condition");
|
|
37
|
+
if (secret[0] !== "P2PK")
|
|
38
|
+
throw new Error("Token proofs must be P2PK locked");
|
|
39
|
+
if (secret[1].data !== fullPubkey)
|
|
40
|
+
throw new Error("Token proofs must be P2PK locked to the recipient's nutzap pubkey");
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { Proof } from "@cashu/cashu-ts";
|
|
2
|
+
import { NostrEvent } from "nostr-tools";
|
|
3
|
+
import { AddressPointer, EventPointer } from "nostr-tools/nip19";
|
|
4
|
+
export declare const NUTZAP_KIND = 9321;
|
|
5
|
+
export declare const NutzapProofsSymbol: unique symbol;
|
|
6
|
+
export declare const NutzapAmountSymbol: unique symbol;
|
|
7
|
+
/** Returns the cashu proofs from a kind:9321 nutzap event */
|
|
8
|
+
export declare function getNutzapProofs(event: NostrEvent): Proof[];
|
|
9
|
+
/** Returns the mint URL from a kind:9321 nutzap event */
|
|
10
|
+
export declare function getNutzapMint(event: NostrEvent): string | undefined;
|
|
11
|
+
/** Returns the recipient pubkey from a kind:9321 nutzap event */
|
|
12
|
+
export declare function getNutzapRecipient(event: NostrEvent): string | undefined;
|
|
13
|
+
/** Returns the event ID being nutzapped from a kind:9321 nutzap event */
|
|
14
|
+
export declare function getNutzapEventPointer(event: NostrEvent): EventPointer | undefined;
|
|
15
|
+
/** Returns the event ID being nutzapped from a kind:9321 nutzap event */
|
|
16
|
+
export declare function getNutzapAddressPointer(event: NostrEvent): AddressPointer | undefined;
|
|
17
|
+
/** Returns the EventPointer or AddressPointer from a kind:9321 nutzap event */
|
|
18
|
+
export declare function getNutzapPointer(event: NostrEvent): EventPointer | AddressPointer | undefined;
|
|
19
|
+
/** Returns the comment from a kind:9321 nutzap event */
|
|
20
|
+
export declare function getNutzapComment(event: NostrEvent): string | undefined;
|
|
21
|
+
/** Calculates the total amount of sats in a kind:9321 nutzap event */
|
|
22
|
+
export declare function getNutzapAmount(event: NostrEvent): number;
|
|
23
|
+
/** Checks if a nutzap is valid according to NIP-61 requirements */
|
|
24
|
+
export declare function isValidNutzap(nutzap: NostrEvent): boolean;
|
|
25
|
+
/** Checks if a nutzap event has already been redeemed based on kind:7376 wallet history events */
|
|
26
|
+
export declare function isNutzapRedeemed(nutzapId: string, history: NostrEvent[]): boolean;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { getAddressPointerFromATag, getEventPointerFromETag, getOrComputeCachedValue, processTags, safeParse, } from "applesauce-core/helpers";
|
|
2
|
+
import { getHistoryRedeemed } from "./history.js";
|
|
3
|
+
export const NUTZAP_KIND = 9321;
|
|
4
|
+
// Symbols for caching computed values
|
|
5
|
+
export const NutzapProofsSymbol = Symbol.for("nutzap-proofs");
|
|
6
|
+
export const NutzapAmountSymbol = Symbol.for("nutzap-amount");
|
|
7
|
+
/** Returns the cashu proofs from a kind:9321 nutzap event */
|
|
8
|
+
export function getNutzapProofs(event) {
|
|
9
|
+
return getOrComputeCachedValue(event, NutzapProofsSymbol, () => {
|
|
10
|
+
return processTags(event.tags, (tag) => (tag[0] === "proof" ? safeParse(tag[1]) : undefined));
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
/** Returns the mint URL from a kind:9321 nutzap event */
|
|
14
|
+
export function getNutzapMint(event) {
|
|
15
|
+
return event.tags.find((t) => t[0] === "u")?.[1];
|
|
16
|
+
}
|
|
17
|
+
/** Returns the recipient pubkey from a kind:9321 nutzap event */
|
|
18
|
+
export function getNutzapRecipient(event) {
|
|
19
|
+
return event.tags.find((t) => t[0] === "p")?.[1];
|
|
20
|
+
}
|
|
21
|
+
/** Returns the event ID being nutzapped from a kind:9321 nutzap event */
|
|
22
|
+
export function getNutzapEventPointer(event) {
|
|
23
|
+
const tag = event.tags.find((t) => t[0] === "e");
|
|
24
|
+
if (!tag)
|
|
25
|
+
return;
|
|
26
|
+
return getEventPointerFromETag(tag);
|
|
27
|
+
}
|
|
28
|
+
/** Returns the event ID being nutzapped from a kind:9321 nutzap event */
|
|
29
|
+
export function getNutzapAddressPointer(event) {
|
|
30
|
+
const tag = event.tags.find((t) => t[0] === "a");
|
|
31
|
+
if (!tag)
|
|
32
|
+
return;
|
|
33
|
+
return getAddressPointerFromATag(tag);
|
|
34
|
+
}
|
|
35
|
+
/** Returns the EventPointer or AddressPointer from a kind:9321 nutzap event */
|
|
36
|
+
export function getNutzapPointer(event) {
|
|
37
|
+
return getNutzapEventPointer(event) ?? getNutzapAddressPointer(event);
|
|
38
|
+
}
|
|
39
|
+
/** Returns the comment from a kind:9321 nutzap event */
|
|
40
|
+
export function getNutzapComment(event) {
|
|
41
|
+
return event.content || undefined;
|
|
42
|
+
}
|
|
43
|
+
/** Calculates the total amount of sats in a kind:9321 nutzap event */
|
|
44
|
+
export function getNutzapAmount(event) {
|
|
45
|
+
return getOrComputeCachedValue(event, NutzapAmountSymbol, () => {
|
|
46
|
+
const proofs = getNutzapProofs(event);
|
|
47
|
+
return proofs.reduce((total, proof) => total + (proof.amount || 0), 0);
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
/** Checks if a nutzap is valid according to NIP-61 requirements */
|
|
51
|
+
export function isValidNutzap(nutzap) {
|
|
52
|
+
const mint = getNutzapMint(nutzap);
|
|
53
|
+
const recipient = getNutzapRecipient(nutzap);
|
|
54
|
+
const proofs = getNutzapProofs(nutzap);
|
|
55
|
+
if (!mint || !recipient || proofs.length === 0)
|
|
56
|
+
return false;
|
|
57
|
+
return true;
|
|
58
|
+
}
|
|
59
|
+
/** Checks if a nutzap event has already been redeemed based on kind:7376 wallet history events */
|
|
60
|
+
export function isNutzapRedeemed(nutzapId, history) {
|
|
61
|
+
return history.some((entry) => getHistoryRedeemed(entry).includes(nutzapId));
|
|
62
|
+
}
|
package/dist/models/index.d.ts
CHANGED
package/dist/models/index.js
CHANGED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { Model } from "applesauce-core";
|
|
2
|
+
import { NostrEvent } from "nostr-tools";
|
|
3
|
+
/** A model that returns all nutzap events for an event */
|
|
4
|
+
export declare function EventNutZapzModel(event: NostrEvent): Model<NostrEvent[]>;
|
|
5
|
+
/** A model that returns all nutzaps for a users profile */
|
|
6
|
+
export declare function ProfileNutZapzModel(pubkey: string): Model<NostrEvent[]>;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { getReplaceableAddress, isReplaceable } from "applesauce-core/helpers";
|
|
2
|
+
import { map } from "rxjs";
|
|
3
|
+
import { getNutzapPointer, NUTZAP_KIND } from "../helpers/zaps.js";
|
|
4
|
+
/** A model that returns all nutzap events for an event */
|
|
5
|
+
export function EventNutZapzModel(event) {
|
|
6
|
+
return (events) => isReplaceable(event.kind)
|
|
7
|
+
? events.timeline({ kinds: [NUTZAP_KIND], "#e": [event.id] })
|
|
8
|
+
: events.timeline({ kinds: [NUTZAP_KIND], "#a": [getReplaceableAddress(event)] });
|
|
9
|
+
}
|
|
10
|
+
/** A model that returns all nutzaps for a users profile */
|
|
11
|
+
export function ProfileNutZapzModel(pubkey) {
|
|
12
|
+
return (events) => events
|
|
13
|
+
.timeline({ kinds: [NUTZAP_KIND], "#p": [pubkey] })
|
|
14
|
+
// filter out nutzaps that are for events
|
|
15
|
+
.pipe(map((zaps) => zaps.filter((zap) => getNutzapPointer(zap) === undefined)));
|
|
16
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { EventOperation } from "applesauce-factory";
|
|
2
|
+
/** Sets the relays for a nutzap info event */
|
|
3
|
+
export declare function setNutzapInfoRelays(relays: string[]): EventOperation;
|
|
4
|
+
/** Sets the mints for a nutzap info event */
|
|
5
|
+
export declare function setNutzapInfoMints(mints: Array<{
|
|
6
|
+
url: string;
|
|
7
|
+
units?: string[];
|
|
8
|
+
}>): EventOperation;
|
|
9
|
+
/** Sets the pubkey for a nutzap info event */
|
|
10
|
+
export declare function setNutzapInfoPubkey(pubkey: string): EventOperation;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { modifyPublicTags } from "applesauce-factory/operations/event";
|
|
2
|
+
import { addNameValueTag, setSingletonTag } from "applesauce-factory/operations/tag";
|
|
3
|
+
/** Sets the relays for a nutzap info event */
|
|
4
|
+
export function setNutzapInfoRelays(relays) {
|
|
5
|
+
return modifyPublicTags(...relays.map((relay) => addNameValueTag(["relay", relay], false)));
|
|
6
|
+
}
|
|
7
|
+
/** Sets the mints for a nutzap info event */
|
|
8
|
+
export function setNutzapInfoMints(mints) {
|
|
9
|
+
return modifyPublicTags(...mints.map((mint) => {
|
|
10
|
+
const tag = mint.units ? ["mint", mint.url, ...mint.units] : ["mint", mint.url];
|
|
11
|
+
return addNameValueTag(tag, false);
|
|
12
|
+
}));
|
|
13
|
+
}
|
|
14
|
+
/** Sets the pubkey for a nutzap info event */
|
|
15
|
+
export function setNutzapInfoPubkey(pubkey) {
|
|
16
|
+
return modifyPublicTags(setSingletonTag(["pubkey", pubkey], true));
|
|
17
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Proof } from "@cashu/cashu-ts";
|
|
2
|
+
import { EventOperation } from "applesauce-factory";
|
|
3
|
+
import { NostrEvent } from "nostr-tools";
|
|
4
|
+
import { AddressPointer, EventPointer, ProfilePointer } from "nostr-tools/nip19";
|
|
5
|
+
/** Sets the cashu proofs for a nutzap event */
|
|
6
|
+
export declare function setNutzapProofs(proofs: Proof[]): EventOperation;
|
|
7
|
+
/** Sets the mint URL for a nutzap event */
|
|
8
|
+
export declare function setNutzapMint(mint: string): EventOperation;
|
|
9
|
+
/** Sets the recipient of a nutzap event */
|
|
10
|
+
export declare function setNutzapRecipient(recipient: string | ProfilePointer): EventOperation;
|
|
11
|
+
/** Sets the event that is being nutzapped */
|
|
12
|
+
export declare function setNutzapEvent(event: EventPointer | AddressPointer | NostrEvent): EventOperation;
|
|
13
|
+
/** Sets the comment content for a nutzap event */
|
|
14
|
+
export declare function setNutzapComment(comment: string): EventOperation;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { getReplaceableAddress, isAddressPointer, isEvent, isReplaceable } from "applesauce-core/helpers";
|
|
2
|
+
import { modifyPublicTags } from "applesauce-factory/operations/event";
|
|
3
|
+
import { addAddressTag, addEventTag, addPubkeyTag, setSingletonTag } from "applesauce-factory/operations/tag";
|
|
4
|
+
/** Sets the cashu proofs for a nutzap event */
|
|
5
|
+
export function setNutzapProofs(proofs) {
|
|
6
|
+
// Create an operation to append proof tags
|
|
7
|
+
const operation = (tags) => [...tags, ...proofs.map((proof) => ["proof", JSON.stringify(proof)])];
|
|
8
|
+
return modifyPublicTags(operation);
|
|
9
|
+
}
|
|
10
|
+
/** Sets the mint URL for a nutzap event */
|
|
11
|
+
export function setNutzapMint(mint) {
|
|
12
|
+
return modifyPublicTags(setSingletonTag(["u", mint]));
|
|
13
|
+
}
|
|
14
|
+
/** Sets the recipient of a nutzap event */
|
|
15
|
+
export function setNutzapRecipient(recipient) {
|
|
16
|
+
return modifyPublicTags(addPubkeyTag(recipient, true));
|
|
17
|
+
}
|
|
18
|
+
/** Sets the event that is being nutzapped */
|
|
19
|
+
export function setNutzapEvent(event) {
|
|
20
|
+
let operation;
|
|
21
|
+
if (isEvent(event))
|
|
22
|
+
operation = isReplaceable(event.kind) ? addEventTag(event.id) : addAddressTag(getReplaceableAddress(event));
|
|
23
|
+
else if (isAddressPointer(event))
|
|
24
|
+
operation = addAddressTag(event);
|
|
25
|
+
else
|
|
26
|
+
operation = addEventTag(event);
|
|
27
|
+
return modifyPublicTags(operation);
|
|
28
|
+
}
|
|
29
|
+
/** Sets the comment content for a nutzap event */
|
|
30
|
+
export function setNutzapComment(comment) {
|
|
31
|
+
return (draft) => ({ ...draft, content: comment });
|
|
32
|
+
}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export * as TagOperations from "./tag/index.js";
|
|
2
|
-
export * as EventOperations from "./event/
|
|
2
|
+
export * as EventOperations from "./event/index.js";
|
package/dist/operations/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export * as TagOperations from "./tag/index.js";
|
|
2
|
-
export * as EventOperations from "./event/
|
|
2
|
+
export * as EventOperations from "./event/index.js";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "applesauce-wallet",
|
|
3
|
-
"version": "0.0.0-next-
|
|
3
|
+
"version": "0.0.0-next-20250718170433",
|
|
4
4
|
"description": "",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -80,16 +80,16 @@
|
|
|
80
80
|
"@cashu/cashu-ts": "2.0.0-rc1",
|
|
81
81
|
"@gandlaf21/bc-ur": "^1.1.12",
|
|
82
82
|
"@noble/hashes": "^1.7.1",
|
|
83
|
-
"applesauce-actions": "0.0.0-next-
|
|
84
|
-
"applesauce-core": "
|
|
85
|
-
"applesauce-factory": "0.0.0-next-
|
|
83
|
+
"applesauce-actions": "0.0.0-next-20250718170433",
|
|
84
|
+
"applesauce-core": "^2.0.0",
|
|
85
|
+
"applesauce-factory": "0.0.0-next-20250718170433",
|
|
86
86
|
"nostr-tools": "^2.13",
|
|
87
87
|
"rxjs": "^7.8.1"
|
|
88
88
|
},
|
|
89
89
|
"devDependencies": {
|
|
90
90
|
"@hirez_io/observer-spy": "^2.2.0",
|
|
91
91
|
"@types/debug": "^4.1.12",
|
|
92
|
-
"applesauce-signers": "0.0.0-next-
|
|
92
|
+
"applesauce-signers": "0.0.0-next-20250718170433",
|
|
93
93
|
"typescript": "^5.8.3",
|
|
94
94
|
"vitest": "^3.2.3"
|
|
95
95
|
},
|