1
- import 'dart:isolate' ;
2
1
import 'package:witnet/data_structures.dart' ;
3
2
import 'package:witnet/schema.dart' ;
4
3
import 'package:witnet/witnet.dart' ;
@@ -40,19 +39,12 @@ class ApiCrypto {
40
39
Future <String > generateMnemonic (int wordCount, String language) async {
41
40
try {
42
41
CryptoIsolate cryptoIsolate = Locator .instance.get <CryptoIsolate >();
43
- var receivePort = ReceivePort ();
44
- cryptoIsolate.send (
45
- method: 'generateMnemonic' ,
46
- params: {
47
- 'wordCount' : wordCount,
48
- 'language' : language,
49
- },
50
- port: receivePort.sendPort);
51
- return await receivePort.first.then ((value) {
52
- return value;
42
+ return await cryptoIsolate.send (method: 'generateMnemonic' , params: {
43
+ 'wordCount' : wordCount,
44
+ 'language' : language,
53
45
});
54
46
} catch (e) {
55
- print ('Error $e ' );
47
+ print ('Error in generate Mnemonic $e ' );
56
48
rethrow ;
57
49
}
58
50
}
@@ -64,21 +56,11 @@ class ApiCrypto {
64
56
return wallet.masterAccount! ;
65
57
}
66
58
CryptoIsolate cryptoIsolate = Locator .instance.get <CryptoIsolate >();
67
- final receivePort = ReceivePort ();
68
- cryptoIsolate.send (
69
- method: 'generateKey' ,
70
- params: {
71
- 'keyType' : keyType.name,
72
- 'external_keychain' : wallet.externalXpub,
73
- 'internal_keychain' : wallet.internalXpub,
74
- 'index' : index
75
- },
76
- port: receivePort.sendPort,
77
- );
78
- Xpub xpub = await receivePort.first.then ((value) {
79
- var val = value as Map <String , dynamic >;
80
- var _xpub = val['xpub' ];
81
- return _xpub;
59
+ Xpub xpub = await cryptoIsolate.send (method: 'generateKey' , params: {
60
+ 'keyType' : keyType.name,
61
+ 'external_keychain' : wallet.externalXpub,
62
+ 'internal_keychain' : wallet.internalXpub,
63
+ 'index' : index
82
64
});
83
65
Account _account = Account (
84
66
walletName: wallet.name, address: xpub.address, path: xpub.path! );
@@ -92,27 +74,16 @@ class ApiCrypto {
92
74
Future <Wallet > initializeWallet () async {
93
75
try {
94
76
CryptoIsolate cryptoIsolate = Locator .instance.get <CryptoIsolate >();
95
-
96
- final receivePort = ReceivePort ();
97
-
98
- cryptoIsolate.send (
99
- method: 'initializeWallet' ,
100
- params: {
101
- 'walletName' : walletName,
102
- 'walletType' : walletType != null ? walletType! .name : null ,
103
- 'seedSource' : seedSource,
104
- 'seed' : seed,
105
- 'password' : password,
106
- },
107
- port: receivePort.sendPort);
108
- clearInitialWalletData ();
109
- Wallet dbWallet = await receivePort.first.then ((value) {
110
- var val = value as Map <String , dynamic >;
111
- var _wallet = val['wallet' ];
112
- return _wallet;
77
+ Wallet wallet =
78
+ await cryptoIsolate.send (method: 'initializeWallet' , params: {
79
+ 'walletName' : walletName,
80
+ 'walletType' : walletType != null ? walletType! .name : null ,
81
+ 'seedSource' : seedSource,
82
+ 'seed' : seed,
83
+ 'password' : password,
113
84
});
114
-
115
- return dbWallet ;
85
+ clearInitialWalletData ();
86
+ return wallet ;
116
87
} catch (e) {
117
88
rethrow ;
118
89
}
@@ -165,51 +136,31 @@ class ApiCrypto {
165
136
}
166
137
}
167
138
}
168
- final receivePort = ReceivePort ();
169
139
CryptoIsolate cryptoIsolate = Locator .instance.get <CryptoIsolate >();
170
- cryptoIsolate.send (
140
+ List < KeyedSignature > signatures = await cryptoIsolate.send (
171
141
method: 'signTransaction' ,
172
142
params: {
173
143
'password' : key,
174
144
'signers' : _signers,
175
145
'transaction_id' : transactionId
176
- },
177
- port: receivePort.sendPort);
178
-
179
- List <KeyedSignature > signatures = await receivePort.first.then ((value) {
180
- return value as List <KeyedSignature >;
181
- });
146
+ });
182
147
return signatures;
183
148
}
184
149
185
150
Future <String > hashPassword ({required String password}) async {
186
- final receivePort = ReceivePort ();
187
151
CryptoIsolate cryptoIsolate = Locator .instance.get <CryptoIsolate >();
188
- cryptoIsolate.send (
189
- method: 'hashPassword' ,
190
- params: {
191
- 'password' : password,
192
- },
193
- port: receivePort.sendPort);
194
- Map <String , String > response =
195
- await receivePort.first.then ((value) => value as Map <String , String >);
196
- return response['hash' ]! ;
152
+ return await cryptoIsolate.send (method: 'hashPassword' , params: {
153
+ 'password' : password,
154
+ });
197
155
}
198
156
199
157
Future <String > encryptXprv (
200
158
{required String xprv, required String password}) async {
201
- final receivePort = ReceivePort ();
202
159
CryptoIsolate cryptoIsolate = Locator .instance.get <CryptoIsolate >();
203
- cryptoIsolate.send (
204
- method: 'encryptXprv' ,
205
- params: {
206
- 'xprv' : xprv,
207
- 'password' : password,
208
- },
209
- port: receivePort.sendPort);
210
- Map <String , String > passwordHash =
211
- await receivePort.first.then ((value) => value as Map <String , String >);
212
- return passwordHash['xprv' ]! ;
160
+ return await cryptoIsolate.send (method: 'encryptXprv' , params: {
161
+ 'xprv' : xprv,
162
+ 'password' : password,
163
+ });
213
164
}
214
165
215
166
Future <String > verifiedXprv ({required String xprv}) async {
@@ -225,18 +176,12 @@ class ApiCrypto {
225
176
226
177
Future <String > decryptXprv (
227
178
{required String xprv, required String password}) async {
228
- final receivePort = ReceivePort ();
229
179
CryptoIsolate cryptoIsolate = Locator .instance.get <CryptoIsolate >();
230
- cryptoIsolate.send (
231
- method: 'decryptXprv' ,
232
- params: {
233
- 'xprv' : xprv,
234
- 'password' : password,
235
- },
236
- port: receivePort.sendPort);
180
+ final response = await cryptoIsolate.send (method: 'decryptXprv' , params: {
181
+ 'xprv' : xprv,
182
+ 'password' : password,
183
+ }) as Map <String , String >;
237
184
238
- Map <String , String > response =
239
- await receivePort.first.then ((value) => value as Map <String , String >);
240
185
if (response.containsKey ('xprv' )) {
241
186
return response['xprv' ]! ;
242
187
} else {
@@ -246,32 +191,20 @@ class ApiCrypto {
246
191
247
192
Future <bool > verifySheikahXprv (
248
193
{required String xprv, required String password}) async {
249
- final receivePort = ReceivePort ();
250
194
CryptoIsolate cryptoIsolate = Locator .instance.get <CryptoIsolate >();
251
- cryptoIsolate.send (
252
- method: 'decryptXprv' ,
253
- params: {
254
- 'xprv' : xprv,
255
- 'password' : password,
256
- },
257
- port: receivePort.sendPort);
258
- bool valid = await receivePort.first.then ((value) => value as bool );
259
- return valid;
195
+ return await cryptoIsolate.send (method: 'decryptXprv' , params: {
196
+ 'xprv' : xprv,
197
+ 'password' : password,
198
+ });
260
199
}
261
200
262
201
Future <bool > verifyLocalXprv (
263
202
{required String xprv, required String password}) async {
264
- final receivePort = ReceivePort ();
265
203
CryptoIsolate cryptoIsolate = Locator .instance.get <CryptoIsolate >();
266
- cryptoIsolate.send (
267
- method: 'decryptXprv' ,
268
- params: {
269
- 'xprv' : xprv,
270
- 'password' : password,
271
- },
272
- port: receivePort.sendPort);
273
- bool valid = await receivePort.first.then ((value) => value as bool );
274
- return valid;
204
+ return await cryptoIsolate.send (method: 'decryptXprv' , params: {
205
+ 'xprv' : xprv,
206
+ 'password' : password,
207
+ });
275
208
}
276
209
277
210
Future <Map <String , dynamic >> signMessage (
@@ -287,18 +220,11 @@ class ApiCrypto {
287
220
currentWallet.xprv! : _account! .path,
288
221
};
289
222
290
- final receivePort = ReceivePort ();
291
223
CryptoIsolate cryptoIsolate = Locator .instance.get <CryptoIsolate >();
292
- cryptoIsolate.send (
293
- method: 'signMessage' ,
294
- params: {
295
- 'password' : key,
296
- 'signer' : _signer,
297
- 'message' : message,
298
- },
299
- port: receivePort.sendPort);
300
- var signedMessage = await receivePort.first.then ((value) => value);
301
-
302
- return signedMessage;
224
+ return await cryptoIsolate.send (method: 'signMessage' , params: {
225
+ 'password' : key,
226
+ 'signer' : _signer,
227
+ 'message' : message,
228
+ });
303
229
}
304
230
}
0 commit comments