@provablehq/sdk 0.8.1 → 0.8.4

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.
Files changed (108) hide show
  1. package/README.md +44 -928
  2. package/dist/mainnet/account.d.ts +140 -0
  3. package/dist/mainnet/browser.d.ts +37 -0
  4. package/dist/mainnet/browser.js +994 -0
  5. package/dist/mainnet/browser.js.map +1 -0
  6. package/dist/mainnet/constants.d.ts +34 -0
  7. package/dist/mainnet/function-key-provider.d.ts +349 -0
  8. package/dist/mainnet/managed-worker.d.ts +3 -0
  9. package/dist/mainnet/models/blockJSON.d.ts +33 -0
  10. package/dist/mainnet/models/confirmed_transaction.d.ts +9 -0
  11. package/dist/mainnet/models/deployment/deploymentJSON.d.ts +6 -0
  12. package/dist/mainnet/models/deployment/deploymentObject.d.ts +6 -0
  13. package/dist/mainnet/models/execution/executionJSON.d.ts +11 -0
  14. package/dist/mainnet/models/execution/executionObject.d.ts +11 -0
  15. package/dist/mainnet/models/finalizeJSON.d.ts +6 -0
  16. package/dist/mainnet/models/functionObject.d.ts +8 -0
  17. package/dist/mainnet/models/input/inputJSON.d.ts +9 -0
  18. package/dist/mainnet/models/input/inputObject.d.ts +14 -0
  19. package/dist/mainnet/models/output/outputJSON.d.ts +6 -0
  20. package/dist/mainnet/models/output/outputObject.d.ts +17 -0
  21. package/dist/mainnet/models/owner/ownerJSON.d.ts +4 -0
  22. package/dist/mainnet/models/owner/ownerObject.d.ts +5 -0
  23. package/dist/mainnet/models/plaintext/array.d.ts +3 -0
  24. package/dist/mainnet/models/plaintext/literal.d.ts +1 -0
  25. package/dist/mainnet/models/plaintext/plaintext.d.ts +5 -0
  26. package/dist/mainnet/models/plaintext/struct.d.ts +5 -0
  27. package/dist/mainnet/models/ratification.d.ts +4 -0
  28. package/dist/mainnet/models/solution.d.ts +14 -0
  29. package/dist/mainnet/models/transaction/transactionJSON.d.ts +11 -0
  30. package/dist/mainnet/models/transaction/transactionObject.d.ts +14 -0
  31. package/dist/mainnet/models/transition/transitionJSON.d.ts +13 -0
  32. package/dist/mainnet/models/transition/transitionObject.d.ts +15 -0
  33. package/dist/mainnet/network-client.d.ts +396 -0
  34. package/dist/mainnet/node-polyfill.d.ts +5 -0
  35. package/dist/mainnet/node-polyfill.js +261 -0
  36. package/dist/mainnet/node-polyfill.js.map +1 -0
  37. package/dist/mainnet/node.d.ts +2 -0
  38. package/dist/mainnet/node.js +14 -0
  39. package/dist/mainnet/node.js.map +1 -0
  40. package/dist/mainnet/offline-key-provider.d.ts +348 -0
  41. package/dist/mainnet/polyfill/crypto.d.ts +1 -0
  42. package/dist/mainnet/polyfill/fetch.d.ts +1 -0
  43. package/dist/mainnet/polyfill/shared.d.ts +1 -0
  44. package/dist/mainnet/polyfill/worker.d.ts +1 -0
  45. package/dist/mainnet/polyfill/xmlhttprequest.d.ts +1 -0
  46. package/dist/mainnet/program-manager-DUAbZEGO.js +2434 -0
  47. package/dist/mainnet/program-manager-DUAbZEGO.js.map +1 -0
  48. package/dist/mainnet/program-manager.d.ts +669 -0
  49. package/dist/mainnet/record-provider.d.ts +236 -0
  50. package/dist/mainnet/utils.d.ts +4 -0
  51. package/dist/mainnet/wasm.d.ts +1 -0
  52. package/dist/mainnet/worker.d.ts +9 -0
  53. package/dist/mainnet/worker.js +78 -0
  54. package/dist/mainnet/worker.js.map +1 -0
  55. package/dist/testnet/account.d.ts +140 -0
  56. package/dist/testnet/browser.d.ts +37 -0
  57. package/dist/testnet/browser.js +994 -0
  58. package/dist/testnet/browser.js.map +1 -0
  59. package/dist/testnet/constants.d.ts +34 -0
  60. package/dist/testnet/function-key-provider.d.ts +349 -0
  61. package/dist/testnet/managed-worker.d.ts +3 -0
  62. package/dist/testnet/models/blockJSON.d.ts +33 -0
  63. package/dist/testnet/models/confirmed_transaction.d.ts +9 -0
  64. package/dist/testnet/models/deployment/deploymentJSON.d.ts +6 -0
  65. package/dist/testnet/models/deployment/deploymentObject.d.ts +6 -0
  66. package/dist/testnet/models/execution/executionJSON.d.ts +11 -0
  67. package/dist/testnet/models/execution/executionObject.d.ts +11 -0
  68. package/dist/testnet/models/finalizeJSON.d.ts +6 -0
  69. package/dist/testnet/models/functionObject.d.ts +8 -0
  70. package/dist/testnet/models/input/inputJSON.d.ts +9 -0
  71. package/dist/testnet/models/input/inputObject.d.ts +14 -0
  72. package/dist/testnet/models/output/outputJSON.d.ts +6 -0
  73. package/dist/testnet/models/output/outputObject.d.ts +17 -0
  74. package/dist/testnet/models/owner/ownerJSON.d.ts +4 -0
  75. package/dist/testnet/models/owner/ownerObject.d.ts +5 -0
  76. package/dist/testnet/models/plaintext/array.d.ts +3 -0
  77. package/dist/testnet/models/plaintext/literal.d.ts +1 -0
  78. package/dist/testnet/models/plaintext/plaintext.d.ts +5 -0
  79. package/dist/testnet/models/plaintext/struct.d.ts +5 -0
  80. package/dist/testnet/models/ratification.d.ts +4 -0
  81. package/dist/testnet/models/solution.d.ts +14 -0
  82. package/dist/testnet/models/transaction/transactionJSON.d.ts +11 -0
  83. package/dist/testnet/models/transaction/transactionObject.d.ts +14 -0
  84. package/dist/testnet/models/transition/transitionJSON.d.ts +13 -0
  85. package/dist/testnet/models/transition/transitionObject.d.ts +15 -0
  86. package/dist/testnet/network-client.d.ts +396 -0
  87. package/dist/testnet/node-polyfill.d.ts +5 -0
  88. package/dist/testnet/node-polyfill.js +261 -0
  89. package/dist/testnet/node-polyfill.js.map +1 -0
  90. package/dist/testnet/node.d.ts +2 -0
  91. package/dist/testnet/node.js +14 -0
  92. package/dist/testnet/node.js.map +1 -0
  93. package/dist/testnet/offline-key-provider.d.ts +348 -0
  94. package/dist/testnet/polyfill/crypto.d.ts +1 -0
  95. package/dist/testnet/polyfill/fetch.d.ts +1 -0
  96. package/dist/testnet/polyfill/shared.d.ts +1 -0
  97. package/dist/testnet/polyfill/worker.d.ts +1 -0
  98. package/dist/testnet/polyfill/xmlhttprequest.d.ts +1 -0
  99. package/dist/testnet/program-manager-B2GtLTAh.js +2434 -0
  100. package/dist/testnet/program-manager-B2GtLTAh.js.map +1 -0
  101. package/dist/testnet/program-manager.d.ts +669 -0
  102. package/dist/testnet/record-provider.d.ts +236 -0
  103. package/dist/testnet/utils.d.ts +4 -0
  104. package/dist/testnet/wasm.d.ts +1 -0
  105. package/dist/testnet/worker.d.ts +9 -0
  106. package/dist/testnet/worker.js +78 -0
  107. package/dist/testnet/worker.js.map +1 -0
  108. package/package.json +2 -2
@@ -0,0 +1,994 @@
1
+ import 'core-js/proposals/json-parse-with-source.js';
2
+ import { ViewKey, ComputeKey, Address, PrivateKeyCiphertext, PrivateKey, RecordCiphertext, ProvingKey, VerifyingKey } from '@provablehq/wasm/testnet.js';
3
+ export { Address, Ciphertext, ComputeKey, ExecutionResponse, Field, Execution as FunctionExecution, Group, OfflineQuery, Plaintext, PrivateKey, PrivateKeyCiphertext, Program, ProgramManager as ProgramManagerBase, ProvingKey, RecordCiphertext, RecordPlaintext, Scalar, Signature, Transaction, Transition, VerifyingKey, ViewKey, initThreadPool, verifyFunctionExecution } from '@provablehq/wasm/testnet.js';
4
+ import { C as CREDITS_PROGRAM_KEYS, c as PRIVATE_TRANSFER, d as PRIVATE_TO_PUBLIC_TRANSFER, f as PUBLIC_TRANSFER, g as PUBLIC_TRANSFER_AS_SIGNER, h as PUBLIC_TO_PRIVATE_TRANSFER, l as logAndThrow } from './program-manager-B2GtLTAh.js';
5
+ export { A as AleoKeyProvider, a as AleoKeyProviderParams, b as AleoNetworkClient, K as KEY_STORE, e as PRIVATE_TRANSFER_TYPES, P as ProgramManager, V as VALID_TRANSFER_TYPES } from './program-manager-B2GtLTAh.js';
6
+ import { wrap } from 'comlink';
7
+
8
+ /**
9
+ * Key Management class. Enables the creation of a new Aleo Account, importation of an existing account from
10
+ * an existing private key or seed, and message signing and verification functionality.
11
+ *
12
+ * An Aleo Account is generated from a randomly generated seed (number) from which an account private key, view key,
13
+ * and a public account address are derived. The private key lies at the root of an Aleo account. It is a highly
14
+ * sensitive secret and should be protected as it allows for creation of Aleo Program executions and arbitrary value
15
+ * transfers. The View Key allows for decryption of a user's activity on the blockchain. The Address is the public
16
+ * address to which other users of Aleo can send Aleo credits and other records to. This class should only be used
17
+ * environments where the safety of the underlying key material can be assured.
18
+ *
19
+ * @example
20
+ * // Create a new account
21
+ * const myRandomAccount = new Account();
22
+ *
23
+ * // Create an account from a randomly generated seed
24
+ * const seed = new Uint8Array([94, 91, 52, 251, 240, 230, 226, 35, 117, 253, 224, 210, 175, 13, 205, 120, 155, 214, 7, 169, 66, 62, 206, 50, 188, 40, 29, 122, 40, 250, 54, 18]);
25
+ * const mySeededAccount = new Account({seed: seed});
26
+ *
27
+ * // Create an account from an existing private key
28
+ * const myExistingAccount = new Account({privateKey: 'myExistingPrivateKey'})
29
+ *
30
+ * // Sign a message
31
+ * const hello_world = Uint8Array.from([104, 101, 108, 108, 111 119, 111, 114, 108, 100])
32
+ * const signature = myRandomAccount.sign(hello_world)
33
+ *
34
+ * // Verify a signature
35
+ * myRandomAccount.verify(hello_world, signature)
36
+ */
37
+ class Account {
38
+ _privateKey;
39
+ _viewKey;
40
+ _computeKey;
41
+ _address;
42
+ constructor(params = {}) {
43
+ try {
44
+ this._privateKey = this.privateKeyFromParams(params);
45
+ }
46
+ catch (e) {
47
+ console.error("Wrong parameter", e);
48
+ throw new Error("Wrong Parameter");
49
+ }
50
+ this._viewKey = ViewKey.from_private_key(this._privateKey);
51
+ this._computeKey = ComputeKey.from_private_key(this._privateKey);
52
+ this._address = Address.from_private_key(this._privateKey);
53
+ }
54
+ /**
55
+ * Attempts to create an account from a private key ciphertext
56
+ * @param {PrivateKeyCiphertext | string} ciphertext
57
+ * @param {string} password
58
+ * @returns {PrivateKey}
59
+ *
60
+ * @example
61
+ * const ciphertext = PrivateKey.newEncrypted("password");
62
+ * const account = Account.fromCiphertext(ciphertext, "password");
63
+ */
64
+ static fromCiphertext(ciphertext, password) {
65
+ try {
66
+ ciphertext = (typeof ciphertext === "string") ? PrivateKeyCiphertext.fromString(ciphertext) : ciphertext;
67
+ const _privateKey = PrivateKey.fromPrivateKeyCiphertext(ciphertext, password);
68
+ return new Account({ privateKey: _privateKey.to_string() });
69
+ }
70
+ catch (e) {
71
+ throw new Error("Wrong password or invalid ciphertext");
72
+ }
73
+ }
74
+ privateKeyFromParams(params) {
75
+ if (params.seed) {
76
+ return PrivateKey.from_seed_unchecked(params.seed);
77
+ }
78
+ if (params.privateKey) {
79
+ return PrivateKey.from_string(params.privateKey);
80
+ }
81
+ return new PrivateKey();
82
+ }
83
+ privateKey() {
84
+ return this._privateKey;
85
+ }
86
+ viewKey() {
87
+ return this._viewKey;
88
+ }
89
+ computeKey() {
90
+ return this._computeKey;
91
+ }
92
+ address() {
93
+ return this._address;
94
+ }
95
+ clone() {
96
+ return new Account({ privateKey: this._privateKey.to_string() });
97
+ }
98
+ toString() {
99
+ return this.address().to_string();
100
+ }
101
+ /**
102
+ * Encrypt the account's private key with a password
103
+ * @param {string} ciphertext
104
+ * @returns {PrivateKeyCiphertext}
105
+ *
106
+ * @example
107
+ * const account = new Account();
108
+ * const ciphertext = account.encryptAccount("password");
109
+ */
110
+ encryptAccount(password) {
111
+ return this._privateKey.toCiphertext(password);
112
+ }
113
+ /**
114
+ * Decrypts a Record in ciphertext form into plaintext
115
+ * @param {string} ciphertext
116
+ * @returns {Record}
117
+ *
118
+ * @example
119
+ * const account = new Account();
120
+ * const record = account.decryptRecord("record1ciphertext");
121
+ */
122
+ decryptRecord(ciphertext) {
123
+ return this._viewKey.decrypt(ciphertext);
124
+ }
125
+ /**
126
+ * Decrypts an array of Records in ciphertext form into plaintext
127
+ * @param {string[]} ciphertexts
128
+ * @returns {Record[]}
129
+ *
130
+ * @example
131
+ * const account = new Account();
132
+ * const record = account.decryptRecords(["record1ciphertext", "record2ciphertext"]);
133
+ */
134
+ decryptRecords(ciphertexts) {
135
+ return ciphertexts.map((ciphertext) => this._viewKey.decrypt(ciphertext));
136
+ }
137
+ /**
138
+ * Determines whether the account owns a ciphertext record
139
+ * @param {RecordCipherText | string} ciphertext
140
+ * @returns {boolean}
141
+ *
142
+ * @example
143
+ * // Create a connection to the Aleo network and an account
144
+ * const connection = new AleoNetworkClient("https://api.explorer.provable.com/v1");
145
+ * const account = Account.fromCiphertext("ciphertext", "password");
146
+ *
147
+ * // Get a record from the network
148
+ * const record = connection.getBlock(1234);
149
+ * const recordCipherText = record.transactions[0].execution.transitions[0].id;
150
+ *
151
+ * // Check if the account owns the record
152
+ * if account.ownsRecord(recordCipherText) {
153
+ * // Then one can do something like:
154
+ * // Decrypt the record and check if it's spent
155
+ * // Store the record in a local database
156
+ * // Etc.
157
+ * }
158
+ */
159
+ ownsRecordCiphertext(ciphertext) {
160
+ if (typeof ciphertext === 'string') {
161
+ try {
162
+ const ciphertextObject = RecordCiphertext.fromString(ciphertext);
163
+ return ciphertextObject.isOwner(this._viewKey);
164
+ }
165
+ catch (e) {
166
+ return false;
167
+ }
168
+ }
169
+ else {
170
+ return ciphertext.isOwner(this._viewKey);
171
+ }
172
+ }
173
+ /**
174
+ * Signs a message with the account's private key.
175
+ * Returns a Signature.
176
+ *
177
+ * @param {Uint8Array} message
178
+ * @returns {Signature}
179
+ *
180
+ * @example
181
+ * const account = new Account();
182
+ * const message = Uint8Array.from([104, 101, 108, 108, 111 119, 111, 114, 108, 100])
183
+ * account.sign(message);
184
+ */
185
+ sign(message) {
186
+ return this._privateKey.sign(message);
187
+ }
188
+ /**
189
+ * Verifies the Signature on a message.
190
+ *
191
+ * @param {Uint8Array} message
192
+ * @param {Signature} signature
193
+ * @returns {boolean}
194
+ *
195
+ * @example
196
+ * const account = new Account();
197
+ * const message = Uint8Array.from([104, 101, 108, 108, 111 119, 111, 114, 108, 100])
198
+ * const signature = account.sign(message);
199
+ * account.verify(message, signature);
200
+ */
201
+ verify(message, signature) {
202
+ return this._address.verify(message, signature);
203
+ }
204
+ }
205
+
206
+ /**
207
+ * Search parameters for the offline key provider. This class implements the KeySearchParams interface and includes
208
+ * a convenience method for creating a new instance of this class for each function of the credits.aleo program.
209
+ *
210
+ * @example
211
+ * // If storing a key for a custom program function
212
+ * offlineSearchParams = new OfflineSearchParams("myprogram.aleo/myfunction");
213
+ *
214
+ * // If storing a key for a credits.aleo program function
215
+ * bondPublicKeyParams = OfflineSearchParams.bondPublicKeyParams();
216
+ */
217
+ class OfflineSearchParams {
218
+ cacheKey;
219
+ verifyCreditsKeys;
220
+ /**
221
+ * Create a new OfflineSearchParams instance.
222
+ *
223
+ * @param {string} cacheKey - Key used to store the local function proving & verifying keys. This should be stored
224
+ * under the naming convention "programName/functionName" (i.e. "myprogram.aleo/myfunction")
225
+ * @param {boolean} verifyCreditsKeys - Whether to verify the keys against the credits.aleo program,
226
+ * defaults to false, but should be set to true if using keys from the credits.aleo program
227
+ */
228
+ constructor(cacheKey, verifyCreditsKeys = false) {
229
+ this.cacheKey = cacheKey;
230
+ this.verifyCreditsKeys = verifyCreditsKeys;
231
+ }
232
+ /**
233
+ * Create a new OfflineSearchParams instance for the bond_public function of the credits.aleo program.
234
+ */
235
+ static bondPublicKeyParams() {
236
+ return new OfflineSearchParams(CREDITS_PROGRAM_KEYS.bond_public.locator, true);
237
+ }
238
+ /**
239
+ * Create a new OfflineSearchParams instance for the bond_validator function of the credits.aleo program.
240
+ */
241
+ static bondValidatorKeyParams() {
242
+ return new OfflineSearchParams(CREDITS_PROGRAM_KEYS.bond_validator.locator, true);
243
+ }
244
+ /**
245
+ * Create a new OfflineSearchParams instance for the claim_unbond_public function of the
246
+ */
247
+ static claimUnbondPublicKeyParams() {
248
+ return new OfflineSearchParams(CREDITS_PROGRAM_KEYS.claim_unbond_public.locator, true);
249
+ }
250
+ /**
251
+ * Create a new OfflineSearchParams instance for the fee_private function of the credits.aleo program.
252
+ */
253
+ static feePrivateKeyParams() {
254
+ return new OfflineSearchParams(CREDITS_PROGRAM_KEYS.fee_private.locator, true);
255
+ }
256
+ /**
257
+ * Create a new OfflineSearchParams instance for the fee_public function of the credits.aleo program.
258
+ */
259
+ static feePublicKeyParams() {
260
+ return new OfflineSearchParams(CREDITS_PROGRAM_KEYS.fee_public.locator, true);
261
+ }
262
+ /**
263
+ * Create a new OfflineSearchParams instance for the inclusion prover function.
264
+ */
265
+ static inclusionKeyParams() {
266
+ return new OfflineSearchParams(CREDITS_PROGRAM_KEYS.inclusion.locator, true);
267
+ }
268
+ /**
269
+ * Create a new OfflineSearchParams instance for the join function of the credits.aleo program.
270
+ */
271
+ static joinKeyParams() {
272
+ return new OfflineSearchParams(CREDITS_PROGRAM_KEYS.join.locator, true);
273
+ }
274
+ /**
275
+ * Create a new OfflineSearchParams instance for the set_validator_state function of the credits.aleo program.
276
+ */
277
+ static setValidatorStateKeyParams() {
278
+ return new OfflineSearchParams(CREDITS_PROGRAM_KEYS.set_validator_state.locator, true);
279
+ }
280
+ /**
281
+ * Create a new OfflineSearchParams instance for the split function of the credits.aleo program.
282
+ */
283
+ static splitKeyParams() {
284
+ return new OfflineSearchParams(CREDITS_PROGRAM_KEYS.split.locator, true);
285
+ }
286
+ /**
287
+ * Create a new OfflineSearchParams instance for the transfer_private function of the credits.aleo program.
288
+ */
289
+ static transferPrivateKeyParams() {
290
+ return new OfflineSearchParams(CREDITS_PROGRAM_KEYS.transfer_private.locator, true);
291
+ }
292
+ /**
293
+ * Create a new OfflineSearchParams instance for the transfer_private_to_public function of the credits.aleo program.
294
+ */
295
+ static transferPrivateToPublicKeyParams() {
296
+ return new OfflineSearchParams(CREDITS_PROGRAM_KEYS.transfer_private_to_public.locator, true);
297
+ }
298
+ /**
299
+ * Create a new OfflineSearchParams instance for the transfer_public function of the credits.aleo program.
300
+ */
301
+ static transferPublicKeyParams() {
302
+ return new OfflineSearchParams(CREDITS_PROGRAM_KEYS.transfer_public.locator, true);
303
+ }
304
+ /**
305
+ * Create a new OfflineSearchParams instance for the transfer_public_as_signer function of the credits.aleo program.
306
+ */
307
+ static transferPublicAsSignerKeyParams() {
308
+ return new OfflineSearchParams(CREDITS_PROGRAM_KEYS.transfer_public_as_signer.locator, true);
309
+ }
310
+ /**
311
+ * Create a new OfflineSearchParams instance for the transfer_public_to_private function of the credits.aleo program.
312
+ */
313
+ static transferPublicToPrivateKeyParams() {
314
+ return new OfflineSearchParams(CREDITS_PROGRAM_KEYS.transfer_public_to_private.locator, true);
315
+ }
316
+ /**
317
+ * Create a new OfflineSearchParams instance for the unbond_public function of the credits.aleo program.
318
+ */
319
+ static unbondPublicKeyParams() {
320
+ return new OfflineSearchParams(CREDITS_PROGRAM_KEYS.unbond_public.locator, true);
321
+ }
322
+ }
323
+ /**
324
+ * A key provider meant for building transactions offline on devices such as hardware wallets. This key provider is not
325
+ * able to contact the internet for key material and instead relies on the user to insert Aleo function proving &
326
+ * verifying keys from local storage prior to usage.
327
+ *
328
+ * @example
329
+ * // Create an offline program manager
330
+ * const programManager = new ProgramManager();
331
+ *
332
+ * // Create a temporary account for the execution of the program
333
+ * const account = new Account();
334
+ * programManager.setAccount(account);
335
+ *
336
+ * // Create the proving keys from the key bytes on the offline machine
337
+ * console.log("Creating proving keys from local key files");
338
+ * const program = "program hello_hello.aleo; function hello: input r0 as u32.public; input r1 as u32.private; add r0 r1 into r2; output r2 as u32.private;";
339
+ * const myFunctionProver = await getLocalKey("/path/to/my/function/hello_hello.prover");
340
+ * const myFunctionVerifier = await getLocalKey("/path/to/my/function/hello_hello.verifier");
341
+ * const feePublicProvingKeyBytes = await getLocalKey("/path/to/credits.aleo/feePublic.prover");
342
+ *
343
+ * myFunctionProvingKey = ProvingKey.fromBytes(myFunctionProver);
344
+ * myFunctionVerifyingKey = VerifyingKey.fromBytes(myFunctionVerifier);
345
+ * const feePublicProvingKey = ProvingKey.fromBytes(feePublicKeyBytes);
346
+ *
347
+ * // Create an offline key provider
348
+ * console.log("Creating offline key provider");
349
+ * const offlineKeyProvider = new OfflineKeyProvider();
350
+ *
351
+ * // Cache the keys
352
+ * // Cache the proving and verifying keys for the custom hello function
353
+ * OfflineKeyProvider.cacheKeys("hello_hello.aleo/hello", myFunctionProvingKey, myFunctionVerifyingKey);
354
+ *
355
+ * // Cache the proving key for the fee_public function (the verifying key is automatically cached)
356
+ * OfflineKeyProvider.insertFeePublicKey(feePublicProvingKey);
357
+ *
358
+ * // Create an offline query using the latest state root in order to create the inclusion proof
359
+ * const offlineQuery = new OfflineQuery("latestStateRoot");
360
+ *
361
+ * // Insert the key provider into the program manager
362
+ * programManager.setKeyProvider(offlineKeyProvider);
363
+ *
364
+ * // Create the offline search params
365
+ * const offlineSearchParams = new OfflineSearchParams("hello_hello.aleo/hello");
366
+ *
367
+ * // Create the offline transaction
368
+ * const offlineExecuteTx = <Transaction>await this.buildExecutionTransaction("hello_hello.aleo", "hello", 1, false, ["5u32", "5u32"], undefined, offlineSearchParams, undefined, undefined, undefined, undefined, offlineQuery, program);
369
+ *
370
+ * // Broadcast the transaction later on a machine with internet access
371
+ * const networkClient = new AleoNetworkClient("https://api.explorer.provable.com/v1");
372
+ * const txId = await networkClient.broadcastTransaction(offlineExecuteTx);
373
+ */
374
+ class OfflineKeyProvider {
375
+ cache;
376
+ constructor() {
377
+ this.cache = new Map();
378
+ }
379
+ /**
380
+ * Get bond_public function keys from the credits.aleo program. The keys must be cached prior to calling this
381
+ * method for it to work.
382
+ *
383
+ * @returns {Promise<FunctionKeyPair>} Proving and verifying keys for the bond_public function
384
+ */
385
+ bondPublicKeys() {
386
+ return this.functionKeys(OfflineSearchParams.bondPublicKeyParams());
387
+ }
388
+ ;
389
+ /**
390
+ * Get bond_validator function keys from the credits.aleo program. The keys must be cached prior to calling this
391
+ * method for it to work.
392
+ *
393
+ * @returns {Promise<FunctionKeyPair>} Proving and verifying keys for the bond_public function
394
+ */
395
+ bondValidatorKeys() {
396
+ return this.functionKeys(OfflineSearchParams.bondValidatorKeyParams());
397
+ }
398
+ ;
399
+ /**
400
+ * Cache a set of keys. This will overwrite any existing keys with the same keyId. The user can check if a keyId
401
+ * exists in the cache using the containsKeys method prior to calling this method if overwriting is not desired.
402
+ *
403
+ * @param {string} keyId access key for the cache
404
+ * @param {FunctionKeyPair} keys keys to cache
405
+ */
406
+ cacheKeys(keyId, keys) {
407
+ const [provingKey, verifyingKey] = keys;
408
+ this.cache.set(keyId, [provingKey.toBytes(), verifyingKey.toBytes()]);
409
+ }
410
+ ;
411
+ /**
412
+ * Get unbond_public function keys from the credits.aleo program. The keys must be cached prior to calling this
413
+ * method for it to work.
414
+ *
415
+ * @returns {Promise<FunctionKeyPair>} Proving and verifying keys for the unbond_public function
416
+ */
417
+ claimUnbondPublicKeys() {
418
+ return this.functionKeys(OfflineSearchParams.claimUnbondPublicKeyParams());
419
+ }
420
+ ;
421
+ /**
422
+ * Get arbitrary function key from the offline key provider cache.
423
+ *
424
+ * @param {KeySearchParams | undefined} params - Optional search parameters for the key provider
425
+ * @returns {Promise<FunctionKeyPair>} Proving and verifying keys for the specified program
426
+ *
427
+ * @example
428
+ * /// First cache the keys from local offline resources
429
+ * const offlineKeyProvider = new OfflineKeyProvider();
430
+ * const myFunctionVerifyingKey = VerifyingKey.fromString("verifier...");
431
+ * const myFunctionProvingKeyBytes = await readBinaryFile('./resources/myfunction.prover');
432
+ * const myFunctionProvingKey = ProvingKey.fromBytes(myFunctionProvingKeyBytes);
433
+ *
434
+ * /// Cache the keys for future use with a memorable locator
435
+ * offlineKeyProvider.cacheKeys("myprogram.aleo/myfunction", [myFunctionProvingKey, myFunctionVerifyingKey]);
436
+ *
437
+ * /// When they're needed, retrieve the keys from the cache
438
+ *
439
+ * /// First create a search parameter object with the same locator used to cache the keys
440
+ * const keyParams = new OfflineSearchParams("myprogram.aleo/myfunction");
441
+ *
442
+ * /// Then retrieve the keys
443
+ * const [myFunctionProver, myFunctionVerifier] = await offlineKeyProvider.functionKeys(keyParams);
444
+ */
445
+ functionKeys(params) {
446
+ return new Promise((resolve, reject) => {
447
+ if (params === undefined) {
448
+ reject(new Error("No search parameters provided, cannot retrieve keys"));
449
+ }
450
+ else {
451
+ const keyId = params.cacheKey;
452
+ const verifyCreditsKeys = params.verifyCreditsKeys;
453
+ if (this.cache.has(keyId)) {
454
+ const [provingKeyBytes, verifyingKeyBytes] = this.cache.get(keyId);
455
+ const provingKey = ProvingKey.fromBytes(provingKeyBytes);
456
+ const verifyingKey = VerifyingKey.fromBytes(verifyingKeyBytes);
457
+ if (verifyCreditsKeys) {
458
+ const keysMatchExpected = this.verifyCreditsKeys(keyId, provingKey, verifyingKey);
459
+ if (!keysMatchExpected) {
460
+ reject(new Error(`Cached keys do not match expected keys for ${keyId}`));
461
+ }
462
+ }
463
+ resolve([provingKey, verifyingKey]);
464
+ }
465
+ else {
466
+ reject(new Error("Keys not found in cache for " + keyId));
467
+ }
468
+ }
469
+ });
470
+ }
471
+ ;
472
+ /**
473
+ * Determines if the keys for a given credits function match the expected keys.
474
+ *
475
+ * @returns {boolean} Whether the keys match the expected keys
476
+ */
477
+ verifyCreditsKeys(locator, provingKey, verifyingKey) {
478
+ switch (locator) {
479
+ case CREDITS_PROGRAM_KEYS.bond_public.locator:
480
+ return provingKey.isBondPublicProver() && verifyingKey.isBondPublicVerifier();
481
+ case CREDITS_PROGRAM_KEYS.claim_unbond_public.locator:
482
+ return provingKey.isClaimUnbondPublicProver() && verifyingKey.isClaimUnbondPublicVerifier();
483
+ case CREDITS_PROGRAM_KEYS.fee_private.locator:
484
+ return provingKey.isFeePrivateProver() && verifyingKey.isFeePrivateVerifier();
485
+ case CREDITS_PROGRAM_KEYS.fee_public.locator:
486
+ return provingKey.isFeePublicProver() && verifyingKey.isFeePublicVerifier();
487
+ case CREDITS_PROGRAM_KEYS.inclusion.locator:
488
+ return provingKey.isInclusionProver() && verifyingKey.isInclusionVerifier();
489
+ case CREDITS_PROGRAM_KEYS.join.locator:
490
+ return provingKey.isJoinProver() && verifyingKey.isJoinVerifier();
491
+ case CREDITS_PROGRAM_KEYS.set_validator_state.locator:
492
+ return provingKey.isSetValidatorStateProver() && verifyingKey.isSetValidatorStateVerifier();
493
+ case CREDITS_PROGRAM_KEYS.split.locator:
494
+ return provingKey.isSplitProver() && verifyingKey.isSplitVerifier();
495
+ case CREDITS_PROGRAM_KEYS.transfer_private.locator:
496
+ return provingKey.isTransferPrivateProver() && verifyingKey.isTransferPrivateVerifier();
497
+ case CREDITS_PROGRAM_KEYS.transfer_private_to_public.locator:
498
+ return provingKey.isTransferPrivateToPublicProver() && verifyingKey.isTransferPrivateToPublicVerifier();
499
+ case CREDITS_PROGRAM_KEYS.transfer_public.locator:
500
+ return provingKey.isTransferPublicProver() && verifyingKey.isTransferPublicVerifier();
501
+ case CREDITS_PROGRAM_KEYS.transfer_public_to_private.locator:
502
+ return provingKey.isTransferPublicToPrivateProver() && verifyingKey.isTransferPublicToPrivateVerifier();
503
+ case CREDITS_PROGRAM_KEYS.unbond_public.locator:
504
+ return provingKey.isUnbondPublicProver() && verifyingKey.isUnbondPublicVerifier();
505
+ default:
506
+ return false;
507
+ }
508
+ }
509
+ /**
510
+ * Get fee_private function keys from the credits.aleo program. The keys must be cached prior to calling this
511
+ * method for it to work.
512
+ *
513
+ * @returns {Promise<FunctionKeyPair>} Proving and verifying keys for the join function
514
+ */
515
+ feePrivateKeys() {
516
+ return this.functionKeys(OfflineSearchParams.feePrivateKeyParams());
517
+ }
518
+ ;
519
+ /**
520
+ * Get fee_public function keys from the credits.aleo program. The keys must be cached prior to calling this
521
+ * method for it to work.
522
+ *
523
+ * @returns {Promise<FunctionKeyPair>} Proving and verifying keys for the join function
524
+ */
525
+ feePublicKeys() {
526
+ return this.functionKeys(OfflineSearchParams.feePublicKeyParams());
527
+ }
528
+ ;
529
+ /**
530
+ * Get join function keys from the credits.aleo program. The keys must be cached prior to calling this
531
+ * method for it to work.
532
+ *
533
+ * @returns {Promise<FunctionKeyPair>} Proving and verifying keys for the join function
534
+ */
535
+ joinKeys() {
536
+ return this.functionKeys(OfflineSearchParams.joinKeyParams());
537
+ }
538
+ ;
539
+ /**
540
+ * Get split function keys from the credits.aleo program. The keys must be cached prior to calling this
541
+ * method for it to work.
542
+ *
543
+ * @returns {Promise<FunctionKeyPair>} Proving and verifying keys for the join function
544
+ */
545
+ splitKeys() {
546
+ return this.functionKeys(OfflineSearchParams.splitKeyParams());
547
+ }
548
+ ;
549
+ /**
550
+ * Get keys for a variant of the transfer function from the credits.aleo program.
551
+ *
552
+ *
553
+ * @param {string} visibility Visibility of the transfer function (private, public, privateToPublic, publicToPrivate)
554
+ * @returns {Promise<FunctionKeyPair>} Proving and verifying keys for the specified transfer function
555
+ *
556
+ * @example
557
+ * // Create a new OfflineKeyProvider
558
+ * const offlineKeyProvider = new OfflineKeyProvider();
559
+ *
560
+ * // Cache the keys for future use with the official locator
561
+ * const transferPublicProvingKeyBytes = await readBinaryFile('./resources/transfer_public.prover.a74565e');
562
+ * const transferPublicProvingKey = ProvingKey.fromBytes(transferPublicProvingKeyBytes);
563
+ *
564
+ * // Cache the transfer_public keys for future use with the OfflinKeyProvider's convenience method for
565
+ * // transfer_public (the verifying key will be cached automatically)
566
+ * offlineKeyProvider.insertTransferPublicKeys(transferPublicProvingKey);
567
+ *
568
+ * /// When they're needed, retrieve the keys from the cache
569
+ * const [transferPublicProvingKey, transferPublicVerifyingKey] = await keyProvider.transferKeys("public");
570
+ */
571
+ transferKeys(visibility) {
572
+ if (PRIVATE_TRANSFER.has(visibility)) {
573
+ return this.functionKeys(OfflineSearchParams.transferPrivateKeyParams());
574
+ }
575
+ else if (PRIVATE_TO_PUBLIC_TRANSFER.has(visibility)) {
576
+ return this.functionKeys(OfflineSearchParams.transferPrivateToPublicKeyParams());
577
+ }
578
+ else if (PUBLIC_TRANSFER.has(visibility)) {
579
+ return this.functionKeys(OfflineSearchParams.transferPublicKeyParams());
580
+ }
581
+ else if (PUBLIC_TRANSFER_AS_SIGNER.has(visibility)) {
582
+ return this.functionKeys(OfflineSearchParams.transferPublicAsSignerKeyParams());
583
+ }
584
+ else if (PUBLIC_TO_PRIVATE_TRANSFER.has(visibility)) {
585
+ return this.functionKeys(OfflineSearchParams.transferPublicToPrivateKeyParams());
586
+ }
587
+ else {
588
+ throw new Error("Invalid visibility type");
589
+ }
590
+ }
591
+ ;
592
+ /**
593
+ * Get unbond_public function keys from the credits.aleo program
594
+ *
595
+ * @returns {Promise<FunctionKeyPair>} Proving and verifying keys for the join function
596
+ */
597
+ async unBondPublicKeys() {
598
+ return this.functionKeys(OfflineSearchParams.unbondPublicKeyParams());
599
+ }
600
+ ;
601
+ /**
602
+ * Insert the proving and verifying keys for the bond_public function into the cache. Only the proving key needs
603
+ * to be inserted, the verifying key is automatically inserted by the SDK. This function will automatically check
604
+ * that the keys match the expected checksum for bond_public before inserting them into the cache.
605
+ *
606
+ * @param provingKey
607
+ */
608
+ insertBondPublicKeys(provingKey) {
609
+ if (provingKey.isBondPublicProver()) {
610
+ this.cache.set(CREDITS_PROGRAM_KEYS.bond_public.locator, [provingKey.toBytes(), VerifyingKey.bondPublicVerifier().toBytes()]);
611
+ }
612
+ else {
613
+ throw new Error("Attempted to insert invalid proving keys for bond_public");
614
+ }
615
+ }
616
+ /**
617
+ * Insert the proving and verifying keys for the claim_unbond_public function into the cache. Only the proving key needs
618
+ * to be inserted, the verifying key is automatically inserted by the SDK. This function will automatically check
619
+ * that the keys match the expected checksum for claim_unbond_public before inserting them into the cache.
620
+ *
621
+ * @param provingKey
622
+ */
623
+ insertClaimUnbondPublicKeys(provingKey) {
624
+ if (provingKey.isClaimUnbondPublicProver()) {
625
+ this.cache.set(CREDITS_PROGRAM_KEYS.claim_unbond_public.locator, [provingKey.toBytes(), VerifyingKey.claimUnbondPublicVerifier().toBytes()]);
626
+ }
627
+ else {
628
+ throw new Error("Attempted to insert invalid proving keys for claim_unbond_public");
629
+ }
630
+ }
631
+ /**
632
+ * Insert the proving and verifying keys for the fee_private function into the cache. Only the proving key needs
633
+ * to be inserted, the verifying key is automatically inserted by the SDK. This function will automatically check
634
+ * that the keys match the expected checksum for fee_private before inserting them into the cache.
635
+ *
636
+ * @param provingKey
637
+ */
638
+ insertFeePrivateKeys(provingKey) {
639
+ if (provingKey.isFeePrivateProver()) {
640
+ this.cache.set(CREDITS_PROGRAM_KEYS.fee_private.locator, [provingKey.toBytes(), VerifyingKey.feePrivateVerifier().toBytes()]);
641
+ }
642
+ else {
643
+ throw new Error("Attempted to insert invalid proving keys for fee_private");
644
+ }
645
+ }
646
+ /**
647
+ * Insert the proving and verifying keys for the fee_public function into the cache. Only the proving key needs
648
+ * to be inserted, the verifying key is automatically inserted by the SDK. This function will automatically check
649
+ * that the keys match the expected checksum for fee_public before inserting them into the cache.
650
+ *
651
+ * @param provingKey
652
+ */
653
+ insertFeePublicKeys(provingKey) {
654
+ if (provingKey.isFeePublicProver()) {
655
+ this.cache.set(CREDITS_PROGRAM_KEYS.fee_public.locator, [provingKey.toBytes(), VerifyingKey.feePublicVerifier().toBytes()]);
656
+ }
657
+ else {
658
+ throw new Error("Attempted to insert invalid proving keys for fee_public");
659
+ }
660
+ }
661
+ /**
662
+ * Insert the proving and verifying keys for the join function into the cache. Only the proving key needs
663
+ * to be inserted, the verifying key is automatically inserted by the SDK. This function will automatically check
664
+ * that the keys match the expected checksum for join before inserting them into the cache.
665
+ *
666
+ * @param provingKey
667
+ */
668
+ insertJoinKeys(provingKey) {
669
+ if (provingKey.isJoinProver()) {
670
+ this.cache.set(CREDITS_PROGRAM_KEYS.join.locator, [provingKey.toBytes(), VerifyingKey.joinVerifier().toBytes()]);
671
+ }
672
+ else {
673
+ throw new Error("Attempted to insert invalid proving keys for join");
674
+ }
675
+ }
676
+ /**
677
+ * Insert the proving and verifying keys for the set_validator_state function into the cache. Only the proving key needs
678
+ * to be inserted, the verifying key is automatically inserted by the SDK. This function will automatically check
679
+ * that the keys match the expected checksum for set_validator_state before inserting them into the cache.
680
+ *
681
+ * @param provingKey
682
+ */
683
+ insertSetValidatorStateKeys(provingKey) {
684
+ if (provingKey.isSetValidatorStateProver()) {
685
+ this.cache.set(CREDITS_PROGRAM_KEYS.set_validator_state.locator, [provingKey.toBytes(), VerifyingKey.setValidatorStateVerifier().toBytes()]);
686
+ }
687
+ else {
688
+ throw new Error("Attempted to insert invalid proving keys for set_validator_state");
689
+ }
690
+ }
691
+ /**
692
+ * Insert the proving and verifying keys for the split function into the cache. Only the proving key needs
693
+ * to be inserted, the verifying key is automatically inserted by the SDK. This function will automatically check
694
+ * that the keys match the expected checksum for split before inserting them into the cache.
695
+ *
696
+ * @param provingKey
697
+ */
698
+ insertSplitKeys(provingKey) {
699
+ if (provingKey.isSplitProver()) {
700
+ this.cache.set(CREDITS_PROGRAM_KEYS.split.locator, [provingKey.toBytes(), VerifyingKey.splitVerifier().toBytes()]);
701
+ }
702
+ else {
703
+ throw new Error("Attempted to insert invalid proving keys for split");
704
+ }
705
+ }
706
+ /**
707
+ * Insert the proving and verifying keys for the transfer_private function into the cache. Only the proving key needs
708
+ * to be inserted, the verifying key is automatically inserted by the SDK. This function will automatically check
709
+ * that the keys match the expected checksum for transfer_private before inserting them into the cache.
710
+ *
711
+ * @param provingKey
712
+ */
713
+ insertTransferPrivateKeys(provingKey) {
714
+ if (provingKey.isTransferPrivateProver()) {
715
+ this.cache.set(CREDITS_PROGRAM_KEYS.transfer_private.locator, [provingKey.toBytes(), VerifyingKey.transferPrivateVerifier().toBytes()]);
716
+ }
717
+ else {
718
+ throw new Error("Attempted to insert invalid proving keys for transfer_private");
719
+ }
720
+ }
721
+ /**
722
+ * Insert the proving and verifying keys for the transfer_private_to_public function into the cache. Only the proving key needs
723
+ * to be inserted, the verifying key is automatically inserted by the SDK. This function will automatically check
724
+ * that the keys match the expected checksum for transfer_private_to_public before inserting them into the cache.
725
+ *
726
+ * @param provingKey
727
+ */
728
+ insertTransferPrivateToPublicKeys(provingKey) {
729
+ if (provingKey.isTransferPrivateToPublicProver()) {
730
+ this.cache.set(CREDITS_PROGRAM_KEYS.transfer_private_to_public.locator, [provingKey.toBytes(), VerifyingKey.transferPrivateToPublicVerifier().toBytes()]);
731
+ }
732
+ else {
733
+ throw new Error("Attempted to insert invalid proving keys for transfer_private_to_public");
734
+ }
735
+ }
736
+ /**
737
+ * Insert the proving and verifying keys for the transfer_public function into the cache. Only the proving key needs
738
+ * to be inserted, the verifying key is automatically inserted by the SDK. This function will automatically check
739
+ * that the keys match the expected checksum for transfer_public before inserting them into the cache.
740
+ *
741
+ * @param provingKey
742
+ */
743
+ insertTransferPublicKeys(provingKey) {
744
+ if (provingKey.isTransferPublicProver()) {
745
+ this.cache.set(CREDITS_PROGRAM_KEYS.transfer_public.locator, [provingKey.toBytes(), VerifyingKey.transferPublicVerifier().toBytes()]);
746
+ }
747
+ else {
748
+ throw new Error("Attempted to insert invalid proving keys for transfer_public");
749
+ }
750
+ }
751
+ /**
752
+ * Insert the proving and verifying keys for the transfer_public_to_private function into the cache. Only the proving key needs
753
+ * to be inserted, the verifying key is automatically inserted by the SDK. This function will automatically check
754
+ * that the keys match the expected checksum for transfer_public_to_private before inserting them into the cache.
755
+ *
756
+ * @param provingKey
757
+ */
758
+ insertTransferPublicToPrivateKeys(provingKey) {
759
+ if (provingKey.isTransferPublicToPrivateProver()) {
760
+ this.cache.set(CREDITS_PROGRAM_KEYS.transfer_public_to_private.locator, [provingKey.toBytes(), VerifyingKey.transferPublicToPrivateVerifier().toBytes()]);
761
+ }
762
+ else {
763
+ throw new Error("Attempted to insert invalid proving keys for transfer_public_to_private");
764
+ }
765
+ }
766
+ insertUnbondPublicKeys(provingKey) {
767
+ if (provingKey.isUnbondPublicProver()) {
768
+ this.cache.set(CREDITS_PROGRAM_KEYS.unbond_public.locator, [provingKey.toBytes(), VerifyingKey.unbondPublicVerifier().toBytes()]);
769
+ }
770
+ else {
771
+ throw new Error("Attempted to insert invalid proving keys for unbond_public");
772
+ }
773
+ }
774
+ }
775
+
776
+ /**
777
+ * A record provider implementation that uses the official Aleo API to find records for usage in program execution and
778
+ * deployment, wallet functionality, and other use cases.
779
+ */
780
+ class NetworkRecordProvider {
781
+ account;
782
+ networkClient;
783
+ constructor(account, networkClient) {
784
+ this.account = account;
785
+ this.networkClient = networkClient;
786
+ }
787
+ /**
788
+ * Set the account used to search for records
789
+ *
790
+ * @param {Account} account The account to use for searching for records
791
+ */
792
+ setAccount(account) {
793
+ this.account = account;
794
+ }
795
+ /**
796
+ * Find a list of credit records with a given number of microcredits by via the official Aleo API
797
+ *
798
+ * @param {number[]} microcredits The number of microcredits to search for
799
+ * @param {boolean} unspent Whether or not the record is unspent
800
+ * @param {string[]} nonces Nonces of records already found so that they are not found again
801
+ * @param {RecordSearchParams} searchParameters Additional parameters to search for
802
+ * @returns {Promise<RecordPlaintext>} The record if found, otherwise an error
803
+ *
804
+ * @example
805
+ * // Create a new NetworkRecordProvider
806
+ * const networkClient = new AleoNetworkClient("https://api.explorer.provable.com/v1");
807
+ * const keyProvider = new AleoKeyProvider();
808
+ * const recordProvider = new NetworkRecordProvider(account, networkClient);
809
+ *
810
+ * // The record provider can be used to find records with a given number of microcredits
811
+ * const record = await recordProvider.findCreditsRecord(5000, true, []);
812
+ *
813
+ * // When a record is found but not yet used, it's nonce should be added to the nonces parameter so that it is not
814
+ * // found again if a subsequent search is performed
815
+ * const records = await recordProvider.findCreditsRecords(5000, true, [record.nonce()]);
816
+ *
817
+ * // When the program manager is initialized with the record provider it will be used to find automatically find
818
+ * // fee records and amount records for value transfers so that they do not need to be specified manually
819
+ * const programManager = new ProgramManager("https://api.explorer.provable.com/v1", keyProvider, recordProvider);
820
+ * programManager.transfer(1, "aleo166q6ww6688cug7qxwe7nhctjpymydwzy2h7rscfmatqmfwnjvggqcad0at", "public", 0.5);
821
+ *
822
+ * */
823
+ async findCreditsRecords(microcredits, unspent, nonces, searchParameters) {
824
+ let startHeight = 0;
825
+ let endHeight = 0;
826
+ let maxAmount = undefined;
827
+ if (searchParameters) {
828
+ if ("startHeight" in searchParameters && typeof searchParameters["endHeight"] == "number") {
829
+ startHeight = searchParameters["startHeight"];
830
+ }
831
+ if ("endHeight" in searchParameters && typeof searchParameters["endHeight"] == "number") {
832
+ endHeight = searchParameters["endHeight"];
833
+ }
834
+ if ("amounts" in searchParameters && Array.isArray(searchParameters["amounts"]) && searchParameters["amount"].every((item) => typeof item === 'number')) {
835
+ microcredits = searchParameters["amounts"];
836
+ }
837
+ if ("maxAmount" in searchParameters && typeof searchParameters["maxAmount"] == "number") {
838
+ maxAmount = searchParameters["maxAmount"];
839
+ }
840
+ if ("unspent" in searchParameters && typeof searchParameters["unspent"] == "boolean") {
841
+ unspent = searchParameters["unspent"];
842
+ }
843
+ }
844
+ // If the end height is not specified, use the current block height
845
+ if (endHeight == 0) {
846
+ const end = await this.networkClient.getLatestHeight();
847
+ endHeight = end;
848
+ }
849
+ // If the start height is greater than the end height, throw an error
850
+ if (startHeight >= endHeight) {
851
+ logAndThrow("Start height must be less than end height");
852
+ }
853
+ return await this.networkClient.findRecords(startHeight, endHeight, unspent, ["credits.aleo"], microcredits, maxAmount, nonces, this.account.privateKey());
854
+ }
855
+ /**
856
+ * Find a credit record with a given number of microcredits by via the official Aleo API
857
+ *
858
+ * @param {number} microcredits The number of microcredits to search for
859
+ * @param {boolean} unspent Whether or not the record is unspent
860
+ * @param {string[]} nonces Nonces of records already found so that they are not found again
861
+ * @param {RecordSearchParams} searchParameters Additional parameters to search for
862
+ * @returns {Promise<RecordPlaintext>} The record if found, otherwise an error
863
+ *
864
+ * @example
865
+ * // Create a new NetworkRecordProvider
866
+ * const networkClient = new AleoNetworkClient("https://api.explorer.provable.com/v1");
867
+ * const keyProvider = new AleoKeyProvider();
868
+ * const recordProvider = new NetworkRecordProvider(account, networkClient);
869
+ *
870
+ * // The record provider can be used to find records with a given number of microcredits
871
+ * const record = await recordProvider.findCreditsRecord(5000, true, []);
872
+ *
873
+ * // When a record is found but not yet used, it's nonce should be added to the nonces parameter so that it is not
874
+ * // found again if a subsequent search is performed
875
+ * const records = await recordProvider.findCreditsRecords(5000, true, [record.nonce()]);
876
+ *
877
+ * // When the program manager is initialized with the record provider it will be used to find automatically find
878
+ * // fee records and amount records for value transfers so that they do not need to be specified manually
879
+ * const programManager = new ProgramManager("https://api.explorer.provable.com/v1", keyProvider, recordProvider);
880
+ * programManager.transfer(1, "aleo166q6ww6688cug7qxwe7nhctjpymydwzy2h7rscfmatqmfwnjvggqcad0at", "public", 0.5);
881
+ */
882
+ async findCreditsRecord(microcredits, unspent, nonces, searchParameters) {
883
+ let records = null;
884
+ try {
885
+ records = await this.findCreditsRecords([microcredits], unspent, nonces, searchParameters);
886
+ }
887
+ catch (e) {
888
+ console.log("No records found with error:", e);
889
+ }
890
+ if (records && records.length > 0) {
891
+ return records[0];
892
+ }
893
+ console.error("Record not found with error:", records);
894
+ throw new Error("Record not found");
895
+ }
896
+ /**
897
+ * Find an arbitrary record. WARNING: This function is not implemented yet and will throw an error.
898
+ */
899
+ async findRecord(unspent, nonces, searchParameters) {
900
+ throw new Error("Not implemented");
901
+ }
902
+ /**
903
+ * Find multiple records from a specified program.
904
+ */
905
+ async findRecords(unspent, nonces, searchParameters) {
906
+ let startHeight = 0;
907
+ let endHeight = 0;
908
+ let amounts = undefined;
909
+ let maxAmount = undefined;
910
+ let programs = undefined;
911
+ if (searchParameters) {
912
+ if ("startHeight" in searchParameters && typeof searchParameters["endHeight"] == "number") {
913
+ startHeight = searchParameters["startHeight"];
914
+ }
915
+ if ("endHeight" in searchParameters && typeof searchParameters["endHeight"] == "number") {
916
+ endHeight = searchParameters["endHeight"];
917
+ }
918
+ if ("amounts" in searchParameters && Array.isArray(searchParameters["amounts"]) && searchParameters["amounts"].every((item) => typeof item === 'number')) {
919
+ amounts = searchParameters["amounts"];
920
+ }
921
+ if ("maxAmount" in searchParameters && typeof searchParameters["maxAmount"] == "number") {
922
+ maxAmount = searchParameters["maxAmount"];
923
+ }
924
+ if ("nonces" in searchParameters && Array.isArray(searchParameters["nonces"]) && searchParameters["nonces"].every((item) => typeof item === "string")) {
925
+ nonces = searchParameters["nonces"];
926
+ }
927
+ if ("program" in searchParameters && typeof searchParameters["program"] == "string") {
928
+ programs = [searchParameters["program"]];
929
+ }
930
+ if ("programs" in searchParameters && Array.isArray(searchParameters["programs"]) && searchParameters["programs"].every((item) => typeof item === "string")) {
931
+ programs = searchParameters["programs"];
932
+ }
933
+ if ("unspent" in searchParameters && typeof searchParameters["unspent"] == "boolean") {
934
+ unspent = searchParameters["unspent"];
935
+ }
936
+ }
937
+ // If the end height is not specified, use the current block height
938
+ if (endHeight == 0) {
939
+ const end = await this.networkClient.getLatestHeight();
940
+ endHeight = end;
941
+ }
942
+ // If the start height is greater than the end height, throw an error
943
+ if (startHeight >= endHeight) {
944
+ logAndThrow("Start height must be less than end height");
945
+ }
946
+ return await this.networkClient.findRecords(startHeight, endHeight, unspent, programs, amounts, maxAmount, nonces, this.account.privateKey());
947
+ }
948
+ }
949
+ /**
950
+ * BlockHeightSearch is a RecordSearchParams implementation that allows for searching for records within a given
951
+ * block height range.
952
+ *
953
+ * @example
954
+ * // Create a new BlockHeightSearch
955
+ * const params = new BlockHeightSearch(89995, 99995);
956
+ *
957
+ * // Create a new NetworkRecordProvider
958
+ * const networkClient = new AleoNetworkClient("https://api.explorer.provable.com/v1");
959
+ * const keyProvider = new AleoKeyProvider();
960
+ * const recordProvider = new NetworkRecordProvider(account, networkClient);
961
+ *
962
+ * // The record provider can be used to find records with a given number of microcredits and the block height search
963
+ * // can be used to find records within a given block height range
964
+ * const record = await recordProvider.findCreditsRecord(5000, true, [], params);
965
+ *
966
+ */
967
+ class BlockHeightSearch {
968
+ startHeight;
969
+ endHeight;
970
+ constructor(startHeight, endHeight) {
971
+ this.startHeight = startHeight;
972
+ this.endHeight = endHeight;
973
+ }
974
+ }
975
+
976
+ // Experimental example where SDK manages worker
977
+ let singletonWorker = null;
978
+ const createAleoWorker = () => {
979
+ if (!singletonWorker) {
980
+ const worker = new Worker(new URL("worker.js", import.meta.url), {
981
+ type: "module",
982
+ });
983
+ singletonWorker = wrap(worker);
984
+ }
985
+ return singletonWorker;
986
+ };
987
+
988
+ // @TODO: This function is no longer needed, remove it.
989
+ async function initializeWasm() {
990
+ console.warn("initializeWasm is deprecated, you no longer need to use it");
991
+ }
992
+
993
+ export { Account, BlockHeightSearch, CREDITS_PROGRAM_KEYS, NetworkRecordProvider, OfflineKeyProvider, OfflineSearchParams, PRIVATE_TO_PUBLIC_TRANSFER, PRIVATE_TRANSFER, PUBLIC_TO_PRIVATE_TRANSFER, PUBLIC_TRANSFER, PUBLIC_TRANSFER_AS_SIGNER, createAleoWorker, initializeWasm, logAndThrow };
994
+ //# sourceMappingURL=browser.js.map