@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
package/src/crypto.ts ADDED
@@ -0,0 +1,226 @@
1
+ import { mnemonicToAccount } from "viem/accounts";
2
+ import { bytesToNumber } from "viem/utils";
3
+ import { poseidon } from "maci-crypto/build/ts/hashing.js";
4
+ import { LeanIMT, LeanIMTMerkleProof } from "@zk-kit/lean-imt";
5
+ import {
6
+ ErrorCode,
7
+ PrivacyPoolError,
8
+ } from "./exceptions/privacyPool.exception.js";
9
+ import {
10
+ Commitment,
11
+ Hash,
12
+ Secret,
13
+ Withdrawal,
14
+ MasterKeys,
15
+ } from "./types/index.js";
16
+ import { encodeAbiParameters, Hex, keccak256, numberToHex } from "viem";
17
+ import { SNARK_SCALAR_FIELD } from "./constants.js";
18
+
19
+ /**
20
+ * Validates that a bigint value is not zero
21
+ * @param value The value to check
22
+ * @param name The name of the value for the error message
23
+ * @throws {PrivacyPoolError} If the value is zero
24
+ */
25
+ function validateNonZero(value: bigint, name: string) {
26
+ if (value === BigInt(0)) {
27
+ throw new PrivacyPoolError(
28
+ ErrorCode.INVALID_VALUE,
29
+ `Invalid input: '${name}' cannot be zero.`,
30
+ );
31
+ }
32
+ }
33
+
34
+ /**
35
+ * Generates two master keys based on some provided seed or a random value.
36
+ *
37
+ * @param {Hex} seed - The optional seed.
38
+ * @returns {MasterKeys} The master key pair.
39
+ */
40
+ export function generateMasterKeys(mnemonic: string): MasterKeys {
41
+ if (!mnemonic) {
42
+ throw new PrivacyPoolError(
43
+ ErrorCode.INVALID_VALUE,
44
+ "Invalid input: mnemonic phrase is required."
45
+ );
46
+ }
47
+
48
+ const key1 = bytesToNumber(
49
+ mnemonicToAccount(mnemonic, { accountIndex: 0 }).getHdKey().privateKey!,
50
+ );
51
+
52
+ const key2 = bytesToNumber(
53
+ mnemonicToAccount(mnemonic, { accountIndex: 1 }).getHdKey().privateKey!,
54
+ );
55
+
56
+ const masterNullifier = poseidon([BigInt(key1)]) as Secret;
57
+ const masterSecret = poseidon([BigInt(key2)]) as Secret;
58
+
59
+ return { masterNullifier, masterSecret };
60
+ }
61
+
62
+ /**
63
+ * Generates a nullifier and secret pair for a deposit commitment.
64
+ *
65
+ * @param {MasterKeys} keys - The master keys pair.
66
+ * @param {Hash} scope - The pool scope.
67
+ * @param {bigint} index - The pool account index for the scope.
68
+ * @returns {Secret, Secret} The commitment nullifier and secret pair.
69
+ */
70
+ export function generateDepositSecrets(
71
+ keys: MasterKeys,
72
+ scope: Hash,
73
+ index: bigint,
74
+ ): { nullifier: Secret; secret: Secret } {
75
+ const nullifier = poseidon([keys.masterNullifier, scope, index]) as Secret;
76
+ const secret = poseidon([keys.masterSecret, scope, index]) as Secret;
77
+
78
+ return { nullifier, secret };
79
+ }
80
+
81
+ /**
82
+ * Generates a nullifier and secret pair for a withdrawal commitment.
83
+ *
84
+ * @param {MasterKeys} keys - The master keys pair.
85
+ * @param {Hash} label - The deposit commitment label.
86
+ * @param {bigint} index - The withdrawal index for the pool account.
87
+ * @returns {Secret, Secret} The commitment nullifier and secret pair.
88
+ */
89
+ export function generateWithdrawalSecrets(
90
+ keys: MasterKeys,
91
+ label: Hash,
92
+ index: bigint,
93
+ ): { nullifier: Secret; secret: Secret } {
94
+ const nullifier = poseidon([keys.masterNullifier, label, index]) as Secret;
95
+ const secret = poseidon([keys.masterSecret, label, index]) as Secret;
96
+
97
+ return { nullifier, secret };
98
+ }
99
+
100
+ /**
101
+ * Computes a Poseidon hash for the given nullifier and secret.
102
+ *
103
+ * @param {Secret} nullifier - The nullifier to hash.
104
+ * @param {Secret} secret - The secret to hash.
105
+ * @returns {Hash} The Poseidon hash.
106
+ */
107
+ export function hashPrecommitment(nullifier: Secret, secret: Secret): Hash {
108
+ return poseidon([nullifier, secret]) as Hash;
109
+ }
110
+
111
+ /**
112
+ * Generates a commitment using the given parameters.
113
+ *
114
+ * @param {bigint} value - The value associated with the commitment.
115
+ * @param {bigint} label - The label used for the commitment.
116
+ * @param {Secret} nullifier - The nullifier used in the precommitment.
117
+ * @param {Secret} secret - The secret used in the precommitment.
118
+ * @returns {Commitment} The generated commitment object.
119
+ */
120
+ export function getCommitment(
121
+ value: bigint,
122
+ label: bigint,
123
+ nullifier: Secret,
124
+ secret: Secret,
125
+ ): Commitment {
126
+ validateNonZero(nullifier as bigint, "nullifier");
127
+ validateNonZero(label, "label");
128
+ validateNonZero(secret as bigint, "secret");
129
+
130
+ const precommitment = {
131
+ hash: hashPrecommitment(nullifier, secret),
132
+ nullifier,
133
+ secret,
134
+ };
135
+
136
+ const hash = poseidon([value, label, precommitment.hash]) as Hash;
137
+
138
+ return {
139
+ hash,
140
+ nullifierHash: precommitment.hash,
141
+ preimage: {
142
+ value,
143
+ label,
144
+ precommitment,
145
+ },
146
+ };
147
+ }
148
+
149
+ /**
150
+ * Generates a Merkle inclusion proof for a given leaf in a set of leaves.
151
+ *
152
+ * @param {bigint[]} leaves - Array of leaves for the Lean Incremental Merkle tree.
153
+ * @param {bigint} leaf - The specific leaf to generate the inclusion proof for.
154
+ * @returns {LeanIMTMerkleProof<bigint>} A lean incremental Merkle tree inclusion proof.
155
+ * @throws {Error} If the leaf is not found in the leaves array.
156
+ */
157
+ export function generateMerkleProof(
158
+ leaves: bigint[],
159
+ leaf: bigint,
160
+ ): LeanIMTMerkleProof<bigint> {
161
+ const tree = new LeanIMT<bigint>((a: bigint, b: bigint) => poseidon([a, b]));
162
+
163
+ tree.insertMany(leaves);
164
+
165
+ const leafIndex = tree.indexOf(leaf);
166
+
167
+ // if leaf does not exist in tree, throw error
168
+ if (leafIndex === -1) {
169
+ throw new PrivacyPoolError(
170
+ ErrorCode.MERKLE_ERROR,
171
+ "Leaf not found in the leaves array.",
172
+ );
173
+ }
174
+
175
+ const proof = tree.generateProof(leafIndex);
176
+
177
+ if (proof.siblings.length < 32) {
178
+ proof.siblings = [
179
+ ...proof.siblings,
180
+ ...Array(32 - proof.siblings.length).fill(BigInt(0)),
181
+ ];
182
+ }
183
+
184
+ return proof;
185
+ }
186
+
187
+ export function bigintToHash(value: bigint): Hash {
188
+ return `0x${value.toString(16).padStart(64, "0")}` as unknown as Hash;
189
+ }
190
+
191
+ export function bigintToHex(num: bigint | string | undefined): Hex {
192
+ if (num === undefined) throw new Error("Undefined bigint value!");
193
+ return `0x${BigInt(num).toString(16).padStart(64, "0")}`;
194
+ }
195
+
196
+ /**
197
+ * Calculates the context hash for a withdrawal.
198
+ */
199
+ export function calculateContext(withdrawal: Withdrawal, scope: Hash): string {
200
+ const hash =
201
+ BigInt(
202
+ keccak256(
203
+ encodeAbiParameters(
204
+ [
205
+ {
206
+ name: "withdrawal",
207
+ type: "tuple",
208
+ components: [
209
+ { name: "processooor", type: "address" },
210
+ { name: "data", type: "bytes" },
211
+ ],
212
+ },
213
+ { name: "scope", type: "uint256" },
214
+ ],
215
+ [
216
+ {
217
+ processooor: withdrawal.processooor,
218
+ data: withdrawal.data,
219
+ },
220
+ scope,
221
+ ],
222
+ ),
223
+ ),
224
+ ) % SNARK_SCALAR_FIELD;
225
+ return numberToHex(hash);
226
+ }
@@ -0,0 +1,4 @@
1
+ import { dirname } from "path";
2
+ import __filename from "./filename.helper.js";
3
+
4
+ export default dirname(__filename);
@@ -0,0 +1,49 @@
1
+ import { ErrorCode, SDKError } from "./base.error.js";
2
+ import { Hash } from "../types/commitment.js";
3
+
4
+ export class AccountError extends SDKError {
5
+ constructor(
6
+ message: string,
7
+ code: ErrorCode = ErrorCode.OPERATION_FAILED,
8
+ details?: Record<string, unknown>,
9
+ ) {
10
+ super(message, code, details);
11
+ this.name = "AccountError";
12
+ }
13
+
14
+ public static commitmentNotFound(hash: Hash | string): AccountError {
15
+ const hashStr = typeof hash === 'string' ? hash : hash.toString();
16
+ return new AccountError(
17
+ `No account found for commitment ${hashStr}`,
18
+ ErrorCode.INVALID_INPUT,
19
+ );
20
+ }
21
+
22
+ public static invalidPoolAccount(): AccountError {
23
+ return new AccountError(
24
+ "Invalid pool account state",
25
+ ErrorCode.INVALID_INPUT,
26
+ );
27
+ }
28
+
29
+ public static accountInitializationFailed(reason: string): AccountError {
30
+ return new AccountError(
31
+ `Failed to initialize account: ${reason}`,
32
+ ErrorCode.OPERATION_FAILED,
33
+ );
34
+ }
35
+
36
+ public static duplicatePools(scope: bigint): AccountError {
37
+ return new AccountError(
38
+ `Duplicate pools found for scope: ${scope.toString()}`,
39
+ ErrorCode.INVALID_INPUT,
40
+ );
41
+ }
42
+
43
+ public static invalidIndex(index: bigint): AccountError {
44
+ return new AccountError(
45
+ `Invalid index: ${index.toString()}`,
46
+ ErrorCode.INVALID_INPUT,
47
+ );
48
+ }
49
+ }
@@ -0,0 +1,125 @@
1
+ /**
2
+ * Unified error codes for the SDK.
3
+ */
4
+ export enum ErrorCode {
5
+ // Base errors
6
+ UNKNOWN = "UNKNOWN",
7
+ INVALID_INPUT = "INVALID_INPUT",
8
+ OPERATION_FAILED = "OPERATION_FAILED",
9
+ NETWORK_ERROR = "NETWORK_ERROR",
10
+
11
+ // Proof errors
12
+ PROOF_GENERATION_FAILED = "PROOF_GENERATION_FAILED",
13
+ PROOF_VERIFICATION_FAILED = "PROOF_VERIFICATION_FAILED",
14
+ INVALID_PROOF = "INVALID_PROOF",
15
+ INVALID_PUBLIC_SIGNALS = "INVALID_PUBLIC_SIGNALS",
16
+ CIRCUIT_ERROR = "CIRCUIT_ERROR",
17
+
18
+ // Contract errors
19
+ CONTRACT_ERROR = "CONTRACT_ERROR",
20
+
21
+ // Crypto errors
22
+ CRYPTO_ERROR = "CRYPTO_ERROR",
23
+ MERKLE_ERROR = "MERKLE_ERROR",
24
+ }
25
+
26
+ /**
27
+ * Base error class for the SDK.
28
+ * All other error classes should extend this.
29
+ */
30
+ export class SDKError extends Error {
31
+ constructor(
32
+ message: string,
33
+ public readonly code: ErrorCode = ErrorCode.UNKNOWN,
34
+ public readonly details?: Record<string, unknown>,
35
+ ) {
36
+ super(message);
37
+ this.name = this.constructor.name;
38
+
39
+ // Maintains proper stack trace
40
+ Error.captureStackTrace(this, this.constructor);
41
+ }
42
+
43
+ /**
44
+ * Creates a JSON representation of the error.
45
+ */
46
+ public toJSON(): Record<string, unknown> {
47
+ return {
48
+ name: this.name,
49
+ message: this.message,
50
+ code: this.code,
51
+ details: this.details,
52
+ stack: this.stack,
53
+ };
54
+ }
55
+ }
56
+
57
+ /**
58
+ * Specialized error class for proof-related operations.
59
+ */
60
+ export class ProofError extends SDKError {
61
+ constructor(
62
+ message: string,
63
+ code: ErrorCode = ErrorCode.PROOF_GENERATION_FAILED,
64
+ details?: Record<string, unknown>,
65
+ ) {
66
+ super(message, code, details);
67
+ }
68
+
69
+ /**
70
+ * Creates an error for proof generation failures.
71
+ */
72
+ public static generationFailed(
73
+ details?: Record<string, unknown>,
74
+ ): ProofError {
75
+ return new ProofError(
76
+ "Failed to generate proof",
77
+ ErrorCode.PROOF_GENERATION_FAILED,
78
+ details,
79
+ );
80
+ }
81
+
82
+ /**
83
+ * Creates an error for proof verification failures.
84
+ */
85
+ public static verificationFailed(
86
+ details?: Record<string, unknown>,
87
+ ): ProofError {
88
+ return new ProofError(
89
+ "Failed to verify proof",
90
+ ErrorCode.PROOF_VERIFICATION_FAILED,
91
+ details,
92
+ );
93
+ }
94
+
95
+ /**
96
+ * Creates an error for invalid proof format.
97
+ */
98
+ public static invalidProof(details?: Record<string, unknown>): ProofError {
99
+ return new ProofError(
100
+ "Invalid proof format",
101
+ ErrorCode.INVALID_PROOF,
102
+ details,
103
+ );
104
+ }
105
+ }
106
+
107
+ export class ContractError extends SDKError {
108
+ constructor(
109
+ message: string,
110
+ code: ErrorCode = ErrorCode.CONTRACT_ERROR,
111
+ details?: Record<string, unknown>,
112
+ ) {
113
+ super(message, code);
114
+ this.name = "ContractError";
115
+ }
116
+
117
+ public static scopeNotFound(scope: bigint): ContractError {
118
+ return new ContractError(`No pool found for scope ${scope.toString()}`, ErrorCode.CONTRACT_ERROR);
119
+ }
120
+
121
+ public static assetNotFound(address: string): ContractError {
122
+ return new ContractError(`Asset ${address} has no pool`, ErrorCode.CONTRACT_ERROR);
123
+ }
124
+
125
+ }
@@ -0,0 +1,34 @@
1
+ import { ErrorCode, SDKError } from "./base.error.js";
2
+
3
+ export class DataError extends SDKError {
4
+ constructor(
5
+ message: string,
6
+ code: ErrorCode = ErrorCode.NETWORK_ERROR,
7
+ details?: Record<string, unknown>,
8
+ ) {
9
+ super(message, code, details);
10
+ this.name = "DataError";
11
+ }
12
+
13
+ public static invalidLog(type: string, reason: string): DataError {
14
+ return new DataError(
15
+ `Invalid ${type} log: ${reason}`,
16
+ ErrorCode.INVALID_INPUT,
17
+ );
18
+ }
19
+
20
+ public static chainNotConfigured(chainId: number): DataError {
21
+ return new DataError(
22
+ `No configuration found for chain ID ${chainId}`,
23
+ ErrorCode.INVALID_INPUT,
24
+ );
25
+ }
26
+
27
+ public static networkError(chainId: number, error: Error): DataError {
28
+ return new DataError(
29
+ `Network error on chain ${chainId}: ${error.message}`,
30
+ ErrorCode.NETWORK_ERROR,
31
+ { originalError: error },
32
+ );
33
+ }
34
+ }
@@ -0,0 +1,38 @@
1
+ import { ErrorCode } from "./base.error.js";
2
+ import { DataError } from "./data.error.js";
3
+ import { Hash } from "../types/commitment.js";
4
+
5
+ export class EventError extends DataError {
6
+ constructor(
7
+ message: string,
8
+ code: ErrorCode = ErrorCode.NETWORK_ERROR,
9
+ details?: Record<string, unknown>,
10
+ ) {
11
+ super(message, code, details);
12
+ this.name = "EventError";
13
+ }
14
+
15
+ public static depositEventError(chainId: number, scope: Hash, error: Error): EventError {
16
+ return new EventError(
17
+ `Error fetching deposit events for chain ${chainId}: ${error.message}`,
18
+ ErrorCode.NETWORK_ERROR,
19
+ { originalError: error, scope },
20
+ );
21
+ }
22
+
23
+ public static withdrawalEventError(chainId: number, scope: Hash, error: Error): EventError {
24
+ return new EventError(
25
+ `Error fetching withdrawal events for chain ${chainId}: ${error.message}`,
26
+ ErrorCode.NETWORK_ERROR,
27
+ { originalError: error, scope },
28
+ );
29
+ }
30
+
31
+ public static ragequitEventError(chainId: number, scope: Hash, error: Error): EventError {
32
+ return new EventError(
33
+ `Error fetching ragequit events for chain ${chainId}: ${error.message}`,
34
+ ErrorCode.NETWORK_ERROR,
35
+ { originalError: error, scope },
36
+ );
37
+ }
38
+ }
@@ -0,0 +1,6 @@
1
+ export class CircuitInitialization extends Error {
2
+ constructor(message: string) {
3
+ super(`There was an error initializing the circuits: ${message}`);
4
+ this.name = "CircuitInitialization";
5
+ }
6
+ }
@@ -0,0 +1,7 @@
1
+ export class FetchArtifact extends Error {
2
+ constructor(artifact: URL) {
3
+ const message = `Encountered error while loading artifact at ${artifact.toString()}.\nIf web, make sure assets are hosted from /artifacts.\nIf Node, make sure the assets were bundled correctly at dist/node/artifacts/`;
4
+ super(message);
5
+ this.name = "FetchArtifact";
6
+ }
7
+ }
@@ -0,0 +1,4 @@
1
+ export * from "./invalidRpcUrl.exception.js";
2
+ export * from "./fetchArtifacts.exception.js";
3
+ export * from "./circuitInitialization.exception.js";
4
+ export * from "./privacyPool.exception.js";
@@ -0,0 +1,6 @@
1
+ export class InvalidRpcUrl extends Error {
2
+ constructor(url: string) {
3
+ super(`${url} is invalid`);
4
+ this.name = "InvalidRpcUrl";
5
+ }
6
+ }
@@ -0,0 +1,19 @@
1
+ export enum ErrorCode {
2
+ INVALID_COMMITMENT = "INVALID_COMMITMENT",
3
+ INVALID_MERKLE_PROOF = "INVALID_MERKLE_PROOF",
4
+ INVALID_NULLIFIER = "INVALID_NULLIFIER",
5
+ INVALID_SECRET = "INVALID_SECRET",
6
+ INVALID_VALUE = "INVALID_VALUE",
7
+ INVALID_LABEL = "INVALID_LABEL",
8
+ MERKLE_ERROR = "MERKLE_ERROR",
9
+ }
10
+
11
+ export class PrivacyPoolError extends Error {
12
+ constructor(
13
+ public code: ErrorCode,
14
+ message: string,
15
+ ) {
16
+ super(message);
17
+ this.name = "PrivacyPoolError";
18
+ }
19
+ }
@@ -0,0 +1,13 @@
1
+ export type { IBlockchainProvider } from "./internal.js";
2
+
3
+ export { InvalidRpcUrl } from "./internal.js";
4
+
5
+ export { BlockchainProvider } from "./internal.js";
6
+
7
+ export { Circuits } from "./circuits/index.js";
8
+
9
+ export { ContractInteractionsService } from "./core/contracts.service.js";
10
+
11
+ // This file is for re-exporting external dependencies that need to be available to consumers
12
+ export type { LeanIMTMerkleProof } from "@zk-kit/lean-imt";
13
+ export type { Address } from "viem";
@@ -0,0 +1,4 @@
1
+ import { fileURLToPath } from "url";
2
+
3
+ const __filename = fileURLToPath(import.meta.url);
4
+ export default __filename;
package/src/index.ts ADDED
@@ -0,0 +1,21 @@
1
+ export * from "./types/index.js";
2
+ export * from "./crypto.js";
3
+ export * from "./external.js";
4
+ export { PrivacyPoolSDK } from "./core/sdk.js";
5
+
6
+ // Additional Types (not included in types/index.js)
7
+ export * from "./types/account.js";
8
+ export * from "./types/events.js";
9
+
10
+ // Errors
11
+ export * from "./errors/base.error.js";
12
+ export * from "./errors/account.error.js";
13
+
14
+ // Interfaces
15
+ export * from "./interfaces/circuits.interface.js";
16
+
17
+ // Services (exported for advanced usage)
18
+ export { CommitmentService } from "./core/commitment.service.js";
19
+ export { WithdrawalService } from "./core/withdrawal.service.js";
20
+ export { AccountService } from "./core/account.service.js";
21
+ export { DataService } from "./core/data.service.js";
@@ -0,0 +1,13 @@
1
+ import { Address } from "../internal.js";
2
+
3
+ /**
4
+ * Represents an interface for a blockchain provider.
5
+ */
6
+ export interface IBlockchainProvider {
7
+ /**
8
+ * Retrieves the balance of the specified address.
9
+ * @param {Address} address The address for which to retrieve the balance.
10
+ * @returns {Promise<bigint>} A Promise that resolves to the balance of the address.
11
+ */
12
+ getBalance(address: Address): Promise<bigint>;
13
+ }
@@ -0,0 +1,34 @@
1
+ /**
2
+ * Available circuit types in the system.
3
+ */
4
+ export enum CircuitName {
5
+ Commitment = "commitment",
6
+ Withdraw = "withdraw",
7
+ }
8
+
9
+ /**
10
+ * Type for circuit input signals.
11
+ */
12
+ export type CircuitSignals = {
13
+ [key: string]: bigint | bigint[] | string;
14
+ };
15
+
16
+ /**
17
+ * Interface for accessing circuit-related resources.
18
+ */
19
+ export interface CircuitsInterface {
20
+ /**
21
+ * Gets the WASM binary for a circuit.
22
+ */
23
+ getWasm(name: CircuitName): Promise<Uint8Array>;
24
+
25
+ /**
26
+ * Gets the proving key for a circuit.
27
+ */
28
+ getProvingKey(name: CircuitName): Promise<Uint8Array>;
29
+
30
+ /**
31
+ * Gets the verification key for a circuit.
32
+ */
33
+ getVerificationKey(name: CircuitName): Promise<Uint8Array>;
34
+ }
@@ -0,0 +1,66 @@
1
+ import { Address } from "viem";
2
+ import { Withdrawal, WithdrawalProof } from "../types/withdrawal.js";
3
+ import { CommitmentProof, Hash } from "../types/commitment.js";
4
+
5
+ export interface SolidityGroth16Proof {
6
+ pA: [bigint, bigint];
7
+ pB: [[bigint, bigint], [bigint, bigint]];
8
+ pC: [bigint, bigint];
9
+ pubSignals: bigint[];
10
+ }
11
+
12
+ export interface AssetConfig {
13
+ pool: Address,
14
+ minimumDepositAmount: bigint,
15
+ vettingFeeBPS: bigint,
16
+ maxRelayFeeBPS: bigint
17
+ }
18
+
19
+ export interface TransactionResponse {
20
+ hash: string;
21
+ wait: () => Promise<void>;
22
+ }
23
+
24
+ export interface ContractInteractions {
25
+ depositERC20(
26
+ asset: Address,
27
+ amount: bigint,
28
+ precommitment: bigint,
29
+ ): Promise<TransactionResponse>;
30
+
31
+ depositETH(
32
+ amount: bigint,
33
+ precommitment: bigint,
34
+ ): Promise<TransactionResponse>;
35
+
36
+ withdraw(
37
+ withdrawal: Withdrawal,
38
+ withdrawalProof: WithdrawalProof,
39
+ scope: Hash,
40
+ ): Promise<TransactionResponse>;
41
+
42
+ relay(
43
+ withdrawal: Withdrawal,
44
+ withdrawalProof: WithdrawalProof,
45
+ scope: Hash,
46
+ ): Promise<TransactionResponse>;
47
+
48
+ ragequit(
49
+ commitmentProof: CommitmentProof,
50
+ privacyPoolAddress: Address,
51
+ ): Promise<TransactionResponse>;
52
+
53
+ getScope(privacyPoolAddress: Address): Promise<bigint>;
54
+ getStateRoot(privacyPoolAddress: Address): Promise<bigint>;
55
+ getStateSize(privacyPoolAddress: Address): Promise<bigint>;
56
+ getAssetConfig(assetAddress: Address): Promise<AssetConfig>;
57
+ getScopeData(
58
+ scope: bigint,
59
+ ): Promise<{ poolAddress: Address | null; assetAddress: Address | null }>;
60
+
61
+ approveERC20(
62
+ spenderAddress: Address,
63
+ tokenAddress: Address,
64
+ amount: bigint,
65
+ ): Promise<TransactionResponse>;
66
+ }
@@ -0,0 +1 @@
1
+ export * from "./blockchainProvider.interface.js";