@ocash/sdk 0.1.4-rc.0 → 0.1.4-rc.2

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
@@ -3291,8 +3291,8 @@ var MemoryStore = class {
3291
3291
  this.utxos = /* @__PURE__ */ new Map();
3292
3292
  this.operations = [];
3293
3293
  this.merkleLeavesByChain = /* @__PURE__ */ new Map();
3294
- this.merkleTreesByChain = /* @__PURE__ */ new Map();
3295
- this.merkleNodesByChain = /* @__PURE__ */ new Map();
3294
+ this.chairmanMerkleVersionsByChain = /* @__PURE__ */ new Map();
3295
+ this.chairmanMerkleNodesByChain = /* @__PURE__ */ new Map();
3296
3296
  this.entryMemosByChain = /* @__PURE__ */ new Map();
3297
3297
  this.entryNullifiersByChain = /* @__PURE__ */ new Map();
3298
3298
  const max = options?.maxOperations;
@@ -3308,8 +3308,8 @@ var MemoryStore = class {
3308
3308
  this.utxos.clear();
3309
3309
  this.operations = [];
3310
3310
  this.merkleLeavesByChain.clear();
3311
- this.merkleTreesByChain.clear();
3312
- this.merkleNodesByChain.clear();
3311
+ this.chairmanMerkleVersionsByChain.clear();
3312
+ this.chairmanMerkleNodesByChain.clear();
3313
3313
  this.entryMemosByChain.clear();
3314
3314
  this.entryNullifiersByChain.clear();
3315
3315
  }
@@ -3425,49 +3425,64 @@ var MemoryStore = class {
3425
3425
  return { chainId, cid: row.cid, commitment: row.commitment };
3426
3426
  }
3427
3427
  /**
3428
- * Get a merkle node by id.
3428
+ * Get a chairmanMerkle tree node by id.
3429
3429
  */
3430
- async getMerkleNode(chainId, id) {
3431
- return this.merkleNodesByChain.get(chainId)?.get(id);
3430
+ async getChairmanMerkleNode(chainId, id) {
3431
+ return this.chairmanMerkleNodesByChain.get(chainId)?.get(id);
3432
3432
  }
3433
3433
  /**
3434
- * Upsert merkle nodes for a chain.
3434
+ * Put chairmanMerkle tree nodes for a chain.
3435
3435
  */
3436
- async upsertMerkleNodes(chainId, nodes) {
3436
+ async putChairmanMerkleNodes(chainId, nodes) {
3437
3437
  if (!nodes.length) return;
3438
- let map = this.merkleNodesByChain.get(chainId);
3438
+ let map = this.chairmanMerkleNodesByChain.get(chainId);
3439
3439
  if (!map) {
3440
3440
  map = /* @__PURE__ */ new Map();
3441
- this.merkleNodesByChain.set(chainId, map);
3441
+ this.chairmanMerkleNodesByChain.set(chainId, map);
3442
3442
  }
3443
3443
  for (const node of nodes) {
3444
3444
  map.set(node.id, { ...node, chainId });
3445
3445
  }
3446
3446
  }
3447
3447
  /**
3448
- * Clear merkle nodes for a chain.
3448
+ * Get a chairmanMerkle version record by chain and version number.
3449
3449
  */
3450
- async clearMerkleNodes(chainId) {
3451
- this.merkleNodesByChain.delete(chainId);
3450
+ async getChairmanMerkleVersion(chainId, version) {
3451
+ const byVersion = this.chairmanMerkleVersionsByChain.get(chainId);
3452
+ const record = byVersion?.get(version);
3453
+ return record ? { ...record } : void 0;
3452
3454
  }
3453
3455
  /**
3454
- * Get persisted merkle tree state.
3456
+ * Get the latest chairmanMerkle version record (highest version number) for a chain.
3455
3457
  */
3456
- async getMerkleTree(chainId) {
3457
- const tree = this.merkleTreesByChain.get(chainId);
3458
- return tree ? { ...tree } : void 0;
3458
+ async getLatestChairmanMerkleVersion(chainId) {
3459
+ const byVersion = this.chairmanMerkleVersionsByChain.get(chainId);
3460
+ if (!byVersion || byVersion.size === 0) return void 0;
3461
+ let latest;
3462
+ for (const record of byVersion.values()) {
3463
+ if (!latest || record.version > latest.version) {
3464
+ latest = record;
3465
+ }
3466
+ }
3467
+ return latest ? { ...latest } : void 0;
3459
3468
  }
3460
3469
  /**
3461
- * Persist merkle tree state.
3470
+ * Persist a chairmanMerkle version record.
3462
3471
  */
3463
- async setMerkleTree(chainId, tree) {
3464
- this.merkleTreesByChain.set(chainId, { ...tree, chainId });
3472
+ async putChairmanMerkleVersion(chainId, record) {
3473
+ let byVersion = this.chairmanMerkleVersionsByChain.get(chainId);
3474
+ if (!byVersion) {
3475
+ byVersion = /* @__PURE__ */ new Map();
3476
+ this.chairmanMerkleVersionsByChain.set(chainId, byVersion);
3477
+ }
3478
+ byVersion.set(record.version, { ...record, chainId });
3465
3479
  }
3466
3480
  /**
3467
- * Clear merkle tree state.
3481
+ * Clear all chairmanMerkle tree state (both nodes and versions) for a chain.
3468
3482
  */
3469
- async clearMerkleTree(chainId) {
3470
- this.merkleTreesByChain.delete(chainId);
3483
+ async clearChairmanMerkleTree(chainId) {
3484
+ this.chairmanMerkleNodesByChain.delete(chainId);
3485
+ this.chairmanMerkleVersionsByChain.delete(chainId);
3471
3486
  }
3472
3487
  /**
3473
3488
  * Upsert entry memos (raw EntryService cache).
@@ -3991,13 +4006,14 @@ var KeyValueStore = class {
3991
4006
  this.utxoCache = /* @__PURE__ */ new Map();
3992
4007
  this.operationCache = /* @__PURE__ */ new Map();
3993
4008
  this.merkleLeafCids = {};
3994
- this.merkleTrees = {};
3995
- this.merkleNodeIds = {};
4009
+ this.chairmanMerkleLatestVersions = {};
4010
+ this.chairmanMerkleNodeIds = {};
4011
+ this.chairmanMerkleVersionNums = {};
3996
4012
  this.entryMemoCids = {};
3997
4013
  this.entryNullifierNids = {};
3998
4014
  this.loadedMerkleLeaves = /* @__PURE__ */ new Set();
3999
- this.loadedMerkleTrees = /* @__PURE__ */ new Set();
4000
- this.loadedMerkleNodes = /* @__PURE__ */ new Set();
4015
+ this.loadedChairmanMerkleVersions = /* @__PURE__ */ new Set();
4016
+ this.loadedChairmanMerkleNodes = /* @__PURE__ */ new Set();
4001
4017
  this.loadedEntryMemos = /* @__PURE__ */ new Set();
4002
4018
  this.loadedEntryNullifiers = /* @__PURE__ */ new Set();
4003
4019
  this.saveChain = Promise.resolve();
@@ -4031,9 +4047,6 @@ var KeyValueStore = class {
4031
4047
  walletOperationKey(id) {
4032
4048
  return `${this.walletBaseKey()}:operation:${id}`;
4033
4049
  }
4034
- sharedChainKey(part, chainId) {
4035
- return `${this.keyPrefix()}:shared:${part}:${chainId}`;
4036
- }
4037
4050
  sharedChainMetaKey(part, chainId) {
4038
4051
  return `${this.keyPrefix()}:shared:${part}:${chainId}:meta`;
4039
4052
  }
@@ -4086,13 +4099,14 @@ var KeyValueStore = class {
4086
4099
  this.operationCache.clear();
4087
4100
  this.walletMetaLoaded = false;
4088
4101
  this.merkleLeafCids = {};
4089
- this.merkleTrees = {};
4090
- this.merkleNodeIds = {};
4102
+ this.chairmanMerkleLatestVersions = {};
4103
+ this.chairmanMerkleNodeIds = {};
4104
+ this.chairmanMerkleVersionNums = {};
4091
4105
  this.entryMemoCids = {};
4092
4106
  this.entryNullifierNids = {};
4093
4107
  this.loadedMerkleLeaves.clear();
4094
- this.loadedMerkleTrees.clear();
4095
- this.loadedMerkleNodes.clear();
4108
+ this.loadedChairmanMerkleVersions.clear();
4109
+ this.loadedChairmanMerkleNodes.clear();
4096
4110
  this.loadedEntryMemos.clear();
4097
4111
  this.loadedEntryNullifiers.clear();
4098
4112
  }
@@ -4187,20 +4201,19 @@ var KeyValueStore = class {
4187
4201
  this.merkleLeafCids[key] = new Set(this.parseNumberIndex(cidsRaw));
4188
4202
  this.loadedMerkleLeaves.add(chainId);
4189
4203
  }
4190
- async ensureMerkleTreeLoaded(chainId) {
4191
- if (this.loadedMerkleTrees.has(chainId)) return;
4204
+ async ensureChairmanMerkleVersionsLoaded(chainId) {
4205
+ if (this.loadedChairmanMerkleVersions.has(chainId)) return;
4192
4206
  const key = String(chainId);
4193
- const raw = await this.options.client.get(this.sharedChainKey("merkleTrees", chainId));
4194
- const row = this.parseJson(raw, null);
4195
- if (row && typeof row === "object") this.merkleTrees[key] = row;
4196
- this.loadedMerkleTrees.add(chainId);
4207
+ const numsRaw = await this.options.client.get(this.sharedChainMetaKey("chairmanMerkleVersions", chainId));
4208
+ this.chairmanMerkleVersionNums[key] = new Set(this.parseNumberIndex(numsRaw));
4209
+ this.loadedChairmanMerkleVersions.add(chainId);
4197
4210
  }
4198
- async ensureMerkleNodesLoaded(chainId) {
4199
- if (this.loadedMerkleNodes.has(chainId)) return;
4211
+ async ensureChairmanMerkleNodesLoaded(chainId) {
4212
+ if (this.loadedChairmanMerkleNodes.has(chainId)) return;
4200
4213
  const key = String(chainId);
4201
- const idsRaw = await this.options.client.get(this.sharedChainMetaKey("merkleNodes", chainId));
4202
- this.merkleNodeIds[key] = new Set(this.parseStringIndex(idsRaw));
4203
- this.loadedMerkleNodes.add(chainId);
4214
+ const idsRaw = await this.options.client.get(this.sharedChainMetaKey("chairmanMerkleNodes", chainId));
4215
+ this.chairmanMerkleNodeIds[key] = new Set(this.parseStringIndex(idsRaw));
4216
+ this.loadedChairmanMerkleNodes.add(chainId);
4204
4217
  }
4205
4218
  async ensureEntryMemosLoaded(chainId) {
4206
4219
  if (this.loadedEntryMemos.has(chainId)) return;
@@ -4216,64 +4229,88 @@ var KeyValueStore = class {
4216
4229
  this.entryNullifierNids[key] = new Set(this.parseNumberIndex(nidsRaw));
4217
4230
  this.loadedEntryNullifiers.add(chainId);
4218
4231
  }
4219
- async getMerkleNode(chainId, id) {
4220
- await this.ensureMerkleNodesLoaded(chainId);
4221
- if (!this.merkleNodeIds[String(chainId)]?.has(id)) return void 0;
4222
- const raw = await this.options.client.get(this.sharedRecordKey("merkleNodes", chainId, id));
4232
+ async getChairmanMerkleNode(chainId, id) {
4233
+ await this.ensureChairmanMerkleNodesLoaded(chainId);
4234
+ if (!this.chairmanMerkleNodeIds[String(chainId)]?.has(id)) return void 0;
4235
+ const raw = await this.options.client.get(this.sharedRecordKey("chairmanMerkleNodes", chainId, id));
4223
4236
  const node = this.parseJson(raw, null);
4224
4237
  if (!node) return void 0;
4225
4238
  const hash = node.hash;
4226
4239
  if (typeof hash !== "string" || !hash.startsWith("0x")) return void 0;
4227
4240
  return { ...node, chainId };
4228
4241
  }
4229
- async upsertMerkleNodes(chainId, nodes) {
4242
+ async putChairmanMerkleNodes(chainId, nodes) {
4230
4243
  if (!nodes.length) return;
4231
- await this.ensureMerkleNodesLoaded(chainId);
4244
+ await this.ensureChairmanMerkleNodesLoaded(chainId);
4232
4245
  const key = String(chainId);
4233
- const ids = this.merkleNodeIds[key] ?? /* @__PURE__ */ new Set();
4246
+ const ids = this.chairmanMerkleNodeIds[key] ?? /* @__PURE__ */ new Set();
4234
4247
  const beforeSize = ids.size;
4235
4248
  for (const node of nodes) {
4236
4249
  ids.add(node.id);
4237
4250
  }
4238
- this.merkleNodeIds[key] = ids;
4251
+ this.chairmanMerkleNodeIds[key] = ids;
4239
4252
  await this.enqueueWrite(async () => {
4240
- await Promise.all(nodes.map((node) => this.writeJson(this.sharedRecordKey("merkleNodes", chainId, node.id), { ...node, chainId })));
4253
+ await Promise.all(nodes.map((node) => this.writeJson(this.sharedRecordKey("chairmanMerkleNodes", chainId, node.id), { ...node, chainId })));
4241
4254
  if (ids.size !== beforeSize) {
4242
- await this.writeJson(this.sharedChainMetaKey("merkleNodes", chainId), Array.from(ids));
4255
+ await this.writeJson(this.sharedChainMetaKey("chairmanMerkleNodes", chainId), Array.from(ids));
4243
4256
  }
4244
4257
  });
4245
4258
  }
4246
- async clearMerkleNodes(chainId) {
4247
- await this.ensureMerkleNodesLoaded(chainId);
4248
- const ids = Array.from(this.merkleNodeIds[String(chainId)] ?? []);
4249
- delete this.merkleNodeIds[String(chainId)];
4259
+ async getChairmanMerkleVersion(chainId, version) {
4260
+ await this.ensureChairmanMerkleVersionsLoaded(chainId);
4261
+ if (!this.chairmanMerkleVersionNums[String(chainId)]?.has(version)) return void 0;
4262
+ const raw = await this.options.client.get(this.sharedRecordKey("chairmanMerkleVersions", chainId, version));
4263
+ const record = this.parseJson(raw, null);
4264
+ if (!record) return void 0;
4265
+ if (typeof record.rootHash !== "string" || !record.rootHash.startsWith("0x")) return void 0;
4266
+ if (typeof record.rootId !== "string") return void 0;
4267
+ const v = Number(record.version);
4268
+ if (!Number.isFinite(v) || v < 0) return void 0;
4269
+ return { chainId, version: Math.floor(v), rootId: record.rootId, rootHash: record.rootHash };
4270
+ }
4271
+ async getLatestChairmanMerkleVersion(chainId) {
4272
+ await this.ensureChairmanMerkleVersionsLoaded(chainId);
4273
+ const nums = this.chairmanMerkleVersionNums[String(chainId)];
4274
+ if (!nums || nums.size === 0) return void 0;
4275
+ const maxVersion = Math.max(...nums);
4276
+ return this.getChairmanMerkleVersion(chainId, maxVersion);
4277
+ }
4278
+ async putChairmanMerkleVersion(chainId, record) {
4279
+ await this.ensureChairmanMerkleVersionsLoaded(chainId);
4280
+ const key = String(chainId);
4281
+ const nums = this.chairmanMerkleVersionNums[key] ?? /* @__PURE__ */ new Set();
4282
+ const beforeSize = nums.size;
4283
+ nums.add(record.version);
4284
+ this.chairmanMerkleVersionNums[key] = nums;
4285
+ const current = this.chairmanMerkleLatestVersions[key];
4286
+ if (!current || record.version >= current.version) {
4287
+ this.chairmanMerkleLatestVersions[key] = { ...record, chainId };
4288
+ }
4289
+ const row = { ...record, chainId };
4250
4290
  await this.enqueueWrite(async () => {
4251
- await Promise.all(ids.map((id) => this.deleteOrReset(this.sharedRecordKey("merkleNodes", chainId, id), null)));
4252
- await this.deleteOrReset(this.sharedChainMetaKey("merkleNodes", chainId), []);
4291
+ await this.writeJson(this.sharedRecordKey("chairmanMerkleVersions", chainId, record.version), row);
4292
+ if (nums.size !== beforeSize) {
4293
+ await this.writeJson(this.sharedChainMetaKey("chairmanMerkleVersions", chainId), Array.from(nums).sort((a, b) => a - b));
4294
+ }
4253
4295
  });
4254
4296
  }
4255
- async getMerkleTree(chainId) {
4256
- await this.ensureMerkleTreeLoaded(chainId);
4257
- const row = this.merkleTrees[String(chainId)];
4258
- if (!row) return void 0;
4259
- const totalElements = Number(row.totalElements);
4260
- const lastUpdated = Number(row.lastUpdated);
4261
- const root = row.root;
4262
- if (typeof root !== "string" || !root.startsWith("0x")) return void 0;
4263
- if (!Number.isFinite(totalElements) || totalElements < 0) return void 0;
4264
- return { chainId, root, totalElements: Math.floor(totalElements), lastUpdated: Number.isFinite(lastUpdated) ? Math.floor(lastUpdated) : 0 };
4265
- }
4266
- async setMerkleTree(chainId, tree) {
4267
- await this.ensureMerkleTreeLoaded(chainId);
4268
- const row = { ...tree, chainId };
4269
- this.merkleTrees[String(chainId)] = row;
4270
- await this.enqueueWrite(() => this.writeJson(this.sharedChainKey("merkleTrees", chainId), row));
4271
- }
4272
- async clearMerkleTree(chainId) {
4273
- await this.ensureMerkleTreeLoaded(chainId);
4274
- delete this.merkleTrees[String(chainId)];
4297
+ async clearChairmanMerkleTree(chainId) {
4298
+ await this.ensureChairmanMerkleNodesLoaded(chainId);
4299
+ await this.ensureChairmanMerkleVersionsLoaded(chainId);
4300
+ const nodeIds = Array.from(this.chairmanMerkleNodeIds[String(chainId)] ?? []);
4301
+ const versionNums = Array.from(this.chairmanMerkleVersionNums[String(chainId)] ?? []);
4302
+ delete this.chairmanMerkleNodeIds[String(chainId)];
4303
+ delete this.chairmanMerkleVersionNums[String(chainId)];
4304
+ delete this.chairmanMerkleLatestVersions[String(chainId)];
4275
4305
  await this.enqueueWrite(async () => {
4276
- await this.deleteOrReset(this.sharedChainKey("merkleTrees", chainId), null);
4306
+ await Promise.all([
4307
+ ...nodeIds.map((id) => this.deleteOrReset(this.sharedRecordKey("chairmanMerkleNodes", chainId, id), null)),
4308
+ ...versionNums.map((v) => this.deleteOrReset(this.sharedRecordKey("chairmanMerkleVersions", chainId, v), null))
4309
+ ]);
4310
+ await Promise.all([
4311
+ this.deleteOrReset(this.sharedChainMetaKey("chairmanMerkleNodes", chainId), []),
4312
+ this.deleteOrReset(this.sharedChainMetaKey("chairmanMerkleVersions", chainId), [])
4313
+ ]);
4277
4314
  });
4278
4315
  }
4279
4316
  async upsertEntryMemos(memos) {
@@ -7117,6 +7154,7 @@ var MerkleEngine = class _MerkleEngine {
7117
7154
  this.hydrateInFlight = /* @__PURE__ */ new Map();
7118
7155
  this.mode = options?.mode ?? "hybrid";
7119
7156
  this.treeDepth = Math.max(1, Math.floor(options?.treeDepth ?? TREE_DEPTH_DEFAULT));
7157
+ this.readContractRoot = options?.readContractRoot;
7120
7158
  }
7121
7159
  /**
7122
7160
  * Compute the current merkle root index from total elements.
@@ -7125,9 +7163,6 @@ var MerkleEngine = class _MerkleEngine {
7125
7163
  if (totalElements <= tempArraySize) return 0;
7126
7164
  return Math.floor((totalElements - 1) / tempArraySize);
7127
7165
  }
7128
- /**
7129
- * Get or initialize the pending leaf buffer for a chain.
7130
- */
7131
7166
  ensurePendingLeaves(chainId) {
7132
7167
  let pending = this.pendingLeavesByChain.get(chainId);
7133
7168
  if (!pending) {
@@ -7136,9 +7171,6 @@ var MerkleEngine = class _MerkleEngine {
7136
7171
  }
7137
7172
  return pending;
7138
7173
  }
7139
- /**
7140
- * Get or initialize chain-level merkle state.
7141
- */
7142
7174
  ensureChainState(chainId) {
7143
7175
  let state = this.chainStateByChain.get(chainId);
7144
7176
  if (!state) {
@@ -7147,15 +7179,32 @@ var MerkleEngine = class _MerkleEngine {
7147
7179
  }
7148
7180
  return state;
7149
7181
  }
7150
- /**
7151
- * Poseidon2 merkle hash for a left/right pair.
7152
- */
7182
+ // ── Hashing ──
7153
7183
  static hashPair(left, right) {
7154
7184
  return Poseidon2.hashToHex(BigInt(left), BigInt(right), Poseidon2Domain.Merkle);
7155
7185
  }
7186
+ static normalizeHex32(value, name) {
7187
+ try {
7188
+ const bi = BigInt(value);
7189
+ if (bi < 0n) throw new Error("negative");
7190
+ const hex = bi.toString(16).padStart(64, "0");
7191
+ if (hex.length > 64) throw new Error("too_large");
7192
+ return `0x${hex}`;
7193
+ } catch (error) {
7194
+ throw new SdkError("MERKLE", `Invalid ${name}`, { value }, error);
7195
+ }
7196
+ }
7197
+ // ── Static helpers ──
7198
+ static totalElementsInTree(totalElements, tempArraySize = TEMP_ARRAY_SIZE_DEFAULT) {
7199
+ if (tempArraySize <= 0) throw new SdkError("MERKLE", "tempArraySize must be greater than zero", { tempArraySize });
7200
+ if (totalElements <= 0n) return 0;
7201
+ const size = BigInt(tempArraySize);
7202
+ return Number((totalElements - 1n) / size * size);
7203
+ }
7204
+ // ── Subtree (levels 0-5, 32 leaves → 1 root) ──
7156
7205
  /**
7157
7206
  * Build a fixed-depth subtree from 32 contiguous leaves.
7158
- * Returns the subtree root and all intermediate nodes for storage.
7207
+ * Returns the subtree root hash and all intermediate nodes for storage.
7159
7208
  */
7160
7209
  static buildSubtree(leafCommitments, baseIndex) {
7161
7210
  if (leafCommitments.length !== SUBTREE_SIZE) {
@@ -7176,71 +7225,71 @@ var MerkleEngine = class _MerkleEngine {
7176
7225
  const position = basePos + i;
7177
7226
  nodesToStore.push({
7178
7227
  chainId: 0,
7179
- id: `${level}-${position}`,
7180
- level,
7181
- position,
7182
- hash: next[i]
7228
+ id: `st-${level}-${position}`,
7229
+ hash: next[i],
7230
+ leftId: null,
7231
+ rightId: null
7183
7232
  });
7184
7233
  }
7185
7234
  currentLevel = next;
7186
7235
  }
7187
7236
  return { subtreeRoot: currentLevel[0], nodesToStore };
7188
7237
  }
7238
+ // ── ChairmanMerkle tree (persistent segment tree, levels 5-32) ──
7189
7239
  /**
7190
- * Fetch a node hash from storage if available.
7191
- */
7192
- async getNodeHash(chainId, id) {
7193
- const node = await this.storage?.getMerkleNode?.(chainId, id);
7194
- return node?.hash;
7195
- }
7196
- /**
7197
- * Merge a completed subtree root into the main tree, updating frontier nodes.
7198
- */
7199
- async mergeSubtreeToMainTree(input) {
7200
- let currentValue = input.subtreeRoot;
7201
- let frontierUpdated = false;
7202
- const nodesToStore = [];
7203
- for (let level = SUBTREE_DEPTH; level < this.treeDepth; level++) {
7204
- const nodeIndex = input.newTotalElements - 1 >> level;
7205
- if ((nodeIndex & 1) === 0) {
7206
- if (!frontierUpdated) {
7207
- nodesToStore.push({
7208
- chainId: input.chainId,
7209
- id: `frontier-${level}`,
7210
- level,
7211
- position: nodeIndex,
7212
- hash: currentValue
7213
- });
7214
- frontierUpdated = true;
7240
+ * Insert a subtree root into the persistent main tree.
7241
+ *
7242
+ * Top-down recursive: descends from root (level treeDepth) to the target
7243
+ * leaf position (level SUBTREE_DEPTH). At each level only the node on the
7244
+ * update path is newly created; the sibling is shared from the previous
7245
+ * version's tree.
7246
+ *
7247
+ * @returns new root node ID/hash and all newly created nodes.
7248
+ */
7249
+ async insertSubtreeRoot(chainId, prevRootId, subtreeRootHash, batchIndex, version) {
7250
+ const MAIN_DEPTH = this.treeDepth - SUBTREE_DEPTH;
7251
+ const nodes = [];
7252
+ const descend = async (nodeId, depth) => {
7253
+ const originalLevel = this.treeDepth - depth;
7254
+ if (depth === MAIN_DEPTH) {
7255
+ const newId2 = `cm-${version}-${originalLevel}`;
7256
+ nodes.push({ chainId, id: newId2, hash: subtreeRootHash, leftId: null, rightId: null });
7257
+ return { id: newId2, hash: subtreeRootHash };
7258
+ }
7259
+ let prevLeftId = null;
7260
+ let prevRightId = null;
7261
+ if (nodeId) {
7262
+ const prevNode = await this.storage?.getChairmanMerkleNode?.(chainId, nodeId);
7263
+ if (prevNode) {
7264
+ prevLeftId = prevNode.leftId;
7265
+ prevRightId = prevNode.rightId;
7215
7266
  }
7216
- currentValue = _MerkleEngine.hashPair(currentValue, getZeroHash(level));
7217
- } else {
7218
- const leftHash = await this.getNodeHash(input.chainId, `frontier-${level}`) ?? getZeroHash(level);
7219
- currentValue = _MerkleEngine.hashPair(leftHash, currentValue);
7220
7267
  }
7221
- const nextLevel = level + 1;
7222
- nodesToStore.push({
7223
- chainId: input.chainId,
7224
- id: `${nextLevel}-${nodeIndex >> 1}`,
7225
- level: nextLevel,
7226
- position: nodeIndex >> 1,
7227
- hash: currentValue
7228
- });
7229
- }
7230
- return { finalRoot: currentValue, nodesToStore };
7231
- }
7232
- /**
7233
- * Convert on-chain totalElements to the count of fully merged elements.
7234
- */
7235
- static totalElementsInTree(totalElements, tempArraySize = TEMP_ARRAY_SIZE_DEFAULT) {
7236
- if (tempArraySize <= 0) throw new SdkError("MERKLE", "tempArraySize must be greater than zero", { tempArraySize });
7237
- if (totalElements <= 0n) return 0;
7238
- const size = BigInt(tempArraySize);
7239
- return Number((totalElements - 1n) / size * size);
7268
+ const childLevel = originalLevel - 1;
7269
+ const remainingDepth = MAIN_DEPTH - depth - 1;
7270
+ const goRight = (batchIndex >> remainingDepth & 1) === 1;
7271
+ let leftResult;
7272
+ let rightResult;
7273
+ if (goRight) {
7274
+ const leftHash = prevLeftId ? (await this.storage?.getChairmanMerkleNode?.(chainId, prevLeftId))?.hash ?? getZeroHash(childLevel) : getZeroHash(childLevel);
7275
+ leftResult = { id: prevLeftId, hash: leftHash };
7276
+ const right = await descend(prevRightId, depth + 1);
7277
+ rightResult = { id: right.id, hash: right.hash };
7278
+ } else {
7279
+ const left = await descend(prevLeftId, depth + 1);
7280
+ leftResult = { id: left.id, hash: left.hash };
7281
+ const rightHash = prevRightId ? (await this.storage?.getChairmanMerkleNode?.(chainId, prevRightId))?.hash ?? getZeroHash(childLevel) : getZeroHash(childLevel);
7282
+ rightResult = { id: prevRightId, hash: rightHash };
7283
+ }
7284
+ const hash = _MerkleEngine.hashPair(leftResult.hash, rightResult.hash);
7285
+ const newId = `cm-${version}-${originalLevel}`;
7286
+ nodes.push({ chainId, id: newId, hash, leftId: leftResult.id, rightId: rightResult.id });
7287
+ return { id: newId, hash };
7288
+ };
7289
+ const root = await descend(prevRootId, 0);
7290
+ return { rootId: root.id, rootHash: root.hash, nodes };
7240
7291
  }
7241
- /**
7242
- * Hydrate local merkle state from storage on first use.
7243
- */
7292
+ // ── Hydration ──
7244
7293
  async hydrateFromStorage(chainId) {
7245
7294
  if (this.mode === "remote") return;
7246
7295
  if (this.hydratedChains.has(chainId)) return;
@@ -7249,31 +7298,17 @@ var MerkleEngine = class _MerkleEngine {
7249
7298
  const task = (async () => {
7250
7299
  try {
7251
7300
  const state = this.ensureChainState(chainId);
7252
- const leaves = await this.storage?.getMerkleLeaves?.(chainId);
7253
- if (!leaves || leaves.length === 0) return;
7254
- const sorted = [...leaves].map((l) => ({
7255
- cid: l.cid,
7256
- commitment: _MerkleEngine.normalizeHex32(l.commitment, "memo.commitment")
7257
- })).sort((a, b) => a.cid - b.cid);
7258
- for (let i = 0; i < sorted.length; i++) {
7259
- if (sorted[i].cid !== i) throw new Error(`Non-contiguous persisted merkle leaves: expected cid=${i}, got cid=${sorted[i].cid}`);
7260
- }
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);
7266
7301
  const pending = this.ensurePendingLeaves(chainId);
7267
- pending.length = 0;
7268
- state.mergedElements = mergedElements;
7269
- if (leafCount > mergedElements) {
7270
- pending.push(...sorted.slice(mergedElements).map((l) => l.commitment));
7302
+ const latest = await this.storage?.getLatestChairmanMerkleVersion?.(chainId);
7303
+ if (latest) {
7304
+ state.mergedElements = latest.version;
7305
+ state.root = _MerkleEngine.normalizeHex32(latest.rootHash, "chairmanMerkleVersion.rootHash");
7271
7306
  }
7272
- if (storedTree?.root) {
7273
- state.root = _MerkleEngine.normalizeHex32(storedTree.root, "merkleTree.root");
7274
- } else {
7275
- const rootNode = await this.storage?.getMerkleNode?.(chainId, `${this.treeDepth}-0`);
7276
- state.root = rootNode?.hash ?? getZeroHash(this.treeDepth);
7307
+ const leaves = await this.storage?.getMerkleLeaves?.(chainId);
7308
+ if (leaves && leaves.length > state.mergedElements) {
7309
+ const sorted = [...leaves].sort((a, b) => a.cid - b.cid).slice(state.mergedElements);
7310
+ pending.length = 0;
7311
+ pending.push(...sorted.map((l) => _MerkleEngine.normalizeHex32(l.commitment, "leaf.commitment")));
7277
7312
  }
7278
7313
  } catch (error) {
7279
7314
  if (this.mode === "hybrid") return;
@@ -7286,26 +7321,7 @@ var MerkleEngine = class _MerkleEngine {
7286
7321
  this.hydrateInFlight.set(chainId, task);
7287
7322
  return task;
7288
7323
  }
7289
- /**
7290
- * Normalize unknown values to a 32-byte hex string.
7291
- */
7292
- static normalizeHex32(value, name) {
7293
- try {
7294
- const bi = BigInt(value);
7295
- if (bi < 0n) throw new Error("negative");
7296
- const hex = bi.toString(16).padStart(64, "0");
7297
- if (hex.length > 64) throw new Error("too_large");
7298
- return `0x${hex}`;
7299
- } catch (error) {
7300
- throw new SdkError("MERKLE", `Invalid ${name}`, { value }, error);
7301
- }
7302
- }
7303
- /**
7304
- * Feed contiguous (cid-ordered) memo leaves into the local merkle tree.
7305
- *
7306
- * This mirrors the client/app behavior: only after we have a full consecutive batch of 32 leaves
7307
- * do we merge them into the main tree. Leaves that are still in the buffer do not get local proofs.
7308
- */
7324
+ // ── Ingestion ──
7309
7325
  async ingestEntryMemos(chainId, memos) {
7310
7326
  if (this.mode === "remote") return;
7311
7327
  await this.hydrateFromStorage(chainId);
@@ -7333,26 +7349,42 @@ var MerkleEngine = class _MerkleEngine {
7333
7349
  expected++;
7334
7350
  while (pending.length >= SUBTREE_SIZE) {
7335
7351
  const batch = pending.splice(0, SUBTREE_SIZE);
7336
- const baseIndex = state.mergedElements;
7337
- const subtree = _MerkleEngine.buildSubtree(batch, baseIndex);
7338
- const merged = await this.mergeSubtreeToMainTree({ chainId, subtreeRoot: subtree.subtreeRoot, newTotalElements: baseIndex + SUBTREE_SIZE });
7339
- state.mergedElements += SUBTREE_SIZE;
7340
- state.root = merged.finalRoot;
7341
- const checkpointNode = {
7342
- chainId,
7343
- id: `checkpoint-${state.mergedElements}`,
7344
- level: -1,
7345
- position: 0,
7346
- hash: state.root
7347
- };
7348
- const nodes = [...subtree.nodesToStore, ...merged.nodesToStore, checkpointNode].map((n) => ({ ...n, chainId }));
7349
- await this.storage?.upsertMerkleNodes?.(chainId, nodes);
7350
- await this.storage?.setMerkleTree?.(chainId, {
7352
+ const batchIndex = state.mergedElements / SUBTREE_SIZE;
7353
+ const subtree = _MerkleEngine.buildSubtree(batch, state.mergedElements);
7354
+ const subtreeNodes = subtree.nodesToStore.map((n) => ({ ...n, chainId }));
7355
+ const prevVersion = await this.storage?.getLatestChairmanMerkleVersion?.(chainId);
7356
+ const prevRootId = prevVersion?.rootId ?? null;
7357
+ const newVersion = state.mergedElements + SUBTREE_SIZE;
7358
+ const result = await this.insertSubtreeRoot(chainId, prevRootId, subtree.subtreeRoot, batchIndex, newVersion);
7359
+ if (this.readContractRoot) {
7360
+ const rootIndex = newVersion / SUBTREE_SIZE;
7361
+ const onChainRoot = await this.readContractRoot(chainId, rootIndex).catch(() => null);
7362
+ if (onChainRoot !== null) {
7363
+ const onChainNorm = _MerkleEngine.normalizeHex32(onChainRoot, "onChainRoot");
7364
+ const isZero = BigInt(onChainNorm) === 0n;
7365
+ if (!isZero && onChainNorm !== result.rootHash) {
7366
+ const target = state.mergedElements;
7367
+ await this.rollback(chainId, target);
7368
+ throw new SdkError("MERKLE", "Local merkle root mismatch with on-chain root \u2014 rolled back", {
7369
+ chainId,
7370
+ rootIndex,
7371
+ localRoot: result.rootHash,
7372
+ onChainRoot: onChainNorm,
7373
+ version: newVersion,
7374
+ rollbackTarget: target
7375
+ });
7376
+ }
7377
+ }
7378
+ }
7379
+ await this.storage?.putChairmanMerkleNodes?.(chainId, [...subtreeNodes, ...result.nodes]);
7380
+ await this.storage?.putChairmanMerkleVersion?.(chainId, {
7351
7381
  chainId,
7352
- root: state.root,
7353
- totalElements: state.mergedElements,
7354
- lastUpdated: Date.now()
7382
+ version: newVersion,
7383
+ rootId: result.rootId,
7384
+ rootHash: result.rootHash
7355
7385
  });
7386
+ state.mergedElements = newVersion;
7387
+ state.root = result.rootHash;
7356
7388
  }
7357
7389
  }
7358
7390
  this.hydratedChains.add(chainId);
@@ -7363,15 +7395,59 @@ var MerkleEngine = class _MerkleEngine {
7363
7395
  throw new SdkError("MERKLE", "Failed to ingest local merkle leaves", { chainId, leafCount: leaves.length }, error);
7364
7396
  }
7365
7397
  }
7398
+ // ── Rollback (tree O(1) + sync cursor reset) ──
7366
7399
  /**
7367
- * Convenience wrapper to request a single proof.
7400
+ * Unified rollback: rewind tree to a previous batch boundary AND reset the
7401
+ * sync cursor so memo sync restarts from the same point.
7402
+ *
7403
+ * What gets rolled back:
7404
+ * - ChairmanMerkle tree version pointer (O(1) — old nodes still in storage)
7405
+ * - Pending leaves buffer (cleared)
7406
+ * - Sync cursor: memo + merkle fields (nullifier left unchanged — independent)
7407
+ *
7408
+ * @param targetMergedElements Must be a non-negative multiple of 32.
7409
+ * Pass 0 to reset to the empty tree.
7410
+ * @returns true if rollback succeeded, false if the target version doesn't exist.
7368
7411
  */
7369
- async getProofByCid(input) {
7370
- return this.getProofByCids({ chainId: input.chainId, cids: [input.cid], totalElements: input.totalElements });
7412
+ async rollback(chainId, targetMergedElements) {
7413
+ if (targetMergedElements < 0 || targetMergedElements % SUBTREE_SIZE !== 0) {
7414
+ throw new SdkError("MERKLE", "rollback target must be a non-negative multiple of 32", { targetMergedElements });
7415
+ }
7416
+ const state = this.ensureChainState(chainId);
7417
+ const pending = this.ensurePendingLeaves(chainId);
7418
+ if (targetMergedElements === 0) {
7419
+ state.mergedElements = 0;
7420
+ state.root = getZeroHash(this.treeDepth);
7421
+ pending.length = 0;
7422
+ await this.resetSyncCursor(chainId, 0);
7423
+ return true;
7424
+ }
7425
+ const version = await this.storage?.getChairmanMerkleVersion?.(chainId, targetMergedElements);
7426
+ if (!version) return false;
7427
+ state.mergedElements = targetMergedElements;
7428
+ state.root = _MerkleEngine.normalizeHex32(version.rootHash, "version.rootHash");
7429
+ pending.length = 0;
7430
+ this.hydratedChains.add(chainId);
7431
+ await this.resetSyncCursor(chainId, targetMergedElements);
7432
+ return true;
7371
7433
  }
7372
7434
  /**
7373
- * Get merkle proofs for a set of cids using local/hybrid/remote logic.
7435
+ * Reset the sync cursor's memo field to `targetMemo` (and derive merkle cursor),
7436
+ * but only if the current cursor is ahead of the target.
7437
+ * Nullifier cursor is left unchanged — nullifiers are independent of tree state.
7374
7438
  */
7439
+ async resetSyncCursor(chainId, targetMemo) {
7440
+ if (!this.storage?.getSyncCursor || !this.storage?.setSyncCursor) return;
7441
+ const cursor = await this.storage.getSyncCursor(chainId);
7442
+ if (!cursor || cursor.memo <= targetMemo) return;
7443
+ cursor.memo = targetMemo;
7444
+ cursor.merkle = this.currentMerkleRootIndex(targetMemo);
7445
+ await this.storage.setSyncCursor(chainId, cursor);
7446
+ }
7447
+ // ── Proof generation ──
7448
+ async getProofByCid(input) {
7449
+ return this.getProofByCids({ chainId: input.chainId, cids: [input.cid], totalElements: input.totalElements });
7450
+ }
7375
7451
  async getProofByCids(input) {
7376
7452
  const cids = [...input.cids];
7377
7453
  if (cids.length === 0) throw new SdkError("MERKLE", "No cids provided", { chainId: input.chainId });
@@ -7386,15 +7462,16 @@ var MerkleEngine = class _MerkleEngine {
7386
7462
  await this.hydrateFromStorage(input.chainId);
7387
7463
  const canUseLocal = this.mode !== "remote";
7388
7464
  if (canUseLocal) {
7389
- const tree = await this.storage?.getMerkleTree?.(input.chainId);
7390
- const hasDb = typeof this.storage?.getMerkleLeaf === "function" && typeof this.storage?.getMerkleNode === "function" && typeof tree?.totalElements === "number" && typeof tree?.root === "string";
7391
- if (hasDb && tree) {
7392
- if (tree.totalElements < contractTreeElements) {
7465
+ const version = contractTreeElements > 0 ? await this.storage?.getChairmanMerkleVersion?.(input.chainId, contractTreeElements) : void 0;
7466
+ const hasDb = typeof this.storage?.getMerkleLeaf === "function" && typeof this.storage?.getChairmanMerkleNode === "function" && (contractTreeElements === 0 || !!version);
7467
+ if (hasDb) {
7468
+ const state = this.ensureChainState(input.chainId);
7469
+ if (contractTreeElements > 0 && state.mergedElements < contractTreeElements) {
7393
7470
  if (this.mode === "local") {
7394
7471
  throw new SdkError("MERKLE", "Local merkle db is behind contract", {
7395
7472
  chainId: input.chainId,
7396
7473
  cids,
7397
- localTotalElements: tree.totalElements,
7474
+ localMergedElements: state.mergedElements,
7398
7475
  contractTreeElements
7399
7476
  });
7400
7477
  }
@@ -7406,30 +7483,13 @@ var MerkleEngine = class _MerkleEngine {
7406
7483
  proof.push({ leaf_index: cid, path: new Array(this.treeDepth + 1).fill("0") });
7407
7484
  continue;
7408
7485
  }
7409
- const leaf = await this.storage.getMerkleLeaf(input.chainId, cid);
7410
- if (!leaf) throw new Error(`missing_leaf:${cid}`);
7411
- const path2 = [leaf.commitment];
7412
- for (let level = 1; level <= this.treeDepth; level++) {
7413
- const siblingIndex = cid >> level - 1 ^ 1;
7414
- if (level === 1) {
7415
- const siblingLeaf = await this.storage.getMerkleLeaf(input.chainId, siblingIndex);
7416
- path2.push(siblingLeaf?.commitment ?? getZeroHash(0));
7417
- continue;
7418
- }
7419
- const targetLevel = level - 1;
7420
- const siblingNode = await this.storage.getMerkleNode(input.chainId, `${targetLevel}-${siblingIndex}`);
7421
- path2.push(siblingNode?.hash ?? getZeroHash(targetLevel));
7422
- }
7486
+ const path2 = await this.buildLocalProofPath(input.chainId, cid, version);
7423
7487
  proof.push({ leaf_index: cid, path: path2 });
7424
7488
  }
7425
- let effectiveRoot = tree.root;
7426
- if (tree.totalElements > contractTreeElements && contractTreeElements > 0) {
7427
- const checkpoint = await this.storage.getMerkleNode(input.chainId, `checkpoint-${contractTreeElements}`);
7428
- if (checkpoint) effectiveRoot = checkpoint.hash;
7429
- }
7489
+ const effectiveRoot = contractTreeElements > 0 ? _MerkleEngine.normalizeHex32(version.rootHash, "version.rootHash") : getZeroHash(this.treeDepth);
7430
7490
  return {
7431
7491
  proof,
7432
- merkle_root: _MerkleEngine.normalizeHex32(effectiveRoot, "merkleTree.root"),
7492
+ merkle_root: effectiveRoot,
7433
7493
  latest_cid: totalElements > 0n ? Number(totalElements - 1n) : -1
7434
7494
  };
7435
7495
  } catch (error) {
@@ -7439,7 +7499,7 @@ var MerkleEngine = class _MerkleEngine {
7439
7499
  }
7440
7500
  }
7441
7501
  } else if (this.mode === "local" && needsTreeProof.length) {
7442
- throw new SdkError("MERKLE", "Local merkle db unavailable", { chainId: input.chainId, cids, reason: "missing_adapter_merkle_db" });
7502
+ throw new SdkError("MERKLE", "Local merkle db unavailable", { chainId: input.chainId, cids, reason: "missing_adapter_or_version" });
7443
7503
  }
7444
7504
  }
7445
7505
  if (needsTreeProof.length === 0) {
@@ -7464,15 +7524,64 @@ var MerkleEngine = class _MerkleEngine {
7464
7524
  };
7465
7525
  }
7466
7526
  /**
7467
- * Fetch a remote merkle root (used when no proofs are needed).
7527
+ * Build a local proof path by traversing the chairmanMerkle tree.
7528
+ *
7529
+ * Levels 0-4: sibling hashes from subtree internal nodes (st-{level}-{pos}).
7530
+ * Levels 5-31: sibling hashes from chairmanMerkle tree traversal (top-down from version root).
7468
7531
  */
7532
+ async buildLocalProofPath(chainId, cid, version) {
7533
+ const leaf = await this.storage.getMerkleLeaf(chainId, cid);
7534
+ if (!leaf) throw new Error(`missing_leaf:${cid}`);
7535
+ const path2 = [leaf.commitment];
7536
+ for (let level = 1; level <= SUBTREE_DEPTH; level++) {
7537
+ const siblingPos = cid >> level - 1 ^ 1;
7538
+ if (level === 1) {
7539
+ const siblingLeaf = await this.storage.getMerkleLeaf(chainId, siblingPos);
7540
+ path2.push(siblingLeaf?.commitment ?? getZeroHash(0));
7541
+ } else {
7542
+ const targetLevel = level - 1;
7543
+ const node = await this.storage.getChairmanMerkleNode(chainId, `st-${targetLevel}-${siblingPos}`);
7544
+ path2.push(node?.hash ?? getZeroHash(targetLevel));
7545
+ }
7546
+ }
7547
+ const batchIndex = cid >> SUBTREE_DEPTH;
7548
+ const MAIN_DEPTH = this.treeDepth - SUBTREE_DEPTH;
7549
+ const mainSiblings = [];
7550
+ let nodeId = version.rootId;
7551
+ for (let depth = 0; depth < MAIN_DEPTH; depth++) {
7552
+ const childLevel = this.treeDepth - depth - 1;
7553
+ if (!nodeId) {
7554
+ mainSiblings.push(getZeroHash(childLevel));
7555
+ continue;
7556
+ }
7557
+ const node = await this.storage.getChairmanMerkleNode(chainId, nodeId);
7558
+ if (!node) {
7559
+ mainSiblings.push(getZeroHash(childLevel));
7560
+ nodeId = null;
7561
+ continue;
7562
+ }
7563
+ const remainingDepth = MAIN_DEPTH - depth - 1;
7564
+ const goRight = (batchIndex >> remainingDepth & 1) === 1;
7565
+ if (goRight) {
7566
+ const leftNode = node.leftId ? await this.storage.getChairmanMerkleNode(chainId, node.leftId) : null;
7567
+ mainSiblings.push(leftNode?.hash ?? getZeroHash(childLevel));
7568
+ nodeId = node.rightId;
7569
+ } else {
7570
+ const rightNode = node.rightId ? await this.storage.getChairmanMerkleNode(chainId, node.rightId) : null;
7571
+ mainSiblings.push(rightNode?.hash ?? getZeroHash(childLevel));
7572
+ nodeId = node.leftId;
7573
+ }
7574
+ }
7575
+ for (let i = mainSiblings.length - 1; i >= 0; i--) {
7576
+ path2.push(mainSiblings[i]);
7577
+ }
7578
+ return path2;
7579
+ }
7580
+ // ── Remote helpers ──
7469
7581
  async fetchRemoteRootOnly(chainId) {
7470
7582
  const remote = await this.fetchRemoteProofFromService({ chainId, cids: [0] });
7471
7583
  return _MerkleEngine.normalizeHex32(remote.merkle_root, "remote.merkle_root");
7472
7584
  }
7473
- /**
7474
- * Fetch proofs from the remote merkle service.
7475
- */
7476
7585
  async fetchRemoteProofFromService(input) {
7477
7586
  const chain = this.getChain(input.chainId);
7478
7587
  if (!chain.merkleProofUrl) {
@@ -7481,9 +7590,7 @@ var MerkleEngine = class _MerkleEngine {
7481
7590
  const client = new MerkleClient(chain.merkleProofUrl);
7482
7591
  return client.getProofByCids(input.cids);
7483
7592
  }
7484
- /**
7485
- * Build membership witnesses for provided UTXOs from a remote proof response.
7486
- */
7593
+ // ── Witness builders (unchanged) ──
7487
7594
  buildAccMemberWitnesses(input) {
7488
7595
  return input.utxos.map((utxo, idx) => {
7489
7596
  const remoteProof = input.remote.proof[idx];
@@ -7497,9 +7604,6 @@ var MerkleEngine = class _MerkleEngine {
7497
7604
  };
7498
7605
  });
7499
7606
  }
7500
- /**
7501
- * Convert UTXOs into circuit input secrets, decrypting memos and padding if needed.
7502
- */
7503
7607
  async buildInputSecretsFromUtxos(input) {
7504
7608
  if (!Array.isArray(input.utxos) || input.utxos.length === 0) {
7505
7609
  throw new SdkError("MERKLE", "No utxos provided", { count: 0 });
@@ -8683,8 +8787,9 @@ var FileStore = class {
8683
8787
  this.cursors = /* @__PURE__ */ new Map();
8684
8788
  this.utxos = /* @__PURE__ */ new Map();
8685
8789
  this.operations = [];
8686
- this.merkleTrees = {};
8687
- this.merkleNodes = {};
8790
+ this.chairmanMerkleLatestVersions = {};
8791
+ this.chairmanMerkleVersions = {};
8792
+ this.chairmanMerkleNodes = {};
8688
8793
  this.entryMemos = {};
8689
8794
  this.entryNullifiers = {};
8690
8795
  this.saveChain = Promise.resolve();
@@ -8788,8 +8893,9 @@ var FileStore = class {
8788
8893
  this.utxos.clear();
8789
8894
  this.operations = [];
8790
8895
  this.merkleNextCid.clear();
8791
- this.merkleTrees = {};
8792
- this.merkleNodes = {};
8896
+ this.chairmanMerkleLatestVersions = {};
8897
+ this.chairmanMerkleVersions = {};
8898
+ this.chairmanMerkleNodes = {};
8793
8899
  this.entryMemos = {};
8794
8900
  this.entryNullifiers = {};
8795
8901
  try {
@@ -8805,13 +8911,24 @@ var FileStore = class {
8805
8911
  try {
8806
8912
  const raw = await (0, import_promises.readFile)(this.sharedFilePath(), "utf8");
8807
8913
  const parsed = JSON.parse(raw);
8808
- const merkleTreesRaw = parsed.merkleTrees;
8809
- if (merkleTreesRaw && typeof merkleTreesRaw === "object") {
8810
- this.merkleTrees = merkleTreesRaw;
8914
+ const chairmanMerkleVersionsRaw = parsed.chairmanMerkleVersions;
8915
+ if (chairmanMerkleVersionsRaw && typeof chairmanMerkleVersionsRaw === "object") {
8916
+ this.chairmanMerkleVersions = chairmanMerkleVersionsRaw;
8917
+ for (const [chainKey, versions] of Object.entries(chairmanMerkleVersionsRaw)) {
8918
+ let latest;
8919
+ for (const record of Object.values(versions)) {
8920
+ if (!latest || record.version > latest.version) {
8921
+ latest = record;
8922
+ }
8923
+ }
8924
+ if (latest) {
8925
+ this.chairmanMerkleLatestVersions[chainKey] = latest;
8926
+ }
8927
+ }
8811
8928
  }
8812
- const merkleNodesRaw = parsed.merkleNodes;
8813
- if (merkleNodesRaw && typeof merkleNodesRaw === "object") {
8814
- this.merkleNodes = merkleNodesRaw;
8929
+ const chairmanMerkleNodesRaw = parsed.chairmanMerkleNodes;
8930
+ if (chairmanMerkleNodesRaw && typeof chairmanMerkleNodesRaw === "object") {
8931
+ this.chairmanMerkleNodes = chairmanMerkleNodesRaw;
8815
8932
  }
8816
8933
  const entryMemosRaw = parsed.entryMemos;
8817
8934
  if (entryMemosRaw && typeof entryMemosRaw === "object") {
@@ -8851,8 +8968,8 @@ var FileStore = class {
8851
8968
  this.saveChain = this.saveChain.catch(() => void 0).then(async () => {
8852
8969
  await (0, import_promises.mkdir)(this.options.baseDir, { recursive: true });
8853
8970
  const sharedState = {
8854
- merkleTrees: this.merkleTrees,
8855
- merkleNodes: this.merkleNodes,
8971
+ chairmanMerkleVersions: this.chairmanMerkleVersions,
8972
+ chairmanMerkleNodes: this.chairmanMerkleNodes,
8856
8973
  entryMemos: this.entryMemos,
8857
8974
  entryNullifiers: this.entryNullifiers
8858
8975
  };
@@ -8864,56 +8981,70 @@ var FileStore = class {
8864
8981
  return this.saveChain;
8865
8982
  }
8866
8983
  /**
8867
- * Get persisted merkle tree metadata for a chain.
8984
+ * Get a chairmanMerkle tree node by id.
8868
8985
  */
8869
- async getMerkleTree(chainId) {
8870
- const row = this.merkleTrees[String(chainId)];
8871
- if (!row) return void 0;
8872
- const totalElements = Number(row.totalElements);
8873
- const lastUpdated = Number(row.lastUpdated);
8874
- const root = row.root;
8875
- if (typeof root !== "string" || !root.startsWith("0x")) return void 0;
8876
- if (!Number.isFinite(totalElements) || totalElements < 0) return void 0;
8877
- return { chainId, root, totalElements: Math.floor(totalElements), lastUpdated: Number.isFinite(lastUpdated) ? Math.floor(lastUpdated) : 0 };
8986
+ async getChairmanMerkleNode(chainId, id) {
8987
+ return this.chairmanMerkleNodes[String(chainId)]?.[id];
8878
8988
  }
8879
8989
  /**
8880
- * Persist merkle tree metadata for a chain.
8990
+ * Put chairmanMerkle tree nodes for a chain and persist.
8881
8991
  */
8882
- async setMerkleTree(chainId, tree) {
8883
- this.merkleTrees[String(chainId)] = { ...tree, chainId };
8992
+ async putChairmanMerkleNodes(chainId, nodes) {
8993
+ if (!nodes.length) return;
8994
+ const key = String(chainId);
8995
+ const existing = this.chairmanMerkleNodes[key] ?? {};
8996
+ for (const node of nodes) {
8997
+ existing[node.id] = { ...node, chainId };
8998
+ }
8999
+ this.chairmanMerkleNodes[key] = existing;
8884
9000
  await this.saveShared();
8885
9001
  }
8886
9002
  /**
8887
- * Clear merkle tree metadata for a chain.
9003
+ * Get a specific chairmanMerkle tree version for a chain.
8888
9004
  */
8889
- async clearMerkleTree(chainId) {
8890
- delete this.merkleTrees[String(chainId)];
8891
- await this.saveShared();
9005
+ async getChairmanMerkleVersion(chainId, version) {
9006
+ return this.chairmanMerkleVersions[String(chainId)]?.[version];
8892
9007
  }
8893
9008
  /**
8894
- * Get a merkle node by id.
9009
+ * Get the latest (highest version number) chairmanMerkle tree version for a chain.
8895
9010
  */
8896
- async getMerkleNode(chainId, id) {
8897
- return this.merkleNodes[String(chainId)]?.[id];
9011
+ async getLatestChairmanMerkleVersion(chainId) {
9012
+ const cached = this.chairmanMerkleLatestVersions[String(chainId)];
9013
+ if (cached) return { ...cached };
9014
+ const versions = this.chairmanMerkleVersions[String(chainId)];
9015
+ if (!versions) return void 0;
9016
+ let latest;
9017
+ for (const record of Object.values(versions)) {
9018
+ if (!latest || record.version > latest.version) {
9019
+ latest = record;
9020
+ }
9021
+ }
9022
+ if (latest) {
9023
+ this.chairmanMerkleLatestVersions[String(chainId)] = latest;
9024
+ }
9025
+ return latest ? { ...latest } : void 0;
8898
9026
  }
8899
9027
  /**
8900
- * Upsert merkle nodes for a chain and persist.
9028
+ * Persist a chairmanMerkle tree version for a chain and track latest.
8901
9029
  */
8902
- async upsertMerkleNodes(chainId, nodes) {
8903
- if (!nodes.length) return;
9030
+ async putChairmanMerkleVersion(chainId, record) {
8904
9031
  const key = String(chainId);
8905
- const existing = this.merkleNodes[key] ?? {};
8906
- for (const node of nodes) {
8907
- existing[node.id] = { ...node, chainId };
9032
+ const existing = this.chairmanMerkleVersions[key] ?? {};
9033
+ existing[record.version] = { ...record, chainId };
9034
+ this.chairmanMerkleVersions[key] = existing;
9035
+ const currentLatest = this.chairmanMerkleLatestVersions[key];
9036
+ if (!currentLatest || record.version >= currentLatest.version) {
9037
+ this.chairmanMerkleLatestVersions[key] = { ...record, chainId };
8908
9038
  }
8909
- this.merkleNodes[key] = existing;
8910
9039
  await this.saveShared();
8911
9040
  }
8912
9041
  /**
8913
- * Clear merkle nodes for a chain.
9042
+ * Clear chairmanMerkle tree nodes and versions for a chain.
8914
9043
  */
8915
- async clearMerkleNodes(chainId) {
8916
- delete this.merkleNodes[String(chainId)];
9044
+ async clearChairmanMerkleTree(chainId) {
9045
+ delete this.chairmanMerkleNodes[String(chainId)];
9046
+ delete this.chairmanMerkleVersions[String(chainId)];
9047
+ delete this.chairmanMerkleLatestVersions[String(chainId)];
8917
9048
  await this.saveShared();
8918
9049
  }
8919
9050
  /**
@@ -9193,10 +9324,6 @@ function toChanges(result) {
9193
9324
  if (typeof changes === "bigint") return Number(changes);
9194
9325
  return 0;
9195
9326
  }
9196
- function toHexOrNull(value) {
9197
- if (typeof value !== "string") return null;
9198
- return value.startsWith("0x") ? value : null;
9199
- }
9200
9327
  function sortTextValues(value) {
9201
9328
  if (value == null) return void 0;
9202
9329
  const list = Array.isArray(value) ? value : [value];
@@ -9336,21 +9463,21 @@ var SqliteStore = class {
9336
9463
  PRIMARY KEY (chain_id, cid)
9337
9464
  );
9338
9465
 
9339
- CREATE TABLE IF NOT EXISTS merkle_nodes (
9466
+ CREATE TABLE IF NOT EXISTS chairman_merkle_nodes (
9340
9467
  chain_id INTEGER NOT NULL,
9341
9468
  id TEXT NOT NULL,
9342
- level INTEGER NOT NULL,
9343
- position INTEGER NOT NULL,
9344
9469
  hash TEXT NOT NULL,
9470
+ left_id TEXT,
9471
+ right_id TEXT,
9345
9472
  PRIMARY KEY (chain_id, id)
9346
9473
  );
9347
- CREATE INDEX IF NOT EXISTS idx_merkle_nodes_chain_level_pos ON merkle_nodes(chain_id, level, position);
9348
9474
 
9349
- CREATE TABLE IF NOT EXISTS merkle_trees (
9350
- chain_id INTEGER PRIMARY KEY,
9351
- root TEXT NOT NULL,
9352
- total_elements INTEGER NOT NULL,
9353
- last_updated INTEGER NOT NULL
9475
+ CREATE TABLE IF NOT EXISTS chairman_merkle_versions (
9476
+ chain_id INTEGER NOT NULL,
9477
+ version INTEGER NOT NULL,
9478
+ root_id TEXT NOT NULL,
9479
+ root_hash TEXT NOT NULL,
9480
+ PRIMARY KEY (chain_id, version)
9354
9481
  );
9355
9482
 
9356
9483
  CREATE TABLE IF NOT EXISTS entry_memos (
@@ -9587,29 +9714,29 @@ var SqliteStore = class {
9587
9714
  async clearMerkleLeaves(chainId) {
9588
9715
  this.run(`DELETE FROM merkle_leaves WHERE chain_id = ?`, [chainId]);
9589
9716
  }
9590
- async getMerkleNode(chainId, id) {
9717
+ async getChairmanMerkleNode(chainId, id) {
9591
9718
  const row = this.row(
9592
- `SELECT id, level, position, hash FROM merkle_nodes WHERE chain_id = ? AND id = ?`,
9719
+ `SELECT id, hash, left_id, right_id FROM chairman_merkle_nodes WHERE chain_id = ? AND id = ?`,
9593
9720
  [chainId, id]
9594
9721
  );
9595
9722
  if (!row) return void 0;
9596
- return { chainId, id: row.id, level: row.level, position: row.position, hash: row.hash };
9723
+ return { chainId, id: row.id, hash: row.hash, leftId: row.left_id, rightId: row.right_id };
9597
9724
  }
9598
- async upsertMerkleNodes(chainId, nodes) {
9725
+ async putChairmanMerkleNodes(chainId, nodes) {
9599
9726
  if (!nodes.length) return;
9600
9727
  const db = this.ensureDb();
9601
9728
  const stmt = db.prepare(
9602
- `INSERT INTO merkle_nodes (chain_id, id, level, position, hash)
9729
+ `INSERT INTO chairman_merkle_nodes (chain_id, id, hash, left_id, right_id)
9603
9730
  VALUES (?, ?, ?, ?, ?)
9604
9731
  ON CONFLICT(chain_id, id) DO UPDATE SET
9605
- level = excluded.level,
9606
- position = excluded.position,
9607
- hash = excluded.hash`
9732
+ hash = excluded.hash,
9733
+ left_id = excluded.left_id,
9734
+ right_id = excluded.right_id`
9608
9735
  );
9609
9736
  db.exec("BEGIN IMMEDIATE");
9610
9737
  try {
9611
9738
  for (const node of nodes) {
9612
- stmt.run(chainId, node.id, node.level, node.position, node.hash);
9739
+ stmt.run(chainId, node.id, node.hash, node.leftId ?? null, node.rightId ?? null);
9613
9740
  }
9614
9741
  db.exec("COMMIT");
9615
9742
  } catch (error) {
@@ -9617,40 +9744,35 @@ var SqliteStore = class {
9617
9744
  throw error;
9618
9745
  }
9619
9746
  }
9620
- async clearMerkleNodes(chainId) {
9621
- this.run(`DELETE FROM merkle_nodes WHERE chain_id = ?`, [chainId]);
9747
+ async getChairmanMerkleVersion(chainId, version) {
9748
+ const row = this.row(
9749
+ `SELECT version, root_id, root_hash FROM chairman_merkle_versions WHERE chain_id = ? AND version = ?`,
9750
+ [chainId, version]
9751
+ );
9752
+ if (!row) return void 0;
9753
+ return { chainId, version: row.version, rootId: row.root_id, rootHash: row.root_hash };
9622
9754
  }
9623
- async getMerkleTree(chainId) {
9755
+ async getLatestChairmanMerkleVersion(chainId) {
9624
9756
  const row = this.row(
9625
- `SELECT root, total_elements, last_updated FROM merkle_trees WHERE chain_id = ?`,
9757
+ `SELECT version, root_id, root_hash FROM chairman_merkle_versions WHERE chain_id = ? ORDER BY version DESC LIMIT 1`,
9626
9758
  [chainId]
9627
9759
  );
9628
9760
  if (!row) return void 0;
9629
- const root = toHexOrNull(row.root);
9630
- if (!root) return void 0;
9631
- const totalElements = Number(row.total_elements);
9632
- if (!Number.isFinite(totalElements) || totalElements < 0) return void 0;
9633
- const lastUpdated = Number(row.last_updated);
9634
- return {
9635
- chainId,
9636
- root,
9637
- totalElements: Math.floor(totalElements),
9638
- lastUpdated: Number.isFinite(lastUpdated) ? Math.floor(lastUpdated) : 0
9639
- };
9761
+ return { chainId, version: row.version, rootId: row.root_id, rootHash: row.root_hash };
9640
9762
  }
9641
- async setMerkleTree(chainId, tree) {
9763
+ async putChairmanMerkleVersion(chainId, record) {
9642
9764
  this.run(
9643
- `INSERT INTO merkle_trees (chain_id, root, total_elements, last_updated)
9765
+ `INSERT INTO chairman_merkle_versions (chain_id, version, root_id, root_hash)
9644
9766
  VALUES (?, ?, ?, ?)
9645
- ON CONFLICT(chain_id) DO UPDATE SET
9646
- root = excluded.root,
9647
- total_elements = excluded.total_elements,
9648
- last_updated = excluded.last_updated`,
9649
- [chainId, tree.root, tree.totalElements, tree.lastUpdated]
9767
+ ON CONFLICT(chain_id, version) DO UPDATE SET
9768
+ root_id = excluded.root_id,
9769
+ root_hash = excluded.root_hash`,
9770
+ [chainId, record.version, record.rootId, record.rootHash]
9650
9771
  );
9651
9772
  }
9652
- async clearMerkleTree(chainId) {
9653
- this.run(`DELETE FROM merkle_trees WHERE chain_id = ?`, [chainId]);
9773
+ async clearChairmanMerkleTree(chainId) {
9774
+ this.run(`DELETE FROM chairman_merkle_nodes WHERE chain_id = ?`, [chainId]);
9775
+ this.run(`DELETE FROM chairman_merkle_versions WHERE chain_id = ?`, [chainId]);
9654
9776
  }
9655
9777
  async upsertEntryMemos(memos) {
9656
9778
  if (!memos.length) return;