1
- import "babel-polyfill" ;
1
+ import init , * as aleo from '@aleohq/wasm' ;
2
+
3
+ await init ( ) ;
4
+
5
+ await aleo . initThreadPool ( navigator . hardwareConcurrency ) ;
2
6
3
7
let TRANSFER_KEY ;
4
8
let INCLUSION_KEY ;
5
9
6
- import ( "@aleohq/wasm" ) . then ( aleo => {
7
- self . addEventListener ( "message" , ev => {
8
- // Load Transfer Prover Key
9
- if ( ev . data . type == 'ALEO_LOAD_TRANSFER_KEY' ) {
10
- console . log ( 'Web worker: Deserialize Transfer Key...' ) ;
11
- let startTime = performance . now ( ) ;
12
- TRANSFER_KEY = aleo . ProvingKey . from_bytes ( ev . data . transferProverBytes ) ;
13
- console . log ( `Web worker: Deserialized transfer proving key Completed: ${ performance . now ( ) - startTime } ms` ) ;
14
- self . postMessage ( { type : 'TRANSFER_KEY_DESERIALIZED' } ) ;
15
- }
16
- // Load Inclusion Prover Key
17
- else if ( ev . data . type == 'ALEO_LOAD_INCLUSION_KEY' ) {
18
- console . log ( 'Web worker: Deserialize Inclusion Key...' ) ;
19
- console . log ( ev . data ) ;
20
- let startTime = performance . now ( ) ;
21
- INCLUSION_KEY = aleo . ProvingKey . from_bytes ( ev . data . inclusionProverBytes ) ;
22
- console . log ( `Web worker: Deserialized inclusion proving key Completed: ${ performance . now ( ) - startTime } ms` ) ;
23
- self . postMessage ( { type : 'INCLUSION_KEY_DESERIALIZED' } ) ;
24
- }
25
- // Create Transition
26
- else if ( ev . data . type == 'ALEO_CREATE_TRANSITION' ) {
27
- const {
28
- privateKey,
29
- toAddress,
30
- amount,
31
- plaintext
32
- } = ev . data ;
33
- const program = aleo . Program . credits ( ) ;
10
+ self . addEventListener ( "message" , ev => {
11
+ // Load Transfer Prover Key
12
+ if ( ev . data . type == 'ALEO_LOAD_TRANSFER_KEY' ) {
13
+ console . log ( 'Web worker: Deserialize Transfer Key...' ) ;
14
+ let startTime = performance . now ( ) ;
15
+ TRANSFER_KEY = aleo . ProvingKey . from_bytes ( ev . data . transferProverBytes ) ;
16
+ console . log ( `Web worker: Deserialized transfer proving key Completed: ${ performance . now ( ) - startTime } ms` ) ;
17
+ self . postMessage ( { type : 'TRANSFER_KEY_DESERIALIZED' } ) ;
18
+ }
19
+ // Load Inclusion Prover Key
20
+ else if ( ev . data . type == 'ALEO_LOAD_INCLUSION_KEY' ) {
21
+ console . log ( 'Web worker: Deserialize Inclusion Key...' ) ;
22
+ console . log ( ev . data ) ;
23
+ let startTime = performance . now ( ) ;
24
+ INCLUSION_KEY = aleo . ProvingKey . from_bytes ( ev . data . inclusionProverBytes ) ;
25
+ console . log ( `Web worker: Deserialized inclusion proving key Completed: ${ performance . now ( ) - startTime } ms` ) ;
26
+ self . postMessage ( { type : 'INCLUSION_KEY_DESERIALIZED' } ) ;
27
+ }
28
+ // Create Transition
29
+ else if ( ev . data . type == 'ALEO_CREATE_TRANSITION' ) {
30
+ const {
31
+ privateKey,
32
+ toAddress,
33
+ amount,
34
+ plaintext
35
+ } = ev . data ;
36
+ const program = aleo . Program . credits ( ) ;
34
37
35
- console . log ( 'Web worker: Building Transition for program: ' , program . id ( ) ) ;
36
- let startTime = performance . now ( ) ;
38
+ console . log ( 'Web worker: Building Transition for program: ' , program . id ( ) ) ;
39
+ let startTime = performance . now ( ) ;
37
40
38
- // Prepare inputs
39
- const pK = aleo . PrivateKey . from_string ( privateKey ) ;
40
- const inputs = JSON . stringify ( [ plaintext , toAddress , `${ amount } u64` ] )
41
+ // Prepare inputs
42
+ const pK = aleo . PrivateKey . from_string ( privateKey ) ;
43
+ const inputs = JSON . stringify ( [ plaintext , toAddress , `${ amount } u64` ] )
41
44
42
- const transition = aleo . TransactionBuilder . build_transition (
43
- program ,
44
- 'transfer' ,
45
- inputs ,
46
- pK ,
47
- TRANSFER_KEY
48
- ) ;
49
- console . log ( `Web worker: Transition Completed: ${ performance . now ( ) - startTime } ms` ) ;
50
- console . log ( `Transition: ${ transition } ` ) ;
51
- self . postMessage ( { type : 'TRANSITION_COMPLETED' , transition } ) ;
52
- }
53
- else if ( ev . data . type == 'ALEO_CREATE_TRANSACTION' ) {
54
- const {
55
- transition,
56
- inputIds,
57
- stateRoot,
58
- statePaths
59
- } = ev . data ;
45
+ const transition = aleo . TransactionBuilder . build_transition (
46
+ program ,
47
+ 'transfer' ,
48
+ inputs ,
49
+ pK ,
50
+ TRANSFER_KEY
51
+ ) ;
52
+ console . log ( `Web worker: Transition Completed: ${ performance . now ( ) - startTime } ms` ) ;
53
+ console . log ( `Transition: ${ transition } ` ) ;
54
+ self . postMessage ( { type : 'TRANSITION_COMPLETED' , transition } ) ;
55
+ }
56
+ else if ( ev . data . type == 'ALEO_CREATE_TRANSACTION' ) {
57
+ const {
58
+ transition,
59
+ inputIds,
60
+ stateRoot,
61
+ statePaths
62
+ } = ev . data ;
60
63
61
- console . log ( 'Web worker: Building Transaction...' ) ;
62
- let startTime = performance . now ( ) ;
63
- let transitionParsed = JSON . parse ( transition ) ;
64
- let inputIdsParsed = JSON . parse ( inputIds ) ;
64
+ console . log ( 'Web worker: Building Transaction...' ) ;
65
+ let startTime = performance . now ( ) ;
66
+ let transitionParsed = JSON . parse ( transition ) ;
67
+ let inputIdsParsed = JSON . parse ( inputIds ) ;
65
68
66
- const transaction = aleo . TransactionBuilder . build_transaction (
67
- INCLUSION_KEY ,
68
- JSON . stringify ( [ { transition : transitionParsed , input_ids : inputIdsParsed } ] ) ,
69
- stateRoot ,
70
- statePaths
71
- ) ;
72
- console . log ( `Web worker: Transaction Completed: ${ performance . now ( ) - startTime } ms` ) ;
73
- console . log ( `Transaction: ${ transaction } ` ) ;
74
- self . postMessage ( { type : 'TRANSACTION_COMPLETED' , transaction } ) ;
75
- }
76
- else if ( ev . data . type == 'ALEO_VERIFY_TRANSACTION' ) {
77
- const {
78
- transaction,
79
- transferVerifierBytes,
80
- inclusionVerifierBytes
81
- } = ev . data ;
82
- console . log ( 'Web worker: Verifying Transaction...' ) ;
83
- let startTime = performance . now ( ) ;
69
+ const transaction = aleo . TransactionBuilder . build_transaction (
70
+ INCLUSION_KEY ,
71
+ JSON . stringify ( [ { transition : transitionParsed , input_ids : inputIdsParsed } ] ) ,
72
+ stateRoot ,
73
+ statePaths
74
+ ) ;
75
+ console . log ( `Web worker: Transaction Completed: ${ performance . now ( ) - startTime } ms` ) ;
76
+ console . log ( `Transaction: ${ transaction } ` ) ;
77
+ self . postMessage ( { type : 'TRANSACTION_COMPLETED' , transaction } ) ;
78
+ }
79
+ else if ( ev . data . type == 'ALEO_VERIFY_TRANSACTION' ) {
80
+ const {
81
+ transaction,
82
+ transferVerifierBytes,
83
+ inclusionVerifierBytes
84
+ } = ev . data ;
85
+ console . log ( 'Web worker: Verifying Transaction...' ) ;
86
+ let startTime = performance . now ( ) ;
84
87
85
- const program = aleo . Program . credits ( ) ;
86
- const functionName = 'transfer' ;
87
- const transferVerifyingKey = aleo . VerifyingKey . from_bytes ( transferVerifierBytes ) ;
88
- const inclusionVerifyingKey = aleo . VerifyingKey . from_bytes ( inclusionVerifierBytes ) ;
88
+ const program = aleo . Program . credits ( ) ;
89
+ const functionName = 'transfer' ;
90
+ const transferVerifyingKey = aleo . VerifyingKey . from_bytes ( transferVerifierBytes ) ;
91
+ const inclusionVerifyingKey = aleo . VerifyingKey . from_bytes ( inclusionVerifierBytes ) ;
89
92
90
- const verified = aleo . TransactionBuilder . verify_transaction (
91
- transaction ,
92
- program ,
93
- functionName ,
94
- transferVerifyingKey ,
95
- inclusionVerifyingKey ,
96
- true ,
97
- true
98
- ) ;
99
- console . log ( `Web worker: Transaction Verified: ${ performance . now ( ) - startTime } ms` ) ;
100
- console . log ( verified ) ;
101
- }
102
- } ) ;
93
+ const verified = aleo . TransactionBuilder . verify_transaction (
94
+ transaction ,
95
+ program ,
96
+ functionName ,
97
+ transferVerifyingKey ,
98
+ inclusionVerifyingKey ,
99
+ true ,
100
+ true
101
+ ) ;
102
+ console . log ( `Web worker: Transaction Verified: ${ performance . now ( ) - startTime } ms` ) ;
103
+ console . log ( verified ) ;
104
+ }
103
105
} ) ;
0 commit comments