|
1 | 1 | import {
|
2 |
| - Account, |
3 |
| - ProgramManager, |
4 |
| - PrivateKey, |
5 |
| - initThreadPool, |
6 |
| - AleoKeyProvider, |
7 |
| - AleoNetworkClient, |
8 |
| - NetworkRecordProvider, |
9 |
| - } from "@aleohq/sdk"; |
10 |
| - |
11 |
| - await initThreadPool(); |
12 |
| - |
13 |
| - const hello_hello_program = |
14 |
| - "program hello_hello.aleo;\n" + |
15 |
| - "\n" + |
16 |
| - "function hello:\n" + |
17 |
| - " input r0 as u32.public;\n" + |
18 |
| - " input r1 as u32.private;\n" + |
19 |
| - " add r0 r1 into r2;\n" + |
20 |
| - " output r2 as u32.private;\n"; |
21 |
| - |
22 |
| - async function localProgramExecution(program, aleoFunction, inputs) { |
23 |
| - const programManager = new ProgramManager(); |
24 |
| - |
25 |
| - // Create a temporary account for the execution of the program |
26 |
| - const account = new Account(); |
27 |
| - programManager.setAccount(account); |
28 |
| - |
29 |
| - const executionResponse = await programManager.run( |
30 |
| - hello_hello_program, |
31 |
| - "hello", |
32 |
| - ["5u32", "5u32"], |
33 |
| - false, |
34 |
| - ); |
35 |
| - return executionResponse.getOutputs(); |
36 |
| - } |
37 |
| - |
38 |
| - function getPrivateKey() { |
39 |
| - return new PrivateKey().to_string(); |
40 |
| - } |
41 |
| - |
42 |
| - async function deployProgram(program) { |
43 |
| - const keyProvider = new AleoKeyProvider(); |
44 |
| - keyProvider.useCache(true); |
45 |
| - |
46 |
| - // Create a record provider that will be used to find records and transaction data for Aleo programs |
47 |
| - const networkClient = new AleoNetworkClient("https://api.explorer.aleo.org/v1"); |
48 |
| - |
49 |
| - // Use existing account with funds |
50 |
| - const account = new Account({ |
51 |
| - privateKey: "user1PrivateKey", |
52 |
| - }); |
53 |
| - |
54 |
| - const recordProvider = new NetworkRecordProvider(account, networkClient); |
55 |
| - |
56 |
| - // Initialize a program manager to talk to the Aleo network with the configured key and record providers |
57 |
| - const programManager = new ProgramManager( |
58 |
| - "https://api.explorer.aleo.org/v1", |
59 |
| - keyProvider, |
60 |
| - recordProvider, |
61 |
| - ); |
62 |
| - |
| 2 | + Account, |
| 3 | + ProgramManager, |
| 4 | + PrivateKey, |
| 5 | + initThreadPool, |
| 6 | + AleoKeyProvider, |
| 7 | + AleoNetworkClient, |
| 8 | + NetworkRecordProvider, |
| 9 | +} from "@aleohq/sdk"; |
| 10 | + |
| 11 | +await initThreadPool(); |
| 12 | + |
| 13 | +// A program name must be unique. To deploy the program, you should change the name from hello_hello.aleo to something else that is unique. |
| 14 | +const hello_hello_program =` |
| 15 | +program hello_hello.aleo; |
| 16 | +
|
| 17 | +function hello: |
| 18 | + input r0 as u32.public; |
| 19 | + input r1 as u32.private; |
| 20 | + add r0 r1 into r2; |
| 21 | + output r2 as u32.private;` |
| 22 | + |
| 23 | +async function localProgramExecution(program, aleoFunction, inputs) { |
| 24 | + const programManager = new ProgramManager(); |
| 25 | + |
| 26 | + // Create a temporary account for the execution of the program |
| 27 | + const account = new Account(); |
63 | 28 | programManager.setAccount(account);
|
64 |
| - |
65 |
| - // Define a fee to pay to deploy the program |
66 |
| - const fee = 1.9; // 1.9 Aleo credits |
67 |
| - |
68 |
| - // Deploy the program to the Aleo network |
69 |
| - const tx_id = await programManager.deploy(program, fee); |
70 |
| - |
71 |
| - // Optional: Pass in fee record manually to avoid long scan times |
72 |
| - // const feeRecord = "{ owner: aleo1xxx...xxx.private, microcredits: 2000000u64.private, _nonce: 123...789group.public}"; |
73 |
| - // const tx_id = await programManager.deploy(program, fee, undefined, feeRecord); |
74 |
| - |
75 |
| - return tx_id; |
76 |
| - } |
77 |
| - |
78 |
| - onmessage = async function (e) { |
79 |
| - if (e.data === "execute") { |
80 |
| - const result = await localProgramExecution(); |
81 |
| - postMessage(result); |
82 |
| - } else if (e.data === "key") { |
83 |
| - const result = getPrivateKey(); |
84 |
| - postMessage(result); |
85 |
| - } else if (e.data === "deploy") { |
86 |
| - try { |
87 |
| - const result = await deployProgram(hello_hello_program); |
88 |
| - postMessage(result); |
89 |
| - } catch (error) { |
90 |
| - postMessage({ error: error.message }); |
91 |
| - } |
| 29 | + |
| 30 | + const executionResponse = await programManager.run( |
| 31 | + program, |
| 32 | + aleoFunction, |
| 33 | + inputs, |
| 34 | + false, |
| 35 | + ); |
| 36 | + return executionResponse.getOutputs(); |
| 37 | +} |
| 38 | + |
| 39 | +function getPrivateKey() { |
| 40 | + return new PrivateKey().to_string(); |
| 41 | +} |
| 42 | + |
| 43 | +async function deployProgram(program) { |
| 44 | + const keyProvider = new AleoKeyProvider(); |
| 45 | + keyProvider.useCache(true); |
| 46 | + |
| 47 | + // Create a record provider that will be used to find records and transaction data for Aleo programs |
| 48 | + const networkClient = new AleoNetworkClient("https://api.explorer.aleo.org/v1"); |
| 49 | + |
| 50 | + // Use existing account with funds |
| 51 | + const account = new Account({ |
| 52 | + privateKey: "user1PrivateKey", |
| 53 | + }); |
| 54 | + |
| 55 | + const recordProvider = new NetworkRecordProvider(account, networkClient); |
| 56 | + |
| 57 | + // Initialize a program manager to talk to the Aleo network with the configured key and record providers |
| 58 | + const programManager = new ProgramManager( |
| 59 | + "https://api.explorer.aleo.org/v1", |
| 60 | + keyProvider, |
| 61 | + recordProvider, |
| 62 | + ); |
| 63 | + |
| 64 | + programManager.setAccount(account); |
| 65 | + |
| 66 | + // Define a fee to pay to deploy the program |
| 67 | + const fee = 1.9; // 1.9 Aleo credits |
| 68 | + |
| 69 | + // Deploy the program to the Aleo network |
| 70 | + const tx_id = await programManager.deploy(program, fee); |
| 71 | + |
| 72 | + // Optional: Pass in fee record manually to avoid long scan times |
| 73 | + // const feeRecord = "{ owner: aleo1xxx...xxx.private, microcredits: 2000000u64.private, _nonce: 123...789group.public}"; |
| 74 | + // const tx_id = await programManager.deploy(program, fee, undefined, feeRecord); |
| 75 | + |
| 76 | + return tx_id; |
| 77 | +} |
| 78 | + |
| 79 | +onmessage = async function (e) { |
| 80 | + if (e.data === "execute") { |
| 81 | + const result = await localProgramExecution(hello_hello_program, "hello", ["5u32", "5u32"]); |
| 82 | + postMessage(result); |
| 83 | + } else if (e.data === "key") { |
| 84 | + const result = getPrivateKey(); |
| 85 | + postMessage(result); |
| 86 | + } else if (e.data === "deploy") { |
| 87 | + try { |
| 88 | + const result = await deployProgram(hello_hello_program); |
| 89 | + postMessage(result); |
| 90 | + } catch (error) { |
| 91 | + postMessage({ error: error.message }); |
92 | 92 | }
|
93 |
| - |
94 |
| - }; |
| 93 | + } |
95 | 94 |
|
| 95 | +}; |
0 commit comments