applesauce-wallet 1.0.0 → 3.0.0

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.
Files changed (82) hide show
  1. package/README.md +1 -1
  2. package/dist/actions/index.d.ts +2 -0
  3. package/dist/actions/index.js +2 -0
  4. package/dist/actions/tokens.js +1 -1
  5. package/dist/actions/zap-info.d.ts +22 -0
  6. package/dist/actions/zap-info.js +83 -0
  7. package/dist/actions/zaps.d.ts +8 -0
  8. package/dist/actions/zaps.js +30 -0
  9. package/dist/blueprints/history.d.ts +1 -2
  10. package/dist/blueprints/history.js +3 -3
  11. package/dist/blueprints/index.d.ts +3 -1
  12. package/dist/blueprints/index.js +3 -1
  13. package/dist/blueprints/tokens.d.ts +1 -2
  14. package/dist/blueprints/tokens.js +3 -3
  15. package/dist/blueprints/wallet.d.ts +2 -3
  16. package/dist/blueprints/wallet.js +4 -6
  17. package/dist/blueprints/zaps.d.ts +8 -0
  18. package/dist/blueprints/zaps.js +12 -0
  19. package/dist/helpers/history.js +3 -1
  20. package/dist/helpers/index.d.ts +5 -3
  21. package/dist/helpers/index.js +5 -3
  22. package/dist/helpers/nutzap.d.ts +27 -0
  23. package/dist/helpers/nutzap.js +66 -0
  24. package/dist/helpers/tokens.d.ts +1 -1
  25. package/dist/helpers/tokens.js +4 -2
  26. package/dist/helpers/wallet.js +4 -1
  27. package/dist/helpers/zap-info.d.ts +19 -0
  28. package/dist/helpers/zap-info.js +42 -0
  29. package/dist/index.d.ts +2 -2
  30. package/dist/index.js +2 -2
  31. package/dist/models/history.d.ts +6 -0
  32. package/dist/{queries → models}/history.js +5 -5
  33. package/dist/{operations/event → models}/index.d.ts +3 -2
  34. package/dist/{operations/event → models}/index.js +3 -2
  35. package/dist/models/nutzap.d.ts +6 -0
  36. package/dist/models/nutzap.js +16 -0
  37. package/dist/models/tokens.d.ts +6 -0
  38. package/dist/{queries → models}/tokens.js +6 -6
  39. package/dist/{queries → models}/wallet.d.ts +3 -3
  40. package/dist/{queries → models}/wallet.js +3 -3
  41. package/dist/operations/{event/history.d.ts → history.d.ts} +2 -2
  42. package/dist/operations/history.js +34 -0
  43. package/dist/operations/index.d.ts +5 -2
  44. package/dist/operations/index.js +5 -2
  45. package/dist/operations/nutzap.d.ts +14 -0
  46. package/dist/operations/nutzap.js +33 -0
  47. package/dist/operations/{event/tokens.d.ts → tokens.d.ts} +1 -1
  48. package/dist/operations/{event/tokens.js → tokens.js} +2 -2
  49. package/dist/operations/wallet.d.ts +8 -0
  50. package/dist/operations/wallet.js +30 -0
  51. package/dist/operations/zap-info.d.ts +10 -0
  52. package/dist/operations/zap-info.js +17 -0
  53. package/package.json +19 -24
  54. package/dist/__tests__/fake-user.d.ts +0 -10
  55. package/dist/__tests__/fake-user.js +0 -31
  56. package/dist/actions/__tests__/tokens.test.d.ts +0 -1
  57. package/dist/actions/__tests__/tokens.test.js +0 -139
  58. package/dist/actions/__tests__/wallet.test.d.ts +0 -1
  59. package/dist/actions/__tests__/wallet.test.js +0 -56
  60. package/dist/helpers/__tests__/animated-qr.test.d.ts +0 -1
  61. package/dist/helpers/__tests__/animated-qr.test.js +0 -44
  62. package/dist/helpers/__tests__/tokens.test.d.ts +0 -1
  63. package/dist/helpers/__tests__/tokens.test.js +0 -127
  64. package/dist/operations/event/__tests__/wallet.test.d.ts +0 -1
  65. package/dist/operations/event/__tests__/wallet.test.js +0 -25
  66. package/dist/operations/event/history.js +0 -19
  67. package/dist/operations/event/wallet.d.ts +0 -4
  68. package/dist/operations/event/wallet.js +0 -14
  69. package/dist/operations/tag/__tests__/wallet.test.d.ts +0 -1
  70. package/dist/operations/tag/__tests__/wallet.test.js +0 -16
  71. package/dist/operations/tag/history.d.ts +0 -14
  72. package/dist/operations/tag/history.js +0 -34
  73. package/dist/operations/tag/index.d.ts +0 -2
  74. package/dist/operations/tag/index.js +0 -2
  75. package/dist/operations/tag/wallet.d.ts +0 -5
  76. package/dist/operations/tag/wallet.js +0 -15
  77. package/dist/queries/__tests__/wallet.test.d.ts +0 -1
  78. package/dist/queries/__tests__/wallet.test.js +0 -30
  79. package/dist/queries/history.d.ts +0 -6
  80. package/dist/queries/index.d.ts +0 -3
  81. package/dist/queries/index.js +0 -3
  82. package/dist/queries/tokens.d.ts +0 -6
package/README.md CHANGED
@@ -1,3 +1,3 @@
1
1
  # Applesauce Wallet
2
2
 
3
- The `applesauce-wallet` package is a package of helpers, queries, blueprints and other useful stuff for [NIP-60](https://github.com/nostr-protocol/nips/blob/master/60.md) wallets and [NIP-61](https://github.com/nostr-protocol/nips/blob/master/61.md) nutzaps
3
+ The `applesauce-wallet` package is a package of helpers, models, blueprints and other useful stuff for [NIP-60](https://github.com/nostr-protocol/nips/blob/master/60.md) wallets and [NIP-61](https://github.com/nostr-protocol/nips/blob/master/61.md) nutzaps
@@ -1,2 +1,4 @@
1
1
  export * from "./wallet.js";
2
2
  export * from "./tokens.js";
3
+ export * from "./zaps.js";
4
+ export * from "./zap-info.js";
@@ -1,2 +1,4 @@
1
1
  export * from "./wallet.js";
2
2
  export * from "./tokens.js";
3
+ export * from "./zaps.js";
4
+ export * from "./zap-info.js";
@@ -66,7 +66,7 @@ export function CompleteSpend(spent, change) {
66
66
  /** Combines all unlocked token events into a single event per mint */
67
67
  export function ConsolidateTokens(opts) {
68
68
  return async function* ({ events, factory, self }) {
69
- const tokens = Array.from(events.getAll({ kinds: [WALLET_TOKEN_KIND], authors: [self] })).filter((token) => {
69
+ const tokens = Array.from(events.getByFilters({ kinds: [WALLET_TOKEN_KIND], authors: [self] })).filter((token) => {
70
70
  if (isTokenContentLocked(token)) {
71
71
  if (opts?.ignoreLocked)
72
72
  return false;
@@ -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";
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/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
+ }
@@ -1,5 +1,4 @@
1
1
  import { EventPointer } from "nostr-tools/nip19";
2
2
  import { HistoryContent } from "../helpers/history.js";
3
- import { EventBlueprint } from "applesauce-factory";
4
3
  /** A blueprint that creates a wallet history event */
5
- export declare function WalletHistoryBlueprint(content: HistoryContent, redeemed: (string | EventPointer)[]): EventBlueprint;
4
+ export declare function WalletHistoryBlueprint(content: HistoryContent, redeemed: (string | EventPointer)[]): import("applesauce-factory").EventBlueprint;
@@ -1,9 +1,9 @@
1
+ import { blueprint } from "applesauce-factory";
1
2
  import { WALLET_HISTORY_KIND } from "../helpers/history.js";
2
- import { EventFactory } from "applesauce-factory";
3
- import { setHistoryContent, setHistoryRedeemed } from "../operations/event/history.js";
3
+ import { setHistoryContent, setHistoryRedeemed } from "../operations/history.js";
4
4
  /** A blueprint that creates a wallet history event */
5
5
  export function WalletHistoryBlueprint(content, redeemed) {
6
- return (ctx) => EventFactory.runProcess({ kind: WALLET_HISTORY_KIND }, ctx,
6
+ return blueprint(WALLET_HISTORY_KIND,
7
7
  // set the encrypted tags on the event
8
8
  setHistoryContent(content),
9
9
  // set the public redeemed tags
@@ -1,2 +1,4 @@
1
- export * from "./wallet.js";
1
+ export * from "./history.js";
2
2
  export * from "./tokens.js";
3
+ export * from "./wallet.js";
4
+ export * from "./zaps.js";
@@ -1,2 +1,4 @@
1
- export * from "./wallet.js";
1
+ export * from "./history.js";
2
2
  export * from "./tokens.js";
3
+ export * from "./wallet.js";
4
+ export * from "./zaps.js";
@@ -1,8 +1,7 @@
1
1
  import { Token } from "@cashu/cashu-ts";
2
- import { EventBlueprint } from "applesauce-factory";
3
2
  /**
4
3
  * A blueprint for a wallet token event, takes a cashu token and previous deleted token event ids
5
4
  * @param token the cashu token to store
6
5
  * @param [del=[]] an array of previous token event ids that are deleted
7
6
  */
8
- export declare function WalletTokenBlueprint(token: Token, del?: string[]): EventBlueprint;
7
+ export declare function WalletTokenBlueprint(token: Token, del?: string[]): import("applesauce-factory").EventBlueprint;
@@ -1,11 +1,11 @@
1
- import { EventFactory } from "applesauce-factory";
1
+ import { blueprint } from "applesauce-factory";
2
2
  import { WALLET_TOKEN_KIND } from "../helpers/tokens.js";
3
- import { setTokenContent } from "../operations/event/tokens.js";
3
+ import { setToken } from "../operations/tokens.js";
4
4
  /**
5
5
  * A blueprint for a wallet token event, takes a cashu token and previous deleted token event ids
6
6
  * @param token the cashu token to store
7
7
  * @param [del=[]] an array of previous token event ids that are deleted
8
8
  */
9
9
  export function WalletTokenBlueprint(token, del = []) {
10
- return (ctx) => EventFactory.runProcess({ kind: WALLET_TOKEN_KIND }, ctx, setTokenContent(token, del));
10
+ return blueprint(WALLET_TOKEN_KIND, setToken(token, del));
11
11
  }
@@ -1,6 +1,5 @@
1
- import { EventBlueprint } from "applesauce-factory";
2
1
  import { NostrEvent } from "nostr-tools";
3
2
  /** A blueprint to create a new 17375 wallet */
4
- export declare function WalletBlueprint(mints: string[], privateKey?: Uint8Array): EventBlueprint;
3
+ export declare function WalletBlueprint(mints: string[], privateKey?: Uint8Array): import("applesauce-factory").EventBlueprint;
5
4
  /** A blueprint that creates a new 375 wallet backup event */
6
- export declare function WalletBackupBlueprint(wallet: NostrEvent): EventBlueprint;
5
+ export declare function WalletBackupBlueprint(wallet: NostrEvent): import("applesauce-factory").EventBlueprint;
@@ -1,13 +1,11 @@
1
- import { EventFactory } from "applesauce-factory";
2
- import { modifyHiddenTags } from "applesauce-factory/operations/event";
1
+ import { blueprint } from "applesauce-factory";
3
2
  import { WALLET_BACKUP_KIND, WALLET_KIND } from "../helpers/wallet.js";
4
- import { setWalletBackupContent } from "../operations/event/wallet.js";
5
- import { setMintTags, setPrivateKeyTag } from "../operations/tag/wallet.js";
3
+ import { setBackupContent, setMints, setPrivateKey } from "../operations/wallet.js";
6
4
  /** A blueprint to create a new 17375 wallet */
7
5
  export function WalletBlueprint(mints, privateKey) {
8
- return (ctx) => EventFactory.runProcess({ kind: WALLET_KIND }, ctx, modifyHiddenTags(privateKey ? setPrivateKeyTag(privateKey) : undefined, setMintTags(mints)));
6
+ return blueprint(WALLET_KIND, setMints(mints), privateKey ? setPrivateKey(privateKey) : undefined);
9
7
  }
10
8
  /** A blueprint that creates a new 375 wallet backup event */
11
9
  export function WalletBackupBlueprint(wallet) {
12
- return (ctx) => EventFactory.runProcess({ kind: WALLET_BACKUP_KIND }, ctx, setWalletBackupContent(wallet));
10
+ return blueprint(WALLET_BACKUP_KIND, setBackupContent(wallet));
13
11
  }
@@ -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/nutzap.js";
4
+ import { setComment, setEvent, setMint, setProofs, setRecipient } from "../operations/nutzap.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, setProofs(token.proofs), setMint(token.mint), setEvent(event), setRecipient(event.pubkey), comment ? setComment(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, setProofs(token.proofs), setMint(token.mint), setRecipient(user), comment ? setComment(comment) : skip());
12
+ }
@@ -1,5 +1,7 @@
1
- import { getHiddenTags, getOrComputeCachedValue, isETag, isHiddenContentLocked, isHiddenTagsLocked, lockHiddenTags, unlockHiddenTags, } from "applesauce-core/helpers";
1
+ import { getHiddenTags, getOrComputeCachedValue, isETag, isHiddenContentLocked, isHiddenTagsLocked, lockHiddenTags, setHiddenTagsEncryptionMethod, unlockHiddenTags, } from "applesauce-core/helpers";
2
2
  export const WALLET_HISTORY_KIND = 7376;
3
+ // Enable hidden content for wallet history kind
4
+ setHiddenTagsEncryptionMethod(WALLET_HISTORY_KIND, "nip44");
3
5
  export const HistoryContentSymbol = Symbol.for("history-content");
4
6
  /** returns an array of redeemed event ids in a history event */
5
7
  export function getHistoryRedeemed(history) {
@@ -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 "./nutzap.js";
@@ -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 "./nutzap.js";
@@ -0,0 +1,27 @@
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
+ export declare const NutzapMintSymbol: unique symbol;
8
+ /** Returns the cashu proofs from a kind:9321 nutzap event */
9
+ export declare function getNutzapProofs(event: NostrEvent): Proof[];
10
+ /** Returns the mint URL from a kind:9321 nutzap event */
11
+ export declare function getNutzapMint(event: NostrEvent): string | undefined;
12
+ /** Returns the recipient pubkey from a kind:9321 nutzap event */
13
+ export declare function getNutzapRecipient(event: NostrEvent): string | undefined;
14
+ /** Returns the event ID being nutzapped from a kind:9321 nutzap event */
15
+ export declare function getNutzapEventPointer(event: NostrEvent): EventPointer | undefined;
16
+ /** Returns the event ID being nutzapped from a kind:9321 nutzap event */
17
+ export declare function getNutzapAddressPointer(event: NostrEvent): AddressPointer | undefined;
18
+ /** Returns the EventPointer or AddressPointer from a kind:9321 nutzap event */
19
+ export declare function getNutzapPointer(event: NostrEvent): EventPointer | AddressPointer | undefined;
20
+ /** Returns the comment from a kind:9321 nutzap event */
21
+ export declare function getNutzapComment(event: NostrEvent): string | undefined;
22
+ /** Calculates the total amount of sats in a kind:9321 nutzap event */
23
+ export declare function getNutzapAmount(event: NostrEvent): number;
24
+ /** Checks if a nutzap is valid according to NIP-61 requirements */
25
+ export declare function isValidNutzap(nutzap: NostrEvent): boolean;
26
+ /** Checks if a nutzap event has already been redeemed based on kind:7376 wallet history events */
27
+ export declare function isNutzapRedeemed(nutzapId: string, history: NostrEvent[]): boolean;
@@ -0,0 +1,66 @@
1
+ import { getAddressPointerFromATag, getEventPointerFromETag, getOrComputeCachedValue, getTagValue, 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
+ export const NutzapMintSymbol = Symbol.for("nutzap-mint");
8
+ /** Returns the cashu proofs from a kind:9321 nutzap event */
9
+ export function getNutzapProofs(event) {
10
+ return getOrComputeCachedValue(event, NutzapProofsSymbol, () => {
11
+ return processTags(event.tags, (tag) => (tag[0] === "proof" ? safeParse(tag[1]) : undefined));
12
+ });
13
+ }
14
+ /** Returns the mint URL from a kind:9321 nutzap event */
15
+ export function getNutzapMint(event) {
16
+ return getOrComputeCachedValue(event, NutzapMintSymbol, () => {
17
+ const url = getTagValue(event, "u");
18
+ return url && URL.canParse(url) ? url : undefined;
19
+ });
20
+ }
21
+ /** Returns the recipient pubkey from a kind:9321 nutzap event */
22
+ export function getNutzapRecipient(event) {
23
+ return getTagValue(event, "p");
24
+ }
25
+ /** Returns the event ID being nutzapped from a kind:9321 nutzap event */
26
+ export function getNutzapEventPointer(event) {
27
+ const tag = event.tags.find((t) => t[0] === "e");
28
+ if (!tag)
29
+ return;
30
+ return getEventPointerFromETag(tag);
31
+ }
32
+ /** Returns the event ID being nutzapped from a kind:9321 nutzap event */
33
+ export function getNutzapAddressPointer(event) {
34
+ const tag = event.tags.find((t) => t[0] === "a");
35
+ if (!tag)
36
+ return;
37
+ return getAddressPointerFromATag(tag);
38
+ }
39
+ /** Returns the EventPointer or AddressPointer from a kind:9321 nutzap event */
40
+ export function getNutzapPointer(event) {
41
+ return getNutzapEventPointer(event) ?? getNutzapAddressPointer(event);
42
+ }
43
+ /** Returns the comment from a kind:9321 nutzap event */
44
+ export function getNutzapComment(event) {
45
+ return event.content || undefined;
46
+ }
47
+ /** Calculates the total amount of sats in a kind:9321 nutzap event */
48
+ export function getNutzapAmount(event) {
49
+ return getOrComputeCachedValue(event, NutzapAmountSymbol, () => {
50
+ const proofs = getNutzapProofs(event);
51
+ return proofs.reduce((total, proof) => total + (proof.amount || 0), 0);
52
+ });
53
+ }
54
+ /** Checks if a nutzap is valid according to NIP-61 requirements */
55
+ export function isValidNutzap(nutzap) {
56
+ const mint = getNutzapMint(nutzap);
57
+ const recipient = getNutzapRecipient(nutzap);
58
+ const proofs = getNutzapProofs(nutzap);
59
+ if (!mint || !recipient || proofs.length === 0)
60
+ return false;
61
+ return true;
62
+ }
63
+ /** Checks if a nutzap event has already been redeemed based on kind:7376 wallet history events */
64
+ export function isNutzapRedeemed(nutzapId, history) {
65
+ return history.some((entry) => getHistoryRedeemed(entry).includes(nutzapId));
66
+ }
@@ -1,11 +1,11 @@
1
1
  import { Proof, Token } from "@cashu/cashu-ts";
2
2
  import { HiddenContentSigner } from "applesauce-core/helpers";
3
3
  import { NostrEvent } from "nostr-tools";
4
+ export declare const WALLET_TOKEN_KIND = 7375;
4
5
  /** Internal method for creating a unique id for each proof */
5
6
  export declare function getProofUID(proof: Proof): string;
6
7
  /** Internal method to filter out duplicate proofs */
7
8
  export declare function ignoreDuplicateProofs(seen?: Set<string>): (proof: Proof) => boolean;
8
- export declare const WALLET_TOKEN_KIND = 7375;
9
9
  export type TokenContent = {
10
10
  /** Cashu mint for the proofs */
11
11
  mint: string;
@@ -1,5 +1,8 @@
1
1
  import { getDecodedToken, getEncodedToken } from "@cashu/cashu-ts";
2
- import { getHiddenContent, getOrComputeCachedValue, isHiddenContentLocked, isHiddenTagsLocked, lockHiddenContent, unlockHiddenContent, } from "applesauce-core/helpers";
2
+ import { getHiddenContent, getOrComputeCachedValue, isHiddenContentLocked, isHiddenTagsLocked, lockHiddenContent, setHiddenContentEncryptionMethod, unlockHiddenContent, } from "applesauce-core/helpers";
3
+ export const WALLET_TOKEN_KIND = 7375;
4
+ // Enable hidden content for wallet token kind
5
+ setHiddenContentEncryptionMethod(WALLET_TOKEN_KIND, "nip44");
3
6
  /** Internal method for creating a unique id for each proof */
4
7
  export function getProofUID(proof) {
5
8
  return proof.id + proof.amount + proof.C + proof.secret;
@@ -16,7 +19,6 @@ export function ignoreDuplicateProofs(seen = new Set()) {
16
19
  }
17
20
  };
18
21
  }
19
- export const WALLET_TOKEN_KIND = 7375;
20
22
  export const TokenContentSymbol = Symbol.for("token-content");
21
23
  /**
22
24
  * Returns the decrypted and parsed details of a 7375 token event
@@ -1,7 +1,10 @@
1
1
  import { hexToBytes } from "@noble/hashes/utils";
2
- import { getHiddenTags, getOrComputeCachedValue, isHiddenTagsLocked, lockHiddenTags, unlockHiddenTags, } from "applesauce-core/helpers";
2
+ import { getHiddenTags, getOrComputeCachedValue, isHiddenTagsLocked, lockHiddenTags, setHiddenTagsEncryptionMethod, unlockHiddenTags, } from "applesauce-core/helpers";
3
3
  export const WALLET_KIND = 17375;
4
4
  export const WALLET_BACKUP_KIND = 375;
5
+ // Enable hidden content for wallet kinds
6
+ setHiddenTagsEncryptionMethod(WALLET_KIND, "nip44");
7
+ setHiddenTagsEncryptionMethod(WALLET_BACKUP_KIND, "nip44");
5
8
  export const WalletPrivateKeySymbol = Symbol.for("wallet-private-key");
6
9
  export const WalletMintsSymbol = Symbol.for("wallet-mints");
7
10
  /** Returns if a wallet is locked */
@@ -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
+ }
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  export * as Actions from "./actions/index.js";
2
2
  export * as Blueprints from "./blueprints/index.js";
3
3
  export * as Helpers from "./helpers/index.js";
4
- export * as Queries from "./queries/index.js";
5
- export * from "./operations/index.js";
4
+ export * as Models from "./models/index.js";
5
+ export * as Operations from "./operations/index.js";
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  export * as Actions from "./actions/index.js";
2
2
  export * as Blueprints from "./blueprints/index.js";
3
3
  export * as Helpers from "./helpers/index.js";
4
- export * as Queries from "./queries/index.js";
5
- export * from "./operations/index.js";
4
+ export * as Models from "./models/index.js";
5
+ export * as Operations from "./operations/index.js";
@@ -0,0 +1,6 @@
1
+ import { Model } from "applesauce-core";
2
+ import { NostrEvent } from "nostr-tools";
3
+ /** A model that returns an array of redeemed event ids for a wallet */
4
+ export declare function WalletRedeemedModel(pubkey: string): Model<string[]>;
5
+ /** A model that returns a timeline of wallet history events */
6
+ export declare function WalletHistoryModel(pubkey: string, locked?: boolean | undefined): Model<NostrEvent[]>;
@@ -1,15 +1,15 @@
1
1
  import { combineLatest, filter, map, scan, startWith } from "rxjs";
2
2
  import { getHistoryRedeemed, isHistoryContentLocked, WALLET_HISTORY_KIND } from "../helpers/history.js";
3
- /** Query that returns an array of redeemed event ids for a wallet */
4
- export function WalletRedeemedQuery(pubkey) {
3
+ /** A model that returns an array of redeemed event ids for a wallet */
4
+ export function WalletRedeemedModel(pubkey) {
5
5
  return (events) => events
6
6
  .filters({ kinds: [WALLET_HISTORY_KIND], authors: [pubkey] })
7
7
  .pipe(scan((ids, history) => [...ids, ...getHistoryRedeemed(history)], []));
8
8
  }
9
- /** A query that returns a timeline of wallet history events */
10
- export function WalletHistoryQuery(pubkey, locked) {
9
+ /** A model that returns a timeline of wallet history events */
10
+ export function WalletHistoryModel(pubkey, locked) {
11
11
  return (events) => {
12
- const updates = events.updates.pipe(filter((e) => e.kind === WALLET_HISTORY_KIND && e.pubkey === pubkey), startWith(undefined));
12
+ const updates = events.update$.pipe(filter((e) => e.kind === WALLET_HISTORY_KIND && e.pubkey === pubkey), startWith(undefined));
13
13
  const timeline = events.timeline({ kinds: [WALLET_HISTORY_KIND], authors: [pubkey] });
14
14
  return combineLatest([updates, timeline]).pipe(map(([_, history]) => {
15
15
  if (locked === undefined)
@@ -1,3 +1,4 @@
1
- export * from "./wallet.js";
2
- export * from "./tokens.js";
3
1
  export * from "./history.js";
2
+ export * from "./tokens.js";
3
+ export * from "./wallet.js";
4
+ export * from "./nutzap.js";
@@ -1,3 +1,4 @@
1
- export * from "./wallet.js";
2
- export * from "./tokens.js";
3
1
  export * from "./history.js";
2
+ export * from "./tokens.js";
3
+ export * from "./wallet.js";
4
+ export * from "./nutzap.js";
@@ -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/nutzap.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,6 @@
1
+ import { Model } from "applesauce-core";
2
+ import { NostrEvent } from "nostr-tools";
3
+ /** A model that subscribes to all token events for a wallet, passing locked will filter by token locked status */
4
+ export declare function WalletTokensModel(pubkey: string, locked?: boolean | undefined): Model<NostrEvent[]>;
5
+ /** A model that returns the visible balance of a wallet for each mint */
6
+ export declare function WalletBalanceModel(pubkey: string): Model<Record<string, number>>;