veilix-sdk 0.3.0 → 0.3.1

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.
@@ -8291,7 +8291,7 @@ function getDepositQueueForNetwork(network) {
8291
8291
  // package.json
8292
8292
  var package_default = {
8293
8293
  name: "veilix-sdk",
8294
- version: "0.3.0"};
8294
+ version: "0.3.1"};
8295
8295
 
8296
8296
  // src/circuits/constants.ts
8297
8297
  var PACKAGE_NAME = package_default.name;
@@ -8466,10 +8466,19 @@ async function createRuntimeConfig(options) {
8466
8466
  };
8467
8467
  }
8468
8468
  function pushRuntimeContext(config) {
8469
+ const active = runtimeContextStack.find((other) => other.programId !== config.programId);
8470
+ if (active) {
8471
+ throw new Error(
8472
+ `veilix-sdk: another SDK instance is running on program ${active.programId}; instances on different networks must run one at a time`
8473
+ );
8474
+ }
8469
8475
  runtimeContextStack.push(config);
8470
8476
  }
8471
- function popRuntimeContext() {
8472
- runtimeContextStack.pop();
8477
+ function popRuntimeContext(config) {
8478
+ const index = runtimeContextStack.lastIndexOf(config);
8479
+ if (index !== -1) {
8480
+ runtimeContextStack.splice(index, 1);
8481
+ }
8473
8482
  }
8474
8483
  function getConfig() {
8475
8484
  const active = runtimeContextStack.at(-1);
@@ -17660,9 +17669,11 @@ async function buildDepositTx({
17660
17669
  onChainBackup = false,
17661
17670
  computeUnitPrice = 5e3,
17662
17671
  computeUnitLimit = DEPOSIT_COMPUTE_UNIT_LIMIT,
17663
- circuitArtifacts
17672
+ circuitArtifacts,
17673
+ encryptKey
17664
17674
  }) {
17665
17675
  const { connection } = getConfig();
17676
+ const sealKey = onChainBackup ? encryptKey ?? requireEncryptKey() : null;
17666
17677
  const sender = toPublicKey(senderPublicKey);
17667
17678
  const utxo = decodeNote(note);
17668
17679
  const mint = utxo.mint;
@@ -17675,7 +17686,7 @@ async function buildDepositTx({
17675
17686
  `veilix-sdk: deposit amount ${utxo.amount} exceeds the tree limit of ${tree.maxDepositAmount}`
17676
17687
  );
17677
17688
  }
17678
- const encryptedOutput1 = onChainBackup ? encryptNoteOutput(requireEncryptKey(), utxo) : new Uint8Array();
17689
+ const encryptedOutput1 = sealKey ? encryptNoteOutput(sealKey, utxo) : new Uint8Array();
17679
17690
  const encryptedOutput2 = new Uint8Array();
17680
17691
  const proved = await withCircuitArtifacts(
17681
17692
  circuitArtifacts,
@@ -17730,16 +17741,15 @@ async function buildInitNotesInstructionIfNeeded(sender) {
17730
17741
  if (await fetchNotesAccount(sender)) return null;
17731
17742
  return program.methods.initNotes().accountsPartial({ user: sender }).instruction();
17732
17743
  }
17733
- async function resolveEncryptKey() {
17734
- const config = getConfig();
17744
+ async function resolveEncryptKey(config) {
17735
17745
  if (!config.encryptKey) {
17736
17746
  config.encryptKey = await getEncryptionKey(config.wallet);
17737
17747
  }
17738
17748
  return config.encryptKey;
17739
17749
  }
17740
- async function buildBackupNotesInstruction(sender, notes) {
17741
- const { program } = getConfig();
17742
- const key2 = await resolveEncryptKey();
17750
+ async function buildBackupNotesInstruction(sender, notes, config = getConfig()) {
17751
+ const { program } = config;
17752
+ const key2 = await resolveEncryptKey(config);
17743
17753
  return program.methods.backupNotes(notes.map((note) => encryptData(key2, note))).accountsPartial({ user: sender }).instruction();
17744
17754
  }
17745
17755
  async function isExtendBackupPdaNeeded(walletPubkey, newNotesLength) {
@@ -17752,7 +17762,8 @@ function buildRequestHeapIx() {
17752
17762
  return web3_js.ComputeBudgetProgram.requestHeapFrame({ bytes: 32 * 1024 });
17753
17763
  }
17754
17764
  async function buildBackupNotesTx(senderPublicKey, notes) {
17755
- const { connection } = getConfig();
17765
+ const config = getConfig();
17766
+ const { connection } = config;
17756
17767
  const sender = toPublicKey(senderPublicKey);
17757
17768
  const tx = new web3_js.Transaction();
17758
17769
  const initIx = await buildInitNotesInstructionIfNeeded(sender);
@@ -17761,7 +17772,7 @@ async function buildBackupNotesTx(senderPublicKey, notes) {
17761
17772
  } else if (await isExtendBackupPdaNeeded(sender, notes.length)) {
17762
17773
  tx.add(buildRequestHeapIx());
17763
17774
  }
17764
- tx.add(await buildBackupNotesInstruction(sender, notes));
17775
+ tx.add(await buildBackupNotesInstruction(sender, notes, config));
17765
17776
  tx.recentBlockhash = (await connection.getLatestBlockhash()).blockhash;
17766
17777
  tx.feePayer = sender;
17767
17778
  return tx;
@@ -18893,6 +18904,8 @@ var SEND_OPTIONS = { skipPreflight: false, maxRetries: 3 };
18893
18904
  var SUBMIT_MAX_PROOFS = 8;
18894
18905
  var NOTE_INDEX_TIMEOUT_MS = 12e4;
18895
18906
  var NOTE_INDEX_POLL_MS = 4e3;
18907
+ var SEND_ATTEMPTS = 4;
18908
+ var SEND_RETRY_MS = 2e3;
18896
18909
  function sleep(ms) {
18897
18910
  return new Promise((resolve) => setTimeout(resolve, ms));
18898
18911
  }
@@ -18914,7 +18927,18 @@ async function signTransactions(wallet, transactions) {
18914
18927
  }
18915
18928
  async function sendAndConfirm(signed) {
18916
18929
  const { connection } = getConfig();
18917
- const signature = await connection.sendRawTransaction(signed.serialize(), SEND_OPTIONS);
18930
+ const raw = signed.serialize();
18931
+ let signature;
18932
+ for (let attempt = 1; signature === void 0; attempt++) {
18933
+ try {
18934
+ signature = await connection.sendRawTransaction(raw, SEND_OPTIONS);
18935
+ } catch (error) {
18936
+ if (attempt >= SEND_ATTEMPTS || !/blockhash not found/i.test(errorMessage(error))) {
18937
+ throw error;
18938
+ }
18939
+ await sleep(SEND_RETRY_MS);
18940
+ }
18941
+ }
18918
18942
  const { value } = await connection.confirmTransaction(signature, "confirmed");
18919
18943
  if (value.err) {
18920
18944
  throw new Error(`Deposit transaction failed: ${JSON.stringify(value.err)}`);
@@ -18960,6 +18984,7 @@ async function executeVeilixTransfer(items, options = {}) {
18960
18984
  const { wallet, network, relayers, encryptKey } = getConfig();
18961
18985
  const sender = wallet.publicKey;
18962
18986
  const onChainBackup = options.onChainBackup ?? !!encryptKey;
18987
+ const sealKey = onChainBackup ? encryptKey ?? requireEncryptKey() : void 0;
18963
18988
  const prepared = prepareDeposits(items);
18964
18989
  result.notes = prepared.map((deposit) => deposit.note);
18965
18990
  const circuitArtifacts = await prepareWithdrawCircuitArtifacts();
@@ -18970,7 +18995,8 @@ async function executeVeilixTransfer(items, options = {}) {
18970
18995
  senderPublicKey: sender,
18971
18996
  note: deposit.note,
18972
18997
  onChainBackup,
18973
- circuitArtifacts
18998
+ circuitArtifacts,
18999
+ ...sealKey ? { encryptKey: sealKey } : {}
18974
19000
  });
18975
19001
  } catch (error) {
18976
19002
  result.depositFailed.push(toDepositFailure(deposit, error));
@@ -18981,6 +19007,10 @@ async function executeVeilixTransfer(items, options = {}) {
18981
19007
  result.error = true;
18982
19008
  return result;
18983
19009
  }
19010
+ const { blockhash } = await getConfig().connection.getLatestBlockhash();
19011
+ toSend.forEach((deposit) => {
19012
+ deposit.tx.message.recentBlockhash = blockhash;
19013
+ });
18984
19014
  const signed = await signTransactions(wallet, toSend.map((deposit) => deposit.tx));
18985
19015
  if (options.downloadNotes ?? true) {
18986
19016
  downloadNotesCsv(toSend.map((deposit) => deposit.note));
@@ -19084,7 +19114,7 @@ var VeilixSDK = class _VeilixSDK {
19084
19114
  try {
19085
19115
  return fn();
19086
19116
  } finally {
19087
- popRuntimeContext();
19117
+ popRuntimeContext(this.config);
19088
19118
  }
19089
19119
  }
19090
19120
  async runAsync(fn) {
@@ -19092,7 +19122,7 @@ var VeilixSDK = class _VeilixSDK {
19092
19122
  try {
19093
19123
  return await fn();
19094
19124
  } finally {
19095
- popRuntimeContext();
19125
+ popRuntimeContext(this.config);
19096
19126
  }
19097
19127
  }
19098
19128
  // ── Config ───────────────────────────────────────────────────────────