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