@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/browser.cjs +413 -286
- package/dist/browser.cjs.map +1 -1
- package/dist/browser.d.cts +18 -16
- package/dist/browser.d.ts +18 -16
- package/dist/browser.js +413 -286
- package/dist/browser.js.map +1 -1
- package/dist/{index-CI7UllxU.d.cts → index-Dvl0HZkw.d.cts} +66 -64
- package/dist/{index-CI7UllxU.d.ts → index-Dvl0HZkw.d.ts} +66 -64
- package/dist/index.cjs +353 -249
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +353 -249
- package/dist/index.js.map +1 -1
- package/dist/node.cjs +459 -337
- package/dist/node.cjs.map +1 -1
- package/dist/node.d.cts +24 -23
- package/dist/node.d.ts +24 -23
- package/dist/node.js +459 -337
- package/dist/node.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -3222,8 +3222,8 @@ var MemoryStore = class {
|
|
|
3222
3222
|
this.utxos = /* @__PURE__ */ new Map();
|
|
3223
3223
|
this.operations = [];
|
|
3224
3224
|
this.merkleLeavesByChain = /* @__PURE__ */ new Map();
|
|
3225
|
-
this.
|
|
3226
|
-
this.
|
|
3225
|
+
this.chairmanMerkleVersionsByChain = /* @__PURE__ */ new Map();
|
|
3226
|
+
this.chairmanMerkleNodesByChain = /* @__PURE__ */ new Map();
|
|
3227
3227
|
this.entryMemosByChain = /* @__PURE__ */ new Map();
|
|
3228
3228
|
this.entryNullifiersByChain = /* @__PURE__ */ new Map();
|
|
3229
3229
|
const max = options?.maxOperations;
|
|
@@ -3239,8 +3239,8 @@ var MemoryStore = class {
|
|
|
3239
3239
|
this.utxos.clear();
|
|
3240
3240
|
this.operations = [];
|
|
3241
3241
|
this.merkleLeavesByChain.clear();
|
|
3242
|
-
this.
|
|
3243
|
-
this.
|
|
3242
|
+
this.chairmanMerkleVersionsByChain.clear();
|
|
3243
|
+
this.chairmanMerkleNodesByChain.clear();
|
|
3244
3244
|
this.entryMemosByChain.clear();
|
|
3245
3245
|
this.entryNullifiersByChain.clear();
|
|
3246
3246
|
}
|
|
@@ -3356,49 +3356,64 @@ var MemoryStore = class {
|
|
|
3356
3356
|
return { chainId, cid: row.cid, commitment: row.commitment };
|
|
3357
3357
|
}
|
|
3358
3358
|
/**
|
|
3359
|
-
* Get a
|
|
3359
|
+
* Get a chairmanMerkle tree node by id.
|
|
3360
3360
|
*/
|
|
3361
|
-
async
|
|
3362
|
-
return this.
|
|
3361
|
+
async getChairmanMerkleNode(chainId, id) {
|
|
3362
|
+
return this.chairmanMerkleNodesByChain.get(chainId)?.get(id);
|
|
3363
3363
|
}
|
|
3364
3364
|
/**
|
|
3365
|
-
*
|
|
3365
|
+
* Put chairmanMerkle tree nodes for a chain.
|
|
3366
3366
|
*/
|
|
3367
|
-
async
|
|
3367
|
+
async putChairmanMerkleNodes(chainId, nodes) {
|
|
3368
3368
|
if (!nodes.length) return;
|
|
3369
|
-
let map = this.
|
|
3369
|
+
let map = this.chairmanMerkleNodesByChain.get(chainId);
|
|
3370
3370
|
if (!map) {
|
|
3371
3371
|
map = /* @__PURE__ */ new Map();
|
|
3372
|
-
this.
|
|
3372
|
+
this.chairmanMerkleNodesByChain.set(chainId, map);
|
|
3373
3373
|
}
|
|
3374
3374
|
for (const node of nodes) {
|
|
3375
3375
|
map.set(node.id, { ...node, chainId });
|
|
3376
3376
|
}
|
|
3377
3377
|
}
|
|
3378
3378
|
/**
|
|
3379
|
-
*
|
|
3379
|
+
* Get a chairmanMerkle version record by chain and version number.
|
|
3380
3380
|
*/
|
|
3381
|
-
async
|
|
3382
|
-
this.
|
|
3381
|
+
async getChairmanMerkleVersion(chainId, version) {
|
|
3382
|
+
const byVersion = this.chairmanMerkleVersionsByChain.get(chainId);
|
|
3383
|
+
const record = byVersion?.get(version);
|
|
3384
|
+
return record ? { ...record } : void 0;
|
|
3383
3385
|
}
|
|
3384
3386
|
/**
|
|
3385
|
-
* Get
|
|
3387
|
+
* Get the latest chairmanMerkle version record (highest version number) for a chain.
|
|
3386
3388
|
*/
|
|
3387
|
-
async
|
|
3388
|
-
const
|
|
3389
|
-
|
|
3389
|
+
async getLatestChairmanMerkleVersion(chainId) {
|
|
3390
|
+
const byVersion = this.chairmanMerkleVersionsByChain.get(chainId);
|
|
3391
|
+
if (!byVersion || byVersion.size === 0) return void 0;
|
|
3392
|
+
let latest;
|
|
3393
|
+
for (const record of byVersion.values()) {
|
|
3394
|
+
if (!latest || record.version > latest.version) {
|
|
3395
|
+
latest = record;
|
|
3396
|
+
}
|
|
3397
|
+
}
|
|
3398
|
+
return latest ? { ...latest } : void 0;
|
|
3390
3399
|
}
|
|
3391
3400
|
/**
|
|
3392
|
-
* Persist
|
|
3401
|
+
* Persist a chairmanMerkle version record.
|
|
3393
3402
|
*/
|
|
3394
|
-
async
|
|
3395
|
-
this.
|
|
3403
|
+
async putChairmanMerkleVersion(chainId, record) {
|
|
3404
|
+
let byVersion = this.chairmanMerkleVersionsByChain.get(chainId);
|
|
3405
|
+
if (!byVersion) {
|
|
3406
|
+
byVersion = /* @__PURE__ */ new Map();
|
|
3407
|
+
this.chairmanMerkleVersionsByChain.set(chainId, byVersion);
|
|
3408
|
+
}
|
|
3409
|
+
byVersion.set(record.version, { ...record, chainId });
|
|
3396
3410
|
}
|
|
3397
3411
|
/**
|
|
3398
|
-
* Clear
|
|
3412
|
+
* Clear all chairmanMerkle tree state (both nodes and versions) for a chain.
|
|
3399
3413
|
*/
|
|
3400
|
-
async
|
|
3401
|
-
this.
|
|
3414
|
+
async clearChairmanMerkleTree(chainId) {
|
|
3415
|
+
this.chairmanMerkleNodesByChain.delete(chainId);
|
|
3416
|
+
this.chairmanMerkleVersionsByChain.delete(chainId);
|
|
3402
3417
|
}
|
|
3403
3418
|
/**
|
|
3404
3419
|
* Upsert entry memos (raw EntryService cache).
|
|
@@ -3922,13 +3937,14 @@ var KeyValueStore = class {
|
|
|
3922
3937
|
this.utxoCache = /* @__PURE__ */ new Map();
|
|
3923
3938
|
this.operationCache = /* @__PURE__ */ new Map();
|
|
3924
3939
|
this.merkleLeafCids = {};
|
|
3925
|
-
this.
|
|
3926
|
-
this.
|
|
3940
|
+
this.chairmanMerkleLatestVersions = {};
|
|
3941
|
+
this.chairmanMerkleNodeIds = {};
|
|
3942
|
+
this.chairmanMerkleVersionNums = {};
|
|
3927
3943
|
this.entryMemoCids = {};
|
|
3928
3944
|
this.entryNullifierNids = {};
|
|
3929
3945
|
this.loadedMerkleLeaves = /* @__PURE__ */ new Set();
|
|
3930
|
-
this.
|
|
3931
|
-
this.
|
|
3946
|
+
this.loadedChairmanMerkleVersions = /* @__PURE__ */ new Set();
|
|
3947
|
+
this.loadedChairmanMerkleNodes = /* @__PURE__ */ new Set();
|
|
3932
3948
|
this.loadedEntryMemos = /* @__PURE__ */ new Set();
|
|
3933
3949
|
this.loadedEntryNullifiers = /* @__PURE__ */ new Set();
|
|
3934
3950
|
this.saveChain = Promise.resolve();
|
|
@@ -3962,9 +3978,6 @@ var KeyValueStore = class {
|
|
|
3962
3978
|
walletOperationKey(id) {
|
|
3963
3979
|
return `${this.walletBaseKey()}:operation:${id}`;
|
|
3964
3980
|
}
|
|
3965
|
-
sharedChainKey(part, chainId) {
|
|
3966
|
-
return `${this.keyPrefix()}:shared:${part}:${chainId}`;
|
|
3967
|
-
}
|
|
3968
3981
|
sharedChainMetaKey(part, chainId) {
|
|
3969
3982
|
return `${this.keyPrefix()}:shared:${part}:${chainId}:meta`;
|
|
3970
3983
|
}
|
|
@@ -4017,13 +4030,14 @@ var KeyValueStore = class {
|
|
|
4017
4030
|
this.operationCache.clear();
|
|
4018
4031
|
this.walletMetaLoaded = false;
|
|
4019
4032
|
this.merkleLeafCids = {};
|
|
4020
|
-
this.
|
|
4021
|
-
this.
|
|
4033
|
+
this.chairmanMerkleLatestVersions = {};
|
|
4034
|
+
this.chairmanMerkleNodeIds = {};
|
|
4035
|
+
this.chairmanMerkleVersionNums = {};
|
|
4022
4036
|
this.entryMemoCids = {};
|
|
4023
4037
|
this.entryNullifierNids = {};
|
|
4024
4038
|
this.loadedMerkleLeaves.clear();
|
|
4025
|
-
this.
|
|
4026
|
-
this.
|
|
4039
|
+
this.loadedChairmanMerkleVersions.clear();
|
|
4040
|
+
this.loadedChairmanMerkleNodes.clear();
|
|
4027
4041
|
this.loadedEntryMemos.clear();
|
|
4028
4042
|
this.loadedEntryNullifiers.clear();
|
|
4029
4043
|
}
|
|
@@ -4118,20 +4132,19 @@ var KeyValueStore = class {
|
|
|
4118
4132
|
this.merkleLeafCids[key] = new Set(this.parseNumberIndex(cidsRaw));
|
|
4119
4133
|
this.loadedMerkleLeaves.add(chainId);
|
|
4120
4134
|
}
|
|
4121
|
-
async
|
|
4122
|
-
if (this.
|
|
4135
|
+
async ensureChairmanMerkleVersionsLoaded(chainId) {
|
|
4136
|
+
if (this.loadedChairmanMerkleVersions.has(chainId)) return;
|
|
4123
4137
|
const key = String(chainId);
|
|
4124
|
-
const
|
|
4125
|
-
|
|
4126
|
-
|
|
4127
|
-
this.loadedMerkleTrees.add(chainId);
|
|
4138
|
+
const numsRaw = await this.options.client.get(this.sharedChainMetaKey("chairmanMerkleVersions", chainId));
|
|
4139
|
+
this.chairmanMerkleVersionNums[key] = new Set(this.parseNumberIndex(numsRaw));
|
|
4140
|
+
this.loadedChairmanMerkleVersions.add(chainId);
|
|
4128
4141
|
}
|
|
4129
|
-
async
|
|
4130
|
-
if (this.
|
|
4142
|
+
async ensureChairmanMerkleNodesLoaded(chainId) {
|
|
4143
|
+
if (this.loadedChairmanMerkleNodes.has(chainId)) return;
|
|
4131
4144
|
const key = String(chainId);
|
|
4132
|
-
const idsRaw = await this.options.client.get(this.sharedChainMetaKey("
|
|
4133
|
-
this.
|
|
4134
|
-
this.
|
|
4145
|
+
const idsRaw = await this.options.client.get(this.sharedChainMetaKey("chairmanMerkleNodes", chainId));
|
|
4146
|
+
this.chairmanMerkleNodeIds[key] = new Set(this.parseStringIndex(idsRaw));
|
|
4147
|
+
this.loadedChairmanMerkleNodes.add(chainId);
|
|
4135
4148
|
}
|
|
4136
4149
|
async ensureEntryMemosLoaded(chainId) {
|
|
4137
4150
|
if (this.loadedEntryMemos.has(chainId)) return;
|
|
@@ -4147,64 +4160,88 @@ var KeyValueStore = class {
|
|
|
4147
4160
|
this.entryNullifierNids[key] = new Set(this.parseNumberIndex(nidsRaw));
|
|
4148
4161
|
this.loadedEntryNullifiers.add(chainId);
|
|
4149
4162
|
}
|
|
4150
|
-
async
|
|
4151
|
-
await this.
|
|
4152
|
-
if (!this.
|
|
4153
|
-
const raw = await this.options.client.get(this.sharedRecordKey("
|
|
4163
|
+
async getChairmanMerkleNode(chainId, id) {
|
|
4164
|
+
await this.ensureChairmanMerkleNodesLoaded(chainId);
|
|
4165
|
+
if (!this.chairmanMerkleNodeIds[String(chainId)]?.has(id)) return void 0;
|
|
4166
|
+
const raw = await this.options.client.get(this.sharedRecordKey("chairmanMerkleNodes", chainId, id));
|
|
4154
4167
|
const node = this.parseJson(raw, null);
|
|
4155
4168
|
if (!node) return void 0;
|
|
4156
4169
|
const hash = node.hash;
|
|
4157
4170
|
if (typeof hash !== "string" || !hash.startsWith("0x")) return void 0;
|
|
4158
4171
|
return { ...node, chainId };
|
|
4159
4172
|
}
|
|
4160
|
-
async
|
|
4173
|
+
async putChairmanMerkleNodes(chainId, nodes) {
|
|
4161
4174
|
if (!nodes.length) return;
|
|
4162
|
-
await this.
|
|
4175
|
+
await this.ensureChairmanMerkleNodesLoaded(chainId);
|
|
4163
4176
|
const key = String(chainId);
|
|
4164
|
-
const ids = this.
|
|
4177
|
+
const ids = this.chairmanMerkleNodeIds[key] ?? /* @__PURE__ */ new Set();
|
|
4165
4178
|
const beforeSize = ids.size;
|
|
4166
4179
|
for (const node of nodes) {
|
|
4167
4180
|
ids.add(node.id);
|
|
4168
4181
|
}
|
|
4169
|
-
this.
|
|
4182
|
+
this.chairmanMerkleNodeIds[key] = ids;
|
|
4170
4183
|
await this.enqueueWrite(async () => {
|
|
4171
|
-
await Promise.all(nodes.map((node) => this.writeJson(this.sharedRecordKey("
|
|
4184
|
+
await Promise.all(nodes.map((node) => this.writeJson(this.sharedRecordKey("chairmanMerkleNodes", chainId, node.id), { ...node, chainId })));
|
|
4172
4185
|
if (ids.size !== beforeSize) {
|
|
4173
|
-
await this.writeJson(this.sharedChainMetaKey("
|
|
4186
|
+
await this.writeJson(this.sharedChainMetaKey("chairmanMerkleNodes", chainId), Array.from(ids));
|
|
4174
4187
|
}
|
|
4175
4188
|
});
|
|
4176
4189
|
}
|
|
4177
|
-
async
|
|
4178
|
-
await this.
|
|
4179
|
-
|
|
4180
|
-
|
|
4190
|
+
async getChairmanMerkleVersion(chainId, version) {
|
|
4191
|
+
await this.ensureChairmanMerkleVersionsLoaded(chainId);
|
|
4192
|
+
if (!this.chairmanMerkleVersionNums[String(chainId)]?.has(version)) return void 0;
|
|
4193
|
+
const raw = await this.options.client.get(this.sharedRecordKey("chairmanMerkleVersions", chainId, version));
|
|
4194
|
+
const record = this.parseJson(raw, null);
|
|
4195
|
+
if (!record) return void 0;
|
|
4196
|
+
if (typeof record.rootHash !== "string" || !record.rootHash.startsWith("0x")) return void 0;
|
|
4197
|
+
if (typeof record.rootId !== "string") return void 0;
|
|
4198
|
+
const v = Number(record.version);
|
|
4199
|
+
if (!Number.isFinite(v) || v < 0) return void 0;
|
|
4200
|
+
return { chainId, version: Math.floor(v), rootId: record.rootId, rootHash: record.rootHash };
|
|
4201
|
+
}
|
|
4202
|
+
async getLatestChairmanMerkleVersion(chainId) {
|
|
4203
|
+
await this.ensureChairmanMerkleVersionsLoaded(chainId);
|
|
4204
|
+
const nums = this.chairmanMerkleVersionNums[String(chainId)];
|
|
4205
|
+
if (!nums || nums.size === 0) return void 0;
|
|
4206
|
+
const maxVersion = Math.max(...nums);
|
|
4207
|
+
return this.getChairmanMerkleVersion(chainId, maxVersion);
|
|
4208
|
+
}
|
|
4209
|
+
async putChairmanMerkleVersion(chainId, record) {
|
|
4210
|
+
await this.ensureChairmanMerkleVersionsLoaded(chainId);
|
|
4211
|
+
const key = String(chainId);
|
|
4212
|
+
const nums = this.chairmanMerkleVersionNums[key] ?? /* @__PURE__ */ new Set();
|
|
4213
|
+
const beforeSize = nums.size;
|
|
4214
|
+
nums.add(record.version);
|
|
4215
|
+
this.chairmanMerkleVersionNums[key] = nums;
|
|
4216
|
+
const current = this.chairmanMerkleLatestVersions[key];
|
|
4217
|
+
if (!current || record.version >= current.version) {
|
|
4218
|
+
this.chairmanMerkleLatestVersions[key] = { ...record, chainId };
|
|
4219
|
+
}
|
|
4220
|
+
const row = { ...record, chainId };
|
|
4181
4221
|
await this.enqueueWrite(async () => {
|
|
4182
|
-
await
|
|
4183
|
-
|
|
4222
|
+
await this.writeJson(this.sharedRecordKey("chairmanMerkleVersions", chainId, record.version), row);
|
|
4223
|
+
if (nums.size !== beforeSize) {
|
|
4224
|
+
await this.writeJson(this.sharedChainMetaKey("chairmanMerkleVersions", chainId), Array.from(nums).sort((a, b) => a - b));
|
|
4225
|
+
}
|
|
4184
4226
|
});
|
|
4185
4227
|
}
|
|
4186
|
-
async
|
|
4187
|
-
await this.
|
|
4188
|
-
|
|
4189
|
-
|
|
4190
|
-
const
|
|
4191
|
-
|
|
4192
|
-
|
|
4193
|
-
|
|
4194
|
-
if (!Number.isFinite(totalElements) || totalElements < 0) return void 0;
|
|
4195
|
-
return { chainId, root, totalElements: Math.floor(totalElements), lastUpdated: Number.isFinite(lastUpdated) ? Math.floor(lastUpdated) : 0 };
|
|
4196
|
-
}
|
|
4197
|
-
async setMerkleTree(chainId, tree) {
|
|
4198
|
-
await this.ensureMerkleTreeLoaded(chainId);
|
|
4199
|
-
const row = { ...tree, chainId };
|
|
4200
|
-
this.merkleTrees[String(chainId)] = row;
|
|
4201
|
-
await this.enqueueWrite(() => this.writeJson(this.sharedChainKey("merkleTrees", chainId), row));
|
|
4202
|
-
}
|
|
4203
|
-
async clearMerkleTree(chainId) {
|
|
4204
|
-
await this.ensureMerkleTreeLoaded(chainId);
|
|
4205
|
-
delete this.merkleTrees[String(chainId)];
|
|
4228
|
+
async clearChairmanMerkleTree(chainId) {
|
|
4229
|
+
await this.ensureChairmanMerkleNodesLoaded(chainId);
|
|
4230
|
+
await this.ensureChairmanMerkleVersionsLoaded(chainId);
|
|
4231
|
+
const nodeIds = Array.from(this.chairmanMerkleNodeIds[String(chainId)] ?? []);
|
|
4232
|
+
const versionNums = Array.from(this.chairmanMerkleVersionNums[String(chainId)] ?? []);
|
|
4233
|
+
delete this.chairmanMerkleNodeIds[String(chainId)];
|
|
4234
|
+
delete this.chairmanMerkleVersionNums[String(chainId)];
|
|
4235
|
+
delete this.chairmanMerkleLatestVersions[String(chainId)];
|
|
4206
4236
|
await this.enqueueWrite(async () => {
|
|
4207
|
-
await
|
|
4237
|
+
await Promise.all([
|
|
4238
|
+
...nodeIds.map((id) => this.deleteOrReset(this.sharedRecordKey("chairmanMerkleNodes", chainId, id), null)),
|
|
4239
|
+
...versionNums.map((v) => this.deleteOrReset(this.sharedRecordKey("chairmanMerkleVersions", chainId, v), null))
|
|
4240
|
+
]);
|
|
4241
|
+
await Promise.all([
|
|
4242
|
+
this.deleteOrReset(this.sharedChainMetaKey("chairmanMerkleNodes", chainId), []),
|
|
4243
|
+
this.deleteOrReset(this.sharedChainMetaKey("chairmanMerkleVersions", chainId), [])
|
|
4244
|
+
]);
|
|
4208
4245
|
});
|
|
4209
4246
|
}
|
|
4210
4247
|
async upsertEntryMemos(memos) {
|
|
@@ -7048,6 +7085,7 @@ var MerkleEngine = class _MerkleEngine {
|
|
|
7048
7085
|
this.hydrateInFlight = /* @__PURE__ */ new Map();
|
|
7049
7086
|
this.mode = options?.mode ?? "hybrid";
|
|
7050
7087
|
this.treeDepth = Math.max(1, Math.floor(options?.treeDepth ?? TREE_DEPTH_DEFAULT));
|
|
7088
|
+
this.readContractRoot = options?.readContractRoot;
|
|
7051
7089
|
}
|
|
7052
7090
|
/**
|
|
7053
7091
|
* Compute the current merkle root index from total elements.
|
|
@@ -7056,9 +7094,6 @@ var MerkleEngine = class _MerkleEngine {
|
|
|
7056
7094
|
if (totalElements <= tempArraySize) return 0;
|
|
7057
7095
|
return Math.floor((totalElements - 1) / tempArraySize);
|
|
7058
7096
|
}
|
|
7059
|
-
/**
|
|
7060
|
-
* Get or initialize the pending leaf buffer for a chain.
|
|
7061
|
-
*/
|
|
7062
7097
|
ensurePendingLeaves(chainId) {
|
|
7063
7098
|
let pending = this.pendingLeavesByChain.get(chainId);
|
|
7064
7099
|
if (!pending) {
|
|
@@ -7067,9 +7102,6 @@ var MerkleEngine = class _MerkleEngine {
|
|
|
7067
7102
|
}
|
|
7068
7103
|
return pending;
|
|
7069
7104
|
}
|
|
7070
|
-
/**
|
|
7071
|
-
* Get or initialize chain-level merkle state.
|
|
7072
|
-
*/
|
|
7073
7105
|
ensureChainState(chainId) {
|
|
7074
7106
|
let state = this.chainStateByChain.get(chainId);
|
|
7075
7107
|
if (!state) {
|
|
@@ -7078,15 +7110,32 @@ var MerkleEngine = class _MerkleEngine {
|
|
|
7078
7110
|
}
|
|
7079
7111
|
return state;
|
|
7080
7112
|
}
|
|
7081
|
-
|
|
7082
|
-
* Poseidon2 merkle hash for a left/right pair.
|
|
7083
|
-
*/
|
|
7113
|
+
// ── Hashing ──
|
|
7084
7114
|
static hashPair(left, right) {
|
|
7085
7115
|
return Poseidon2.hashToHex(BigInt(left), BigInt(right), Poseidon2Domain.Merkle);
|
|
7086
7116
|
}
|
|
7117
|
+
static normalizeHex32(value, name) {
|
|
7118
|
+
try {
|
|
7119
|
+
const bi = BigInt(value);
|
|
7120
|
+
if (bi < 0n) throw new Error("negative");
|
|
7121
|
+
const hex = bi.toString(16).padStart(64, "0");
|
|
7122
|
+
if (hex.length > 64) throw new Error("too_large");
|
|
7123
|
+
return `0x${hex}`;
|
|
7124
|
+
} catch (error) {
|
|
7125
|
+
throw new SdkError("MERKLE", `Invalid ${name}`, { value }, error);
|
|
7126
|
+
}
|
|
7127
|
+
}
|
|
7128
|
+
// ── Static helpers ──
|
|
7129
|
+
static totalElementsInTree(totalElements, tempArraySize = TEMP_ARRAY_SIZE_DEFAULT) {
|
|
7130
|
+
if (tempArraySize <= 0) throw new SdkError("MERKLE", "tempArraySize must be greater than zero", { tempArraySize });
|
|
7131
|
+
if (totalElements <= 0n) return 0;
|
|
7132
|
+
const size = BigInt(tempArraySize);
|
|
7133
|
+
return Number((totalElements - 1n) / size * size);
|
|
7134
|
+
}
|
|
7135
|
+
// ── Subtree (levels 0-5, 32 leaves → 1 root) ──
|
|
7087
7136
|
/**
|
|
7088
7137
|
* Build a fixed-depth subtree from 32 contiguous leaves.
|
|
7089
|
-
* Returns the subtree root and all intermediate nodes for storage.
|
|
7138
|
+
* Returns the subtree root hash and all intermediate nodes for storage.
|
|
7090
7139
|
*/
|
|
7091
7140
|
static buildSubtree(leafCommitments, baseIndex) {
|
|
7092
7141
|
if (leafCommitments.length !== SUBTREE_SIZE) {
|
|
@@ -7107,71 +7156,71 @@ var MerkleEngine = class _MerkleEngine {
|
|
|
7107
7156
|
const position = basePos + i;
|
|
7108
7157
|
nodesToStore.push({
|
|
7109
7158
|
chainId: 0,
|
|
7110
|
-
id:
|
|
7111
|
-
|
|
7112
|
-
|
|
7113
|
-
|
|
7159
|
+
id: `st-${level}-${position}`,
|
|
7160
|
+
hash: next[i],
|
|
7161
|
+
leftId: null,
|
|
7162
|
+
rightId: null
|
|
7114
7163
|
});
|
|
7115
7164
|
}
|
|
7116
7165
|
currentLevel = next;
|
|
7117
7166
|
}
|
|
7118
7167
|
return { subtreeRoot: currentLevel[0], nodesToStore };
|
|
7119
7168
|
}
|
|
7169
|
+
// ── ChairmanMerkle tree (persistent segment tree, levels 5-32) ──
|
|
7120
7170
|
/**
|
|
7121
|
-
*
|
|
7122
|
-
|
|
7123
|
-
|
|
7124
|
-
|
|
7125
|
-
|
|
7126
|
-
|
|
7127
|
-
|
|
7128
|
-
*
|
|
7129
|
-
*/
|
|
7130
|
-
async
|
|
7131
|
-
|
|
7132
|
-
|
|
7133
|
-
const
|
|
7134
|
-
|
|
7135
|
-
|
|
7136
|
-
|
|
7137
|
-
|
|
7138
|
-
|
|
7139
|
-
|
|
7140
|
-
|
|
7141
|
-
|
|
7142
|
-
|
|
7143
|
-
|
|
7144
|
-
|
|
7145
|
-
|
|
7171
|
+
* Insert a subtree root into the persistent main tree.
|
|
7172
|
+
*
|
|
7173
|
+
* Top-down recursive: descends from root (level treeDepth) to the target
|
|
7174
|
+
* leaf position (level SUBTREE_DEPTH). At each level only the node on the
|
|
7175
|
+
* update path is newly created; the sibling is shared from the previous
|
|
7176
|
+
* version's tree.
|
|
7177
|
+
*
|
|
7178
|
+
* @returns new root node ID/hash and all newly created nodes.
|
|
7179
|
+
*/
|
|
7180
|
+
async insertSubtreeRoot(chainId, prevRootId, subtreeRootHash, batchIndex, version) {
|
|
7181
|
+
const MAIN_DEPTH = this.treeDepth - SUBTREE_DEPTH;
|
|
7182
|
+
const nodes = [];
|
|
7183
|
+
const descend = async (nodeId, depth) => {
|
|
7184
|
+
const originalLevel = this.treeDepth - depth;
|
|
7185
|
+
if (depth === MAIN_DEPTH) {
|
|
7186
|
+
const newId2 = `cm-${version}-${originalLevel}`;
|
|
7187
|
+
nodes.push({ chainId, id: newId2, hash: subtreeRootHash, leftId: null, rightId: null });
|
|
7188
|
+
return { id: newId2, hash: subtreeRootHash };
|
|
7189
|
+
}
|
|
7190
|
+
let prevLeftId = null;
|
|
7191
|
+
let prevRightId = null;
|
|
7192
|
+
if (nodeId) {
|
|
7193
|
+
const prevNode = await this.storage?.getChairmanMerkleNode?.(chainId, nodeId);
|
|
7194
|
+
if (prevNode) {
|
|
7195
|
+
prevLeftId = prevNode.leftId;
|
|
7196
|
+
prevRightId = prevNode.rightId;
|
|
7146
7197
|
}
|
|
7147
|
-
currentValue = _MerkleEngine.hashPair(currentValue, getZeroHash(level));
|
|
7148
|
-
} else {
|
|
7149
|
-
const leftHash = await this.getNodeHash(input.chainId, `frontier-${level}`) ?? getZeroHash(level);
|
|
7150
|
-
currentValue = _MerkleEngine.hashPair(leftHash, currentValue);
|
|
7151
7198
|
}
|
|
7152
|
-
const
|
|
7153
|
-
|
|
7154
|
-
|
|
7155
|
-
|
|
7156
|
-
|
|
7157
|
-
|
|
7158
|
-
hash:
|
|
7159
|
-
|
|
7160
|
-
|
|
7161
|
-
|
|
7162
|
-
|
|
7163
|
-
|
|
7164
|
-
|
|
7165
|
-
|
|
7166
|
-
|
|
7167
|
-
|
|
7168
|
-
|
|
7169
|
-
|
|
7170
|
-
|
|
7199
|
+
const childLevel = originalLevel - 1;
|
|
7200
|
+
const remainingDepth = MAIN_DEPTH - depth - 1;
|
|
7201
|
+
const goRight = (batchIndex >> remainingDepth & 1) === 1;
|
|
7202
|
+
let leftResult;
|
|
7203
|
+
let rightResult;
|
|
7204
|
+
if (goRight) {
|
|
7205
|
+
const leftHash = prevLeftId ? (await this.storage?.getChairmanMerkleNode?.(chainId, prevLeftId))?.hash ?? getZeroHash(childLevel) : getZeroHash(childLevel);
|
|
7206
|
+
leftResult = { id: prevLeftId, hash: leftHash };
|
|
7207
|
+
const right = await descend(prevRightId, depth + 1);
|
|
7208
|
+
rightResult = { id: right.id, hash: right.hash };
|
|
7209
|
+
} else {
|
|
7210
|
+
const left = await descend(prevLeftId, depth + 1);
|
|
7211
|
+
leftResult = { id: left.id, hash: left.hash };
|
|
7212
|
+
const rightHash = prevRightId ? (await this.storage?.getChairmanMerkleNode?.(chainId, prevRightId))?.hash ?? getZeroHash(childLevel) : getZeroHash(childLevel);
|
|
7213
|
+
rightResult = { id: prevRightId, hash: rightHash };
|
|
7214
|
+
}
|
|
7215
|
+
const hash = _MerkleEngine.hashPair(leftResult.hash, rightResult.hash);
|
|
7216
|
+
const newId = `cm-${version}-${originalLevel}`;
|
|
7217
|
+
nodes.push({ chainId, id: newId, hash, leftId: leftResult.id, rightId: rightResult.id });
|
|
7218
|
+
return { id: newId, hash };
|
|
7219
|
+
};
|
|
7220
|
+
const root = await descend(prevRootId, 0);
|
|
7221
|
+
return { rootId: root.id, rootHash: root.hash, nodes };
|
|
7171
7222
|
}
|
|
7172
|
-
|
|
7173
|
-
* Hydrate local merkle state from storage on first use.
|
|
7174
|
-
*/
|
|
7223
|
+
// ── Hydration ──
|
|
7175
7224
|
async hydrateFromStorage(chainId) {
|
|
7176
7225
|
if (this.mode === "remote") return;
|
|
7177
7226
|
if (this.hydratedChains.has(chainId)) return;
|
|
@@ -7180,31 +7229,17 @@ var MerkleEngine = class _MerkleEngine {
|
|
|
7180
7229
|
const task = (async () => {
|
|
7181
7230
|
try {
|
|
7182
7231
|
const state = this.ensureChainState(chainId);
|
|
7183
|
-
const leaves = await this.storage?.getMerkleLeaves?.(chainId);
|
|
7184
|
-
if (!leaves || leaves.length === 0) return;
|
|
7185
|
-
const sorted = [...leaves].map((l) => ({
|
|
7186
|
-
cid: l.cid,
|
|
7187
|
-
commitment: _MerkleEngine.normalizeHex32(l.commitment, "memo.commitment")
|
|
7188
|
-
})).sort((a, b) => a.cid - b.cid);
|
|
7189
|
-
for (let i = 0; i < sorted.length; i++) {
|
|
7190
|
-
if (sorted[i].cid !== i) throw new Error(`Non-contiguous persisted merkle leaves: expected cid=${i}, got cid=${sorted[i].cid}`);
|
|
7191
|
-
}
|
|
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);
|
|
7197
7232
|
const pending = this.ensurePendingLeaves(chainId);
|
|
7198
|
-
|
|
7199
|
-
|
|
7200
|
-
|
|
7201
|
-
|
|
7233
|
+
const latest = await this.storage?.getLatestChairmanMerkleVersion?.(chainId);
|
|
7234
|
+
if (latest) {
|
|
7235
|
+
state.mergedElements = latest.version;
|
|
7236
|
+
state.root = _MerkleEngine.normalizeHex32(latest.rootHash, "chairmanMerkleVersion.rootHash");
|
|
7202
7237
|
}
|
|
7203
|
-
|
|
7204
|
-
|
|
7205
|
-
|
|
7206
|
-
|
|
7207
|
-
|
|
7238
|
+
const leaves = await this.storage?.getMerkleLeaves?.(chainId);
|
|
7239
|
+
if (leaves && leaves.length > state.mergedElements) {
|
|
7240
|
+
const sorted = [...leaves].sort((a, b) => a.cid - b.cid).slice(state.mergedElements);
|
|
7241
|
+
pending.length = 0;
|
|
7242
|
+
pending.push(...sorted.map((l) => _MerkleEngine.normalizeHex32(l.commitment, "leaf.commitment")));
|
|
7208
7243
|
}
|
|
7209
7244
|
} catch (error) {
|
|
7210
7245
|
if (this.mode === "hybrid") return;
|
|
@@ -7217,26 +7252,7 @@ var MerkleEngine = class _MerkleEngine {
|
|
|
7217
7252
|
this.hydrateInFlight.set(chainId, task);
|
|
7218
7253
|
return task;
|
|
7219
7254
|
}
|
|
7220
|
-
|
|
7221
|
-
* Normalize unknown values to a 32-byte hex string.
|
|
7222
|
-
*/
|
|
7223
|
-
static normalizeHex32(value, name) {
|
|
7224
|
-
try {
|
|
7225
|
-
const bi = BigInt(value);
|
|
7226
|
-
if (bi < 0n) throw new Error("negative");
|
|
7227
|
-
const hex = bi.toString(16).padStart(64, "0");
|
|
7228
|
-
if (hex.length > 64) throw new Error("too_large");
|
|
7229
|
-
return `0x${hex}`;
|
|
7230
|
-
} catch (error) {
|
|
7231
|
-
throw new SdkError("MERKLE", `Invalid ${name}`, { value }, error);
|
|
7232
|
-
}
|
|
7233
|
-
}
|
|
7234
|
-
/**
|
|
7235
|
-
* Feed contiguous (cid-ordered) memo leaves into the local merkle tree.
|
|
7236
|
-
*
|
|
7237
|
-
* This mirrors the client/app behavior: only after we have a full consecutive batch of 32 leaves
|
|
7238
|
-
* do we merge them into the main tree. Leaves that are still in the buffer do not get local proofs.
|
|
7239
|
-
*/
|
|
7255
|
+
// ── Ingestion ──
|
|
7240
7256
|
async ingestEntryMemos(chainId, memos) {
|
|
7241
7257
|
if (this.mode === "remote") return;
|
|
7242
7258
|
await this.hydrateFromStorage(chainId);
|
|
@@ -7264,26 +7280,42 @@ var MerkleEngine = class _MerkleEngine {
|
|
|
7264
7280
|
expected++;
|
|
7265
7281
|
while (pending.length >= SUBTREE_SIZE) {
|
|
7266
7282
|
const batch = pending.splice(0, SUBTREE_SIZE);
|
|
7267
|
-
const
|
|
7268
|
-
const subtree = _MerkleEngine.buildSubtree(batch,
|
|
7269
|
-
const
|
|
7270
|
-
|
|
7271
|
-
|
|
7272
|
-
const
|
|
7273
|
-
|
|
7274
|
-
|
|
7275
|
-
|
|
7276
|
-
|
|
7277
|
-
|
|
7278
|
-
|
|
7279
|
-
|
|
7280
|
-
|
|
7281
|
-
|
|
7283
|
+
const batchIndex = state.mergedElements / SUBTREE_SIZE;
|
|
7284
|
+
const subtree = _MerkleEngine.buildSubtree(batch, state.mergedElements);
|
|
7285
|
+
const subtreeNodes = subtree.nodesToStore.map((n) => ({ ...n, chainId }));
|
|
7286
|
+
const prevVersion = await this.storage?.getLatestChairmanMerkleVersion?.(chainId);
|
|
7287
|
+
const prevRootId = prevVersion?.rootId ?? null;
|
|
7288
|
+
const newVersion = state.mergedElements + SUBTREE_SIZE;
|
|
7289
|
+
const result = await this.insertSubtreeRoot(chainId, prevRootId, subtree.subtreeRoot, batchIndex, newVersion);
|
|
7290
|
+
if (this.readContractRoot) {
|
|
7291
|
+
const rootIndex = newVersion / SUBTREE_SIZE;
|
|
7292
|
+
const onChainRoot = await this.readContractRoot(chainId, rootIndex).catch(() => null);
|
|
7293
|
+
if (onChainRoot !== null) {
|
|
7294
|
+
const onChainNorm = _MerkleEngine.normalizeHex32(onChainRoot, "onChainRoot");
|
|
7295
|
+
const isZero = BigInt(onChainNorm) === 0n;
|
|
7296
|
+
if (!isZero && onChainNorm !== result.rootHash) {
|
|
7297
|
+
const target = state.mergedElements;
|
|
7298
|
+
await this.rollback(chainId, target);
|
|
7299
|
+
throw new SdkError("MERKLE", "Local merkle root mismatch with on-chain root \u2014 rolled back", {
|
|
7300
|
+
chainId,
|
|
7301
|
+
rootIndex,
|
|
7302
|
+
localRoot: result.rootHash,
|
|
7303
|
+
onChainRoot: onChainNorm,
|
|
7304
|
+
version: newVersion,
|
|
7305
|
+
rollbackTarget: target
|
|
7306
|
+
});
|
|
7307
|
+
}
|
|
7308
|
+
}
|
|
7309
|
+
}
|
|
7310
|
+
await this.storage?.putChairmanMerkleNodes?.(chainId, [...subtreeNodes, ...result.nodes]);
|
|
7311
|
+
await this.storage?.putChairmanMerkleVersion?.(chainId, {
|
|
7282
7312
|
chainId,
|
|
7283
|
-
|
|
7284
|
-
|
|
7285
|
-
|
|
7313
|
+
version: newVersion,
|
|
7314
|
+
rootId: result.rootId,
|
|
7315
|
+
rootHash: result.rootHash
|
|
7286
7316
|
});
|
|
7317
|
+
state.mergedElements = newVersion;
|
|
7318
|
+
state.root = result.rootHash;
|
|
7287
7319
|
}
|
|
7288
7320
|
}
|
|
7289
7321
|
this.hydratedChains.add(chainId);
|
|
@@ -7294,15 +7326,59 @@ var MerkleEngine = class _MerkleEngine {
|
|
|
7294
7326
|
throw new SdkError("MERKLE", "Failed to ingest local merkle leaves", { chainId, leafCount: leaves.length }, error);
|
|
7295
7327
|
}
|
|
7296
7328
|
}
|
|
7329
|
+
// ── Rollback (tree O(1) + sync cursor reset) ──
|
|
7297
7330
|
/**
|
|
7298
|
-
*
|
|
7331
|
+
* Unified rollback: rewind tree to a previous batch boundary AND reset the
|
|
7332
|
+
* sync cursor so memo sync restarts from the same point.
|
|
7333
|
+
*
|
|
7334
|
+
* What gets rolled back:
|
|
7335
|
+
* - ChairmanMerkle tree version pointer (O(1) — old nodes still in storage)
|
|
7336
|
+
* - Pending leaves buffer (cleared)
|
|
7337
|
+
* - Sync cursor: memo + merkle fields (nullifier left unchanged — independent)
|
|
7338
|
+
*
|
|
7339
|
+
* @param targetMergedElements Must be a non-negative multiple of 32.
|
|
7340
|
+
* Pass 0 to reset to the empty tree.
|
|
7341
|
+
* @returns true if rollback succeeded, false if the target version doesn't exist.
|
|
7299
7342
|
*/
|
|
7300
|
-
async
|
|
7301
|
-
|
|
7343
|
+
async rollback(chainId, targetMergedElements) {
|
|
7344
|
+
if (targetMergedElements < 0 || targetMergedElements % SUBTREE_SIZE !== 0) {
|
|
7345
|
+
throw new SdkError("MERKLE", "rollback target must be a non-negative multiple of 32", { targetMergedElements });
|
|
7346
|
+
}
|
|
7347
|
+
const state = this.ensureChainState(chainId);
|
|
7348
|
+
const pending = this.ensurePendingLeaves(chainId);
|
|
7349
|
+
if (targetMergedElements === 0) {
|
|
7350
|
+
state.mergedElements = 0;
|
|
7351
|
+
state.root = getZeroHash(this.treeDepth);
|
|
7352
|
+
pending.length = 0;
|
|
7353
|
+
await this.resetSyncCursor(chainId, 0);
|
|
7354
|
+
return true;
|
|
7355
|
+
}
|
|
7356
|
+
const version = await this.storage?.getChairmanMerkleVersion?.(chainId, targetMergedElements);
|
|
7357
|
+
if (!version) return false;
|
|
7358
|
+
state.mergedElements = targetMergedElements;
|
|
7359
|
+
state.root = _MerkleEngine.normalizeHex32(version.rootHash, "version.rootHash");
|
|
7360
|
+
pending.length = 0;
|
|
7361
|
+
this.hydratedChains.add(chainId);
|
|
7362
|
+
await this.resetSyncCursor(chainId, targetMergedElements);
|
|
7363
|
+
return true;
|
|
7302
7364
|
}
|
|
7303
7365
|
/**
|
|
7304
|
-
*
|
|
7366
|
+
* Reset the sync cursor's memo field to `targetMemo` (and derive merkle cursor),
|
|
7367
|
+
* but only if the current cursor is ahead of the target.
|
|
7368
|
+
* Nullifier cursor is left unchanged — nullifiers are independent of tree state.
|
|
7305
7369
|
*/
|
|
7370
|
+
async resetSyncCursor(chainId, targetMemo) {
|
|
7371
|
+
if (!this.storage?.getSyncCursor || !this.storage?.setSyncCursor) return;
|
|
7372
|
+
const cursor = await this.storage.getSyncCursor(chainId);
|
|
7373
|
+
if (!cursor || cursor.memo <= targetMemo) return;
|
|
7374
|
+
cursor.memo = targetMemo;
|
|
7375
|
+
cursor.merkle = this.currentMerkleRootIndex(targetMemo);
|
|
7376
|
+
await this.storage.setSyncCursor(chainId, cursor);
|
|
7377
|
+
}
|
|
7378
|
+
// ── Proof generation ──
|
|
7379
|
+
async getProofByCid(input) {
|
|
7380
|
+
return this.getProofByCids({ chainId: input.chainId, cids: [input.cid], totalElements: input.totalElements });
|
|
7381
|
+
}
|
|
7306
7382
|
async getProofByCids(input) {
|
|
7307
7383
|
const cids = [...input.cids];
|
|
7308
7384
|
if (cids.length === 0) throw new SdkError("MERKLE", "No cids provided", { chainId: input.chainId });
|
|
@@ -7317,15 +7393,16 @@ var MerkleEngine = class _MerkleEngine {
|
|
|
7317
7393
|
await this.hydrateFromStorage(input.chainId);
|
|
7318
7394
|
const canUseLocal = this.mode !== "remote";
|
|
7319
7395
|
if (canUseLocal) {
|
|
7320
|
-
const
|
|
7321
|
-
const hasDb = typeof this.storage?.getMerkleLeaf === "function" && typeof this.storage?.
|
|
7322
|
-
if (hasDb
|
|
7323
|
-
|
|
7396
|
+
const version = contractTreeElements > 0 ? await this.storage?.getChairmanMerkleVersion?.(input.chainId, contractTreeElements) : void 0;
|
|
7397
|
+
const hasDb = typeof this.storage?.getMerkleLeaf === "function" && typeof this.storage?.getChairmanMerkleNode === "function" && (contractTreeElements === 0 || !!version);
|
|
7398
|
+
if (hasDb) {
|
|
7399
|
+
const state = this.ensureChainState(input.chainId);
|
|
7400
|
+
if (contractTreeElements > 0 && state.mergedElements < contractTreeElements) {
|
|
7324
7401
|
if (this.mode === "local") {
|
|
7325
7402
|
throw new SdkError("MERKLE", "Local merkle db is behind contract", {
|
|
7326
7403
|
chainId: input.chainId,
|
|
7327
7404
|
cids,
|
|
7328
|
-
|
|
7405
|
+
localMergedElements: state.mergedElements,
|
|
7329
7406
|
contractTreeElements
|
|
7330
7407
|
});
|
|
7331
7408
|
}
|
|
@@ -7337,30 +7414,13 @@ var MerkleEngine = class _MerkleEngine {
|
|
|
7337
7414
|
proof.push({ leaf_index: cid, path: new Array(this.treeDepth + 1).fill("0") });
|
|
7338
7415
|
continue;
|
|
7339
7416
|
}
|
|
7340
|
-
const
|
|
7341
|
-
if (!leaf) throw new Error(`missing_leaf:${cid}`);
|
|
7342
|
-
const path = [leaf.commitment];
|
|
7343
|
-
for (let level = 1; level <= this.treeDepth; level++) {
|
|
7344
|
-
const siblingIndex = cid >> level - 1 ^ 1;
|
|
7345
|
-
if (level === 1) {
|
|
7346
|
-
const siblingLeaf = await this.storage.getMerkleLeaf(input.chainId, siblingIndex);
|
|
7347
|
-
path.push(siblingLeaf?.commitment ?? getZeroHash(0));
|
|
7348
|
-
continue;
|
|
7349
|
-
}
|
|
7350
|
-
const targetLevel = level - 1;
|
|
7351
|
-
const siblingNode = await this.storage.getMerkleNode(input.chainId, `${targetLevel}-${siblingIndex}`);
|
|
7352
|
-
path.push(siblingNode?.hash ?? getZeroHash(targetLevel));
|
|
7353
|
-
}
|
|
7417
|
+
const path = await this.buildLocalProofPath(input.chainId, cid, version);
|
|
7354
7418
|
proof.push({ leaf_index: cid, path });
|
|
7355
7419
|
}
|
|
7356
|
-
|
|
7357
|
-
if (tree.totalElements > contractTreeElements && contractTreeElements > 0) {
|
|
7358
|
-
const checkpoint = await this.storage.getMerkleNode(input.chainId, `checkpoint-${contractTreeElements}`);
|
|
7359
|
-
if (checkpoint) effectiveRoot = checkpoint.hash;
|
|
7360
|
-
}
|
|
7420
|
+
const effectiveRoot = contractTreeElements > 0 ? _MerkleEngine.normalizeHex32(version.rootHash, "version.rootHash") : getZeroHash(this.treeDepth);
|
|
7361
7421
|
return {
|
|
7362
7422
|
proof,
|
|
7363
|
-
merkle_root:
|
|
7423
|
+
merkle_root: effectiveRoot,
|
|
7364
7424
|
latest_cid: totalElements > 0n ? Number(totalElements - 1n) : -1
|
|
7365
7425
|
};
|
|
7366
7426
|
} catch (error) {
|
|
@@ -7370,7 +7430,7 @@ var MerkleEngine = class _MerkleEngine {
|
|
|
7370
7430
|
}
|
|
7371
7431
|
}
|
|
7372
7432
|
} else if (this.mode === "local" && needsTreeProof.length) {
|
|
7373
|
-
throw new SdkError("MERKLE", "Local merkle db unavailable", { chainId: input.chainId, cids, reason: "
|
|
7433
|
+
throw new SdkError("MERKLE", "Local merkle db unavailable", { chainId: input.chainId, cids, reason: "missing_adapter_or_version" });
|
|
7374
7434
|
}
|
|
7375
7435
|
}
|
|
7376
7436
|
if (needsTreeProof.length === 0) {
|
|
@@ -7395,15 +7455,64 @@ var MerkleEngine = class _MerkleEngine {
|
|
|
7395
7455
|
};
|
|
7396
7456
|
}
|
|
7397
7457
|
/**
|
|
7398
|
-
*
|
|
7458
|
+
* Build a local proof path by traversing the chairmanMerkle tree.
|
|
7459
|
+
*
|
|
7460
|
+
* Levels 0-4: sibling hashes from subtree internal nodes (st-{level}-{pos}).
|
|
7461
|
+
* Levels 5-31: sibling hashes from chairmanMerkle tree traversal (top-down from version root).
|
|
7399
7462
|
*/
|
|
7463
|
+
async buildLocalProofPath(chainId, cid, version) {
|
|
7464
|
+
const leaf = await this.storage.getMerkleLeaf(chainId, cid);
|
|
7465
|
+
if (!leaf) throw new Error(`missing_leaf:${cid}`);
|
|
7466
|
+
const path = [leaf.commitment];
|
|
7467
|
+
for (let level = 1; level <= SUBTREE_DEPTH; level++) {
|
|
7468
|
+
const siblingPos = cid >> level - 1 ^ 1;
|
|
7469
|
+
if (level === 1) {
|
|
7470
|
+
const siblingLeaf = await this.storage.getMerkleLeaf(chainId, siblingPos);
|
|
7471
|
+
path.push(siblingLeaf?.commitment ?? getZeroHash(0));
|
|
7472
|
+
} else {
|
|
7473
|
+
const targetLevel = level - 1;
|
|
7474
|
+
const node = await this.storage.getChairmanMerkleNode(chainId, `st-${targetLevel}-${siblingPos}`);
|
|
7475
|
+
path.push(node?.hash ?? getZeroHash(targetLevel));
|
|
7476
|
+
}
|
|
7477
|
+
}
|
|
7478
|
+
const batchIndex = cid >> SUBTREE_DEPTH;
|
|
7479
|
+
const MAIN_DEPTH = this.treeDepth - SUBTREE_DEPTH;
|
|
7480
|
+
const mainSiblings = [];
|
|
7481
|
+
let nodeId = version.rootId;
|
|
7482
|
+
for (let depth = 0; depth < MAIN_DEPTH; depth++) {
|
|
7483
|
+
const childLevel = this.treeDepth - depth - 1;
|
|
7484
|
+
if (!nodeId) {
|
|
7485
|
+
mainSiblings.push(getZeroHash(childLevel));
|
|
7486
|
+
continue;
|
|
7487
|
+
}
|
|
7488
|
+
const node = await this.storage.getChairmanMerkleNode(chainId, nodeId);
|
|
7489
|
+
if (!node) {
|
|
7490
|
+
mainSiblings.push(getZeroHash(childLevel));
|
|
7491
|
+
nodeId = null;
|
|
7492
|
+
continue;
|
|
7493
|
+
}
|
|
7494
|
+
const remainingDepth = MAIN_DEPTH - depth - 1;
|
|
7495
|
+
const goRight = (batchIndex >> remainingDepth & 1) === 1;
|
|
7496
|
+
if (goRight) {
|
|
7497
|
+
const leftNode = node.leftId ? await this.storage.getChairmanMerkleNode(chainId, node.leftId) : null;
|
|
7498
|
+
mainSiblings.push(leftNode?.hash ?? getZeroHash(childLevel));
|
|
7499
|
+
nodeId = node.rightId;
|
|
7500
|
+
} else {
|
|
7501
|
+
const rightNode = node.rightId ? await this.storage.getChairmanMerkleNode(chainId, node.rightId) : null;
|
|
7502
|
+
mainSiblings.push(rightNode?.hash ?? getZeroHash(childLevel));
|
|
7503
|
+
nodeId = node.leftId;
|
|
7504
|
+
}
|
|
7505
|
+
}
|
|
7506
|
+
for (let i = mainSiblings.length - 1; i >= 0; i--) {
|
|
7507
|
+
path.push(mainSiblings[i]);
|
|
7508
|
+
}
|
|
7509
|
+
return path;
|
|
7510
|
+
}
|
|
7511
|
+
// ── Remote helpers ──
|
|
7400
7512
|
async fetchRemoteRootOnly(chainId) {
|
|
7401
7513
|
const remote = await this.fetchRemoteProofFromService({ chainId, cids: [0] });
|
|
7402
7514
|
return _MerkleEngine.normalizeHex32(remote.merkle_root, "remote.merkle_root");
|
|
7403
7515
|
}
|
|
7404
|
-
/**
|
|
7405
|
-
* Fetch proofs from the remote merkle service.
|
|
7406
|
-
*/
|
|
7407
7516
|
async fetchRemoteProofFromService(input) {
|
|
7408
7517
|
const chain = this.getChain(input.chainId);
|
|
7409
7518
|
if (!chain.merkleProofUrl) {
|
|
@@ -7412,9 +7521,7 @@ var MerkleEngine = class _MerkleEngine {
|
|
|
7412
7521
|
const client = new MerkleClient(chain.merkleProofUrl);
|
|
7413
7522
|
return client.getProofByCids(input.cids);
|
|
7414
7523
|
}
|
|
7415
|
-
|
|
7416
|
-
* Build membership witnesses for provided UTXOs from a remote proof response.
|
|
7417
|
-
*/
|
|
7524
|
+
// ── Witness builders (unchanged) ──
|
|
7418
7525
|
buildAccMemberWitnesses(input) {
|
|
7419
7526
|
return input.utxos.map((utxo, idx) => {
|
|
7420
7527
|
const remoteProof = input.remote.proof[idx];
|
|
@@ -7428,9 +7535,6 @@ var MerkleEngine = class _MerkleEngine {
|
|
|
7428
7535
|
};
|
|
7429
7536
|
});
|
|
7430
7537
|
}
|
|
7431
|
-
/**
|
|
7432
|
-
* Convert UTXOs into circuit input secrets, decrypting memos and padding if needed.
|
|
7433
|
-
*/
|
|
7434
7538
|
async buildInputSecretsFromUtxos(input) {
|
|
7435
7539
|
if (!Array.isArray(input.utxos) || input.utxos.length === 0) {
|
|
7436
7540
|
throw new SdkError("MERKLE", "No utxos provided", { count: 0 });
|