@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,60 @@
1
+ /**
2
+ * Configuration for the prover system
3
+ * Handles artifact loading from local files or remote artifact server
4
+ */
5
+ /**
6
+ * Environment-based configuration for artifact loading
7
+ */
8
+ export interface ProverConfig {
9
+ /** Base URL for the artifact server (browser mode) */
10
+ rpcUrl: string;
11
+ /** Whether to enable artifact caching */
12
+ enableCache: boolean;
13
+ }
14
+ /**
15
+ * Default configuration
16
+ * Can be overridden via environment variables
17
+ */
18
+ export declare const DEFAULT_CONFIG: ProverConfig;
19
+ /**
20
+ * Artifact file types
21
+ */
22
+ export declare enum ArtifactType {
23
+ WASM = "joinsplit.wasm",
24
+ ZKEY = "joinsplit.zkey",
25
+ VKEY = "joinsplit.vkey.json"
26
+ }
27
+ /**
28
+ * Build artifact URL for a given circuit and artifact type
29
+ * @param circuitName Full circuit name (e.g., "joinsplit_2x3_16")
30
+ * @param artifactType Type of artifact to fetch
31
+ * @param baseUrl Base URL of the artifact server
32
+ * @returns Full URL to the artifact
33
+ */
34
+ export declare function buildArtifactUrl(circuitName: string, artifactType: ArtifactType, baseUrl?: string): string;
35
+ /**
36
+ * Get relative path for local artifact loading
37
+ * Used in Node.js environment when artifacts are bundled
38
+ * @param circuitName Full circuit name (e.g., "joinsplit_2x3_16")
39
+ * @param artifactType Type of artifact
40
+ * @returns Relative path to artifact
41
+ */
42
+ export declare function getLocalArtifactPath(circuitName: string, artifactType: ArtifactType): string[];
43
+ /**
44
+ * Runtime detection utilities
45
+ */
46
+ export declare const Runtime: {
47
+ /**
48
+ * Check if running in browser environment
49
+ */
50
+ isBrowser(): boolean;
51
+ /**
52
+ * Check if running in Node.js environment
53
+ */
54
+ isNode(): boolean;
55
+ /**
56
+ * Get environment description for debugging
57
+ */
58
+ getEnvironment(): string;
59
+ };
60
+ //# sourceMappingURL=config.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../../prover/config.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;GAEG;AACH,MAAM,WAAW,YAAY;IAC3B,sDAAsD;IACtD,MAAM,EAAE,MAAM,CAAC;IACf,yCAAyC;IACzC,WAAW,EAAE,OAAO,CAAC;CACtB;AAED;;;GAGG;AACH,eAAO,MAAM,cAAc,EAAE,YAM5B,CAAC;AAEF;;GAEG;AACH,oBAAY,YAAY;IACtB,IAAI,mBAAmB;IACvB,IAAI,mBAAmB;IACvB,IAAI,wBAAwB;CAC7B;AAED;;;;;;GAMG;AACH,wBAAgB,gBAAgB,CAC9B,WAAW,EAAE,MAAM,EACnB,YAAY,EAAE,YAAY,EAC1B,OAAO,GAAE,MAA8B,GACtC,MAAM,CAIR;AAED;;;;;;GAMG;AACH,wBAAgB,oBAAoB,CAClC,WAAW,EAAE,MAAM,EACnB,YAAY,EAAE,YAAY,GACzB,MAAM,EAAE,CAQV;AAED;;GAEG;AACH,eAAO,MAAM,OAAO;IAClB;;OAEG;iBACU,OAAO;IAIpB;;OAEG;cACO,OAAO;IAQjB;;OAEG;sBACe,MAAM;CAKzB,CAAC"}
@@ -0,0 +1,80 @@
1
+ /**
2
+ * Configuration for the prover system
3
+ * Handles artifact loading from local files or remote artifact server
4
+ */
5
+ /**
6
+ * Default configuration
7
+ * Can be overridden via environment variables
8
+ */
9
+ export const DEFAULT_CONFIG = {
10
+ rpcUrl: typeof process !== "undefined" && process.env?.UNLINK_RPC_URL
11
+ ? process.env.UNLINK_RPC_URL
12
+ : "http://localhost:3000",
13
+ enableCache: true,
14
+ };
15
+ /**
16
+ * Artifact file types
17
+ */
18
+ export var ArtifactType;
19
+ (function (ArtifactType) {
20
+ ArtifactType["WASM"] = "joinsplit.wasm";
21
+ ArtifactType["ZKEY"] = "joinsplit.zkey";
22
+ ArtifactType["VKEY"] = "joinsplit.vkey.json";
23
+ })(ArtifactType || (ArtifactType = {}));
24
+ /**
25
+ * Build artifact URL for a given circuit and artifact type
26
+ * @param circuitName Full circuit name (e.g., "joinsplit_2x3_16")
27
+ * @param artifactType Type of artifact to fetch
28
+ * @param baseUrl Base URL of the artifact server
29
+ * @returns Full URL to the artifact
30
+ */
31
+ export function buildArtifactUrl(circuitName, artifactType, baseUrl = DEFAULT_CONFIG.rpcUrl) {
32
+ // Remove trailing slash from baseUrl
33
+ const cleanBaseUrl = baseUrl.replace(/\/$/, "");
34
+ return `${cleanBaseUrl}/artifacts/${circuitName}/${artifactType}`;
35
+ }
36
+ /**
37
+ * Get relative path for local artifact loading
38
+ * Used in Node.js environment when artifacts are bundled
39
+ * @param circuitName Full circuit name (e.g., "joinsplit_2x3_16")
40
+ * @param artifactType Type of artifact
41
+ * @returns Relative path to artifact
42
+ */
43
+ export function getLocalArtifactPath(circuitName, artifactType) {
44
+ // Return multiple candidate paths for different build scenarios
45
+ return [
46
+ // When compiled: dist/prover/ -> ../../artifacts/circuits/{circuit}/
47
+ `../../artifacts/circuits/${circuitName}/${artifactType}`,
48
+ // When in source: prover/ -> ../artifacts/circuits/{circuit}/
49
+ `../artifacts/circuits/${circuitName}/${artifactType}`,
50
+ ];
51
+ }
52
+ /**
53
+ * Runtime detection utilities
54
+ */
55
+ export const Runtime = {
56
+ /**
57
+ * Check if running in browser environment
58
+ */
59
+ isBrowser() {
60
+ return typeof window !== "undefined" && typeof document !== "undefined";
61
+ },
62
+ /**
63
+ * Check if running in Node.js environment
64
+ */
65
+ isNode() {
66
+ return (typeof process !== "undefined" &&
67
+ process.versions != null &&
68
+ process.versions.node != null);
69
+ },
70
+ /**
71
+ * Get environment description for debugging
72
+ */
73
+ getEnvironment() {
74
+ if (this.isBrowser())
75
+ return "browser";
76
+ if (this.isNode())
77
+ return "node";
78
+ return "unknown";
79
+ },
80
+ };
@@ -0,0 +1,2 @@
1
+ export * from "./prover.js";
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../prover/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC"}
@@ -0,0 +1 @@
1
+ export * from "./prover.js";
@@ -0,0 +1,59 @@
1
+ import { Groth16Proof, PublicSignals } from "snarkjs";
2
+ export interface JoinsplitProof {
3
+ readonly proof: Groth16Proof;
4
+ readonly publicSignals: PublicSignals;
5
+ }
6
+ export interface VerificationKey {
7
+ readonly protocol: string;
8
+ readonly curve: string;
9
+ readonly nPublic: number;
10
+ readonly vk_alpha_1: string[];
11
+ readonly vk_beta_2: string[][];
12
+ readonly vk_gamma_2: string[][];
13
+ readonly vk_delta_2: string[][];
14
+ readonly vk_alphabeta_12: string[][][];
15
+ readonly IC: string[][];
16
+ }
17
+ export interface JoinsplitProofInput {
18
+ readonly merkleRoot: bigint;
19
+ readonly boundParamsHash: bigint;
20
+ readonly nullifiers: bigint[];
21
+ readonly commitmentsOut: bigint[];
22
+ readonly token: bigint;
23
+ readonly publicKey: bigint[];
24
+ readonly signature: bigint[];
25
+ readonly randomIn: bigint[];
26
+ readonly valueIn: bigint[];
27
+ readonly pathElements: bigint[][];
28
+ readonly leavesIndices: bigint[];
29
+ readonly nullifyingKey: bigint;
30
+ readonly npkOut: bigint[];
31
+ readonly valueOut: bigint[];
32
+ }
33
+ export declare function proveTransaction(input: JoinsplitProofInput): Promise<JoinsplitProof>;
34
+ /**
35
+ * Get verification key for a specific circuit configuration
36
+ * @param inputs Number of inputs (nullifiers)
37
+ * @param outputs Number of outputs (commitments)
38
+ * @returns Verification key for the circuit
39
+ */
40
+ export declare function getVerificationKey(inputs: number, outputs: number): Promise<VerificationKey>;
41
+ /**
42
+ * Verify a proof with public signals
43
+ * Auto-detects the circuit from the public signals structure
44
+ * @param proof Groth16 proof
45
+ * @param publicSignals Public signals array
46
+ * @returns True if proof is valid
47
+ */
48
+ export declare function verifyProof(proof: Groth16Proof, publicSignals: PublicSignals): Promise<boolean>;
49
+ /**
50
+ * Verify a proof with explicit circuit specification
51
+ * Preferred method when you know the circuit dimensions
52
+ * @param proof Groth16 proof
53
+ * @param publicSignals Public signals array
54
+ * @param inputs Number of inputs (nullifiers)
55
+ * @param outputs Number of outputs (commitments)
56
+ * @returns True if proof is valid
57
+ */
58
+ export declare function verifyProofWithCircuit(proof: Groth16Proof, publicSignals: PublicSignals, inputs: number, outputs: number): Promise<boolean>;
59
+ //# sourceMappingURL=prover.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"prover.d.ts","sourceRoot":"","sources":["../../prover/prover.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AA4BtD,MAAM,WAAW,cAAc;IAC7B,QAAQ,CAAC,KAAK,EAAE,YAAY,CAAC;IAC7B,QAAQ,CAAC,aAAa,EAAE,aAAa,CAAC;CACvC;AAED,MAAM,WAAW,eAAe;IAC9B,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,UAAU,EAAE,MAAM,EAAE,CAAC;IAC9B,QAAQ,CAAC,SAAS,EAAE,MAAM,EAAE,EAAE,CAAC;IAC/B,QAAQ,CAAC,UAAU,EAAE,MAAM,EAAE,EAAE,CAAC;IAChC,QAAQ,CAAC,UAAU,EAAE,MAAM,EAAE,EAAE,CAAC;IAChC,QAAQ,CAAC,eAAe,EAAE,MAAM,EAAE,EAAE,EAAE,CAAC;IACvC,QAAQ,CAAC,EAAE,EAAE,MAAM,EAAE,EAAE,CAAC;CACzB;AAED,MAAM,WAAW,mBAAmB;IAClC,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,QAAQ,CAAC,eAAe,EAAE,MAAM,CAAC;IACjC,QAAQ,CAAC,UAAU,EAAE,MAAM,EAAE,CAAC;IAC9B,QAAQ,CAAC,cAAc,EAAE,MAAM,EAAE,CAAC;IAClC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,SAAS,EAAE,MAAM,EAAE,CAAC;IAC7B,QAAQ,CAAC,SAAS,EAAE,MAAM,EAAE,CAAC;IAC7B,QAAQ,CAAC,QAAQ,EAAE,MAAM,EAAE,CAAC;IAC5B,QAAQ,CAAC,OAAO,EAAE,MAAM,EAAE,CAAC;IAC3B,QAAQ,CAAC,YAAY,EAAE,MAAM,EAAE,EAAE,CAAC;IAClC,QAAQ,CAAC,aAAa,EAAE,MAAM,EAAE,CAAC;IACjC,QAAQ,CAAC,aAAa,EAAE,MAAM,CAAC;IAC/B,QAAQ,CAAC,MAAM,EAAE,MAAM,EAAE,CAAC;IAC1B,QAAQ,CAAC,QAAQ,EAAE,MAAM,EAAE,CAAC;CAC7B;AAiND,wBAAsB,gBAAgB,CACpC,KAAK,EAAE,mBAAmB,GACzB,OAAO,CAAC,cAAc,CAAC,CAmCzB;AAED;;;;;GAKG;AACH,wBAAsB,kBAAkB,CACtC,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,MAAM,GACd,OAAO,CAAC,eAAe,CAAC,CAU1B;AAED;;;;;;GAMG;AACH,wBAAsB,WAAW,CAC/B,KAAK,EAAE,YAAY,EACnB,aAAa,EAAE,aAAa,GAC3B,OAAO,CAAC,OAAO,CAAC,CAuClB;AAED;;;;;;;;GAQG;AACH,wBAAsB,sBAAsB,CAC1C,KAAK,EAAE,YAAY,EACnB,aAAa,EAAE,aAAa,EAC5B,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,MAAM,GACd,OAAO,CAAC,OAAO,CAAC,CAUlB"}
@@ -0,0 +1,274 @@
1
+ import * as snarkjs from "snarkjs";
2
+ import { CoreError } from "../errors.js";
3
+ import { ArtifactType, buildArtifactUrl, DEFAULT_CONFIG, getLocalArtifactPath, Runtime, } from "./config.js";
4
+ import { getCircuitConfig, SUPPORTED_CIRCUITS, } from "./registry.js";
5
+ // Cache for loaded artifacts to avoid reloading 18MB of files on every call
6
+ // Now keyed by circuit name to support multiple circuits
7
+ const artifactCache = {};
8
+ /**
9
+ * Select the appropriate circuit based on input/output dimensions
10
+ * @param inputs Number of inputs (nullifiers)
11
+ * @param outputs Number of outputs (commitments)
12
+ * @returns Circuit configuration
13
+ * @throws CoreError if no matching circuit is found
14
+ */
15
+ function selectCircuit(inputs, outputs) {
16
+ const config = getCircuitConfig(inputs, outputs);
17
+ if (!config) {
18
+ throw new CoreError(`No circuit available for ${inputs} inputs and ${outputs} outputs. ` +
19
+ `Supported circuits: ${SUPPORTED_CIRCUITS.join(", ")}`);
20
+ }
21
+ return config;
22
+ }
23
+ /**
24
+ * Fetch circuit artifacts for a specific circuit
25
+ * Supports both browser (remote) and Node.js (local) environments
26
+ * @param circuitName Full circuit name (e.g., "joinsplit_2x3_16")
27
+ * @returns Artifacts object with wasm, zkey, and vkey
28
+ */
29
+ async function fetchCircuitArtifacts(circuitName) {
30
+ // Check cache first
31
+ if (artifactCache[circuitName]) {
32
+ return artifactCache[circuitName];
33
+ }
34
+ let wasm;
35
+ let zkey;
36
+ let vkey;
37
+ // Browser environment: fetch from artifact server
38
+ if (Runtime.isBrowser()) {
39
+ try {
40
+ const baseUrl = DEFAULT_CONFIG.rpcUrl;
41
+ const wasmUrl = buildArtifactUrl(circuitName, ArtifactType.WASM, baseUrl);
42
+ const zkeyUrl = buildArtifactUrl(circuitName, ArtifactType.ZKEY, baseUrl);
43
+ const vkeyUrl = buildArtifactUrl(circuitName, ArtifactType.VKEY, baseUrl);
44
+ const [wasmRes, zkeyRes, vkeyRes] = await Promise.all([
45
+ fetch(wasmUrl),
46
+ fetch(zkeyUrl),
47
+ fetch(vkeyUrl),
48
+ ]);
49
+ if (!wasmRes.ok || !zkeyRes.ok || !vkeyRes.ok) {
50
+ throw new CoreError(`Failed to fetch artifacts for ${circuitName} from ${baseUrl}. ` +
51
+ `Status: wasm=${wasmRes.status}, zkey=${zkeyRes.status}, vkey=${vkeyRes.status}`);
52
+ }
53
+ wasm = new Uint8Array(await wasmRes.arrayBuffer());
54
+ zkey = new Uint8Array(await zkeyRes.arrayBuffer());
55
+ vkey = await vkeyRes.json();
56
+ }
57
+ catch (error) {
58
+ throw new CoreError(`Failed to fetch artifacts from server for ${circuitName}: ${error.message}`);
59
+ }
60
+ }
61
+ // Node.js environment: try local file paths
62
+ else {
63
+ const wasmCandidates = getLocalArtifactPath(circuitName, ArtifactType.WASM);
64
+ const zkeyCandidates = getLocalArtifactPath(circuitName, ArtifactType.ZKEY);
65
+ const vkeyCandidates = getLocalArtifactPath(circuitName, ArtifactType.VKEY);
66
+ let wasmLoaded = false;
67
+ for (const candidate of wasmCandidates) {
68
+ try {
69
+ wasm = await readBinaryResource(candidate);
70
+ wasmLoaded = true;
71
+ break;
72
+ }
73
+ catch {
74
+ // Try next candidate
75
+ }
76
+ }
77
+ if (!wasmLoaded) {
78
+ throw new CoreError(`Could not find WASM artifact for ${circuitName}. ` +
79
+ `Please run 'pnpm run build:all' in the circuits package.`);
80
+ }
81
+ // Load zkey
82
+ let zkeyLoaded = false;
83
+ for (const candidate of zkeyCandidates) {
84
+ try {
85
+ zkey = await readBinaryResource(candidate);
86
+ zkeyLoaded = true;
87
+ break;
88
+ }
89
+ catch {
90
+ // Try next candidate
91
+ }
92
+ }
93
+ if (!zkeyLoaded) {
94
+ throw new CoreError(`Could not find zkey artifact for ${circuitName}. ` +
95
+ `Please run 'pnpm run build:all' in the circuits package.`);
96
+ }
97
+ // Load vkey
98
+ let vkeyLoaded = false;
99
+ for (const candidate of vkeyCandidates) {
100
+ try {
101
+ const vkeyText = await readTextResource(candidate);
102
+ vkey = JSON.parse(vkeyText);
103
+ vkeyLoaded = true;
104
+ break;
105
+ }
106
+ catch {
107
+ // Try next candidate
108
+ }
109
+ }
110
+ if (!vkeyLoaded) {
111
+ throw new CoreError(`Could not find verification key for ${circuitName}. ` +
112
+ `Please run 'pnpm run build:all' in the circuits package.`);
113
+ }
114
+ }
115
+ // Verify all artifacts were loaded
116
+ if (!wasm || !zkey || !vkey) {
117
+ throw new CoreError(`Failed to load all artifacts for ${circuitName}. ` +
118
+ `Missing: ${!wasm ? "wasm " : ""}${!zkey ? "zkey " : ""}${!vkey ? "vkey" : ""}`);
119
+ }
120
+ // Cache the artifacts for future calls
121
+ // TypeScript now knows these are defined after the check above
122
+ const artifacts = { wasm, zkey, vkey };
123
+ artifactCache[circuitName] = artifacts;
124
+ return artifacts;
125
+ }
126
+ async function readBinaryResource(relativePath) {
127
+ const url = new URL(relativePath, import.meta.url);
128
+ try {
129
+ if (url.protocol === "file:") {
130
+ const fs = (await import("fs")).default;
131
+ const readPromise = new Promise((resolve, reject) => {
132
+ fs.readFile(url.pathname, (err, data) => {
133
+ if (err) {
134
+ reject(err);
135
+ }
136
+ else {
137
+ resolve(data);
138
+ }
139
+ });
140
+ });
141
+ const buf = await readPromise;
142
+ return new Uint8Array(buf);
143
+ }
144
+ const response = await fetch(url);
145
+ if (!response.ok) {
146
+ throw new CoreError(`failed to fetch ${relativePath}: ${response.statusText}`);
147
+ }
148
+ return new Uint8Array(await response.arrayBuffer());
149
+ }
150
+ catch (error) {
151
+ throw new CoreError(`failed to read binary resource ${relativePath}: ${error.message}`);
152
+ }
153
+ }
154
+ async function readTextResource(relativePath) {
155
+ const url = new URL(relativePath, import.meta.url);
156
+ if (url.protocol === "file:") {
157
+ const fs = (await import("fs")).default;
158
+ const readPromise = new Promise((resolve, reject) => {
159
+ fs.readFile(url.pathname, (err, data) => {
160
+ if (err) {
161
+ reject(err);
162
+ }
163
+ else {
164
+ resolve(data);
165
+ }
166
+ });
167
+ });
168
+ const buf = await readPromise;
169
+ return buf.toString("utf-8");
170
+ }
171
+ const response = await fetch(url);
172
+ if (!response.ok) {
173
+ throw new CoreError(`failed to fetch ${relativePath}: ${response.statusText}`);
174
+ }
175
+ return response.text();
176
+ }
177
+ export async function proveTransaction(input) {
178
+ try {
179
+ // Auto-detect circuit based on input dimensions
180
+ const nInputs = input.nullifiers.length;
181
+ const nOutputs = input.commitmentsOut.length;
182
+ const circuit = selectCircuit(nInputs, nOutputs);
183
+ // Fetch circuit artifacts (wasm and zkey)
184
+ const { wasm, vkey, zkey } = await fetchCircuitArtifacts(circuit.name);
185
+ const inputSignals = input;
186
+ const { proof, publicSignals } = await snarkjs.groth16.fullProve(inputSignals, wasm, zkey);
187
+ // Additional verification step (optional)
188
+ const isValid = await snarkjs.groth16.verify(vkey, publicSignals, proof);
189
+ if (!isValid) {
190
+ throw new CoreError("generated proof is invalid");
191
+ }
192
+ return {
193
+ proof,
194
+ publicSignals,
195
+ };
196
+ }
197
+ catch (error) {
198
+ throw new CoreError("proof generation failed: " + error.message);
199
+ }
200
+ }
201
+ /**
202
+ * Get verification key for a specific circuit configuration
203
+ * @param inputs Number of inputs (nullifiers)
204
+ * @param outputs Number of outputs (commitments)
205
+ * @returns Verification key for the circuit
206
+ */
207
+ export async function getVerificationKey(inputs, outputs) {
208
+ try {
209
+ const circuit = selectCircuit(inputs, outputs);
210
+ const { vkey } = await fetchCircuitArtifacts(circuit.name);
211
+ return vkey;
212
+ }
213
+ catch (error) {
214
+ throw new CoreError("failed to load verification key: " + error.message);
215
+ }
216
+ }
217
+ /**
218
+ * Verify a proof with public signals
219
+ * Auto-detects the circuit from the public signals structure
220
+ * @param proof Groth16 proof
221
+ * @param publicSignals Public signals array
222
+ * @returns True if proof is valid
223
+ */
224
+ export async function verifyProof(proof, publicSignals) {
225
+ try {
226
+ // Public signals structure: [merkleRoot, boundParamsHash, ...nullifiers, ...commitments]
227
+ // We need to determine the circuit from the length
228
+ // This is tricky without additional context, so we'll need to try to infer
229
+ // or require explicit circuit specification
230
+ // For now, extract nullifier and commitment counts from the public signals
231
+ // The caller should know this information, but we can make an educated guess
232
+ // based on the total length and circuit registry
233
+ // Length = 2 (root + hash) + nInputs + nOutputs
234
+ const totalLength = publicSignals.length;
235
+ const variableCount = totalLength - 2; // Remove merkleRoot and boundParamsHash
236
+ // Try to find a matching circuit
237
+ let vkey = null;
238
+ for (const circuitKey of SUPPORTED_CIRCUITS) {
239
+ const parts = circuitKey.split("x");
240
+ const config = getCircuitConfig(parseInt(parts[0]), parseInt(parts[1]));
241
+ if (config && config.inputs + config.outputs === variableCount) {
242
+ const artifacts = await fetchCircuitArtifacts(config.name);
243
+ vkey = artifacts.vkey;
244
+ break;
245
+ }
246
+ }
247
+ if (!vkey) {
248
+ throw new CoreError(`Could not determine circuit from ${totalLength} public signals`);
249
+ }
250
+ return await snarkjs.groth16.verify(vkey, publicSignals, proof);
251
+ }
252
+ catch (error) {
253
+ throw new CoreError("proof verification failed: " + error.message);
254
+ }
255
+ }
256
+ /**
257
+ * Verify a proof with explicit circuit specification
258
+ * Preferred method when you know the circuit dimensions
259
+ * @param proof Groth16 proof
260
+ * @param publicSignals Public signals array
261
+ * @param inputs Number of inputs (nullifiers)
262
+ * @param outputs Number of outputs (commitments)
263
+ * @returns True if proof is valid
264
+ */
265
+ export async function verifyProofWithCircuit(proof, publicSignals, inputs, outputs) {
266
+ try {
267
+ const circuit = selectCircuit(inputs, outputs);
268
+ const { vkey } = await fetchCircuitArtifacts(circuit.name);
269
+ return await snarkjs.groth16.verify(vkey, publicSignals, proof);
270
+ }
271
+ catch (error) {
272
+ throw new CoreError("proof verification failed: " + error.message);
273
+ }
274
+ }
@@ -0,0 +1,39 @@
1
+ /**
2
+ * Circuit registry for automatic prover selection
3
+ * Maps circuit dimensions (inputs x outputs) to circuit configurations
4
+ */
5
+ export interface CircuitConfig {
6
+ /** Full circuit name (e.g., "joinsplit_2x3_16") */
7
+ readonly name: string;
8
+ /** Number of inputs (nullifiers) */
9
+ readonly inputs: number;
10
+ /** Number of outputs (commitments) */
11
+ readonly outputs: number;
12
+ /** Merkle tree depth */
13
+ readonly depth: number;
14
+ }
15
+ export type CircuitKey = "3x3" | "2x3" | "1x2" | "1x1";
16
+ /**
17
+ * Registry of available circuits
18
+ * Auto-generated from circuits/circuits.json
19
+ */
20
+ export declare const CIRCUIT_REGISTRY: Record<CircuitKey, CircuitConfig>;
21
+ /**
22
+ * Get list of all supported circuit keys
23
+ */
24
+ export declare const SUPPORTED_CIRCUITS: CircuitKey[];
25
+ /**
26
+ * Get circuit configuration by dimensions
27
+ * @param inputs Number of inputs (nullifiers)
28
+ * @param outputs Number of outputs (commitments)
29
+ * @returns Circuit configuration or undefined if not supported
30
+ */
31
+ export declare function getCircuitConfig(inputs: number, outputs: number): CircuitConfig | undefined;
32
+ /**
33
+ * Check if a circuit configuration is supported
34
+ * @param inputs Number of inputs (nullifiers)
35
+ * @param outputs Number of outputs (commitments)
36
+ * @returns True if supported
37
+ */
38
+ export declare function isCircuitSupported(inputs: number, outputs: number): boolean;
39
+ //# sourceMappingURL=registry.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"registry.d.ts","sourceRoot":"","sources":["../../prover/registry.ts"],"names":[],"mappings":"AAKA;;;GAGG;AAEH,MAAM,WAAW,aAAa;IAC5B,mDAAmD;IACnD,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,oCAAoC;IACpC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,sCAAsC;IACtC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,wBAAwB;IACxB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;CACxB;AAED,MAAM,MAAM,UAAU,GAAG,KAAK,GAAG,KAAK,GAAG,KAAK,GAAG,KAAK,CAAC;AAEvD;;;GAGG;AACH,eAAO,MAAM,gBAAgB,EAAE,MAAM,CAAC,UAAU,EAAE,aAAa,CAyBrD,CAAC;AAEX;;GAEG;AACH,eAAO,MAAM,kBAAkB,EAAoC,UAAU,EAAE,CAAC;AAEhF;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAC9B,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,MAAM,GACd,aAAa,GAAG,SAAS,CAG3B;AAED;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAE3E"}
@@ -0,0 +1,57 @@
1
+ // AUTO-GENERATED FILE - DO NOT EDIT MANUALLY
2
+ // Generated by scripts/generate-registry.ts
3
+ // Source: circuits/circuits.json
4
+ // Generated at: 2025-11-25T13:26:58.576Z
5
+ /**
6
+ * Registry of available circuits
7
+ * Auto-generated from circuits/circuits.json
8
+ */
9
+ export const CIRCUIT_REGISTRY = {
10
+ "3x3": {
11
+ name: "joinsplit_3x3_16",
12
+ inputs: 3,
13
+ outputs: 3,
14
+ depth: 16,
15
+ },
16
+ "2x3": {
17
+ name: "joinsplit_2x3_16",
18
+ inputs: 2,
19
+ outputs: 3,
20
+ depth: 16,
21
+ },
22
+ "1x2": {
23
+ name: "joinsplit_1x2_16",
24
+ inputs: 1,
25
+ outputs: 2,
26
+ depth: 16,
27
+ },
28
+ "1x1": {
29
+ name: "joinsplit_1x1_16",
30
+ inputs: 1,
31
+ outputs: 1,
32
+ depth: 16,
33
+ },
34
+ };
35
+ /**
36
+ * Get list of all supported circuit keys
37
+ */
38
+ export const SUPPORTED_CIRCUITS = Object.keys(CIRCUIT_REGISTRY);
39
+ /**
40
+ * Get circuit configuration by dimensions
41
+ * @param inputs Number of inputs (nullifiers)
42
+ * @param outputs Number of outputs (commitments)
43
+ * @returns Circuit configuration or undefined if not supported
44
+ */
45
+ export function getCircuitConfig(inputs, outputs) {
46
+ const key = `${inputs}x${outputs}`;
47
+ return CIRCUIT_REGISTRY[key];
48
+ }
49
+ /**
50
+ * Check if a circuit configuration is supported
51
+ * @param inputs Number of inputs (nullifiers)
52
+ * @param outputs Number of outputs (commitments)
53
+ * @returns True if supported
54
+ */
55
+ export function isCircuitSupported(inputs, outputs) {
56
+ return getCircuitConfig(inputs, outputs) !== undefined;
57
+ }
@@ -0,0 +1,4 @@
1
+ import type { Storage } from "./types.js";
2
+ export declare const CORE_SCHEMA_VERSION = 1;
3
+ export declare function migrate(storage: Storage): Promise<void>;
4
+ //# sourceMappingURL=schema.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../schema.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAE1C,eAAO,MAAM,mBAAmB,IAAI,CAAC;AAErC,wBAAsB,OAAO,CAAC,OAAO,EAAE,OAAO,iBAW7C"}
package/dist/schema.js ADDED
@@ -0,0 +1,14 @@
1
+ import { SchemaMismatchError } from "./errors.js";
2
+ export const CORE_SCHEMA_VERSION = 1;
3
+ export async function migrate(storage) {
4
+ await storage.open();
5
+ const current = await storage.getSchemaVersion();
6
+ if (current === 0) {
7
+ // seed structures if needed
8
+ await storage.setSchemaVersion(CORE_SCHEMA_VERSION);
9
+ return;
10
+ }
11
+ if (current !== CORE_SCHEMA_VERSION) {
12
+ throw new SchemaMismatchError(current, CORE_SCHEMA_VERSION);
13
+ }
14
+ }
@@ -0,0 +1,12 @@
1
+ import type { Storage } from "../types.js";
2
+ export declare function createCiphertextStore(storage: Storage): {
3
+ /**
4
+ * Store encrypted note payloads alongside their on-chain commitments.
5
+ */
6
+ putCiphertext(chainId: number, index: number, payload: Uint8Array): Promise<void>;
7
+ /**
8
+ * Retrieve encrypted note payloads; returns null when the ciphertext is missing.
9
+ */
10
+ getCiphertext(chainId: number, index: number): Promise<Uint8Array<ArrayBuffer> | null>;
11
+ };
12
+ //# sourceMappingURL=ciphertext-store.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ciphertext-store.d.ts","sourceRoot":"","sources":["../../state/ciphertext-store.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAG3C,wBAAgB,qBAAqB,CAAC,OAAO,EAAE,OAAO;IAElD;;OAEG;2BAC0B,MAAM,SAAS,MAAM,WAAW,UAAU;IAQvE;;OAEG;2BAC0B,MAAM,SAAS,MAAM;EAOrD"}
@@ -0,0 +1,25 @@
1
+ import { keys, validateKey } from "../keys.js";
2
+ import { ensureChainId, ensurePositiveInt } from "../utils/validators.js";
3
+ export function createCiphertextStore(storage) {
4
+ return {
5
+ /**
6
+ * Store encrypted note payloads alongside their on-chain commitments.
7
+ */
8
+ async putCiphertext(chainId, index, payload) {
9
+ ensureChainId(chainId);
10
+ ensurePositiveInt("ciphertext index", index);
11
+ const key = keys.ciphertext(chainId, index);
12
+ validateKey(key);
13
+ await storage.put(key, new Uint8Array(payload));
14
+ },
15
+ /**
16
+ * Retrieve encrypted note payloads; returns null when the ciphertext is missing.
17
+ */
18
+ async getCiphertext(chainId, index) {
19
+ ensureChainId(chainId);
20
+ ensurePositiveInt("ciphertext index", index);
21
+ const data = await storage.get(keys.ciphertext(chainId, index));
22
+ return data ? new Uint8Array(data) : null;
23
+ },
24
+ };
25
+ }
@@ -0,0 +1,3 @@
1
+ export * from "./merkle/index.js";
2
+ export * from "./store/index.js";
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../state/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC;AAClC,cAAc,kBAAkB,CAAC"}