@0xbow/privacy-pools-core-sdk 0.1.17 → 0.1.19

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 (37) hide show
  1. package/dist/esm/{fetchArtifacts.esm-pBY20mFG.js → fetchArtifacts.esm-DYA5xgwo.js} +2 -2
  2. package/dist/esm/{fetchArtifacts.esm-pBY20mFG.js.map → fetchArtifacts.esm-DYA5xgwo.js.map} +1 -1
  3. package/dist/esm/{fetchArtifacts.node-B2yyjJ3A.js → fetchArtifacts.node-nt928XQR.js} +2 -2
  4. package/dist/esm/{fetchArtifacts.node-B2yyjJ3A.js.map → fetchArtifacts.node-nt928XQR.js.map} +1 -1
  5. package/dist/esm/{index-Bce8vsoP.js → index-IxAruGRY.js} +365 -51
  6. package/dist/esm/index-IxAruGRY.js.map +1 -0
  7. package/dist/esm/index.mjs +1 -1
  8. package/dist/index.d.mts +169 -46
  9. package/dist/node/{fetchArtifacts.esm-CPhwfL6_.js → fetchArtifacts.esm-DkDPFXn4.js} +2 -2
  10. package/dist/node/{fetchArtifacts.esm-CPhwfL6_.js.map → fetchArtifacts.esm-DkDPFXn4.js.map} +1 -1
  11. package/dist/node/{fetchArtifacts.node-BnY4WJme.js → fetchArtifacts.node-C3SnDq3C.js} +2 -2
  12. package/dist/node/{fetchArtifacts.node-BnY4WJme.js.map → fetchArtifacts.node-C3SnDq3C.js.map} +1 -1
  13. package/dist/node/{index-CFy_zYT2.js → index-D8Hrpc7M.js} +365 -51
  14. package/dist/node/index-D8Hrpc7M.js.map +1 -0
  15. package/dist/node/index.mjs +1 -1
  16. package/dist/types/abi/IEntrypoint.d.ts +13 -13
  17. package/dist/types/core/account.service.d.ts +116 -4
  18. package/dist/types/core/sdk.d.ts +2 -1
  19. package/dist/types/core/withdrawal.service.d.ts +2 -2
  20. package/dist/types/errors/account.error.d.ts +2 -0
  21. package/dist/types/errors/events.error.d.ts +9 -0
  22. package/dist/types/{fetchArtifacts.esm-BtNsuoIK.js → fetchArtifacts.esm-4yQJboFd.js} +1 -1
  23. package/dist/types/{fetchArtifacts.node-DyFnCB2A.js → fetchArtifacts.node-C5d2dtAy.js} +1 -1
  24. package/dist/types/{index-ai6P7NYa.js → index-CdgQB9Pw.js} +364 -50
  25. package/dist/types/index.js +1 -1
  26. package/dist/types/types/events.d.ts +11 -1
  27. package/package.json +1 -1
  28. package/src/abi/IEntrypoint.ts +15 -15
  29. package/src/core/account.service.ts +463 -66
  30. package/src/core/data.service.ts +2 -3
  31. package/src/core/sdk.ts +2 -1
  32. package/src/core/withdrawal.service.ts +25 -9
  33. package/src/errors/account.error.ts +14 -0
  34. package/src/errors/events.error.ts +38 -0
  35. package/src/types/events.ts +15 -1
  36. package/dist/esm/index-Bce8vsoP.js.map +0 -1
  37. package/dist/node/index-CFy_zYT2.js.map +0 -1
@@ -1,4 +1,4 @@
1
- import { F as FetchArtifact } from './index-Bce8vsoP.js';
1
+ import { F as FetchArtifact } from './index-IxAruGRY.js';
2
2
  import 'viem/accounts';
3
3
  import 'buffer';
4
4
  import 'assert';
@@ -15,4 +15,4 @@ async function fetchVersionedArtifact(artifactUrl) {
15
15
  }
16
16
 
17
17
  export { fetchVersionedArtifact };
18
- //# sourceMappingURL=fetchArtifacts.esm-pBY20mFG.js.map
18
+ //# sourceMappingURL=fetchArtifacts.esm-DYA5xgwo.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"fetchArtifacts.esm-pBY20mFG.js","sources":["../../src/circuits/fetchArtifacts.esm.ts"],"sourcesContent":[null],"names":[],"mappings":";;;;;;;AAEO,eAAe,sBAAsB,CAC1C,WAAgB,EAAA;AAEhB,IAAA,MAAM,GAAG,GAAG,MAAM,KAAK,CAAC,WAAW,CAAC;AACpC,IAAA,IAAI,GAAG,CAAC,MAAM,KAAK,GAAG,EAAE;AACtB,QAAA,MAAM,IAAI,aAAa,CAAC,WAAW,CAAC;;AAEtC,IAAA,MAAM,IAAI,GAAG,MAAM,GAAG,CAAC,WAAW,EAAE;AACpC,IAAA,OAAO,IAAI,UAAU,CAAC,IAAI,CAAC;AAC7B;;;;"}
1
+ {"version":3,"file":"fetchArtifacts.esm-DYA5xgwo.js","sources":["../../src/circuits/fetchArtifacts.esm.ts"],"sourcesContent":[null],"names":[],"mappings":";;;;;;;AAEO,eAAe,sBAAsB,CAC1C,WAAgB,EAAA;AAEhB,IAAA,MAAM,GAAG,GAAG,MAAM,KAAK,CAAC,WAAW,CAAC;AACpC,IAAA,IAAI,GAAG,CAAC,MAAM,KAAK,GAAG,EAAE;AACtB,QAAA,MAAM,IAAI,aAAa,CAAC,WAAW,CAAC;;AAEtC,IAAA,MAAM,IAAI,GAAG,MAAM,GAAG,CAAC,WAAW,EAAE;AACpC,IAAA,OAAO,IAAI,UAAU,CAAC,IAAI,CAAC;AAC7B;;;;"}
@@ -1,4 +1,4 @@
1
- import { F as FetchArtifact } from './index-Bce8vsoP.js';
1
+ import { F as FetchArtifact } from './index-IxAruGRY.js';
2
2
  import 'viem/accounts';
3
3
  import 'buffer';
4
4
  import 'assert';
@@ -28,4 +28,4 @@ async function fetchVersionedArtifact(artifactUrl) {
28
28
  }
29
29
 
30
30
  export { fetchVersionedArtifact };
31
- //# sourceMappingURL=fetchArtifacts.node-B2yyjJ3A.js.map
31
+ //# sourceMappingURL=fetchArtifacts.node-nt928XQR.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"fetchArtifacts.node-B2yyjJ3A.js","sources":["../../src/circuits/fetchArtifacts.node.ts"],"sourcesContent":[null],"names":[],"mappings":";;;;;;;AAEO,eAAe,sBAAsB,CAC1C,WAAgB,EAAA;AAEhB,IAAA,IAAI;QACF,MAAM,EAAE,GAAG,CAAC,MAAM,OAAO,IAAI,CAAC,EAAE,OAAO;QACvC,MAAM,WAAW,GAAoB,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,KAAI;AACnE,YAAA,EAAE,CAAC,QAAQ,CAAC,WAAW,CAAC,QAAQ,EAAE,CAAC,GAAG,EAAE,IAAI,KAAI;gBAC9C,IAAI,GAAG,EAAE;oBACP,MAAM,CAAC,GAAG,CAAC;;qBACN;oBACL,OAAO,CAAC,IAAI,CAAC;;AAEjB,aAAC,CAAC;AACJ,SAAC,CAAC;AACF,QAAA,MAAM,GAAG,GAAG,MAAM,WAAW;AAC7B,QAAA,OAAO,IAAI,UAAU,CAAC,GAAG,CAAC;;IAC1B,OAAO,KAAK,EAAE;AACd,QAAA,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC;AACpB,QAAA,MAAM,IAAI,aAAa,CAAC,WAAW,CAAC;;AAExC;;;;"}
1
+ {"version":3,"file":"fetchArtifacts.node-nt928XQR.js","sources":["../../src/circuits/fetchArtifacts.node.ts"],"sourcesContent":[null],"names":[],"mappings":";;;;;;;AAEO,eAAe,sBAAsB,CAC1C,WAAgB,EAAA;AAEhB,IAAA,IAAI;QACF,MAAM,EAAE,GAAG,CAAC,MAAM,OAAO,IAAI,CAAC,EAAE,OAAO;QACvC,MAAM,WAAW,GAAoB,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,KAAI;AACnE,YAAA,EAAE,CAAC,QAAQ,CAAC,WAAW,CAAC,QAAQ,EAAE,CAAC,GAAG,EAAE,IAAI,KAAI;gBAC9C,IAAI,GAAG,EAAE;oBACP,MAAM,CAAC,GAAG,CAAC;;qBACN;oBACL,OAAO,CAAC,IAAI,CAAC;;AAEjB,aAAC,CAAC;AACJ,SAAC,CAAC;AACF,QAAA,MAAM,GAAG,GAAG,MAAM,WAAW;AAC7B,QAAA,OAAO,IAAI,UAAU,CAAC,GAAG,CAAC;;IAC1B,OAAO,KAAK,EAAE;AACd,QAAA,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC;AACpB,QAAA,MAAM,IAAI,aAAa,CAAC,WAAW,CAAC;;AAExC;;;;"}
@@ -45815,10 +45815,10 @@ const circuitToAsset = {
45815
45815
 
45816
45816
  async function importFetchVersionedArtifact(isBrowser) {
45817
45817
  if (isBrowser) {
45818
- return import('./fetchArtifacts.esm-pBY20mFG.js');
45818
+ return import('./fetchArtifacts.esm-DYA5xgwo.js');
45819
45819
  }
45820
45820
  else {
45821
- return import('./fetchArtifacts.node-B2yyjJ3A.js');
45821
+ return import('./fetchArtifacts.node-nt928XQR.js');
45822
45822
  }
45823
45823
  }
45824
45824
 
@@ -46098,9 +46098,9 @@ const IEntrypointABI = [
46098
46098
  "internalType": "uint256"
46099
46099
  },
46100
46100
  {
46101
- "name": "ipfsHash",
46102
- "type": "bytes32",
46103
- "internalType": "bytes32"
46101
+ "name": "ipfsCID",
46102
+ "type": "string",
46103
+ "internalType": "string"
46104
46104
  },
46105
46105
  {
46106
46106
  "name": "timestamp",
@@ -46497,9 +46497,9 @@ const IEntrypointABI = [
46497
46497
  "internalType": "uint256"
46498
46498
  },
46499
46499
  {
46500
- "name": "_ipfsHash",
46501
- "type": "bytes32",
46502
- "internalType": "bytes32"
46500
+ "name": "_ipfsCID",
46501
+ "type": "string",
46502
+ "internalType": "string"
46503
46503
  }
46504
46504
  ],
46505
46505
  "outputs": [
@@ -46815,10 +46815,10 @@ const IEntrypointABI = [
46815
46815
  "internalType": "uint256"
46816
46816
  },
46817
46817
  {
46818
- "name": "_ipfsHash",
46819
- "type": "bytes32",
46818
+ "name": "_ipfsCID",
46819
+ "type": "string",
46820
46820
  "indexed": false,
46821
- "internalType": "bytes32"
46821
+ "internalType": "string"
46822
46822
  },
46823
46823
  {
46824
46824
  "name": "_timestamp",
@@ -46937,11 +46937,6 @@ const IEntrypointABI = [
46937
46937
  "name": "ERC1967NonPayable",
46938
46938
  "inputs": []
46939
46939
  },
46940
- {
46941
- "type": "error",
46942
- "name": "EmptyIPFSHash",
46943
- "inputs": []
46944
- },
46945
46940
  {
46946
46941
  "type": "error",
46947
46942
  "name": "EmptyRoot",
@@ -46962,6 +46957,11 @@ const IEntrypointABI = [
46962
46957
  "name": "InvalidFeeBPS",
46963
46958
  "inputs": []
46964
46959
  },
46960
+ {
46961
+ "type": "error",
46962
+ "name": "InvalidIPFSCIDLength",
46963
+ "inputs": []
46964
+ },
46965
46965
  {
46966
46966
  "type": "error",
46967
46967
  "name": "InvalidIndex",
@@ -47068,7 +47068,7 @@ const IEntrypointABI = [
47068
47068
  "type": "error",
47069
47069
  "name": "ZeroAddress",
47070
47070
  "inputs": []
47071
- },
47071
+ }
47072
47072
  ];
47073
47073
 
47074
47074
  const IPrivacyPoolABI = [
@@ -69145,6 +69145,22 @@ class WithdrawalService {
69145
69145
  * Prepares input signals for the withdrawal circuit.
69146
69146
  */
69147
69147
  prepareInputSignals(commitment, input) {
69148
+ let existingValue;
69149
+ let existingNullifier;
69150
+ let existingSecret;
69151
+ let label;
69152
+ if ("preimage" in commitment) {
69153
+ existingValue = commitment.preimage.value;
69154
+ existingNullifier = commitment.preimage.precommitment.nullifier;
69155
+ existingSecret = commitment.preimage.precommitment.secret;
69156
+ label = commitment.preimage.label;
69157
+ }
69158
+ else {
69159
+ existingValue = commitment.value;
69160
+ existingNullifier = commitment.nullifier;
69161
+ existingSecret = commitment.secret;
69162
+ label = commitment.label;
69163
+ }
69148
69164
  return {
69149
69165
  // Public signals
69150
69166
  withdrawnValue: input.withdrawalAmount,
@@ -69154,10 +69170,10 @@ class WithdrawalService {
69154
69170
  ASPTreeDepth: input.aspTreeDepth,
69155
69171
  context: input.context,
69156
69172
  // Private signals
69157
- label: commitment.preimage.label,
69158
- existingValue: commitment.preimage.value,
69159
- existingNullifier: commitment.preimage.precommitment.nullifier,
69160
- existingSecret: commitment.preimage.precommitment.secret,
69173
+ label,
69174
+ existingValue,
69175
+ existingNullifier,
69176
+ existingSecret,
69161
69177
  newNullifier: input.newNullifier,
69162
69178
  newSecret: input.newSecret,
69163
69179
  // Merkle Proofs
@@ -69237,6 +69253,12 @@ class AccountError extends SDKError {
69237
69253
  static accountInitializationFailed(reason) {
69238
69254
  return new AccountError(`Failed to initialize account: ${reason}`, ErrorCode.OPERATION_FAILED);
69239
69255
  }
69256
+ static duplicatePools(scope) {
69257
+ return new AccountError(`Duplicate pools found for scope: ${scope.toString()}`, ErrorCode.INVALID_INPUT);
69258
+ }
69259
+ static invalidIndex(index) {
69260
+ return new AccountError(`Invalid index: ${index.toString()}`, ErrorCode.INVALID_INPUT);
69261
+ }
69240
69262
  }
69241
69263
 
69242
69264
  var LogLevel;
@@ -69280,6 +69302,38 @@ class Logger {
69280
69302
  }
69281
69303
  }
69282
69304
 
69305
+ class DataError extends SDKError {
69306
+ constructor(message, code = ErrorCode.NETWORK_ERROR, details) {
69307
+ super(message, code, details);
69308
+ this.name = "DataError";
69309
+ }
69310
+ static invalidLog(type, reason) {
69311
+ return new DataError(`Invalid ${type} log: ${reason}`, ErrorCode.INVALID_INPUT);
69312
+ }
69313
+ static chainNotConfigured(chainId) {
69314
+ return new DataError(`No configuration found for chain ID ${chainId}`, ErrorCode.INVALID_INPUT);
69315
+ }
69316
+ static networkError(chainId, error) {
69317
+ return new DataError(`Network error on chain ${chainId}: ${error.message}`, ErrorCode.NETWORK_ERROR, { originalError: error });
69318
+ }
69319
+ }
69320
+
69321
+ class EventError extends DataError {
69322
+ constructor(message, code = ErrorCode.NETWORK_ERROR, details) {
69323
+ super(message, code, details);
69324
+ this.name = "EventError";
69325
+ }
69326
+ static depositEventError(chainId, scope, error) {
69327
+ return new EventError(`Error fetching deposit events for chain ${chainId}: ${error.message}`, ErrorCode.NETWORK_ERROR, { originalError: error, scope });
69328
+ }
69329
+ static withdrawalEventError(chainId, scope, error) {
69330
+ return new EventError(`Error fetching withdrawal events for chain ${chainId}: ${error.message}`, ErrorCode.NETWORK_ERROR, { originalError: error, scope });
69331
+ }
69332
+ static ragequitEventError(chainId, scope, error) {
69333
+ return new EventError(`Error fetching ragequit events for chain ${chainId}: ${error.message}`, ErrorCode.NETWORK_ERROR, { originalError: error, scope });
69334
+ }
69335
+ }
69336
+
69283
69337
  /**
69284
69338
  * Service responsible for managing privacy pool accounts and their associated commitments.
69285
69339
  * Handles account initialization, deposit/withdrawal tracking, and history synchronization.
@@ -69296,15 +69350,21 @@ class AccountService {
69296
69350
  * Creates a new AccountService instance.
69297
69351
  *
69298
69352
  * @param dataService - Service for fetching on-chain events
69299
- * @param account - Optional existing account to initialize with
69300
- * @param mnemonic - Optional mnemonic for deterministic key generation
69353
+ * @param config - Configuration for the account service (either mnemonic or existing account)
69354
+ * @param config.mnemonic - Optional mnemonic for deterministic key generation
69355
+ * @param config.account - Optional existing account to initialize with
69301
69356
  *
69302
69357
  * @throws {AccountError} If account initialization fails
69303
69358
  */
69304
- constructor(dataService, mnemonic, account) {
69359
+ constructor(dataService, config) {
69305
69360
  this.dataService = dataService;
69306
69361
  this.logger = new Logger({ prefix: "Account" });
69307
- this.account = account || this._initializeAccount(mnemonic);
69362
+ if ("mnemonic" in config) {
69363
+ this.account = this._initializeAccount(config.mnemonic);
69364
+ }
69365
+ else {
69366
+ this.account = config.account;
69367
+ }
69308
69368
  }
69309
69369
  /**
69310
69370
  * Initializes a new account from a mnemonic phrase.
@@ -69332,7 +69392,7 @@ class AccountService {
69332
69392
  masterKeys: [masterNullifier, masterSecret],
69333
69393
  poolAccounts: new Map(),
69334
69394
  creationTimestamp: 0n,
69335
- lastUpdateTimestamp: 0n
69395
+ lastUpdateTimestamp: 0n,
69336
69396
  };
69337
69397
  }
69338
69398
  catch (error) {
@@ -69454,8 +69514,11 @@ class AccountService {
69454
69514
  * The precommitment is a hash of the nullifier and secret, used in the deposit process.
69455
69515
  */
69456
69516
  createDepositSecrets(scope, index) {
69517
+ if (index && index < 0n) {
69518
+ throw AccountError.invalidIndex(index);
69519
+ }
69457
69520
  const accounts = this.account.poolAccounts.get(scope);
69458
- index = index || BigInt(accounts?.length || 0);
69521
+ index = index ?? BigInt(accounts?.length || 0);
69459
69522
  const nullifier = this._genDepositNullifier(scope, index);
69460
69523
  const secret = this._genDepositSecret(scope, index);
69461
69524
  const precommitment = this._hashPrecommitment(nullifier, secret);
@@ -69613,6 +69676,272 @@ class AccountService {
69613
69676
  return foundAccount;
69614
69677
  }
69615
69678
  /**
69679
+ * Fetches deposit events for a given pool and returns a map of precommitments to their events for efficient lookup
69680
+ *
69681
+ * @param pool - The pool to fetch deposit events for
69682
+ *
69683
+ * @returns A map of precommitments to their events
69684
+ */
69685
+ async getDepositEvents(pool) {
69686
+ try {
69687
+ const depositEvents = await this.dataService.getDeposits(pool);
69688
+ this.logger.info(`Found deposits for pool`, {
69689
+ poolAddress: pool.address,
69690
+ poolChainId: pool.chainId,
69691
+ depositCount: depositEvents.length,
69692
+ });
69693
+ const depositMap = new Map();
69694
+ for (const event of depositEvents) {
69695
+ depositMap.set(event.precommitment, event);
69696
+ }
69697
+ return depositMap;
69698
+ }
69699
+ catch (error) {
69700
+ throw EventError.depositEventError(pool.chainId, pool.scope, error);
69701
+ }
69702
+ }
69703
+ /**
69704
+ * Fetches withdrawal events for a given pool and returns a map of spent nullifiers to their events for efficient lookup
69705
+ *
69706
+ * @param pool - The pool to fetch withdrawal events for
69707
+ *
69708
+ * @returns A map of spent nullifiers to their events
69709
+ */
69710
+ async getWithdrawalEvents(pool) {
69711
+ try {
69712
+ const withdrawalEvents = await this.dataService.getWithdrawals(pool);
69713
+ const withdrawalMap = new Map();
69714
+ for (const event of withdrawalEvents) {
69715
+ withdrawalMap.set(event.spentNullifier, event);
69716
+ }
69717
+ return withdrawalMap;
69718
+ }
69719
+ catch (error) {
69720
+ throw EventError.withdrawalEventError(pool.chainId, pool.scope, error);
69721
+ }
69722
+ }
69723
+ /**
69724
+ * Fetches ragequit events for a given pool and returns a map of ragequit labels to their events for efficient lookup
69725
+ *
69726
+ * @param pool - The pool to fetch ragequit events for
69727
+ *
69728
+ * @returns A map of ragequit labels to their events
69729
+ */
69730
+ async getRagequitEvents(pool) {
69731
+ try {
69732
+ const ragequitEvents = await this.dataService.getRagequits(pool);
69733
+ const ragequitMap = new Map();
69734
+ for (const event of ragequitEvents) {
69735
+ ragequitMap.set(event.label, event);
69736
+ }
69737
+ return ragequitMap;
69738
+ }
69739
+ catch (error) {
69740
+ throw EventError.ragequitEventError(pool.chainId, pool.scope, error);
69741
+ }
69742
+ }
69743
+ /**
69744
+ * Fetches events for a given set of pools
69745
+ *
69746
+ * @param pools - The pools to fetch events for
69747
+ *
69748
+ * @returns A map of pool scopes to their events
69749
+ */
69750
+ async getEvents(pools) {
69751
+ const events = new Map();
69752
+ const poolEventResults = await Promise.allSettled(pools.map(async (pool) => {
69753
+ this.logger.info(`Fetching events for pool`, {
69754
+ poolAddress: pool.address,
69755
+ poolChainId: pool.chainId,
69756
+ poolDeploymentBlock: pool.deploymentBlock,
69757
+ });
69758
+ const [depositEvents, withdrawalEvents, ragequitEvents] = await Promise.all([
69759
+ this.getDepositEvents(pool),
69760
+ this.getWithdrawalEvents(pool),
69761
+ this.getRagequitEvents(pool),
69762
+ ]);
69763
+ return {
69764
+ scope: pool.scope,
69765
+ depositEvents,
69766
+ withdrawalEvents,
69767
+ ragequitEvents,
69768
+ };
69769
+ }));
69770
+ for (const result of poolEventResults) {
69771
+ if (result.status === "fulfilled") {
69772
+ const { scope, depositEvents, withdrawalEvents, ragequitEvents } = result.value;
69773
+ events.set(scope, {
69774
+ depositEvents,
69775
+ withdrawalEvents,
69776
+ ragequitEvents,
69777
+ });
69778
+ }
69779
+ else {
69780
+ events.set(result.reason.details?.scope, {
69781
+ reason: result.reason.message,
69782
+ scope: result.reason.details?.scope,
69783
+ });
69784
+ }
69785
+ }
69786
+ return events;
69787
+ }
69788
+ /**
69789
+ * Processes deposit events for a given scope and adds them to the account
69790
+ * Deterministically generate deposit secrets and check if they match on-chain deposits
69791
+ *
69792
+ * @param scope - The scope of the pool
69793
+ * @param depositEvents - The map of deposit events
69794
+ *
69795
+ */
69796
+ _processDepositEvents(scope, depositEvents) {
69797
+ for (let index = BigInt(0); index < depositEvents.size; index++) {
69798
+ // Generate nullifier, secret, and precommitment for this index
69799
+ const { nullifier, secret, precommitment } = this.createDepositSecrets(scope, index);
69800
+ // Look for a deposit with this precommitment
69801
+ const event = depositEvents.get(precommitment);
69802
+ if (!event) {
69803
+ break; // No more deposits found, exit the loop
69804
+ }
69805
+ // Create a new pool account for this deposit
69806
+ this.addPoolAccount(scope, event.value, nullifier, secret, event.label, event.blockNumber, event.transactionHash);
69807
+ }
69808
+ }
69809
+ /**
69810
+ * Processes withdrawal events for a given scope and adds them to the account
69811
+ *
69812
+ * @param scope - The scope of the pool
69813
+ * @param withdrawalEvents - The map of withdrawal events
69814
+ *
69815
+ * @remarks
69816
+ * This method performs the following steps for each pool:
69817
+ * 1. Identifies the earliest deposit block for each scope
69818
+ * 2. For each account, reconstructs the withdrawal history by:
69819
+ * - Generating nullifiers sequentially
69820
+ * - Matching them against on-chain events
69821
+ * - Adding matched withdrawals to the account state
69822
+ *
69823
+ * @throws {DataError} If event fetching fails
69824
+ * @private
69825
+ *
69826
+ */
69827
+ _processWithdrawalEvents(scope, withdrawalEvents) {
69828
+ const accounts = this.account.poolAccounts.get(scope);
69829
+ // Skip if no accounts for this scope
69830
+ if (!accounts || accounts.length === 0) {
69831
+ this.logger.info(`No accounts found for pool with this scope`, {
69832
+ scope,
69833
+ });
69834
+ return;
69835
+ }
69836
+ // Process each account in parallel for better performance
69837
+ for (const account of accounts) {
69838
+ let currentCommitment = account.deposit;
69839
+ let index = BigInt(0);
69840
+ // Continue processing withdrawals until no more are found secuentially
69841
+ while (true) {
69842
+ // Generate nullifier for this withdrawal
69843
+ const nullifierHash = hashingExports.poseidon([currentCommitment.nullifier]);
69844
+ // Look for a withdrawal event with this nullifier
69845
+ const withdrawal = withdrawalEvents.get(nullifierHash);
69846
+ if (!withdrawal) {
69847
+ break;
69848
+ }
69849
+ // Generate secret for this withdrawal
69850
+ const nullifier = this._genWithdrawalNullifier(account.label, index);
69851
+ const secret = this._genWithdrawalSecret(account.label, index);
69852
+ // Add the withdrawal commitment to the account
69853
+ const newCommitment = this.addWithdrawalCommitment(currentCommitment, currentCommitment.value - withdrawal.withdrawn, nullifier, secret, withdrawal.blockNumber, withdrawal.transactionHash);
69854
+ // Update current commitment to the newly created one
69855
+ currentCommitment = newCommitment;
69856
+ // Increment index for next potential withdrawal
69857
+ index++;
69858
+ }
69859
+ }
69860
+ }
69861
+ /**
69862
+ * Processes ragequit events for a given scope and adds them to the account
69863
+ *
69864
+ * @param scope - The scope of the pool
69865
+ * @param ragequitEvents - The map of ragequit events
69866
+ *
69867
+ * @remarks
69868
+ * This method performs the following steps for each pool:
69869
+ * 1. Adds ragequit events to accounts if found
69870
+ *
69871
+ * @throws {DataError} If event fetching fails
69872
+ * @private
69873
+ *
69874
+ */
69875
+ _processRagequitEvents(scope, ragequitEvents) {
69876
+ const accounts = this.account.poolAccounts.get(scope);
69877
+ if (!accounts || accounts.length === 0) {
69878
+ this.logger.info(`No accounts found for pool with this scope`, {
69879
+ scope,
69880
+ });
69881
+ return;
69882
+ }
69883
+ for (const account of accounts) {
69884
+ const ragequit = ragequitEvents.get(account.label);
69885
+ if (ragequit) {
69886
+ this.addRagequitToAccount(account.label, ragequit);
69887
+ }
69888
+ }
69889
+ }
69890
+ /**
69891
+ * Initializes an AccountService instance with events for a given set of pools
69892
+ *
69893
+ * @param dataService - The data service to use for fetching events
69894
+ * @param source - The source to use for initializing the account. Either a mnemonic or an existing account service instance
69895
+ * @param pools - The pools to fetch events for
69896
+ *
69897
+ * @remarks
69898
+ * This method performs the following steps for each pool:
69899
+ * 1. Fetches deposit, withdrawal, and ragequit events for each pool
69900
+ * 2. Processes deposit events and creates pool accounts
69901
+ * 3. Processes withdrawal events and adds commitments to pool accounts
69902
+ * 4. Processes ragequit events and adds ragequit to pool accounts
69903
+ *
69904
+ * @returns The initialized AccountService instance and array of errors if any pool events fetching fails
69905
+ *
69906
+ * if any pool events fetching fails, the account will be initialized without the events for that pool
69907
+ * user can then call to this method again with the same account and missing pools to fetch the missing events
69908
+ *
69909
+ * @throws {AccountError} If account state reconstruction fails or if duplicate pools are found
69910
+ */
69911
+ static async initializeWithEvents(dataService, source, pools) {
69912
+ // Log the start of the history retrieval process
69913
+ const logger = new Logger({ prefix: "Account" });
69914
+ logger.info(`Fetching events for pools`, { poolLength: pools.length });
69915
+ // verify that pools don't contain duplicates based on scope
69916
+ const uniqueScopes = new Set();
69917
+ for (const pool of pools) {
69918
+ if (uniqueScopes.has(pool.scope)) {
69919
+ throw AccountError.duplicatePools(pool.scope);
69920
+ }
69921
+ uniqueScopes.add(pool.scope);
69922
+ }
69923
+ const errors = [];
69924
+ const account = new AccountService(dataService, "mnemonic" in source
69925
+ ? { mnemonic: source.mnemonic }
69926
+ : { account: source.service.account });
69927
+ const events = await account.getEvents(pools);
69928
+ for (const [scope, result] of events.entries()) {
69929
+ if ("reason" in result) {
69930
+ errors.push(result);
69931
+ }
69932
+ else {
69933
+ // Process deposit events an create pool accounts
69934
+ account._processDepositEvents(scope, result.depositEvents);
69935
+ // Process withdrawal events and add commitments to pool accounts
69936
+ account._processWithdrawalEvents(scope, result.withdrawalEvents);
69937
+ // Process ragequit events and add ragequit to pool accounts
69938
+ account._processRagequitEvents(scope, result.ragequitEvents);
69939
+ }
69940
+ }
69941
+ return { account, errors };
69942
+ }
69943
+ /**
69944
+ * @deprecated Use `initializeWithEvents` for instantiating an account with history reconstruction
69616
69945
  * Retrieves the history of deposits and withdrawals for the given pools.
69617
69946
  *
69618
69947
  * @param pools - Array of pool configurations to sync history for
@@ -69682,10 +70011,10 @@ class AccountService {
69682
70011
  return;
69683
70012
  }
69684
70013
  this.logger.info(`Found ${foundDeposits.length} deposits for pool ${pool.address}`);
69685
- // Process withdrawals and ragequits for all pools
69686
- // This is done after all deposits are processed to ensure we have the complete account state
69687
- await this._processWithdrawalsAndRagequits(pools);
69688
70014
  }));
70015
+ // Process withdrawals and ragequits for all pools
70016
+ // This is done after all deposits are processed to ensure we have the complete account state
70017
+ await this._processWithdrawalsAndRagequits(pools);
69689
70018
  }
69690
70019
  /**
69691
70020
  * Processes withdrawal events for all pools and updates account state.
@@ -69745,7 +70074,9 @@ class AccountService {
69745
70074
  // Continue processing withdrawals until no more are found
69746
70075
  while (true) {
69747
70076
  // Generate nullifier for this withdrawal
69748
- const nullifierHash = hashingExports.poseidon([currentCommitment.nullifier]);
70077
+ const nullifierHash = hashingExports.poseidon([
70078
+ currentCommitment.nullifier,
70079
+ ]);
69749
70080
  // Look for a withdrawal event with this nullifier
69750
70081
  const withdrawal = withdrawalMap.get(nullifierHash);
69751
70082
  if (!withdrawal) {
@@ -69770,22 +70101,6 @@ class AccountService {
69770
70101
  }
69771
70102
  }
69772
70103
 
69773
- class DataError extends SDKError {
69774
- constructor(message, code = ErrorCode.NETWORK_ERROR, details) {
69775
- super(message, code, details);
69776
- this.name = "DataError";
69777
- }
69778
- static invalidLog(type, reason) {
69779
- return new DataError(`Invalid ${type} log: ${reason}`, ErrorCode.INVALID_INPUT);
69780
- }
69781
- static chainNotConfigured(chainId) {
69782
- return new DataError(`No configuration found for chain ID ${chainId}`, ErrorCode.INVALID_INPUT);
69783
- }
69784
- static networkError(chainId, error) {
69785
- return new DataError(`Network error on chain ${chainId}: ${error.message}`, ErrorCode.NETWORK_ERROR, { originalError: error });
69786
- }
69787
- }
69788
-
69789
70104
  // Event signatures from the contract
69790
70105
  const DEPOSIT_EVENT = parseAbiItem('event Deposited(address indexed _depositor, uint256 _commitment, uint256 _label, uint256 _value, uint256 _merkleRoot)');
69791
70106
  const WITHDRAWAL_EVENT = parseAbiItem('event Withdrawn(address indexed _processooor, uint256 _value, uint256 _spentNullifier, uint256 _newCommitment)');
@@ -69814,12 +70129,11 @@ class DataService {
69814
70129
  this.logger = new Logger({ prefix: "Data" });
69815
70130
  try {
69816
70131
  for (const config of chainConfigs) {
69817
- if (!config.rpcUrl || !config.apiKey) {
69818
- throw new Error(`Missing RPC URL or API key for chain ${config.chainId}`);
70132
+ if (!config.rpcUrl) {
70133
+ throw new Error(`Missing RPC URL for chain ${config.chainId}`);
69819
70134
  }
69820
70135
  const client = createPublicClient({
69821
70136
  transport: http(config.rpcUrl),
69822
- key: config.apiKey,
69823
70137
  });
69824
70138
  this.clients.set(config.chainId, client);
69825
70139
  }
@@ -69995,4 +70309,4 @@ class DataService {
69995
70309
  }
69996
70310
 
69997
70311
  export { AccountService as A, BlockchainProvider as B, CommitmentService as C, DataService as D, ErrorCode as E, FetchArtifact as F, InvalidRpcUrl as I, PrivacyPoolSDK as P, SDKError as S, WithdrawalService as W, generateDepositSecrets as a, generateWithdrawalSecrets as b, getCommitment as c, generateMerkleProof as d, bigintToHash as e, bigintToHex as f, generateMasterKeys as g, hashPrecommitment as h, calculateContext as i, Circuits as j, ContractInteractionsService as k, ProofError as l, ContractError as m, AccountError as n, CircuitName as o };
69998
- //# sourceMappingURL=index-Bce8vsoP.js.map
70312
+ //# sourceMappingURL=index-IxAruGRY.js.map