applesauce-wallet 0.11.0 → 0.12.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 (68) hide show
  1. package/README.md +3 -0
  2. package/dist/__tests__/fake-user.d.ts +10 -0
  3. package/dist/__tests__/fake-user.js +31 -0
  4. package/dist/actions/__tests__/tokens.test.d.ts +1 -0
  5. package/dist/actions/__tests__/tokens.test.js +139 -0
  6. package/dist/actions/__tests__/wallet.test.d.ts +1 -0
  7. package/dist/actions/__tests__/wallet.test.js +56 -0
  8. package/dist/actions/index.d.ts +2 -0
  9. package/dist/actions/index.js +2 -0
  10. package/dist/actions/tokens.d.ts +17 -0
  11. package/dist/actions/tokens.js +110 -0
  12. package/dist/actions/wallet.d.ts +13 -0
  13. package/dist/actions/wallet.js +64 -0
  14. package/dist/blueprints/history.d.ts +5 -0
  15. package/dist/blueprints/history.js +11 -0
  16. package/dist/blueprints/index.d.ts +2 -0
  17. package/dist/blueprints/index.js +2 -0
  18. package/dist/blueprints/tokens.d.ts +8 -0
  19. package/dist/blueprints/tokens.js +11 -0
  20. package/dist/blueprints/wallet.d.ts +6 -0
  21. package/dist/blueprints/wallet.js +13 -0
  22. package/dist/helpers/__tests__/animated-qr.test.d.ts +1 -0
  23. package/dist/helpers/__tests__/animated-qr.test.js +44 -0
  24. package/dist/helpers/__tests__/tokens.test.d.ts +1 -0
  25. package/dist/helpers/__tests__/tokens.test.js +127 -0
  26. package/dist/helpers/animated-qr.d.ts +30 -0
  27. package/dist/helpers/animated-qr.js +71 -0
  28. package/dist/helpers/history.d.ts +26 -0
  29. package/dist/helpers/history.js +45 -0
  30. package/dist/helpers/index.d.ts +3 -0
  31. package/dist/helpers/index.js +3 -0
  32. package/dist/helpers/tokens.d.ts +58 -0
  33. package/dist/helpers/tokens.js +160 -0
  34. package/dist/helpers/wallet.d.ts +3 -1
  35. package/dist/helpers/wallet.js +9 -4
  36. package/dist/index.d.ts +4 -1
  37. package/dist/index.js +4 -1
  38. package/dist/operations/event/__tests__/wallet.test.d.ts +1 -0
  39. package/dist/operations/event/__tests__/wallet.test.js +25 -0
  40. package/dist/operations/event/history.d.ts +7 -0
  41. package/dist/operations/event/history.js +19 -0
  42. package/dist/operations/event/index.d.ts +3 -0
  43. package/dist/operations/event/index.js +3 -0
  44. package/dist/operations/event/tokens.d.ts +4 -0
  45. package/dist/operations/event/tokens.js +24 -0
  46. package/dist/operations/event/wallet.d.ts +4 -0
  47. package/dist/operations/event/wallet.js +14 -0
  48. package/dist/operations/index.d.ts +2 -0
  49. package/dist/operations/index.js +2 -0
  50. package/dist/operations/tag/__tests__/wallet.test.d.ts +1 -0
  51. package/dist/operations/tag/__tests__/wallet.test.js +16 -0
  52. package/dist/operations/tag/history.d.ts +14 -0
  53. package/dist/operations/tag/history.js +34 -0
  54. package/dist/operations/tag/index.d.ts +2 -0
  55. package/dist/operations/tag/index.js +2 -0
  56. package/dist/operations/tag/wallet.d.ts +5 -0
  57. package/dist/operations/tag/wallet.js +15 -0
  58. package/dist/queries/__tests__/wallet.test.d.ts +1 -0
  59. package/dist/queries/__tests__/wallet.test.js +30 -0
  60. package/dist/queries/history.d.ts +6 -0
  61. package/dist/queries/history.js +27 -0
  62. package/dist/queries/index.d.ts +2 -0
  63. package/dist/queries/index.js +2 -0
  64. package/dist/queries/tokens.d.ts +6 -0
  65. package/dist/queries/tokens.js +64 -0
  66. package/dist/queries/wallet.d.ts +4 -1
  67. package/dist/queries/wallet.js +3 -3
  68. package/package.json +43 -2
@@ -0,0 +1,3 @@
1
+ export * from "./wallet.js";
2
+ export * from "./tokens.js";
3
+ export * from "./history.js";
@@ -0,0 +1,3 @@
1
+ export * from "./wallet.js";
2
+ export * from "./tokens.js";
3
+ export * from "./history.js";
@@ -0,0 +1,4 @@
1
+ import { Token } from "@cashu/cashu-ts";
2
+ import { EventOperation } from "applesauce-factory";
3
+ /** Sets the content of a 7375 token event */
4
+ export declare function setTokenContent(token: Token, del?: string[]): EventOperation;
@@ -0,0 +1,24 @@
1
+ import { EventContentEncryptionMethod } from "applesauce-core/helpers";
2
+ import { setEncryptedContent } from "applesauce-factory/operations/event";
3
+ /** Sets the content of a 7375 token event */
4
+ export function setTokenContent(token, del = []) {
5
+ return async (draft, ctx) => {
6
+ if (!ctx.signer)
7
+ throw new Error(`Missing signer`);
8
+ const pubkey = await ctx.signer.getPublicKey();
9
+ const method = EventContentEncryptionMethod[draft.kind];
10
+ if (!method)
11
+ throw new Error("Failed to find encryption method");
12
+ if (!token.mint)
13
+ throw new Error("Token mint is required");
14
+ if (!token.proofs || token.proofs.length === 0)
15
+ throw new Error("Token proofs are required");
16
+ const content = {
17
+ mint: token.mint,
18
+ proofs: token.proofs,
19
+ unit: token.unit,
20
+ del,
21
+ };
22
+ return await setEncryptedContent(pubkey, JSON.stringify(content), method)(draft, ctx);
23
+ };
24
+ }
@@ -0,0 +1,4 @@
1
+ import { EventOperation } from "applesauce-factory";
2
+ import { NostrEvent } from "nostr-tools";
3
+ /** Sets the content of a kind 375 wallet backup event */
4
+ export declare function setWalletBackupContent(wallet: NostrEvent): EventOperation;
@@ -0,0 +1,14 @@
1
+ import { WALLET_KIND } from "../../helpers/wallet.js";
2
+ /** Sets the content of a kind 375 wallet backup event */
3
+ export function setWalletBackupContent(wallet) {
4
+ return async (draft, ctx) => {
5
+ if (wallet.kind !== WALLET_KIND)
6
+ throw new Error(`Cant create a wallet backup from kind ${wallet.kind}`);
7
+ if (!wallet.content)
8
+ throw new Error("Wallet missing content");
9
+ const pubkey = await ctx.signer?.getPublicKey();
10
+ if (wallet.pubkey !== pubkey)
11
+ throw new Error("Wallet pubkey dose not match signer pubkey");
12
+ return { ...draft, content: wallet.content };
13
+ };
14
+ }
@@ -0,0 +1,2 @@
1
+ export * as TagOperations from "./tag/index.js";
2
+ export * as EventOperations from "./event/wallet.js";
@@ -0,0 +1,2 @@
1
+ export * as TagOperations from "./tag/index.js";
2
+ export * as EventOperations from "./event/wallet.js";
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,16 @@
1
+ import { describe, it, expect } from "vitest";
2
+ import { setMintTags } from "../wallet.js";
3
+ describe("setMintTags", () => {
4
+ it("should replace existing mint tags", () => {
5
+ expect(setMintTags(["https://mint.com"])([["mint", "https://other.mint.com"]], {})).toEqual(expect.arrayContaining([["mint", "https://mint.com"]]));
6
+ });
7
+ it("should ignore other tags", () => {
8
+ expect(setMintTags(["https://mint.com"])([
9
+ ["mint", "https://other.mint.com"],
10
+ ["privkey", "00000000"],
11
+ ], {})).toEqual(expect.arrayContaining([
12
+ ["mint", "https://mint.com"],
13
+ ["privkey", "00000000"],
14
+ ]));
15
+ });
16
+ });
@@ -0,0 +1,14 @@
1
+ import { TagOperation } from "applesauce-factory";
2
+ import { EventPointer } from "nostr-tools/nip19";
3
+ import { HistoryDirection } from "../../helpers/history.js";
4
+ /** Sets the "direction" tag on wallet history tags */
5
+ export declare function setHistoryDirectionTag(direction: HistoryDirection): TagOperation;
6
+ /** Sets the "amount" tag on wallet history tags */
7
+ export declare function setHistoryAmountTag(amount: number): TagOperation;
8
+ /** Sets the "fee" tag in wallet history tags */
9
+ export declare function setHistoryFeeTag(fee: number): TagOperation;
10
+ export declare function setHistoryMintTag(mint: string): TagOperation;
11
+ /** Includes "created" "e" tags in wallet history tags */
12
+ export declare function includeHistoryCreatedTags(created: (string | EventPointer)[]): TagOperation;
13
+ /** Includes the "redeemed" tags in wallet history tags */
14
+ export declare function includeHistoryRedeemedTags(redeemed: (string | EventPointer)[]): TagOperation;
@@ -0,0 +1,34 @@
1
+ import { ensureMarkedEventPointerTag, ensureSingletonTag } from "applesauce-factory/helpers";
2
+ /** Sets the "direction" tag on wallet history tags */
3
+ export function setHistoryDirectionTag(direction) {
4
+ return (tags) => ensureSingletonTag(tags, ["direction", direction], true);
5
+ }
6
+ /** Sets the "amount" tag on wallet history tags */
7
+ export function setHistoryAmountTag(amount) {
8
+ return (tags) => ensureSingletonTag(tags, ["amount", String(amount)], true);
9
+ }
10
+ /** Sets the "fee" tag in wallet history tags */
11
+ export function setHistoryFeeTag(fee) {
12
+ return (tags) => ensureSingletonTag(tags, ["fee", String(fee)], true);
13
+ }
14
+ export function setHistoryMintTag(mint) {
15
+ return (tags) => ensureSingletonTag(tags, ["mint", mint], true);
16
+ }
17
+ /** Includes "created" "e" tags in wallet history tags */
18
+ export function includeHistoryCreatedTags(created) {
19
+ return (tags) => {
20
+ for (const id of created) {
21
+ tags = ensureMarkedEventPointerTag(tags, typeof id === "string" ? { id } : id, "created");
22
+ }
23
+ return tags;
24
+ };
25
+ }
26
+ /** Includes the "redeemed" tags in wallet history tags */
27
+ export function includeHistoryRedeemedTags(redeemed) {
28
+ return (tags) => {
29
+ for (const id of redeemed) {
30
+ tags = ensureMarkedEventPointerTag(tags, typeof id === "string" ? { id } : id, "redeemed");
31
+ }
32
+ return tags;
33
+ };
34
+ }
@@ -0,0 +1,2 @@
1
+ export * from "./wallet.js";
2
+ export * from "./history.js";
@@ -0,0 +1,2 @@
1
+ export * from "./wallet.js";
2
+ export * from "./history.js";
@@ -0,0 +1,5 @@
1
+ import { TagOperation } from "applesauce-factory";
2
+ /** Sets the "mint" tags in a wallet event */
3
+ export declare function setMintTags(mints: string[]): TagOperation;
4
+ /** Sets the "privkey" tag on a wallet event */
5
+ export declare function setPrivateKeyTag(privateKey: Uint8Array): TagOperation;
@@ -0,0 +1,15 @@
1
+ import { bytesToHex } from "@noble/hashes/utils";
2
+ import { ensureSingletonTag } from "applesauce-factory/helpers";
3
+ /** Sets the "mint" tags in a wallet event */
4
+ export function setMintTags(mints) {
5
+ return (tags) => [
6
+ // remove all existing mint tags
7
+ ...tags.filter((t) => t[0] !== "mint"),
8
+ // add new mint tags
9
+ ...mints.map((mint) => ["mint", mint]),
10
+ ];
11
+ }
12
+ /** Sets the "privkey" tag on a wallet event */
13
+ export function setPrivateKeyTag(privateKey) {
14
+ return (tags) => ensureSingletonTag(tags, ["privkey", bytesToHex(privateKey)], true);
15
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,30 @@
1
+ import { EventStore, QueryStore } from "applesauce-core";
2
+ import { beforeEach, describe, expect, it } from "vitest";
3
+ import { EventFactory } from "applesauce-factory";
4
+ import { generateSecretKey } from "nostr-tools";
5
+ import { subscribeSpyTo } from "@hirez_io/observer-spy";
6
+ import { FakeUser } from "../../__tests__/fake-user.js";
7
+ import { WalletBlueprint } from "../../blueprints/wallet.js";
8
+ import { WalletQuery } from "../wallet.js";
9
+ import { lockWallet, unlockWallet } from "../../helpers/wallet.js";
10
+ const user = new FakeUser();
11
+ const factory = new EventFactory({ signer: user });
12
+ let events;
13
+ let queries;
14
+ beforeEach(() => {
15
+ events = new EventStore();
16
+ queries = new QueryStore(events);
17
+ });
18
+ describe("WalletQuery", () => {
19
+ it("it should update when event is unlocked", async () => {
20
+ const wallet = await user.signEvent(await factory.create(WalletBlueprint, [], generateSecretKey()));
21
+ lockWallet(wallet);
22
+ events.add(wallet);
23
+ const spy = subscribeSpyTo(queries.createQuery(WalletQuery, await user.getPublicKey()));
24
+ await unlockWallet(wallet, user);
25
+ expect(spy.getValues()).toEqual([
26
+ expect.objectContaining({ locked: true }),
27
+ expect.objectContaining({ locked: false }),
28
+ ]);
29
+ });
30
+ });
@@ -0,0 +1,6 @@
1
+ import { Query } from "applesauce-core";
2
+ import { NostrEvent } from "nostr-tools";
3
+ /** Query that returns an array of redeemed event ids for a wallet */
4
+ export declare function WalletRedeemedQuery(pubkey: string): Query<string[]>;
5
+ /** A query that returns a timeline of wallet history events */
6
+ export declare function WalletHistoryQuery(pubkey: string, locked?: boolean | undefined): Query<NostrEvent[]>;
@@ -0,0 +1,27 @@
1
+ import { combineLatest, filter, map, scan, startWith } from "rxjs";
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) {
5
+ return {
6
+ key: pubkey,
7
+ run: (events) => events
8
+ .filters({ kinds: [WALLET_HISTORY_KIND], authors: [pubkey] })
9
+ .pipe(scan((ids, history) => [...ids, ...getHistoryRedeemed(history)], [])),
10
+ };
11
+ }
12
+ /** A query that returns a timeline of wallet history events */
13
+ export function WalletHistoryQuery(pubkey, locked) {
14
+ return {
15
+ key: pubkey,
16
+ run: (events) => {
17
+ const updates = events.updates.pipe(filter((e) => e.kind === WALLET_HISTORY_KIND && e.pubkey === pubkey), startWith(undefined));
18
+ const timeline = events.timeline({ kinds: [WALLET_HISTORY_KIND], authors: [pubkey] });
19
+ return combineLatest([updates, timeline]).pipe(map(([_, history]) => {
20
+ if (locked === undefined)
21
+ return history;
22
+ else
23
+ return history.filter((entry) => isHistoryContentLocked(entry) === locked);
24
+ }));
25
+ },
26
+ };
27
+ }
@@ -1 +1,3 @@
1
1
  export * from "./wallet.js";
2
+ export * from "./history.js";
3
+ export * from "./tokens.js";
@@ -1 +1,3 @@
1
1
  export * from "./wallet.js";
2
+ export * from "./history.js";
3
+ export * from "./tokens.js";
@@ -0,0 +1,6 @@
1
+ import { Query } from "applesauce-core";
2
+ import { NostrEvent } from "nostr-tools";
3
+ /** A query that subscribes to all token events for a wallet, passing locked will filter by token locked status */
4
+ export declare function WalletTokensQuery(pubkey: string, locked?: boolean | undefined): Query<NostrEvent[]>;
5
+ /** A query that returns the visible balance of a wallet for each mint */
6
+ export declare function WalletBalanceQuery(pubkey: string): Query<Record<string, number>>;
@@ -0,0 +1,64 @@
1
+ import { combineLatest, filter, map, startWith } from "rxjs";
2
+ import { getTokenContent, ignoreDuplicateProofs, isTokenContentLocked, WALLET_TOKEN_KIND } from "../helpers/tokens.js";
3
+ /** removes deleted events from sorted array */
4
+ function filterDeleted(tokens) {
5
+ const deleted = new Set();
6
+ return Array.from(tokens)
7
+ .reverse()
8
+ .filter((token) => {
9
+ // skip this event if it a newer event says its deleted
10
+ if (deleted.has(token.id))
11
+ return false;
12
+ // add ids to deleted array
13
+ if (!isTokenContentLocked(token)) {
14
+ const details = getTokenContent(token);
15
+ for (const id of details.del)
16
+ deleted.add(id);
17
+ }
18
+ return true;
19
+ })
20
+ .reverse();
21
+ }
22
+ /** A query that subscribes to all token events for a wallet, passing locked will filter by token locked status */
23
+ export function WalletTokensQuery(pubkey, locked) {
24
+ return {
25
+ key: pubkey + locked,
26
+ run: (events) => {
27
+ const updates = events.updates.pipe(filter((e) => e.kind === WALLET_TOKEN_KIND && e.pubkey === pubkey), startWith(undefined));
28
+ const timeline = events.timeline({ kinds: [WALLET_TOKEN_KIND], authors: [pubkey] });
29
+ return combineLatest([updates, timeline]).pipe(
30
+ // filter out locked tokens
31
+ map(([_, tokens]) => {
32
+ if (locked === undefined)
33
+ return tokens;
34
+ else
35
+ return tokens.filter((t) => isTokenContentLocked(t) === locked);
36
+ }),
37
+ // remove deleted events
38
+ map(filterDeleted));
39
+ },
40
+ };
41
+ }
42
+ /** A query that returns the visible balance of a wallet for each mint */
43
+ export function WalletBalanceQuery(pubkey) {
44
+ return {
45
+ key: pubkey,
46
+ run: (events) => {
47
+ const updates = events.updates.pipe(filter((e) => e.kind === WALLET_TOKEN_KIND && e.pubkey === pubkey), startWith(undefined));
48
+ const timeline = events.timeline({ kinds: [WALLET_TOKEN_KIND], authors: [pubkey] });
49
+ return combineLatest([updates, timeline]).pipe(map(([_, tokens]) => tokens.filter((t) => !isTokenContentLocked(t))),
50
+ // filter out deleted tokens
51
+ map(filterDeleted),
52
+ // map tokens to totals
53
+ map((tokens) => {
54
+ // ignore duplicate proofs
55
+ const seen = new Set();
56
+ return tokens.reduce((totals, token) => {
57
+ const details = getTokenContent(token);
58
+ const total = details.proofs.filter(ignoreDuplicateProofs(seen)).reduce((t, p) => t + p.amount, 0);
59
+ return { ...totals, [details.mint]: (totals[details.mint] ?? 0) + total };
60
+ }, {});
61
+ }));
62
+ },
63
+ };
64
+ }
@@ -1,9 +1,12 @@
1
1
  import { Query } from "applesauce-core";
2
+ import { NostrEvent } from "nostr-tools";
2
3
  export type WalletInfo = {
3
4
  locked: true;
5
+ event: NostrEvent;
4
6
  } | {
5
7
  locked: false;
6
- privateKey: string;
8
+ event: NostrEvent;
9
+ privateKey?: Uint8Array;
7
10
  mints: string[];
8
11
  };
9
12
  /** A query to get the state of a NIP-60 wallet */
@@ -8,14 +8,14 @@ export function WalletQuery(pubkey) {
8
8
  // get the latest replaceable event
9
9
  events.replaceable(WALLET_KIND, pubkey),
10
10
  // and listen for any updates to matching events
11
- events.database.updated.pipe(filter((e) => e.kind === WALLET_KIND && e.pubkey === pubkey))).pipe(map((wallet) => {
11
+ events.updates.pipe(filter((e) => e.kind === WALLET_KIND && e.pubkey === pubkey))).pipe(map((wallet) => {
12
12
  if (!wallet)
13
13
  return;
14
14
  if (isWalletLocked(wallet))
15
- return { locked: true };
15
+ return { locked: true, event: wallet };
16
16
  const mints = getWalletMints(wallet);
17
17
  const privateKey = getWalletPrivateKey(wallet);
18
- return { locked: false, mints, privateKey };
18
+ return { locked: false, mints, privateKey, event: wallet };
19
19
  })),
20
20
  };
21
21
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "applesauce-wallet",
3
- "version": "0.11.0",
3
+ "version": "0.12.0",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -39,16 +39,57 @@
39
39
  "import": "./dist/queries/*.js",
40
40
  "require": "./dist/queries/*.js",
41
41
  "types": "./dist/queries/*.d.ts"
42
+ },
43
+ "./blueprints": {
44
+ "import": "./dist/blueprints/index.js",
45
+ "require": "./dist/blueprints/index.js",
46
+ "types": "./dist/blueprints/index.d.ts"
47
+ },
48
+ "./blueprints/*": {
49
+ "import": "./dist/blueprints/*.js",
50
+ "require": "./dist/blueprints/*.js",
51
+ "types": "./dist/blueprints/*.d.ts"
52
+ },
53
+ "./operations": {
54
+ "import": "./dist/operations/index.js",
55
+ "require": "./dist/operations/index.js",
56
+ "types": "./dist/operations/index.d.ts"
57
+ },
58
+ "./operations/tag/*": {
59
+ "import": "./dist/operations/tag/*.js",
60
+ "require": "./dist/operations/tag/*.js",
61
+ "types": "./dist/operations/tag/*.d.ts"
62
+ },
63
+ "./operations/event/*": {
64
+ "import": "./dist/operations/event/*.js",
65
+ "require": "./dist/operations/event/*.js",
66
+ "types": "./dist/operations/event/*.d.ts"
67
+ },
68
+ "./actions": {
69
+ "import": "./dist/actions/index.js",
70
+ "require": "./dist/actions/index.js",
71
+ "types": "./dist/actions/index.d.ts"
72
+ },
73
+ "./actions/*": {
74
+ "import": "./dist/actions/*.js",
75
+ "require": "./dist/actions/*.js",
76
+ "types": "./dist/actions/*.d.ts"
42
77
  }
43
78
  },
44
79
  "dependencies": {
45
- "applesauce-core": "^0.11.0",
80
+ "@cashu/cashu-ts": "2.0.0-rc1",
81
+ "@gandlaf21/bc-ur": "^1.1.12",
82
+ "@noble/hashes": "^1.7.1",
83
+ "applesauce-actions": "^0.12.0",
84
+ "applesauce-core": "^0.12.0",
85
+ "applesauce-factory": "^0.12.0",
46
86
  "nostr-tools": "^2.10.4",
47
87
  "rxjs": "^7.8.1"
48
88
  },
49
89
  "devDependencies": {
50
90
  "@hirez_io/observer-spy": "^2.2.0",
51
91
  "@types/debug": "^4.1.12",
92
+ "applesauce-signers": "^0.12.0",
52
93
  "typescript": "^5.7.3",
53
94
  "vitest": "^3.0.5"
54
95
  },