@provablehq/sdk 0.9.4 → 0.9.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.
@@ -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, 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, 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.5",
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,42 @@ 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
+ return await this.fetchData("/program/" + programId + "/latest_edition");
1206
1248
  }
1207
1249
  catch (error) {
1208
1250
  throw new Error(`Error fetching program ${programId}: ${error}`);
@@ -1214,8 +1256,9 @@ class AleoNetworkClient {
1214
1256
  /**
1215
1257
  * Returns a program object from a program ID or program source code.
1216
1258
  *
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
1259
+ * @param {string} inputProgram The program ID or program source code of a program deployed to the Aleo Network.
1260
+ * @param {number | undefined} edition The edition of the program to fetch. When this is undefined it will fetch the latest version.
1261
+ * @returns {Promise<Program>} Source code of the program.
1219
1262
  *
1220
1263
  * @example
1221
1264
  * import { AleoNetworkClient } from "@provablehq/sdk/mainnet.js";
@@ -1233,18 +1276,13 @@ class AleoNetworkClient {
1233
1276
  * // Both program objects should be equal
1234
1277
  * assert(programObjectFromID.to_string() === programObjectFromSource.to_string());
1235
1278
  */
1236
- async getProgramObject(inputProgram) {
1279
+ async getProgramObject(inputProgram, edition) {
1237
1280
  try {
1238
1281
  this.ctx = { "X-ALEO-METHOD": "getProgramObject" };
1239
- return Program.fromString(inputProgram);
1282
+ return Program.fromString(await this.getProgram(inputProgram, edition));
1240
1283
  }
1241
1284
  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
- }
1285
+ throw new Error(`${inputProgram} is neither a program name or a valid program: ${error}`);
1248
1286
  }
1249
1287
  finally {
1250
1288
  this.ctx = {};
@@ -1284,18 +1322,32 @@ class AleoNetworkClient {
1284
1322
  try {
1285
1323
  this.ctx = { "X-ALEO-METHOD": "getProgramImports" };
1286
1324
  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);
1325
+ // Normalize input to a Program object
1326
+ let program;
1327
+ if (inputProgram instanceof Program) {
1328
+ program = inputProgram;
1329
+ }
1330
+ else {
1331
+ try {
1332
+ program = Program.fromString(inputProgram);
1333
+ }
1334
+ catch {
1335
+ try {
1336
+ program = await this.getProgramObject(inputProgram);
1337
+ }
1338
+ catch (error2) {
1339
+ throw new Error(`${inputProgram} is neither a program name nor a valid program: ${error2}`);
1340
+ }
1341
+ }
1342
+ }
1291
1343
  // Get the list of programs that the program imports
1292
1344
  const importList = program.getImports();
1293
- // Recursively get any imports that the imported programs have in a depth first search order
1345
+ // Recursively get any imports that the imported programs have in a depth-first search
1294
1346
  for (let i = 0; i < importList.length; i++) {
1295
1347
  const import_id = importList[i];
1296
1348
  if (!imports.hasOwnProperty(import_id)) {
1297
- const programSource = (await this.getProgram(import_id));
1298
- const nestedImports = (await this.getProgramImports(import_id));
1349
+ const programSource = await this.getProgram(import_id);
1350
+ const nestedImports = await this.getProgramImports(import_id);
1299
1351
  for (const key in nestedImports) {
1300
1352
  if (!imports.hasOwnProperty(key)) {
1301
1353
  imports[key] = nestedImports[key];
@@ -1374,7 +1426,7 @@ class AleoNetworkClient {
1374
1426
  return await this.fetchData(`/program/${programId}/mappings`);
1375
1427
  }
1376
1428
  catch (error) {
1377
- throw new Error(`Error fetching mappings for program ${programId} - ensure the program exists on chain before trying again`);
1429
+ throw new Error(`Error fetching mappings for program ${programId} - ensure the program exists on chain before trying again: ${error}`);
1378
1430
  }
1379
1431
  finally {
1380
1432
  this.ctx = {};
@@ -1406,7 +1458,7 @@ class AleoNetworkClient {
1406
1458
  return await this.fetchData(`/program/${programId}/mapping/${mappingName}/${keyString}`);
1407
1459
  }
1408
1460
  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`);
1461
+ 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
1462
  }
1411
1463
  finally {
1412
1464
  this.ctx = {};