@provablehq/sdk 0.7.3 → 0.7.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/mainnet/account.d.ts +137 -0
- package/dist/mainnet/browser.d.ts +20 -0
- package/dist/mainnet/browser.js +934 -0
- package/dist/mainnet/browser.js.map +1 -0
- package/dist/mainnet/constants.d.ts +34 -0
- package/dist/mainnet/function-key-provider.d.ts +349 -0
- package/dist/mainnet/managed-worker.d.ts +3 -0
- package/dist/mainnet/models/block.d.ts +21 -0
- package/dist/mainnet/models/confirmed_transaction.d.ts +6 -0
- package/dist/mainnet/models/execution.d.ts +5 -0
- package/dist/mainnet/models/input.d.ts +10 -0
- package/dist/mainnet/models/output.d.ts +6 -0
- package/dist/mainnet/models/transactionModel.d.ts +6 -0
- package/dist/mainnet/models/transition.d.ts +13 -0
- package/dist/mainnet/network-client.d.ts +270 -0
- package/dist/mainnet/node-polyfill.d.ts +5 -0
- package/dist/mainnet/node-polyfill.js +304 -0
- package/dist/mainnet/node-polyfill.js.map +1 -0
- package/dist/mainnet/node.d.ts +2 -0
- package/dist/mainnet/node.js +13 -0
- package/dist/mainnet/node.js.map +1 -0
- package/dist/mainnet/offline-key-provider.d.ts +348 -0
- package/dist/mainnet/polyfill/crypto.d.ts +1 -0
- package/dist/mainnet/polyfill/fetch.d.ts +1 -0
- package/dist/mainnet/polyfill/shared.d.ts +1 -0
- package/dist/mainnet/polyfill/worker.d.ts +1 -0
- package/dist/mainnet/polyfill/xmlhttprequest.d.ts +1 -0
- package/dist/mainnet/program-manager-a25c2f23.js +2200 -0
- package/dist/mainnet/program-manager-a25c2f23.js.map +1 -0
- package/dist/mainnet/program-manager.d.ts +640 -0
- package/dist/mainnet/record-provider.d.ts +236 -0
- package/dist/mainnet/utils.d.ts +4 -0
- package/dist/mainnet/wasm.d.ts +1 -0
- package/dist/mainnet/worker.d.ts +9 -0
- package/dist/mainnet/worker.js +78 -0
- package/dist/mainnet/worker.js.map +1 -0
- package/dist/testnet/account.d.ts +137 -0
- package/dist/testnet/browser.d.ts +20 -0
- package/dist/testnet/browser.js +934 -0
- package/dist/testnet/browser.js.map +1 -0
- package/dist/testnet/constants.d.ts +34 -0
- package/dist/testnet/function-key-provider.d.ts +349 -0
- package/dist/testnet/managed-worker.d.ts +3 -0
- package/dist/testnet/models/block.d.ts +21 -0
- package/dist/testnet/models/confirmed_transaction.d.ts +6 -0
- package/dist/testnet/models/execution.d.ts +5 -0
- package/dist/testnet/models/input.d.ts +10 -0
- package/dist/testnet/models/output.d.ts +6 -0
- package/dist/testnet/models/transactionModel.d.ts +6 -0
- package/dist/testnet/models/transition.d.ts +13 -0
- package/dist/testnet/network-client.d.ts +270 -0
- package/dist/testnet/node-polyfill.d.ts +5 -0
- package/dist/testnet/node-polyfill.js +304 -0
- package/dist/testnet/node-polyfill.js.map +1 -0
- package/dist/testnet/node.d.ts +2 -0
- package/dist/testnet/node.js +13 -0
- package/dist/testnet/node.js.map +1 -0
- package/dist/testnet/offline-key-provider.d.ts +348 -0
- package/dist/testnet/polyfill/crypto.d.ts +1 -0
- package/dist/testnet/polyfill/fetch.d.ts +1 -0
- package/dist/testnet/polyfill/shared.d.ts +1 -0
- package/dist/testnet/polyfill/worker.d.ts +1 -0
- package/dist/testnet/polyfill/xmlhttprequest.d.ts +1 -0
- package/dist/testnet/program-manager-4e9a7ef4.js +2200 -0
- package/dist/testnet/program-manager-4e9a7ef4.js.map +1 -0
- package/dist/testnet/program-manager.d.ts +640 -0
- package/dist/testnet/record-provider.d.ts +236 -0
- package/dist/testnet/utils.d.ts +4 -0
- package/dist/testnet/wasm.d.ts +1 -0
- package/dist/testnet/worker.d.ts +9 -0
- package/dist/testnet/worker.js +78 -0
- package/dist/testnet/worker.js.map +1 -0
- package/package.json +2 -2
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
import { RecordPlaintext } from "./wasm";
|
|
2
|
+
import { Account } from "./account";
|
|
3
|
+
import { AleoNetworkClient } from "./network-client";
|
|
4
|
+
/**
|
|
5
|
+
* Interface for record search parameters. This allows for arbitrary search parameters to be passed to record provider
|
|
6
|
+
* implementations.
|
|
7
|
+
*/
|
|
8
|
+
interface RecordSearchParams {
|
|
9
|
+
[key: string]: any;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Interface for a record provider. A record provider is used to find records for use in deployment and execution
|
|
13
|
+
* transactions on the Aleo Network. A default implementation is provided by the NetworkRecordProvider class. However,
|
|
14
|
+
* a custom implementation can be provided (say if records are synced locally to a database from the network) by
|
|
15
|
+
* implementing this interface.
|
|
16
|
+
*/
|
|
17
|
+
interface RecordProvider {
|
|
18
|
+
account: Account;
|
|
19
|
+
/**
|
|
20
|
+
* Find a credits.aleo record with a given number of microcredits from the chosen provider
|
|
21
|
+
*
|
|
22
|
+
* @param {number} microcredits The number of microcredits to search for
|
|
23
|
+
* @param {boolean} unspent Whether or not the record is unspent
|
|
24
|
+
* @param {string[]} nonces Nonces of records already found so they are not found again
|
|
25
|
+
* @param {RecordSearchParams} searchParameters Additional parameters to search for
|
|
26
|
+
* @returns {Promise<RecordPlaintext>} The record if found, otherwise an error
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* // A class implementing record provider can be used to find a record with a given number of microcredits
|
|
30
|
+
* const record = await recordProvider.findCreditsRecord(5000, true, []);
|
|
31
|
+
*
|
|
32
|
+
* // When a record is found but not yet used, its nonce should be added to the nonces array so that it is not
|
|
33
|
+
* // found again if a subsequent search is performed
|
|
34
|
+
* const record2 = await recordProvider.findCreditsRecord(5000, true, [record.nonce()]);
|
|
35
|
+
*
|
|
36
|
+
* // When the program manager is initialized with the record provider it will be used to find automatically find
|
|
37
|
+
* // fee records and amount records for value transfers so that they do not need to be specified manually
|
|
38
|
+
* const programManager = new ProgramManager("https://api.explorer.provable.com/v1", keyProvider, recordProvider);
|
|
39
|
+
* programManager.transfer(1, "aleo166q6ww6688cug7qxwe7nhctjpymydwzy2h7rscfmatqmfwnjvggqcad0at", "public", 0.5);
|
|
40
|
+
*/
|
|
41
|
+
findCreditsRecord(microcredits: number, unspent: boolean, nonces?: string[], searchParameters?: RecordSearchParams): Promise<RecordPlaintext>;
|
|
42
|
+
/**
|
|
43
|
+
* Find a list of credit.aleo records with a given number of microcredits from the chosen provider
|
|
44
|
+
*
|
|
45
|
+
* @param {number} microcreditAmounts A list of separate microcredit amounts to search for (e.g. [5000, 100000])
|
|
46
|
+
* @param {boolean} unspent Whether or not the record is unspent
|
|
47
|
+
* @param {string[]} nonces Nonces of records already found so that they are not found again
|
|
48
|
+
* @param {RecordSearchParams} searchParameters Additional parameters to search for
|
|
49
|
+
* @returns {Promise<RecordPlaintext[]>} A list of records with a value greater or equal to the amounts specified if such records exist, otherwise an error
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* // A class implementing record provider can be used to find a record with a given number of microcredits
|
|
53
|
+
* const records = await recordProvider.findCreditsRecords([5000, 5000], true, []);
|
|
54
|
+
*
|
|
55
|
+
* // When a record is found but not yet used, it's nonce should be added to the nonces array so that it is not
|
|
56
|
+
* // found again if a subsequent search is performed
|
|
57
|
+
* const nonces = [];
|
|
58
|
+
* records.forEach(record => { nonces.push(record.nonce()) });
|
|
59
|
+
* const records2 = await recordProvider.findCreditsRecord(5000, true, nonces);
|
|
60
|
+
*
|
|
61
|
+
* // When the program manager is initialized with the record provider it will be used to find automatically find
|
|
62
|
+
* // fee records and amount records for value transfers so that they do not need to be specified manually
|
|
63
|
+
* const programManager = new ProgramManager("https://api.explorer.provable.com/v1", keyProvider, recordProvider);
|
|
64
|
+
* programManager.transfer(1, "aleo166q6ww6688cug7qxwe7nhctjpymydwzy2h7rscfmatqmfwnjvggqcad0at", "public", 0.5);
|
|
65
|
+
*/
|
|
66
|
+
findCreditsRecords(microcreditAmounts: number[], unspent: boolean, nonces?: string[], searchParameters?: RecordSearchParams): Promise<RecordPlaintext[]>;
|
|
67
|
+
/**
|
|
68
|
+
* Find an arbitrary record
|
|
69
|
+
* @param {boolean} unspent Whether or not the record is unspent
|
|
70
|
+
* @param {string[]} nonces Nonces of records already found so that they are not found again
|
|
71
|
+
* @param {RecordSearchParams} searchParameters Additional parameters to search for
|
|
72
|
+
* @returns {Promise<RecordPlaintext>} The record if found, otherwise an error
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* // The RecordSearchParams interface can be used to create parameters for custom record searches which can then
|
|
76
|
+
* // be passed to the record provider. An example of how this would be done for the credits.aleo program is shown
|
|
77
|
+
* // below.
|
|
78
|
+
*
|
|
79
|
+
* class CustomRecordSearch implements RecordSearchParams {
|
|
80
|
+
* startHeight: number;
|
|
81
|
+
* endHeight: number;
|
|
82
|
+
* amount: number;
|
|
83
|
+
* program: string;
|
|
84
|
+
* recordName: string;
|
|
85
|
+
* constructor(startHeight: number, endHeight: number, credits: number, maxRecords: number, programName: string, recordName: string) {
|
|
86
|
+
* this.startHeight = startHeight;
|
|
87
|
+
* this.endHeight = endHeight;
|
|
88
|
+
* this.amount = amount;
|
|
89
|
+
* this.program = programName;
|
|
90
|
+
* this.recordName = recordName;
|
|
91
|
+
* }
|
|
92
|
+
* }
|
|
93
|
+
*
|
|
94
|
+
* const params = new CustomRecordSearch(0, 100, 5000, "credits.aleo", "credits");
|
|
95
|
+
*
|
|
96
|
+
* const record = await recordProvider.findRecord(true, [], params);
|
|
97
|
+
*/
|
|
98
|
+
findRecord(unspent: boolean, nonces?: string[], searchParameters?: RecordSearchParams): Promise<RecordPlaintext>;
|
|
99
|
+
/**
|
|
100
|
+
* Find multiple records from arbitrary programs
|
|
101
|
+
*
|
|
102
|
+
* @param {boolean} unspent Whether or not the record is unspent
|
|
103
|
+
* @param {string[]} nonces Nonces of records already found so that they are not found again
|
|
104
|
+
* @param {RecordSearchParams} searchParameters Additional parameters to search for
|
|
105
|
+
* @returns {Promise<RecordPlaintext>} The record if found, otherwise an error
|
|
106
|
+
*
|
|
107
|
+
* // The RecordSearchParams interface can be used to create parameters for custom record searches which can then
|
|
108
|
+
* // be passed to the record provider. An example of how this would be done for the credits.aleo program is shown
|
|
109
|
+
* // below.
|
|
110
|
+
*
|
|
111
|
+
* class CustomRecordSearch implements RecordSearchParams {
|
|
112
|
+
* startHeight: number;
|
|
113
|
+
* endHeight: number;
|
|
114
|
+
* amount: number;
|
|
115
|
+
* maxRecords: number;
|
|
116
|
+
* programName: string;
|
|
117
|
+
* recordName: string;
|
|
118
|
+
* constructor(startHeight: number, endHeight: number, credits: number, maxRecords: number, programName: string, recordName: string) {
|
|
119
|
+
* this.startHeight = startHeight;
|
|
120
|
+
* this.endHeight = endHeight;
|
|
121
|
+
* this.amount = amount;
|
|
122
|
+
* this.maxRecords = maxRecords;
|
|
123
|
+
* this.programName = programName;
|
|
124
|
+
* this.recordName = recordName;
|
|
125
|
+
* }
|
|
126
|
+
* }
|
|
127
|
+
*
|
|
128
|
+
* const params = new CustomRecordSearch(0, 100, 5000, 2, "credits.aleo", "credits");
|
|
129
|
+
* const records = await recordProvider.findRecord(true, [], params);
|
|
130
|
+
*/
|
|
131
|
+
findRecords(unspent: boolean, nonces?: string[], searchParameters?: RecordSearchParams): Promise<RecordPlaintext[]>;
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* A record provider implementation that uses the official Aleo API to find records for usage in program execution and
|
|
135
|
+
* deployment, wallet functionality, and other use cases.
|
|
136
|
+
*/
|
|
137
|
+
declare class NetworkRecordProvider implements RecordProvider {
|
|
138
|
+
account: Account;
|
|
139
|
+
networkClient: AleoNetworkClient;
|
|
140
|
+
constructor(account: Account, networkClient: AleoNetworkClient);
|
|
141
|
+
/**
|
|
142
|
+
* Set the account used to search for records
|
|
143
|
+
*
|
|
144
|
+
* @param {Account} account The account to use for searching for records
|
|
145
|
+
*/
|
|
146
|
+
setAccount(account: Account): void;
|
|
147
|
+
/**
|
|
148
|
+
* Find a list of credit records with a given number of microcredits by via the official Aleo API
|
|
149
|
+
*
|
|
150
|
+
* @param {number[]} microcredits The number of microcredits to search for
|
|
151
|
+
* @param {boolean} unspent Whether or not the record is unspent
|
|
152
|
+
* @param {string[]} nonces Nonces of records already found so that they are not found again
|
|
153
|
+
* @param {RecordSearchParams} searchParameters Additional parameters to search for
|
|
154
|
+
* @returns {Promise<RecordPlaintext>} The record if found, otherwise an error
|
|
155
|
+
*
|
|
156
|
+
* @example
|
|
157
|
+
* // Create a new NetworkRecordProvider
|
|
158
|
+
* const networkClient = new AleoNetworkClient("https://api.explorer.provable.com/v1");
|
|
159
|
+
* const keyProvider = new AleoKeyProvider();
|
|
160
|
+
* const recordProvider = new NetworkRecordProvider(account, networkClient);
|
|
161
|
+
*
|
|
162
|
+
* // The record provider can be used to find records with a given number of microcredits
|
|
163
|
+
* const record = await recordProvider.findCreditsRecord(5000, true, []);
|
|
164
|
+
*
|
|
165
|
+
* // When a record is found but not yet used, it's nonce should be added to the nonces parameter so that it is not
|
|
166
|
+
* // found again if a subsequent search is performed
|
|
167
|
+
* const records = await recordProvider.findCreditsRecords(5000, true, [record.nonce()]);
|
|
168
|
+
*
|
|
169
|
+
* // When the program manager is initialized with the record provider it will be used to find automatically find
|
|
170
|
+
* // fee records and amount records for value transfers so that they do not need to be specified manually
|
|
171
|
+
* const programManager = new ProgramManager("https://api.explorer.provable.com/v1", keyProvider, recordProvider);
|
|
172
|
+
* programManager.transfer(1, "aleo166q6ww6688cug7qxwe7nhctjpymydwzy2h7rscfmatqmfwnjvggqcad0at", "public", 0.5);
|
|
173
|
+
*
|
|
174
|
+
* */
|
|
175
|
+
findCreditsRecords(microcredits: number[], unspent: boolean, nonces?: string[], searchParameters?: RecordSearchParams): Promise<RecordPlaintext[]>;
|
|
176
|
+
/**
|
|
177
|
+
* Find a credit record with a given number of microcredits by via the official Aleo API
|
|
178
|
+
*
|
|
179
|
+
* @param {number} microcredits The number of microcredits to search for
|
|
180
|
+
* @param {boolean} unspent Whether or not the record is unspent
|
|
181
|
+
* @param {string[]} nonces Nonces of records already found so that they are not found again
|
|
182
|
+
* @param {RecordSearchParams} searchParameters Additional parameters to search for
|
|
183
|
+
* @returns {Promise<RecordPlaintext>} The record if found, otherwise an error
|
|
184
|
+
*
|
|
185
|
+
* @example
|
|
186
|
+
* // Create a new NetworkRecordProvider
|
|
187
|
+
* const networkClient = new AleoNetworkClient("https://api.explorer.provable.com/v1");
|
|
188
|
+
* const keyProvider = new AleoKeyProvider();
|
|
189
|
+
* const recordProvider = new NetworkRecordProvider(account, networkClient);
|
|
190
|
+
*
|
|
191
|
+
* // The record provider can be used to find records with a given number of microcredits
|
|
192
|
+
* const record = await recordProvider.findCreditsRecord(5000, true, []);
|
|
193
|
+
*
|
|
194
|
+
* // When a record is found but not yet used, it's nonce should be added to the nonces parameter so that it is not
|
|
195
|
+
* // found again if a subsequent search is performed
|
|
196
|
+
* const records = await recordProvider.findCreditsRecords(5000, true, [record.nonce()]);
|
|
197
|
+
*
|
|
198
|
+
* // When the program manager is initialized with the record provider it will be used to find automatically find
|
|
199
|
+
* // fee records and amount records for value transfers so that they do not need to be specified manually
|
|
200
|
+
* const programManager = new ProgramManager("https://api.explorer.provable.com/v1", keyProvider, recordProvider);
|
|
201
|
+
* programManager.transfer(1, "aleo166q6ww6688cug7qxwe7nhctjpymydwzy2h7rscfmatqmfwnjvggqcad0at", "public", 0.5);
|
|
202
|
+
*/
|
|
203
|
+
findCreditsRecord(microcredits: number, unspent: boolean, nonces?: string[], searchParameters?: RecordSearchParams): Promise<RecordPlaintext>;
|
|
204
|
+
/**
|
|
205
|
+
* Find an arbitrary record. WARNING: This function is not implemented yet and will throw an error.
|
|
206
|
+
*/
|
|
207
|
+
findRecord(unspent: boolean, nonces?: string[], searchParameters?: RecordSearchParams): Promise<RecordPlaintext>;
|
|
208
|
+
/**
|
|
209
|
+
* Find multiple arbitrary records. WARNING: This function is not implemented yet and will throw an error.
|
|
210
|
+
*/
|
|
211
|
+
findRecords(unspent: boolean, nonces?: string[], searchParameters?: RecordSearchParams): Promise<RecordPlaintext[]>;
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* BlockHeightSearch is a RecordSearchParams implementation that allows for searching for records within a given
|
|
215
|
+
* block height range.
|
|
216
|
+
*
|
|
217
|
+
* @example
|
|
218
|
+
* // Create a new BlockHeightSearch
|
|
219
|
+
* const params = new BlockHeightSearch(89995, 99995);
|
|
220
|
+
*
|
|
221
|
+
* // Create a new NetworkRecordProvider
|
|
222
|
+
* const networkClient = new AleoNetworkClient("https://api.explorer.provable.com/v1");
|
|
223
|
+
* const keyProvider = new AleoKeyProvider();
|
|
224
|
+
* const recordProvider = new NetworkRecordProvider(account, networkClient);
|
|
225
|
+
*
|
|
226
|
+
* // The record provider can be used to find records with a given number of microcredits and the block height search
|
|
227
|
+
* // can be used to find records within a given block height range
|
|
228
|
+
* const record = await recordProvider.findCreditsRecord(5000, true, [], params);
|
|
229
|
+
*
|
|
230
|
+
*/
|
|
231
|
+
declare class BlockHeightSearch implements RecordSearchParams {
|
|
232
|
+
startHeight: number;
|
|
233
|
+
endHeight: number;
|
|
234
|
+
constructor(startHeight: number, endHeight: number);
|
|
235
|
+
}
|
|
236
|
+
export { BlockHeightSearch, NetworkRecordProvider, RecordProvider, RecordSearchParams };
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export declare function logAndThrow(message: string): never;
|
|
2
|
+
export declare function parseJSON(json: string): any;
|
|
3
|
+
export declare function get(url: URL | string, options?: RequestInit): Promise<Response>;
|
|
4
|
+
export declare function post(url: URL | string, options: RequestInit): Promise<Response>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { Address, Execution, ExecutionResponse, Field, OfflineQuery, Metadata, PrivateKey, PrivateKeyCiphertext, Program, ProgramManager, ProvingKey, RecordCiphertext, RecordPlaintext, Signature, Transaction, VerifyingKey, ViewKey, initThreadPool, verifyFunctionExecution, } from "@provablehq/wasm/mainnet.js";
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import "./polyfill/shared";
|
|
2
|
+
import { PrivateKey } from "./browser";
|
|
3
|
+
export interface WorkerAPI {
|
|
4
|
+
run: (localProgram: string, aleoFunction: string, inputs: string[], privateKey: string) => Promise<{
|
|
5
|
+
outputs: any;
|
|
6
|
+
execution: string;
|
|
7
|
+
} | string>;
|
|
8
|
+
getPrivateKey: () => Promise<PrivateKey>;
|
|
9
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import 'core-js/proposals/json-parse-with-source.js';
|
|
2
|
+
import { initThreadPool, PrivateKey, verifyFunctionExecution } from '@provablehq/wasm/mainnet.js';
|
|
3
|
+
import { P as ProgramManager, A as AleoKeyProvider, a as AleoKeyProviderParams } from './program-manager-a25c2f23.js';
|
|
4
|
+
import { expose } from 'comlink';
|
|
5
|
+
|
|
6
|
+
await initThreadPool();
|
|
7
|
+
const defaultHost = "https://api.explorer.provable.com/v1";
|
|
8
|
+
const keyProvider = new AleoKeyProvider();
|
|
9
|
+
const programManager = new ProgramManager(defaultHost, keyProvider, undefined);
|
|
10
|
+
keyProvider.useCache(true);
|
|
11
|
+
let lastLocalProgram = "";
|
|
12
|
+
async function run(localProgram, aleoFunction, inputs, privateKey, proveExecution = false) {
|
|
13
|
+
console.log("Web worker: Executing function locally...");
|
|
14
|
+
const startTime = performance.now();
|
|
15
|
+
// Ensure the program is valid and that it contains the function specified
|
|
16
|
+
let program;
|
|
17
|
+
try {
|
|
18
|
+
program = programManager.createProgramFromSource(localProgram);
|
|
19
|
+
}
|
|
20
|
+
catch (e) {
|
|
21
|
+
throw new Error("Error creating program from source");
|
|
22
|
+
}
|
|
23
|
+
const program_id = program.id();
|
|
24
|
+
if (!program.hasFunction(aleoFunction)) {
|
|
25
|
+
throw new Error(`Program ${program_id} does not contain function ${aleoFunction}`);
|
|
26
|
+
}
|
|
27
|
+
const cacheKey = `${program_id}:${aleoFunction}`;
|
|
28
|
+
// Get the program imports
|
|
29
|
+
let imports;
|
|
30
|
+
try {
|
|
31
|
+
imports = await programManager.networkClient.getProgramImports(localProgram);
|
|
32
|
+
}
|
|
33
|
+
catch (e) {
|
|
34
|
+
throw new Error("Error getting program imports");
|
|
35
|
+
}
|
|
36
|
+
// Get the proving and verifying keys for the function
|
|
37
|
+
if (lastLocalProgram !== localProgram) {
|
|
38
|
+
const keys = await programManager.synthesizeKeys(localProgram, aleoFunction, inputs, PrivateKey.from_string(privateKey));
|
|
39
|
+
programManager.keyProvider.cacheKeys(cacheKey, keys);
|
|
40
|
+
lastLocalProgram = localProgram;
|
|
41
|
+
}
|
|
42
|
+
// Pass the cache key to the execute function
|
|
43
|
+
const keyParams = new AleoKeyProviderParams({
|
|
44
|
+
cacheKey: cacheKey,
|
|
45
|
+
});
|
|
46
|
+
// Execute the function locally
|
|
47
|
+
const response = await programManager.run(localProgram, aleoFunction, inputs, proveExecution, imports, keyParams, undefined, undefined, PrivateKey.from_string(privateKey));
|
|
48
|
+
// Return the outputs to the main thread
|
|
49
|
+
console.log(`Web worker: Local execution completed in ${performance.now() - startTime} ms`);
|
|
50
|
+
const outputs = response.getOutputs();
|
|
51
|
+
const execution = response.getExecution();
|
|
52
|
+
let executionString = "";
|
|
53
|
+
let keys;
|
|
54
|
+
try {
|
|
55
|
+
keys = keyProvider.getKeys(cacheKey);
|
|
56
|
+
}
|
|
57
|
+
catch (e) {
|
|
58
|
+
throw new Error("Could not get verifying key");
|
|
59
|
+
}
|
|
60
|
+
const verifyingKey = keys[1];
|
|
61
|
+
if (execution) {
|
|
62
|
+
verifyFunctionExecution(execution, verifyingKey, program, "hello");
|
|
63
|
+
executionString = execution.toString();
|
|
64
|
+
console.log("Execution verified successfully: " + execution);
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
executionString = "";
|
|
68
|
+
}
|
|
69
|
+
console.log(`Function execution response: ${outputs}`);
|
|
70
|
+
return { outputs: outputs, execution: executionString };
|
|
71
|
+
}
|
|
72
|
+
async function getPrivateKey() {
|
|
73
|
+
const privateKey = new PrivateKey();
|
|
74
|
+
return privateKey.to_string();
|
|
75
|
+
}
|
|
76
|
+
const workerAPI = { run, getPrivateKey };
|
|
77
|
+
expose(workerAPI);
|
|
78
|
+
//# sourceMappingURL=worker.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"worker.js","sources":["../../src/worker.ts"],"sourcesContent":["import \"./polyfill/shared\";\nimport {initThreadPool, ProgramManager, PrivateKey, verifyFunctionExecution, FunctionKeyPair} from \"./browser\";\nimport { AleoKeyProvider, AleoKeyProviderParams} from \"./function-key-provider\";\nimport { expose } from \"comlink\";\n\nawait initThreadPool();\n\nconst defaultHost = \"https://api.explorer.provable.com/v1\";\nconst keyProvider = new AleoKeyProvider();\nconst programManager = new ProgramManager(\n defaultHost,\n keyProvider,\n undefined\n);\n\nkeyProvider.useCache(true);\n\nlet lastLocalProgram: string = \"\";\n\nexport interface WorkerAPI {\n run: (\n localProgram: string,\n aleoFunction: string,\n inputs: string[],\n privateKey: string\n ) => Promise<{ outputs: any; execution: string } | string>;\n\n getPrivateKey: () => Promise<PrivateKey>;\n}\nasync function run(\n localProgram: string,\n aleoFunction: string,\n inputs: string[],\n privateKey: string,\n proveExecution = false\n) {\n console.log(\"Web worker: Executing function locally...\");\n const startTime = performance.now();\n\n // Ensure the program is valid and that it contains the function specified\n let program;\n\n try {\n program = programManager.createProgramFromSource(localProgram);\n } catch (e) {\n throw new Error(\"Error creating program from source\");\n }\n\n const program_id = program.id();\n if (!program.hasFunction(aleoFunction)) {\n throw new Error(`Program ${program_id} does not contain function ${aleoFunction}`);\n }\n const cacheKey = `${program_id}:${aleoFunction}`;\n\n\n // Get the program imports\n let imports;\n\n try {\n imports = await programManager.networkClient.getProgramImports(\n localProgram\n );\n } catch (e) {\n throw new Error(\"Error getting program imports\");\n }\n\n // Get the proving and verifying keys for the function\n if (lastLocalProgram !== localProgram) {\n const keys = <FunctionKeyPair>await programManager.synthesizeKeys(\n localProgram,\n aleoFunction,\n inputs,\n PrivateKey.from_string(privateKey)\n );\n programManager.keyProvider.cacheKeys(cacheKey, keys);\n lastLocalProgram = localProgram;\n }\n\n // Pass the cache key to the execute function\n const keyParams = new AleoKeyProviderParams({\n cacheKey: cacheKey,\n });\n\n // Execute the function locally\n const response = await programManager.run(\n localProgram,\n aleoFunction,\n inputs,\n proveExecution,\n imports,\n keyParams,\n undefined,\n undefined,\n PrivateKey.from_string(privateKey),\n );\n\n // Return the outputs to the main thread\n console.log(\n `Web worker: Local execution completed in ${\n performance.now() - startTime\n } ms`\n );\n const outputs = response.getOutputs();\n const execution = response.getExecution();\n let executionString = \"\";\n\n let keys;\n\n try {\n keys = keyProvider.getKeys(cacheKey);\n } catch (e) {\n throw new Error(\"Could not get verifying key\");\n }\n\n const verifyingKey = keys[1];\n\n if (execution) {\n verifyFunctionExecution(\n execution,\n verifyingKey,\n program,\n \"hello\"\n );\n executionString = execution.toString();\n console.log(\"Execution verified successfully: \" + execution);\n } else {\n executionString = \"\";\n }\n\n console.log(`Function execution response: ${outputs}`);\n\n return { outputs: outputs, execution: executionString };\n}\n\nasync function getPrivateKey() {\n const privateKey = new PrivateKey();\n return privateKey.to_string();\n}\n\nconst workerAPI = { run, getPrivateKey };\nexpose(workerAPI);\n"],"names":[],"mappings":";;;;;AAKA,MAAM,cAAc,EAAE,CAAC;AAEvB,MAAM,WAAW,GAAG,sCAAsC,CAAC;AAC3D,MAAM,WAAW,GAAG,IAAI,eAAe,EAAE,CAAC;AAC1C,MAAM,cAAc,GAAG,IAAI,cAAc,CACrC,WAAW,EACX,WAAW,EACX,SAAS,CACZ,CAAC;AAEF,WAAW,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;AAE3B,IAAI,gBAAgB,GAAW,EAAE,CAAC;AAYlC,eAAe,GAAG,CACd,YAAoB,EACpB,YAAoB,EACpB,MAAgB,EAChB,UAAkB,EAClB,cAAc,GAAG,KAAK,EAAA;AAEtB,IAAA,OAAO,CAAC,GAAG,CAAC,2CAA2C,CAAC,CAAC;AACzD,IAAA,MAAM,SAAS,GAAG,WAAW,CAAC,GAAG,EAAE,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,MAAM,SAAS,GAAG,EAAE,GAAG,EAAE,aAAa,EAAE,CAAC;AACzC,MAAM,CAAC,SAAS,CAAC"}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { Address, PrivateKey, Signature, ViewKey, PrivateKeyCiphertext, RecordCiphertext } from "./wasm";
|
|
2
|
+
interface AccountParam {
|
|
3
|
+
privateKey?: string;
|
|
4
|
+
seed?: Uint8Array;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Key Management class. Enables the creation of a new Aleo Account, importation of an existing account from
|
|
8
|
+
* an existing private key or seed, and message signing and verification functionality.
|
|
9
|
+
*
|
|
10
|
+
* An Aleo Account is generated from a randomly generated seed (number) from which an account private key, view key,
|
|
11
|
+
* and a public account address are derived. The private key lies at the root of an Aleo account. It is a highly
|
|
12
|
+
* sensitive secret and should be protected as it allows for creation of Aleo Program executions and arbitrary value
|
|
13
|
+
* transfers. The View Key allows for decryption of a user's activity on the blockchain. The Address is the public
|
|
14
|
+
* address to which other users of Aleo can send Aleo credits and other records to. This class should only be used
|
|
15
|
+
* environments where the safety of the underlying key material can be assured.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* // Create a new account
|
|
19
|
+
* const myRandomAccount = new Account();
|
|
20
|
+
*
|
|
21
|
+
* // Create an account from a randomly generated seed
|
|
22
|
+
* const seed = new Uint8Array([94, 91, 52, 251, 240, 230, 226, 35, 117, 253, 224, 210, 175, 13, 205, 120, 155, 214, 7, 169, 66, 62, 206, 50, 188, 40, 29, 122, 40, 250, 54, 18]);
|
|
23
|
+
* const mySeededAccount = new Account({seed: seed});
|
|
24
|
+
*
|
|
25
|
+
* // Create an account from an existing private key
|
|
26
|
+
* const myExistingAccount = new Account({privateKey: 'myExistingPrivateKey'})
|
|
27
|
+
*
|
|
28
|
+
* // Sign a message
|
|
29
|
+
* const hello_world = Uint8Array.from([104, 101, 108, 108, 111 119, 111, 114, 108, 100])
|
|
30
|
+
* const signature = myRandomAccount.sign(hello_world)
|
|
31
|
+
*
|
|
32
|
+
* // Verify a signature
|
|
33
|
+
* myRandomAccount.verify(hello_world, signature)
|
|
34
|
+
*/
|
|
35
|
+
export declare class Account {
|
|
36
|
+
_privateKey: PrivateKey;
|
|
37
|
+
_viewKey: ViewKey;
|
|
38
|
+
_address: Address;
|
|
39
|
+
constructor(params?: AccountParam);
|
|
40
|
+
/**
|
|
41
|
+
* Attempts to create an account from a private key ciphertext
|
|
42
|
+
* @param {PrivateKeyCiphertext | string} ciphertext
|
|
43
|
+
* @param {string} password
|
|
44
|
+
* @returns {PrivateKey}
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* const ciphertext = PrivateKey.newEncrypted("password");
|
|
48
|
+
* const account = Account.fromCiphertext(ciphertext, "password");
|
|
49
|
+
*/
|
|
50
|
+
static fromCiphertext(ciphertext: PrivateKeyCiphertext | string, password: string): Account;
|
|
51
|
+
private privateKeyFromParams;
|
|
52
|
+
privateKey(): PrivateKey;
|
|
53
|
+
viewKey(): ViewKey;
|
|
54
|
+
address(): Address;
|
|
55
|
+
toString(): any;
|
|
56
|
+
/**
|
|
57
|
+
* Encrypt the account's private key with a password
|
|
58
|
+
* @param {string} ciphertext
|
|
59
|
+
* @returns {PrivateKeyCiphertext}
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* const account = new Account();
|
|
63
|
+
* const ciphertext = account.encryptAccount("password");
|
|
64
|
+
*/
|
|
65
|
+
encryptAccount(password: string): any;
|
|
66
|
+
/**
|
|
67
|
+
* Decrypts a Record in ciphertext form into plaintext
|
|
68
|
+
* @param {string} ciphertext
|
|
69
|
+
* @returns {Record}
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* const account = new Account();
|
|
73
|
+
* const record = account.decryptRecord("record1ciphertext");
|
|
74
|
+
*/
|
|
75
|
+
decryptRecord(ciphertext: string): any;
|
|
76
|
+
/**
|
|
77
|
+
* Decrypts an array of Records in ciphertext form into plaintext
|
|
78
|
+
* @param {string[]} ciphertexts
|
|
79
|
+
* @returns {Record[]}
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* const account = new Account();
|
|
83
|
+
* const record = account.decryptRecords(["record1ciphertext", "record2ciphertext"]);
|
|
84
|
+
*/
|
|
85
|
+
decryptRecords(ciphertexts: string[]): any[];
|
|
86
|
+
/**
|
|
87
|
+
* Determines whether the account owns a ciphertext record
|
|
88
|
+
* @param {RecordCipherText | string} ciphertext
|
|
89
|
+
* @returns {boolean}
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* // Create a connection to the Aleo network and an account
|
|
93
|
+
* const connection = new AleoNetworkClient("https://api.explorer.provable.com/v1");
|
|
94
|
+
* const account = Account.fromCiphertext("ciphertext", "password");
|
|
95
|
+
*
|
|
96
|
+
* // Get a record from the network
|
|
97
|
+
* const record = connection.getBlock(1234);
|
|
98
|
+
* const recordCipherText = record.transactions[0].execution.transitions[0].id;
|
|
99
|
+
*
|
|
100
|
+
* // Check if the account owns the record
|
|
101
|
+
* if account.ownsRecord(recordCipherText) {
|
|
102
|
+
* // Then one can do something like:
|
|
103
|
+
* // Decrypt the record and check if it's spent
|
|
104
|
+
* // Store the record in a local database
|
|
105
|
+
* // Etc.
|
|
106
|
+
* }
|
|
107
|
+
*/
|
|
108
|
+
ownsRecordCiphertext(ciphertext: RecordCiphertext | string): any;
|
|
109
|
+
/**
|
|
110
|
+
* Signs a message with the account's private key.
|
|
111
|
+
* Returns a Signature.
|
|
112
|
+
*
|
|
113
|
+
* @param {Uint8Array} message
|
|
114
|
+
* @returns {Signature}
|
|
115
|
+
*
|
|
116
|
+
* @example
|
|
117
|
+
* const account = new Account();
|
|
118
|
+
* const message = Uint8Array.from([104, 101, 108, 108, 111 119, 111, 114, 108, 100])
|
|
119
|
+
* account.sign(message);
|
|
120
|
+
*/
|
|
121
|
+
sign(message: Uint8Array): any;
|
|
122
|
+
/**
|
|
123
|
+
* Verifies the Signature on a message.
|
|
124
|
+
*
|
|
125
|
+
* @param {Uint8Array} message
|
|
126
|
+
* @param {Signature} signature
|
|
127
|
+
* @returns {boolean}
|
|
128
|
+
*
|
|
129
|
+
* @example
|
|
130
|
+
* const account = new Account();
|
|
131
|
+
* const message = Uint8Array.from([104, 101, 108, 108, 111 119, 111, 114, 108, 100])
|
|
132
|
+
* const signature = account.sign(message);
|
|
133
|
+
* account.verify(message, signature);
|
|
134
|
+
*/
|
|
135
|
+
verify(message: Uint8Array, signature: Signature): any;
|
|
136
|
+
}
|
|
137
|
+
export {};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import "./polyfill/shared";
|
|
2
|
+
import { Account } from "./account";
|
|
3
|
+
import { AleoNetworkClient, ProgramImports } from "./network-client";
|
|
4
|
+
import { Block } from "./models/block";
|
|
5
|
+
import { Execution } from "./models/execution";
|
|
6
|
+
import { Input } from "./models/input";
|
|
7
|
+
import { Output } from "./models/output";
|
|
8
|
+
import { TransactionModel } from "./models/transactionModel";
|
|
9
|
+
import { Transition } from "./models/transition";
|
|
10
|
+
import { AleoKeyProvider, AleoKeyProviderParams, AleoKeyProviderInitParams, CachedKeyPair, FunctionKeyPair, FunctionKeyProvider, KeySearchParams } from "./function-key-provider";
|
|
11
|
+
import { OfflineKeyProvider, OfflineSearchParams } from "./offline-key-provider";
|
|
12
|
+
import { BlockHeightSearch, NetworkRecordProvider, RecordProvider, RecordSearchParams } from "./record-provider";
|
|
13
|
+
declare function initializeWasm(): Promise<void>;
|
|
14
|
+
export { createAleoWorker } from "./managed-worker";
|
|
15
|
+
export { ProgramManager } from "./program-manager";
|
|
16
|
+
export { logAndThrow } from "./utils";
|
|
17
|
+
export { Address, Execution as FunctionExecution, ExecutionResponse, Field, OfflineQuery, PrivateKey, PrivateKeyCiphertext, Program, ProgramManager as ProgramManagerBase, ProvingKey, RecordCiphertext, RecordPlaintext, Signature, Transaction, VerifyingKey, ViewKey, initThreadPool, verifyFunctionExecution, } from "./wasm";
|
|
18
|
+
export { initializeWasm };
|
|
19
|
+
export { Key, CREDITS_PROGRAM_KEYS, KEY_STORE, PRIVATE_TRANSFER, PRIVATE_TO_PUBLIC_TRANSFER, PRIVATE_TRANSFER_TYPES, PUBLIC_TRANSFER, PUBLIC_TRANSFER_AS_SIGNER, PUBLIC_TO_PRIVATE_TRANSFER, VALID_TRANSFER_TYPES, } from "./constants";
|
|
20
|
+
export { Account, AleoKeyProvider, AleoKeyProviderParams, AleoKeyProviderInitParams, AleoNetworkClient, Block, BlockHeightSearch, CachedKeyPair, Execution, FunctionKeyPair, FunctionKeyProvider, Input, KeySearchParams, NetworkRecordProvider, ProgramImports, OfflineKeyProvider, OfflineSearchParams, Output, RecordProvider, RecordSearchParams, TransactionModel, Transition, };
|