@provablehq/sdk 0.9.11 → 0.9.12

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.
@@ -9,6 +9,7 @@ import { EncryptedRecord } from "./models/record-provider/encryptedRecord.js";
9
9
  import { ExecutionJSON, FeeExecutionJSON } from "./models/execution/executionJSON.js";
10
10
  import { ExecutionObject, FeeExecutionObject } from "./models/execution/executionObject.js";
11
11
  import { FinalizeJSON } from "./models/finalizeJSON.js";
12
+ import { FunctionInput } from "./models/functionInput";
12
13
  import { FunctionObject } from "./models/functionObject.js";
13
14
  import { ImportedVerifyingKeys, ImportedPrograms } from "./models/imports.js";
14
15
  import { InputJSON } from "./models/input/inputJSON.js";
@@ -44,4 +45,4 @@ export { logAndThrow } from "./utils.js";
44
45
  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, getOrInitConsensusVersionTestHeights, verifyFunctionExecution, } from "./wasm.js";
45
46
  export { initializeWasm };
46
47
  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, RECORD_DOMAIN, VALID_TRANSFER_TYPES, } from "./constants.js";
47
- export { Account, AleoKeyProvider, AleoKeyProviderParams, AleoKeyProviderInitParams, AleoNetworkClient, BlockJSON, BlockHeightSearch, CachedKeyPair, ConfirmedTransactionJSON, DeploymentJSON, DeploymentObject, EncryptedRecord, ExecutionJSON, ExecutionObject, FeeExecutionJSON, FeeExecutionObject, FinalizeJSON, FunctionObject, FunctionKeyPair, FunctionKeyProvider, Header, ImportedPrograms, ImportedVerifyingKeys, InputJSON, InputObject, KeySearchParams, Metadata, NetworkRecordProvider, OfflineKeyProvider, OfflineSearchParams, OutputJSON, OutputObject, OwnedFilter, OwnedRecord, OwnerJSON, PartialSolutionJSON, PlaintextArray, PlaintextLiteral, PlaintextObject, PlaintextStruct, ProgramImports, ProvingRequestJSON, ProvingResponse, RatificationJSON, RecordsFilter, RecordsResponseFilter, RecordProvider, RecordScanner, RecordSearchParams, SealanceMerkleTree, SolutionJSON, SolutionsJSON, TransactionJSON, TransactionObject, TransitionJSON, TransitionObject, VerifyingKeys, };
48
+ export { Account, AleoKeyProvider, AleoKeyProviderParams, AleoKeyProviderInitParams, AleoNetworkClient, BlockJSON, BlockHeightSearch, CachedKeyPair, ConfirmedTransactionJSON, DeploymentJSON, DeploymentObject, EncryptedRecord, ExecutionJSON, ExecutionObject, FeeExecutionJSON, FeeExecutionObject, FinalizeJSON, FunctionInput, FunctionObject, FunctionKeyPair, FunctionKeyProvider, Header, ImportedPrograms, ImportedVerifyingKeys, InputJSON, InputObject, KeySearchParams, Metadata, NetworkRecordProvider, OfflineKeyProvider, OfflineSearchParams, OutputJSON, OutputObject, OwnedFilter, OwnedRecord, OwnerJSON, PartialSolutionJSON, PlaintextArray, PlaintextLiteral, PlaintextObject, PlaintextStruct, ProgramImports, ProvingRequestJSON, ProvingResponse, RatificationJSON, RecordsFilter, RecordsResponseFilter, RecordProvider, RecordScanner, RecordSearchParams, SealanceMerkleTree, SolutionJSON, SolutionsJSON, TransactionJSON, TransactionObject, TransitionJSON, TransitionObject, VerifyingKeys, };
@@ -497,7 +497,7 @@ class AleoNetworkClient {
497
497
  else {
498
498
  this.headers = {
499
499
  // This is replaced by the actual version by a Rollup plugin
500
- "X-Aleo-SDK-Version": "0.9.11",
500
+ "X-Aleo-SDK-Version": "0.9.12",
501
501
  "X-Aleo-environment": environment(),
502
502
  };
503
503
  }
@@ -2306,7 +2306,7 @@ class AleoKeyProvider {
2306
2306
  const verifying_key = key.verifyingKey();
2307
2307
  const proving_key = await this.fetchProvingKey(key.prover, key.locator);
2308
2308
  if (this.cacheOption) {
2309
- this.cache.set(CREDITS_PROGRAM_KEYS.bond_public.locator, [proving_key.toBytes(), verifying_key.toBytes()]);
2309
+ this.cache.set(CREDITS_PROGRAM_KEYS.getKey(key.name).locator, [proving_key.toBytes(), verifying_key.toBytes()]);
2310
2310
  }
2311
2311
  return [proving_key, verifying_key];
2312
2312
  }
@@ -3895,6 +3895,44 @@ class ProgramManager {
3895
3895
  setHeader(headerName, value) {
3896
3896
  this.networkClient.headers[headerName] = value;
3897
3897
  }
3898
+ /**
3899
+ * Set the inclusion prover into the wasm memory. This should be done prior to any execution of a function with a
3900
+ * private record.
3901
+ *
3902
+ * @param {ProvingKey} [provingKey]
3903
+ *
3904
+ * @example
3905
+ * import { ProgramManager, AleoKeyProvider } from "@provablehq/sdk/mainnet.js";
3906
+ *
3907
+ * const keyProvider = new AleoKeyProvider();
3908
+ * keyProvider.useCache(true);
3909
+ *
3910
+ * // Create a ProgramManager
3911
+ * const programManager = new ProgramManager("https://api.explorer.provable.com/v1", keyProvider);
3912
+ *
3913
+ * // Set the inclusion keys.
3914
+ * programManager.setInclusionProver();
3915
+ */
3916
+ async setInclusionProver(provingKey) {
3917
+ if (this.inclusionKeysLoaded) {
3918
+ return;
3919
+ }
3920
+ try {
3921
+ if (provingKey) {
3922
+ ProgramManager$1.loadInclusionProver(provingKey);
3923
+ this.inclusionKeysLoaded = true;
3924
+ }
3925
+ else {
3926
+ const inclusionKeys = await this.keyProvider.inclusionKeys();
3927
+ ProgramManager$1.loadInclusionProver(inclusionKeys[0]);
3928
+ this.inclusionKeysLoaded = true;
3929
+ }
3930
+ return;
3931
+ }
3932
+ catch {
3933
+ console.log("Setting the inclusion prover requires either a key provider to be configured for the ProgramManager OR to pass the inclusion prover directly");
3934
+ }
3935
+ }
3898
3936
  /**
3899
3937
  * Remove a header from the `AleoNetworkClient`s header map
3900
3938
  *
@@ -3987,9 +4025,23 @@ class ProgramManager {
3987
4025
  }
3988
4026
  // Get the fee record from the account if it is not provided in the parameters
3989
4027
  try {
3990
- feeRecord = privateFee
3991
- ? RecordPlaintext.fromString((await this.getCreditsRecord(priorityFee, [], feeRecord, recordSearchParams)).record_plaintext ?? '')
3992
- : undefined;
4028
+ if (privateFee) {
4029
+ let fee = priorityFee;
4030
+ // If a private fee is specified, but no fee record is provided, estimate the fee and find a matching record.
4031
+ if (!feeRecord) {
4032
+ console.log("Private fee specified, but no private fee record provided, estimating fee and finding a matching fee record.");
4033
+ const programString = programObject.toString();
4034
+ const imports = await this.networkClient.getProgramImports(programString);
4035
+ const baseFee = Number(ProgramManager$1.estimateDeploymentFee(programString, imports));
4036
+ fee = baseFee + priorityFee;
4037
+ }
4038
+ // Get a credits.aleo record for the fee.
4039
+ feeRecord = await this.getCreditsRecord(fee, [], feeRecord, recordSearchParams);
4040
+ }
4041
+ else {
4042
+ // If it's specified NOT to use a privateFee, use a public fee.
4043
+ feeRecord = undefined;
4044
+ }
3993
4045
  }
3994
4046
  catch (e) {
3995
4047
  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.`);
@@ -4115,21 +4167,35 @@ class ProgramManager {
4115
4167
  let programName = options.programName;
4116
4168
  let imports = options.imports;
4117
4169
  let edition = options.edition;
4170
+ let programObject;
4118
4171
  // Ensure the function exists on the network
4119
4172
  if (program === undefined) {
4120
4173
  try {
4121
- program = (await this.networkClient.getProgram(programName));
4174
+ programObject = await this.networkClient.getProgramObject(programName);
4175
+ program = programObject.toString();
4122
4176
  }
4123
4177
  catch (e) {
4124
4178
  logAndThrow(`Error finding ${programName}. Network response: '${e.message}'. Please ensure you're connected to a valid Aleo network the program is deployed to the network.`);
4125
4179
  }
4126
4180
  }
4181
+ else if (typeof program == "string") {
4182
+ try {
4183
+ programObject = Program.fromString(program);
4184
+ }
4185
+ catch (e) {
4186
+ logAndThrow(`Program sources passed for ${programName} were invalid: ${e}`);
4187
+ }
4188
+ }
4127
4189
  else if (program instanceof Program) {
4190
+ programObject = program;
4128
4191
  program = program.toString();
4129
4192
  }
4193
+ if (!(programObject instanceof Program)) {
4194
+ logAndThrow(`Failed to validate program ${programName}`);
4195
+ }
4130
4196
  // Get the program name if it is not provided in the parameters.
4131
4197
  if (programName === undefined) {
4132
- programName = Program.fromString(program).id();
4198
+ programName = programObject.id();
4133
4199
  }
4134
4200
  if (edition == undefined) {
4135
4201
  try {
@@ -4149,15 +4215,6 @@ class ProgramManager {
4149
4215
  if (typeof executionPrivateKey === "undefined") {
4150
4216
  throw "No private key provided and no private key set in the ProgramManager";
4151
4217
  }
4152
- // Get the fee record from the account if it is not provided in the parameters
4153
- try {
4154
- feeRecord = privateFee
4155
- ? RecordPlaintext.fromString((await this.getCreditsRecord(priorityFee, [], feeRecord, recordSearchParams)).record_plaintext ?? '')
4156
- : undefined;
4157
- }
4158
- catch (e) {
4159
- 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.`);
4160
- }
4161
4218
  // Get the fee proving and verifying keys from the key provider
4162
4219
  let feeKeys;
4163
4220
  try {
@@ -4179,7 +4236,7 @@ class ProgramManager {
4179
4236
  }
4180
4237
  }
4181
4238
  // Resolve the program imports if they exist
4182
- const numberOfImports = Program.fromString(program).getImports().length;
4239
+ const numberOfImports = programObject.getImports().length;
4183
4240
  if (numberOfImports > 0 && !imports) {
4184
4241
  try {
4185
4242
  imports = (await this.networkClient.getProgramImports(programName));
@@ -4188,6 +4245,26 @@ class ProgramManager {
4188
4245
  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.`);
4189
4246
  }
4190
4247
  }
4248
+ // Get the fee record from the account if it is not provided in the parameters
4249
+ try {
4250
+ if (privateFee) {
4251
+ let fee = priorityFee;
4252
+ // If a fee record wasn't provided, estimate the fee that needs to be paid.
4253
+ if (!feeRecord) {
4254
+ const baseFee = Number(await this.estimateExecutionFee({ programName, functionName, program, imports }));
4255
+ fee = baseFee + priorityFee;
4256
+ }
4257
+ // Get a credits.aleo record for the fee.
4258
+ feeRecord = await this.getCreditsRecord(fee, [], feeRecord, recordSearchParams);
4259
+ }
4260
+ else {
4261
+ // If it's specified NOT to use a privateFee, use a public fee.
4262
+ feeRecord = undefined;
4263
+ }
4264
+ }
4265
+ catch (e) {
4266
+ 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.`);
4267
+ }
4191
4268
  if (offlineQuery && !this.inclusionKeysLoaded) {
4192
4269
  try {
4193
4270
  const inclusionKeys = await this.keyProvider.inclusionKeys();
@@ -4584,15 +4661,6 @@ class ProgramManager {
4584
4661
  if (typeof executionPrivateKey === "undefined") {
4585
4662
  throw "No private key provided and no private key set in the ProgramManager";
4586
4663
  }
4587
- // Get the fee record from the account if it is not provided in the parameters.
4588
- try {
4589
- feeRecord = privateFee
4590
- ? RecordPlaintext.fromString((await this.getCreditsRecord(priorityFee, [], feeRecord, recordSearchParams)).record_plaintext ?? '')
4591
- : undefined;
4592
- }
4593
- catch (e) {
4594
- 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.`);
4595
- }
4596
4664
  // Resolve the program imports if they exist.
4597
4665
  const numberOfImports = Program.fromString(program).getImports().length;
4598
4666
  if (numberOfImports > 0 && !imports) {
@@ -4603,6 +4671,26 @@ class ProgramManager {
4603
4671
  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.`);
4604
4672
  }
4605
4673
  }
4674
+ // Get the fee record from the account if it is not provided in the parameters
4675
+ try {
4676
+ if (privateFee) {
4677
+ let fee = priorityFee;
4678
+ // If a fee record wasn't provided, estimate the fee that needs to be paid.
4679
+ if (!feeRecord) {
4680
+ const baseFee = Number(await this.estimateExecutionFee({ programName, functionName, program: program.toString(), imports }));
4681
+ fee = baseFee + priorityFee;
4682
+ }
4683
+ // Get a credits.aleo record for the fee.
4684
+ feeRecord = await this.getCreditsRecord(fee, [], feeRecord, recordSearchParams);
4685
+ }
4686
+ else {
4687
+ // If it's specified NOT to use a privateFee, use a public fee.
4688
+ feeRecord = undefined;
4689
+ }
4690
+ }
4691
+ catch (e) {
4692
+ 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.`);
4693
+ }
4606
4694
  // Build and return the `ProvingRequest`.
4607
4695
  return await ProgramManager$1.buildProvingRequest(executionPrivateKey, program, functionName, inputs, baseFee, priorityFee, feeRecord, imports, broadcast, unchecked, edition);
4608
4696
  }
@@ -4825,9 +4913,20 @@ class ProgramManager {
4825
4913
  const [joinProvingKey, joinVerifyingKey] = joinKeys;
4826
4914
  // Get the fee record from the account if it is not provided in the parameters
4827
4915
  try {
4828
- feeRecord = privateFee
4829
- ? RecordPlaintext.fromString((await this.getCreditsRecord(priorityFee, [], feeRecord, recordSearchParams)).record_plaintext ?? '')
4830
- : undefined;
4916
+ if (privateFee) {
4917
+ let fee = priorityFee;
4918
+ // If a fee record wasn't provided, estimate the fee that needs to be paid.
4919
+ if (!feeRecord) {
4920
+ const baseFee = Number(await this.estimateExecutionFee({ programName: "credits.aleo", functionName: "join" }));
4921
+ fee = baseFee + priorityFee;
4922
+ }
4923
+ // Get a credits.aleo record for the fee.
4924
+ feeRecord = await this.getCreditsRecord(fee, [], feeRecord, recordSearchParams);
4925
+ }
4926
+ else {
4927
+ // If it's specified NOT to use a privateFee, use a public fee.
4928
+ feeRecord = undefined;
4929
+ }
4831
4930
  }
4832
4931
  catch (e) {
4833
4932
  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.`);
@@ -5040,15 +5139,20 @@ class ProgramManager {
5040
5139
  const nonces = [];
5041
5140
  if (requiresAmountRecord(transferType)) {
5042
5141
  // If the transfer type is private and requires an amount record, get it from the record provider
5043
- amountRecord = RecordPlaintext.fromString((await this.getCreditsRecord(priorityFee, [], amountRecord, recordSearchParams)).record_plaintext ?? '');
5142
+ amountRecord = await this.getCreditsRecord(priorityFee, [], amountRecord, recordSearchParams);
5044
5143
  nonces.push(amountRecord.nonce());
5045
5144
  }
5046
5145
  else {
5047
5146
  amountRecord = undefined;
5048
5147
  }
5049
- feeRecord = privateFee
5050
- ? RecordPlaintext.fromString((await this.getCreditsRecord(priorityFee, nonces, feeRecord, recordSearchParams)).record_plaintext ?? '')
5051
- : undefined;
5148
+ if (privateFee) {
5149
+ // Get a credits.aleo record for the fee.
5150
+ feeRecord = await this.getCreditsRecord(priorityFee, [], feeRecord, recordSearchParams);
5151
+ }
5152
+ else {
5153
+ // If it's specified NOT to use a privateFee, use a public fee.
5154
+ feeRecord = undefined;
5155
+ }
5052
5156
  }
5053
5157
  catch (e) {
5054
5158
  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.`);
@@ -5875,26 +5979,29 @@ class ProgramManager {
5875
5979
  }
5876
5980
  // Internal utility function for getting a credits.aleo record
5877
5981
  async getCreditsRecord(amount, nonces, record, params) {
5878
- try {
5879
- // return record instanceof RecordPlaintext
5880
- // ? record
5881
- // : RecordPlaintext.fromString(<string>record);
5882
- if (record && record instanceof RecordPlaintext) {
5883
- record = record.toString();
5982
+ if (record) {
5983
+ try {
5984
+ return record instanceof RecordPlaintext
5985
+ ? record : RecordPlaintext.fromString(record);
5986
+ }
5987
+ catch {
5988
+ logAndThrow(`Record '${record}' could not be parsed, please ensure a valid credits.aleo record
5989
+ is passed prior to trying again`);
5884
5990
  }
5885
- return ({
5886
- recordPlaintext: record,
5887
- programName: 'credits.aleo',
5888
- recordName: 'credits',
5889
- });
5890
5991
  }
5891
- catch (e) {
5992
+ else {
5892
5993
  try {
5893
5994
  const recordProvider = this.recordProvider;
5894
- return await recordProvider.findCreditsRecord(amount, { ...params, unspent: true, nonces });
5995
+ const record = await recordProvider.findCreditsRecord(amount, { ...params, unspent: true, nonces });
5996
+ if (record.record_plaintext) {
5997
+ return RecordPlaintext.fromString(record.record_plaintext);
5998
+ }
5999
+ else {
6000
+ logAndThrow("Failed to deserialize record returned from record provider");
6001
+ }
5895
6002
  }
5896
6003
  catch (e) {
5897
- 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.`);
6004
+ logAndThrow(`Error finding fee record. Record finder response: '${e}'. Please ensure you're connected to a valid Aleo network and a record with enough balance exists.`);
5898
6005
  }
5899
6006
  }
5900
6007
  }