@provablehq/sdk 0.8.7 → 0.9.0
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.
- package/dist/mainnet/account.d.ts +176 -59
- package/dist/mainnet/browser.d.ts +2 -1
- package/dist/mainnet/browser.js +170 -53
- package/dist/mainnet/browser.js.map +1 -1
- package/dist/mainnet/models/imports.d.ts +7 -0
- package/dist/mainnet/network-client.d.ts +340 -65
- package/dist/mainnet/node.js +1 -1
- package/dist/mainnet/{program-manager-D0WpABBc.js → program-manager-C1m8QHkg.js} +817 -221
- package/dist/mainnet/program-manager-C1m8QHkg.js.map +1 -0
- package/dist/mainnet/program-manager.d.ts +88 -31
- package/dist/mainnet/utils.d.ts +9 -0
- package/dist/mainnet/worker.js +1 -1
- package/dist/testnet/account.d.ts +176 -59
- package/dist/testnet/browser.d.ts +2 -1
- package/dist/testnet/browser.js +170 -53
- package/dist/testnet/browser.js.map +1 -1
- package/dist/testnet/models/imports.d.ts +7 -0
- package/dist/testnet/network-client.d.ts +340 -65
- package/dist/testnet/node.js +1 -1
- package/dist/testnet/{program-manager-CVoUpH8X.js → program-manager-Dx4UkssS.js} +817 -221
- package/dist/testnet/program-manager-Dx4UkssS.js.map +1 -0
- package/dist/testnet/program-manager.d.ts +88 -31
- package/dist/testnet/utils.d.ts +9 -0
- package/dist/testnet/worker.js +1 -1
- package/package.json +2 -2
- package/dist/mainnet/program-manager-D0WpABBc.js.map +0 -1
- package/dist/testnet/program-manager-CVoUpH8X.js.map +0 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { PrivateKey, RecordCiphertext, Program, Plaintext, Transaction, Metadata, VerifyingKey, ProvingKey, ProgramManager as ProgramManager$1, RecordPlaintext, verifyFunctionExecution } from '@provablehq/wasm/mainnet.js';
|
|
1
|
+
import { PrivateKey, RecordCiphertext, Program, Plaintext, Address, Transaction, Metadata, VerifyingKey, ProvingKey, ProgramManager as ProgramManager$1, RecordPlaintext, verifyFunctionExecution } from '@provablehq/wasm/mainnet.js';
|
|
2
2
|
|
|
3
3
|
function logAndThrow(message) {
|
|
4
4
|
console.error(message);
|
|
@@ -30,6 +30,37 @@ async function post(url, options) {
|
|
|
30
30
|
}
|
|
31
31
|
return response;
|
|
32
32
|
}
|
|
33
|
+
async function retryWithBackoff(fn, { maxAttempts = 5, baseDelay = 100, jitter, retryOnStatus = [], shouldRetry, } = {}) {
|
|
34
|
+
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
|
|
35
|
+
try {
|
|
36
|
+
return await fn();
|
|
37
|
+
}
|
|
38
|
+
catch (err) {
|
|
39
|
+
const isLast = attempt === maxAttempts;
|
|
40
|
+
const error = err;
|
|
41
|
+
let retryable = false;
|
|
42
|
+
if (typeof error.status === "number") {
|
|
43
|
+
if (error.status >= 500) {
|
|
44
|
+
retryable = true;
|
|
45
|
+
}
|
|
46
|
+
else if (error.status >= 400 && shouldRetry) {
|
|
47
|
+
retryable = shouldRetry(error);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
else if (shouldRetry) {
|
|
51
|
+
retryable = shouldRetry(error);
|
|
52
|
+
}
|
|
53
|
+
if (!retryable || isLast)
|
|
54
|
+
throw error;
|
|
55
|
+
const jitterAmount = jitter ?? baseDelay;
|
|
56
|
+
const actualJitter = Math.floor(Math.random() * jitterAmount);
|
|
57
|
+
const delay = baseDelay * 2 ** (attempt - 1) + actualJitter;
|
|
58
|
+
console.warn(`Retry ${attempt}/${maxAttempts} failed. Retrying in ${delay}ms...`);
|
|
59
|
+
await new Promise((res) => setTimeout(res, delay));
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
throw new Error("retryWithBackoff: unreachable");
|
|
63
|
+
}
|
|
33
64
|
|
|
34
65
|
/**
|
|
35
66
|
* Client library that encapsulates REST calls to publicly exposed endpoints of Aleo nodes. The methods provided in this
|
|
@@ -37,34 +68,39 @@ async function post(url, options) {
|
|
|
37
68
|
*
|
|
38
69
|
* @param {string} host
|
|
39
70
|
* @example
|
|
40
|
-
* // Connection to a local node
|
|
41
|
-
* const localNetworkClient = new AleoNetworkClient("http://
|
|
71
|
+
* // Connection to a local node.
|
|
72
|
+
* const localNetworkClient = new AleoNetworkClient("http://0.0.0.0:3030", undefined, account);
|
|
42
73
|
*
|
|
43
74
|
* // Connection to a public beacon node
|
|
44
75
|
* const account = Account.fromCiphertext(process.env.ciphertext, process.env.password);
|
|
45
|
-
* const
|
|
76
|
+
* const publicNetworkClient = new AleoNetworkClient("http://api.explorer.provable.com/v1", undefined, account);
|
|
46
77
|
*/
|
|
47
78
|
class AleoNetworkClient {
|
|
48
79
|
host;
|
|
49
80
|
headers;
|
|
50
81
|
account;
|
|
82
|
+
network;
|
|
51
83
|
constructor(host, options) {
|
|
52
84
|
this.host = host + "/mainnet";
|
|
85
|
+
this.network = "mainnet";
|
|
53
86
|
if (options && options.headers) {
|
|
54
87
|
this.headers = options.headers;
|
|
55
88
|
}
|
|
56
89
|
else {
|
|
57
90
|
this.headers = {
|
|
58
91
|
// This is replaced by the actual version by a Rollup plugin
|
|
59
|
-
"X-Aleo-SDK-Version": "0.
|
|
92
|
+
"X-Aleo-SDK-Version": "0.9.0",
|
|
60
93
|
};
|
|
61
94
|
}
|
|
62
95
|
}
|
|
63
96
|
/**
|
|
64
97
|
* Set an account to use in networkClient calls
|
|
65
98
|
*
|
|
66
|
-
* @param {Account} account
|
|
99
|
+
* @param {Account} account Set an account to use for record scanning functions.
|
|
67
100
|
* @example
|
|
101
|
+
* import { Account, AleoNetworkClient } from "@provablehq/sdk/mainnet.js";
|
|
102
|
+
*
|
|
103
|
+
* const networkClient = new AleoNetworkClient("http://api.explorer.provable.com/v1");
|
|
68
104
|
* const account = new Account();
|
|
69
105
|
* networkClient.setAccount(account);
|
|
70
106
|
*/
|
|
@@ -85,18 +121,63 @@ class AleoNetworkClient {
|
|
|
85
121
|
*
|
|
86
122
|
* @param {string} host The address of a node hosting the Aleo API
|
|
87
123
|
* @param host
|
|
124
|
+
*
|
|
125
|
+
* @example
|
|
126
|
+
* import { AleoNetworkClient } from "@provablehq/sdk/mainnet.js";
|
|
127
|
+
*
|
|
128
|
+
* // Create a networkClient that connects to a local node.
|
|
129
|
+
* const networkClient = new AleoNetworkClient("http://0.0.0.0:3030", undefined);
|
|
130
|
+
*
|
|
131
|
+
* // Set the host to a public node.
|
|
132
|
+
* networkClient.setHost("http://api.explorer.provable.com/v1");
|
|
88
133
|
*/
|
|
89
134
|
setHost(host) {
|
|
90
135
|
this.host = host + "/mainnet";
|
|
91
136
|
}
|
|
137
|
+
/**
|
|
138
|
+
* Set a header in the `AleoNetworkClient`s header map
|
|
139
|
+
*
|
|
140
|
+
* @param {string} headerName The name of the header to set
|
|
141
|
+
* @param {string} value The header value
|
|
142
|
+
*
|
|
143
|
+
* @example
|
|
144
|
+
* import { AleoNetworkClient } from "@provablehq/sdk/mainnet.js";
|
|
145
|
+
*
|
|
146
|
+
* // Create a networkClient
|
|
147
|
+
* const networkClient = new AleoNetworkClient();
|
|
148
|
+
*
|
|
149
|
+
* // Set the value of the `Accept-Language` header to `en-US`
|
|
150
|
+
* networkClient.setHeader('Accept-Language', 'en-US');
|
|
151
|
+
*/
|
|
152
|
+
setHeader(headerName, value) {
|
|
153
|
+
this.headers[headerName] = value;
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Remove a header from the `AleoNetworkClient`s header map
|
|
157
|
+
*
|
|
158
|
+
* @param {string} headerName The name of the header to be removed
|
|
159
|
+
*
|
|
160
|
+
* @example
|
|
161
|
+
* import { AleoNetworkClient } from "@provablehq/sdk/mainnet.js";
|
|
162
|
+
*
|
|
163
|
+
* // Create a networkClient
|
|
164
|
+
* const networkClient = new AleoNetworkClient();
|
|
165
|
+
*
|
|
166
|
+
* // Remove the default `X-Aleo-SDK-Version` header
|
|
167
|
+
* networkClient.removeHeader('X-Aleo-SDK-Version');
|
|
168
|
+
*/
|
|
169
|
+
removeHeader(headerName) {
|
|
170
|
+
delete this.headers[headerName];
|
|
171
|
+
}
|
|
92
172
|
/**
|
|
93
173
|
* Fetches data from the Aleo network and returns it as a JSON object.
|
|
94
174
|
*
|
|
95
|
-
* @param url
|
|
175
|
+
* @param url The URL to fetch data from.
|
|
96
176
|
*/
|
|
97
177
|
async fetchData(url = "/") {
|
|
98
178
|
try {
|
|
99
|
-
|
|
179
|
+
const raw = await this.fetchRaw(url);
|
|
180
|
+
return parseJSON(raw);
|
|
100
181
|
}
|
|
101
182
|
catch (error) {
|
|
102
183
|
throw new Error(`Error fetching data: ${error}`);
|
|
@@ -112,15 +193,27 @@ class AleoNetworkClient {
|
|
|
112
193
|
*/
|
|
113
194
|
async fetchRaw(url = "/") {
|
|
114
195
|
try {
|
|
115
|
-
|
|
116
|
-
|
|
196
|
+
return await retryWithBackoff(async () => {
|
|
197
|
+
const response = await get(this.host + url, {
|
|
198
|
+
headers: this.headers,
|
|
199
|
+
});
|
|
200
|
+
return await response.text();
|
|
117
201
|
});
|
|
118
|
-
return await response.text();
|
|
119
202
|
}
|
|
120
203
|
catch (error) {
|
|
121
204
|
throw new Error(`Error fetching data: ${error}`);
|
|
122
205
|
}
|
|
123
206
|
}
|
|
207
|
+
/**
|
|
208
|
+
* Wrapper around the POST helper to allow mocking in tests. Not meant for use in production.
|
|
209
|
+
*
|
|
210
|
+
* @param url The URL to POST to.
|
|
211
|
+
* @param options The RequestInit options for the POST request.
|
|
212
|
+
* @returns The Response object from the POST request.
|
|
213
|
+
*/
|
|
214
|
+
async _sendPost(url, options) {
|
|
215
|
+
return post(url, options);
|
|
216
|
+
}
|
|
124
217
|
/**
|
|
125
218
|
* Attempt to find records in the Aleo blockchain.
|
|
126
219
|
*
|
|
@@ -132,8 +225,18 @@ class AleoNetworkClient {
|
|
|
132
225
|
* @param {number} maxMicrocredits - The maximum number of microcredits to search for
|
|
133
226
|
* @param {string[]} nonces - The nonces of already found records to exclude from the search
|
|
134
227
|
* @param {string | PrivateKey} privateKey - An optional private key to use to find unspent records.
|
|
228
|
+
* @returns {Promise<Array<RecordPlaintext>>} An array of records belonging to the account configured in the network client.
|
|
135
229
|
*
|
|
136
230
|
* @example
|
|
231
|
+
* import { Account, AleoNetworkClient } from "@provablehq/sdk/mainnet.js";
|
|
232
|
+
*
|
|
233
|
+
* // Import an account from a ciphertext and password.
|
|
234
|
+
* const account = Account.fromCiphertext(process.env.ciphertext, process.env.password);
|
|
235
|
+
*
|
|
236
|
+
* // Create a network client.
|
|
237
|
+
* const networkClient = new AleoNetworkClient("http://api.explorer.provable.com/v1", undefined);
|
|
238
|
+
* networkClient.setAccount(account);
|
|
239
|
+
*
|
|
137
240
|
* // Find specific amounts
|
|
138
241
|
* const startHeight = 500000;
|
|
139
242
|
* const amounts = [600000, 1000000];
|
|
@@ -168,7 +271,10 @@ class AleoNetworkClient {
|
|
|
168
271
|
}
|
|
169
272
|
else {
|
|
170
273
|
try {
|
|
171
|
-
resolvedPrivateKey =
|
|
274
|
+
resolvedPrivateKey =
|
|
275
|
+
privateKey instanceof PrivateKey
|
|
276
|
+
? privateKey
|
|
277
|
+
: PrivateKey.from_string(privateKey);
|
|
172
278
|
}
|
|
173
279
|
catch (error) {
|
|
174
280
|
throw new Error("Error parsing private key provided.");
|
|
@@ -219,8 +325,12 @@ class AleoNetworkClient {
|
|
|
219
325
|
// Search for unspent records in execute transactions of credits.aleo
|
|
220
326
|
if (confirmedTransaction.type == "execute") {
|
|
221
327
|
const transaction = confirmedTransaction.transaction;
|
|
222
|
-
if (transaction.execution &&
|
|
223
|
-
|
|
328
|
+
if (transaction.execution &&
|
|
329
|
+
!(typeof transaction.execution
|
|
330
|
+
.transitions == "undefined")) {
|
|
331
|
+
for (let k = 0; k <
|
|
332
|
+
transaction.execution.transitions
|
|
333
|
+
.length; k++) {
|
|
224
334
|
const transition = transaction.execution.transitions[k];
|
|
225
335
|
// Only search for unspent records in the specified programs.
|
|
226
336
|
if (!(typeof programs === "undefined")) {
|
|
@@ -228,7 +338,8 @@ class AleoNetworkClient {
|
|
|
228
338
|
continue;
|
|
229
339
|
}
|
|
230
340
|
}
|
|
231
|
-
if (!(typeof transition.outputs ==
|
|
341
|
+
if (!(typeof transition.outputs ==
|
|
342
|
+
"undefined")) {
|
|
232
343
|
for (let l = 0; l < transition.outputs.length; l++) {
|
|
233
344
|
const output = transition.outputs[l];
|
|
234
345
|
if (output.type === "record") {
|
|
@@ -249,7 +360,7 @@ class AleoNetworkClient {
|
|
|
249
360
|
const serialNumber = recordPlaintext.serialNumberString(resolvedPrivateKey, "credits.aleo", "credits");
|
|
250
361
|
// Attempt to see if the serial number is spent
|
|
251
362
|
try {
|
|
252
|
-
await this.getTransitionId(serialNumber);
|
|
363
|
+
await retryWithBackoff(() => this.getTransitionId(serialNumber));
|
|
253
364
|
continue;
|
|
254
365
|
}
|
|
255
366
|
catch (error) {
|
|
@@ -260,37 +371,47 @@ class AleoNetworkClient {
|
|
|
260
371
|
if (!amounts) {
|
|
261
372
|
records.push(recordPlaintext);
|
|
262
373
|
// If the user specified a maximum number of microcredits, check if the search has found enough
|
|
263
|
-
if (typeof maxMicrocredits ===
|
|
264
|
-
|
|
374
|
+
if (typeof maxMicrocredits ===
|
|
375
|
+
"number") {
|
|
376
|
+
totalRecordValue +=
|
|
377
|
+
recordPlaintext.microcredits();
|
|
265
378
|
// Exit if the search has found the amount specified
|
|
266
|
-
if (totalRecordValue >=
|
|
379
|
+
if (totalRecordValue >=
|
|
380
|
+
BigInt(maxMicrocredits)) {
|
|
267
381
|
return records;
|
|
268
382
|
}
|
|
269
383
|
}
|
|
270
384
|
}
|
|
271
385
|
// If the user specified a list of amounts, check if the search has found them
|
|
272
|
-
if (!(typeof amounts ===
|
|
386
|
+
if (!(typeof amounts ===
|
|
387
|
+
"undefined") &&
|
|
388
|
+
amounts.length >
|
|
389
|
+
0) {
|
|
273
390
|
let amounts_found = 0;
|
|
274
|
-
if (recordPlaintext.microcredits() >
|
|
391
|
+
if (recordPlaintext.microcredits() >
|
|
392
|
+
amounts[amounts_found]) {
|
|
275
393
|
amounts_found += 1;
|
|
276
394
|
records.push(recordPlaintext);
|
|
277
395
|
// If the user specified a maximum number of microcredits, check if the search has found enough
|
|
278
|
-
if (typeof maxMicrocredits ===
|
|
279
|
-
|
|
396
|
+
if (typeof maxMicrocredits ===
|
|
397
|
+
"number") {
|
|
398
|
+
totalRecordValue +=
|
|
399
|
+
recordPlaintext.microcredits();
|
|
280
400
|
// Exit if the search has found the amount specified
|
|
281
|
-
if (totalRecordValue >=
|
|
401
|
+
if (totalRecordValue >=
|
|
402
|
+
BigInt(maxMicrocredits)) {
|
|
282
403
|
return records;
|
|
283
404
|
}
|
|
284
405
|
}
|
|
285
|
-
if (records.length >=
|
|
406
|
+
if (records.length >=
|
|
407
|
+
amounts.length) {
|
|
286
408
|
return records;
|
|
287
409
|
}
|
|
288
410
|
}
|
|
289
411
|
}
|
|
290
412
|
}
|
|
291
413
|
}
|
|
292
|
-
catch (error) {
|
|
293
|
-
}
|
|
414
|
+
catch (error) { }
|
|
294
415
|
}
|
|
295
416
|
}
|
|
296
417
|
}
|
|
@@ -303,7 +424,10 @@ class AleoNetworkClient {
|
|
|
303
424
|
}
|
|
304
425
|
catch (error) {
|
|
305
426
|
// If there is an error fetching blocks, log it and keep searching
|
|
306
|
-
console.warn("Error fetching blocks in range: " +
|
|
427
|
+
console.warn("Error fetching blocks in range: " +
|
|
428
|
+
start.toString() +
|
|
429
|
+
"-" +
|
|
430
|
+
end.toString());
|
|
307
431
|
console.warn("Error: ", error);
|
|
308
432
|
failures += 1;
|
|
309
433
|
if (failures > 10) {
|
|
@@ -324,8 +448,17 @@ class AleoNetworkClient {
|
|
|
324
448
|
* @param {number} maxMicrocredits - The maximum number of microcredits to search for
|
|
325
449
|
* @param {string[]} nonces - The nonces of already found records to exclude from the search
|
|
326
450
|
* @param {string | PrivateKey} privateKey - An optional private key to use to find unspent records.
|
|
451
|
+
* @returns {Promise<Array<RecordPlaintext>>} An array of unspent records belonging to the account configured in the network client.
|
|
327
452
|
*
|
|
328
453
|
* @example
|
|
454
|
+
* import { Account, AleoNetworkClient } from "@provablehq/sdk/mainnet.js";
|
|
455
|
+
*
|
|
456
|
+
* const account = Account.fromCiphertext(process.env.ciphertext, process.env.password);
|
|
457
|
+
*
|
|
458
|
+
* // Create a network client and set an account to search for records with.
|
|
459
|
+
* const networkClient = new AleoNetworkClient("http://api.explorer.provable.com/v1", undefined);
|
|
460
|
+
* networkClient.setAccount(account);
|
|
461
|
+
*
|
|
329
462
|
* // Find specific amounts
|
|
330
463
|
* const startHeight = 500000;
|
|
331
464
|
* const endHeight = 550000;
|
|
@@ -342,7 +475,9 @@ class AleoNetworkClient {
|
|
|
342
475
|
/**
|
|
343
476
|
* Returns the contents of the block at the specified block height.
|
|
344
477
|
*
|
|
345
|
-
* @param {number} blockHeight
|
|
478
|
+
* @param {number} blockHeight - The height of the block to fetch
|
|
479
|
+
* @returns {Promise<BlockJSON>} A javascript object containing the block at the specified height
|
|
480
|
+
*
|
|
346
481
|
* @example
|
|
347
482
|
* const block = networkClient.getBlock(1234);
|
|
348
483
|
*/
|
|
@@ -358,8 +493,13 @@ class AleoNetworkClient {
|
|
|
358
493
|
/**
|
|
359
494
|
* Returns the contents of the block with the specified hash.
|
|
360
495
|
*
|
|
361
|
-
* @param {string} blockHash
|
|
496
|
+
* @param {string} blockHash The hash of the block to fetch.
|
|
497
|
+
* @returns {Promise<BlockJSON>} A javascript object representation of the block matching the hash.
|
|
498
|
+
*
|
|
362
499
|
* @example
|
|
500
|
+
* import { AleoNetworkClient } from "@provablehq/sdk/mainnet.js";
|
|
501
|
+
*
|
|
502
|
+
* const networkClient = new AleoNetworkClient("http://api.explorer.provable.com/v1", undefined);
|
|
363
503
|
* const block = networkClient.getBlockByHash("ab19dklwl9vp63zu3hwg57wyhvmqf92fx5g8x0t6dr72py8r87pxupqfne5t9");
|
|
364
504
|
*/
|
|
365
505
|
async getBlockByHash(blockHash) {
|
|
@@ -372,12 +512,24 @@ class AleoNetworkClient {
|
|
|
372
512
|
}
|
|
373
513
|
}
|
|
374
514
|
/**
|
|
375
|
-
* Returns a range of blocks between the specified block heights.
|
|
515
|
+
* Returns a range of blocks between the specified block heights. A maximum of 50 blocks can be fetched at a time.
|
|
516
|
+
*
|
|
517
|
+
* @param {number} start Starting block to fetch.
|
|
518
|
+
* @param {number} end Ending block to fetch. This cannot be more than 50 blocks ahead of the start block.
|
|
519
|
+
* @returns {Promise<Array<BlockJSON>>} An array of block objects
|
|
376
520
|
*
|
|
377
|
-
* @param {number} start
|
|
378
|
-
* @param {number} end
|
|
379
521
|
* @example
|
|
380
|
-
*
|
|
522
|
+
* import { AleoNetworkClient } from "@provablehq/sdk/mainnet.js";
|
|
523
|
+
*
|
|
524
|
+
* // Fetch 50 blocks.
|
|
525
|
+
* const (start, end) = (2050, 2100);
|
|
526
|
+
* const blockRange = networkClient.getBlockRange(start, end);
|
|
527
|
+
*
|
|
528
|
+
* let cursor = start;
|
|
529
|
+
* blockRange.forEach((block) => {
|
|
530
|
+
* assert(block.height == cursor);
|
|
531
|
+
* cursor += 1;
|
|
532
|
+
* }
|
|
381
533
|
*/
|
|
382
534
|
async getBlockRange(start, end) {
|
|
383
535
|
try {
|
|
@@ -390,8 +542,21 @@ class AleoNetworkClient {
|
|
|
390
542
|
/**
|
|
391
543
|
* Returns the deployment transaction id associated with the specified program.
|
|
392
544
|
*
|
|
393
|
-
* @param {Program | string} program
|
|
394
|
-
* @returns {
|
|
545
|
+
* @param {Program | string} program The name of the deployed program OR a wasm Program object.
|
|
546
|
+
* @returns {Promise<string>} The transaction ID of the deployment transaction.
|
|
547
|
+
*
|
|
548
|
+
* @example
|
|
549
|
+
* import { AleoNetworkClient } from "@provablehq/sdk/testnet.js";
|
|
550
|
+
*
|
|
551
|
+
* // Get the transaction ID of the deployment transaction for a program.
|
|
552
|
+
* const networkClient = new AleoNetworkClient("http://api.explorer.provable.com/v1", undefined);
|
|
553
|
+
* const transactionId = networkClient.getDeploymentTransactionIDForProgram("hello_hello.aleo");
|
|
554
|
+
*
|
|
555
|
+
* // Get the transaction data for the deployment transaction.
|
|
556
|
+
* const transaction = networkClient.getTransactionObject(transactionId);
|
|
557
|
+
*
|
|
558
|
+
* // Get the verifying keys for the functions in the deployed program.
|
|
559
|
+
* const verifyingKeys = transaction.verifyingKeys();
|
|
395
560
|
*/
|
|
396
561
|
async getDeploymentTransactionIDForProgram(program) {
|
|
397
562
|
if (program instanceof Program) {
|
|
@@ -399,24 +564,35 @@ class AleoNetworkClient {
|
|
|
399
564
|
}
|
|
400
565
|
try {
|
|
401
566
|
const id = await this.fetchData("/find/transactionID/deployment/" + program);
|
|
402
|
-
return id.replace("
|
|
567
|
+
return id.replace('"', "");
|
|
403
568
|
}
|
|
404
569
|
catch (error) {
|
|
405
570
|
throw new Error(`Error fetching deployment transaction for program ${program}: ${error}`);
|
|
406
571
|
}
|
|
407
572
|
}
|
|
408
573
|
/**
|
|
409
|
-
* Returns the deployment transaction associated with a specified program.
|
|
574
|
+
* Returns the deployment transaction associated with a specified program as a JSON object.
|
|
575
|
+
*
|
|
576
|
+
* @param {Program | string} program The name of the deployed program OR a wasm Program object.
|
|
577
|
+
* @returns {Promise<Transaction>} JSON representation of the deployment transaction.
|
|
578
|
+
*
|
|
579
|
+
* @example
|
|
580
|
+
* import { AleoNetworkClient, DeploymentJSON } from "@provablehq/sdk/testnet.js";
|
|
581
|
+
*
|
|
582
|
+
* // Get the transaction ID of the deployment transaction for a program.
|
|
583
|
+
* const networkClient = new AleoNetworkClient("http://api.explorer.provable.com/v1", undefined);
|
|
584
|
+
* const transaction = networkClient.getDeploymentTransactionForProgram("hello_hello.aleo");
|
|
410
585
|
*
|
|
411
|
-
*
|
|
412
|
-
*
|
|
586
|
+
* // Get the verifying keys for each function in the deployment.
|
|
587
|
+
* const deployment = <DeploymentJSON>transaction.deployment;
|
|
588
|
+
* const verifyingKeys = deployment.verifying_keys;
|
|
413
589
|
*/
|
|
414
590
|
async getDeploymentTransactionForProgram(program) {
|
|
415
591
|
if (program instanceof Program) {
|
|
416
592
|
program = program.id();
|
|
417
593
|
}
|
|
418
594
|
try {
|
|
419
|
-
const transaction_id = await this.getDeploymentTransactionIDForProgram(program);
|
|
595
|
+
const transaction_id = (await this.getDeploymentTransactionIDForProgram(program));
|
|
420
596
|
return await this.getTransaction(transaction_id);
|
|
421
597
|
}
|
|
422
598
|
catch (error) {
|
|
@@ -426,12 +602,25 @@ class AleoNetworkClient {
|
|
|
426
602
|
/**
|
|
427
603
|
* Returns the deployment transaction associated with a specified program as a wasm object.
|
|
428
604
|
*
|
|
429
|
-
* @param {Program | string} program
|
|
430
|
-
* @returns {
|
|
605
|
+
* @param {Program | string} program The name of the deployed program OR a wasm Program object.
|
|
606
|
+
* @returns {Promise<Transaction>} Wasm object representation of the deployment transaction.
|
|
607
|
+
*
|
|
608
|
+
* @example
|
|
609
|
+
* import { AleoNetworkClient } from "@provablehq/sdk/testnet.js";
|
|
610
|
+
*
|
|
611
|
+
* // Get the transaction ID of the deployment transaction for a program.
|
|
612
|
+
* const networkClient = new AleoNetworkClient("http://api.explorer.provable.com/v1", undefined);
|
|
613
|
+
* const transactionId = networkClient.getDeploymentTransactionIDForProgram("hello_hello.aleo");
|
|
614
|
+
*
|
|
615
|
+
* // Get the transaction data for the deployment transaction.
|
|
616
|
+
* const transaction = networkClient.getDeploymentTransactionObjectForProgram(transactionId);
|
|
617
|
+
*
|
|
618
|
+
* // Get the verifying keys for the functions in the deployed program.
|
|
619
|
+
* const verifyingKeys = transaction.verifyingKeys();
|
|
431
620
|
*/
|
|
432
621
|
async getDeploymentTransactionObjectForProgram(program) {
|
|
433
622
|
try {
|
|
434
|
-
const transaction_id = await this.getDeploymentTransactionIDForProgram(program);
|
|
623
|
+
const transaction_id = (await this.getDeploymentTransactionIDForProgram(program));
|
|
435
624
|
return await this.getTransactionObject(transaction_id);
|
|
436
625
|
}
|
|
437
626
|
catch (error) {
|
|
@@ -439,14 +628,21 @@ class AleoNetworkClient {
|
|
|
439
628
|
}
|
|
440
629
|
}
|
|
441
630
|
/**
|
|
442
|
-
* Returns the contents of the latest block.
|
|
631
|
+
* Returns the contents of the latest block as JSON.
|
|
632
|
+
*
|
|
633
|
+
* @returns {Promise<BlockJSON>} A javascript object containing the latest block
|
|
443
634
|
*
|
|
444
635
|
* @example
|
|
636
|
+
* import { AleoNetworkClient } from "@provablehq/sdk/testnet.js";
|
|
637
|
+
*
|
|
638
|
+
* // Create a network client.
|
|
639
|
+
* const networkClient = new AleoNetworkClient("http://api.explorer.provable.com/v1", undefined);
|
|
640
|
+
*
|
|
445
641
|
* const latestHeight = networkClient.getLatestBlock();
|
|
446
642
|
*/
|
|
447
643
|
async getLatestBlock() {
|
|
448
644
|
try {
|
|
449
|
-
return await this.fetchData("/block/latest");
|
|
645
|
+
return (await this.fetchData("/block/latest"));
|
|
450
646
|
}
|
|
451
647
|
catch (error) {
|
|
452
648
|
throw new Error(`Error fetching latest block: ${error}`);
|
|
@@ -456,6 +652,16 @@ class AleoNetworkClient {
|
|
|
456
652
|
* Returns the latest committee.
|
|
457
653
|
*
|
|
458
654
|
* @returns {Promise<object>} A javascript object containing the latest committee
|
|
655
|
+
*
|
|
656
|
+
* @example
|
|
657
|
+
* import { AleoNetworkClient } from "@provablehq/sdk/mainnet.js";
|
|
658
|
+
*
|
|
659
|
+
* // Create a network client.
|
|
660
|
+
* const networkClient = new AleoNetworkClient("http://api.explorer.provable.com/v1", undefined);
|
|
661
|
+
*
|
|
662
|
+
* // Create a network client and get the latest committee.
|
|
663
|
+
* const networkClient = new AleoNetworkClient("http://api.explorer.provable.com/v1", undefined);
|
|
664
|
+
* const latestCommittee = await networkClient.getLatestCommittee();
|
|
459
665
|
*/
|
|
460
666
|
async getLatestCommittee() {
|
|
461
667
|
try {
|
|
@@ -466,14 +672,20 @@ class AleoNetworkClient {
|
|
|
466
672
|
}
|
|
467
673
|
}
|
|
468
674
|
/**
|
|
469
|
-
* Returns the
|
|
470
|
-
*
|
|
471
|
-
* @param {number} blockHeight
|
|
675
|
+
* Returns the committee at the specified block height.
|
|
472
676
|
*
|
|
677
|
+
* @param {number} blockHeight - The height of the block to fetch the committee for
|
|
473
678
|
* @returns {Promise<object>} A javascript object containing the committee
|
|
474
679
|
*
|
|
475
680
|
* @example
|
|
476
|
-
*
|
|
681
|
+
* import { AleoNetworkClient } from "@provablehq/sdk/mainnet.js";
|
|
682
|
+
*
|
|
683
|
+
* // Create a network client.
|
|
684
|
+
* const networkClient = new AleoNetworkClient("http://api.explorer.provable.com/v1", undefined);
|
|
685
|
+
*
|
|
686
|
+
* // Create a network client and get the committee for a specific block.
|
|
687
|
+
* const networkClient = new AleoNetworkClient("http://api.explorer.provable.com/v1", undefined);
|
|
688
|
+
* const committee = await networkClient.getCommitteeByBlockHeight(1234);
|
|
477
689
|
*/
|
|
478
690
|
async getCommitteeByBlockHeight(blockHeight) {
|
|
479
691
|
try {
|
|
@@ -486,7 +698,14 @@ class AleoNetworkClient {
|
|
|
486
698
|
/**
|
|
487
699
|
* Returns the latest block height.
|
|
488
700
|
*
|
|
701
|
+
* @returns {Promise<number>} The latest block height.
|
|
702
|
+
*
|
|
489
703
|
* @example
|
|
704
|
+
* import { AleoNetworkClient } from "@provablehq/sdk/mainnet.js";
|
|
705
|
+
*
|
|
706
|
+
* // Create a network client.
|
|
707
|
+
* const networkClient = new AleoNetworkClient("http://api.explorer.provable.com/v1", undefined);
|
|
708
|
+
*
|
|
490
709
|
* const latestHeight = networkClient.getLatestHeight();
|
|
491
710
|
*/
|
|
492
711
|
async getLatestHeight() {
|
|
@@ -500,8 +719,16 @@ class AleoNetworkClient {
|
|
|
500
719
|
/**
|
|
501
720
|
* Returns the latest block hash.
|
|
502
721
|
*
|
|
722
|
+
* @returns {Promise<string>} The latest block hash.
|
|
723
|
+
*
|
|
503
724
|
* @example
|
|
504
|
-
*
|
|
725
|
+
* import { AleoNetworkClient } from "@provablehq/sdk/mainnet.js";
|
|
726
|
+
*
|
|
727
|
+
* // Create a network client.
|
|
728
|
+
* const networkClient = new AleoNetworkClient("http://api.explorer.provable.com/v1", undefined);
|
|
729
|
+
*
|
|
730
|
+
* // Get the latest block hash.
|
|
731
|
+
* const latestHash = networkClient.getLatestBlockHash();
|
|
505
732
|
*/
|
|
506
733
|
async getLatestBlockHash() {
|
|
507
734
|
try {
|
|
@@ -515,9 +742,14 @@ class AleoNetworkClient {
|
|
|
515
742
|
* Returns the source code of a program given a program ID.
|
|
516
743
|
*
|
|
517
744
|
* @param {string} programId The program ID of a program deployed to the Aleo Network
|
|
518
|
-
* @
|
|
745
|
+
* @returns {Promise<string>} Source code of the program
|
|
519
746
|
*
|
|
520
747
|
* @example
|
|
748
|
+
* import { AleoNetworkClient } from "@provablehq/sdk/mainnet.js";
|
|
749
|
+
*
|
|
750
|
+
* // Create a network client.
|
|
751
|
+
* const networkClient = new AleoNetworkClient("http://api.explorer.provable.com/v1", undefined);
|
|
752
|
+
*
|
|
521
753
|
* const program = networkClient.getProgram("hello_hello.aleo");
|
|
522
754
|
* 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"
|
|
523
755
|
* assert.equal(program, expectedSource);
|
|
@@ -534,9 +766,14 @@ class AleoNetworkClient {
|
|
|
534
766
|
* Returns a program object from a program ID or program source code.
|
|
535
767
|
*
|
|
536
768
|
* @param {string} inputProgram The program ID or program source code of a program deployed to the Aleo Network
|
|
537
|
-
* @
|
|
769
|
+
* @returns {Promise<Program>} Source code of the program
|
|
538
770
|
*
|
|
539
771
|
* @example
|
|
772
|
+
* import { AleoNetworkClient } from "@provablehq/sdk/mainnet.js";
|
|
773
|
+
*
|
|
774
|
+
* // Create a network client.
|
|
775
|
+
* const networkClient = new AleoNetworkClient("http://api.explorer.provable.com/v1", undefined);
|
|
776
|
+
*
|
|
540
777
|
* const programID = "hello_hello.aleo";
|
|
541
778
|
* const programSource = "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"
|
|
542
779
|
*
|
|
@@ -545,7 +782,7 @@ class AleoNetworkClient {
|
|
|
545
782
|
* const programObjectFromSource = await networkClient.getProgramObject(programSource);
|
|
546
783
|
*
|
|
547
784
|
* // Both program objects should be equal
|
|
548
|
-
* assert
|
|
785
|
+
* assert(programObjectFromID.to_string() === programObjectFromSource.to_string());
|
|
549
786
|
*/
|
|
550
787
|
async getProgramObject(inputProgram) {
|
|
551
788
|
try {
|
|
@@ -553,7 +790,7 @@ class AleoNetworkClient {
|
|
|
553
790
|
}
|
|
554
791
|
catch (error) {
|
|
555
792
|
try {
|
|
556
|
-
return Program.fromString(
|
|
793
|
+
return Program.fromString(await this.getProgram(inputProgram));
|
|
557
794
|
}
|
|
558
795
|
catch (error) {
|
|
559
796
|
throw new Error(`${inputProgram} is neither a program name or a valid program: ${error}`);
|
|
@@ -567,12 +804,17 @@ class AleoNetworkClient {
|
|
|
567
804
|
* @returns {Promise<ProgramImports>} Object of the form { "program_id": "program_source", .. } containing program id & source code for all program imports
|
|
568
805
|
*
|
|
569
806
|
* @example
|
|
807
|
+
* import { AleoNetworkClient } from "@provablehq/sdk/mainnet.js";
|
|
808
|
+
*
|
|
570
809
|
* const double_test_source = "import multiply_test.aleo;\n\nprogram double_test.aleo;\n\nfunction double_it:\n input r0 as u32.private;\n call multiply_test.aleo/multiply 2u32 r0 into r1;\n output r1 as u32.private;\n"
|
|
571
810
|
* const double_test = Program.fromString(double_test_source);
|
|
572
811
|
* const expectedImports = {
|
|
573
812
|
* "multiply_test.aleo": "program multiply_test.aleo;\n\nfunction multiply:\n input r0 as u32.public;\n input r1 as u32.private;\n mul r0 r1 into r2;\n output r2 as u32.private;\n"
|
|
574
813
|
* }
|
|
575
814
|
*
|
|
815
|
+
* // Create a network client.
|
|
816
|
+
* const networkClient = new AleoNetworkClient("http://api.explorer.provable.com/v1", undefined);
|
|
817
|
+
*
|
|
576
818
|
* // Imports can be fetched using the program ID, source code, or program object
|
|
577
819
|
* let programImports = await networkClient.getProgramImports("double_test.aleo");
|
|
578
820
|
* assert.deepStrictEqual(programImports, expectedImports);
|
|
@@ -589,15 +831,17 @@ class AleoNetworkClient {
|
|
|
589
831
|
try {
|
|
590
832
|
const imports = {};
|
|
591
833
|
// Get the program object or fail if the program is not valid or does not exist
|
|
592
|
-
const program = inputProgram instanceof Program
|
|
834
|
+
const program = inputProgram instanceof Program
|
|
835
|
+
? inputProgram
|
|
836
|
+
: await this.getProgramObject(inputProgram);
|
|
593
837
|
// Get the list of programs that the program imports
|
|
594
838
|
const importList = program.getImports();
|
|
595
839
|
// Recursively get any imports that the imported programs have in a depth first search order
|
|
596
840
|
for (let i = 0; i < importList.length; i++) {
|
|
597
841
|
const import_id = importList[i];
|
|
598
842
|
if (!imports.hasOwnProperty(import_id)) {
|
|
599
|
-
const programSource = await this.getProgram(import_id);
|
|
600
|
-
const nestedImports = await this.getProgramImports(import_id);
|
|
843
|
+
const programSource = (await this.getProgram(import_id));
|
|
844
|
+
const nestedImports = (await this.getProgramImports(import_id));
|
|
601
845
|
for (const key in nestedImports) {
|
|
602
846
|
if (!imports.hasOwnProperty(key)) {
|
|
603
847
|
imports[key] = nestedImports[key];
|
|
@@ -619,13 +863,20 @@ class AleoNetworkClient {
|
|
|
619
863
|
* @returns {string[]} - The list of program names that the program imports
|
|
620
864
|
*
|
|
621
865
|
* @example
|
|
622
|
-
*
|
|
623
|
-
*
|
|
866
|
+
* import { AleoNetworkClient } from "@provablehq/sdk/mainnet.js";
|
|
867
|
+
*
|
|
868
|
+
* // Create a network client.
|
|
869
|
+
* const networkClient = new AleoNetworkClient("http://api.explorer.provable.com/v1", undefined);
|
|
870
|
+
*
|
|
871
|
+
* const programImportsNames = networkClient.getProgramImports("wrapped_credits.aleo");
|
|
872
|
+
* const expectedImportsNames = ["credits.aleo"];
|
|
624
873
|
* assert.deepStrictEqual(programImportsNames, expectedImportsNames);
|
|
625
874
|
*/
|
|
626
875
|
async getProgramImportNames(inputProgram) {
|
|
627
876
|
try {
|
|
628
|
-
const program = inputProgram instanceof Program
|
|
877
|
+
const program = inputProgram instanceof Program
|
|
878
|
+
? inputProgram
|
|
879
|
+
: await this.getProgramObject(inputProgram);
|
|
629
880
|
return program.getImports();
|
|
630
881
|
}
|
|
631
882
|
catch (error) {
|
|
@@ -636,7 +887,14 @@ class AleoNetworkClient {
|
|
|
636
887
|
* Returns the names of the mappings of a program.
|
|
637
888
|
*
|
|
638
889
|
* @param {string} programId - The program ID to get the mappings of (e.g. "credits.aleo")
|
|
890
|
+
* @returns {Promise<Array<string>>} - The names of the mappings of the program.
|
|
891
|
+
*
|
|
639
892
|
* @example
|
|
893
|
+
* import { AleoNetworkClient } from "@provablehq/sdk/mainnet.js";
|
|
894
|
+
*
|
|
895
|
+
* // Create a network client.
|
|
896
|
+
* const networkClient = new AleoNetworkClient("http://api.explorer.provable.com/v1", undefined);
|
|
897
|
+
*
|
|
640
898
|
* const mappings = networkClient.getProgramMappingNames("credits.aleo");
|
|
641
899
|
* const expectedMappings = [
|
|
642
900
|
* "committee",
|
|
@@ -662,14 +920,19 @@ class AleoNetworkClient {
|
|
|
662
920
|
*
|
|
663
921
|
* @param {string} programId - The program ID to get the mapping value of (e.g. "credits.aleo")
|
|
664
922
|
* @param {string} mappingName - The name of the mapping to get the value of (e.g. "account")
|
|
665
|
-
* @param {string | Plaintext} key - The key
|
|
666
|
-
* @
|
|
923
|
+
* @param {string | Plaintext} key - The key to look up in the mapping (e.g. an address for the "account" mapping)
|
|
924
|
+
* @returns {Promise<string>} String representation of the value of the mapping
|
|
667
925
|
*
|
|
668
926
|
* @example
|
|
927
|
+
* import { AleoNetworkClient } from "@provablehq/sdk/mainnet.js";
|
|
928
|
+
*
|
|
929
|
+
* // Create a network client.
|
|
930
|
+
* const networkClient = new AleoNetworkClient("http://api.explorer.provable.com/v1", undefined);
|
|
931
|
+
*
|
|
669
932
|
* // Get public balance of an account
|
|
670
933
|
* const mappingValue = networkClient.getMappingValue("credits.aleo", "account", "aleo1rhgdu77hgyqd3xjj8ucu3jj9r2krwz6mnzyd80gncr5fxcwlh5rsvzp9px");
|
|
671
934
|
* const expectedValue = "0u64";
|
|
672
|
-
* assert
|
|
935
|
+
* assert(mappingValue === expectedValue);
|
|
673
936
|
*/
|
|
674
937
|
async getProgramMappingValue(programId, mappingName, key) {
|
|
675
938
|
try {
|
|
@@ -681,11 +944,19 @@ class AleoNetworkClient {
|
|
|
681
944
|
}
|
|
682
945
|
}
|
|
683
946
|
/**
|
|
684
|
-
* Returns the value of a mapping as a wasm Plaintext object. Returning an
|
|
685
|
-
*
|
|
686
|
-
*
|
|
947
|
+
* Returns the value of a mapping as a wasm Plaintext object. Returning an object in this format allows it to be converted to a Js type and for its internal members to be inspected if it's a struct or array.
|
|
948
|
+
*
|
|
949
|
+
* @param {string} programId - The program ID to get the mapping value of (e.g. "credits.aleo")
|
|
950
|
+
* @param {string} mappingName - The name of the mapping to get the value of (e.g. "bonded")
|
|
951
|
+
* @param {string | Plaintext} key - The key to look up in the mapping (e.g. an address for the "bonded" mapping)
|
|
952
|
+
* @returns {Promise<Plaintext>} String representation of the value of the mapping
|
|
687
953
|
*
|
|
688
954
|
* @example
|
|
955
|
+
* import { AleoNetworkClient } from "@provablehq/sdk/mainnet.js";
|
|
956
|
+
*
|
|
957
|
+
* // Create a network client.
|
|
958
|
+
* const networkClient = new AleoNetworkClient("http://api.explorer.provable.com/v1", undefined);
|
|
959
|
+
*
|
|
689
960
|
* // Get the bond state as an account.
|
|
690
961
|
* const unbondedState = networkClient.getMappingPlaintext("credits.aleo", "bonded", "aleo1rhgdu77hgyqd3xjj8ucu3jj9r2krwz6mnzyd80gncr5fxcwlh5rsvzp9px");
|
|
691
962
|
*
|
|
@@ -702,15 +973,9 @@ class AleoNetworkClient {
|
|
|
702
973
|
*
|
|
703
974
|
* const expectedState = {
|
|
704
975
|
* validator: "aleo1u6940v5m0fzud859xx2c9tj2gjg6m5qrd28n636e6fdd2akvfcgqs34mfd",
|
|
705
|
-
* microcredits: BigInt(
|
|
976
|
+
* microcredits: BigInt(9007199254740991)
|
|
706
977
|
* };
|
|
707
978
|
* assert.equal(unbondedState, expectedState);
|
|
708
|
-
*
|
|
709
|
-
* @param {string} programId - The program ID to get the mapping value of (e.g. "credits.aleo")
|
|
710
|
-
* @param {string} mappingName - The name of the mapping to get the value of (e.g. "account")
|
|
711
|
-
* @param {string | Plaintext} key - The key of the mapping to get the value of (e.g. "aleo1rhgdu77hgyqd3xjj8ucu3jj9r2krwz6mnzyd80gncr5fxcwlh5rsvzp9px")
|
|
712
|
-
*
|
|
713
|
-
* @return {Promise<string>} String representation of the value of the mapping
|
|
714
979
|
*/
|
|
715
980
|
async getProgramMappingPlaintext(programId, mappingName, key) {
|
|
716
981
|
try {
|
|
@@ -725,15 +990,25 @@ class AleoNetworkClient {
|
|
|
725
990
|
/**
|
|
726
991
|
* Returns the public balance of an address from the account mapping in credits.aleo
|
|
727
992
|
*
|
|
728
|
-
* @param {string} address
|
|
993
|
+
* @param {Address | string} address A string or wasm object representing an address.
|
|
994
|
+
* @returns {Promise<number>} The public balance of the address in microcredits.
|
|
729
995
|
*
|
|
730
996
|
* @example
|
|
731
|
-
*
|
|
732
|
-
*
|
|
997
|
+
* import { AleoNetworkClient, Account } from "@provablehq/sdk/mainnet.js";
|
|
998
|
+
*
|
|
999
|
+
* // Create a network client.
|
|
1000
|
+
* const networkClient = new AleoNetworkClient("http://api.explorer.provable.com/v1", undefined);
|
|
1001
|
+
*
|
|
1002
|
+
* // Get the balance of an account from either an address object or address string.
|
|
1003
|
+
* const account = Account.fromCiphertext(process.env.ciphertext, process.env.password);
|
|
1004
|
+
* const publicBalance = await networkClient.getPublicBalance(account.address());
|
|
1005
|
+
* const publicBalanceFromString = await networkClient.getPublicBalance(account.address().to_string());
|
|
1006
|
+
* assert(publicBalance === publicBalanceFromString);
|
|
733
1007
|
*/
|
|
734
1008
|
async getPublicBalance(address) {
|
|
735
1009
|
try {
|
|
736
|
-
const
|
|
1010
|
+
const addressString = address instanceof Address ? address.to_string() : address;
|
|
1011
|
+
const balanceStr = await this.getProgramMappingValue("credits.aleo", "account", addressString);
|
|
737
1012
|
return balanceStr ? parseInt(balanceStr) : 0;
|
|
738
1013
|
}
|
|
739
1014
|
catch (error) {
|
|
@@ -743,12 +1018,20 @@ class AleoNetworkClient {
|
|
|
743
1018
|
/**
|
|
744
1019
|
* Returns the latest state/merkle root of the Aleo blockchain.
|
|
745
1020
|
*
|
|
1021
|
+
* @returns {Promise<string>} A string representing the latest state root of the Aleo blockchain.
|
|
1022
|
+
*
|
|
746
1023
|
* @example
|
|
1024
|
+
* import { AleoNetworkClient, Account } from "@provablehq/sdk/mainnet.js";
|
|
1025
|
+
*
|
|
1026
|
+
* // Create a network client.
|
|
1027
|
+
* const networkClient = new AleoNetworkClient("http://api.explorer.provable.com/v1", undefined);
|
|
1028
|
+
*
|
|
1029
|
+
* // Get the latest state root.
|
|
747
1030
|
* const stateRoot = networkClient.getStateRoot();
|
|
748
1031
|
*/
|
|
749
1032
|
async getStateRoot() {
|
|
750
1033
|
try {
|
|
751
|
-
return await this.fetchData(
|
|
1034
|
+
return await this.fetchData("/stateRoot/latest");
|
|
752
1035
|
}
|
|
753
1036
|
catch (error) {
|
|
754
1037
|
throw new Error(`Error fetching latest state root: ${error}`);
|
|
@@ -757,8 +1040,15 @@ class AleoNetworkClient {
|
|
|
757
1040
|
/**
|
|
758
1041
|
* Returns a transaction by its unique identifier.
|
|
759
1042
|
*
|
|
760
|
-
* @param {string} transactionId
|
|
1043
|
+
* @param {string} transactionId The transaction ID to fetch.
|
|
1044
|
+
* @returns {Promise<TransactionJSON>} A json representation of the transaction.
|
|
1045
|
+
*
|
|
761
1046
|
* @example
|
|
1047
|
+
* import { AleoNetworkClient, Account } from "@provablehq/sdk/mainnet.js";
|
|
1048
|
+
*
|
|
1049
|
+
* // Create a network client.
|
|
1050
|
+
* const networkClient = new AleoNetworkClient("http://api.explorer.provable.com/v1", undefined);
|
|
1051
|
+
*
|
|
762
1052
|
* const transaction = networkClient.getTransaction("at1handz9xjrqeynjrr0xay4pcsgtnczdksz3e584vfsgaz0dh0lyxq43a4wj");
|
|
763
1053
|
*/
|
|
764
1054
|
async getTransaction(transactionId) {
|
|
@@ -772,9 +1062,17 @@ class AleoNetworkClient {
|
|
|
772
1062
|
/**
|
|
773
1063
|
* Returns a confirmed transaction by its unique identifier.
|
|
774
1064
|
*
|
|
775
|
-
* @param {string} transactionId
|
|
1065
|
+
* @param {string} transactionId The transaction ID to fetch.
|
|
1066
|
+
* @returns {Promise<ConfirmedTransactionJSON>} A json object containing the confirmed transaction.
|
|
1067
|
+
*
|
|
776
1068
|
* @example
|
|
1069
|
+
* import { AleoNetworkClient, Account } from "@provablehq/sdk/mainnet.js";
|
|
1070
|
+
*
|
|
1071
|
+
* // Create a network client.
|
|
1072
|
+
* const networkClient = new AleoNetworkClient("http://api.explorer.provable.com/v1", undefined);
|
|
1073
|
+
*
|
|
777
1074
|
* const transaction = networkClient.getConfirmedTransaction("at1handz9xjrqeynjrr0xay4pcsgtnczdksz3e584vfsgaz0dh0lyxq43a4wj");
|
|
1075
|
+
* assert.equal(transaction.status, "confirmed");
|
|
778
1076
|
*/
|
|
779
1077
|
async getConfirmedTransaction(transactionId) {
|
|
780
1078
|
try {
|
|
@@ -788,13 +1086,16 @@ class AleoNetworkClient {
|
|
|
788
1086
|
* Returns a transaction as a wasm object. Getting a transaction of this type will allow the ability for the inputs,
|
|
789
1087
|
* outputs, and records to be searched for and displayed.
|
|
790
1088
|
*
|
|
1089
|
+
* @param {string} transactionId - The unique identifier of the transaction to fetch
|
|
1090
|
+
* @returns {Promise<Transaction>} A wasm object representation of the transaction.
|
|
1091
|
+
*
|
|
791
1092
|
* @example
|
|
792
1093
|
* const transactionObject = networkClient.getTransaction("at1handz9xjrqeynjrr0xay4pcsgtnczdksz3e584vfsgaz0dh0lyxq43a4wj");
|
|
793
1094
|
* // Get the transaction inputs as a JS array.
|
|
794
|
-
* const
|
|
1095
|
+
* const transactionInputs = transactionObject.inputs(true);
|
|
795
1096
|
*
|
|
796
1097
|
* // Get the transaction outputs as a JS object.
|
|
797
|
-
* const
|
|
1098
|
+
* const transactionOutputs = transactionObject.outputs(true);
|
|
798
1099
|
*
|
|
799
1100
|
* // Get any records generated in transitions in the transaction as a JS object.
|
|
800
1101
|
* const records = transactionObject.records();
|
|
@@ -805,10 +1106,6 @@ class AleoNetworkClient {
|
|
|
805
1106
|
*
|
|
806
1107
|
* // Get a JS representation of all inputs, outputs, and transaction metadata.
|
|
807
1108
|
* const transactionSummary = transactionObject.summary();
|
|
808
|
-
*
|
|
809
|
-
* @param {string} transactionId
|
|
810
|
-
* @example
|
|
811
|
-
* const transaction = networkClient.getTransactionObject("at1handz9xjrqeynjrr0xay4pcsgtnczdksz3e584vfsgaz0dh0lyxq43a4wj");
|
|
812
1109
|
*/
|
|
813
1110
|
async getTransactionObject(transactionId) {
|
|
814
1111
|
try {
|
|
@@ -822,8 +1119,15 @@ class AleoNetworkClient {
|
|
|
822
1119
|
/**
|
|
823
1120
|
* Returns the transactions present at the specified block height.
|
|
824
1121
|
*
|
|
825
|
-
* @param {number} blockHeight
|
|
1122
|
+
* @param {number} blockHeight The block height to fetch the confirmed transactions at.
|
|
1123
|
+
* @returns {Promise<Array<ConfirmedTransactionJSON>>} An array of confirmed transactions (in JSON format) for the block height.
|
|
1124
|
+
*
|
|
826
1125
|
* @example
|
|
1126
|
+
* import { AleoNetworkClient, Account } from "@provablehq/sdk/mainnet.js";
|
|
1127
|
+
*
|
|
1128
|
+
* // Create a network client.
|
|
1129
|
+
* const networkClient = new AleoNetworkClient("http://api.explorer.provable.com/v1", undefined);
|
|
1130
|
+
*
|
|
827
1131
|
* const transactions = networkClient.getTransactions(654);
|
|
828
1132
|
*/
|
|
829
1133
|
async getTransactions(blockHeight) {
|
|
@@ -837,9 +1141,16 @@ class AleoNetworkClient {
|
|
|
837
1141
|
/**
|
|
838
1142
|
* Returns the confirmed transactions present in the block with the specified block hash.
|
|
839
1143
|
*
|
|
840
|
-
* @param {string} blockHash
|
|
1144
|
+
* @param {string} blockHash The block hash to fetch the confirmed transactions at.
|
|
1145
|
+
* @returns {Promise<Array<ConfirmedTransactionJSON>>} An array of confirmed transactions (in JSON format) for the block hash.
|
|
1146
|
+
*
|
|
841
1147
|
* @example
|
|
842
|
-
*
|
|
1148
|
+
* import { AleoNetworkClient, Account } from "@provablehq/sdk/mainnet.js";
|
|
1149
|
+
*
|
|
1150
|
+
* // Create a network client.
|
|
1151
|
+
* const networkClient = new AleoNetworkClient("http://api.explorer.provable.com/v1", undefined);
|
|
1152
|
+
*
|
|
1153
|
+
* const transactions = networkClient.getTransactionsByBlockHash("ab19dklwl9vp63zu3hwg57wyhvmqf92fx5g8x0t6dr72py8r87pxupqfne5t9");
|
|
843
1154
|
*/
|
|
844
1155
|
async getTransactionsByBlockHash(blockHash) {
|
|
845
1156
|
try {
|
|
@@ -854,7 +1165,15 @@ class AleoNetworkClient {
|
|
|
854
1165
|
/**
|
|
855
1166
|
* Returns the transactions in the memory pool. This method requires access to a validator's REST API.
|
|
856
1167
|
*
|
|
1168
|
+
* @returns {Promise<Array<TransactionJSON>>} An array of transactions (in JSON format) currently in the mempool.
|
|
1169
|
+
*
|
|
857
1170
|
* @example
|
|
1171
|
+
* import { AleoNetworkClient, Account } from "@provablehq/sdk/mainnet.js";
|
|
1172
|
+
*
|
|
1173
|
+
* // Create a network client.
|
|
1174
|
+
* const networkClient = new AleoNetworkClient("http://api.explorer.provable.com/v1", undefined);
|
|
1175
|
+
*
|
|
1176
|
+
* // Get the current transactions in the mempool.
|
|
858
1177
|
* const transactions = networkClient.getTransactionsInMempool();
|
|
859
1178
|
*/
|
|
860
1179
|
async getTransactionsInMempool() {
|
|
@@ -867,7 +1186,8 @@ class AleoNetworkClient {
|
|
|
867
1186
|
}
|
|
868
1187
|
/**
|
|
869
1188
|
* Returns the transition ID of the transition corresponding to the ID of the input or output.
|
|
870
|
-
* @param {string} inputOrOutputID -
|
|
1189
|
+
* @param {string} inputOrOutputID - The unique identifier of the input or output to find the transition ID for
|
|
1190
|
+
* @returns {Promise<string>} - The transition ID of the input or output ID.
|
|
871
1191
|
*
|
|
872
1192
|
* @example
|
|
873
1193
|
* const transitionId = networkClient.getTransitionId("2429232855236830926144356377868449890830704336664550203176918782554219952323field");
|
|
@@ -883,18 +1203,20 @@ class AleoNetworkClient {
|
|
|
883
1203
|
/**
|
|
884
1204
|
* Submit an execute or deployment transaction to the Aleo network.
|
|
885
1205
|
*
|
|
886
|
-
* @param {Transaction | string} transaction
|
|
887
|
-
* @returns {string} - The transaction id of the submitted transaction or the resulting error
|
|
1206
|
+
* @param {Transaction | string} transaction - The transaction to submit, either as a Transaction object or string representation
|
|
1207
|
+
* @returns {Promise<string>} - The transaction id of the submitted transaction or the resulting error
|
|
888
1208
|
*/
|
|
889
1209
|
async submitTransaction(transaction) {
|
|
890
|
-
const transaction_string = transaction instanceof Transaction
|
|
1210
|
+
const transaction_string = transaction instanceof Transaction
|
|
1211
|
+
? transaction.toString()
|
|
1212
|
+
: transaction;
|
|
891
1213
|
try {
|
|
892
|
-
const response = await
|
|
1214
|
+
const response = await retryWithBackoff(() => this._sendPost(this.host + "/transaction/broadcast", {
|
|
893
1215
|
body: transaction_string,
|
|
894
1216
|
headers: Object.assign({}, this.headers, {
|
|
895
1217
|
"Content-Type": "application/json",
|
|
896
1218
|
}),
|
|
897
|
-
});
|
|
1219
|
+
}));
|
|
898
1220
|
try {
|
|
899
1221
|
const text = await response.text();
|
|
900
1222
|
return parseJSON(text);
|
|
@@ -910,50 +1232,102 @@ class AleoNetworkClient {
|
|
|
910
1232
|
/**
|
|
911
1233
|
* Submit a solution to the Aleo network.
|
|
912
1234
|
*
|
|
913
|
-
* @param {string} solution The string representation of the solution
|
|
1235
|
+
* @param {string} solution - The string representation of the solution to submit
|
|
1236
|
+
* @returns {Promise<string>} The solution id of the submitted solution or the resulting error.
|
|
914
1237
|
*/
|
|
915
1238
|
async submitSolution(solution) {
|
|
916
1239
|
try {
|
|
917
|
-
const response = await post(this.host + "/solution/broadcast", {
|
|
1240
|
+
const response = await retryWithBackoff(() => post(this.host + "/solution/broadcast", {
|
|
918
1241
|
body: solution,
|
|
919
1242
|
headers: Object.assign({}, this.headers, {
|
|
920
1243
|
"Content-Type": "application/json",
|
|
921
1244
|
}),
|
|
922
|
-
});
|
|
1245
|
+
}));
|
|
923
1246
|
try {
|
|
924
1247
|
const text = await response.text();
|
|
925
1248
|
return parseJSON(text);
|
|
926
1249
|
}
|
|
927
1250
|
catch (error) {
|
|
928
|
-
throw new Error(`Error posting
|
|
1251
|
+
throw new Error(`Error posting solution. Aleo network response: ${error.message}`);
|
|
929
1252
|
}
|
|
930
1253
|
}
|
|
931
1254
|
catch (error) {
|
|
932
|
-
throw new Error(`Error posting
|
|
1255
|
+
throw new Error(`Error posting solution: No response received: ${error.message}`);
|
|
933
1256
|
}
|
|
934
1257
|
}
|
|
935
1258
|
/**
|
|
936
|
-
* Await a transaction to be confirmed on the Aleo network.
|
|
1259
|
+
* Await a submitted transaction to be confirmed or rejected on the Aleo network.
|
|
1260
|
+
*
|
|
1261
|
+
* @param {string} transactionId - The transaction ID to wait for confirmation
|
|
1262
|
+
* @param {number} checkInterval - The interval in milliseconds to check for confirmation (default: 2000)
|
|
1263
|
+
* @param {number} timeout - The maximum time in milliseconds to wait for confirmation (default: 45000)
|
|
1264
|
+
* @returns {Promise<Transaction>} The confirmed transaction object that returns if the transaction is confirmed.
|
|
1265
|
+
*
|
|
1266
|
+
* @example
|
|
1267
|
+
* import { AleoNetworkClient, Account, ProgramManager } from "@provablehq/sdk/mainnet.js";
|
|
1268
|
+
*
|
|
1269
|
+
* // Create a network client and program manager.
|
|
1270
|
+
* const networkClient = new AleoNetworkClient("http://api.explorer.provable.com/v1", undefined);
|
|
1271
|
+
* const programManager = new ProgramManager(networkClient);
|
|
1272
|
+
*
|
|
1273
|
+
* // Set the account for the program manager.
|
|
1274
|
+
* programManager.setAccount(Account.fromCiphertext(process.env.ciphertext, process.env.password));
|
|
937
1275
|
*
|
|
938
|
-
*
|
|
1276
|
+
* // Build a transfer transaction.
|
|
1277
|
+
* const tx = await programManager.buildTransferPublicTransaction(100, "aleo1rhgdu77hgyqd3xjj8ucu3jj9r2krwz6mnzyd80gncr5fxcwlh5rsvzp9px", 0);
|
|
1278
|
+
*
|
|
1279
|
+
* // Submit the transaction to the network.
|
|
1280
|
+
* const transactionId = await networkClient.submitTransaction(tx);
|
|
1281
|
+
*
|
|
1282
|
+
* // Wait for the transaction to be confirmed.
|
|
1283
|
+
* const transaction = await networkClient.waitForTransactionConfirmation(transactionId);
|
|
939
1284
|
*/
|
|
940
|
-
async waitForTransactionConfirmation(transactionId, checkInterval = 2000,
|
|
941
|
-
timeout = 45000 // Timeout after 45 seconds
|
|
942
|
-
) {
|
|
1285
|
+
async waitForTransactionConfirmation(transactionId, checkInterval = 2000, timeout = 45000) {
|
|
943
1286
|
const startTime = Date.now();
|
|
944
1287
|
return new Promise((resolve, reject) => {
|
|
945
1288
|
const interval = setInterval(async () => {
|
|
1289
|
+
const elapsed = Date.now() - startTime;
|
|
1290
|
+
if (elapsed > timeout) {
|
|
1291
|
+
clearInterval(interval);
|
|
1292
|
+
return reject(new Error(`Transaction ${transactionId} did not appear after the timeout period of ${interval}ms - consider resubmitting the transaction`));
|
|
1293
|
+
}
|
|
946
1294
|
try {
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
if (
|
|
1295
|
+
const res = await fetch(`${this.host}/transaction/confirmed/${transactionId}`, {
|
|
1296
|
+
headers: this.headers,
|
|
1297
|
+
});
|
|
1298
|
+
if (!res.ok) {
|
|
1299
|
+
let text = "";
|
|
1300
|
+
try {
|
|
1301
|
+
text = await res.text();
|
|
1302
|
+
console.warn("Response text from server:", text);
|
|
1303
|
+
}
|
|
1304
|
+
catch (err) {
|
|
1305
|
+
console.warn("Failed to read response text:", err);
|
|
1306
|
+
}
|
|
1307
|
+
// If the transaction ID is malformed (e.g. invalid checksum, wrong length),
|
|
1308
|
+
// the API returns a 4XX with "Invalid URL" — we treat this as a fatal error and stop polling.
|
|
1309
|
+
if (res.status >= 400 &&
|
|
1310
|
+
res.status < 500 &&
|
|
1311
|
+
text.includes("Invalid URL")) {
|
|
1312
|
+
clearInterval(interval);
|
|
1313
|
+
return reject(new Error(`Malformed transaction ID: ${text}`));
|
|
1314
|
+
}
|
|
1315
|
+
// Log and continue polling for 404s or 5XX errors in case a tx doesn't exist yet
|
|
1316
|
+
console.warn("Non-OK response (retrying):", res.status, text);
|
|
1317
|
+
return;
|
|
1318
|
+
}
|
|
1319
|
+
const data = parseJSON(await res.text());
|
|
1320
|
+
if (data?.status === "accepted") {
|
|
951
1321
|
clearInterval(interval);
|
|
952
|
-
|
|
1322
|
+
return resolve(data);
|
|
1323
|
+
}
|
|
1324
|
+
if (data?.status === "rejected") {
|
|
1325
|
+
clearInterval(interval);
|
|
1326
|
+
return reject(new Error(`Transaction ${transactionId} was rejected by the network. Ensure that the account paying the fee has enough credits and that the inputs to the on-chain function are valid.`));
|
|
953
1327
|
}
|
|
954
1328
|
}
|
|
955
|
-
catch (
|
|
956
|
-
console.error("
|
|
1329
|
+
catch (err) {
|
|
1330
|
+
console.error("Polling error:", err);
|
|
957
1331
|
}
|
|
958
1332
|
}, checkInterval);
|
|
959
1333
|
});
|
|
@@ -1472,12 +1846,21 @@ class ProgramManager {
|
|
|
1472
1846
|
* @param { FunctionKeyProvider | undefined } keyProvider A key provider that implements {@link FunctionKeyProvider} interface
|
|
1473
1847
|
* @param { RecordProvider | undefined } recordProvider A record provider that implements {@link RecordProvider} interface
|
|
1474
1848
|
*/
|
|
1475
|
-
constructor(host, keyProvider, recordProvider) {
|
|
1476
|
-
this.host = host ? host :
|
|
1477
|
-
this.networkClient = new AleoNetworkClient(this.host);
|
|
1849
|
+
constructor(host, keyProvider, recordProvider, networkClientOptions) {
|
|
1850
|
+
this.host = host ? host : "https://api.explorer.provable.com/v1";
|
|
1851
|
+
this.networkClient = new AleoNetworkClient(this.host, networkClientOptions);
|
|
1478
1852
|
this.keyProvider = keyProvider ? keyProvider : new AleoKeyProvider();
|
|
1479
1853
|
this.recordProvider = recordProvider;
|
|
1480
1854
|
}
|
|
1855
|
+
/**
|
|
1856
|
+
* Check if the fee is sufficient to pay for the transaction
|
|
1857
|
+
*/
|
|
1858
|
+
async checkFee(address, feeAmount) {
|
|
1859
|
+
const balance = BigInt(await this.networkClient.getPublicBalance(address));
|
|
1860
|
+
if (feeAmount > balance) {
|
|
1861
|
+
throw Error(`The desired execution requires a fee of ${feeAmount} microcredits, but the account paying the fee has ${balance} microcredits available.`);
|
|
1862
|
+
}
|
|
1863
|
+
}
|
|
1481
1864
|
/**
|
|
1482
1865
|
* Set the account to use for transaction submission to the Aleo network
|
|
1483
1866
|
*
|
|
@@ -1511,11 +1894,46 @@ class ProgramManager {
|
|
|
1511
1894
|
setRecordProvider(recordProvider) {
|
|
1512
1895
|
this.recordProvider = recordProvider;
|
|
1513
1896
|
}
|
|
1897
|
+
/**
|
|
1898
|
+
* Set a header in the `AleoNetworkClient`s header map
|
|
1899
|
+
*
|
|
1900
|
+
* @param {string} headerName The name of the header to set
|
|
1901
|
+
* @param {string} value The header value
|
|
1902
|
+
*
|
|
1903
|
+
* @example
|
|
1904
|
+
* import { ProgramManager } from "@provablehq/sdk/mainnet.js";
|
|
1905
|
+
*
|
|
1906
|
+
* // Create a ProgramManager
|
|
1907
|
+
* const programManager = new ProgramManager("https://api.explorer.provable.com/v1");
|
|
1908
|
+
*
|
|
1909
|
+
* // Set the value of the `Accept-Language` header to `en-US`
|
|
1910
|
+
* programManager.setHeader('Accept-Language', 'en-US');
|
|
1911
|
+
*/
|
|
1912
|
+
setHeader(headerName, value) {
|
|
1913
|
+
this.networkClient.headers[headerName] = value;
|
|
1914
|
+
}
|
|
1915
|
+
/**
|
|
1916
|
+
* Remove a header from the `AleoNetworkClient`s header map
|
|
1917
|
+
*
|
|
1918
|
+
* @param {string} headerName The name of the header to be removed
|
|
1919
|
+
*
|
|
1920
|
+
* @example
|
|
1921
|
+
* import { ProgramManager } from "@provablehq/sdk/mainnet.js";
|
|
1922
|
+
*
|
|
1923
|
+
* // Create a ProgramManager
|
|
1924
|
+
* const programManager = new ProgramManager("https://api.explorer.provable.com/v1");
|
|
1925
|
+
*
|
|
1926
|
+
* // Remove the default `X-Aleo-SDK-Version` header
|
|
1927
|
+
* programManager.removeHeader('X-Aleo-SDK-Version');
|
|
1928
|
+
*/
|
|
1929
|
+
removeHeader(headerName) {
|
|
1930
|
+
delete this.networkClient.headers[headerName];
|
|
1931
|
+
}
|
|
1514
1932
|
/**
|
|
1515
1933
|
* Builds a deployment transaction for submission to the Aleo network.
|
|
1516
1934
|
*
|
|
1517
1935
|
* @param {string} program Program source code
|
|
1518
|
-
* @param {number} fee
|
|
1936
|
+
* @param {number} priorityFee The optional priority fee to be paid for that transaction.
|
|
1519
1937
|
* @param {boolean} privateFee Use a private record to pay the fee. If false this will use the account's public credit balance
|
|
1520
1938
|
* @param {RecordSearchParams | undefined} recordSearchParams Optional parameters for searching for a record to use pay the deployment fee
|
|
1521
1939
|
* @param {string | RecordPlaintext | undefined} feeRecord Optional Fee record to use for the transaction
|
|
@@ -1537,7 +1955,7 @@ class ProgramManager {
|
|
|
1537
1955
|
* programManager.setAccount(Account);
|
|
1538
1956
|
*
|
|
1539
1957
|
* // Define a fee in credits
|
|
1540
|
-
* const
|
|
1958
|
+
* const priorityFee = 0.0;
|
|
1541
1959
|
*
|
|
1542
1960
|
* // Create the deployment transaction.
|
|
1543
1961
|
* const tx = await programManager.buildDeploymentTransaction(program, fee, false);
|
|
@@ -1549,10 +1967,17 @@ class ProgramManager {
|
|
|
1549
1967
|
* assert(transaction.id() === tx.id());
|
|
1550
1968
|
* }, 20000);
|
|
1551
1969
|
*/
|
|
1552
|
-
async buildDeploymentTransaction(program,
|
|
1970
|
+
async buildDeploymentTransaction(program, priorityFee, privateFee, recordSearchParams, feeRecord, privateKey) {
|
|
1971
|
+
// Ensure the program is valid.
|
|
1972
|
+
let programObject;
|
|
1973
|
+
try {
|
|
1974
|
+
programObject = Program.fromString(program);
|
|
1975
|
+
}
|
|
1976
|
+
catch (e) {
|
|
1977
|
+
logAndThrow(`Error parsing program: '${e.message}'. Please ensure the program is valid.`);
|
|
1978
|
+
}
|
|
1553
1979
|
// Ensure the program is valid and does not exist on the network
|
|
1554
1980
|
try {
|
|
1555
|
-
const programObject = Program.fromString(program);
|
|
1556
1981
|
let programSource;
|
|
1557
1982
|
try {
|
|
1558
1983
|
programSource = await this.networkClient.getProgram(programObject.id());
|
|
@@ -1561,8 +1986,8 @@ class ProgramManager {
|
|
|
1561
1986
|
// Program does not exist on the network, deployment can proceed
|
|
1562
1987
|
console.log(`Program ${programObject.id()} does not exist on the network, deploying...`);
|
|
1563
1988
|
}
|
|
1564
|
-
if (typeof programSource
|
|
1565
|
-
throw (`Program ${programObject.id()} already exists on the network, please rename your program`);
|
|
1989
|
+
if (typeof programSource === "string") {
|
|
1990
|
+
throw Error(`Program ${programObject.id()} already exists on the network, please rename your program`);
|
|
1566
1991
|
}
|
|
1567
1992
|
}
|
|
1568
1993
|
catch (e) {
|
|
@@ -1570,15 +1995,18 @@ class ProgramManager {
|
|
|
1570
1995
|
}
|
|
1571
1996
|
// Get the private key from the account if it is not provided in the parameters
|
|
1572
1997
|
let deploymentPrivateKey = privateKey;
|
|
1573
|
-
if (typeof privateKey === "undefined" &&
|
|
1998
|
+
if (typeof privateKey === "undefined" &&
|
|
1999
|
+
typeof this.account !== "undefined") {
|
|
1574
2000
|
deploymentPrivateKey = this.account.privateKey();
|
|
1575
2001
|
}
|
|
1576
2002
|
if (typeof deploymentPrivateKey === "undefined") {
|
|
1577
|
-
throw
|
|
2003
|
+
throw "No private key provided and no private key set in the ProgramManager";
|
|
1578
2004
|
}
|
|
1579
2005
|
// Get the fee record from the account if it is not provided in the parameters
|
|
1580
2006
|
try {
|
|
1581
|
-
feeRecord = privateFee
|
|
2007
|
+
feeRecord = privateFee
|
|
2008
|
+
? (await this.getCreditsRecord(priorityFee, [], feeRecord, recordSearchParams))
|
|
2009
|
+
: undefined;
|
|
1582
2010
|
}
|
|
1583
2011
|
catch (e) {
|
|
1584
2012
|
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.`);
|
|
@@ -1586,7 +2014,9 @@ class ProgramManager {
|
|
|
1586
2014
|
// Get the proving and verifying keys from the key provider
|
|
1587
2015
|
let feeKeys;
|
|
1588
2016
|
try {
|
|
1589
|
-
feeKeys = privateFee
|
|
2017
|
+
feeKeys = privateFee
|
|
2018
|
+
? await this.keyProvider.feePrivateKeys()
|
|
2019
|
+
: await this.keyProvider.feePublicKeys();
|
|
1590
2020
|
}
|
|
1591
2021
|
catch (e) {
|
|
1592
2022
|
logAndThrow(`Error finding fee keys. Key finder response: '${e.message}'. Please ensure your key provider is configured correctly.`);
|
|
@@ -1600,14 +2030,14 @@ class ProgramManager {
|
|
|
1600
2030
|
catch (e) {
|
|
1601
2031
|
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.`);
|
|
1602
2032
|
}
|
|
1603
|
-
// Build a deployment transaction
|
|
1604
|
-
return await ProgramManager$1.buildDeploymentTransaction(deploymentPrivateKey, program,
|
|
2033
|
+
// Build a deployment transaction
|
|
2034
|
+
return await ProgramManager$1.buildDeploymentTransaction(deploymentPrivateKey, program, priorityFee, feeRecord, this.host, imports, feeProvingKey, feeVerifyingKey);
|
|
1605
2035
|
}
|
|
1606
2036
|
/**
|
|
1607
2037
|
* Deploy an Aleo program to the Aleo network
|
|
1608
2038
|
*
|
|
1609
2039
|
* @param {string} program Program source code
|
|
1610
|
-
* @param {number} fee
|
|
2040
|
+
* @param {number} priorityFee The optional fee to be paid for the transaction
|
|
1611
2041
|
* @param {boolean} privateFee Use a private record to pay the fee. If false this will use the account's public credit balance
|
|
1612
2042
|
* @param {RecordSearchParams | undefined} recordSearchParams Optional parameters for searching for a record to used pay the deployment fee
|
|
1613
2043
|
* @param {string | RecordPlaintext | undefined} feeRecord Optional Fee record to use for the transaction
|
|
@@ -1628,7 +2058,7 @@ class ProgramManager {
|
|
|
1628
2058
|
* const programManager = new ProgramManager("https://api.explorer.provable.com/v1", keyProvider, recordProvider);
|
|
1629
2059
|
*
|
|
1630
2060
|
* // Define a fee in credits
|
|
1631
|
-
* const
|
|
2061
|
+
* const priorityFee = 0.0;
|
|
1632
2062
|
*
|
|
1633
2063
|
* // Deploy the program
|
|
1634
2064
|
* const tx_id = await programManager.deploy(program, fee, false);
|
|
@@ -1639,8 +2069,20 @@ class ProgramManager {
|
|
|
1639
2069
|
* assert(transaction.id() === tx_id);
|
|
1640
2070
|
* }, 20000);
|
|
1641
2071
|
*/
|
|
1642
|
-
async deploy(program,
|
|
1643
|
-
const tx = await this.buildDeploymentTransaction(program,
|
|
2072
|
+
async deploy(program, priorityFee, privateFee, recordSearchParams, feeRecord, privateKey) {
|
|
2073
|
+
const tx = (await this.buildDeploymentTransaction(program, priorityFee, privateFee, recordSearchParams, feeRecord, privateKey));
|
|
2074
|
+
let feeAddress;
|
|
2075
|
+
if (typeof privateKey !== "undefined") {
|
|
2076
|
+
feeAddress = Address.from_private_key(privateKey);
|
|
2077
|
+
}
|
|
2078
|
+
else if (this.account !== undefined) {
|
|
2079
|
+
feeAddress = this.account?.address();
|
|
2080
|
+
}
|
|
2081
|
+
else {
|
|
2082
|
+
throw Error("No private key provided and no private key set in the ProgramManager. Please set an account or provide a private key.");
|
|
2083
|
+
}
|
|
2084
|
+
// Check if the account has sufficient credits to pay for the transaction
|
|
2085
|
+
await this.checkFee(feeAddress.to_string(), tx.feeAmount());
|
|
1644
2086
|
return await this.networkClient.submitTransaction(tx);
|
|
1645
2087
|
}
|
|
1646
2088
|
/**
|
|
@@ -1665,7 +2107,7 @@ class ProgramManager {
|
|
|
1665
2107
|
* const tx = await programManager.buildExecutionTransaction({
|
|
1666
2108
|
* programName: "hello_hello.aleo",
|
|
1667
2109
|
* functionName: "hello_hello",
|
|
1668
|
-
*
|
|
2110
|
+
* priorityFee: 0.0,
|
|
1669
2111
|
* privateFee: false,
|
|
1670
2112
|
* inputs: ["5u32", "5u32"],
|
|
1671
2113
|
* keySearchParams: { "cacheKey": "hello_hello:hello" }
|
|
@@ -1682,7 +2124,7 @@ class ProgramManager {
|
|
|
1682
2124
|
*/
|
|
1683
2125
|
async buildExecutionTransaction(options) {
|
|
1684
2126
|
// Destructure the options object to access the parameters
|
|
1685
|
-
const { programName, functionName,
|
|
2127
|
+
const { programName, functionName, priorityFee, privateFee, inputs, recordSearchParams, keySearchParams, privateKey, offlineQuery, } = options;
|
|
1686
2128
|
let feeRecord = options.feeRecord;
|
|
1687
2129
|
let provingKey = options.provingKey;
|
|
1688
2130
|
let verifyingKey = options.verifyingKey;
|
|
@@ -1702,15 +2144,18 @@ class ProgramManager {
|
|
|
1702
2144
|
}
|
|
1703
2145
|
// Get the private key from the account if it is not provided in the parameters
|
|
1704
2146
|
let executionPrivateKey = privateKey;
|
|
1705
|
-
if (typeof privateKey === "undefined" &&
|
|
2147
|
+
if (typeof privateKey === "undefined" &&
|
|
2148
|
+
typeof this.account !== "undefined") {
|
|
1706
2149
|
executionPrivateKey = this.account.privateKey();
|
|
1707
2150
|
}
|
|
1708
2151
|
if (typeof executionPrivateKey === "undefined") {
|
|
1709
|
-
throw
|
|
2152
|
+
throw "No private key provided and no private key set in the ProgramManager";
|
|
1710
2153
|
}
|
|
1711
2154
|
// Get the fee record from the account if it is not provided in the parameters
|
|
1712
2155
|
try {
|
|
1713
|
-
feeRecord = privateFee
|
|
2156
|
+
feeRecord = privateFee
|
|
2157
|
+
? (await this.getCreditsRecord(priorityFee, [], feeRecord, recordSearchParams))
|
|
2158
|
+
: undefined;
|
|
1714
2159
|
}
|
|
1715
2160
|
catch (e) {
|
|
1716
2161
|
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.`);
|
|
@@ -1718,7 +2163,9 @@ class ProgramManager {
|
|
|
1718
2163
|
// Get the fee proving and verifying keys from the key provider
|
|
1719
2164
|
let feeKeys;
|
|
1720
2165
|
try {
|
|
1721
|
-
feeKeys = privateFee
|
|
2166
|
+
feeKeys = privateFee
|
|
2167
|
+
? await this.keyProvider.feePrivateKeys()
|
|
2168
|
+
: await this.keyProvider.feePublicKeys();
|
|
1722
2169
|
}
|
|
1723
2170
|
catch (e) {
|
|
1724
2171
|
logAndThrow(`Error finding fee keys. Key finder response: '${e.message}'. Please ensure your key provider is configured correctly.`);
|
|
@@ -1727,7 +2174,7 @@ class ProgramManager {
|
|
|
1727
2174
|
// If the function proving and verifying keys are not provided, attempt to find them using the key provider
|
|
1728
2175
|
if (!provingKey || !verifyingKey) {
|
|
1729
2176
|
try {
|
|
1730
|
-
[provingKey, verifyingKey] = await this.keyProvider.functionKeys(keySearchParams);
|
|
2177
|
+
[provingKey, verifyingKey] = (await this.keyProvider.functionKeys(keySearchParams));
|
|
1731
2178
|
}
|
|
1732
2179
|
catch (e) {
|
|
1733
2180
|
console.log(`Function keys not found. Key finder response: '${e}'. The function keys will be synthesized`);
|
|
@@ -1737,14 +2184,14 @@ class ProgramManager {
|
|
|
1737
2184
|
const numberOfImports = Program.fromString(program).getImports().length;
|
|
1738
2185
|
if (numberOfImports > 0 && !imports) {
|
|
1739
2186
|
try {
|
|
1740
|
-
imports = await this.networkClient.getProgramImports(programName);
|
|
2187
|
+
imports = (await this.networkClient.getProgramImports(programName));
|
|
1741
2188
|
}
|
|
1742
2189
|
catch (e) {
|
|
1743
2190
|
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.`);
|
|
1744
2191
|
}
|
|
1745
2192
|
}
|
|
1746
|
-
// Build an execution transaction
|
|
1747
|
-
return await ProgramManager$1.buildExecutionTransaction(executionPrivateKey, program, functionName, inputs,
|
|
2193
|
+
// Build an execution transaction
|
|
2194
|
+
return await ProgramManager$1.buildExecutionTransaction(executionPrivateKey, program, functionName, inputs, priorityFee, feeRecord, this.host, imports, provingKey, verifyingKey, feeProvingKey, feeVerifyingKey, offlineQuery);
|
|
1748
2195
|
}
|
|
1749
2196
|
/**
|
|
1750
2197
|
* Builds an execution transaction for submission to the Aleo network.
|
|
@@ -1768,7 +2215,7 @@ class ProgramManager {
|
|
|
1768
2215
|
* const tx_id = await programManager.execute({
|
|
1769
2216
|
* programName: "hello_hello.aleo",
|
|
1770
2217
|
* functionName: "hello_hello",
|
|
1771
|
-
*
|
|
2218
|
+
* priorityFee: 0.0,
|
|
1772
2219
|
* privateFee: false,
|
|
1773
2220
|
* inputs: ["5u32", "5u32"],
|
|
1774
2221
|
* keySearchParams: { "cacheKey": "hello_hello:hello" }
|
|
@@ -1782,6 +2229,18 @@ class ProgramManager {
|
|
|
1782
2229
|
*/
|
|
1783
2230
|
async execute(options) {
|
|
1784
2231
|
const tx = await this.buildExecutionTransaction(options);
|
|
2232
|
+
let feeAddress;
|
|
2233
|
+
if (typeof options.privateKey !== "undefined") {
|
|
2234
|
+
feeAddress = Address.from_private_key(options.privateKey);
|
|
2235
|
+
}
|
|
2236
|
+
else if (this.account !== undefined) {
|
|
2237
|
+
feeAddress = this.account?.address();
|
|
2238
|
+
}
|
|
2239
|
+
else {
|
|
2240
|
+
throw Error("No private key provided and no private key set in the ProgramManager. Please set an account or provide a private key.");
|
|
2241
|
+
}
|
|
2242
|
+
// Check if the account has sufficient credits to pay for the transaction
|
|
2243
|
+
await this.checkFee(feeAddress.to_string(), tx.feeAmount());
|
|
1785
2244
|
return await this.networkClient.submitTransaction(tx);
|
|
1786
2245
|
}
|
|
1787
2246
|
/**
|
|
@@ -1819,16 +2278,17 @@ class ProgramManager {
|
|
|
1819
2278
|
async run(program, function_name, inputs, proveExecution, imports, keySearchParams, provingKey, verifyingKey, privateKey, offlineQuery) {
|
|
1820
2279
|
// Get the private key from the account if it is not provided in the parameters
|
|
1821
2280
|
let executionPrivateKey = privateKey;
|
|
1822
|
-
if (typeof privateKey === "undefined" &&
|
|
2281
|
+
if (typeof privateKey === "undefined" &&
|
|
2282
|
+
typeof this.account !== "undefined") {
|
|
1823
2283
|
executionPrivateKey = this.account.privateKey();
|
|
1824
2284
|
}
|
|
1825
2285
|
if (typeof executionPrivateKey === "undefined") {
|
|
1826
|
-
throw
|
|
2286
|
+
throw "No private key provided and no private key set in the ProgramManager";
|
|
1827
2287
|
}
|
|
1828
2288
|
// If the function proving and verifying keys are not provided, attempt to find them using the key provider
|
|
1829
2289
|
if (!provingKey || !verifyingKey) {
|
|
1830
2290
|
try {
|
|
1831
|
-
[provingKey, verifyingKey] = await this.keyProvider.functionKeys(keySearchParams);
|
|
2291
|
+
[provingKey, verifyingKey] = (await this.keyProvider.functionKeys(keySearchParams));
|
|
1832
2292
|
}
|
|
1833
2293
|
catch (e) {
|
|
1834
2294
|
console.log(`Function keys not found. Key finder response: '${e}'. The function keys will be synthesized`);
|
|
@@ -1845,7 +2305,7 @@ class ProgramManager {
|
|
|
1845
2305
|
*
|
|
1846
2306
|
* @param {RecordPlaintext | string} recordOne First credits record to join
|
|
1847
2307
|
* @param {RecordPlaintext | string} recordTwo Second credits record to join
|
|
1848
|
-
* @param {number} fee
|
|
2308
|
+
* @param {number} priorityFee The optional priority fee to be paid for the transaction
|
|
1849
2309
|
* @param {boolean} privateFee Use a private record to pay the fee. If false this will use the account's public credit balance
|
|
1850
2310
|
* @param {RecordSearchParams | undefined} recordSearchParams Optional parameters for finding the fee record to use to pay the fee for the join transaction
|
|
1851
2311
|
* @param {RecordPlaintext | string | undefined} feeRecord Fee record to use for the join transaction
|
|
@@ -1874,20 +2334,28 @@ class ProgramManager {
|
|
|
1874
2334
|
* assert(transaction.id() === tx_id);
|
|
1875
2335
|
* }, 10000);
|
|
1876
2336
|
*/
|
|
1877
|
-
async join(recordOne, recordTwo,
|
|
1878
|
-
// Get the private key from the account if it is not provided in the parameters
|
|
2337
|
+
async join(recordOne, recordTwo, priorityFee, privateFee, recordSearchParams, feeRecord, privateKey, offlineQuery) {
|
|
2338
|
+
// Get the private key from the account if it is not provided in the parameters and assign the fee address
|
|
1879
2339
|
let executionPrivateKey = privateKey;
|
|
1880
|
-
|
|
2340
|
+
let feeAddress;
|
|
2341
|
+
if (typeof privateKey === "undefined" &&
|
|
2342
|
+
typeof this.account !== "undefined") {
|
|
1881
2343
|
executionPrivateKey = this.account.privateKey();
|
|
2344
|
+
feeAddress = this.account?.address();
|
|
1882
2345
|
}
|
|
1883
|
-
if (typeof executionPrivateKey === "undefined") {
|
|
1884
|
-
throw
|
|
2346
|
+
else if (typeof executionPrivateKey === "undefined") {
|
|
2347
|
+
throw "No private key provided and no private key set in the ProgramManager";
|
|
2348
|
+
}
|
|
2349
|
+
else {
|
|
2350
|
+
feeAddress = Address.from_private_key(executionPrivateKey);
|
|
1885
2351
|
}
|
|
1886
2352
|
// Get the proving and verifying keys from the key provider
|
|
1887
2353
|
let feeKeys;
|
|
1888
2354
|
let joinKeys;
|
|
1889
2355
|
try {
|
|
1890
|
-
feeKeys = privateFee
|
|
2356
|
+
feeKeys = privateFee
|
|
2357
|
+
? await this.keyProvider.feePrivateKeys()
|
|
2358
|
+
: await this.keyProvider.feePublicKeys();
|
|
1891
2359
|
joinKeys = await this.keyProvider.joinKeys();
|
|
1892
2360
|
}
|
|
1893
2361
|
catch (e) {
|
|
@@ -1897,21 +2365,31 @@ class ProgramManager {
|
|
|
1897
2365
|
const [joinProvingKey, joinVerifyingKey] = joinKeys;
|
|
1898
2366
|
// Get the fee record from the account if it is not provided in the parameters
|
|
1899
2367
|
try {
|
|
1900
|
-
feeRecord = privateFee
|
|
2368
|
+
feeRecord = privateFee
|
|
2369
|
+
? (await this.getCreditsRecord(priorityFee, [], feeRecord, recordSearchParams))
|
|
2370
|
+
: undefined;
|
|
1901
2371
|
}
|
|
1902
2372
|
catch (e) {
|
|
1903
2373
|
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.`);
|
|
1904
2374
|
}
|
|
1905
2375
|
// Validate the records provided are valid plaintext records
|
|
1906
2376
|
try {
|
|
1907
|
-
recordOne =
|
|
1908
|
-
|
|
2377
|
+
recordOne =
|
|
2378
|
+
recordOne instanceof RecordPlaintext
|
|
2379
|
+
? recordOne
|
|
2380
|
+
: RecordPlaintext.fromString(recordOne);
|
|
2381
|
+
recordTwo =
|
|
2382
|
+
recordTwo instanceof RecordPlaintext
|
|
2383
|
+
? recordTwo
|
|
2384
|
+
: RecordPlaintext.fromString(recordTwo);
|
|
1909
2385
|
}
|
|
1910
2386
|
catch (e) {
|
|
1911
|
-
logAndThrow(
|
|
2387
|
+
logAndThrow("Records provided are not valid. Please ensure they are valid plaintext records.");
|
|
1912
2388
|
}
|
|
1913
2389
|
// Build an execution transaction and submit it to the network
|
|
1914
|
-
const tx = await ProgramManager$1.buildJoinTransaction(executionPrivateKey, recordOne, recordTwo,
|
|
2390
|
+
const tx = await ProgramManager$1.buildJoinTransaction(executionPrivateKey, recordOne, recordTwo, priorityFee, feeRecord, this.host, joinProvingKey, joinVerifyingKey, feeProvingKey, feeVerifyingKey, offlineQuery);
|
|
2391
|
+
// Check if the account has sufficient credits to pay for the transaction
|
|
2392
|
+
await this.checkFee(feeAddress.to_string(), tx.feeAmount());
|
|
1915
2393
|
return await this.networkClient.submitTransaction(tx);
|
|
1916
2394
|
}
|
|
1917
2395
|
/**
|
|
@@ -1946,11 +2424,12 @@ class ProgramManager {
|
|
|
1946
2424
|
async split(splitAmount, amountRecord, privateKey, offlineQuery) {
|
|
1947
2425
|
// Get the private key from the account if it is not provided in the parameters
|
|
1948
2426
|
let executionPrivateKey = privateKey;
|
|
1949
|
-
if (typeof
|
|
2427
|
+
if (typeof privateKey === "undefined" &&
|
|
2428
|
+
typeof this.account !== "undefined") {
|
|
1950
2429
|
executionPrivateKey = this.account.privateKey();
|
|
1951
2430
|
}
|
|
1952
2431
|
if (typeof executionPrivateKey === "undefined") {
|
|
1953
|
-
throw
|
|
2432
|
+
throw "No private key provided and no private key set in the ProgramManager";
|
|
1954
2433
|
}
|
|
1955
2434
|
// Get the split keys from the key provider
|
|
1956
2435
|
let splitKeys;
|
|
@@ -1963,7 +2442,10 @@ class ProgramManager {
|
|
|
1963
2442
|
const [splitProvingKey, splitVerifyingKey] = splitKeys;
|
|
1964
2443
|
// Validate the record to be split
|
|
1965
2444
|
try {
|
|
1966
|
-
amountRecord =
|
|
2445
|
+
amountRecord =
|
|
2446
|
+
amountRecord instanceof RecordPlaintext
|
|
2447
|
+
? amountRecord
|
|
2448
|
+
: RecordPlaintext.fromString(amountRecord);
|
|
1967
2449
|
}
|
|
1968
2450
|
catch (e) {
|
|
1969
2451
|
logAndThrow("Record provided is not valid. Please ensure it is a valid plaintext record.");
|
|
@@ -1998,7 +2480,10 @@ class ProgramManager {
|
|
|
1998
2480
|
try {
|
|
1999
2481
|
imports = await this.networkClient.getProgramImports(program);
|
|
2000
2482
|
const keyPair = await ProgramManager$1.synthesizeKeyPair(executionPrivateKey, program, function_id, inputs, imports);
|
|
2001
|
-
return [
|
|
2483
|
+
return [
|
|
2484
|
+
keyPair.provingKey(),
|
|
2485
|
+
keyPair.verifyingKey(),
|
|
2486
|
+
];
|
|
2002
2487
|
}
|
|
2003
2488
|
catch (e) {
|
|
2004
2489
|
logAndThrow(`Could not synthesize keys - error ${e.message}. Please ensure the program is valid and the inputs are correct.`);
|
|
@@ -2010,7 +2495,7 @@ class ProgramManager {
|
|
|
2010
2495
|
* @param {number} amount The amount of credits to transfer
|
|
2011
2496
|
* @param {string} recipient The recipient of the transfer
|
|
2012
2497
|
* @param {string} transferType The type of transfer to perform - options: 'private', 'privateToPublic', 'public', 'publicToPrivate'
|
|
2013
|
-
* @param {number}
|
|
2498
|
+
* @param {number} priorityFee The optional priority fee to be paid for the transaction
|
|
2014
2499
|
* @param {boolean} privateFee Use a private record to pay the fee. If false this will use the account's public credit balance
|
|
2015
2500
|
* @param {RecordSearchParams | undefined} recordSearchParams Optional parameters for finding the amount and fee records for the transfer transaction
|
|
2016
2501
|
* @param {RecordPlaintext | string} amountRecord Optional amount record to use for the transfer
|
|
@@ -2039,23 +2524,26 @@ class ProgramManager {
|
|
|
2039
2524
|
* assert(transaction.id() === tx.id());
|
|
2040
2525
|
* }, 10000);
|
|
2041
2526
|
*/
|
|
2042
|
-
async buildTransferTransaction(amount, recipient, transferType,
|
|
2527
|
+
async buildTransferTransaction(amount, recipient, transferType, priorityFee, privateFee, recordSearchParams, amountRecord, feeRecord, privateKey, offlineQuery) {
|
|
2043
2528
|
// Validate the transfer type
|
|
2044
2529
|
transferType = validateTransferType(transferType);
|
|
2045
2530
|
// Get the private key from the account if it is not provided in the parameters
|
|
2046
2531
|
let executionPrivateKey = privateKey;
|
|
2047
|
-
if (typeof executionPrivateKey === "undefined" &&
|
|
2532
|
+
if (typeof executionPrivateKey === "undefined" &&
|
|
2533
|
+
typeof this.account !== "undefined") {
|
|
2048
2534
|
executionPrivateKey = this.account.privateKey();
|
|
2049
2535
|
}
|
|
2050
2536
|
if (typeof executionPrivateKey === "undefined") {
|
|
2051
|
-
throw
|
|
2537
|
+
throw "No private key provided and no private key set in the ProgramManager";
|
|
2052
2538
|
}
|
|
2053
2539
|
// Get the proving and verifying keys from the key provider
|
|
2054
2540
|
let feeKeys;
|
|
2055
2541
|
let transferKeys;
|
|
2056
2542
|
try {
|
|
2057
|
-
feeKeys = privateFee
|
|
2058
|
-
|
|
2543
|
+
feeKeys = privateFee
|
|
2544
|
+
? await this.keyProvider.feePrivateKeys()
|
|
2545
|
+
: await this.keyProvider.feePublicKeys();
|
|
2546
|
+
transferKeys = (await this.keyProvider.transferKeys(transferType));
|
|
2059
2547
|
}
|
|
2060
2548
|
catch (e) {
|
|
2061
2549
|
logAndThrow(`Error finding fee keys. Key finder response: '${e.message}'. Please ensure your key provider is configured correctly.`);
|
|
@@ -2068,26 +2556,28 @@ class ProgramManager {
|
|
|
2068
2556
|
const nonces = [];
|
|
2069
2557
|
if (requiresAmountRecord(transferType)) {
|
|
2070
2558
|
// If the transfer type is private and requires an amount record, get it from the record provider
|
|
2071
|
-
amountRecord = await this.getCreditsRecord(
|
|
2559
|
+
amountRecord = (await this.getCreditsRecord(priorityFee, [], amountRecord, recordSearchParams));
|
|
2072
2560
|
nonces.push(amountRecord.nonce());
|
|
2073
2561
|
}
|
|
2074
2562
|
else {
|
|
2075
2563
|
amountRecord = undefined;
|
|
2076
2564
|
}
|
|
2077
|
-
feeRecord = privateFee
|
|
2565
|
+
feeRecord = privateFee
|
|
2566
|
+
? (await this.getCreditsRecord(priorityFee, nonces, feeRecord, recordSearchParams))
|
|
2567
|
+
: undefined;
|
|
2078
2568
|
}
|
|
2079
2569
|
catch (e) {
|
|
2080
2570
|
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.`);
|
|
2081
2571
|
}
|
|
2082
|
-
// Build an execution transaction
|
|
2083
|
-
return await ProgramManager$1.buildTransferTransaction(executionPrivateKey, amount, recipient, transferType, amountRecord,
|
|
2572
|
+
// Build an execution transaction
|
|
2573
|
+
return await ProgramManager$1.buildTransferTransaction(executionPrivateKey, amount, recipient, transferType, amountRecord, priorityFee, feeRecord, this.host, transferProvingKey, transferVerifyingKey, feeProvingKey, feeVerifyingKey, offlineQuery);
|
|
2084
2574
|
}
|
|
2085
2575
|
/**
|
|
2086
2576
|
* Build a transfer_public transaction to transfer credits to another account for later submission to the Aleo network
|
|
2087
2577
|
*
|
|
2088
2578
|
* @param {number} amount The amount of credits to transfer
|
|
2089
2579
|
* @param {string} recipient The recipient of the transfer
|
|
2090
|
-
* @param {number}
|
|
2580
|
+
* @param {number} priorityFee The optional priority fee to be paid for the transfer
|
|
2091
2581
|
* @param {PrivateKey | undefined} privateKey Optional private key to use for the transfer transaction
|
|
2092
2582
|
* @param {OfflineQuery | undefined} offlineQuery Optional offline query if creating transactions in an offline environment
|
|
2093
2583
|
* @returns {Promise<Transaction>} The transaction object
|
|
@@ -2112,20 +2602,15 @@ class ProgramManager {
|
|
|
2112
2602
|
* assert(transaction.id() === tx.id());
|
|
2113
2603
|
* }, 10000);
|
|
2114
2604
|
*/
|
|
2115
|
-
async buildTransferPublicTransaction(amount, recipient,
|
|
2116
|
-
return this.buildTransferTransaction(amount, recipient, "public",
|
|
2605
|
+
async buildTransferPublicTransaction(amount, recipient, priorityFee, privateKey, offlineQuery) {
|
|
2606
|
+
return this.buildTransferTransaction(amount, recipient, "public", priorityFee, false, undefined, undefined, undefined, privateKey, offlineQuery);
|
|
2117
2607
|
}
|
|
2118
2608
|
/**
|
|
2119
2609
|
* Build a transfer_public_as_signer transaction to transfer credits to another account for later submission to the Aleo network
|
|
2120
2610
|
*
|
|
2121
2611
|
* @param {number} amount The amount of credits to transfer
|
|
2122
2612
|
* @param {string} recipient The recipient of the transfer
|
|
2123
|
-
* @param {
|
|
2124
|
-
* @param {number} fee The fee to pay for the transfer
|
|
2125
|
-
* @param {boolean} privateFee Use a private record to pay the fee. If false this will use the account's public credit balance
|
|
2126
|
-
* @param {RecordSearchParams | undefined} recordSearchParams Optional parameters for finding the amount and fee records for the transfer transaction
|
|
2127
|
-
* @param {RecordPlaintext | string} amountRecord Optional amount record to use for the transfer
|
|
2128
|
-
* @param {RecordPlaintext | string} feeRecord Optional fee record to use for the transfer
|
|
2613
|
+
* @param {number} priorityFee The optional priority fee to be paid for the transfer
|
|
2129
2614
|
* @param {PrivateKey | undefined} privateKey Optional private key to use for the transfer transaction
|
|
2130
2615
|
* @param {OfflineQuery | undefined} offlineQuery Optional offline query if creating transactions in an offline environment
|
|
2131
2616
|
* @returns {Promise<Transaction>} The transaction object
|
|
@@ -2150,8 +2635,8 @@ class ProgramManager {
|
|
|
2150
2635
|
* assert(transaction.id() === tx.id());
|
|
2151
2636
|
* }, 10000);
|
|
2152
2637
|
*/
|
|
2153
|
-
async buildTransferPublicAsSignerTransaction(amount, recipient,
|
|
2154
|
-
return this.buildTransferTransaction(amount, recipient, "public",
|
|
2638
|
+
async buildTransferPublicAsSignerTransaction(amount, recipient, priorityFee, privateKey, offlineQuery) {
|
|
2639
|
+
return this.buildTransferTransaction(amount, recipient, "public", priorityFee, false, undefined, undefined, undefined, privateKey, offlineQuery);
|
|
2155
2640
|
}
|
|
2156
2641
|
/**
|
|
2157
2642
|
* Transfer credits to another account
|
|
@@ -2159,7 +2644,7 @@ class ProgramManager {
|
|
|
2159
2644
|
* @param {number} amount The amount of credits to transfer
|
|
2160
2645
|
* @param {string} recipient The recipient of the transfer
|
|
2161
2646
|
* @param {string} transferType The type of transfer to perform - options: 'private', 'privateToPublic', 'public', 'publicToPrivate'
|
|
2162
|
-
* @param {number}
|
|
2647
|
+
* @param {number} priorityFee The optional priority fee to be paid for the transfer
|
|
2163
2648
|
* @param {boolean} privateFee Use a private record to pay the fee. If false this will use the account's public credit balance
|
|
2164
2649
|
* @param {RecordSearchParams | undefined} recordSearchParams Optional parameters for finding the amount and fee records for the transfer transaction
|
|
2165
2650
|
* @param {RecordPlaintext | string} amountRecord Optional amount record to use for the transfer
|
|
@@ -2187,8 +2672,20 @@ class ProgramManager {
|
|
|
2187
2672
|
* assert(transaction.id() === tx_id);
|
|
2188
2673
|
* }, 10000);
|
|
2189
2674
|
*/
|
|
2190
|
-
async transfer(amount, recipient, transferType,
|
|
2191
|
-
const tx = await this.buildTransferTransaction(amount, recipient, transferType,
|
|
2675
|
+
async transfer(amount, recipient, transferType, priorityFee, privateFee, recordSearchParams, amountRecord, feeRecord, privateKey, offlineQuery) {
|
|
2676
|
+
const tx = (await this.buildTransferTransaction(amount, recipient, transferType, priorityFee, privateFee, recordSearchParams, amountRecord, feeRecord, privateKey, offlineQuery));
|
|
2677
|
+
let feeAddress;
|
|
2678
|
+
if (typeof privateKey !== "undefined") {
|
|
2679
|
+
feeAddress = Address.from_private_key(privateKey);
|
|
2680
|
+
}
|
|
2681
|
+
else if (this.account !== undefined) {
|
|
2682
|
+
feeAddress = this.account?.address();
|
|
2683
|
+
}
|
|
2684
|
+
else {
|
|
2685
|
+
throw Error("No private key provided and no private key set in the ProgramManager. Please set an account or provide a private key.");
|
|
2686
|
+
}
|
|
2687
|
+
// Check if the account has sufficient credits to pay for the transaction
|
|
2688
|
+
await this.checkFee(feeAddress.to_string(), tx.feeAmount());
|
|
2192
2689
|
return await this.networkClient.submitTransaction(tx);
|
|
2193
2690
|
}
|
|
2194
2691
|
/**
|
|
@@ -2226,19 +2723,23 @@ class ProgramManager {
|
|
|
2226
2723
|
*/
|
|
2227
2724
|
async buildBondPublicTransaction(validator_address, withdrawal_address, amount, options = {}) {
|
|
2228
2725
|
const scaledAmount = Math.trunc(amount * 1000000);
|
|
2229
|
-
const { programName = "credits.aleo", functionName = "bond_public",
|
|
2726
|
+
const { programName = "credits.aleo", functionName = "bond_public", priorityFee = options.priorityFee || 0, privateFee = false, inputs = [
|
|
2727
|
+
validator_address,
|
|
2728
|
+
withdrawal_address,
|
|
2729
|
+
`${scaledAmount.toString()}u64`,
|
|
2730
|
+
], keySearchParams = new AleoKeyProviderParams({
|
|
2230
2731
|
proverUri: CREDITS_PROGRAM_KEYS.bond_public.prover,
|
|
2231
2732
|
verifierUri: CREDITS_PROGRAM_KEYS.bond_public.verifier,
|
|
2232
|
-
cacheKey: "credits.aleo/bond_public"
|
|
2733
|
+
cacheKey: "credits.aleo/bond_public",
|
|
2233
2734
|
}), program = this.creditsProgram(), ...additionalOptions } = options;
|
|
2234
2735
|
const executeOptions = {
|
|
2235
2736
|
programName,
|
|
2236
2737
|
functionName,
|
|
2237
|
-
|
|
2738
|
+
priorityFee,
|
|
2238
2739
|
privateFee,
|
|
2239
2740
|
inputs,
|
|
2240
2741
|
keySearchParams,
|
|
2241
|
-
...additionalOptions
|
|
2742
|
+
...additionalOptions,
|
|
2242
2743
|
};
|
|
2243
2744
|
return await this.buildExecutionTransaction(executeOptions);
|
|
2244
2745
|
}
|
|
@@ -2272,7 +2773,19 @@ class ProgramManager {
|
|
|
2272
2773
|
* }, 10000);
|
|
2273
2774
|
*/
|
|
2274
2775
|
async bondPublic(validator_address, withdrawal_address, amount, options = {}) {
|
|
2275
|
-
const tx = await this.buildBondPublicTransaction(validator_address, withdrawal_address, amount, options);
|
|
2776
|
+
const tx = (await this.buildBondPublicTransaction(validator_address, withdrawal_address, amount, options));
|
|
2777
|
+
let feeAddress;
|
|
2778
|
+
if (typeof options.privateKey !== "undefined") {
|
|
2779
|
+
feeAddress = Address.from_private_key(options.privateKey);
|
|
2780
|
+
}
|
|
2781
|
+
else if (this.account !== undefined) {
|
|
2782
|
+
feeAddress = this.account?.address();
|
|
2783
|
+
}
|
|
2784
|
+
else {
|
|
2785
|
+
throw Error("No private key provided and no private key set in the ProgramManager. Please set an account or provide a private key.");
|
|
2786
|
+
}
|
|
2787
|
+
// Check if the account has sufficient credits to pay for the transaction
|
|
2788
|
+
await this.checkFee(feeAddress.to_string(), tx.feeAmount());
|
|
2276
2789
|
return await this.networkClient.submitTransaction(tx);
|
|
2277
2790
|
}
|
|
2278
2791
|
/**
|
|
@@ -2312,19 +2825,24 @@ class ProgramManager {
|
|
|
2312
2825
|
async buildBondValidatorTransaction(validator_address, withdrawal_address, amount, commission, options = {}) {
|
|
2313
2826
|
const scaledAmount = Math.trunc(amount * 1000000);
|
|
2314
2827
|
const adjustedCommission = Math.trunc(commission);
|
|
2315
|
-
const { programName = "credits.aleo", functionName = "bond_validator",
|
|
2828
|
+
const { programName = "credits.aleo", functionName = "bond_validator", priorityFee = options.priorityFee || 0, privateFee = false, inputs = [
|
|
2829
|
+
validator_address,
|
|
2830
|
+
withdrawal_address,
|
|
2831
|
+
`${scaledAmount.toString()}u64`,
|
|
2832
|
+
`${adjustedCommission.toString()}u8`,
|
|
2833
|
+
], keySearchParams = new AleoKeyProviderParams({
|
|
2316
2834
|
proverUri: CREDITS_PROGRAM_KEYS.bond_validator.prover,
|
|
2317
2835
|
verifierUri: CREDITS_PROGRAM_KEYS.bond_validator.verifier,
|
|
2318
|
-
cacheKey: "credits.aleo/bond_validator"
|
|
2836
|
+
cacheKey: "credits.aleo/bond_validator",
|
|
2319
2837
|
}), program = this.creditsProgram(), ...additionalOptions } = options;
|
|
2320
2838
|
const executeOptions = {
|
|
2321
2839
|
programName,
|
|
2322
2840
|
functionName,
|
|
2323
|
-
|
|
2841
|
+
priorityFee,
|
|
2324
2842
|
privateFee,
|
|
2325
2843
|
inputs,
|
|
2326
2844
|
keySearchParams,
|
|
2327
|
-
...additionalOptions
|
|
2845
|
+
...additionalOptions,
|
|
2328
2846
|
};
|
|
2329
2847
|
return await this.buildExecutionTransaction(executeOptions);
|
|
2330
2848
|
}
|
|
@@ -2360,7 +2878,19 @@ class ProgramManager {
|
|
|
2360
2878
|
* }, 10000);
|
|
2361
2879
|
*/
|
|
2362
2880
|
async bondValidator(validator_address, withdrawal_address, amount, commission, options = {}) {
|
|
2363
|
-
const tx = await this.buildBondValidatorTransaction(validator_address, withdrawal_address, amount, commission, options);
|
|
2881
|
+
const tx = (await this.buildBondValidatorTransaction(validator_address, withdrawal_address, amount, commission, options));
|
|
2882
|
+
let feeAddress;
|
|
2883
|
+
if (typeof options.privateKey !== "undefined") {
|
|
2884
|
+
feeAddress = Address.from_private_key(options.privateKey);
|
|
2885
|
+
}
|
|
2886
|
+
else if (this.account !== undefined) {
|
|
2887
|
+
feeAddress = this.account?.address();
|
|
2888
|
+
}
|
|
2889
|
+
else {
|
|
2890
|
+
throw Error("No private key provided and no private key set in the ProgramManager. Please set an account or provide a private key.");
|
|
2891
|
+
}
|
|
2892
|
+
// Check if the account has sufficient credits to pay for the transaction
|
|
2893
|
+
await this.checkFee(feeAddress.to_string(), tx.feeAmount());
|
|
2364
2894
|
return await this.networkClient.submitTransaction(tx);
|
|
2365
2895
|
}
|
|
2366
2896
|
/**
|
|
@@ -2394,19 +2924,19 @@ class ProgramManager {
|
|
|
2394
2924
|
*/
|
|
2395
2925
|
async buildUnbondPublicTransaction(staker_address, amount, options = {}) {
|
|
2396
2926
|
const scaledAmount = Math.trunc(amount * 1000000);
|
|
2397
|
-
const { programName = "credits.aleo", functionName = "unbond_public",
|
|
2927
|
+
const { programName = "credits.aleo", functionName = "unbond_public", priorityFee = options.priorityFee || 0, privateFee = false, inputs = [staker_address, `${scaledAmount.toString()}u64`], keySearchParams = new AleoKeyProviderParams({
|
|
2398
2928
|
proverUri: CREDITS_PROGRAM_KEYS.unbond_public.prover,
|
|
2399
2929
|
verifierUri: CREDITS_PROGRAM_KEYS.unbond_public.verifier,
|
|
2400
|
-
cacheKey: "credits.aleo/unbond_public"
|
|
2930
|
+
cacheKey: "credits.aleo/unbond_public",
|
|
2401
2931
|
}), program = this.creditsProgram(), ...additionalOptions } = options;
|
|
2402
2932
|
const executeOptions = {
|
|
2403
2933
|
programName,
|
|
2404
2934
|
functionName,
|
|
2405
|
-
|
|
2935
|
+
priorityFee,
|
|
2406
2936
|
privateFee,
|
|
2407
2937
|
inputs,
|
|
2408
2938
|
keySearchParams,
|
|
2409
|
-
...additionalOptions
|
|
2939
|
+
...additionalOptions,
|
|
2410
2940
|
};
|
|
2411
2941
|
return this.buildExecutionTransaction(executeOptions);
|
|
2412
2942
|
}
|
|
@@ -2445,7 +2975,19 @@ class ProgramManager {
|
|
|
2445
2975
|
* }, 10000);
|
|
2446
2976
|
*/
|
|
2447
2977
|
async unbondPublic(staker_address, amount, options = {}) {
|
|
2448
|
-
const tx = await this.buildUnbondPublicTransaction(staker_address, amount, options);
|
|
2978
|
+
const tx = (await this.buildUnbondPublicTransaction(staker_address, amount, options));
|
|
2979
|
+
let feeAddress;
|
|
2980
|
+
if (typeof options.privateKey !== "undefined") {
|
|
2981
|
+
feeAddress = Address.from_private_key(options.privateKey);
|
|
2982
|
+
}
|
|
2983
|
+
else if (this.account !== undefined) {
|
|
2984
|
+
feeAddress = this.account?.address();
|
|
2985
|
+
}
|
|
2986
|
+
else {
|
|
2987
|
+
throw Error("No private key provided and no private key set in the ProgramManager. Please set an account or provide a private key.");
|
|
2988
|
+
}
|
|
2989
|
+
// Check if the account has sufficient credits to pay for the transaction
|
|
2990
|
+
await this.checkFee(feeAddress.to_string(), tx.feeAmount());
|
|
2449
2991
|
return await this.networkClient.submitTransaction(tx);
|
|
2450
2992
|
}
|
|
2451
2993
|
/**
|
|
@@ -2479,20 +3021,21 @@ class ProgramManager {
|
|
|
2479
3021
|
* }, 10000);
|
|
2480
3022
|
*/
|
|
2481
3023
|
async buildClaimUnbondPublicTransaction(staker_address, options = {}) {
|
|
2482
|
-
const { programName = "credits.aleo", functionName = "claim_unbond_public",
|
|
3024
|
+
const { programName = "credits.aleo", functionName = "claim_unbond_public", priorityFee = options.priorityFee || 0, privateFee = false, inputs = [staker_address], keySearchParams = new AleoKeyProviderParams({
|
|
2483
3025
|
proverUri: CREDITS_PROGRAM_KEYS.claim_unbond_public.prover,
|
|
2484
3026
|
verifierUri: CREDITS_PROGRAM_KEYS.claim_unbond_public.verifier,
|
|
2485
|
-
cacheKey: "credits.aleo/claim_unbond_public"
|
|
3027
|
+
cacheKey: "credits.aleo/claim_unbond_public",
|
|
2486
3028
|
}), program = this.creditsProgram(), ...additionalOptions } = options;
|
|
2487
3029
|
const executeOptions = {
|
|
2488
3030
|
programName,
|
|
2489
3031
|
functionName,
|
|
2490
|
-
|
|
3032
|
+
priorityFee,
|
|
2491
3033
|
privateFee,
|
|
2492
3034
|
inputs,
|
|
2493
3035
|
keySearchParams,
|
|
2494
|
-
...additionalOptions
|
|
3036
|
+
...additionalOptions,
|
|
2495
3037
|
};
|
|
3038
|
+
// Check if the account has sufficient credits to pay for the transaction
|
|
2496
3039
|
return await this.buildExecutionTransaction(executeOptions);
|
|
2497
3040
|
}
|
|
2498
3041
|
/**
|
|
@@ -2525,7 +3068,19 @@ class ProgramManager {
|
|
|
2525
3068
|
* }, 10000);
|
|
2526
3069
|
*/
|
|
2527
3070
|
async claimUnbondPublic(staker_address, options = {}) {
|
|
2528
|
-
const tx = await this.buildClaimUnbondPublicTransaction(staker_address, options);
|
|
3071
|
+
const tx = (await this.buildClaimUnbondPublicTransaction(staker_address, options));
|
|
3072
|
+
let feeAddress;
|
|
3073
|
+
if (typeof options.privateKey !== "undefined") {
|
|
3074
|
+
feeAddress = Address.from_private_key(options.privateKey);
|
|
3075
|
+
}
|
|
3076
|
+
else if (this.account !== undefined) {
|
|
3077
|
+
feeAddress = this.account?.address();
|
|
3078
|
+
}
|
|
3079
|
+
else {
|
|
3080
|
+
throw Error("No private key provided and no private key set in the ProgramManager. Please set an account or provide a private key.");
|
|
3081
|
+
}
|
|
3082
|
+
// Check if the account has sufficient credits to pay for the transaction
|
|
3083
|
+
await this.checkFee(feeAddress.to_string(), tx.feeAmount());
|
|
2529
3084
|
return await this.networkClient.submitTransaction(tx);
|
|
2530
3085
|
}
|
|
2531
3086
|
/**
|
|
@@ -2567,21 +3122,21 @@ class ProgramManager {
|
|
|
2567
3122
|
* }, 10000);
|
|
2568
3123
|
*/
|
|
2569
3124
|
async buildSetValidatorStateTransaction(validator_state, options = {}) {
|
|
2570
|
-
const { programName = "credits.aleo", functionName = "set_validator_state",
|
|
3125
|
+
const { programName = "credits.aleo", functionName = "set_validator_state", priorityFee = 0, privateFee = false, inputs = [validator_state.toString()], keySearchParams = new AleoKeyProviderParams({
|
|
2571
3126
|
proverUri: CREDITS_PROGRAM_KEYS.set_validator_state.prover,
|
|
2572
3127
|
verifierUri: CREDITS_PROGRAM_KEYS.set_validator_state.verifier,
|
|
2573
|
-
cacheKey: "credits.aleo/set_validator_state"
|
|
3128
|
+
cacheKey: "credits.aleo/set_validator_state",
|
|
2574
3129
|
}), ...additionalOptions } = options;
|
|
2575
3130
|
const executeOptions = {
|
|
2576
3131
|
programName,
|
|
2577
3132
|
functionName,
|
|
2578
|
-
|
|
3133
|
+
priorityFee,
|
|
2579
3134
|
privateFee,
|
|
2580
3135
|
inputs,
|
|
2581
3136
|
keySearchParams,
|
|
2582
|
-
...additionalOptions
|
|
3137
|
+
...additionalOptions,
|
|
2583
3138
|
};
|
|
2584
|
-
return await this.
|
|
3139
|
+
return await this.buildExecutionTransaction(executeOptions);
|
|
2585
3140
|
}
|
|
2586
3141
|
/**
|
|
2587
3142
|
* Submit a set_validator_state transaction to the Aleo Network.
|
|
@@ -2619,22 +3174,60 @@ class ProgramManager {
|
|
|
2619
3174
|
* }, 10000);
|
|
2620
3175
|
*/
|
|
2621
3176
|
async setValidatorState(validator_state, options = {}) {
|
|
2622
|
-
const tx = await this.buildSetValidatorStateTransaction(validator_state, options);
|
|
3177
|
+
const tx = (await this.buildSetValidatorStateTransaction(validator_state, options));
|
|
3178
|
+
let feeAddress;
|
|
3179
|
+
if (typeof options.privateKey !== "undefined") {
|
|
3180
|
+
feeAddress = Address.from_private_key(options.privateKey);
|
|
3181
|
+
}
|
|
3182
|
+
else if (this.account !== undefined) {
|
|
3183
|
+
feeAddress = this.account?.address();
|
|
3184
|
+
}
|
|
3185
|
+
else {
|
|
3186
|
+
throw Error("No private key provided and no private key set in the ProgramManager. Please set an account or provide a private key.");
|
|
3187
|
+
}
|
|
3188
|
+
// Check if the account has sufficient credits to pay for the transaction
|
|
3189
|
+
await this.checkFee(feeAddress.to_string(), tx.feeAmount());
|
|
2623
3190
|
return this.networkClient.submitTransaction(tx);
|
|
2624
3191
|
}
|
|
2625
3192
|
/**
|
|
2626
|
-
* Verify a proof
|
|
3193
|
+
* Verify a proof from an offline execution. This is useful when it is desired to do offchain proving and verification.
|
|
2627
3194
|
*
|
|
2628
|
-
* @param {executionResponse} executionResponse
|
|
3195
|
+
* @param {executionResponse} executionResponse The response from an offline function execution (via the `programManager.run` method)
|
|
3196
|
+
* @param {ImportedPrograms} imports The imported programs used in the execution. Specified as { "programName": "programSourceCode", ... }
|
|
3197
|
+
* @param {ImportedVerifyingKeys} importedVerifyingKeys The verifying keys in the execution. Specified as { "programName": [["functionName", "verifyingKey"], ...], ... }
|
|
2629
3198
|
* @returns {boolean} True if the proof is valid, false otherwise
|
|
3199
|
+
*
|
|
3200
|
+
* @example
|
|
3201
|
+
* /// Import the mainnet version of the sdk used to build executions.
|
|
3202
|
+
* import { Account, ProgramManager } from "@provablehq/sdk/mainnet.js";
|
|
3203
|
+
*
|
|
3204
|
+
* /// Create the source for two programs.
|
|
3205
|
+
* const program = "import add_it_up.aleo; \n\n program mul_add.aleo;\n\nfunction mul_and_add:\n input r0 as u32.public;\n input r1 as u32.private;\n mul r0 r1 into r2;\n call add_it_up.aleo/add_it r1 r2 into r3; output r3 as u32.private;\n";
|
|
3206
|
+
* const program_import = "program add_it_up.aleo;\n\nfunction add_it:\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";
|
|
3207
|
+
* const programManager = new ProgramManager(undefined, undefined, undefined);
|
|
3208
|
+
*
|
|
3209
|
+
* /// Create a temporary account for the execution of the program
|
|
3210
|
+
* const account = Account.fromCipherText(process.env.ciphertext, process.env.password);
|
|
3211
|
+
* programManager.setAccount(account);
|
|
3212
|
+
*
|
|
3213
|
+
* /// Get the response and ensure that the program executed correctly
|
|
3214
|
+
* const executionResponse = await programManager.run(program, "mul_and_add", ["5u32", "5u32"], true);
|
|
3215
|
+
*
|
|
3216
|
+
* /// Construct the imports and verifying keys
|
|
3217
|
+
* const imports = { "add_it_up.aleo": program_import };
|
|
3218
|
+
* const importedVerifyingKeys = { "add_it_up.aleo": [["add_it", "verifyingKey1..."]] };
|
|
3219
|
+
*
|
|
3220
|
+
* /// Verify the execution.
|
|
3221
|
+
* const isValid = programManager.verifyExecution(executionResponse, imports, importedVerifyingKeys);
|
|
3222
|
+
* assert(isValid);
|
|
2630
3223
|
*/
|
|
2631
|
-
verifyExecution(executionResponse) {
|
|
3224
|
+
verifyExecution(executionResponse, imports, importedVerifyingKeys) {
|
|
2632
3225
|
try {
|
|
2633
|
-
const execution = executionResponse.getExecution();
|
|
3226
|
+
const execution = (executionResponse.getExecution());
|
|
2634
3227
|
const function_id = executionResponse.getFunctionId();
|
|
2635
3228
|
const program = executionResponse.getProgram();
|
|
2636
3229
|
const verifyingKey = executionResponse.getVerifyingKey();
|
|
2637
|
-
return verifyFunctionExecution(execution, verifyingKey, program, function_id);
|
|
3230
|
+
return verifyFunctionExecution(execution, verifyingKey, program, function_id, imports, importedVerifyingKeys);
|
|
2638
3231
|
}
|
|
2639
3232
|
catch (e) {
|
|
2640
3233
|
console.warn("The execution was not found in the response, cannot verify the execution");
|
|
@@ -2675,7 +3268,9 @@ class ProgramManager {
|
|
|
2675
3268
|
// Internal utility function for getting a credits.aleo record
|
|
2676
3269
|
async getCreditsRecord(amount, nonces, record, params) {
|
|
2677
3270
|
try {
|
|
2678
|
-
return record instanceof RecordPlaintext
|
|
3271
|
+
return record instanceof RecordPlaintext
|
|
3272
|
+
? record
|
|
3273
|
+
: RecordPlaintext.fromString(record);
|
|
2679
3274
|
}
|
|
2680
3275
|
catch (e) {
|
|
2681
3276
|
try {
|
|
@@ -2694,9 +3289,10 @@ function requiresAmountRecord(transferType) {
|
|
|
2694
3289
|
}
|
|
2695
3290
|
// Validate the transfer type
|
|
2696
3291
|
function validateTransferType(transferType) {
|
|
2697
|
-
return VALID_TRANSFER_TYPES.has(transferType)
|
|
2698
|
-
|
|
3292
|
+
return VALID_TRANSFER_TYPES.has(transferType)
|
|
3293
|
+
? transferType
|
|
3294
|
+
: logAndThrow(`Invalid transfer type '${transferType}'. Valid transfer types are 'private', 'privateToPublic', 'public', and 'publicToPrivate'.`);
|
|
2699
3295
|
}
|
|
2700
3296
|
|
|
2701
3297
|
export { AleoKeyProvider as A, CREDITS_PROGRAM_KEYS as C, KEY_STORE as K, ProgramManager as P, VALID_TRANSFER_TYPES as V, AleoKeyProviderParams as a, AleoNetworkClient as b, PRIVATE_TRANSFER as c, PRIVATE_TO_PUBLIC_TRANSFER as d, PRIVATE_TRANSFER_TYPES as e, PUBLIC_TRANSFER as f, PUBLIC_TRANSFER_AS_SIGNER as g, PUBLIC_TO_PRIVATE_TRANSFER as h, logAndThrow as l };
|
|
2702
|
-
//# sourceMappingURL=program-manager-
|
|
3298
|
+
//# sourceMappingURL=program-manager-C1m8QHkg.js.map
|