@0xbow/privacy-pools-core-sdk 0.1.14 → 0.1.15

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 (26) hide show
  1. package/dist/esm/{fetchArtifacts.esm-CN7uN1ly.js → fetchArtifacts.esm-tTGl04fU.js} +2 -2
  2. package/dist/esm/{fetchArtifacts.esm-CN7uN1ly.js.map → fetchArtifacts.esm-tTGl04fU.js.map} +1 -1
  3. package/dist/esm/{fetchArtifacts.node-B93BS926.js → fetchArtifacts.node-xJWJ5ErU.js} +2 -2
  4. package/dist/esm/{fetchArtifacts.node-B93BS926.js.map → fetchArtifacts.node-xJWJ5ErU.js.map} +1 -1
  5. package/dist/esm/{index-B2wT3cOz.js → index-CGSW-te1.js} +255 -62
  6. package/dist/esm/{index-B2wT3cOz.js.map → index-CGSW-te1.js.map} +1 -1
  7. package/dist/esm/index.mjs +1 -1
  8. package/dist/index.d.mts +163 -35
  9. package/dist/node/{fetchArtifacts.esm-BE3E5wR9.js → fetchArtifacts.esm-XK88imHM.js} +2 -2
  10. package/dist/node/{fetchArtifacts.esm-BE3E5wR9.js.map → fetchArtifacts.esm-XK88imHM.js.map} +1 -1
  11. package/dist/node/{fetchArtifacts.node-BoI4I9Vy.js → fetchArtifacts.node-CF88owBm.js} +2 -2
  12. package/dist/node/{fetchArtifacts.node-BoI4I9Vy.js.map → fetchArtifacts.node-CF88owBm.js.map} +1 -1
  13. package/dist/node/{index-BX96yzW6.js → index-wpuEgOIF.js} +255 -62
  14. package/dist/node/{index-BX96yzW6.js.map → index-wpuEgOIF.js.map} +1 -1
  15. package/dist/node/index.mjs +1 -1
  16. package/dist/types/core/account.service.d.ts +132 -4
  17. package/dist/types/core/data.service.d.ts +5 -4
  18. package/dist/types/{fetchArtifacts.esm-XXmZbdtj.js → fetchArtifacts.esm-CLoTjONi.js} +1 -1
  19. package/dist/types/{fetchArtifacts.node-CO7FJNaO.js → fetchArtifacts.node-CS9h2Ka1.js} +1 -1
  20. package/dist/types/{index-Dp0aOfF2.js → index-BWpQiA40.js} +254 -61
  21. package/dist/types/index.js +1 -1
  22. package/dist/types/types/account.d.ts +3 -1
  23. package/package.json +1 -1
  24. package/src/core/account.service.ts +290 -65
  25. package/src/core/data.service.ts +21 -24
  26. package/src/types/account.ts +3 -1
@@ -1,4 +1,4 @@
1
- import { F as FetchArtifact } from './index-B2wT3cOz.js';
1
+ import { F as FetchArtifact } from './index-CGSW-te1.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-CN7uN1ly.js.map
18
+ //# sourceMappingURL=fetchArtifacts.esm-tTGl04fU.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"fetchArtifacts.esm-CN7uN1ly.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-tTGl04fU.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-B2wT3cOz.js';
1
+ import { F as FetchArtifact } from './index-CGSW-te1.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-B93BS926.js.map
31
+ //# sourceMappingURL=fetchArtifacts.node-xJWJ5ErU.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"fetchArtifacts.node-B93BS926.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-xJWJ5ErU.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-CN7uN1ly.js');
45818
+ return import('./fetchArtifacts.esm-tTGl04fU.js');
45819
45819
  }
45820
45820
  else {
45821
- return import('./fetchArtifacts.node-B93BS926.js');
45821
+ return import('./fetchArtifacts.node-xJWJ5ErU.js');
45822
45822
  }
45823
45823
  }
45824
45824
 
@@ -68684,6 +68684,21 @@ class AccountService {
68684
68684
  this.logger = new Logger({ prefix: "Account" });
68685
68685
  this.account = account || this._initializeAccount(mnemonic);
68686
68686
  }
68687
+ /**
68688
+ * Initializes a new account from a mnemonic phrase.
68689
+ *
68690
+ * @param mnemonic - The mnemonic phrase to derive keys from
68691
+ * @returns A new PrivacyPoolAccount with derived master keys
68692
+ *
68693
+ * @remarks
68694
+ * This method derives two master keys from the mnemonic:
68695
+ * 1. A master nullifier key from account index 0
68696
+ * 2. A master secret key from account index 1
68697
+ * These keys are used to deterministically generate nullifiers and secrets for deposits and withdrawals.
68698
+ *
68699
+ * @throws {AccountError} If account initialization fails
68700
+ * @private
68701
+ */
68687
68702
  _initializeAccount(mnemonic) {
68688
68703
  try {
68689
68704
  this.logger.debug("Initializing account with mnemonic");
@@ -68702,25 +68717,74 @@ class AccountService {
68702
68717
  throw AccountError.accountInitializationFailed(error instanceof Error ? error.message : "Unknown error");
68703
68718
  }
68704
68719
  }
68720
+ /**
68721
+ * Generates a deterministic nullifier for a deposit.
68722
+ *
68723
+ * @param scope - The scope of the pool
68724
+ * @param index - The index of the deposit
68725
+ * @returns A deterministic nullifier for the deposit
68726
+ * @private
68727
+ */
68705
68728
  _genDepositNullifier(scope, index) {
68706
68729
  const [masterNullifier] = this.account.masterKeys;
68707
68730
  return hashingExports.poseidon([masterNullifier, scope, index]);
68708
68731
  }
68732
+ /**
68733
+ * Generates a deterministic secret for a deposit.
68734
+ *
68735
+ * @param scope - The scope of the pool
68736
+ * @param index - The index of the deposit
68737
+ * @returns A deterministic secret for the deposit
68738
+ * @private
68739
+ */
68709
68740
  _genDepositSecret(scope, index) {
68710
68741
  const [, masterSecret] = this.account.masterKeys;
68711
68742
  return hashingExports.poseidon([masterSecret, scope, index]);
68712
68743
  }
68744
+ /**
68745
+ * Generates a deterministic nullifier for a withdrawal.
68746
+ *
68747
+ * @param label - The label of the commitment
68748
+ * @param index - The index of the withdrawal
68749
+ * @returns A deterministic nullifier for the withdrawal
68750
+ * @private
68751
+ */
68713
68752
  _genWithdrawalNullifier(label, index) {
68714
68753
  const [masterNullifier] = this.account.masterKeys;
68715
68754
  return hashingExports.poseidon([masterNullifier, label, index]);
68716
68755
  }
68756
+ /**
68757
+ * Generates a deterministic secret for a withdrawal.
68758
+ *
68759
+ * @param label - The label of the commitment
68760
+ * @param index - The index of the withdrawal
68761
+ * @returns A deterministic secret for the withdrawal
68762
+ * @private
68763
+ */
68717
68764
  _genWithdrawalSecret(label, index) {
68718
68765
  const [, masterSecret] = this.account.masterKeys;
68719
68766
  return hashingExports.poseidon([masterSecret, label, index]);
68720
68767
  }
68768
+ /**
68769
+ * Hashes a commitment using the Poseidon hash function.
68770
+ *
68771
+ * @param value - The value of the commitment
68772
+ * @param label - The label of the commitment
68773
+ * @param precommitment - The precommitment hash
68774
+ * @returns The commitment hash
68775
+ * @private
68776
+ */
68721
68777
  _hashCommitment(value, label, precommitment) {
68722
68778
  return hashingExports.poseidon([value, label, precommitment]);
68723
68779
  }
68780
+ /**
68781
+ * Hashes a precommitment using the Poseidon hash function.
68782
+ *
68783
+ * @param nullifier - The nullifier for the commitment
68784
+ * @param secret - The secret for the commitment
68785
+ * @returns The precommitment hash
68786
+ * @private
68787
+ */
68724
68788
  _hashPrecommitment(nullifier, secret) {
68725
68789
  return hashingExports.poseidon([nullifier, secret]);
68726
68790
  }
@@ -68728,12 +68792,21 @@ class AccountService {
68728
68792
  * Gets all spendable commitments across all pools.
68729
68793
  *
68730
68794
  * @returns A map of scope to array of spendable commitments
68795
+ *
68796
+ * @remarks
68797
+ * A commitment is considered spendable if:
68798
+ * 1. It has a non-zero value
68799
+ * 2. The account it belongs to has not been ragequit
68731
68800
  */
68732
68801
  getSpendableCommitments() {
68733
68802
  const result = new Map();
68734
68803
  for (const [scope, accounts] of this.account.poolAccounts.entries()) {
68735
68804
  const nonZeroCommitments = [];
68736
68805
  for (const account of accounts) {
68806
+ // Skip accounts that have been ragequit
68807
+ if (account.ragequit) {
68808
+ continue;
68809
+ }
68737
68810
  const lastCommitment = account.children.length > 0
68738
68811
  ? account.children[account.children.length - 1]
68739
68812
  : account.deposit;
@@ -68753,6 +68826,10 @@ class AccountService {
68753
68826
  * @param scope - The scope of the pool to deposit into
68754
68827
  * @param index - Optional index for deterministic generation
68755
68828
  * @returns The nullifier, secret, and precommitment for the deposit
68829
+ *
68830
+ * @remarks
68831
+ * If no index is provided, it uses the current number of accounts for the scope.
68832
+ * The precommitment is a hash of the nullifier and secret, used in the deposit process.
68756
68833
  */
68757
68834
  createDepositSecrets(scope, index) {
68758
68835
  const accounts = this.account.poolAccounts.get(scope);
@@ -68767,6 +68844,12 @@ class AccountService {
68767
68844
  *
68768
68845
  * @param commitment - The commitment to spend
68769
68846
  * @returns The nullifier and secret for the new commitment
68847
+ *
68848
+ * @remarks
68849
+ * The index used for generating the withdrawal nullifier and secret is based on
68850
+ * the number of children the account already has, ensuring each withdrawal has
68851
+ * a unique nullifier.
68852
+ *
68770
68853
  * @throws {AccountError} If no account is found for the commitment
68771
68854
  */
68772
68855
  createWithdrawalSecrets(commitment) {
@@ -68796,6 +68879,11 @@ class AccountService {
68796
68879
  * @param blockNumber - The block number of the deposit
68797
68880
  * @param txHash - The transaction hash of the deposit
68798
68881
  * @returns The new pool account
68882
+ *
68883
+ * @remarks
68884
+ * This method creates a new account with the deposit commitment and adds it to the
68885
+ * pool accounts map under the specified scope. The commitment hash is calculated
68886
+ * from the value, label, and precommitment.
68799
68887
  */
68800
68888
  addPoolAccount(scope, value, nullifier, secret, label, blockNumber, txHash) {
68801
68889
  const precommitment = this._hashPrecommitment(nullifier, secret);
@@ -68830,6 +68918,12 @@ class AccountService {
68830
68918
  * @param blockNumber - The block number of the withdrawal
68831
68919
  * @param txHash - The transaction hash of the withdrawal
68832
68920
  * @returns The new commitment
68921
+ *
68922
+ * @remarks
68923
+ * This method finds the account containing the parent commitment, creates a new
68924
+ * commitment with the provided parameters, and adds it to the account's children.
68925
+ * The new commitment inherits the label from the parent commitment.
68926
+ *
68833
68927
  * @throws {AccountError} If no account is found for the commitment
68834
68928
  */
68835
68929
  addWithdrawalCommitment(parentCommitment, value, nullifier, secret, blockNumber, txHash) {
@@ -68863,6 +68957,39 @@ class AccountService {
68863
68957
  this.logger.info(`Added new commitment with value ${value} to account with label ${parentCommitment.label}`);
68864
68958
  return newCommitment;
68865
68959
  }
68960
+ /**
68961
+ * Adds a ragequit event to an existing pool account
68962
+ *
68963
+ * @param label - The label of the account to add the ragequit to
68964
+ * @param ragequit - The ragequit event to add
68965
+ * @returns The updated pool account
68966
+ *
68967
+ * @remarks
68968
+ * When an account has a ragequit event, it can no longer be spent.
68969
+ * This method finds the account with the matching label and attaches
68970
+ * the ragequit event to it.
68971
+ *
68972
+ * @throws {AccountError} If no account is found with the given label
68973
+ */
68974
+ addRagequitToAccount(label, ragequit) {
68975
+ let foundAccount;
68976
+ let foundScope;
68977
+ // Find the account with the matching label
68978
+ for (const [scope, accounts] of this.account.poolAccounts.entries()) {
68979
+ foundAccount = accounts.find((account) => account.label === label);
68980
+ if (foundAccount) {
68981
+ foundScope = scope;
68982
+ break;
68983
+ }
68984
+ }
68985
+ if (!foundAccount || !foundScope) {
68986
+ throw new AccountError(`No account found with label ${label}`, ErrorCode.INVALID_INPUT);
68987
+ }
68988
+ // Add the ragequit event to the account
68989
+ foundAccount.ragequit = ragequit;
68990
+ this.logger.info(`Added ragequit event to account with label ${label}, value ${ragequit.value}`);
68991
+ return foundAccount;
68992
+ }
68866
68993
  /**
68867
68994
  * Retrieves the history of deposits and withdrawals for the given pools.
68868
68995
  *
@@ -68870,85 +68997,154 @@ class AccountService {
68870
68997
  *
68871
68998
  * @remarks
68872
68999
  * This method performs the following steps:
68873
- * 1. Fetches all deposit events for each pool
68874
- * 2. Reconstructs account state from deposits
68875
- * 3. Processes withdrawals to update account state
69000
+ * 1. Initializes pool accounts for each pool if they don't exist
69001
+ * 2. For each pool, fetches deposit events and reconstructs accounts
69002
+ * 3. Processes withdrawals and ragequits to update account state
69003
+ *
69004
+ * The account reconstruction is deterministic based on the master keys,
69005
+ * allowing the full state to be recovered from on-chain events.
68876
69006
  *
68877
69007
  * @throws {DataError} If event fetching fails
68878
69008
  * @throws {AccountError} If account state reconstruction fails
68879
69009
  */
68880
69010
  async retrieveHistory(pools) {
69011
+ // Log the start of the history retrieval process
68881
69012
  this.logger.info(`Fetching events for ${pools.length} pools`);
69013
+ // Initialize pool accounts map for each pool if it doesn't exist
68882
69014
  for (const pool of pools) {
68883
69015
  if (!this.account.poolAccounts.has(pool.scope)) {
68884
69016
  this.account.poolAccounts.set(pool.scope, []);
68885
69017
  }
68886
69018
  }
69019
+ // Process all pools in parallel for better performance
68887
69020
  await Promise.all(pools.map(async (pool) => {
69021
+ // Log which pool is being processed
68888
69022
  this.logger.info(`Processing pool ${pool.address} on chain ${pool.chainId} from block ${pool.deploymentBlock}`);
68889
- const deposits = await this.dataService.getDeposits(pool.chainId, {
68890
- fromBlock: pool.deploymentBlock,
68891
- });
69023
+ // Fetch all deposit events for this pool
69024
+ const deposits = await this.dataService.getDeposits(pool);
68892
69025
  this.logger.info(`Found ${deposits.length} deposits for pool ${pool.address}`);
69026
+ // Create a map of deposits by precommitment for efficient lookup
68893
69027
  const depositMap = new Map();
68894
69028
  for (const deposit of deposits) {
68895
69029
  depositMap.set(deposit.precommitment, deposit);
68896
69030
  }
69031
+ // Track found deposits for logging and debugging
68897
69032
  const foundDeposits = [];
69033
+ // Start with index 0 and try to find deposits deterministically
68898
69034
  let index = BigInt(0);
68899
69035
  let firstDepositBlock;
69036
+ // Deterministically generate deposit secrets and check if they match on-chain deposits
68900
69037
  while (true) {
69038
+ // Generate nullifier, secret, and precommitment for this index
68901
69039
  const nullifier = this._genDepositNullifier(pool.scope, index);
68902
69040
  const secret = this._genDepositSecret(pool.scope, index);
68903
69041
  const precommitment = this._hashPrecommitment(nullifier, secret);
69042
+ // Look for a deposit with this precommitment
68904
69043
  const deposit = depositMap.get(precommitment);
68905
69044
  if (!deposit)
68906
- break;
69045
+ break; // No more deposits found, exit the loop
69046
+ // Track the earliest deposit block for later withdrawal processing
68907
69047
  if (!firstDepositBlock || deposit.blockNumber < firstDepositBlock) {
68908
69048
  firstDepositBlock = deposit.blockNumber;
68909
69049
  }
68910
- foundDeposits.push({ index, nullifier, secret, deposit });
69050
+ // Create a new pool account for this deposit
69051
+ this.addPoolAccount(pool.scope, deposit.value, nullifier, secret, deposit.label, deposit.blockNumber, deposit.transactionHash);
69052
+ // Track the found deposit
69053
+ foundDeposits.push({ index, nullifier, secret, pool, deposit });
69054
+ // Move to the next index
68911
69055
  index++;
68912
69056
  }
68913
- if (foundDeposits.length === 0) {
69057
+ // If no accounts were found for this scope, log and skip further processing
69058
+ if (this.account.poolAccounts.get(pool.scope).length === 0) {
68914
69059
  this.logger.info(`No Pool Accounts were found for scope ${pool.scope}`);
68915
69060
  return;
68916
69061
  }
68917
69062
  this.logger.info(`Found ${foundDeposits.length} deposits for pool ${pool.address}`);
68918
- // Process deposits first
68919
- const accounts = foundDeposits.map(({ nullifier, secret, deposit }) => {
68920
- return this.addPoolAccount(pool.scope, deposit.value, nullifier, secret, deposit.label, deposit.blockNumber, deposit.transactionHash);
68921
- });
68922
- // Create a map for faster account lookups
68923
- const accountMap = new Map();
68924
- for (const account of accounts) {
68925
- accountMap.set(account.label, account);
68926
- }
68927
- // Process withdrawals
68928
- await this._processWithdrawals(pool.chainId, firstDepositBlock, accountMap);
69063
+ // Process withdrawals and ragequits for all pools
69064
+ // This is done after all deposits are processed to ensure we have the complete account state
69065
+ await this._processWithdrawalsAndRagequits(pools);
68929
69066
  }));
68930
69067
  }
68931
- async _processWithdrawals(chainId, fromBlock, foundAccounts) {
68932
- const withdrawals = await this.dataService.getWithdrawals(chainId, {
68933
- fromBlock,
68934
- });
68935
- for (const withdrawal of withdrawals) {
68936
- for (const account of foundAccounts.values()) {
68937
- const isParentCommitment = BigInt(account.deposit.nullifier) === BigInt(withdrawal.spentNullifier) ||
68938
- account.children.some(child => BigInt(child.nullifier) === BigInt(withdrawal.spentNullifier));
68939
- if (isParentCommitment) {
68940
- const parentCommitment = account.children.length > 0
68941
- ? account.children[account.children.length - 1]
68942
- : account.deposit;
68943
- if (!parentCommitment) {
68944
- this.logger.warn(`No parent commitment found for withdrawal ${withdrawal.spentNullifier.toString()}`);
68945
- continue;
69068
+ /**
69069
+ * Processes withdrawal events for all pools and updates account state.
69070
+ *
69071
+ * @param pools - Array of pool configurations to process withdrawals for
69072
+ *
69073
+ * @remarks
69074
+ * This method performs the following steps for each pool:
69075
+ * 1. Identifies the earliest deposit block for each scope
69076
+ * 2. Fetches withdrawal and ragequit events from that block
69077
+ * 3. Maps withdrawals by nullifier hash and ragequits by label for efficient lookup
69078
+ * 4. For each account, reconstructs the withdrawal history by:
69079
+ * - Generating nullifiers sequentially
69080
+ * - Matching them against on-chain events
69081
+ * - Adding matched withdrawals to the account state
69082
+ * 5. Adds ragequit events to accounts if found
69083
+ *
69084
+ * @throws {DataError} If event fetching fails
69085
+ * @private
69086
+ */
69087
+ async _processWithdrawalsAndRagequits(pools) {
69088
+ await Promise.all(pools.map(async (pool) => {
69089
+ const accounts = this.account.poolAccounts.get(pool.scope);
69090
+ // Skip if no accounts for this scope
69091
+ if (!accounts || accounts.length === 0) {
69092
+ this.logger.info(`No accounts found for pool ${pool.address} with scope ${pool.scope}`);
69093
+ return;
69094
+ }
69095
+ // Find the earliest deposit block for this scope
69096
+ let firstDepositBlock = BigInt(Number.MAX_SAFE_INTEGER);
69097
+ for (const account of accounts) {
69098
+ if (account.deposit.blockNumber < firstDepositBlock) {
69099
+ firstDepositBlock = account.deposit.blockNumber;
69100
+ }
69101
+ }
69102
+ // Fetch withdrawal and ragequit events from the first deposit block
69103
+ const withdrawals = await this.dataService.getWithdrawals(pool, firstDepositBlock);
69104
+ const ragequits = await this.dataService.getRagequits(pool, firstDepositBlock);
69105
+ this.logger.info(`Found ${withdrawals.length} withdrawals for pool ${pool.address}`);
69106
+ if (withdrawals.length === 0 && ragequits.length === 0) {
69107
+ return;
69108
+ }
69109
+ // Map withdrawals by spent nullifier for quick lookup
69110
+ const withdrawalMap = new Map();
69111
+ for (const withdrawal of withdrawals) {
69112
+ withdrawalMap.set(withdrawal.spentNullifier, withdrawal);
69113
+ }
69114
+ // Map ragequits by label for quick lookup
69115
+ const ragequitMap = new Map();
69116
+ for (const ragequit of ragequits) {
69117
+ ragequitMap.set(ragequit.label, ragequit);
69118
+ }
69119
+ // Process each account
69120
+ for (const account of accounts) {
69121
+ let currentCommitment = account.deposit;
69122
+ let index = BigInt(0);
69123
+ // Continue processing withdrawals until no more are found
69124
+ while (true) {
69125
+ // Generate nullifier for this withdrawal
69126
+ const nullifierHash = hashingExports.poseidon([currentCommitment.nullifier]);
69127
+ // Look for a withdrawal event with this nullifier
69128
+ const withdrawal = withdrawalMap.get(nullifierHash);
69129
+ if (!withdrawal) {
69130
+ break;
68946
69131
  }
68947
- this.addWithdrawalCommitment(parentCommitment, withdrawal.withdrawn, withdrawal.spentNullifier, parentCommitment.secret, withdrawal.blockNumber, withdrawal.transactionHash);
68948
- break;
69132
+ // Generate secret for this withdrawal
69133
+ const nullifier = this._genWithdrawalNullifier(account.label, index);
69134
+ const secret = this._genWithdrawalSecret(account.label, index);
69135
+ // Add the withdrawal commitment to the account
69136
+ const newCommitment = this.addWithdrawalCommitment(currentCommitment, currentCommitment.value - withdrawal.withdrawn, nullifier, secret, withdrawal.blockNumber, withdrawal.transactionHash);
69137
+ // Update current commitment to the newly created one
69138
+ currentCommitment = newCommitment;
69139
+ // Increment index for next potential withdrawal
69140
+ index++;
69141
+ }
69142
+ const ragequit = ragequitMap.get(account.label);
69143
+ if (ragequit) {
69144
+ this.addRagequitToAccount(account.label, ragequit);
68949
69145
  }
68950
69146
  }
68951
- }
69147
+ }));
68952
69148
  }
68953
69149
  }
68954
69150
 
@@ -69018,15 +69214,14 @@ class DataService {
69018
69214
  * @returns Array of deposit events with properly typed fields (bigint for numbers, Hash for commitments)
69019
69215
  * @throws {DataError} If client is not configured, network error occurs, or event data is invalid
69020
69216
  */
69021
- async getDeposits(chainId, options = {}) {
69217
+ async getDeposits(pool) {
69022
69218
  try {
69023
- const client = this.getClientForChain(chainId);
69024
- const config = this.getConfigForChain(chainId);
69219
+ const client = this.getClientForChain(pool.chainId);
69220
+ const config = this.getConfigForChain(pool.chainId);
69025
69221
  const logs = await client.getLogs({
69026
- address: config.privacyPoolAddress,
69222
+ address: pool.address,
69027
69223
  event: DEPOSIT_EVENT,
69028
- fromBlock: options.fromBlock ?? config.startBlock,
69029
- toBlock: options.toBlock,
69224
+ fromBlock: pool.deploymentBlock ?? config.startBlock
69030
69225
  }).catch(error => {
69031
69226
  throw new DataError("Failed to fetch deposit logs", ErrorCode.NETWORK_ERROR, { error: error instanceof Error ? error.message : "Unknown error" });
69032
69227
  });
@@ -69059,7 +69254,7 @@ class DataService {
69059
69254
  catch (error) {
69060
69255
  if (error instanceof DataError)
69061
69256
  throw error;
69062
- throw DataError.networkError(chainId, error instanceof Error ? error : new Error(String(error)));
69257
+ throw DataError.networkError(pool.chainId, error instanceof Error ? error : new Error(String(error)));
69063
69258
  }
69064
69259
  }
69065
69260
  /**
@@ -69070,15 +69265,14 @@ class DataService {
69070
69265
  * @returns Array of withdrawal events with properly typed fields (bigint for numbers, Hash for commitments)
69071
69266
  * @throws {DataError} If client is not configured, network error occurs, or event data is invalid
69072
69267
  */
69073
- async getWithdrawals(chainId, options = {}) {
69268
+ async getWithdrawals(pool, fromBlock = pool.deploymentBlock) {
69074
69269
  try {
69075
- const client = this.getClientForChain(chainId);
69076
- const config = this.getConfigForChain(chainId);
69270
+ const client = this.getClientForChain(pool.chainId);
69271
+ const config = this.getConfigForChain(pool.chainId);
69077
69272
  const logs = await client.getLogs({
69078
- address: config.privacyPoolAddress,
69273
+ address: pool.address,
69079
69274
  event: WITHDRAWAL_EVENT,
69080
- fromBlock: options.fromBlock ?? config.startBlock,
69081
- toBlock: options.toBlock,
69275
+ fromBlock: fromBlock ?? config.startBlock,
69082
69276
  }).catch(error => {
69083
69277
  throw new DataError("Failed to fetch withdrawal logs", ErrorCode.NETWORK_ERROR, { error: error instanceof Error ? error.message : "Unknown error" });
69084
69278
  });
@@ -69109,7 +69303,7 @@ class DataService {
69109
69303
  catch (error) {
69110
69304
  if (error instanceof DataError)
69111
69305
  throw error;
69112
- throw DataError.networkError(chainId, error instanceof Error ? error : new Error(String(error)));
69306
+ throw DataError.networkError(pool.chainId, error instanceof Error ? error : new Error(String(error)));
69113
69307
  }
69114
69308
  }
69115
69309
  /**
@@ -69120,15 +69314,14 @@ class DataService {
69120
69314
  * @returns Array of ragequit events with properly typed fields (bigint for numbers, Hash for commitments)
69121
69315
  * @throws {DataError} If client is not configured, network error occurs, or event data is invalid
69122
69316
  */
69123
- async getRagequits(chainId, options = {}) {
69317
+ async getRagequits(pool, fromBlock = pool.deploymentBlock) {
69124
69318
  try {
69125
- const client = this.getClientForChain(chainId);
69126
- const config = this.getConfigForChain(chainId);
69319
+ const client = this.getClientForChain(pool.chainId);
69320
+ const config = this.getConfigForChain(pool.chainId);
69127
69321
  const logs = await client.getLogs({
69128
- address: config.privacyPoolAddress,
69322
+ address: pool.address,
69129
69323
  event: RAGEQUIT_EVENT,
69130
- fromBlock: options.fromBlock ?? config.startBlock,
69131
- toBlock: options.toBlock,
69324
+ fromBlock: fromBlock ?? config.startBlock,
69132
69325
  }).catch(error => {
69133
69326
  throw new DataError("Failed to fetch ragequit logs", ErrorCode.NETWORK_ERROR, { error: error instanceof Error ? error.message : "Unknown error" });
69134
69327
  });
@@ -69160,7 +69353,7 @@ class DataService {
69160
69353
  catch (error) {
69161
69354
  if (error instanceof DataError)
69162
69355
  throw error;
69163
- throw DataError.networkError(chainId, error instanceof Error ? error : new Error(String(error)));
69356
+ throw DataError.networkError(pool.chainId, error instanceof Error ? error : new Error(String(error)));
69164
69357
  }
69165
69358
  }
69166
69359
  getClientForChain(chainId) {
@@ -69180,4 +69373,4 @@ class DataService {
69180
69373
  }
69181
69374
 
69182
69375
  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 };
69183
- //# sourceMappingURL=index-B2wT3cOz.js.map
69376
+ //# sourceMappingURL=index-CGSW-te1.js.map