@0xbow/privacy-pools-core-sdk 0.0.0-b21c14ba

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 (115) hide show
  1. package/README.md +73 -0
  2. package/dist/esm/fetchArtifacts.esm-TV4yMbDL.js +18 -0
  3. package/dist/esm/fetchArtifacts.esm-TV4yMbDL.js.map +1 -0
  4. package/dist/esm/fetchArtifacts.node-DC_rCgt3.js +31 -0
  5. package/dist/esm/fetchArtifacts.node-DC_rCgt3.js.map +1 -0
  6. package/dist/esm/index-Bw-RoJy9.js +70373 -0
  7. package/dist/esm/index-Bw-RoJy9.js.map +1 -0
  8. package/dist/esm/index.mjs +7 -0
  9. package/dist/esm/index.mjs.map +1 -0
  10. package/dist/index.d.mts +1222 -0
  11. package/dist/node/fetchArtifacts.esm-i9-c1RlE.js +35 -0
  12. package/dist/node/fetchArtifacts.esm-i9-c1RlE.js.map +1 -0
  13. package/dist/node/fetchArtifacts.node-C5OS73zV.js +48 -0
  14. package/dist/node/fetchArtifacts.node-C5OS73zV.js.map +1 -0
  15. package/dist/node/index-BogHNKL8.js +77423 -0
  16. package/dist/node/index-BogHNKL8.js.map +1 -0
  17. package/dist/node/index.mjs +24 -0
  18. package/dist/node/index.mjs.map +1 -0
  19. package/dist/types/abi/ERC20.d.ts +38 -0
  20. package/dist/types/abi/IEntrypoint.d.ts +823 -0
  21. package/dist/types/abi/IPrivacyPool.d.ts +51 -0
  22. package/dist/types/circuits/circuits.impl.d.ts +120 -0
  23. package/dist/types/circuits/circuits.interface.d.ts +129 -0
  24. package/dist/types/circuits/fetchArtifacts.d.ts +1 -0
  25. package/dist/types/circuits/fetchArtifacts.esm.d.ts +1 -0
  26. package/dist/types/circuits/fetchArtifacts.node.d.ts +1 -0
  27. package/dist/types/circuits/index.d.ts +2 -0
  28. package/dist/types/constants.d.ts +2 -0
  29. package/dist/types/core/account.service.d.ts +345 -0
  30. package/dist/types/core/bruteForce.service.d.ts +61 -0
  31. package/dist/types/core/commitment.service.d.ts +30 -0
  32. package/dist/types/core/contracts.service.d.ts +114 -0
  33. package/dist/types/core/data.service.d.ts +52 -0
  34. package/dist/types/core/sdk.d.ts +45 -0
  35. package/dist/types/core/withdrawal.service.d.ts +32 -0
  36. package/dist/types/crypto.d.ts +67 -0
  37. package/dist/types/dirname.helper.d.ts +2 -0
  38. package/dist/types/errors/account.error.d.ts +10 -0
  39. package/dist/types/errors/base.error.d.ts +53 -0
  40. package/dist/types/errors/data.error.d.ts +7 -0
  41. package/dist/types/errors/events.error.d.ts +9 -0
  42. package/dist/types/exceptions/circuitInitialization.exception.d.ts +3 -0
  43. package/dist/types/exceptions/fetchArtifacts.exception.d.ts +3 -0
  44. package/dist/types/exceptions/index.d.ts +4 -0
  45. package/dist/types/exceptions/invalidRpcUrl.exception.d.ts +3 -0
  46. package/dist/types/exceptions/privacyPool.exception.d.ts +13 -0
  47. package/dist/types/external.d.ts +7 -0
  48. package/dist/types/fetchArtifacts.esm-CwvN5XjW.js +34 -0
  49. package/dist/types/fetchArtifacts.node-VEzDC3k8.js +47 -0
  50. package/dist/types/filename.helper.d.ts +2 -0
  51. package/dist/types/index-CPuQ9H-0.js +77436 -0
  52. package/dist/types/index.d.ts +13 -0
  53. package/dist/types/index.js +23 -0
  54. package/dist/types/interfaces/blockchainProvider.interface.d.ts +12 -0
  55. package/dist/types/interfaces/circuits.interface.d.ts +30 -0
  56. package/dist/types/interfaces/contracts.interface.d.ts +35 -0
  57. package/dist/types/interfaces/index.d.ts +1 -0
  58. package/dist/types/internal.d.ts +6 -0
  59. package/dist/types/keys.d.ts +18 -0
  60. package/dist/types/providers/blockchainProvider.d.ts +8 -0
  61. package/dist/types/providers/index.d.ts +1 -0
  62. package/dist/types/types/account.d.ts +31 -0
  63. package/dist/types/types/commitment.d.ts +48 -0
  64. package/dist/types/types/events.d.ts +72 -0
  65. package/dist/types/types/index.d.ts +3 -0
  66. package/dist/types/types/keys.d.ts +5 -0
  67. package/dist/types/types/withdrawal.d.ts +30 -0
  68. package/dist/types/utils/logger.d.ts +22 -0
  69. package/package.json +81 -0
  70. package/src/abi/ERC20.ts +222 -0
  71. package/src/abi/IEntrypoint.ts +1059 -0
  72. package/src/abi/IPrivacyPool.ts +576 -0
  73. package/src/circuits/circuits.impl.ts +232 -0
  74. package/src/circuits/circuits.interface.ts +166 -0
  75. package/src/circuits/fetchArtifacts.esm.ts +12 -0
  76. package/src/circuits/fetchArtifacts.node.ts +23 -0
  77. package/src/circuits/fetchArtifacts.ts +7 -0
  78. package/src/circuits/index.ts +2 -0
  79. package/src/constants.ts +3 -0
  80. package/src/core/account.service.ts +1093 -0
  81. package/src/core/bruteForce.service.ts +120 -0
  82. package/src/core/commitment.service.ts +84 -0
  83. package/src/core/contracts.service.ts +450 -0
  84. package/src/core/data.service.ts +276 -0
  85. package/src/core/sdk.ts +92 -0
  86. package/src/core/withdrawal.service.ts +126 -0
  87. package/src/crypto.ts +226 -0
  88. package/src/dirname.helper.ts +4 -0
  89. package/src/errors/account.error.ts +49 -0
  90. package/src/errors/base.error.ts +125 -0
  91. package/src/errors/data.error.ts +34 -0
  92. package/src/errors/events.error.ts +38 -0
  93. package/src/exceptions/circuitInitialization.exception.ts +6 -0
  94. package/src/exceptions/fetchArtifacts.exception.ts +7 -0
  95. package/src/exceptions/index.ts +4 -0
  96. package/src/exceptions/invalidRpcUrl.exception.ts +6 -0
  97. package/src/exceptions/privacyPool.exception.ts +19 -0
  98. package/src/external.ts +13 -0
  99. package/src/filename.helper.ts +4 -0
  100. package/src/index.ts +21 -0
  101. package/src/interfaces/blockchainProvider.interface.ts +13 -0
  102. package/src/interfaces/circuits.interface.ts +34 -0
  103. package/src/interfaces/contracts.interface.ts +66 -0
  104. package/src/interfaces/index.ts +1 -0
  105. package/src/internal.ts +6 -0
  106. package/src/keys.ts +42 -0
  107. package/src/providers/blockchainProvider.ts +30 -0
  108. package/src/providers/index.ts +1 -0
  109. package/src/types/account.ts +35 -0
  110. package/src/types/commitment.ts +50 -0
  111. package/src/types/events.ts +82 -0
  112. package/src/types/index.ts +3 -0
  113. package/src/types/keys.ts +6 -0
  114. package/src/types/withdrawal.ts +33 -0
  115. package/src/utils/logger.ts +56 -0
@@ -0,0 +1,276 @@
1
+ import {
2
+ type PublicClient,
3
+ createPublicClient,
4
+ http,
5
+ parseAbiItem,
6
+ } from "viem";
7
+ import {
8
+ ChainConfig,
9
+ DepositEvent,
10
+ WithdrawalEvent,
11
+ RagequitEvent,
12
+ } from "../types/events.js";
13
+ import { PoolInfo } from "../types/account.js";
14
+ import { Hash } from "../types/commitment.js";
15
+ import { Logger } from "../utils/logger.js";
16
+ import { DataError } from "../errors/data.error.js";
17
+ import { ErrorCode } from "../errors/base.error.js";
18
+
19
+ // Event signatures from the contract
20
+ const DEPOSIT_EVENT = parseAbiItem('event Deposited(address indexed _depositor, uint256 _commitment, uint256 _label, uint256 _value, uint256 _merkleRoot)');
21
+ const WITHDRAWAL_EVENT = parseAbiItem('event Withdrawn(address indexed _processooor, uint256 _value, uint256 _spentNullifier, uint256 _newCommitment)');
22
+ const RAGEQUIT_EVENT = parseAbiItem('event Ragequit(address indexed _ragequitter, uint256 _commitment, uint256 _label, uint256 _value)');
23
+
24
+ /**
25
+ * Service responsible for fetching and managing privacy pool events across multiple chains.
26
+ * Handles event retrieval, parsing, and validation for deposits, withdrawals, and ragequits.
27
+ *
28
+ * @remarks
29
+ * This service uses viem's PublicClient to efficiently fetch and process blockchain events.
30
+ * It supports multiple chains and provides robust error handling and validation.
31
+ * All uint256 values from events are handled as bigints, with Hash type assertions for commitment-related fields.
32
+ */
33
+ export class DataService {
34
+ private readonly clients: Map<number, PublicClient> = new Map();
35
+ private readonly logger: Logger;
36
+
37
+ /**
38
+ * Initialize the data service with chain configurations
39
+ *
40
+ * @param chainConfigs - Array of chain configurations containing chainId, RPC URL, and API key
41
+ * @throws {DataError} If client initialization fails for any chain
42
+ */
43
+ constructor(private readonly chainConfigs: ChainConfig[]) {
44
+ this.logger = new Logger({ prefix: "Data" });
45
+
46
+ try {
47
+ for (const config of chainConfigs) {
48
+ if (!config.rpcUrl) {
49
+ throw new Error(`Missing RPC URL for chain ${config.chainId}`);
50
+ }
51
+
52
+ const client = createPublicClient({
53
+ transport: http(config.rpcUrl, {
54
+ timeout: 20_000, // 20 seconds timeout for RPC requests
55
+ retryCount: 3, // Retry up to 3 times on failure
56
+ retryDelay: 1000, // Wait 1 second between retries
57
+ }),
58
+ });
59
+ this.clients.set(config.chainId, client);
60
+ }
61
+ } catch (error) {
62
+ throw new DataError(
63
+ "Failed to initialize PublicClient",
64
+ ErrorCode.NETWORK_ERROR,
65
+ { error: error instanceof Error ? error.message : "Unknown error" },
66
+ );
67
+ }
68
+ }
69
+
70
+ /**
71
+ * Get deposit events for a specific chain
72
+ *
73
+ * @param chainId - Chain ID to fetch events from
74
+ * @param options - Event filter options including fromBlock, toBlock, and other filters
75
+ * @returns Array of deposit events with properly typed fields (bigint for numbers, Hash for commitments)
76
+ * @throws {DataError} If client is not configured, network error occurs, or event data is invalid
77
+ */
78
+ async getDeposits(
79
+ pool: PoolInfo
80
+ ): Promise<DepositEvent[]> {
81
+ try {
82
+ const client = this.getClientForChain(pool.chainId);
83
+ const config = this.getConfigForChain(pool.chainId);
84
+
85
+ const logs = await client.getLogs({
86
+ address: pool.address,
87
+ event: DEPOSIT_EVENT,
88
+ fromBlock: pool.deploymentBlock ?? config.startBlock
89
+ }).catch(error => {
90
+ throw new DataError(
91
+ "Failed to fetch deposit logs",
92
+ ErrorCode.NETWORK_ERROR,
93
+ { error: error instanceof Error ? error.message : "Unknown error" },
94
+ );
95
+ });
96
+
97
+ return logs.map((log) => {
98
+ try {
99
+ if (!log.args) {
100
+ throw DataError.invalidLog("deposit", "missing args");
101
+ }
102
+
103
+ const {
104
+ _depositor: depositor,
105
+ _commitment: commitment,
106
+ _label: label,
107
+ _value: value,
108
+ _merkleRoot: precommitment,
109
+ } = log.args;
110
+
111
+ if (!depositor || !commitment || !label || !precommitment || !log.blockNumber || !log.transactionHash) {
112
+ throw DataError.invalidLog("deposit", "missing required fields");
113
+ }
114
+
115
+ return {
116
+ depositor: depositor.toLowerCase(),
117
+ commitment: commitment as Hash,
118
+ label: label as Hash,
119
+ value: value || BigInt(0),
120
+ precommitment: precommitment as Hash,
121
+ blockNumber: BigInt(log.blockNumber),
122
+ transactionHash: log.transactionHash,
123
+ };
124
+ } catch (error) {
125
+ if (error instanceof DataError) throw error;
126
+ throw DataError.invalidLog("deposit", error instanceof Error ? error.message : "Unknown error");
127
+ }
128
+ });
129
+ } catch (error) {
130
+ if (error instanceof DataError) throw error;
131
+ throw DataError.networkError(pool.chainId, error instanceof Error ? error : new Error(String(error)));
132
+ }
133
+ }
134
+
135
+ /**
136
+ * Get withdrawal events for a specific chain
137
+ *
138
+ * @param chainId - Chain ID to fetch events from
139
+ * @param options - Event filter options including fromBlock, toBlock, and other filters
140
+ * @returns Array of withdrawal events with properly typed fields (bigint for numbers, Hash for commitments)
141
+ * @throws {DataError} If client is not configured, network error occurs, or event data is invalid
142
+ */
143
+ async getWithdrawals(
144
+ pool: PoolInfo,
145
+ fromBlock: bigint = pool.deploymentBlock
146
+ ): Promise<WithdrawalEvent[]> {
147
+ try {
148
+ const client = this.getClientForChain(pool.chainId);
149
+ const config = this.getConfigForChain(pool.chainId);
150
+
151
+ const logs = await client.getLogs({
152
+ address: pool.address,
153
+ event: WITHDRAWAL_EVENT,
154
+ fromBlock: fromBlock ?? config.startBlock,
155
+ }).catch(error => {
156
+ throw new DataError(
157
+ "Failed to fetch withdrawal logs",
158
+ ErrorCode.NETWORK_ERROR,
159
+ { error: error instanceof Error ? error.message : "Unknown error" },
160
+ );
161
+ });
162
+
163
+ return logs.map((log) => {
164
+ try {
165
+ if (!log.args) {
166
+ throw DataError.invalidLog("withdrawal", "missing args");
167
+ }
168
+
169
+ const {
170
+ _value: value,
171
+ _spentNullifier: spentNullifier,
172
+ _newCommitment: newCommitment,
173
+ } = log.args;
174
+
175
+ if (!value || !spentNullifier || !newCommitment || !log.blockNumber || !log.transactionHash) {
176
+ throw DataError.invalidLog("withdrawal", "missing required fields");
177
+ }
178
+
179
+ return {
180
+ withdrawn: value,
181
+ spentNullifier: spentNullifier as Hash,
182
+ newCommitment: newCommitment as Hash,
183
+ blockNumber: BigInt(log.blockNumber),
184
+ transactionHash: log.transactionHash,
185
+ };
186
+ } catch (error) {
187
+ if (error instanceof DataError) throw error;
188
+ throw DataError.invalidLog("withdrawal", error instanceof Error ? error.message : "Unknown error");
189
+ }
190
+ });
191
+ } catch (error) {
192
+ if (error instanceof DataError) throw error;
193
+ throw DataError.networkError(pool.chainId, error instanceof Error ? error : new Error(String(error)));
194
+ }
195
+ }
196
+
197
+ /**
198
+ * Get ragequit events for a specific chain
199
+ *
200
+ * @param chainId - Chain ID to fetch events from
201
+ * @param options - Event filter options including fromBlock, toBlock, and other filters
202
+ * @returns Array of ragequit events with properly typed fields (bigint for numbers, Hash for commitments)
203
+ * @throws {DataError} If client is not configured, network error occurs, or event data is invalid
204
+ */
205
+ async getRagequits(
206
+ pool: PoolInfo,
207
+ fromBlock: bigint = pool.deploymentBlock
208
+ ): Promise<RagequitEvent[]> {
209
+ try {
210
+ const client = this.getClientForChain(pool.chainId);
211
+ const config = this.getConfigForChain(pool.chainId);
212
+
213
+ const logs = await client.getLogs({
214
+ address: pool.address,
215
+ event: RAGEQUIT_EVENT,
216
+ fromBlock: fromBlock ?? config.startBlock,
217
+ }).catch(error => {
218
+ throw new DataError(
219
+ "Failed to fetch ragequit logs",
220
+ ErrorCode.NETWORK_ERROR,
221
+ { error: error instanceof Error ? error.message : "Unknown error" },
222
+ );
223
+ });
224
+
225
+ return logs.map((log) => {
226
+ try {
227
+ if (!log.args) {
228
+ throw DataError.invalidLog("ragequit", "missing args");
229
+ }
230
+
231
+ const {
232
+ _ragequitter: ragequitter,
233
+ _commitment: commitment,
234
+ _label: label,
235
+ _value: value,
236
+ } = log.args;
237
+
238
+ if (!ragequitter || !commitment || !label || !log.blockNumber || !log.transactionHash) {
239
+ throw DataError.invalidLog("ragequit", "missing required fields");
240
+ }
241
+
242
+ return {
243
+ ragequitter: ragequitter.toLowerCase(),
244
+ commitment: commitment as Hash,
245
+ label: label as Hash,
246
+ value: value || BigInt(0),
247
+ blockNumber: BigInt(log.blockNumber),
248
+ transactionHash: log.transactionHash,
249
+ };
250
+ } catch (error) {
251
+ if (error instanceof DataError) throw error;
252
+ throw DataError.invalidLog("ragequit", error instanceof Error ? error.message : "Unknown error");
253
+ }
254
+ });
255
+ } catch (error) {
256
+ if (error instanceof DataError) throw error;
257
+ throw DataError.networkError(pool.chainId, error instanceof Error ? error : new Error(String(error)));
258
+ }
259
+ }
260
+
261
+ private getClientForChain(chainId: number): PublicClient {
262
+ const client = this.clients.get(chainId);
263
+ if (!client) {
264
+ throw DataError.chainNotConfigured(chainId);
265
+ }
266
+ return client;
267
+ }
268
+
269
+ private getConfigForChain(chainId: number): ChainConfig {
270
+ const config = this.chainConfigs.find(c => c.chainId === chainId);
271
+ if (!config) {
272
+ throw DataError.chainNotConfigured(chainId);
273
+ }
274
+ return config;
275
+ }
276
+ }
@@ -0,0 +1,92 @@
1
+ import { CommitmentService } from "./commitment.service.js";
2
+ import { WithdrawalService } from "./withdrawal.service.js";
3
+ import { CircuitsInterface } from "../interfaces/circuits.interface.js";
4
+ import { Commitment, CommitmentProof } from "../types/commitment.js";
5
+ import { WithdrawalProof, WithdrawalProofInput } from "../types/withdrawal.js";
6
+ import { ContractInteractionsService } from "./contracts.service.js";
7
+ import { Hex, Address, Chain } from "viem";
8
+ import { AccountCommitment } from "../types/account.js";
9
+
10
+ /**
11
+ * Main SDK class providing access to all privacy pool functionality.
12
+ * Uses Poseidon hash for all commitment operations.
13
+ */
14
+ export class PrivacyPoolSDK {
15
+ private readonly commitmentService: CommitmentService;
16
+ private readonly withdrawalService: WithdrawalService;
17
+
18
+ constructor(circuits: CircuitsInterface) {
19
+ this.commitmentService = new CommitmentService(circuits);
20
+ this.withdrawalService = new WithdrawalService(circuits);
21
+ }
22
+
23
+ public createContractInstance(
24
+ rpcUrl: string,
25
+ chain: Chain,
26
+ entrypointAddress: Address,
27
+ privateKey: Hex,
28
+ ): ContractInteractionsService {
29
+ return new ContractInteractionsService(
30
+ rpcUrl,
31
+ chain,
32
+ entrypointAddress,
33
+ privateKey,
34
+ );
35
+ }
36
+
37
+ /**
38
+ * Generates a commitment proof.
39
+ *
40
+ * @param value - Value to commit
41
+ * @param label - Label for the commitment
42
+ * @param nullifier - Nullifier for the commitment
43
+ * @param secret - Secret for the commitment
44
+ */
45
+ public async proveCommitment(
46
+ value: bigint,
47
+ label: bigint,
48
+ nullifier: bigint,
49
+ secret: bigint,
50
+ ): Promise<CommitmentProof> {
51
+ return this.commitmentService.proveCommitment(
52
+ value,
53
+ label,
54
+ nullifier,
55
+ secret,
56
+ );
57
+ }
58
+
59
+ /**
60
+ * Verifies a commitment proof.
61
+ *
62
+ * @param proof - The proof to verify
63
+ */
64
+ public async verifyCommitment(proof: CommitmentProof): Promise<boolean> {
65
+ return this.commitmentService.verifyCommitment(proof);
66
+ }
67
+
68
+ /**
69
+ * Generates a withdrawal proof.
70
+ *
71
+ * @param commitment - Commitment to withdraw
72
+ * @param input - Input parameters for the withdrawal
73
+ * @param withdrawal - Withdrawal details
74
+ */
75
+ public async proveWithdrawal(
76
+ commitment: Commitment | AccountCommitment ,
77
+ input: WithdrawalProofInput,
78
+ ): Promise<WithdrawalProof> {
79
+ return await this.withdrawalService.proveWithdrawal(commitment, input);
80
+ }
81
+
82
+ /**
83
+ * Verifies a withdrawal proof.
84
+ *
85
+ * @param withdrawalProof - The withdrawal payload to verify
86
+ */
87
+ public async verifyWithdrawal(
88
+ withdrawalProof: WithdrawalProof,
89
+ ): Promise<boolean> {
90
+ return this.withdrawalService.verifyWithdrawal(withdrawalProof);
91
+ }
92
+ }
@@ -0,0 +1,126 @@
1
+ import * as snarkjs from "snarkjs";
2
+ import { ProofError } from "../errors/base.error.js";
3
+ import {
4
+ CircuitName,
5
+ CircuitsInterface,
6
+ } from "../interfaces/circuits.interface.js";
7
+ import { WithdrawalProof, WithdrawalProofInput } from "../types/withdrawal.js";
8
+ import { AccountCommitment, Commitment } from "../index.js";
9
+
10
+ /**
11
+ * Service responsible for handling withdrawal-related operations.
12
+ */
13
+ export class WithdrawalService {
14
+ constructor(private readonly circuits: CircuitsInterface) {}
15
+
16
+ /**
17
+ * Generates a withdrawal proof.
18
+ *
19
+ * @param commitment - Commitment to withdraw
20
+ * @param input - Input parameters for the withdrawal
21
+ * @param withdrawal - Withdrawal details
22
+ * @returns Promise resolving to withdrawal payload
23
+ * @throws {ProofError} If proof generation fails
24
+ */
25
+ public async proveWithdrawal(
26
+ commitment: Commitment | AccountCommitment,
27
+ input: WithdrawalProofInput
28
+ ): Promise<WithdrawalProof> {
29
+ try {
30
+ const inputSignals = this.prepareInputSignals(commitment, input);
31
+
32
+ const wasm = await this.circuits.getWasm(CircuitName.Withdraw);
33
+ const zkey = await this.circuits.getProvingKey(CircuitName.Withdraw);
34
+
35
+ const { proof, publicSignals } = await snarkjs.groth16.fullProve(
36
+ inputSignals,
37
+ wasm,
38
+ zkey,
39
+ );
40
+
41
+ return {
42
+ proof,
43
+ publicSignals,
44
+ };
45
+ } catch (error) {
46
+ throw ProofError.generationFailed({
47
+ error: error instanceof Error ? error.message : "Unknown error",
48
+ });
49
+ }
50
+ }
51
+
52
+ /**
53
+ * Verifies a withdrawal proof.
54
+ *
55
+ * @param withdrawalPayload - The withdrawal payload to verify
56
+ * @returns Promise resolving to boolean indicating proof validity
57
+ * @throws {ProofError} If verification fails
58
+ */
59
+ public async verifyWithdrawal(
60
+ withdrawalPayload: WithdrawalProof,
61
+ ): Promise<boolean> {
62
+ try {
63
+ const vkeyBin = await this.circuits.getVerificationKey(
64
+ CircuitName.Withdraw,
65
+ );
66
+ const vkey = JSON.parse(new TextDecoder("utf-8").decode(vkeyBin));
67
+ return await snarkjs.groth16.verify(
68
+ vkey,
69
+ withdrawalPayload.publicSignals,
70
+ withdrawalPayload.proof,
71
+ );
72
+ } catch (error) {
73
+ throw ProofError.verificationFailed({
74
+ error: error instanceof Error ? error.message : "Unknown error",
75
+ });
76
+ }
77
+ }
78
+
79
+ /**
80
+ * Prepares input signals for the withdrawal circuit.
81
+ */
82
+ private prepareInputSignals(
83
+ commitment: Commitment | AccountCommitment,
84
+ input: WithdrawalProofInput
85
+ ): Record<string, bigint | bigint[] | string> {
86
+ let existingValue: bigint;
87
+ let existingNullifier: bigint;
88
+ let existingSecret: bigint;
89
+ let label: bigint;
90
+ if ("preimage" in commitment) {
91
+ existingValue = commitment.preimage.value;
92
+ existingNullifier = commitment.preimage.precommitment.nullifier;
93
+ existingSecret = commitment.preimage.precommitment.secret;
94
+ label = commitment.preimage.label;
95
+ } else {
96
+ existingValue = commitment.value;
97
+ existingNullifier = commitment.nullifier;
98
+ existingSecret = commitment.secret;
99
+ label = commitment.label;
100
+ }
101
+
102
+ return {
103
+ // Public signals
104
+ withdrawnValue: input.withdrawalAmount,
105
+ stateRoot: input.stateRoot,
106
+ stateTreeDepth: input.stateTreeDepth,
107
+ ASPRoot: input.aspRoot,
108
+ ASPTreeDepth: input.aspTreeDepth,
109
+ context: input.context,
110
+
111
+ // Private signals
112
+ label,
113
+ existingValue,
114
+ existingNullifier,
115
+ existingSecret,
116
+ newNullifier: input.newNullifier,
117
+ newSecret: input.newSecret,
118
+
119
+ // Merkle Proofs
120
+ stateSiblings: input.stateMerkleProof.siblings,
121
+ stateIndex: BigInt(input.stateMerkleProof.index),
122
+ ASPSiblings: input.aspMerkleProof.siblings,
123
+ ASPIndex: BigInt(input.aspMerkleProof.index),
124
+ };
125
+ }
126
+ }