@provablehq/sdk 0.9.10 → 0.9.11

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.
@@ -425,7 +425,12 @@ async function post(url, options) {
425
425
  options.method = "POST";
426
426
  const response = await fetch(url, options);
427
427
  if (!response.ok) {
428
- throw new Error(response.status + " could not post URL " + url);
428
+ const error = await response.text();
429
+ let message = `${response.status} error received from ${url}`;
430
+ if (error) {
431
+ message = `${error}`;
432
+ }
433
+ throw new Error(message);
429
434
  }
430
435
  return response;
431
436
  }
@@ -492,7 +497,7 @@ class AleoNetworkClient {
492
497
  else {
493
498
  this.headers = {
494
499
  // This is replaced by the actual version by a Rollup plugin
495
- "X-Aleo-SDK-Version": "0.9.10",
500
+ "X-Aleo-SDK-Version": "0.9.11",
496
501
  "X-Aleo-environment": environment(),
497
502
  };
498
503
  }
@@ -1807,7 +1812,7 @@ class AleoNetworkClient {
1807
1812
  }
1808
1813
  }
1809
1814
  catch (error) {
1810
- throw new Error(`Error posting transaction: No response received: ${error.message}`);
1815
+ throw new Error(`Error posting transaction: ${error}`);
1811
1816
  }
1812
1817
  }
1813
1818
  /**
@@ -4206,53 +4211,65 @@ class ProgramManager {
4206
4211
  * @returns {Promise<Transaction>} - A promise that resolves to the transaction or an error.
4207
4212
  *
4208
4213
  * @example
4209
- * /// Import the mainnet version of the sdk.
4210
- * import { AleoKeyProvider, ProgramManager, NetworkRecordProvider } from "@provablehq/sdk/mainnet.js";
4214
+ * import { AleoKeyProvider, PrivateKey, initThreadPool, ProgramManager } from "@provablehq/sdk";
4211
4215
  *
4212
- * // Create a new NetworkClient, KeyProvider, and RecordProvider.
4216
+ * await initThreadPool();
4217
+ *
4218
+ * // Create a new KeyProvider.
4213
4219
  * const keyProvider = new AleoKeyProvider();
4214
4220
  * keyProvider.useCache(true);
4215
4221
  *
4216
- * // Initialize a program manager with the key provider to automatically fetch keys for executions
4222
+ * // Initialize a program manager with the key provider to automatically fetch keys for executions.
4217
4223
  * const programManager = new ProgramManager("https://api.explorer.provable.com/v1", keyProvider);
4218
4224
  *
4219
4225
  * // Build the `Authorization`.
4226
+ * const privateKey = new PrivateKey(); // Change this to a private key that has an aleo credit balance.
4220
4227
  * const authorization = await programManager.buildAuthorization({
4221
- * programName: "credits.aleo",
4222
- * functionName: "transfer_public",
4223
- * inputs: [
4224
- * "aleo1vwls2ete8dk8uu2kmkmzumd7q38fvshrht8hlc0a5362uq8ftgyqnm3w08",
4225
- * "10000000u64",
4226
- * ],
4228
+ * programName: "credits.aleo",
4229
+ * functionName: "transfer_public",
4230
+ * privateKey,
4231
+ * inputs: [
4232
+ * "aleo1vwls2ete8dk8uu2kmkmzumd7q38fvshrht8hlc0a5362uq8ftgyqnm3w08",
4233
+ * "10000000u64",
4234
+ * ],
4227
4235
  * });
4228
4236
  *
4237
+ * console.log("Getting execution id");
4238
+ *
4229
4239
  * // Derive the execution ID and base fee.
4230
4240
  * const executionId = authorization.toExecutionId().toString();
4231
4241
  *
4242
+ * console.log("Estimating fee");
4243
+ *
4232
4244
  * // Get the base fee in microcredits.
4233
- * const baseFeeMicrocredits = ProgramManager.estimateFeeForAuthorization(authorization, "credits.aleo");
4234
- * const baseFeeCredits = baseFeeMicrocredits/1000000;
4245
+ * const baseFeeMicrocredits = await programManager.estimateFeeForAuthorization(authorization, "credits.aleo");
4246
+ * const baseFeeCredits = Number(baseFeeMicrocredits)/1000000;
4247
+ *
4248
+ * console.log("Building fee authorization");
4235
4249
  *
4236
4250
  * // Build a credits.aleo/fee_public `Authorization`.
4237
4251
  * const feeAuthorization = await programManager.buildFeeAuthorization({
4238
- * deploymentOrExecutionId: executionId,
4239
- * baseFeeCredits,
4252
+ * deploymentOrExecutionId: executionId,
4253
+ * baseFeeCredits,
4254
+ * privateKey
4240
4255
  * });
4241
4256
  *
4242
- * // Build and execute the transaction
4257
+ * console.log("Executing authorizations");
4258
+ *
4259
+ * // Build and execute the transaction.
4243
4260
  * const tx = await programManager.buildTransactionFromAuthorization({
4244
- * programName: "hello_hello.aleo",
4245
- * authorization,
4246
- * feeAuthorization,
4261
+ * programName: "credits.aleo",
4262
+ * authorization,
4263
+ * feeAuthorization,
4247
4264
  * });
4248
4265
  *
4249
- * // Submit the transaction to the network
4266
+ * // Submit the transaction to the network.
4250
4267
  * await programManager.networkClient.submitTransaction(tx.toString());
4251
4268
  *
4252
- * // Verify the transaction was successful
4269
+ * // Verify the transaction was successful.
4253
4270
  * setTimeout(async () => {
4254
- * const transaction = await programManager.networkClient.getTransaction(tx.id());
4255
- * assert(transaction.id() === tx.id());
4271
+ * const transaction = await programManager.networkClient.getTransaction(tx.id());
4272
+ * console.log(transaction);
4256
4273
  * }, 10000);
4257
4274
  */
4258
4275
  async buildTransactionFromAuthorization(options) {
@@ -4299,6 +4316,7 @@ class ProgramManager {
4299
4316
  }
4300
4317
  }
4301
4318
  // Resolve the program imports if they exist.
4319
+ console.log("Resolving program imports");
4302
4320
  const numberOfImports = Program.fromString(program).getImports().length;
4303
4321
  if (numberOfImports > 0 && !imports) {
4304
4322
  try {
@@ -4310,6 +4328,7 @@ class ProgramManager {
4310
4328
  }
4311
4329
  // If the offline query exists, add the inclusion key.
4312
4330
  if (offlineQuery && !this.inclusionKeysLoaded) {
4331
+ console.log("Loading inclusion keys for offline proving.");
4313
4332
  try {
4314
4333
  const inclusionKeys = await this.keyProvider.inclusionKeys();
4315
4334
  ProgramManager$1.loadInclusionProver(inclusionKeys[0]);
@@ -4321,6 +4340,7 @@ class ProgramManager {
4321
4340
  }
4322
4341
  }
4323
4342
  // Build an execution transaction from the authorization.
4343
+ console.log("Executing authorizations");
4324
4344
  return await ProgramManager$1.executeAuthorization(authorization, feeAuthorization, program, provingKey, verifyingKey, feeProvingKey, feeVerifyingKey, imports, this.host, offlineQuery);
4325
4345
  }
4326
4346
  /**
@@ -4511,7 +4531,6 @@ class ProgramManager {
4511
4531
  * const provingRequest = await programManager.provingRequest({
4512
4532
  * programName: "credits.aleo",
4513
4533
  * functionName: "transfer_public",
4514
- * baseFee: 100000,
4515
4534
  * priorityFee: 0,
4516
4535
  * privateFee: false,
4517
4536
  * inputs: [
@@ -4523,8 +4542,9 @@ class ProgramManager {
4523
4542
  */
4524
4543
  async provingRequest(options) {
4525
4544
  // Destructure the options object to access the parameters.
4526
- const { functionName, baseFee, priorityFee, privateFee, inputs, recordSearchParams, broadcast = false, unchecked = false, } = options;
4545
+ const { functionName, priorityFee, privateFee, inputs, recordSearchParams, broadcast = false, unchecked = false, } = options;
4527
4546
  const privateKey = options.privateKey;
4547
+ const baseFee = options.baseFee ? options.baseFee : 0;
4528
4548
  let program = options.programSource;
4529
4549
  let programName = options.programName;
4530
4550
  let feeRecord = options.feeRecord;
@@ -5719,15 +5739,6 @@ class ProgramManager {
5719
5739
  return false;
5720
5740
  }
5721
5741
  }
5722
- /**
5723
- * Set the inclusion key bytes.
5724
- *
5725
- * @param {executionResponse} executionResponse The response from an offline function execution (via the `programManager.run` method)
5726
- * @param {ImportedPrograms} imports The imported programs used in the execution. Specified as { "programName": "programSourceCode", ... }
5727
- * @param {ImportedVerifyingKeys} importedVerifyingKeys The verifying keys in the execution. Specified as { "programName": [["functionName", "verifyingKey"], ...], ... }
5728
- * @returns {boolean} True if the proof is valid, false otherwise
5729
- *
5730
-
5731
5742
  /**
5732
5743
  * Create a program object from a program's source code
5733
5744
  *
@@ -5759,6 +5770,109 @@ class ProgramManager {
5759
5770
  return false;
5760
5771
  }
5761
5772
  }
5773
+ /**
5774
+ * Estimate the execution fee for an authorization.
5775
+ *
5776
+ * @param {FeeEstimateOptions} options Options for fee estimate.
5777
+ *
5778
+ * @example
5779
+ * import { AleoKeyProvider, PrivateKey, initThreadPool, ProgramManager } from "@provablehq/sdk";
5780
+ *
5781
+ * await initThreadPool();
5782
+ *
5783
+ * // Create a new KeyProvider.
5784
+ * const keyProvider = new AleoKeyProvider();
5785
+ * keyProvider.useCache(true);
5786
+ *
5787
+ * // Initialize a program manager with the key provider to automatically fetch keys for executions.
5788
+ * const programManager = new ProgramManager("https://api.explorer.provable.com/v1", keyProvider);
5789
+ *
5790
+ * // Build the `Authorization`.
5791
+ * const privateKey = new PrivateKey(); // Change this to a private key that has an aleo credit balance.
5792
+ * const authorization = await programManager.buildAuthorization({
5793
+ * programName: "credits.aleo",
5794
+ * functionName: "transfer_public",
5795
+ * privateKey,
5796
+ * inputs: [
5797
+ * "aleo1vwls2ete8dk8uu2kmkmzumd7q38fvshrht8hlc0a5362uq8ftgyqnm3w08",
5798
+ * "10000000u64",
5799
+ * ],
5800
+ * });
5801
+ *
5802
+ * console.log("Getting execution id");
5803
+ *
5804
+ * // Derive the execution ID and base fee.
5805
+ * const executionId = authorization.toExecutionId().toString();
5806
+ *
5807
+ * console.log("Estimating fee");
5808
+ *
5809
+ * // Get the base fee in microcredits.
5810
+ * const baseFeeMicrocredits = await programManager.estimateFeeForAuthorization({
5811
+ * authorization,
5812
+ * programName: "credits.aleo"
5813
+ * });
5814
+ * const baseFeeCredits = Number(baseFeeMicrocredits)/1000000;
5815
+ *
5816
+ * console.log("Building fee authorization");
5817
+ *
5818
+ * // Build a credits.aleo/fee_public `Authorization`.
5819
+ * const feeAuthorization = await programManager.buildFeeAuthorization({
5820
+ * deploymentOrExecutionId: executionId,
5821
+ * baseFeeCredits,
5822
+ * privateKey
5823
+ * });
5824
+ */
5825
+ async estimateFeeForAuthorization(options) {
5826
+ const { authorization, programName, program, imports, edition } = options;
5827
+ if (!authorization) {
5828
+ throw new Error("Authorization must be provided if estimating fee for Authorization.");
5829
+ }
5830
+ const programSource = program ? program.toString() : await this.networkClient.getProgram(programName, edition);
5831
+ const programImports = imports ? imports : await this.networkClient.getProgramImports(programSource);
5832
+ console.log(JSON.stringify(programImports));
5833
+ if (Object.keys(programImports)) {
5834
+ return ProgramManager$1.estimateFeeForAuthorization(authorization, programSource, programImports, edition);
5835
+ }
5836
+ return ProgramManager$1.estimateFeeForAuthorization(authorization, programSource, imports, edition);
5837
+ }
5838
+ /**
5839
+ * Estimate the execution fee for an Aleo function.
5840
+ *
5841
+ * @param {FeeEstimateOptions} options Options for the fee estimate.
5842
+ *
5843
+ * @returns {Promise<bigint>} Execution fee in microcredits for the authorization.
5844
+ *
5845
+ * @example
5846
+ * import { AleoKeyProvider, PrivateKey, initThreadPool, ProgramManager } from "@provablehq/sdk";
5847
+ *
5848
+ * // Initialize a program manager with the key provider to automatically fetch keys for executions.
5849
+ * const programManager = new ProgramManager("https://api.explorer.provable.com/v1", keyProvider);
5850
+ *
5851
+ * // Get the base fee in microcredits.
5852
+ * const baseFeeMicrocredits = await programManager.estimateExecutionFee({programName: "credits.aleo"});
5853
+ * const baseFeeCredits = Number(baseFeeMicrocredits)/1000000;
5854
+ *
5855
+ * console.log("Building fee authorization");
5856
+ *
5857
+ * // Build a credits.aleo/fee_public `Authorization`.
5858
+ * const baseFeeMicrocredits = await programManager.estimateFeeForAuthorization({
5859
+ * programName: "credits.aleo",
5860
+ * functionName: "transfer_public",
5861
+ * });
5862
+ * const baseFeeCredits = Number(baseFeeMicrocredits)/1000000;
5863
+ */
5864
+ async estimateExecutionFee(options) {
5865
+ const { functionName, programName, program, imports, edition } = options;
5866
+ if (!functionName) {
5867
+ throw new Error("Function name must be specified when estimating fee.");
5868
+ }
5869
+ const programSource = program ? program.toString() : await this.networkClient.getProgram(programName, edition);
5870
+ const programImports = imports ? imports : await this.networkClient.getProgramImports(programSource);
5871
+ if (Object.keys(programImports)) {
5872
+ return ProgramManager$1.estimateExecutionFee(programSource, functionName, programImports, edition);
5873
+ }
5874
+ return ProgramManager$1.estimateExecutionFee(programSource, functionName, imports, edition);
5875
+ }
5762
5876
  // Internal utility function for getting a credits.aleo record
5763
5877
  async getCreditsRecord(amount, nonces, record, params) {
5764
5878
  try {