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

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 (97) hide show
  1. package/README.md +73 -0
  2. package/dist/esm/ccip-nJye9Itm.js +166 -0
  3. package/dist/esm/ccip-nJye9Itm.js.map +1 -0
  4. package/dist/esm/index-DAWUECi8.js +84043 -0
  5. package/dist/esm/index-DAWUECi8.js.map +1 -0
  6. package/dist/esm/index.mjs +4 -0
  7. package/dist/esm/index.mjs.map +1 -0
  8. package/dist/index.d.mts +666 -0
  9. package/dist/node/ccip-lPhPeJab.js +183 -0
  10. package/dist/node/ccip-lPhPeJab.js.map +1 -0
  11. package/dist/node/index-BiU5Ef8Z.js +90625 -0
  12. package/dist/node/index-BiU5Ef8Z.js.map +1 -0
  13. package/dist/node/index.mjs +21 -0
  14. package/dist/node/index.mjs.map +1 -0
  15. package/dist/types/abi/ERC20.d.ts +38 -0
  16. package/dist/types/abi/IEntrypoint.d.ts +794 -0
  17. package/dist/types/abi/IPrivacyPool.d.ts +51 -0
  18. package/dist/types/ccip-BZzz1Y5w.js +182 -0
  19. package/dist/types/circuits/circuits.impl.d.ts +108 -0
  20. package/dist/types/circuits/circuits.interface.d.ts +127 -0
  21. package/dist/types/circuits/index.d.ts +2 -0
  22. package/dist/types/constants.d.ts +2 -0
  23. package/dist/types/core/bruteForce.service.d.ts +61 -0
  24. package/dist/types/core/commitment.service.d.ts +30 -0
  25. package/dist/types/core/contracts.service.d.ts +106 -0
  26. package/dist/types/core/sdk.d.ts +44 -0
  27. package/dist/types/core/withdrawal.service.d.ts +32 -0
  28. package/dist/types/crypto.d.ts +45 -0
  29. package/dist/types/dirname.helper.d.ts +2 -0
  30. package/dist/types/errors/base.error.d.ts +52 -0
  31. package/dist/types/exceptions/circuitInitialization.exception.d.ts +3 -0
  32. package/dist/types/exceptions/fetchArtifacts.exception.d.ts +3 -0
  33. package/dist/types/exceptions/index.d.ts +4 -0
  34. package/dist/types/exceptions/invalidRpcUrl.exception.d.ts +3 -0
  35. package/dist/types/exceptions/privacyPool.exception.d.ts +13 -0
  36. package/dist/types/external.d.ts +7 -0
  37. package/dist/types/filename.helper.d.ts +2 -0
  38. package/dist/types/index-D-_1h8-n.js +90638 -0
  39. package/dist/types/index.d.ts +10 -0
  40. package/dist/types/index.js +20 -0
  41. package/dist/types/interfaces/blockchainProvider.interface.d.ts +12 -0
  42. package/dist/types/interfaces/circuits.interface.d.ts +30 -0
  43. package/dist/types/interfaces/contracts.interface.d.ts +28 -0
  44. package/dist/types/interfaces/index.d.ts +1 -0
  45. package/dist/types/internal.d.ts +6 -0
  46. package/dist/types/providers/blockchainProvider.d.ts +8 -0
  47. package/dist/types/providers/index.d.ts +1 -0
  48. package/dist/types/types/commitment.d.ts +48 -0
  49. package/dist/types/types/index.d.ts +2 -0
  50. package/dist/types/types/withdrawal.d.ts +30 -0
  51. package/package.json +81 -0
  52. package/src/abi/ERC20.ts +222 -0
  53. package/src/abi/IEntrypoint.ts +1059 -0
  54. package/src/abi/IPrivacyPool.ts +576 -0
  55. package/src/circuits/circuits.impl.ts +232 -0
  56. package/src/circuits/circuits.interface.ts +166 -0
  57. package/src/circuits/fetchArtifacts.esm.ts +12 -0
  58. package/src/circuits/fetchArtifacts.node.ts +23 -0
  59. package/src/circuits/fetchArtifacts.ts +7 -0
  60. package/src/circuits/index.ts +2 -0
  61. package/src/constants.ts +3 -0
  62. package/src/core/account.service.ts +1077 -0
  63. package/src/core/bruteForce.service.ts +120 -0
  64. package/src/core/commitment.service.ts +84 -0
  65. package/src/core/contracts.service.ts +442 -0
  66. package/src/core/data.service.ts +272 -0
  67. package/src/core/sdk.ts +92 -0
  68. package/src/core/withdrawal.service.ts +126 -0
  69. package/src/crypto.ts +226 -0
  70. package/src/dirname.helper.ts +4 -0
  71. package/src/errors/account.error.ts +49 -0
  72. package/src/errors/base.error.ts +125 -0
  73. package/src/errors/data.error.ts +34 -0
  74. package/src/errors/events.error.ts +38 -0
  75. package/src/exceptions/circuitInitialization.exception.ts +6 -0
  76. package/src/exceptions/fetchArtifacts.exception.ts +7 -0
  77. package/src/exceptions/index.ts +4 -0
  78. package/src/exceptions/invalidRpcUrl.exception.ts +6 -0
  79. package/src/exceptions/privacyPool.exception.ts +19 -0
  80. package/src/external.ts +13 -0
  81. package/src/filename.helper.ts +4 -0
  82. package/src/index.ts +21 -0
  83. package/src/interfaces/blockchainProvider.interface.ts +13 -0
  84. package/src/interfaces/circuits.interface.ts +34 -0
  85. package/src/interfaces/contracts.interface.ts +66 -0
  86. package/src/interfaces/index.ts +1 -0
  87. package/src/internal.ts +6 -0
  88. package/src/keys.ts +42 -0
  89. package/src/providers/blockchainProvider.ts +26 -0
  90. package/src/providers/index.ts +1 -0
  91. package/src/types/account.ts +35 -0
  92. package/src/types/commitment.ts +50 -0
  93. package/src/types/events.ts +82 -0
  94. package/src/types/index.ts +3 -0
  95. package/src/types/keys.ts +6 -0
  96. package/src/types/withdrawal.ts +33 -0
  97. package/src/utils/logger.ts +56 -0
@@ -0,0 +1,6 @@
1
+ export type { Address } from "viem";
2
+ export * from "./exceptions/index.js";
3
+ export * from "./interfaces/index.js";
4
+ export * from "./providers/index.js";
5
+ export * from "./circuits/index.js";
6
+ export * from "./constants.js";
package/src/keys.ts ADDED
@@ -0,0 +1,42 @@
1
+ import { poseidon } from "maci-crypto/build/ts/hashing.js";
2
+ import { Hash, Secret } from "./types/index.js";
3
+ import { Hex } from "viem";
4
+ import { generatePrivateKey } from "viem/accounts";
5
+
6
+ export function genMasterKeys(seed?: Hex): [Secret, Secret] {
7
+ const preimage = seed ? poseidon([BigInt(seed)]) : BigInt(generatePrivateKey());
8
+
9
+ const masterKey1 = poseidon([preimage, BigInt(1)]) as Secret;
10
+ const masterKey2 = poseidon([preimage, BigInt(2)]) as Secret;
11
+
12
+ return [masterKey1, masterKey2];
13
+ }
14
+
15
+ /**
16
+ * Computes a Poseidon hash for the given nullifier and secret.
17
+ *
18
+ * @param {Secret} nullifier - The nullifier to hash.
19
+ * @param {Secret} secret - The secret to hash.
20
+ * @returns {Hash} The Poseidon hash.
21
+ */
22
+ export function getDepositSecrets(
23
+ masterKey: [Secret, Secret],
24
+ scope: Hash,
25
+ index: bigint,
26
+ ): { nullifier: Secret; secret: Secret } {
27
+ const depositNullifier = poseidon([masterKey[0], scope, index]) as Secret;
28
+ const depositSecret = poseidon([masterKey[1], scope, index]) as Secret;
29
+
30
+ return { nullifier: depositNullifier, secret: depositSecret };
31
+ }
32
+
33
+ export function getWithdrawalSecrets(
34
+ masterKey: [Secret, Secret],
35
+ label: Hash,
36
+ index: bigint,
37
+ ): { nullifier: Secret; secret: Secret } {
38
+ const withdrawalNullifier = poseidon([masterKey[0], label, index]) as Secret;
39
+ const withdrawalSecret = poseidon([masterKey[1], label, index]) as Secret;
40
+
41
+ return { nullifier: withdrawalNullifier, secret: withdrawalSecret };
42
+ }
@@ -0,0 +1,26 @@
1
+ import type { Address, Chain, HttpTransport } from "viem";
2
+ import { createPublicClient, http } from "viem";
3
+ import { mainnet } from "viem/chains";
4
+
5
+ import { IBlockchainProvider, InvalidRpcUrl } from "../internal.js";
6
+
7
+ export class BlockchainProvider implements IBlockchainProvider {
8
+ private client: ReturnType<typeof createPublicClient<HttpTransport, Chain>>;
9
+
10
+ constructor(rpcUrl: string) {
11
+ // dummy check for the rpcUrl
12
+ if (!rpcUrl || !rpcUrl.startsWith("http")) {
13
+ throw new InvalidRpcUrl(rpcUrl);
14
+ }
15
+
16
+ this.client = createPublicClient({
17
+ chain: mainnet,
18
+ transport: http(rpcUrl),
19
+ });
20
+ }
21
+
22
+ /** @inheritdoc */
23
+ async getBalance(address: Address): Promise<bigint> {
24
+ return this.client.getBalance({ address });
25
+ }
26
+ }
@@ -0,0 +1 @@
1
+ export * from "./blockchainProvider.js";
@@ -0,0 +1,35 @@
1
+ import { Hash, Secret } from "./commitment.js";
2
+ import { Hex } from "viem";
3
+ import { RagequitEvent } from "./events.js";
4
+
5
+ export interface PoolAccount {
6
+ label: Hash;
7
+ deposit: AccountCommitment;
8
+ children: AccountCommitment[];
9
+ ragequit?: RagequitEvent
10
+ }
11
+
12
+ export interface AccountCommitment {
13
+ hash: Hash;
14
+ value: bigint;
15
+ label: Hash;
16
+ nullifier: Secret;
17
+ secret: Secret;
18
+ blockNumber: bigint;
19
+ timestamp?: bigint;
20
+ txHash: Hex;
21
+ }
22
+
23
+ export interface PrivacyPoolAccount {
24
+ masterKeys: [masterNullifier: Secret, masterSecret: Secret];
25
+ poolAccounts: Map<Hash, PoolAccount[]>;
26
+ creationTimestamp?: bigint;
27
+ lastUpdateTimestamp?: bigint;
28
+ }
29
+
30
+ export interface PoolInfo {
31
+ chainId: number;
32
+ address: Hex;
33
+ scope: Hash;
34
+ deploymentBlock: bigint;
35
+ }
@@ -0,0 +1,50 @@
1
+ import { Groth16Proof, PublicSignals } from "snarkjs";
2
+
3
+ /**
4
+ * Represents a hash value in the system.
5
+ * This is a branded type to ensure type safety.
6
+ */
7
+ export type Hash = bigint & { readonly __brand: unique symbol };
8
+
9
+ /**
10
+ * Represents a secret value in the system.
11
+ * This is a branded type to ensure type safety.
12
+ */
13
+ export type Secret = bigint & { readonly __brand: unique symbol };
14
+
15
+ /**
16
+ * Represents a precommitment structure containing the hash, nullifier, and secret.
17
+ * All hashes are computed using Poseidon.
18
+ */
19
+ export interface Precommitment {
20
+ readonly hash: Hash;
21
+ readonly nullifier: Secret;
22
+ readonly secret: Secret;
23
+ }
24
+
25
+ /**
26
+ * Represents the preimage of a commitment containing the value, label, and precommitment.
27
+ */
28
+ export interface CommitmentPreimage {
29
+ readonly value: bigint;
30
+ readonly label: bigint;
31
+ readonly precommitment: Precommitment;
32
+ }
33
+
34
+ /**
35
+ * Represents a complete commitment structure.
36
+ * All hashes are computed using Poseidon.
37
+ */
38
+ export interface Commitment {
39
+ readonly hash: Hash;
40
+ readonly nullifierHash: Hash;
41
+ readonly preimage: CommitmentPreimage;
42
+ }
43
+
44
+ /**
45
+ * Represents the result of a commitment proof operation.
46
+ */
47
+ export interface CommitmentProof {
48
+ readonly proof: Groth16Proof;
49
+ readonly publicSignals: PublicSignals;
50
+ }
@@ -0,0 +1,82 @@
1
+ import { Address, Hex } from "viem";
2
+ import { Hash } from "./commitment.js";
3
+
4
+ /**
5
+ * Represents a deposit event from a privacy pool
6
+ */
7
+ export interface DepositEvent {
8
+ depositor: string;
9
+ commitment: Hash;
10
+ label: Hash;
11
+ value: bigint;
12
+ precommitment: Hash;
13
+ blockNumber: bigint;
14
+ transactionHash: Hex;
15
+ }
16
+
17
+ /**
18
+ * Represents a withdrawal event from a privacy pool
19
+ */
20
+ export interface WithdrawalEvent {
21
+ withdrawn: bigint;
22
+ spentNullifier: Hash;
23
+ newCommitment: Hash;
24
+ blockNumber: bigint;
25
+ transactionHash: Hex;
26
+ }
27
+
28
+ /**
29
+ * Represents a ragequit event from a privacy pool
30
+ */
31
+ export interface RagequitEvent {
32
+ ragequitter: string;
33
+ commitment: Hash;
34
+ label: Hash;
35
+ value: bigint;
36
+ blockNumber: bigint;
37
+ transactionHash: Hex;
38
+ }
39
+
40
+ /**
41
+ * Configuration for a chain's data provider
42
+ */
43
+ export interface ChainConfig {
44
+ chainId: number;
45
+ privacyPoolAddress: Address;
46
+ startBlock: bigint;
47
+ rpcUrl: string;
48
+ }
49
+
50
+ /**
51
+ * Event filter options
52
+ */
53
+ export interface EventFilterOptions {
54
+ fromBlock?: bigint;
55
+ toBlock?: bigint;
56
+ depositor?: string;
57
+ limit?: number;
58
+ skip?: number;
59
+ }
60
+
61
+ /**
62
+ * Collection of pool events
63
+ */
64
+ export interface PoolEvents {
65
+ deposits: DepositEvent[];
66
+ withdrawals: WithdrawalEvent[];
67
+ }
68
+
69
+ export interface PoolEventsSuccess {
70
+ depositEvents: Map<Hash, DepositEvent>;
71
+ withdrawalEvents: Map<Hash, WithdrawalEvent>;
72
+ ragequitEvents: Map<Hash, RagequitEvent>;
73
+ }
74
+
75
+ export interface PoolEventsError {
76
+ reason: string;
77
+ scope: Hash;
78
+ }
79
+
80
+ export type PoolEventsResult = Map<Hash, PoolEventsSuccess | PoolEventsError>;
81
+
82
+ export type ProcessedDepositEventsResult = Map<Hash, DepositEvent>;
@@ -0,0 +1,3 @@
1
+ export * from "./commitment.js";
2
+ export * from "./withdrawal.js";
3
+ export * from "./keys.js";
@@ -0,0 +1,6 @@
1
+ import { Secret } from "./commitment.js";
2
+
3
+ export interface MasterKeys {
4
+ masterNullifier: Secret;
5
+ masterSecret: Secret;
6
+ }
@@ -0,0 +1,33 @@
1
+ import { Address, Hex } from "viem";
2
+ import { Groth16Proof, PublicSignals } from "snarkjs";
3
+ import { LeanIMTMerkleProof } from "@zk-kit/lean-imt";
4
+ import { Hash, Secret } from "./commitment.js";
5
+
6
+ /**
7
+ * Represents a withdrawal request in the system.
8
+ */
9
+ export interface Withdrawal {
10
+ readonly processooor: Address;
11
+ readonly data: Hex;
12
+ }
13
+
14
+ export interface WithdrawalProof {
15
+ readonly proof: Groth16Proof;
16
+ readonly publicSignals: PublicSignals;
17
+ }
18
+
19
+ /**
20
+ * Input parameters required for withdrawal proof generation.
21
+ */
22
+ export interface WithdrawalProofInput {
23
+ readonly context: bigint;
24
+ readonly withdrawalAmount: bigint;
25
+ readonly stateMerkleProof: LeanIMTMerkleProof<bigint>;
26
+ readonly aspMerkleProof: LeanIMTMerkleProof<bigint>;
27
+ readonly stateRoot: Hash;
28
+ readonly stateTreeDepth: bigint;
29
+ readonly aspRoot: Hash;
30
+ readonly aspTreeDepth: bigint;
31
+ readonly newSecret: Secret;
32
+ readonly newNullifier: Secret;
33
+ }
@@ -0,0 +1,56 @@
1
+ export enum LogLevel {
2
+ DEBUG = 0,
3
+ INFO = 1,
4
+ WARN = 2,
5
+ ERROR = 3,
6
+ }
7
+
8
+ export interface LoggerOptions {
9
+ level?: LogLevel;
10
+ prefix?: string;
11
+ enabled?: boolean;
12
+ }
13
+
14
+ export class Logger {
15
+ private level: LogLevel;
16
+ private prefix: string;
17
+ private enabled: boolean;
18
+
19
+ constructor(options: LoggerOptions = {}) {
20
+ this.level = options.level ?? LogLevel.INFO;
21
+ this.prefix = options.prefix ?? "";
22
+ this.enabled = options.enabled ?? true;
23
+ }
24
+
25
+ private formatMessage(level: string, message: string): string {
26
+ return `${new Date().toISOString()} [${this.prefix}::${level}] ${message}`;
27
+ }
28
+
29
+ debug(message: string, ...args: unknown[]): void {
30
+ if (this.enabled && this.level <= LogLevel.DEBUG) {
31
+ console.debug(this.formatMessage("DEBUG", message), ...args);
32
+ }
33
+ }
34
+
35
+ info(message: string, ...args: unknown[]): void {
36
+ if (this.enabled && this.level <= LogLevel.INFO) {
37
+ console.log(this.formatMessage("INFO", message), ...args);
38
+ }
39
+ }
40
+
41
+ warn(message: string, ...args: unknown[]): void {
42
+ if (this.enabled && this.level <= LogLevel.WARN) {
43
+ console.warn(this.formatMessage("WARN", message), ...args);
44
+ }
45
+ }
46
+
47
+ error(message: string, error?: Error, ...args: unknown[]): void {
48
+ if (this.enabled && this.level <= LogLevel.ERROR) {
49
+ console.error(
50
+ this.formatMessage("ERROR", message),
51
+ error?.stack ?? "",
52
+ ...args,
53
+ );
54
+ }
55
+ }
56
+ }