@unlink-xyz/core 0.1.3-canary.f82115c → 0.1.3-canary.fd5dddf

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.
@@ -75263,82 +75263,6 @@ var init_fs = __esm({
75263
75263
  init_process();
75264
75264
  init_buffer();
75265
75265
 
75266
- // circuits.json
75267
- var circuits_default = {
75268
- joinsplit_1x1_16: {
75269
- file: "joinsplit",
75270
- template: "JoinSplit",
75271
- pubs: ["merkleRoot", "boundParamsHash", "nullifiers", "commitmentsOut"],
75272
- params: [1, 1, 16]
75273
- },
75274
- joinsplit_1x2_16: {
75275
- file: "joinsplit",
75276
- template: "JoinSplit",
75277
- pubs: ["merkleRoot", "boundParamsHash", "nullifiers", "commitmentsOut"],
75278
- params: [1, 2, 16]
75279
- },
75280
- joinsplit_2x1_16: {
75281
- file: "joinsplit",
75282
- template: "JoinSplit",
75283
- pubs: ["merkleRoot", "boundParamsHash", "nullifiers", "commitmentsOut"],
75284
- params: [2, 1, 16]
75285
- },
75286
- joinsplit_2x2_16: {
75287
- file: "joinsplit",
75288
- template: "JoinSplit",
75289
- pubs: ["merkleRoot", "boundParamsHash", "nullifiers", "commitmentsOut"],
75290
- params: [2, 2, 16]
75291
- },
75292
- joinsplit_2x3_16: {
75293
- file: "joinsplit",
75294
- template: "JoinSplit",
75295
- pubs: ["merkleRoot", "boundParamsHash", "nullifiers", "commitmentsOut"],
75296
- params: [2, 3, 16]
75297
- },
75298
- joinsplit_3x1_16: {
75299
- file: "joinsplit",
75300
- template: "JoinSplit",
75301
- pubs: ["merkleRoot", "boundParamsHash", "nullifiers", "commitmentsOut"],
75302
- params: [3, 1, 16]
75303
- },
75304
- joinsplit_3x2_16: {
75305
- file: "joinsplit",
75306
- template: "JoinSplit",
75307
- pubs: ["merkleRoot", "boundParamsHash", "nullifiers", "commitmentsOut"],
75308
- params: [3, 2, 16]
75309
- },
75310
- joinsplit_3x3_16: {
75311
- file: "joinsplit",
75312
- template: "JoinSplit",
75313
- pubs: ["merkleRoot", "boundParamsHash", "nullifiers", "commitmentsOut"],
75314
- params: [3, 3, 16]
75315
- },
75316
- joinsplit_4x1_16: {
75317
- file: "joinsplit",
75318
- template: "JoinSplit",
75319
- pubs: ["merkleRoot", "boundParamsHash", "nullifiers", "commitmentsOut"],
75320
- params: [4, 1, 16]
75321
- },
75322
- joinsplit_4x2_16: {
75323
- file: "joinsplit",
75324
- template: "JoinSplit",
75325
- pubs: ["merkleRoot", "boundParamsHash", "nullifiers", "commitmentsOut"],
75326
- params: [4, 2, 16]
75327
- },
75328
- joinsplit_5x1_16: {
75329
- file: "joinsplit",
75330
- template: "JoinSplit",
75331
- pubs: ["merkleRoot", "boundParamsHash", "nullifiers", "commitmentsOut"],
75332
- params: [5, 1, 16]
75333
- },
75334
- joinsplit_5x2_16: {
75335
- file: "joinsplit",
75336
- template: "JoinSplit",
75337
- pubs: ["merkleRoot", "boundParamsHash", "nullifiers", "commitmentsOut"],
75338
- params: [5, 2, 16]
75339
- }
75340
- };
75341
-
75342
75266
  // constants.ts
75343
75267
  init_process();
75344
75268
  init_buffer();
@@ -75622,7 +75546,7 @@ function createIndexedDbStorage(opts = {}) {
75622
75546
  } catch (err2) {
75623
75547
  try {
75624
75548
  tx.abort();
75625
- } catch (_) {
75549
+ } catch {
75626
75550
  }
75627
75551
  await done.catch(() => {
75628
75552
  });
@@ -78084,7 +78008,7 @@ function encodeJson(value) {
78084
78008
  function decodeJson(payload) {
78085
78009
  try {
78086
78010
  return JSON.parse(decoder.decode(payload));
78087
- } catch (err2) {
78011
+ } catch {
78088
78012
  throw new CoreError("failed to decode stored state payload");
78089
78013
  }
78090
78014
  }
@@ -78536,6 +78460,36 @@ function createNullifierStore(storage) {
78536
78460
  const prefix = `nullifiers:${chainId}:`;
78537
78461
  const entries = await storage.iter({ prefix });
78538
78462
  return entries.length;
78463
+ },
78464
+ /**
78465
+ * Remove all cached nullifiers for a chain.
78466
+ */
78467
+ async clearNullifiers(chainId) {
78468
+ const prefix = `nullifiers:${chainId}:`;
78469
+ const entries = await storage.iter({ prefix });
78470
+ if (entries.length === 0) return;
78471
+ await storage.batch(entries.map(({ key: key2 }) => ({ del: key2 })));
78472
+ },
78473
+ /**
78474
+ * Remove cached nullifiers whose noteIndex >= fromIndex.
78475
+ * Used by partial resync to keep the count-based optimization accurate.
78476
+ */
78477
+ async clearNullifiersFromIndex(chainId, fromIndex) {
78478
+ const prefix = `nullifiers:${chainId}:`;
78479
+ const entries = await storage.iter({ prefix });
78480
+ const ops = [];
78481
+ for (const { key: key2, value } of entries) {
78482
+ try {
78483
+ const record = JSON.parse(
78484
+ new TextDecoder().decode(value)
78485
+ );
78486
+ if (record.noteIndex === void 0 || record.noteIndex >= fromIndex) {
78487
+ ops.push({ del: key2 });
78488
+ }
78489
+ } catch {
78490
+ }
78491
+ }
78492
+ if (ops.length > 0) await storage.batch(ops);
78539
78493
  }
78540
78494
  };
78541
78495
  }
@@ -78615,6 +78569,32 @@ function createStateStore(storage) {
78615
78569
  }
78616
78570
  await storage.batch(ops);
78617
78571
  }
78572
+ async function clearChainDataFromIndex(chainId, fromIndex) {
78573
+ const prefixes = [
78574
+ `notes:${chainId}:`,
78575
+ `leaves:${chainId}:`,
78576
+ `ciphertexts:${chainId}:`
78577
+ ];
78578
+ const ops = [];
78579
+ for (const prefix of prefixes) {
78580
+ const entries = await storage.iter({ prefix });
78581
+ for (const { key: key2 } of entries) {
78582
+ const idx = parseInt(key2.slice(key2.lastIndexOf(":") + 1), 10);
78583
+ if (!isNaN(idx) && idx >= fromIndex) {
78584
+ ops.push({ del: key2 });
78585
+ }
78586
+ }
78587
+ }
78588
+ const unspentPrefix = `idx:notes:unspent:${chainId}:`;
78589
+ const unspentEntries = await storage.iter({ prefix: unspentPrefix });
78590
+ for (const { key: key2 } of unspentEntries) {
78591
+ const idx = parseInt(key2.slice(key2.lastIndexOf(":") + 1), 10);
78592
+ if (!isNaN(idx) && idx >= fromIndex) {
78593
+ ops.push({ del: key2 });
78594
+ }
78595
+ }
78596
+ if (ops.length > 0) await storage.batch(ops);
78597
+ }
78618
78598
  return {
78619
78599
  ...notes,
78620
78600
  ...nullifiers,
@@ -78623,7 +78603,8 @@ function createStateStore(storage) {
78623
78603
  ...ciphertexts,
78624
78604
  ...jobs,
78625
78605
  ...history,
78626
- syncCommitment
78606
+ syncCommitment,
78607
+ clearChainDataFromIndex
78627
78608
  };
78628
78609
  }
78629
78610
 
@@ -89160,7 +89141,7 @@ function createIndexerClient(baseUrl, deps) {
89160
89141
  // config.ts
89161
89142
  init_process();
89162
89143
  init_buffer();
89163
- var CONFIG_URL = "https://raw.githubusercontent.com/unlink-xyz/config/main/environments.json";
89144
+ var CONFIG_URL = "https://api.unlink.xyz/config/networks.json";
89164
89145
  async function fetchEnvironmentConfig(env3) {
89165
89146
  const res = await fetch(CONFIG_URL);
89166
89147
  if (!res.ok) {
@@ -106387,84 +106368,96 @@ var Runtime = {
106387
106368
  // prover/registry.ts
106388
106369
  init_process();
106389
106370
  init_buffer();
106390
- var CIRCUIT_REGISTRY = {
106391
- "1x1": {
106392
- name: "joinsplit_1x1_16",
106393
- inputs: 1,
106394
- outputs: 1,
106395
- depth: 16
106371
+
106372
+ // ../../zk/circuits.json
106373
+ var circuits_default = {
106374
+ joinsplit_1x1_16: {
106375
+ file: "joinsplit",
106376
+ template: "JoinSplit",
106377
+ pubs: ["merkleRoot", "boundParamsHash", "nullifiers", "commitmentsOut"],
106378
+ params: [1, 1, 16]
106396
106379
  },
106397
- "1x2": {
106398
- name: "joinsplit_1x2_16",
106399
- inputs: 1,
106400
- outputs: 2,
106401
- depth: 16
106380
+ joinsplit_1x2_16: {
106381
+ file: "joinsplit",
106382
+ template: "JoinSplit",
106383
+ pubs: ["merkleRoot", "boundParamsHash", "nullifiers", "commitmentsOut"],
106384
+ params: [1, 2, 16]
106402
106385
  },
106403
- "2x1": {
106404
- name: "joinsplit_2x1_16",
106405
- inputs: 2,
106406
- outputs: 1,
106407
- depth: 16
106386
+ joinsplit_2x1_16: {
106387
+ file: "joinsplit",
106388
+ template: "JoinSplit",
106389
+ pubs: ["merkleRoot", "boundParamsHash", "nullifiers", "commitmentsOut"],
106390
+ params: [2, 1, 16]
106408
106391
  },
106409
- "2x2": {
106410
- name: "joinsplit_2x2_16",
106411
- inputs: 2,
106412
- outputs: 2,
106413
- depth: 16
106392
+ joinsplit_2x2_16: {
106393
+ file: "joinsplit",
106394
+ template: "JoinSplit",
106395
+ pubs: ["merkleRoot", "boundParamsHash", "nullifiers", "commitmentsOut"],
106396
+ params: [2, 2, 16]
106414
106397
  },
106415
- "2x3": {
106416
- name: "joinsplit_2x3_16",
106417
- inputs: 2,
106418
- outputs: 3,
106419
- depth: 16
106398
+ joinsplit_2x3_16: {
106399
+ file: "joinsplit",
106400
+ template: "JoinSplit",
106401
+ pubs: ["merkleRoot", "boundParamsHash", "nullifiers", "commitmentsOut"],
106402
+ params: [2, 3, 16]
106420
106403
  },
106421
- "3x1": {
106422
- name: "joinsplit_3x1_16",
106423
- inputs: 3,
106424
- outputs: 1,
106425
- depth: 16
106404
+ joinsplit_3x1_16: {
106405
+ file: "joinsplit",
106406
+ template: "JoinSplit",
106407
+ pubs: ["merkleRoot", "boundParamsHash", "nullifiers", "commitmentsOut"],
106408
+ params: [3, 1, 16]
106426
106409
  },
106427
- "3x2": {
106428
- name: "joinsplit_3x2_16",
106429
- inputs: 3,
106430
- outputs: 2,
106431
- depth: 16
106410
+ joinsplit_3x2_16: {
106411
+ file: "joinsplit",
106412
+ template: "JoinSplit",
106413
+ pubs: ["merkleRoot", "boundParamsHash", "nullifiers", "commitmentsOut"],
106414
+ params: [3, 2, 16]
106432
106415
  },
106433
- "3x3": {
106434
- name: "joinsplit_3x3_16",
106435
- inputs: 3,
106436
- outputs: 3,
106437
- depth: 16
106416
+ joinsplit_3x3_16: {
106417
+ file: "joinsplit",
106418
+ template: "JoinSplit",
106419
+ pubs: ["merkleRoot", "boundParamsHash", "nullifiers", "commitmentsOut"],
106420
+ params: [3, 3, 16]
106438
106421
  },
106439
- "4x1": {
106440
- name: "joinsplit_4x1_16",
106441
- inputs: 4,
106442
- outputs: 1,
106443
- depth: 16
106422
+ joinsplit_4x1_16: {
106423
+ file: "joinsplit",
106424
+ template: "JoinSplit",
106425
+ pubs: ["merkleRoot", "boundParamsHash", "nullifiers", "commitmentsOut"],
106426
+ params: [4, 1, 16]
106444
106427
  },
106445
- "4x2": {
106446
- name: "joinsplit_4x2_16",
106447
- inputs: 4,
106448
- outputs: 2,
106449
- depth: 16
106428
+ joinsplit_4x2_16: {
106429
+ file: "joinsplit",
106430
+ template: "JoinSplit",
106431
+ pubs: ["merkleRoot", "boundParamsHash", "nullifiers", "commitmentsOut"],
106432
+ params: [4, 2, 16]
106450
106433
  },
106451
- "5x1": {
106452
- name: "joinsplit_5x1_16",
106453
- inputs: 5,
106454
- outputs: 1,
106455
- depth: 16
106434
+ joinsplit_5x1_16: {
106435
+ file: "joinsplit",
106436
+ template: "JoinSplit",
106437
+ pubs: ["merkleRoot", "boundParamsHash", "nullifiers", "commitmentsOut"],
106438
+ params: [5, 1, 16]
106456
106439
  },
106457
- "5x2": {
106458
- name: "joinsplit_5x2_16",
106459
- inputs: 5,
106460
- outputs: 2,
106461
- depth: 16
106440
+ joinsplit_5x2_16: {
106441
+ file: "joinsplit",
106442
+ template: "JoinSplit",
106443
+ pubs: ["merkleRoot", "boundParamsHash", "nullifiers", "commitmentsOut"],
106444
+ params: [5, 2, 16]
106462
106445
  }
106463
106446
  };
106447
+
106448
+ // prover/registry.ts
106449
+ var registry = {};
106450
+ for (const [name, def] of Object.entries(circuits_default)) {
106451
+ const params = def.params;
106452
+ const inputs = params[0];
106453
+ const outputs = params[1];
106454
+ const depth = params[2];
106455
+ registry[`${inputs}x${outputs}`] = { name, inputs, outputs, depth };
106456
+ }
106457
+ var CIRCUIT_REGISTRY = registry;
106464
106458
  var SUPPORTED_CIRCUITS = Object.keys(CIRCUIT_REGISTRY);
106465
106459
  function getCircuitConfig(inputs, outputs) {
106466
- const key2 = `${inputs}x${outputs}`;
106467
- return key2 in CIRCUIT_REGISTRY ? CIRCUIT_REGISTRY[key2] : void 0;
106460
+ return CIRCUIT_REGISTRY[`${inputs}x${outputs}`];
106468
106461
  }
106469
106462
 
106470
106463
  // prover/prover.ts
@@ -106848,8 +106841,9 @@ async function transact(store, req, opts) {
106848
106841
  merkleRoot: parseHexToBigInt(r3.merkleRoot),
106849
106842
  nullifierHashes: r3.contexts.map((c) => c.nullifier),
106850
106843
  newCommitments: r3.proof.pubSignals.slice(
106851
- 2 + r3.contexts.length
106844
+ 2 + r3.contexts.length,
106852
106845
  // Skip root + boundParams + nullifiers
106846
+ r3.withdrawal.amount > 0n ? r3.proof.pubSignals.length - 1 : void 0
106853
106847
  ),
106854
106848
  context: { chainId: BigInt(req.chainId), poolAddress: req.poolAddress },
106855
106849
  withdrawal: r3.withdrawal,
@@ -107734,12 +107728,71 @@ function createNoteSyncService(stateStore, options = {}) {
107734
107728
  }
107735
107729
  }
107736
107730
  }
107731
+ async function diagnoseChainState(chainId, localCount) {
107732
+ const firstBatch = await indexerClient.fetchCommitmentBatch({
107733
+ chainId,
107734
+ start: 0,
107735
+ limit: 1
107736
+ });
107737
+ const localFirst = await stateStore.getLeaf(chainId, 0);
107738
+ if (firstBatch.commitments.length === 0 || !localFirst) {
107739
+ return { kind: "full-divergence" };
107740
+ }
107741
+ if (firstBatch.commitments[0].commitment !== localFirst.commitment) {
107742
+ return { kind: "full-divergence" };
107743
+ }
107744
+ if (localCount === 1) {
107745
+ return { kind: "consistent" };
107746
+ }
107747
+ const lastBatch = await indexerClient.fetchCommitmentBatch({
107748
+ chainId,
107749
+ start: localCount - 1,
107750
+ limit: 1
107751
+ });
107752
+ const localLast = await stateStore.getLeaf(chainId, localCount - 1);
107753
+ if (lastBatch.commitments.length > 0 && localLast && lastBatch.commitments[0].commitment === localLast.commitment) {
107754
+ return { kind: "consistent" };
107755
+ }
107756
+ const divergePoint = await findDivergencePoint(chainId, 1, localCount - 1);
107757
+ return { kind: "partial-divergence", validPrefix: divergePoint };
107758
+ }
107759
+ async function findDivergencePoint(chainId, low, high) {
107760
+ while (low < high) {
107761
+ const mid = Math.floor((low + high) / 2);
107762
+ const batch = await indexerClient.fetchCommitmentBatch({
107763
+ chainId,
107764
+ start: mid,
107765
+ limit: 1
107766
+ });
107767
+ const localLeaf = await stateStore.getLeaf(chainId, mid);
107768
+ if (batch.commitments.length > 0 && localLeaf && batch.commitments[0].commitment === localLeaf.commitment) {
107769
+ low = mid + 1;
107770
+ } else {
107771
+ high = mid;
107772
+ }
107773
+ }
107774
+ return low;
107775
+ }
107776
+ async function partialResync(chainId, fromIndex, account) {
107777
+ await Promise.all([
107778
+ stateStore.clearChainDataFromIndex(chainId, fromIndex),
107779
+ stateStore.clearNullifiersFromIndex(chainId, fromIndex)
107780
+ ]);
107781
+ trees.reset(chainId);
107782
+ await rebuildTreeFromStore({
107783
+ chainId,
107784
+ trees,
107785
+ loadLeaf: stateStore.getLeaf.bind(stateStore)
107786
+ });
107787
+ await ingestFrom(chainId, fromIndex, account);
107788
+ }
107737
107789
  async function fullResync(chainId, account) {
107738
107790
  trees.reset(chainId);
107739
107791
  await Promise.all([
107740
107792
  stateStore.clearLeaves(chainId),
107741
107793
  stateStore.clearNotes(chainId),
107742
- stateStore.clearCiphertexts(chainId)
107794
+ stateStore.clearCiphertexts(chainId),
107795
+ stateStore.clearNullifiers(chainId)
107743
107796
  ]);
107744
107797
  await ingestFrom(chainId, 0, account);
107745
107798
  }
@@ -107870,10 +107923,43 @@ function createNoteSyncService(stateStore, options = {}) {
107870
107923
  });
107871
107924
  if (opts.forceFullResync) {
107872
107925
  await fullResync(chainId, account);
107926
+ } else if (start > 0) {
107927
+ try {
107928
+ const diagnosis = await diagnoseChainState(chainId, start);
107929
+ switch (diagnosis.kind) {
107930
+ case "consistent":
107931
+ try {
107932
+ await ingestFrom(chainId, start, account);
107933
+ } catch (err2) {
107934
+ console.warn(
107935
+ `[note-sync] incremental ingest failed for chain ${chainId}, falling back to full resync:`,
107936
+ err2
107937
+ );
107938
+ await fullResync(chainId, account);
107939
+ }
107940
+ break;
107941
+ case "partial-divergence":
107942
+ await partialResync(chainId, diagnosis.validPrefix, account);
107943
+ break;
107944
+ case "full-divergence":
107945
+ await fullResync(chainId, account);
107946
+ break;
107947
+ }
107948
+ } catch (err2) {
107949
+ console.warn(
107950
+ `[note-sync] chain diagnosis failed for chain ${chainId}, falling back to full resync:`,
107951
+ err2
107952
+ );
107953
+ await fullResync(chainId, account);
107954
+ }
107873
107955
  } else {
107874
107956
  try {
107875
- await ingestFrom(chainId, start, account);
107876
- } catch {
107957
+ await ingestFrom(chainId, 0, account);
107958
+ } catch (err2) {
107959
+ console.warn(
107960
+ `[note-sync] initial ingest failed for chain ${chainId}, falling back to full resync:`,
107961
+ err2
107962
+ );
107877
107963
  await fullResync(chainId, account);
107878
107964
  }
107879
107965
  }
@@ -107934,7 +108020,6 @@ export {
107934
108020
  SchemaMismatchError,
107935
108021
  ValidationError,
107936
108022
  assertNonNegative,
107937
- circuits_default as circuitsConfig,
107938
108023
  computeBalances,
107939
108024
  computeMasterPublicKey,
107940
108025
  computeNullifyingKey,