@ocash/sdk 0.1.2 → 0.1.4-rc.0

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.
package/dist/node.cjs CHANGED
@@ -7258,15 +7258,17 @@ var MerkleEngine = class _MerkleEngine {
7258
7258
  for (let i = 0; i < sorted.length; i++) {
7259
7259
  if (sorted[i].cid !== i) throw new Error(`Non-contiguous persisted merkle leaves: expected cid=${i}, got cid=${sorted[i].cid}`);
7260
7260
  }
7261
- const totalElements = BigInt(sorted.length);
7262
- const mergedElements = _MerkleEngine.totalElementsInTree(totalElements);
7261
+ const storedTree = await this.storage?.getMerkleTree?.(chainId);
7262
+ const leafCount = sorted.length;
7263
+ const mergedFromLeaves = _MerkleEngine.totalElementsInTree(BigInt(leafCount));
7264
+ const mergedFromTree = typeof storedTree?.totalElements === "number" && storedTree.totalElements > 0 ? storedTree.totalElements : 0;
7265
+ const mergedElements = Math.max(mergedFromLeaves, mergedFromTree);
7263
7266
  const pending = this.ensurePendingLeaves(chainId);
7264
7267
  pending.length = 0;
7265
7268
  state.mergedElements = mergedElements;
7266
- if (sorted.length > mergedElements) {
7269
+ if (leafCount > mergedElements) {
7267
7270
  pending.push(...sorted.slice(mergedElements).map((l) => l.commitment));
7268
7271
  }
7269
- const storedTree = await this.storage?.getMerkleTree?.(chainId);
7270
7272
  if (storedTree?.root) {
7271
7273
  state.root = _MerkleEngine.normalizeHex32(storedTree.root, "merkleTree.root");
7272
7274
  } else {
@@ -7316,7 +7318,10 @@ var MerkleEngine = class _MerkleEngine {
7316
7318
  if (!leaves.length) return;
7317
7319
  const sorted = [...leaves].sort((a, b) => a.index - b.index);
7318
7320
  const persistLeaves = sorted.map((l) => ({ cid: l.index, commitment: l.commitment }));
7319
- void this.storage?.appendMerkleLeaves?.(chainId, persistLeaves).catch(() => void 0);
7321
+ try {
7322
+ await this.storage?.appendMerkleLeaves?.(chainId, persistLeaves);
7323
+ } catch {
7324
+ }
7320
7325
  try {
7321
7326
  let expected = state.mergedElements + pending.length;
7322
7327
  for (const leaf of sorted) {