@provablehq/sdk 0.10.4 → 0.10.5

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.
@@ -1,5 +1,5 @@
1
1
  import 'core-js/proposals/json-parse-with-source.js';
2
- import { ViewKey, ComputeKey, Address, PrivateKeyCiphertext, PrivateKey, RecordCiphertext, EncryptionToolkit, Group, Metadata, VerifyingKey, Program, Plaintext, Transaction, ProvingRequest, ProvingKey, RecordPlaintext, Field, Poseidon4, ProgramManager as ProgramManager$1, ExecutionRequest, stringToField, verifyFunctionExecution, Value, Proof, Signature } from '@provablehq/wasm/testnet.js';
2
+ import { ViewKey, ComputeKey, Address, PrivateKeyCiphertext, PrivateKey, RecordCiphertext, EncryptionToolkit, Group, Metadata, VerifyingKey, Program, Plaintext, Transaction, ProvingRequest, ProvingKey, RecordPlaintext, Field, Poseidon4, ProgramManager as ProgramManager$1, CallbackQuery, QueryOption, ExecutionRequest, stringToField, verifyFunctionExecution, Value, Proof, Signature } from '@provablehq/wasm/testnet.js';
3
3
  export { Address, Authorization, BHP1024, BHP256, BHP512, BHP768, Boolean, Ciphertext, ComputeKey, DynamicRecord, EncryptionToolkit, ExecutionRequest, ExecutionResponse, Field, Execution as FunctionExecution, GraphKey, Group, I128, I16, I32, I64, I8, OfflineQuery, Pedersen128, Pedersen64, Plaintext, Poseidon2, Poseidon4, Poseidon8, PrivateKey, PrivateKeyCiphertext, Program, ProgramManager as ProgramManagerBase, Proof, ProvingKey, ProvingRequest, RecordCiphertext, RecordPlaintext, Scalar, Signature, Transaction, Transition, U128, U16, U32, U64, U8, Value, VerifyingKey, ViewKey, getOrInitConsensusVersionTestHeights, initThreadPool, snarkVerify, snarkVerifyBatch, stringToField, verifyFunctionExecution } from '@provablehq/wasm/testnet.js';
4
4
  import { cryptoBoxSeal } from '@serenity-kit/noble-sodium';
5
5
  import { base64, bech32m } from '@scure/base';
@@ -841,6 +841,7 @@ class AleoNetworkClient {
841
841
  verboseErrors;
842
842
  network;
843
843
  transport;
844
+ hasCustomTransport;
844
845
  apiKey;
845
846
  consumerId;
846
847
  jwtData;
@@ -855,6 +856,7 @@ class AleoNetworkClient {
855
856
  this.ctx = {};
856
857
  this.verboseErrors = true;
857
858
  this.transport = options?.transport ?? defaultTransport;
859
+ this.hasCustomTransport = !!options?.transport;
858
860
  if (options) {
859
861
  if (options.headers) {
860
862
  this.headers = options.headers;
@@ -862,7 +864,7 @@ class AleoNetworkClient {
862
864
  else {
863
865
  this.headers = {
864
866
  // This is replaced by the actual version by a Rollup plugin
865
- "X-Aleo-SDK-Version": "0.10.4",
867
+ "X-Aleo-SDK-Version": "0.10.5",
866
868
  "X-Aleo-environment": environment(),
867
869
  };
868
870
  }
@@ -878,7 +880,7 @@ class AleoNetworkClient {
878
880
  else {
879
881
  this.headers = {
880
882
  // This is replaced by the actual version by a Rollup plugin
881
- "X-Aleo-SDK-Version": "0.10.4",
883
+ "X-Aleo-SDK-Version": "0.10.5",
882
884
  "X-Aleo-environment": environment(),
883
885
  };
884
886
  }
@@ -1595,6 +1597,25 @@ class AleoNetworkClient {
1595
1597
  this.ctx = {};
1596
1598
  }
1597
1599
  }
1600
+ /**
1601
+ * Returns state paths for the given record commitments.
1602
+ *
1603
+ * @param {string[]} commitments - Array of commitment field strings.
1604
+ * @returns {Promise<string[]>} Array of state path strings corresponding to the commitments.
1605
+ */
1606
+ async getStatePaths(commitments) {
1607
+ try {
1608
+ this.ctx = { "X-ALEO-METHOD": "getStatePaths" };
1609
+ const csv = commitments.map(c => encodeURIComponent(c)).join(",");
1610
+ return await this.fetchData(`/statePaths?commitments=${csv}`);
1611
+ }
1612
+ catch (error) {
1613
+ throw new Error(`Error fetching state paths for commitments: ${error}`);
1614
+ }
1615
+ finally {
1616
+ this.ctx = {};
1617
+ }
1618
+ }
1598
1619
  /**
1599
1620
  * Returns the latest block hash.
1600
1621
  *
@@ -5187,6 +5208,40 @@ class ProgramManager {
5187
5208
  this.keyProvider = keyProvider ? keyProvider : new AleoKeyProvider({ transport: networkClientOptions?.transport });
5188
5209
  this.recordProvider = recordProvider;
5189
5210
  }
5211
+ /**
5212
+ * Pre-load the inclusion prover for offline execution. Required when the
5213
+ * user provides an explicit OfflineQuery (truly offline — can't fetch lazily).
5214
+ * For CallbackQuery, snarkVM handles inclusion key loading on demand.
5215
+ */
5216
+ async ensureInclusionKeys(isOffline) {
5217
+ if (isOffline && !this.inclusionKeysLoaded) {
5218
+ try {
5219
+ const inclusionKeys = await this.keyProvider.inclusionKeys();
5220
+ ProgramManager$1.loadInclusionProver(inclusionKeys[0]);
5221
+ this.inclusionKeysLoaded = true;
5222
+ }
5223
+ catch {
5224
+ logAndThrow(`Inclusion key bytes not loaded, please ensure the program manager is initialized with a KeyProvider that includes the inclusion key.`);
5225
+ }
5226
+ }
5227
+ }
5228
+ /**
5229
+ * Create a CallbackQuery that delegates all state fetching to the
5230
+ * transport-aware network client. WASM calls back into these functions
5231
+ * during trace.prepare_async() instead of making its own reqwest calls.
5232
+ *
5233
+ * This handles ALL programs including those with DynamicRecord inputs.
5234
+ */
5235
+ buildCallbackQuery() {
5236
+ const networkClient = this.networkClient;
5237
+ return new CallbackQuery(
5238
+ // getStateRoot: () => Promise<string>
5239
+ async () => await networkClient.getStateRoot(),
5240
+ // getStatePaths: (commitments: string[]) => Promise<string[]>
5241
+ async (commitments) => await networkClient.getStatePaths(commitments),
5242
+ // getBlockHeight: () => Promise<number>
5243
+ async () => await networkClient.getLatestHeight());
5244
+ }
5190
5245
  /**
5191
5246
  * Check if the fee is sufficient to pay for the transaction
5192
5247
  */
@@ -5731,21 +5786,19 @@ class ProgramManager {
5731
5786
  catch (e) {
5732
5787
  logAndThrow(`Error finding fee record. Record finder response: '${e.message}'. Please ensure you're connected to a valid Aleo network and a record with enough balance exists.`);
5733
5788
  }
5734
- if (offlineQuery && !this.inclusionKeysLoaded) {
5735
- try {
5736
- const inclusionKeys = await this.keyProvider.inclusionKeys();
5737
- ProgramManager$1.loadInclusionProver(inclusionKeys[0]);
5738
- this.inclusionKeysLoaded = true;
5739
- console.log("Successfully loaded inclusion key");
5740
- }
5741
- catch {
5742
- logAndThrow(`Inclusion key bytes not loaded, please ensure the program manager is initialized with a KeyProvider that includes the inclusion key.`);
5743
- }
5744
- }
5745
5789
  // Auto-convert bare string inputs to field elements where the function expects field type.
5746
5790
  const preparedInputs = this.prepareInputs(program, functionName, inputs);
5791
+ // Build the query: user-provided OfflineQuery for truly offline execution,
5792
+ // CallbackQuery when a custom transport is configured, or undefined to
5793
+ // let WASM use its internal SnapshotQuery with the default fetch.
5794
+ const query = offlineQuery
5795
+ ? QueryOption.offlineQuery(offlineQuery)
5796
+ : this.networkClient.hasCustomTransport
5797
+ ? QueryOption.callbackQuery(this.buildCallbackQuery())
5798
+ : undefined;
5799
+ await this.ensureInclusionKeys(!!offlineQuery);
5747
5800
  // Build an execution transaction
5748
- return await ProgramManager$1.buildExecutionTransaction(executionPrivateKey, program, functionName, preparedInputs, priorityFee, feeRecord, this.host, imports, provingKey, verifyingKey, feeProvingKey, feeVerifyingKey, offlineQuery, edition);
5801
+ return await ProgramManager$1.buildExecutionTransaction(executionPrivateKey, program, functionName, preparedInputs, priorityFee, feeRecord, this.host, imports, provingKey, verifyingKey, feeProvingKey, feeVerifyingKey, query, edition);
5749
5802
  }
5750
5803
  /**
5751
5804
  * Builds an execution transaction for submission to the Aleo network from an Authorization and Fee Authorization.
@@ -5871,22 +5924,15 @@ class ProgramManager {
5871
5924
  logAndThrow(`Error finding program imports. Network response: '${e.message}'. Please ensure you're connected to a valid Aleo network and the program is deployed to the network.`);
5872
5925
  }
5873
5926
  }
5874
- // If the offline query exists, add the inclusion key.
5875
- if (offlineQuery && !this.inclusionKeysLoaded) {
5876
- console.log("Loading inclusion keys for offline proving.");
5877
- try {
5878
- const inclusionKeys = await this.keyProvider.inclusionKeys();
5879
- ProgramManager$1.loadInclusionProver(inclusionKeys[0]);
5880
- this.inclusionKeysLoaded = true;
5881
- console.log("Successfully loaded inclusion key");
5882
- }
5883
- catch {
5884
- logAndThrow(`Inclusion key bytes not loaded, please ensure the program manager is initialized with a KeyProvider that includes the inclusion key.`);
5885
- }
5886
- }
5927
+ const query = offlineQuery
5928
+ ? QueryOption.offlineQuery(offlineQuery)
5929
+ : this.networkClient.hasCustomTransport
5930
+ ? QueryOption.callbackQuery(this.buildCallbackQuery())
5931
+ : undefined;
5932
+ await this.ensureInclusionKeys(!!offlineQuery);
5887
5933
  // Build an execution transaction from the authorization.
5888
5934
  console.log("Executing authorizations");
5889
- return await ProgramManager$1.executeAuthorization(authorization, feeAuthorization, program, provingKey, verifyingKey, feeProvingKey, feeVerifyingKey, imports, this.host, offlineQuery);
5935
+ return await ProgramManager$1.executeAuthorization(authorization, feeAuthorization, program, provingKey, verifyingKey, feeProvingKey, feeVerifyingKey, imports, this.host, query);
5890
5936
  }
5891
5937
  /**
5892
5938
  * Builds a SnarkVM `Authorization` for a specific function.
@@ -6373,11 +6419,17 @@ class ProgramManager {
6373
6419
  }
6374
6420
  // Auto-convert bare string inputs to field elements where the function expects field type.
6375
6421
  const preparedInputs = this.prepareInputs(program, function_name, inputs);
6422
+ const query = offlineQuery
6423
+ ? QueryOption.offlineQuery(offlineQuery)
6424
+ : this.networkClient.hasCustomTransport
6425
+ ? QueryOption.callbackQuery(this.buildCallbackQuery())
6426
+ : undefined;
6427
+ await this.ensureInclusionKeys(!!offlineQuery);
6376
6428
  // Run the program offline and return the result
6377
6429
  console.log("Running program offline");
6378
6430
  console.log("Proving key: ", provingKey);
6379
6431
  console.log("Verifying key: ", verifyingKey);
6380
- return ProgramManager$1.executeFunctionOffline(executionPrivateKey, program, function_name, preparedInputs, proveExecution, false, imports, provingKey, verifyingKey, this.host, offlineQuery, edition);
6432
+ return ProgramManager$1.executeFunctionOffline(executionPrivateKey, program, function_name, preparedInputs, proveExecution, false, imports, provingKey, verifyingKey, this.host, query, edition);
6381
6433
  }
6382
6434
  /**
6383
6435
  * Join two credits records into a single credits record
@@ -6476,20 +6528,14 @@ class ProgramManager {
6476
6528
  catch (e) {
6477
6529
  logAndThrow("Records provided are not valid. Please ensure they are valid plaintext records.");
6478
6530
  }
6479
- // Load the inclusion prover offline.
6480
- if (offlineQuery && !this.inclusionKeysLoaded) {
6481
- try {
6482
- const inclusionKeys = await this.keyProvider.inclusionKeys();
6483
- ProgramManager$1.loadInclusionProver(inclusionKeys[0]);
6484
- this.inclusionKeysLoaded = true;
6485
- console.log("Successfully loaded inclusion key");
6486
- }
6487
- catch {
6488
- logAndThrow(`Inclusion key bytes not loaded, please ensure the program manager is initialized with a KeyProvider that includes the inclusion key.`);
6489
- }
6490
- }
6531
+ const query = offlineQuery
6532
+ ? QueryOption.offlineQuery(offlineQuery)
6533
+ : this.networkClient.hasCustomTransport
6534
+ ? QueryOption.callbackQuery(this.buildCallbackQuery())
6535
+ : undefined;
6536
+ await this.ensureInclusionKeys(!!offlineQuery);
6491
6537
  // Build an execution transaction and submit it to the network
6492
- const tx = await ProgramManager$1.buildJoinTransaction(executionPrivateKey, recordOne, recordTwo, priorityFee, feeRecord, this.host, joinProvingKey, joinVerifyingKey, feeProvingKey, feeVerifyingKey, offlineQuery);
6538
+ const tx = await ProgramManager$1.buildJoinTransaction(executionPrivateKey, recordOne, recordTwo, priorityFee, feeRecord, this.host, joinProvingKey, joinVerifyingKey, feeProvingKey, feeVerifyingKey, query);
6493
6539
  // Check if the account has sufficient credits to pay for the transaction
6494
6540
  if (!privateFee) {
6495
6541
  await this.checkFee(feeAddress.to_string(), tx.feeAmount());
@@ -6554,20 +6600,14 @@ class ProgramManager {
6554
6600
  catch (e) {
6555
6601
  logAndThrow("Record provided is not valid. Please ensure it is a valid plaintext record.");
6556
6602
  }
6557
- // Load the inclusion prover offline.
6558
- if (offlineQuery && !this.inclusionKeysLoaded) {
6559
- try {
6560
- const inclusionKeys = await this.keyProvider.inclusionKeys();
6561
- ProgramManager$1.loadInclusionProver(inclusionKeys[0]);
6562
- this.inclusionKeysLoaded = true;
6563
- console.log("Successfully loaded inclusion key");
6564
- }
6565
- catch {
6566
- logAndThrow(`Inclusion key bytes not loaded, please ensure the program manager is initialized with a KeyProvider that includes the inclusion key.`);
6567
- }
6568
- }
6603
+ const query = offlineQuery
6604
+ ? QueryOption.offlineQuery(offlineQuery)
6605
+ : this.networkClient.hasCustomTransport
6606
+ ? QueryOption.callbackQuery(this.buildCallbackQuery())
6607
+ : undefined;
6608
+ await this.ensureInclusionKeys(!!offlineQuery);
6569
6609
  // Build an execution transaction and submit it to the network
6570
- const tx = await ProgramManager$1.buildSplitTransaction(executionPrivateKey, splitAmount, amountRecord, this.host, splitProvingKey, splitVerifyingKey, offlineQuery);
6610
+ const tx = await ProgramManager$1.buildSplitTransaction(executionPrivateKey, splitAmount, amountRecord, this.host, splitProvingKey, splitVerifyingKey, query);
6571
6611
  return await this.networkClient.submitTransaction(tx);
6572
6612
  }
6573
6613
  /**
@@ -6692,22 +6732,14 @@ class ProgramManager {
6692
6732
  catch (e) {
6693
6733
  logAndThrow(`Error finding fee record. Record finder response: '${e.message}'. Please ensure you're connected to a valid Aleo network and a record with enough balance exists.`);
6694
6734
  }
6695
- // Load the inclusion prover offline.
6696
- if (offlineQuery && !this.inclusionKeysLoaded) {
6697
- const inclusionKeys = await this.keyProvider.inclusionKeys();
6698
- ProgramManager$1.loadInclusionProver(inclusionKeys[0]);
6699
- try {
6700
- const inclusionKeys = await this.keyProvider.inclusionKeys();
6701
- ProgramManager$1.loadInclusionProver(inclusionKeys[0]);
6702
- this.inclusionKeysLoaded = true;
6703
- console.log("Successfully loaded inclusion key");
6704
- }
6705
- catch {
6706
- logAndThrow(`Inclusion key bytes not loaded, please ensure the program manager is initialized with a KeyProvider that includes the inclusion key.`);
6707
- }
6708
- }
6735
+ const query = offlineQuery
6736
+ ? QueryOption.offlineQuery(offlineQuery)
6737
+ : this.networkClient.hasCustomTransport
6738
+ ? QueryOption.callbackQuery(this.buildCallbackQuery())
6739
+ : undefined;
6740
+ await this.ensureInclusionKeys(!!offlineQuery);
6709
6741
  // Build an execution transaction
6710
- return await ProgramManager$1.buildTransferTransaction(executionPrivateKey, amount, recipient, transferType, amountRecord, priorityFee, feeRecord, this.host, transferProvingKey, transferVerifyingKey, feeProvingKey, feeVerifyingKey, offlineQuery);
6742
+ return await ProgramManager$1.buildTransferTransaction(executionPrivateKey, amount, recipient, transferType, amountRecord, priorityFee, feeRecord, this.host, transferProvingKey, transferVerifyingKey, feeProvingKey, feeVerifyingKey, query);
6711
6743
  }
6712
6744
  /**
6713
6745
  * Build a transfer_public transaction to transfer credits to another account for later submission to the Aleo network