@cloak.dev/sdk 0.1.3 → 0.1.4

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/index.cjs CHANGED
@@ -4978,6 +4978,7 @@ async function buildMerkleTree(commitments, height = MERKLE_TREE_HEIGHT2) {
4978
4978
 
4979
4979
  // src/utils/relay-client.ts
4980
4980
  var import_bs58 = __toESM(require("bs58"), 1);
4981
+ var bs58 = import_bs58.default.default || import_bs58.default;
4981
4982
  var TRANSACT_TAG = 0;
4982
4983
  var TRANSACT_SWAP_TAG = 1;
4983
4984
  var DEPOSIT_TAG = 1;
@@ -5177,7 +5178,7 @@ async function buildMerkleTreeFromChain(connection, programId, merkleTree, onPro
5177
5178
  for (const ix of tx.transaction.message.instructions) {
5178
5179
  if (!("programId" in ix) || !("data" in ix)) continue;
5179
5180
  if (ix.programId.toBase58() !== programIdBase58) continue;
5180
- const data = import_bs58.default.decode(ix.data);
5181
+ const data = bs58.decode(ix.data);
5181
5182
  const extracted = extractCommitmentsFromInstruction(data);
5182
5183
  for (const commitment of extracted) {
5183
5184
  commitments.push(BigInt("0x" + Buffer.from(commitment).toString("hex")));
@@ -6815,7 +6816,7 @@ async function transact(params, options) {
6815
6816
  }
6816
6817
  merkleTree = null;
6817
6818
  }
6818
- if (needsTreeForProof && relayUrl && !relayTreeDisabledForMint && !forceChainIndexing && (forceRefresh || !merkleTree)) {
6819
+ if (needsTreeForProof && relayUrl && !relayTreeDisabledForMint && !forceChainIndexing && (forceRefresh || !merkleTree) && !hasCachedTree) {
6819
6820
  try {
6820
6821
  const shouldRequestRelaySync = forceSync || requiredRelayIndex >= 0;
6821
6822
  const syncTimeoutMs = shouldRequestRelaySync ? Math.min(3e4, relayRequestTimeoutMs()) : 3500;
@@ -6840,7 +6841,7 @@ async function transact(params, options) {
6840
6841
  merkleTree = null;
6841
6842
  }
6842
6843
  }
6843
- if (merkleTree && treeState) {
6844
+ if (merkleTree && treeState && !hasCachedTree) {
6844
6845
  const relayRoot = merkleTree.root().toString(16).padStart(64, "0");
6845
6846
  const lengthDiff = treeState.nextIndex - merkleTree.length;
6846
6847
  if (merkleTree.length === 0) {
@@ -6971,7 +6972,7 @@ async function transact(params, options) {
6971
6972
  const disableNearTipFallbackEnv = typeof process !== "undefined" && !!process.env && (process.env.CLOAK_DISABLE_NEAR_TIP_ONCHAIN_PROOF === "1" || process.env.CLOAK_DISABLE_NEAR_TIP_ONCHAIN_PROOF === "true");
6972
6973
  const hasSiblingHints = !!utxo.siblingCommitment || !!utxo.leftSiblingCommitment;
6973
6974
  const baseNearTipEligible = chainDistanceFromTip !== null && chainDistanceFromTip >= 0 && chainDistanceFromTip <= nearTipMaxDistance;
6974
- const allowNearTipOnChainFallback = relayTreeDisabledForMint && !disableNearTipFallbackEnv && baseNearTipEligible && (forceEnableNearTipFallbackEnv || useSiblingInfo && hasSiblingHints);
6975
+ let allowNearTipOnChainFallback = relayTreeDisabledForMint && !disableNearTipFallbackEnv && baseNearTipEligible && (forceEnableNearTipFallbackEnv || useSiblingInfo && hasSiblingHints);
6975
6976
  if (!relayTreeDisabledForMint && !useSiblingInfo) {
6976
6977
  onProgress?.(`Relay tree missing index ${utxo.index} (length: ${treeLength}) with sibling cache disabled; proof may require retry after relay sync.`);
6977
6978
  } else if (!relayTreeDisabledForMint && relayUrl && !forceChainIndexing && utxo.index >= treeLength && !allowNearTipOnChainFallback) {
@@ -6982,9 +6983,10 @@ async function transact(params, options) {
6982
6983
  onProgress?.(`Relay tree is lagging (missing index ${utxo.index}, length: ${treeLength}); using guarded on-chain proof fallback.`);
6983
6984
  }
6984
6985
  if (relayUrl && !relayTreeDisabledForMint && !forceChainIndexing && utxo.index >= treeLength && !allowNearTipOnChainFallback) {
6985
- throw new Error(
6986
- `Relay tree missing required commitment index ${utxo.index} (tree length: ${treeLength}); waiting for relay sync is required.`
6986
+ onProgress?.(
6987
+ `Relay tree missing index ${utxo.index} (tree length: ${treeLength}) \u2014 falling through to on-chain proof.`
6987
6988
  );
6989
+ allowNearTipOnChainFallback = true;
6988
6990
  }
6989
6991
  if (allowNearTipOnChainFallback && relayTreeDisabledForMint) {
6990
6992
  onProgress?.(
@@ -7218,11 +7220,19 @@ async function transact(params, options) {
7218
7220
  onProgress?.(`Merkle proof error (stale sibling data), will regenerate with fresh tree...`);
7219
7221
  lastError = new Error(`Circuit assertion failed: ${errorMsg}`);
7220
7222
  if (forceChainIndexing) {
7221
- onProgress?.("On-chain fallback proof failed; returning to relay-sync mode.");
7222
- forceChainIndexing = false;
7223
+ onProgress?.("On-chain frontier proof failed; escalating to full chain tree rebuild...");
7224
+ try {
7225
+ merkleTree = await buildMerkleTreeFromChain(connection, programId, pdas.merkleTree, onProgress);
7226
+ onProgress?.(`Chain tree rebuilt with ${merkleTree.length} leaves.`);
7227
+ } catch (chainBuildErr) {
7228
+ onProgress?.(`Chain tree rebuild failed (${chainBuildErr?.message?.slice(0, 80)}); returning to relay-sync mode.`);
7229
+ forceChainIndexing = false;
7230
+ merkleTree = null;
7231
+ }
7232
+ } else {
7233
+ merkleTree = null;
7223
7234
  }
7224
7235
  treeState = null;
7225
- merkleTree = null;
7226
7236
  useSiblingInfo = false;
7227
7237
  const waitMs = Math.min(retryDelayMs * Math.pow(2, Math.min(submissionAttempts, 4)), 5e3);
7228
7238
  if (waitMs > 0) {
@@ -7569,19 +7579,26 @@ async function transact(params, options) {
7569
7579
  throw new Error(`Transaction failed: ${lastError.message}`);
7570
7580
  }
7571
7581
  if (relayUrl && outputCommitments.length >= 2) {
7572
- const reconcileSeedIndices = commitmentIndices[0] >= 0 && commitmentIndices[1] >= 0 ? commitmentIndices : lastObservedNextIndexForOutputs !== null ? [lastObservedNextIndexForOutputs, lastObservedNextIndexForOutputs + 1] : commitmentIndices;
7573
- const reconciledIndices = await reconcileCommitmentIndicesWithRelay(
7574
- relayUrl,
7575
- mint,
7576
- reconcileSeedIndices,
7577
- [outputCommitments[0], outputCommitments[1]],
7578
- onProgress
7579
- );
7580
- if (reconciledIndices[0] !== commitmentIndices[0] || reconciledIndices[1] !== commitmentIndices[1]) {
7582
+ const hasOnChainIndices = commitmentIndices[0] >= 0 && commitmentIndices[1] >= 0;
7583
+ if (!hasOnChainIndices) {
7584
+ const reconcileSeedIndices = lastObservedNextIndexForOutputs !== null ? [lastObservedNextIndexForOutputs, lastObservedNextIndexForOutputs + 1] : commitmentIndices;
7585
+ const reconciledIndices = await reconcileCommitmentIndicesWithRelay(
7586
+ relayUrl,
7587
+ mint,
7588
+ reconcileSeedIndices,
7589
+ [outputCommitments[0], outputCommitments[1]],
7590
+ onProgress
7591
+ );
7592
+ if (reconciledIndices[0] !== commitmentIndices[0] || reconciledIndices[1] !== commitmentIndices[1]) {
7593
+ onProgress?.(
7594
+ `Adjusted commitment indices after relay reconciliation: [${commitmentIndices[0]}, ${commitmentIndices[1]}] -> [${reconciledIndices[0]}, ${reconciledIndices[1]}]`
7595
+ );
7596
+ commitmentIndices = reconciledIndices;
7597
+ }
7598
+ } else {
7581
7599
  onProgress?.(
7582
- `Adjusted commitment indices after relay reconciliation: [${commitmentIndices[0]}, ${commitmentIndices[1]}] -> [${reconciledIndices[0]}, ${reconciledIndices[1]}]`
7600
+ `Using on-chain commitment indices [${commitmentIndices[0]}, ${commitmentIndices[1]}] (skipping relay reconciliation).`
7583
7601
  );
7584
- commitmentIndices = reconciledIndices;
7585
7602
  }
7586
7603
  }
7587
7604
  const siblingCommitments = [
@@ -7934,7 +7951,7 @@ async function swapUtxo(params, options) {
7934
7951
  }
7935
7952
  merkleTree = null;
7936
7953
  }
7937
- if (needsTreeForProof && relayUrl && !relayTreeDisabledForMint && !forceChainIndexing && (forceRefresh || !merkleTree)) {
7954
+ if (needsTreeForProof && relayUrl && !relayTreeDisabledForMint && !forceChainIndexing && (forceRefresh || !merkleTree) && !hasCachedTree) {
7938
7955
  try {
7939
7956
  const shouldRequestRelaySync = forceSync || requiredRelayIndex >= 0;
7940
7957
  const syncTimeoutMs = shouldRequestRelaySync ? Math.min(3e4, relayRequestTimeoutMs()) : 3500;
@@ -7959,7 +7976,7 @@ async function swapUtxo(params, options) {
7959
7976
  merkleTree = null;
7960
7977
  }
7961
7978
  }
7962
- if (merkleTree && merkleState) {
7979
+ if (merkleTree && merkleState && !hasCachedTree) {
7963
7980
  const relayRoot = merkleTree.root().toString(16).padStart(64, "0");
7964
7981
  const lengthDiff = merkleState.nextIndex - merkleTree.length;
7965
7982
  if (merkleTree.length === 0) {
@@ -8087,7 +8104,7 @@ async function swapUtxo(params, options) {
8087
8104
  const disableNearTipFallbackEnv = typeof process !== "undefined" && !!process.env && (process.env.CLOAK_DISABLE_NEAR_TIP_ONCHAIN_PROOF === "1" || process.env.CLOAK_DISABLE_NEAR_TIP_ONCHAIN_PROOF === "true");
8088
8105
  const hasSiblingHints = !!utxo.siblingCommitment || !!utxo.leftSiblingCommitment;
8089
8106
  const baseNearTipEligible = chainDistanceFromTip !== null && chainDistanceFromTip >= 0 && chainDistanceFromTip <= nearTipMaxDistance;
8090
- const allowNearTipOnChainFallback = relayTreeDisabledForMint && !disableNearTipFallbackEnv && baseNearTipEligible && (forceEnableNearTipFallbackEnv || useSiblingInfo && hasSiblingHints);
8107
+ let allowNearTipOnChainFallback = relayTreeDisabledForMint && !disableNearTipFallbackEnv && baseNearTipEligible && (forceEnableNearTipFallbackEnv || useSiblingInfo && hasSiblingHints);
8091
8108
  if (!relayTreeDisabledForMint && !useSiblingInfo) {
8092
8109
  onProgress?.(`Relay tree missing index ${utxo.index} (length: ${treeLength}) with sibling cache disabled; proof may require retry after relay sync.`);
8093
8110
  } else if (!relayTreeDisabledForMint && relayUrl && !forceChainIndexing && utxo.index >= treeLength && !allowNearTipOnChainFallback) {
@@ -8098,9 +8115,10 @@ async function swapUtxo(params, options) {
8098
8115
  onProgress?.(`Relay tree is lagging (missing index ${utxo.index}, length: ${treeLength}); using guarded on-chain proof fallback.`);
8099
8116
  }
8100
8117
  if (relayUrl && !relayTreeDisabledForMint && !forceChainIndexing && utxo.index >= treeLength && !allowNearTipOnChainFallback) {
8101
- throw new Error(
8102
- `Relay tree missing required commitment index ${utxo.index} (tree length: ${treeLength}); waiting for relay sync is required.`
8118
+ onProgress?.(
8119
+ `Relay tree missing index ${utxo.index} (tree length: ${treeLength}) \u2014 falling through to on-chain proof.`
8103
8120
  );
8121
+ allowNearTipOnChainFallback = true;
8104
8122
  }
8105
8123
  if (allowNearTipOnChainFallback && relayTreeDisabledForMint) {
8106
8124
  onProgress?.(
@@ -8304,10 +8322,18 @@ async function swapUtxo(params, options) {
8304
8322
  onProgress?.(`Merkle proof error (stale sibling data), will regenerate with fresh tree...`);
8305
8323
  lastError = new Error(`Circuit assertion failed: ${errorMsg}`);
8306
8324
  if (forceChainIndexing) {
8307
- onProgress?.("On-chain fallback proof failed; returning to relay-sync mode.");
8308
- forceChainIndexing = false;
8325
+ onProgress?.("On-chain frontier proof failed; escalating to full chain tree rebuild...");
8326
+ try {
8327
+ merkleTree = await buildMerkleTreeFromChain(connection, programId, pdas.merkleTree, onProgress);
8328
+ onProgress?.(`Chain tree rebuilt with ${merkleTree.length} leaves.`);
8329
+ } catch (chainBuildErr) {
8330
+ onProgress?.(`Chain tree rebuild failed (${chainBuildErr?.message?.slice(0, 80)}); returning to relay-sync mode.`);
8331
+ forceChainIndexing = false;
8332
+ merkleTree = null;
8333
+ }
8334
+ } else {
8335
+ merkleTree = null;
8309
8336
  }
8310
- merkleTree = null;
8311
8337
  merkleState = null;
8312
8338
  useSiblingInfo = false;
8313
8339
  const waitMs = Math.min(retryDelayMs * Math.pow(2, Math.min(attempt, 4)), 5e3);
@@ -8502,19 +8528,26 @@ async function swapUtxo(params, options) {
8502
8528
  throw new Error(`Swap transaction failed: ${lastError.message}`);
8503
8529
  }
8504
8530
  if (relayUrl && outputCommitments.length >= 2) {
8505
- const reconcileSeedIndices = commitmentIndices[0] >= 0 && commitmentIndices[1] >= 0 ? commitmentIndices : lastObservedNextIndexForOutputs !== null ? [lastObservedNextIndexForOutputs, lastObservedNextIndexForOutputs + 1] : commitmentIndices;
8506
- const reconciledIndices = await reconcileCommitmentIndicesWithRelay(
8507
- relayUrl,
8508
- swapPoolMint,
8509
- reconcileSeedIndices,
8510
- [outputCommitments[0], outputCommitments[1]],
8511
- onProgress
8512
- );
8513
- if (reconciledIndices[0] !== commitmentIndices[0] || reconciledIndices[1] !== commitmentIndices[1]) {
8531
+ const hasOnChainIndices = commitmentIndices[0] >= 0 && commitmentIndices[1] >= 0;
8532
+ if (!hasOnChainIndices) {
8533
+ const reconcileSeedIndices = lastObservedNextIndexForOutputs !== null ? [lastObservedNextIndexForOutputs, lastObservedNextIndexForOutputs + 1] : commitmentIndices;
8534
+ const reconciledIndices = await reconcileCommitmentIndicesWithRelay(
8535
+ relayUrl,
8536
+ swapPoolMint,
8537
+ reconcileSeedIndices,
8538
+ [outputCommitments[0], outputCommitments[1]],
8539
+ onProgress
8540
+ );
8541
+ if (reconciledIndices[0] !== commitmentIndices[0] || reconciledIndices[1] !== commitmentIndices[1]) {
8542
+ onProgress?.(
8543
+ `Adjusted swap commitment indices after relay reconciliation: [${commitmentIndices[0]}, ${commitmentIndices[1]}] -> [${reconciledIndices[0]}, ${reconciledIndices[1]}]`
8544
+ );
8545
+ commitmentIndices = reconciledIndices;
8546
+ }
8547
+ } else {
8514
8548
  onProgress?.(
8515
- `Adjusted swap commitment indices after relay reconciliation: [${commitmentIndices[0]}, ${commitmentIndices[1]}] -> [${reconciledIndices[0]}, ${reconciledIndices[1]}]`
8549
+ `Using on-chain swap commitment indices [${commitmentIndices[0]}, ${commitmentIndices[1]}] (skipping relay reconciliation).`
8516
8550
  );
8517
- commitmentIndices = reconciledIndices;
8518
8551
  }
8519
8552
  }
8520
8553
  const siblingCommitments = [
@@ -8580,6 +8613,7 @@ async function swapWithChange(inputUtxos, swapAmount, outputMint, recipientAta,
8580
8613
  var import_bs582 = __toESM(require("bs58"), 1);
8581
8614
  var import_web38 = require("@solana/web3.js");
8582
8615
  var import_spl_token2 = require("@solana/spl-token");
8616
+ var bs582 = import_bs582.default.default || import_bs582.default;
8583
8617
  var TRANSACT_TAG2 = 0;
8584
8618
  var TRANSACT_SWAP_TAG2 = 1;
8585
8619
  var PROOF_LEN2 = 256;
@@ -8763,7 +8797,7 @@ function normalizeIxData(rawData) {
8763
8797
  if (rawData instanceof Uint8Array) return rawData;
8764
8798
  if (typeof rawData === "string") {
8765
8799
  try {
8766
- const decoded = import_bs582.default.decode(rawData);
8800
+ const decoded = bs582.decode(rawData);
8767
8801
  return new Uint8Array(decoded);
8768
8802
  } catch {
8769
8803
  if (typeof Buffer !== "undefined") {
package/dist/index.js CHANGED
@@ -4517,7 +4517,8 @@ async function buildMerkleTree(commitments, height = MERKLE_TREE_HEIGHT2) {
4517
4517
  }
4518
4518
 
4519
4519
  // src/utils/relay-client.ts
4520
- import bs58 from "bs58";
4520
+ import _bs58 from "bs58";
4521
+ var bs58 = _bs58.default || _bs58;
4521
4522
  var TRANSACT_TAG = 0;
4522
4523
  var TRANSACT_SWAP_TAG = 1;
4523
4524
  var DEPOSIT_TAG = 1;
@@ -6368,7 +6369,7 @@ async function transact(params, options) {
6368
6369
  }
6369
6370
  merkleTree = null;
6370
6371
  }
6371
- if (needsTreeForProof && relayUrl && !relayTreeDisabledForMint && !forceChainIndexing && (forceRefresh || !merkleTree)) {
6372
+ if (needsTreeForProof && relayUrl && !relayTreeDisabledForMint && !forceChainIndexing && (forceRefresh || !merkleTree) && !hasCachedTree) {
6372
6373
  try {
6373
6374
  const shouldRequestRelaySync = forceSync || requiredRelayIndex >= 0;
6374
6375
  const syncTimeoutMs = shouldRequestRelaySync ? Math.min(3e4, relayRequestTimeoutMs()) : 3500;
@@ -6393,7 +6394,7 @@ async function transact(params, options) {
6393
6394
  merkleTree = null;
6394
6395
  }
6395
6396
  }
6396
- if (merkleTree && treeState) {
6397
+ if (merkleTree && treeState && !hasCachedTree) {
6397
6398
  const relayRoot = merkleTree.root().toString(16).padStart(64, "0");
6398
6399
  const lengthDiff = treeState.nextIndex - merkleTree.length;
6399
6400
  if (merkleTree.length === 0) {
@@ -6524,7 +6525,7 @@ async function transact(params, options) {
6524
6525
  const disableNearTipFallbackEnv = typeof process !== "undefined" && !!process.env && (process.env.CLOAK_DISABLE_NEAR_TIP_ONCHAIN_PROOF === "1" || process.env.CLOAK_DISABLE_NEAR_TIP_ONCHAIN_PROOF === "true");
6525
6526
  const hasSiblingHints = !!utxo.siblingCommitment || !!utxo.leftSiblingCommitment;
6526
6527
  const baseNearTipEligible = chainDistanceFromTip !== null && chainDistanceFromTip >= 0 && chainDistanceFromTip <= nearTipMaxDistance;
6527
- const allowNearTipOnChainFallback = relayTreeDisabledForMint && !disableNearTipFallbackEnv && baseNearTipEligible && (forceEnableNearTipFallbackEnv || useSiblingInfo && hasSiblingHints);
6528
+ let allowNearTipOnChainFallback = relayTreeDisabledForMint && !disableNearTipFallbackEnv && baseNearTipEligible && (forceEnableNearTipFallbackEnv || useSiblingInfo && hasSiblingHints);
6528
6529
  if (!relayTreeDisabledForMint && !useSiblingInfo) {
6529
6530
  onProgress?.(`Relay tree missing index ${utxo.index} (length: ${treeLength}) with sibling cache disabled; proof may require retry after relay sync.`);
6530
6531
  } else if (!relayTreeDisabledForMint && relayUrl && !forceChainIndexing && utxo.index >= treeLength && !allowNearTipOnChainFallback) {
@@ -6535,9 +6536,10 @@ async function transact(params, options) {
6535
6536
  onProgress?.(`Relay tree is lagging (missing index ${utxo.index}, length: ${treeLength}); using guarded on-chain proof fallback.`);
6536
6537
  }
6537
6538
  if (relayUrl && !relayTreeDisabledForMint && !forceChainIndexing && utxo.index >= treeLength && !allowNearTipOnChainFallback) {
6538
- throw new Error(
6539
- `Relay tree missing required commitment index ${utxo.index} (tree length: ${treeLength}); waiting for relay sync is required.`
6539
+ onProgress?.(
6540
+ `Relay tree missing index ${utxo.index} (tree length: ${treeLength}) \u2014 falling through to on-chain proof.`
6540
6541
  );
6542
+ allowNearTipOnChainFallback = true;
6541
6543
  }
6542
6544
  if (allowNearTipOnChainFallback && relayTreeDisabledForMint) {
6543
6545
  onProgress?.(
@@ -6771,11 +6773,19 @@ async function transact(params, options) {
6771
6773
  onProgress?.(`Merkle proof error (stale sibling data), will regenerate with fresh tree...`);
6772
6774
  lastError = new Error(`Circuit assertion failed: ${errorMsg}`);
6773
6775
  if (forceChainIndexing) {
6774
- onProgress?.("On-chain fallback proof failed; returning to relay-sync mode.");
6775
- forceChainIndexing = false;
6776
+ onProgress?.("On-chain frontier proof failed; escalating to full chain tree rebuild...");
6777
+ try {
6778
+ merkleTree = await buildMerkleTreeFromChain(connection, programId, pdas.merkleTree, onProgress);
6779
+ onProgress?.(`Chain tree rebuilt with ${merkleTree.length} leaves.`);
6780
+ } catch (chainBuildErr) {
6781
+ onProgress?.(`Chain tree rebuild failed (${chainBuildErr?.message?.slice(0, 80)}); returning to relay-sync mode.`);
6782
+ forceChainIndexing = false;
6783
+ merkleTree = null;
6784
+ }
6785
+ } else {
6786
+ merkleTree = null;
6776
6787
  }
6777
6788
  treeState = null;
6778
- merkleTree = null;
6779
6789
  useSiblingInfo = false;
6780
6790
  const waitMs = Math.min(retryDelayMs * Math.pow(2, Math.min(submissionAttempts, 4)), 5e3);
6781
6791
  if (waitMs > 0) {
@@ -7122,19 +7132,26 @@ async function transact(params, options) {
7122
7132
  throw new Error(`Transaction failed: ${lastError.message}`);
7123
7133
  }
7124
7134
  if (relayUrl && outputCommitments.length >= 2) {
7125
- const reconcileSeedIndices = commitmentIndices[0] >= 0 && commitmentIndices[1] >= 0 ? commitmentIndices : lastObservedNextIndexForOutputs !== null ? [lastObservedNextIndexForOutputs, lastObservedNextIndexForOutputs + 1] : commitmentIndices;
7126
- const reconciledIndices = await reconcileCommitmentIndicesWithRelay(
7127
- relayUrl,
7128
- mint,
7129
- reconcileSeedIndices,
7130
- [outputCommitments[0], outputCommitments[1]],
7131
- onProgress
7132
- );
7133
- if (reconciledIndices[0] !== commitmentIndices[0] || reconciledIndices[1] !== commitmentIndices[1]) {
7135
+ const hasOnChainIndices = commitmentIndices[0] >= 0 && commitmentIndices[1] >= 0;
7136
+ if (!hasOnChainIndices) {
7137
+ const reconcileSeedIndices = lastObservedNextIndexForOutputs !== null ? [lastObservedNextIndexForOutputs, lastObservedNextIndexForOutputs + 1] : commitmentIndices;
7138
+ const reconciledIndices = await reconcileCommitmentIndicesWithRelay(
7139
+ relayUrl,
7140
+ mint,
7141
+ reconcileSeedIndices,
7142
+ [outputCommitments[0], outputCommitments[1]],
7143
+ onProgress
7144
+ );
7145
+ if (reconciledIndices[0] !== commitmentIndices[0] || reconciledIndices[1] !== commitmentIndices[1]) {
7146
+ onProgress?.(
7147
+ `Adjusted commitment indices after relay reconciliation: [${commitmentIndices[0]}, ${commitmentIndices[1]}] -> [${reconciledIndices[0]}, ${reconciledIndices[1]}]`
7148
+ );
7149
+ commitmentIndices = reconciledIndices;
7150
+ }
7151
+ } else {
7134
7152
  onProgress?.(
7135
- `Adjusted commitment indices after relay reconciliation: [${commitmentIndices[0]}, ${commitmentIndices[1]}] -> [${reconciledIndices[0]}, ${reconciledIndices[1]}]`
7153
+ `Using on-chain commitment indices [${commitmentIndices[0]}, ${commitmentIndices[1]}] (skipping relay reconciliation).`
7136
7154
  );
7137
- commitmentIndices = reconciledIndices;
7138
7155
  }
7139
7156
  }
7140
7157
  const siblingCommitments = [
@@ -7487,7 +7504,7 @@ async function swapUtxo(params, options) {
7487
7504
  }
7488
7505
  merkleTree = null;
7489
7506
  }
7490
- if (needsTreeForProof && relayUrl && !relayTreeDisabledForMint && !forceChainIndexing && (forceRefresh || !merkleTree)) {
7507
+ if (needsTreeForProof && relayUrl && !relayTreeDisabledForMint && !forceChainIndexing && (forceRefresh || !merkleTree) && !hasCachedTree) {
7491
7508
  try {
7492
7509
  const shouldRequestRelaySync = forceSync || requiredRelayIndex >= 0;
7493
7510
  const syncTimeoutMs = shouldRequestRelaySync ? Math.min(3e4, relayRequestTimeoutMs()) : 3500;
@@ -7512,7 +7529,7 @@ async function swapUtxo(params, options) {
7512
7529
  merkleTree = null;
7513
7530
  }
7514
7531
  }
7515
- if (merkleTree && merkleState) {
7532
+ if (merkleTree && merkleState && !hasCachedTree) {
7516
7533
  const relayRoot = merkleTree.root().toString(16).padStart(64, "0");
7517
7534
  const lengthDiff = merkleState.nextIndex - merkleTree.length;
7518
7535
  if (merkleTree.length === 0) {
@@ -7640,7 +7657,7 @@ async function swapUtxo(params, options) {
7640
7657
  const disableNearTipFallbackEnv = typeof process !== "undefined" && !!process.env && (process.env.CLOAK_DISABLE_NEAR_TIP_ONCHAIN_PROOF === "1" || process.env.CLOAK_DISABLE_NEAR_TIP_ONCHAIN_PROOF === "true");
7641
7658
  const hasSiblingHints = !!utxo.siblingCommitment || !!utxo.leftSiblingCommitment;
7642
7659
  const baseNearTipEligible = chainDistanceFromTip !== null && chainDistanceFromTip >= 0 && chainDistanceFromTip <= nearTipMaxDistance;
7643
- const allowNearTipOnChainFallback = relayTreeDisabledForMint && !disableNearTipFallbackEnv && baseNearTipEligible && (forceEnableNearTipFallbackEnv || useSiblingInfo && hasSiblingHints);
7660
+ let allowNearTipOnChainFallback = relayTreeDisabledForMint && !disableNearTipFallbackEnv && baseNearTipEligible && (forceEnableNearTipFallbackEnv || useSiblingInfo && hasSiblingHints);
7644
7661
  if (!relayTreeDisabledForMint && !useSiblingInfo) {
7645
7662
  onProgress?.(`Relay tree missing index ${utxo.index} (length: ${treeLength}) with sibling cache disabled; proof may require retry after relay sync.`);
7646
7663
  } else if (!relayTreeDisabledForMint && relayUrl && !forceChainIndexing && utxo.index >= treeLength && !allowNearTipOnChainFallback) {
@@ -7651,9 +7668,10 @@ async function swapUtxo(params, options) {
7651
7668
  onProgress?.(`Relay tree is lagging (missing index ${utxo.index}, length: ${treeLength}); using guarded on-chain proof fallback.`);
7652
7669
  }
7653
7670
  if (relayUrl && !relayTreeDisabledForMint && !forceChainIndexing && utxo.index >= treeLength && !allowNearTipOnChainFallback) {
7654
- throw new Error(
7655
- `Relay tree missing required commitment index ${utxo.index} (tree length: ${treeLength}); waiting for relay sync is required.`
7671
+ onProgress?.(
7672
+ `Relay tree missing index ${utxo.index} (tree length: ${treeLength}) \u2014 falling through to on-chain proof.`
7656
7673
  );
7674
+ allowNearTipOnChainFallback = true;
7657
7675
  }
7658
7676
  if (allowNearTipOnChainFallback && relayTreeDisabledForMint) {
7659
7677
  onProgress?.(
@@ -7857,10 +7875,18 @@ async function swapUtxo(params, options) {
7857
7875
  onProgress?.(`Merkle proof error (stale sibling data), will regenerate with fresh tree...`);
7858
7876
  lastError = new Error(`Circuit assertion failed: ${errorMsg}`);
7859
7877
  if (forceChainIndexing) {
7860
- onProgress?.("On-chain fallback proof failed; returning to relay-sync mode.");
7861
- forceChainIndexing = false;
7878
+ onProgress?.("On-chain frontier proof failed; escalating to full chain tree rebuild...");
7879
+ try {
7880
+ merkleTree = await buildMerkleTreeFromChain(connection, programId, pdas.merkleTree, onProgress);
7881
+ onProgress?.(`Chain tree rebuilt with ${merkleTree.length} leaves.`);
7882
+ } catch (chainBuildErr) {
7883
+ onProgress?.(`Chain tree rebuild failed (${chainBuildErr?.message?.slice(0, 80)}); returning to relay-sync mode.`);
7884
+ forceChainIndexing = false;
7885
+ merkleTree = null;
7886
+ }
7887
+ } else {
7888
+ merkleTree = null;
7862
7889
  }
7863
- merkleTree = null;
7864
7890
  merkleState = null;
7865
7891
  useSiblingInfo = false;
7866
7892
  const waitMs = Math.min(retryDelayMs * Math.pow(2, Math.min(attempt, 4)), 5e3);
@@ -8055,19 +8081,26 @@ async function swapUtxo(params, options) {
8055
8081
  throw new Error(`Swap transaction failed: ${lastError.message}`);
8056
8082
  }
8057
8083
  if (relayUrl && outputCommitments.length >= 2) {
8058
- const reconcileSeedIndices = commitmentIndices[0] >= 0 && commitmentIndices[1] >= 0 ? commitmentIndices : lastObservedNextIndexForOutputs !== null ? [lastObservedNextIndexForOutputs, lastObservedNextIndexForOutputs + 1] : commitmentIndices;
8059
- const reconciledIndices = await reconcileCommitmentIndicesWithRelay(
8060
- relayUrl,
8061
- swapPoolMint,
8062
- reconcileSeedIndices,
8063
- [outputCommitments[0], outputCommitments[1]],
8064
- onProgress
8065
- );
8066
- if (reconciledIndices[0] !== commitmentIndices[0] || reconciledIndices[1] !== commitmentIndices[1]) {
8084
+ const hasOnChainIndices = commitmentIndices[0] >= 0 && commitmentIndices[1] >= 0;
8085
+ if (!hasOnChainIndices) {
8086
+ const reconcileSeedIndices = lastObservedNextIndexForOutputs !== null ? [lastObservedNextIndexForOutputs, lastObservedNextIndexForOutputs + 1] : commitmentIndices;
8087
+ const reconciledIndices = await reconcileCommitmentIndicesWithRelay(
8088
+ relayUrl,
8089
+ swapPoolMint,
8090
+ reconcileSeedIndices,
8091
+ [outputCommitments[0], outputCommitments[1]],
8092
+ onProgress
8093
+ );
8094
+ if (reconciledIndices[0] !== commitmentIndices[0] || reconciledIndices[1] !== commitmentIndices[1]) {
8095
+ onProgress?.(
8096
+ `Adjusted swap commitment indices after relay reconciliation: [${commitmentIndices[0]}, ${commitmentIndices[1]}] -> [${reconciledIndices[0]}, ${reconciledIndices[1]}]`
8097
+ );
8098
+ commitmentIndices = reconciledIndices;
8099
+ }
8100
+ } else {
8067
8101
  onProgress?.(
8068
- `Adjusted swap commitment indices after relay reconciliation: [${commitmentIndices[0]}, ${commitmentIndices[1]}] -> [${reconciledIndices[0]}, ${reconciledIndices[1]}]`
8102
+ `Using on-chain swap commitment indices [${commitmentIndices[0]}, ${commitmentIndices[1]}] (skipping relay reconciliation).`
8069
8103
  );
8070
- commitmentIndices = reconciledIndices;
8071
8104
  }
8072
8105
  }
8073
8106
  const siblingCommitments = [
@@ -8130,11 +8163,12 @@ async function swapWithChange(inputUtxos, swapAmount, outputMint, recipientAta,
8130
8163
  }
8131
8164
 
8132
8165
  // src/core/scanner.ts
8133
- import bs582 from "bs58";
8166
+ import _bs582 from "bs58";
8134
8167
  import {
8135
8168
  PublicKey as PublicKey7
8136
8169
  } from "@solana/web3.js";
8137
8170
  import { getAssociatedTokenAddressSync as getAssociatedTokenAddressSync2 } from "@solana/spl-token";
8171
+ var bs582 = _bs582.default || _bs582;
8138
8172
  var TRANSACT_TAG2 = 0;
8139
8173
  var TRANSACT_SWAP_TAG2 = 1;
8140
8174
  var PROOF_LEN2 = 256;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@cloak.dev/sdk",
3
3
  "description": "Shield, send, and swap on Solana privately — TypeScript SDK with UTXO-based zero-knowledge transactions",
4
- "version": "0.1.3",
4
+ "version": "0.1.4",
5
5
  "type": "module",
6
6
  "main": "dist/index.cjs",
7
7
  "module": "dist/index.js",
@@ -47,7 +47,7 @@
47
47
  "web3",
48
48
  "blockchain"
49
49
  ],
50
- "author": "Cloak Labs",
50
+ "author": "Cloak",
51
51
  "license": "Apache-2.0",
52
52
  "repository": {
53
53
  "type": "git",
@@ -61,15 +61,15 @@
61
61
  "@jup-ag/api": "^6.0.48",
62
62
  "@lightprotocol/hasher.rs": "^0.2.1",
63
63
  "@noble/hashes": "^1.8.0",
64
- "@solana/spl-token": "^0.4.0",
65
- "@solana/web3.js": "^1.0.0",
64
+ "@solana/spl-token": "0.4.14",
65
+ "@solana/web3.js": "^1.98.0",
66
66
  "bs58": "^6.0.0",
67
- "circomlibjs": "^0.1.7",
67
+ "circomlibjs": "0.1.7",
68
68
  "ffjavascript": "^0.3.0",
69
- "snarkjs": "^0.7.4",
69
+ "snarkjs": "0.7.6",
70
70
  "tweetnacl": "^1.0.3",
71
71
  "tweetnacl-util": "^0.15.1",
72
- "yaml": "^2.8.2"
72
+ "yaml": "^2.8.3"
73
73
  },
74
74
  "devDependencies": {
75
75
  "@coral-xyz/anchor": "^0.32.1",
@@ -86,8 +86,8 @@
86
86
  "peerDependencies": {
87
87
  "@switchboard-xyz/common": "^5.6.1",
88
88
  "@switchboard-xyz/on-demand": "^3.8.2",
89
- "@solana/spl-token": "^0.4.0",
90
- "@solana/web3.js": "^1.0.0"
89
+ "@solana/spl-token": ">=0.4.9",
90
+ "@solana/web3.js": ">=1.98.0"
91
91
  },
92
92
  "peerDependenciesMeta": {
93
93
  "@switchboard-xyz/common": {