@provablehq/sdk 0.9.4 → 0.9.6

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.
@@ -33,7 +33,7 @@ import { BlockHeightSearch, NetworkRecordProvider, RecordProvider, RecordSearchP
33
33
  declare function initializeWasm(): Promise<void>;
34
34
  export { ProgramManager, ProvingRequestOptions, ExecuteOptions, FeeAuthorizationOptions, AuthorizationOptions } from "./program-manager.js";
35
35
  export { logAndThrow } from "./utils.js";
36
- export { Address, Authorization, Boolean, BHP256, BHP512, BHP768, BHP1024, Ciphertext, ComputeKey, Execution as FunctionExecution, ExecutionRequest, ExecutionResponse, EncryptionToolkit, Field, Group, OfflineQuery, Pedersen64, Pedersen128, Plaintext, Poseidon2, Poseidon4, Poseidon8, PrivateKey, PrivateKeyCiphertext, Program, ProgramManager as ProgramManagerBase, ProvingKey, ProvingRequest, RecordCiphertext, RecordPlaintext, Signature, Scalar, Transaction, Transition, VerifyingKey, ViewKey, initThreadPool, verifyFunctionExecution, } from "./wasm.js";
36
+ export { Address, Authorization, Boolean, BHP256, BHP512, BHP768, BHP1024, Ciphertext, ComputeKey, Execution as FunctionExecution, ExecutionRequest, ExecutionResponse, EncryptionToolkit, Field, GraphKey, Group, I8, I16, I32, I64, I128, OfflineQuery, Pedersen64, Pedersen128, Plaintext, Poseidon2, Poseidon4, Poseidon8, PrivateKey, PrivateKeyCiphertext, Program, ProgramManager as ProgramManagerBase, ProvingKey, ProvingRequest, RecordCiphertext, RecordPlaintext, Signature, Scalar, Transaction, Transition, U8, U16, U32, U64, U128, VerifyingKey, ViewKey, initThreadPool, verifyFunctionExecution, } from "./wasm.js";
37
37
  export { initializeWasm };
38
38
  export { Key, CREDITS_PROGRAM_KEYS, KEY_STORE, PRIVATE_TRANSFER, PRIVATE_TO_PUBLIC_TRANSFER, PRIVATE_TRANSFER_TYPES, PUBLIC_TRANSFER, PUBLIC_TRANSFER_AS_SIGNER, PUBLIC_TO_PRIVATE_TRANSFER, VALID_TRANSFER_TYPES, } from "./constants.js";
39
39
  export { Account, AleoKeyProvider, AleoKeyProviderParams, AleoKeyProviderInitParams, AleoNetworkClient, BlockJSON, BlockHeightSearch, CachedKeyPair, ConfirmedTransactionJSON, DeploymentJSON, DeploymentObject, ExecutionJSON, ExecutionObject, FeeExecutionJSON, FeeExecutionObject, FinalizeJSON, FunctionObject, FunctionKeyPair, FunctionKeyProvider, Header, ImportedPrograms, ImportedVerifyingKeys, InputJSON, InputObject, KeySearchParams, Metadata, NetworkRecordProvider, OfflineKeyProvider, OfflineSearchParams, OutputJSON, OutputObject, OwnerJSON, PartialSolutionJSON, PlaintextArray, PlaintextLiteral, PlaintextObject, PlaintextStruct, ProgramImports, ProvingRequestJSON, ProvingResponse, RatificationJSON, RecordProvider, RecordSearchParams, SolutionJSON, SolutionsJSON, TransactionJSON, TransactionObject, TransitionJSON, TransitionObject, VerifyingKeys, };
@@ -1,6 +1,6 @@
1
1
  import 'core-js/proposals/json-parse-with-source.js';
2
2
  import { ViewKey, ComputeKey, Address, PrivateKeyCiphertext, PrivateKey, RecordCiphertext, EncryptionToolkit, Group, Program, Plaintext, Transaction, Metadata, VerifyingKey, ProvingKey, ProgramManager as ProgramManager$1, RecordPlaintext, verifyFunctionExecution } from '@provablehq/wasm/mainnet.js';
3
- export { Address, Authorization, BHP1024, BHP256, BHP512, BHP768, Boolean, Ciphertext, ComputeKey, EncryptionToolkit, ExecutionRequest, ExecutionResponse, Field, Execution as FunctionExecution, Group, OfflineQuery, Pedersen128, Pedersen64, Plaintext, Poseidon2, Poseidon4, Poseidon8, PrivateKey, PrivateKeyCiphertext, Program, ProgramManager as ProgramManagerBase, ProvingKey, ProvingRequest, RecordCiphertext, RecordPlaintext, Scalar, Signature, Transaction, Transition, VerifyingKey, ViewKey, initThreadPool, verifyFunctionExecution } from '@provablehq/wasm/mainnet.js';
3
+ export { Address, Authorization, BHP1024, BHP256, BHP512, BHP768, Boolean, Ciphertext, ComputeKey, 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, ProvingKey, ProvingRequest, RecordCiphertext, RecordPlaintext, Scalar, Signature, Transaction, Transition, U128, U16, U32, U64, U8, VerifyingKey, ViewKey, initThreadPool, verifyFunctionExecution } from '@provablehq/wasm/mainnet.js';
4
4
 
5
5
  /**
6
6
  * Key Management class. Enables the creation of a new Aleo Account, importation of an existing account from
@@ -489,7 +489,7 @@ class AleoNetworkClient {
489
489
  else {
490
490
  this.headers = {
491
491
  // This is replaced by the actual version by a Rollup plugin
492
- "X-Aleo-SDK-Version": "0.9.4",
492
+ "X-Aleo-SDK-Version": "0.9.6",
493
493
  "X-Aleo-environment": environment(),
494
494
  };
495
495
  }
@@ -1186,7 +1186,17 @@ class AleoNetworkClient {
1186
1186
  /**
1187
1187
  * Returns the source code of a program given a program ID.
1188
1188
  *
1189
- * @param {string} programId The program ID of a program deployed to the Aleo Network
1189
+ * @param {string} programId The program ID of a program deployed to the Aleo Network.
1190
+ * @param {number | undefined} edition The edition of the program to fetch. When this is undefined it will fetch the latest version.
1191
+ * @returns {Promise<string>} The source code of the program.
1192
+ *
1193
+ * @example
1194
+ * import { AleoNetworkClient } from "@provablehq/sdk/mainnet.js";
1195
+ *
1196
+ * // Create a network client.
1197
+ * const networkClient = new AleoNetworkClient("http://api.explorer.provable.com/v1", undefined);
1198
+ *
1199
+ * // Get the source code of a program.)
1190
1200
  * @returns {Promise<string>} Source code of the program
1191
1201
  *
1192
1202
  * @example
@@ -1199,10 +1209,43 @@ class AleoNetworkClient {
1199
1209
  * const expectedSource = "program hello_hello.aleo;\n\nfunction hello:\n input r0 as u32.public;\n input r1 as u32.private;\n add r0 r1 into r2;\n output r2 as u32.private;\n"
1200
1210
  * assert.equal(program, expectedSource);
1201
1211
  */
1202
- async getProgram(programId) {
1212
+ async getProgram(programId, edition) {
1203
1213
  try {
1204
- this.ctx = { "X-ALEO-METHOD": "getProgram" };
1205
- return await this.fetchData("/program/" + programId);
1214
+ this.ctx = { "X-ALEO-METHOD": "getProgramVersion" };
1215
+ if (typeof edition === "number") {
1216
+ return await this.fetchData(`/program/${programId}/${edition}`);
1217
+ }
1218
+ else {
1219
+ return await this.fetchData("/program/" + programId);
1220
+ }
1221
+ }
1222
+ catch (error) {
1223
+ throw new Error(`Error fetching program ${programId}: ${error}`);
1224
+ }
1225
+ finally {
1226
+ this.ctx = {};
1227
+ }
1228
+ }
1229
+ /**
1230
+ * Returns the current program edition deployed on the Aleo network.
1231
+ *
1232
+ * @param {string} programId The program ID of a program deployed to the Aleo Network.
1233
+ * @returns {Promise<number>} The edition of the program.
1234
+ *
1235
+ * @example
1236
+ * import { AleoNetworkClient } from "@provablehq/sdk/mainnet.js";
1237
+ *
1238
+ * // Create a network client.
1239
+ * const networkClient = new AleoNetworkClient("http://api.explorer.provable.com/v1", undefined);
1240
+ *
1241
+ * const programVersion = networkClient.getLatestProgramEdition("hello_hello.aleo");
1242
+ * assert.equal(programVersion, 1);
1243
+ */
1244
+ async getLatestProgramEdition(programId) {
1245
+ try {
1246
+ this.ctx = { "X-ALEO-METHOD": "getLatestProgramEdition" };
1247
+ const raw = await this.fetchRaw("/program/" + programId + "/latest_edition");
1248
+ return JSON.parse(raw);
1206
1249
  }
1207
1250
  catch (error) {
1208
1251
  throw new Error(`Error fetching program ${programId}: ${error}`);
@@ -1214,8 +1257,9 @@ class AleoNetworkClient {
1214
1257
  /**
1215
1258
  * Returns a program object from a program ID or program source code.
1216
1259
  *
1217
- * @param {string} inputProgram The program ID or program source code of a program deployed to the Aleo Network
1218
- * @returns {Promise<Program>} Source code of the program
1260
+ * @param {string} inputProgram The program ID or program source code of a program deployed to the Aleo Network.
1261
+ * @param {number | undefined} edition The edition of the program to fetch. When this is undefined it will fetch the latest version.
1262
+ * @returns {Promise<Program>} Source code of the program.
1219
1263
  *
1220
1264
  * @example
1221
1265
  * import { AleoNetworkClient } from "@provablehq/sdk/mainnet.js";
@@ -1233,18 +1277,13 @@ class AleoNetworkClient {
1233
1277
  * // Both program objects should be equal
1234
1278
  * assert(programObjectFromID.to_string() === programObjectFromSource.to_string());
1235
1279
  */
1236
- async getProgramObject(inputProgram) {
1280
+ async getProgramObject(inputProgram, edition) {
1237
1281
  try {
1238
1282
  this.ctx = { "X-ALEO-METHOD": "getProgramObject" };
1239
- return Program.fromString(inputProgram);
1283
+ return Program.fromString(await this.getProgram(inputProgram, edition));
1240
1284
  }
1241
1285
  catch (error) {
1242
- try {
1243
- return Program.fromString(await this.getProgram(inputProgram));
1244
- }
1245
- catch (error) {
1246
- throw new Error(`${inputProgram} is neither a program name or a valid program: ${error}`);
1247
- }
1286
+ throw new Error(`${inputProgram} is neither a program name or a valid program: ${error}`);
1248
1287
  }
1249
1288
  finally {
1250
1289
  this.ctx = {};
@@ -1284,18 +1323,32 @@ class AleoNetworkClient {
1284
1323
  try {
1285
1324
  this.ctx = { "X-ALEO-METHOD": "getProgramImports" };
1286
1325
  const imports = {};
1287
- // Get the program object or fail if the program is not valid or does not exist
1288
- const program = inputProgram instanceof Program
1289
- ? inputProgram
1290
- : await this.getProgramObject(inputProgram);
1326
+ // Normalize input to a Program object
1327
+ let program;
1328
+ if (inputProgram instanceof Program) {
1329
+ program = inputProgram;
1330
+ }
1331
+ else {
1332
+ try {
1333
+ program = Program.fromString(inputProgram);
1334
+ }
1335
+ catch {
1336
+ try {
1337
+ program = await this.getProgramObject(inputProgram);
1338
+ }
1339
+ catch (error2) {
1340
+ throw new Error(`${inputProgram} is neither a program name nor a valid program: ${error2}`);
1341
+ }
1342
+ }
1343
+ }
1291
1344
  // Get the list of programs that the program imports
1292
1345
  const importList = program.getImports();
1293
- // Recursively get any imports that the imported programs have in a depth first search order
1346
+ // Recursively get any imports that the imported programs have in a depth-first search
1294
1347
  for (let i = 0; i < importList.length; i++) {
1295
1348
  const import_id = importList[i];
1296
1349
  if (!imports.hasOwnProperty(import_id)) {
1297
- const programSource = (await this.getProgram(import_id));
1298
- const nestedImports = (await this.getProgramImports(import_id));
1350
+ const programSource = await this.getProgram(import_id);
1351
+ const nestedImports = await this.getProgramImports(import_id);
1299
1352
  for (const key in nestedImports) {
1300
1353
  if (!imports.hasOwnProperty(key)) {
1301
1354
  imports[key] = nestedImports[key];
@@ -1374,7 +1427,7 @@ class AleoNetworkClient {
1374
1427
  return await this.fetchData(`/program/${programId}/mappings`);
1375
1428
  }
1376
1429
  catch (error) {
1377
- throw new Error(`Error fetching mappings for program ${programId} - ensure the program exists on chain before trying again`);
1430
+ throw new Error(`Error fetching mappings for program ${programId} - ensure the program exists on chain before trying again: ${error}`);
1378
1431
  }
1379
1432
  finally {
1380
1433
  this.ctx = {};
@@ -1406,7 +1459,7 @@ class AleoNetworkClient {
1406
1459
  return await this.fetchData(`/program/${programId}/mapping/${mappingName}/${keyString}`);
1407
1460
  }
1408
1461
  catch (error) {
1409
- throw new Error(`Error fetching value for key '${key}' in mapping '${mappingName}' in program '${programId}' - ensure the mapping exists and the key is correct`);
1462
+ throw new Error(`Error fetching value for key '${key}' in mapping '${mappingName}' in program '${programId}' - ensure the mapping exists and the key is correct: ${error}`);
1410
1463
  }
1411
1464
  finally {
1412
1465
  this.ctx = {};
@@ -3412,6 +3465,7 @@ class ProgramManager {
3412
3465
  let verifyingKey = options.verifyingKey;
3413
3466
  let program = options.program;
3414
3467
  let imports = options.imports;
3468
+ let edition = options.edition;
3415
3469
  // Ensure the function exists on the network
3416
3470
  if (program === undefined) {
3417
3471
  try {
@@ -3424,6 +3478,15 @@ class ProgramManager {
3424
3478
  else if (program instanceof Program) {
3425
3479
  program = program.toString();
3426
3480
  }
3481
+ if (edition == undefined) {
3482
+ try {
3483
+ edition = await this.networkClient.getLatestProgramEdition(programName);
3484
+ }
3485
+ catch (e) {
3486
+ console.warn(`Error finding edition for ${programName}. Network response: '${e.message}'. Assuming edition 1.`);
3487
+ edition = 1;
3488
+ }
3489
+ }
3427
3490
  // Get the private key from the account if it is not provided in the parameters
3428
3491
  let executionPrivateKey = privateKey;
3429
3492
  if (typeof privateKey === "undefined" &&
@@ -3473,7 +3536,7 @@ class ProgramManager {
3473
3536
  }
3474
3537
  }
3475
3538
  // Build an execution transaction
3476
- return await ProgramManager$1.buildExecutionTransaction(executionPrivateKey, program, functionName, inputs, priorityFee, feeRecord, this.host, imports, provingKey, verifyingKey, feeProvingKey, feeVerifyingKey, offlineQuery);
3539
+ return await ProgramManager$1.buildExecutionTransaction(executionPrivateKey, program, functionName, inputs, priorityFee, feeRecord, this.host, imports, provingKey, verifyingKey, feeProvingKey, feeVerifyingKey, offlineQuery, edition);
3477
3540
  }
3478
3541
  /**
3479
3542
  * Builds a SnarkVM `Authorization` for a specific function.
@@ -3509,6 +3572,7 @@ class ProgramManager {
3509
3572
  const privateKey = options.privateKey;
3510
3573
  let program = options.programSource;
3511
3574
  let imports = options.programImports;
3575
+ let edition = options.edition;
3512
3576
  // Ensure the function exists on the network.
3513
3577
  if (program === undefined) {
3514
3578
  try {
@@ -3530,6 +3594,15 @@ class ProgramManager {
3530
3594
  if (typeof executionPrivateKey === "undefined") {
3531
3595
  throw "No private key provided and no private key set in the ProgramManager";
3532
3596
  }
3597
+ if (edition == undefined) {
3598
+ try {
3599
+ edition = await this.networkClient.getLatestProgramEdition(programName);
3600
+ }
3601
+ catch (e) {
3602
+ console.warn(`Error finding edition for ${programName}. Network response: '${e.message}'. Assuming edition 1.`);
3603
+ edition = 1;
3604
+ }
3605
+ }
3533
3606
  // Resolve the program imports if they exist.
3534
3607
  const numberOfImports = Program.fromString(program).getImports().length;
3535
3608
  if (numberOfImports > 0 && !imports) {
@@ -3541,7 +3614,7 @@ class ProgramManager {
3541
3614
  }
3542
3615
  }
3543
3616
  // Build and return an `Authorization` for the desired function.
3544
- return await ProgramManager$1.authorize(executionPrivateKey, program, functionName, inputs, imports);
3617
+ return await ProgramManager$1.authorize(executionPrivateKey, program, functionName, inputs, imports, edition);
3545
3618
  }
3546
3619
  /**
3547
3620
  * Builds a SnarkVM `Authorization` for a specific function without building a circuit first. This should be used when fast authorization generation is needed and the invoker is confident inputs are coorect.
@@ -3577,6 +3650,7 @@ class ProgramManager {
3577
3650
  const privateKey = options.privateKey;
3578
3651
  let program = options.programSource;
3579
3652
  let imports = options.programImports;
3653
+ let edition = options.edition;
3580
3654
  // Ensure the function exists on the network.
3581
3655
  if (program === undefined) {
3582
3656
  try {
@@ -3608,8 +3682,17 @@ class ProgramManager {
3608
3682
  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.`);
3609
3683
  }
3610
3684
  }
3685
+ if (edition == undefined) {
3686
+ try {
3687
+ edition = await this.networkClient.getLatestProgramEdition(programName);
3688
+ }
3689
+ catch (e) {
3690
+ console.warn(`Error finding edition for ${programName}. Network response: '${e.message}'. Assuming edition 1.`);
3691
+ edition = 1;
3692
+ }
3693
+ }
3611
3694
  // Build and return an `Authorization` for the desired function.
3612
- return await ProgramManager$1.buildAuthorizationUnchecked(executionPrivateKey, program, functionName, inputs, imports);
3695
+ return await ProgramManager$1.buildAuthorizationUnchecked(executionPrivateKey, program, functionName, inputs, imports, edition);
3613
3696
  }
3614
3697
  /**
3615
3698
  * Builds a `ProvingRequest` for submission to a prover for execution.
@@ -3650,6 +3733,7 @@ class ProgramManager {
3650
3733
  let program = options.programSource;
3651
3734
  let feeRecord = options.feeRecord;
3652
3735
  let imports = options.programImports;
3736
+ let edition = options.edition;
3653
3737
  // Ensure the function exists on the network.
3654
3738
  if (program === undefined) {
3655
3739
  try {
@@ -3662,6 +3746,15 @@ class ProgramManager {
3662
3746
  else if (program instanceof Program) {
3663
3747
  program = program.toString();
3664
3748
  }
3749
+ if (edition == undefined) {
3750
+ try {
3751
+ edition = await this.networkClient.getLatestProgramEdition(programName);
3752
+ }
3753
+ catch (e) {
3754
+ console.warn(`Error finding edition for ${programName}. Network response: '${e.message}'. Assuming edition 1.`);
3755
+ edition = 1;
3756
+ }
3757
+ }
3665
3758
  // Get the private key from the account if it is not provided in the parameters.
3666
3759
  let executionPrivateKey = privateKey;
3667
3760
  if (typeof privateKey === "undefined" &&
@@ -3691,7 +3784,7 @@ class ProgramManager {
3691
3784
  }
3692
3785
  }
3693
3786
  // Build and return the `ProvingRequest`.
3694
- return await ProgramManager$1.buildProvingRequest(executionPrivateKey, program, functionName, inputs, baseFee, priorityFee, feeRecord, imports, broadcast, unchecked);
3787
+ return await ProgramManager$1.buildProvingRequest(executionPrivateKey, program, functionName, inputs, baseFee, priorityFee, feeRecord, imports, broadcast, unchecked, edition);
3695
3788
  }
3696
3789
  /**
3697
3790
  * Builds a SnarkVM fee `Authorization` for `credits.aleo/fee_private` or `credits.aleo/fee_public`. If a record is provided `fee_private` will be executed, otherwise `fee_public` will be executed.
@@ -3822,7 +3915,7 @@ class ProgramManager {
3822
3915
  * const result = executionResponse.getOutputs();
3823
3916
  * assert(result === ["10u32"]);
3824
3917
  */
3825
- async run(program, function_name, inputs, proveExecution, imports, keySearchParams, provingKey, verifyingKey, privateKey, offlineQuery) {
3918
+ async run(program, function_name, inputs, proveExecution, imports, keySearchParams, provingKey, verifyingKey, privateKey, offlineQuery, edition) {
3826
3919
  // Get the private key from the account if it is not provided in the parameters
3827
3920
  let executionPrivateKey = privateKey;
3828
3921
  if (typeof privateKey === "undefined" &&
@@ -3845,7 +3938,7 @@ class ProgramManager {
3845
3938
  console.log("Running program offline");
3846
3939
  console.log("Proving key: ", provingKey);
3847
3940
  console.log("Verifying key: ", verifyingKey);
3848
- return ProgramManager$1.executeFunctionOffline(executionPrivateKey, program, function_name, inputs, proveExecution, false, imports, provingKey, verifyingKey, this.host, offlineQuery);
3941
+ return ProgramManager$1.executeFunctionOffline(executionPrivateKey, program, function_name, inputs, proveExecution, false, imports, provingKey, verifyingKey, this.host, offlineQuery, edition);
3849
3942
  }
3850
3943
  /**
3851
3944
  * Join two credits records into a single credits record