@unlink-xyz/core 0.1.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 (185) hide show
  1. package/dist/account/zkAccount.d.ts +38 -0
  2. package/dist/account/zkAccount.d.ts.map +1 -0
  3. package/dist/account/zkAccount.js +128 -0
  4. package/dist/clients/broadcaster.d.ts +33 -0
  5. package/dist/clients/broadcaster.d.ts.map +1 -0
  6. package/dist/clients/broadcaster.js +23 -0
  7. package/dist/clients/http.d.ts +23 -0
  8. package/dist/clients/http.d.ts.map +1 -0
  9. package/dist/clients/http.js +57 -0
  10. package/dist/clients/indexer.d.ts +44 -0
  11. package/dist/clients/indexer.d.ts.map +1 -0
  12. package/dist/clients/indexer.js +67 -0
  13. package/dist/config.d.ts +12 -0
  14. package/dist/config.d.ts.map +1 -0
  15. package/dist/config.js +29 -0
  16. package/dist/core.d.ts +10 -0
  17. package/dist/core.d.ts.map +1 -0
  18. package/dist/core.js +12 -0
  19. package/dist/errors.d.ts +10 -0
  20. package/dist/errors.d.ts.map +1 -0
  21. package/dist/errors.js +18 -0
  22. package/dist/index.d.ts +18 -0
  23. package/dist/index.d.ts.map +1 -0
  24. package/dist/index.js +16 -0
  25. package/dist/key-derivation/babyjubjub.d.ts +9 -0
  26. package/dist/key-derivation/babyjubjub.d.ts.map +1 -0
  27. package/dist/key-derivation/babyjubjub.js +9 -0
  28. package/dist/key-derivation/bech32.d.ts +22 -0
  29. package/dist/key-derivation/bech32.d.ts.map +1 -0
  30. package/dist/key-derivation/bech32.js +86 -0
  31. package/dist/key-derivation/bip32.d.ts +17 -0
  32. package/dist/key-derivation/bip32.d.ts.map +1 -0
  33. package/dist/key-derivation/bip32.js +41 -0
  34. package/dist/key-derivation/bip39.d.ts +22 -0
  35. package/dist/key-derivation/bip39.d.ts.map +1 -0
  36. package/dist/key-derivation/bip39.js +56 -0
  37. package/dist/key-derivation/bytes.d.ts +19 -0
  38. package/dist/key-derivation/bytes.d.ts.map +1 -0
  39. package/dist/key-derivation/bytes.js +92 -0
  40. package/dist/key-derivation/hash.d.ts +3 -0
  41. package/dist/key-derivation/hash.d.ts.map +1 -0
  42. package/dist/key-derivation/hash.js +10 -0
  43. package/dist/key-derivation/index.d.ts +8 -0
  44. package/dist/key-derivation/index.d.ts.map +1 -0
  45. package/dist/key-derivation/index.js +7 -0
  46. package/dist/key-derivation/wallet-node.d.ts +45 -0
  47. package/dist/key-derivation/wallet-node.d.ts.map +1 -0
  48. package/dist/key-derivation/wallet-node.js +109 -0
  49. package/dist/keys.d.ts +22 -0
  50. package/dist/keys.d.ts.map +1 -0
  51. package/dist/keys.js +41 -0
  52. package/dist/prover/config.d.ts +60 -0
  53. package/dist/prover/config.d.ts.map +1 -0
  54. package/dist/prover/config.js +80 -0
  55. package/dist/prover/index.d.ts +2 -0
  56. package/dist/prover/index.d.ts.map +1 -0
  57. package/dist/prover/index.js +1 -0
  58. package/dist/prover/prover.d.ts +59 -0
  59. package/dist/prover/prover.d.ts.map +1 -0
  60. package/dist/prover/prover.js +274 -0
  61. package/dist/prover/registry.d.ts +39 -0
  62. package/dist/prover/registry.d.ts.map +1 -0
  63. package/dist/prover/registry.js +57 -0
  64. package/dist/schema.d.ts +4 -0
  65. package/dist/schema.d.ts.map +1 -0
  66. package/dist/schema.js +14 -0
  67. package/dist/state/ciphertext-store.d.ts +12 -0
  68. package/dist/state/ciphertext-store.d.ts.map +1 -0
  69. package/dist/state/ciphertext-store.js +25 -0
  70. package/dist/state/index.d.ts +3 -0
  71. package/dist/state/index.d.ts.map +1 -0
  72. package/dist/state/index.js +2 -0
  73. package/dist/state/leaf-store.d.ts +17 -0
  74. package/dist/state/leaf-store.d.ts.map +1 -0
  75. package/dist/state/leaf-store.js +35 -0
  76. package/dist/state/merkle/hydrator.d.ts +27 -0
  77. package/dist/state/merkle/hydrator.d.ts.map +1 -0
  78. package/dist/state/merkle/hydrator.js +36 -0
  79. package/dist/state/merkle/index.d.ts +3 -0
  80. package/dist/state/merkle/index.d.ts.map +1 -0
  81. package/dist/state/merkle/index.js +2 -0
  82. package/dist/state/merkle/merkle-tree.d.ts +34 -0
  83. package/dist/state/merkle/merkle-tree.d.ts.map +1 -0
  84. package/dist/state/merkle/merkle-tree.js +104 -0
  85. package/dist/state/merkle-tree.d.ts +34 -0
  86. package/dist/state/merkle-tree.d.ts.map +1 -0
  87. package/dist/state/merkle-tree.js +104 -0
  88. package/dist/state/note-store.d.ts +37 -0
  89. package/dist/state/note-store.d.ts.map +1 -0
  90. package/dist/state/note-store.js +133 -0
  91. package/dist/state/nullifier-store.d.ts +13 -0
  92. package/dist/state/nullifier-store.d.ts.map +1 -0
  93. package/dist/state/nullifier-store.js +21 -0
  94. package/dist/state/records.d.ts +57 -0
  95. package/dist/state/records.d.ts.map +1 -0
  96. package/dist/state/records.js +1 -0
  97. package/dist/state/root-store.d.ts +13 -0
  98. package/dist/state/root-store.d.ts.map +1 -0
  99. package/dist/state/root-store.js +30 -0
  100. package/dist/state/store/ciphertext-store.d.ts +12 -0
  101. package/dist/state/store/ciphertext-store.d.ts.map +1 -0
  102. package/dist/state/store/ciphertext-store.js +25 -0
  103. package/dist/state/store/index.d.ts +10 -0
  104. package/dist/state/store/index.d.ts.map +1 -0
  105. package/dist/state/store/index.js +8 -0
  106. package/dist/state/store/job-store.d.ts +12 -0
  107. package/dist/state/store/job-store.d.ts.map +1 -0
  108. package/dist/state/store/job-store.js +118 -0
  109. package/dist/state/store/jobs.d.ts +50 -0
  110. package/dist/state/store/jobs.d.ts.map +1 -0
  111. package/dist/state/store/jobs.js +1 -0
  112. package/dist/state/store/leaf-store.d.ts +17 -0
  113. package/dist/state/store/leaf-store.d.ts.map +1 -0
  114. package/dist/state/store/leaf-store.js +35 -0
  115. package/dist/state/store/note-store.d.ts +38 -0
  116. package/dist/state/store/note-store.d.ts.map +1 -0
  117. package/dist/state/store/note-store.js +142 -0
  118. package/dist/state/store/nullifier-store.d.ts +17 -0
  119. package/dist/state/store/nullifier-store.d.ts.map +1 -0
  120. package/dist/state/store/nullifier-store.js +30 -0
  121. package/dist/state/store/records.d.ts +57 -0
  122. package/dist/state/store/records.d.ts.map +1 -0
  123. package/dist/state/store/records.js +1 -0
  124. package/dist/state/store/root-store.d.ts +13 -0
  125. package/dist/state/store/root-store.d.ts.map +1 -0
  126. package/dist/state/store/root-store.js +30 -0
  127. package/dist/state/store/store.d.ts +34 -0
  128. package/dist/state/store/store.d.ts.map +1 -0
  129. package/dist/state/store/store.js +22 -0
  130. package/dist/state/store.d.ts +26 -0
  131. package/dist/state/store.d.ts.map +1 -0
  132. package/dist/state/store.js +19 -0
  133. package/dist/storage/index.d.ts +4 -0
  134. package/dist/storage/index.d.ts.map +1 -0
  135. package/dist/storage/index.js +2 -0
  136. package/dist/storage/indexeddb.d.ts +27 -0
  137. package/dist/storage/indexeddb.d.ts.map +1 -0
  138. package/dist/storage/indexeddb.js +205 -0
  139. package/dist/storage/memory.d.ts +25 -0
  140. package/dist/storage/memory.d.ts.map +1 -0
  141. package/dist/storage/memory.js +87 -0
  142. package/dist/transactions/deposit.d.ts +18 -0
  143. package/dist/transactions/deposit.d.ts.map +1 -0
  144. package/dist/transactions/deposit.js +173 -0
  145. package/dist/transactions/index.d.ts +7 -0
  146. package/dist/transactions/index.d.ts.map +1 -0
  147. package/dist/transactions/index.js +4 -0
  148. package/dist/transactions/note-sync.d.ts +46 -0
  149. package/dist/transactions/note-sync.d.ts.map +1 -0
  150. package/dist/transactions/note-sync.js +320 -0
  151. package/dist/transactions/reconcile.d.ts +22 -0
  152. package/dist/transactions/reconcile.d.ts.map +1 -0
  153. package/dist/transactions/reconcile.js +39 -0
  154. package/dist/transactions/transact.d.ts +34 -0
  155. package/dist/transactions/transact.d.ts.map +1 -0
  156. package/dist/transactions/transact.js +561 -0
  157. package/dist/transactions/types.d.ts +114 -0
  158. package/dist/transactions/types.d.ts.map +1 -0
  159. package/dist/transactions/types.js +1 -0
  160. package/dist/tsconfig.tsbuildinfo +1 -0
  161. package/dist/types.d.ts +27 -0
  162. package/dist/types.d.ts.map +1 -0
  163. package/dist/types.js +1 -0
  164. package/dist/utils/async.d.ts +10 -0
  165. package/dist/utils/async.d.ts.map +1 -0
  166. package/dist/utils/async.js +13 -0
  167. package/dist/utils/bigint.d.ts +9 -0
  168. package/dist/utils/bigint.d.ts.map +1 -0
  169. package/dist/utils/bigint.js +29 -0
  170. package/dist/utils/crypto.d.ts +12 -0
  171. package/dist/utils/crypto.d.ts.map +1 -0
  172. package/dist/utils/crypto.js +39 -0
  173. package/dist/utils/json-codec.d.ts +9 -0
  174. package/dist/utils/json-codec.d.ts.map +1 -0
  175. package/dist/utils/json-codec.js +25 -0
  176. package/dist/utils/polling.d.ts +7 -0
  177. package/dist/utils/polling.d.ts.map +1 -0
  178. package/dist/utils/polling.js +6 -0
  179. package/dist/utils/signature.d.ts +9 -0
  180. package/dist/utils/signature.d.ts.map +1 -0
  181. package/dist/utils/signature.js +12 -0
  182. package/dist/utils/validators.d.ts +30 -0
  183. package/dist/utils/validators.d.ts.map +1 -0
  184. package/dist/utils/validators.js +70 -0
  185. package/package.json +52 -0
@@ -0,0 +1,30 @@
1
+ import { keys } from "../../keys.js";
2
+ import { getJson, putJson } from "../../utils/json-codec.js";
3
+ import { ensureChainId } from "../../utils/validators.js";
4
+ export function createNullifierStore(storage) {
5
+ return {
6
+ /**
7
+ * Record a nullifier observation so later syncs can de-duplicate work.
8
+ */
9
+ async putNullifier(nullifier) {
10
+ ensureChainId(nullifier.chainId);
11
+ await putJson(storage, keys.nullifier(nullifier.chainId, nullifier.nullifier), nullifier);
12
+ },
13
+ /**
14
+ * Lookup a previously observed nullifier by value.
15
+ */
16
+ async getNullifier(chainId, value) {
17
+ ensureChainId(chainId);
18
+ return getJson(storage, keys.nullifier(chainId, value));
19
+ },
20
+ /**
21
+ * Count all nullifiers stored locally for a given chain.
22
+ */
23
+ async countNullifiers(chainId) {
24
+ ensureChainId(chainId);
25
+ const prefix = `nullifiers:${chainId}:`;
26
+ const entries = await storage.iter({ prefix });
27
+ return entries.length;
28
+ },
29
+ };
30
+ }
@@ -0,0 +1,57 @@
1
+ /**
2
+ * Locally cached metadata for a deposit note we consider owned.
3
+ */
4
+ export type NoteRecord = {
5
+ chainId: number;
6
+ /** Leaf index within the on-chain tree (zero-based, monotonic). */
7
+ index: number;
8
+ /** Canonical asset identifier, typically the ERC-20 address lowercased. */
9
+ token: string;
10
+ /** Amount expressed as an unsigned BigInt serialized to a base-10 string. */
11
+ value: string;
12
+ /** Commitment stored on-chain (Poseidon(note)). */
13
+ commitment: string;
14
+ /** Note public key derived from the master public key. */
15
+ npk: string;
16
+ /**
17
+ * Master public key for the account (Poseidon(spendingPk.public, nullifyingKey)).
18
+ * Acts as the stable account identifier when grouping notes locally.
19
+ */
20
+ mpk: string;
21
+ /** Randomizer used when committing the note. */
22
+ random: string;
23
+ /** Nullifier that will be revealed when the note is spent. */
24
+ nullifier: string;
25
+ /** Optional timestamp when the note was marked as spent. */
26
+ spentAt?: number;
27
+ };
28
+ export type NoteInsert = Omit<NoteRecord, 'spentAt'>;
29
+ /**
30
+ * Details about a nullifier observed on-chain, whether or not we own the note.
31
+ */
32
+ export type NullifierRecord = {
33
+ chainId: number;
34
+ /** Nullifier value (expected to match NoteRecord.nullifier). */
35
+ nullifier: string;
36
+ /** Leaf index of the corresponding note when known. */
37
+ noteIndex?: number;
38
+ };
39
+ /**
40
+ * Public information about a leaf commitment inserted into the pool's tree.
41
+ */
42
+ export type LeafRecord = {
43
+ chainId: number;
44
+ /** Leaf index inside the tree (zero-based). */
45
+ index: number;
46
+ /** Commitment value stored in the tree. */
47
+ commitment: string;
48
+ };
49
+ /**
50
+ * Historical merkle root snapshot used for proof construction.
51
+ */
52
+ export type RootRecord = {
53
+ chainId: number;
54
+ /** Merkle root value committed on-chain. */
55
+ root: string;
56
+ };
57
+ //# sourceMappingURL=records.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"records.d.ts","sourceRoot":"","sources":["../../../state/store/records.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,MAAM,UAAU,GAAG;IACvB,OAAO,EAAE,MAAM,CAAC;IAChB,mEAAmE;IACnE,KAAK,EAAE,MAAM,CAAC;IACd,2EAA2E;IAC3E,KAAK,EAAE,MAAM,CAAC;IACd,6EAA6E;IAC7E,KAAK,EAAE,MAAM,CAAC;IACd,mDAAmD;IACnD,UAAU,EAAE,MAAM,CAAC;IACnB,0DAA0D;IAC1D,GAAG,EAAE,MAAM,CAAC;IACZ;;;OAGG;IACH,GAAG,EAAE,MAAM,CAAC;IACZ,gDAAgD;IAChD,MAAM,EAAE,MAAM,CAAC;IACf,8DAA8D;IAC9D,SAAS,EAAE,MAAM,CAAC;IAClB,4DAA4D;IAC5D,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB,CAAC;AAGF,MAAM,MAAM,UAAU,GAAG,IAAI,CAAC,UAAU,EAAE,SAAS,CAAC,CAAC;AAErD;;GAEG;AACH,MAAM,MAAM,eAAe,GAAG;IAC5B,OAAO,EAAE,MAAM,CAAC;IAChB,gEAAgE;IAChE,SAAS,EAAE,MAAM,CAAC;IAClB,uDAAuD;IACvD,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,UAAU,GAAG;IACvB,OAAO,EAAE,MAAM,CAAC;IAChB,+CAA+C;IAC/C,KAAK,EAAE,MAAM,CAAC;IACd,2CAA2C;IAC3C,UAAU,EAAE,MAAM,CAAC;CACpB,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,UAAU,GAAG;IACvB,OAAO,EAAE,MAAM,CAAC;IAChB,4CAA4C;IAC5C,IAAI,EAAE,MAAM,CAAC;CACd,CAAC"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,13 @@
1
+ import type { Storage } from "../../types.js";
2
+ import type { RootRecord } from "../store/records.js";
3
+ export declare function createRootStore(storage: Storage): {
4
+ /**
5
+ * Persist a historical root snapshot for proof generation.
6
+ */
7
+ putRoot(root: RootRecord): Promise<void>;
8
+ /**
9
+ * Retrieve a root snapshot by Merkle root value.
10
+ */
11
+ getRoot(chainId: number, value: string): Promise<RootRecord | null>;
12
+ };
13
+ //# sourceMappingURL=root-store.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"root-store.d.ts","sourceRoot":"","sources":["../../../state/store/root-store.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AAG9C,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AAEtD,wBAAgB,eAAe,CAAC,OAAO,EAAE,OAAO;IAE5C;;OAEG;kBACiB,UAAU;IAU9B;;OAEG;qBACoB,MAAM,SAAS,MAAM;EAQ/C"}
@@ -0,0 +1,30 @@
1
+ import { CoreError } from "../../errors.js";
2
+ import { keys, validateKey } from "../../keys.js";
3
+ import { getJson, putJson } from "../../utils/json-codec.js";
4
+ import { ensureChainId } from "../../utils/validators.js";
5
+ export function createRootStore(storage) {
6
+ return {
7
+ /**
8
+ * Persist a historical root snapshot for proof generation.
9
+ */
10
+ async putRoot(root) {
11
+ ensureChainId(root.chainId);
12
+ if (typeof root.root !== "string" || root.root.length === 0) {
13
+ throw new CoreError("root value must be a non-empty string");
14
+ }
15
+ const key = keys.root(root.chainId, root.root);
16
+ validateKey(key);
17
+ await putJson(storage, key, root);
18
+ },
19
+ /**
20
+ * Retrieve a root snapshot by Merkle root value.
21
+ */
22
+ async getRoot(chainId, value) {
23
+ ensureChainId(chainId);
24
+ if (typeof value !== "string" || value.length === 0) {
25
+ throw new CoreError("root value must be a non-empty string");
26
+ }
27
+ return getJson(storage, keys.root(chainId, value));
28
+ },
29
+ };
30
+ }
@@ -0,0 +1,34 @@
1
+ import type { Storage } from "../../types.js";
2
+ export declare function createStateStore(storage: Storage): {
3
+ putPendingJob(job: import("./jobs.js").PendingJobRecord): Promise<void>;
4
+ getPendingJob(relayId: string): Promise<import("./jobs.js").PendingJobRecord | null>;
5
+ listPendingJobs(filter?: {
6
+ kind?: import("./jobs.js").PendingJobKind;
7
+ statuses?: import("./jobs.js").JobStatus[];
8
+ }): Promise<import("./jobs.js").PendingJobRecord[]>;
9
+ deletePendingJob(relayId: string): Promise<void>;
10
+ putCiphertext(chainId: number, index: number, payload: Uint8Array): Promise<void>;
11
+ getCiphertext(chainId: number, index: number): Promise<Uint8Array<ArrayBuffer> | null>;
12
+ putRoot(root: import("./records.js").RootRecord): Promise<void>;
13
+ getRoot(chainId: number, value: string): Promise<import("./records.js").RootRecord | null>;
14
+ putLeaf(leaf: import("./records.js").LeafRecord): Promise<void>;
15
+ getLeaf(chainId: number, index: number): Promise<import("./records.js").LeafRecord | null>;
16
+ clearLeaves(chainId: number): Promise<void>;
17
+ putNullifier(nullifier: import("./records.js").NullifierRecord): Promise<void>;
18
+ getNullifier(chainId: number, value: string): Promise<import("./records.js").NullifierRecord | null>;
19
+ countNullifiers(chainId: number): Promise<number>;
20
+ putNote(note: import("./records.js").NoteInsert): Promise<void>;
21
+ getNote(chainId: number, index: number): Promise<import("./records.js").NoteRecord | null>;
22
+ listNotes(options?: {
23
+ chainId?: number;
24
+ mpk?: string;
25
+ token?: string;
26
+ includeSpent?: boolean;
27
+ }): Promise<import("./records.js").NoteRecord[]>;
28
+ markNoteSpent(chainId: number, index: number, spentAt?: number): Promise<import("./records.js").NoteRecord>;
29
+ markNoteUnspent(chainId: number, index: number): Promise<import("./records.js").NoteRecord>;
30
+ getZkBalances(mpk: string, options?: {
31
+ chainId?: number;
32
+ }): Promise<Record<string, bigint>>;
33
+ };
34
+ //# sourceMappingURL=store.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"store.d.ts","sourceRoot":"","sources":["../../../state/store/store.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AAQ9C,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,OAAO;;;;YAiBg2F,CAAC;gBAAkC,CAAC;;;;;;;;;;;;;;;;eAA5jC,CAAC;WAAqB,CAAC;aAAuB,CAAC;oBAA8B,CAAC;;;;;eAA+qE,CAAC;;EADtnI"}
@@ -0,0 +1,22 @@
1
+ import { createCiphertextStore } from "./ciphertext-store.js";
2
+ import { createJobStore } from "./job-store.js";
3
+ import { createLeafStore } from "./leaf-store.js";
4
+ import { createNoteStore } from "./note-store.js";
5
+ import { createNullifierStore } from "./nullifier-store.js";
6
+ import { createRootStore } from "./root-store.js";
7
+ export function createStateStore(storage) {
8
+ const notes = createNoteStore(storage);
9
+ const nullifiers = createNullifierStore(storage);
10
+ const leaves = createLeafStore(storage);
11
+ const roots = createRootStore(storage);
12
+ const ciphertexts = createCiphertextStore(storage);
13
+ const jobs = createJobStore(storage);
14
+ return {
15
+ ...notes,
16
+ ...nullifiers,
17
+ ...leaves,
18
+ ...roots,
19
+ ...ciphertexts,
20
+ ...jobs,
21
+ };
22
+ }
@@ -0,0 +1,26 @@
1
+ import type { Storage } from "../types.js";
2
+ export declare function createStateStore(storage: Storage): {
3
+ putCiphertext(chainId: number, index: number, payload: Uint8Array): Promise<void>;
4
+ getCiphertext(chainId: number, index: number): Promise<Uint8Array<ArrayBuffer> | null>;
5
+ putRoot(root: import("./records.js").RootRecord): Promise<void>;
6
+ getRoot(chainId: number, value: string): Promise<import("./records.js").RootRecord | null>;
7
+ putLeaf(leaf: import("./records.js").LeafRecord): Promise<void>;
8
+ getLeaf(chainId: number, index: number): Promise<import("./records.js").LeafRecord | null>;
9
+ clearLeaves(chainId: number): Promise<void>;
10
+ putNullifier(nullifier: import("./records.js").NullifierRecord): Promise<void>;
11
+ getNullifier(chainId: number, value: string): Promise<import("./records.js").NullifierRecord | null>;
12
+ putNote(note: import("./records.js").NoteInsert): Promise<void>;
13
+ getNote(chainId: number, index: number): Promise<import("./records.js").NoteRecord | null>;
14
+ listNotes(options?: {
15
+ chainId?: number;
16
+ mpk?: string;
17
+ token?: string;
18
+ includeSpent?: boolean;
19
+ }): Promise<import("./records.js").NoteRecord[]>;
20
+ markNoteSpent(chainId: number, index: number, spentAt?: number): Promise<import("./records.js").NoteRecord>;
21
+ markNoteUnspent(chainId: number, index: number): Promise<import("./records.js").NoteRecord>;
22
+ getZkBalances(mpk: string, options?: {
23
+ chainId?: number;
24
+ }): Promise<Record<string, bigint>>;
25
+ };
26
+ //# sourceMappingURL=store.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"store.d.ts","sourceRoot":"","sources":["../../state/store.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAO3C,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,OAAO;;;;;;;;;;;;;eAe6/C,CAAC;WAAqB,CAAC;aAAuB,CAAC;oBAA8B,CAAC;;;;;eAA+qE,CAAC;;EAD3yH"}
@@ -0,0 +1,19 @@
1
+ import { createCiphertextStore } from "./ciphertext-store.js";
2
+ import { createLeafStore } from "./leaf-store.js";
3
+ import { createNoteStore } from "./note-store.js";
4
+ import { createNullifierStore } from "./nullifier-store.js";
5
+ import { createRootStore } from "./root-store.js";
6
+ export function createStateStore(storage) {
7
+ const notes = createNoteStore(storage);
8
+ const nullifiers = createNullifierStore(storage);
9
+ const leaves = createLeafStore(storage);
10
+ const roots = createRootStore(storage);
11
+ const ciphertexts = createCiphertextStore(storage);
12
+ return {
13
+ ...notes,
14
+ ...nullifiers,
15
+ ...leaves,
16
+ ...roots,
17
+ ...ciphertexts,
18
+ };
19
+ }
@@ -0,0 +1,4 @@
1
+ export { createMemoryStorage } from "./memory.js";
2
+ export { createIndexedDbStorage, type IndexedDbOptions } from "./indexeddb.js";
3
+ export type { Storage } from "../types.js";
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../storage/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AAClD,OAAO,EAAE,sBAAsB,EAAE,KAAK,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAC/E,YAAY,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC"}
@@ -0,0 +1,2 @@
1
+ export { createMemoryStorage } from "./memory.js";
2
+ export { createIndexedDbStorage } from "./indexeddb.js";
@@ -0,0 +1,27 @@
1
+ /**
2
+ * IndexedDB-backed key/value storage for Unlink Core.
3
+ *
4
+ * What this is
5
+ * - A durable, transactional KV store implemented on top of the browser's IndexedDB.
6
+ * - Two object stores: "kv" for app data, "meta" for schema/versioning.
7
+ *
8
+ * Why we implement it
9
+ * - We need persistent client-side state (notes, leaves, nullifiers, cfg, etc.) that survives reloads
10
+ * and works in both web apps and browser extensions without introducing a new backend.
11
+ *
12
+ * When to use
13
+ * - Browser UIs and extensions (MV2/MV3) where persistence is required.
14
+ * - Any environment with a real IndexedDB (tests use `fake-indexeddb`).
15
+ *
16
+ * Trade-offs
17
+ * - 👍 Durable, quota-managed, non-blocking, transactional writes (atomic batch).
18
+ * - 👍 Works offline; no server dependency.
19
+ * - ⚠️ Not natively available in Node/SSR; needs a polyfill for tests only.
20
+ *
21
+ */
22
+ import type { Storage } from "../types.js";
23
+ export type IndexedDbOptions = {
24
+ name?: string;
25
+ };
26
+ export declare function createIndexedDbStorage(opts?: IndexedDbOptions): Storage;
27
+ //# sourceMappingURL=indexeddb.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"indexeddb.d.ts","sourceRoot":"","sources":["../../storage/indexeddb.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAIH,OAAO,KAAK,EAAuC,OAAO,EAAE,MAAM,aAAa,CAAC;AAiBhF,MAAM,MAAM,gBAAgB,GAAG;IAC7B,IAAI,CAAC,EAAE,MAAM,CAAC;CACf,CAAC;AAsCF,wBAAgB,sBAAsB,CAAC,IAAI,GAAE,gBAAqB,GAAG,OAAO,CAyJ3E"}
@@ -0,0 +1,205 @@
1
+ /**
2
+ * IndexedDB-backed key/value storage for Unlink Core.
3
+ *
4
+ * What this is
5
+ * - A durable, transactional KV store implemented on top of the browser's IndexedDB.
6
+ * - Two object stores: "kv" for app data, "meta" for schema/versioning.
7
+ *
8
+ * Why we implement it
9
+ * - We need persistent client-side state (notes, leaves, nullifiers, cfg, etc.) that survives reloads
10
+ * and works in both web apps and browser extensions without introducing a new backend.
11
+ *
12
+ * When to use
13
+ * - Browser UIs and extensions (MV2/MV3) where persistence is required.
14
+ * - Any environment with a real IndexedDB (tests use `fake-indexeddb`).
15
+ *
16
+ * Trade-offs
17
+ * - 👍 Durable, quota-managed, non-blocking, transactional writes (atomic batch).
18
+ * - 👍 Works offline; no server dependency.
19
+ * - ⚠️ Not natively available in Node/SSR; needs a polyfill for tests only.
20
+ *
21
+ */
22
+ import { CoreError } from "../errors.js";
23
+ import { validateKey } from "../keys.js";
24
+ const DEFAULT_DB_NAME = "unlink-core";
25
+ const DB_VERSION = 1;
26
+ const KV_STORE = "kv";
27
+ const META_STORE = "meta";
28
+ const SCHEMA_KEY = "schema_version";
29
+ const copy = (value) => new Uint8Array(value);
30
+ function assertIndexedDb() {
31
+ if (typeof indexedDB === "undefined") {
32
+ throw new CoreError("indexedDB is not available in this environment");
33
+ }
34
+ return indexedDB;
35
+ }
36
+ // Translate IndexedDB's event-based transaction API into a promise that resolves
37
+ // when the transaction completes or rejects on failure/abort.
38
+ function txDone(tx) {
39
+ return new Promise((resolve, reject) => {
40
+ tx.oncomplete = () => resolve();
41
+ tx.onerror = () => reject(tx.error ?? new CoreError("indexedDB transaction failed"));
42
+ tx.onabort = () => reject(tx.error ?? new CoreError("indexedDB transaction aborted"));
43
+ });
44
+ }
45
+ function wrapRequest(request) {
46
+ return new Promise((resolve, reject) => {
47
+ request.onsuccess = () => resolve(request.result);
48
+ request.onerror = () => reject(request.error ?? new CoreError("indexedDB request failed"));
49
+ });
50
+ }
51
+ // Coerce whatever IndexedDB gives us back into our Bytes type.
52
+ function asBytes(raw) {
53
+ if (raw instanceof Uint8Array)
54
+ return copy(raw);
55
+ if (raw instanceof ArrayBuffer)
56
+ return copy(new Uint8Array(raw));
57
+ if (ArrayBuffer.isView(raw)) {
58
+ const view = raw;
59
+ const buf = view.buffer.slice(view.byteOffset, view.byteOffset + view.byteLength);
60
+ return copy(new Uint8Array(buf));
61
+ }
62
+ throw new CoreError("indexedDB value is not binary data");
63
+ }
64
+ // IndexedDB-backed implementation implementing the storage API.
65
+ export function createIndexedDbStorage(opts = {}) {
66
+ const name = opts.name ?? DEFAULT_DB_NAME;
67
+ let db = null;
68
+ // Lazy-open the database so we only touch IndexedDB when needed.
69
+ async function ensureOpen() {
70
+ if (db)
71
+ return db;
72
+ // Guard for environments without IndexedDB (unit tests or SSR).
73
+ const idb = assertIndexedDb();
74
+ const request = idb.open(name, DB_VERSION);
75
+ // Versioned schema: create one store for kv entries and one for metadata.
76
+ request.onupgradeneeded = () => {
77
+ const upgradeDb = request.result;
78
+ if (!upgradeDb.objectStoreNames.contains(KV_STORE)) {
79
+ upgradeDb.createObjectStore(KV_STORE);
80
+ }
81
+ if (!upgradeDb.objectStoreNames.contains(META_STORE)) {
82
+ upgradeDb.createObjectStore(META_STORE);
83
+ }
84
+ };
85
+ db = await new Promise((resolve, reject) => {
86
+ request.onsuccess = () => resolve(request.result);
87
+ request.onerror = () => reject(request.error ?? new CoreError("indexedDB open failed"));
88
+ request.onblocked = () => reject(new CoreError("indexedDB open request was blocked"));
89
+ });
90
+ return db;
91
+ }
92
+ // Start a transaction and give callers both the store and a completion promise.
93
+ const getStore = async (storeName, mode) => {
94
+ const database = await ensureOpen();
95
+ const tx = database.transaction(storeName, mode);
96
+ const done = txDone(tx);
97
+ return { store: tx.objectStore(storeName), done };
98
+ };
99
+ return {
100
+ async open() {
101
+ await ensureOpen();
102
+ },
103
+ async get(key) {
104
+ validateKey(key);
105
+ const { store, done } = await getStore(KV_STORE, "readonly");
106
+ const result = await wrapRequest(store.get(key));
107
+ await done;
108
+ if (result === undefined)
109
+ return null;
110
+ return asBytes(result);
111
+ },
112
+ async put(key, value) {
113
+ validateKey(key);
114
+ const { store, done } = await getStore(KV_STORE, "readwrite");
115
+ await wrapRequest(store.put(copy(value), key)); // copy avoids shared mutation; revisit if this becomes costly
116
+ await done;
117
+ },
118
+ async delete(key) {
119
+ validateKey(key);
120
+ const { store, done } = await getStore(KV_STORE, "readwrite");
121
+ await wrapRequest(store.delete(key));
122
+ await done;
123
+ },
124
+ async batch(ops) {
125
+ // Execute the batch within a single transaction to preserve atomicity.
126
+ const database = await ensureOpen();
127
+ const tx = database.transaction(KV_STORE, "readwrite");
128
+ const store = tx.objectStore(KV_STORE);
129
+ const done = txDone(tx);
130
+ try {
131
+ // Replay the batch within a single transaction; validation failure aborts everything.
132
+ for (const op of ops) {
133
+ if (op.put) {
134
+ const [key, value] = op.put;
135
+ validateKey(key);
136
+ store.put(copy(value), key); // copy avoids shared mutation; revisit if this becomes costly
137
+ }
138
+ if (op.del) {
139
+ validateKey(op.del);
140
+ store.delete(op.del);
141
+ }
142
+ }
143
+ await done;
144
+ }
145
+ catch (err) {
146
+ try {
147
+ tx.abort();
148
+ }
149
+ catch (_) {
150
+ /* noop */
151
+ }
152
+ await done.catch(() => { });
153
+ throw err;
154
+ }
155
+ },
156
+ async iter(options = {}) {
157
+ if (options.start && options.end && options.start > options.end) {
158
+ throw new CoreError("iter start bound must not exceed end bound");
159
+ }
160
+ // Pull everything into memory for now. TODO: stream w/ cursor when needed for perf
161
+ const database = await ensureOpen();
162
+ const tx = database.transaction(KV_STORE, "readonly");
163
+ const store = tx.objectStore(KV_STORE);
164
+ const done = txDone(tx);
165
+ const [keys, values] = await Promise.all([
166
+ wrapRequest(store.getAllKeys()),
167
+ wrapRequest(store.getAll()),
168
+ ]);
169
+ await done;
170
+ const pairs = keys.map((key, idx) => {
171
+ if (typeof key !== "string") {
172
+ throw new CoreError("encountered non-string key in indexedDB");
173
+ }
174
+ const raw = values[idx];
175
+ return { key, value: asBytes(raw) };
176
+ });
177
+ let filtered = pairs.filter(({ key }) => {
178
+ if (options.prefix && !key.startsWith(options.prefix))
179
+ return false;
180
+ if (options.start && key < options.start)
181
+ return false;
182
+ if (options.end && key > options.end)
183
+ return false;
184
+ return true;
185
+ });
186
+ filtered.sort((a, b) => a.key.localeCompare(b.key));
187
+ if (options.reverse)
188
+ filtered = filtered.reverse();
189
+ if (options.limit)
190
+ filtered = filtered.slice(0, options.limit);
191
+ return filtered;
192
+ },
193
+ async getSchemaVersion() {
194
+ const { store, done } = await getStore(META_STORE, "readonly");
195
+ const value = await wrapRequest(store.get(SCHEMA_KEY));
196
+ await done;
197
+ return value ?? 0;
198
+ },
199
+ async setSchemaVersion(version) {
200
+ const { store, done } = await getStore(META_STORE, "readwrite");
201
+ await wrapRequest(store.put(version, SCHEMA_KEY));
202
+ await done;
203
+ },
204
+ };
205
+ }
@@ -0,0 +1,25 @@
1
+ /**
2
+ * In-memory key/value storage for Unlink Core.
3
+ *
4
+ * What this is
5
+ * - A minimal, process-local KV store backed by a `Map<string, Uint8Array>`.
6
+ *
7
+ * Why we implement it
8
+ * - Fast, dependency-free storage for unit tests, demos, and ephemeral runs.
9
+ * - Useful as a reference driver for the Storage interface.
10
+ *
11
+ * When to use
12
+ * - Unit tests (deterministic, zero I/O).
13
+ * - CLI/Node scripts that do not need persistence beyond process lifetime.
14
+ * - Prototyping or sandboxing Core logic.
15
+ *
16
+ * Trade-offs
17
+ * - 👍 Extremely fast; zero external APIs; simple to reason about.
18
+ * - 👍 Great for isolation in tests; easy to snapshot/clone.
19
+ * - ⚠️ No durability (data is lost on process exit).
20
+ * - ⚠️ Per-process only; no cross-tab or reload survival.
21
+ *
22
+ */
23
+ import { Storage } from "../types.js";
24
+ export declare function createMemoryStorage(): Storage;
25
+ //# sourceMappingURL=memory.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"memory.d.ts","sourceRoot":"","sources":["../../storage/memory.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAIH,OAAO,EAAuC,OAAO,EAAE,MAAM,aAAa,CAAC;AAI3E,wBAAgB,mBAAmB,IAAI,OAAO,CA+D7C"}
@@ -0,0 +1,87 @@
1
+ /**
2
+ * In-memory key/value storage for Unlink Core.
3
+ *
4
+ * What this is
5
+ * - A minimal, process-local KV store backed by a `Map<string, Uint8Array>`.
6
+ *
7
+ * Why we implement it
8
+ * - Fast, dependency-free storage for unit tests, demos, and ephemeral runs.
9
+ * - Useful as a reference driver for the Storage interface.
10
+ *
11
+ * When to use
12
+ * - Unit tests (deterministic, zero I/O).
13
+ * - CLI/Node scripts that do not need persistence beyond process lifetime.
14
+ * - Prototyping or sandboxing Core logic.
15
+ *
16
+ * Trade-offs
17
+ * - 👍 Extremely fast; zero external APIs; simple to reason about.
18
+ * - 👍 Great for isolation in tests; easy to snapshot/clone.
19
+ * - ⚠️ No durability (data is lost on process exit).
20
+ * - ⚠️ Per-process only; no cross-tab or reload survival.
21
+ *
22
+ */
23
+ import { CoreError } from "../errors.js";
24
+ import { validateKey } from "../keys.js";
25
+ const copy = (value) => new Uint8Array(value);
26
+ export function createMemoryStorage() {
27
+ const data = new Map();
28
+ let schema = 0;
29
+ return {
30
+ async open() { },
31
+ async get(k) {
32
+ validateKey(k);
33
+ return data.has(k) ? copy(data.get(k)) : null;
34
+ },
35
+ async put(k, v) {
36
+ validateKey(k);
37
+ data.set(k, copy(v));
38
+ },
39
+ async delete(k) {
40
+ validateKey(k);
41
+ data.delete(k);
42
+ },
43
+ async batch(ops) {
44
+ const draft = new Map(data);
45
+ for (const op of ops) {
46
+ if (op.put) {
47
+ const [k, v] = op.put;
48
+ validateKey(k);
49
+ draft.set(k, copy(v));
50
+ }
51
+ if (op.del) {
52
+ validateKey(op.del);
53
+ draft.delete(op.del);
54
+ }
55
+ }
56
+ data.clear();
57
+ for (const [k, v] of draft.entries()) {
58
+ data.set(k, copy(v));
59
+ }
60
+ },
61
+ async iter(o = {}) {
62
+ if (o.start && o.end && o.start > o.end) {
63
+ throw new CoreError("iter start bound must not exceed end bound");
64
+ }
65
+ const all = [...data.entries()].map(([key, value]) => ({
66
+ key,
67
+ value: copy(value),
68
+ }));
69
+ let ks = all
70
+ .filter(({ key }) => (!o.prefix || key.startsWith(o.prefix)) &&
71
+ (!o.start || key >= o.start) &&
72
+ (!o.end || key <= o.end))
73
+ .sort((a, b) => a.key.localeCompare(b.key));
74
+ if (o.reverse)
75
+ ks.reverse();
76
+ if (o.limit)
77
+ ks = ks.slice(0, o.limit);
78
+ return ks;
79
+ },
80
+ async getSchemaVersion() {
81
+ return schema;
82
+ },
83
+ async setSchemaVersion(n) {
84
+ schema = n;
85
+ },
86
+ };
87
+ }
@@ -0,0 +1,18 @@
1
+ import { FetchLike } from "../clients/http.js";
2
+ import { createIndexerClient } from "../clients/indexer.js";
3
+ import { LocalMerkleTrees } from "../state/index.js";
4
+ import type { DepositRelayResult, DepositRequest, DepositStateStore, DepositSyncResult } from "./types.js";
5
+ export declare const DEPOSIT_ABI: readonly ["function deposit(address _depositor, (uint256 npk, uint256 amount, address token)[] notes, (uint256[3] data)[] ciphertexts)"];
6
+ type DepositClientOptions = {
7
+ merkleTrees?: LocalMerkleTrees;
8
+ fetch: FetchLike;
9
+ pollIntervalMs?: number;
10
+ pollTimeoutMs?: number;
11
+ indexerClient?: ReturnType<typeof createIndexerClient>;
12
+ };
13
+ export declare function createDepositClient(stateStore: DepositStateStore, options: DepositClientOptions): {
14
+ request(request: DepositRequest): Promise<DepositRelayResult>;
15
+ syncPendingDeposit(relayId: string): Promise<DepositSyncResult>;
16
+ };
17
+ export {};
18
+ //# sourceMappingURL=deposit.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"deposit.d.ts","sourceRoot":"","sources":["../../transactions/deposit.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAC/C,OAAO,EAAoB,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAG9E,OAAO,EAGL,gBAAgB,EAGjB,MAAM,mBAAmB,CAAC;AAU3B,OAAO,KAAK,EACV,kBAAkB,EAClB,cAAc,EACd,iBAAiB,EACjB,iBAAiB,EAClB,MAAM,YAAY,CAAC;AAEpB,eAAO,MAAM,WAAW,0IAEd,CAAC;AAGX,KAAK,oBAAoB,GAAG;IAC1B,WAAW,CAAC,EAAE,gBAAgB,CAAC;IAC/B,KAAK,EAAE,SAAS,CAAC;IACjB,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,aAAa,CAAC,EAAE,UAAU,CAAC,OAAO,mBAAmB,CAAC,CAAC;CACxD,CAAC;AAuBF,wBAAgB,mBAAmB,CACjC,UAAU,EAAE,iBAAiB,EAC7B,OAAO,EAAE,oBAAoB;qBAiEJ,cAAc,GAAG,OAAO,CAAC,kBAAkB,CAAC;gCA6BjC,MAAM,GAAG,OAAO,CAAC,iBAAiB,CAAC;EAwExE"}