@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/browser.js CHANGED
@@ -7189,15 +7189,17 @@ var MerkleEngine = class _MerkleEngine {
7189
7189
  for (let i = 0; i < sorted.length; i++) {
7190
7190
  if (sorted[i].cid !== i) throw new Error(`Non-contiguous persisted merkle leaves: expected cid=${i}, got cid=${sorted[i].cid}`);
7191
7191
  }
7192
- const totalElements = BigInt(sorted.length);
7193
- const mergedElements = _MerkleEngine.totalElementsInTree(totalElements);
7192
+ const storedTree = await this.storage?.getMerkleTree?.(chainId);
7193
+ const leafCount = sorted.length;
7194
+ const mergedFromLeaves = _MerkleEngine.totalElementsInTree(BigInt(leafCount));
7195
+ const mergedFromTree = typeof storedTree?.totalElements === "number" && storedTree.totalElements > 0 ? storedTree.totalElements : 0;
7196
+ const mergedElements = Math.max(mergedFromLeaves, mergedFromTree);
7194
7197
  const pending = this.ensurePendingLeaves(chainId);
7195
7198
  pending.length = 0;
7196
7199
  state.mergedElements = mergedElements;
7197
- if (sorted.length > mergedElements) {
7200
+ if (leafCount > mergedElements) {
7198
7201
  pending.push(...sorted.slice(mergedElements).map((l) => l.commitment));
7199
7202
  }
7200
- const storedTree = await this.storage?.getMerkleTree?.(chainId);
7201
7203
  if (storedTree?.root) {
7202
7204
  state.root = _MerkleEngine.normalizeHex32(storedTree.root, "merkleTree.root");
7203
7205
  } else {
@@ -7247,7 +7249,10 @@ var MerkleEngine = class _MerkleEngine {
7247
7249
  if (!leaves.length) return;
7248
7250
  const sorted = [...leaves].sort((a, b) => a.index - b.index);
7249
7251
  const persistLeaves = sorted.map((l) => ({ cid: l.index, commitment: l.commitment }));
7250
- void this.storage?.appendMerkleLeaves?.(chainId, persistLeaves).catch(() => void 0);
7252
+ try {
7253
+ await this.storage?.appendMerkleLeaves?.(chainId, persistLeaves);
7254
+ } catch {
7255
+ }
7251
7256
  try {
7252
7257
  let expected = state.mergedElements + pending.length;
7253
7258
  for (const leaf of sorted) {