Skip to content

Commit cfdca72

Browse files
committed
refactor: use run instead of spawn in crypto isolate
1 parent 5ca0fc9 commit cfdca72

File tree

8 files changed

+222
-389
lines changed

8 files changed

+222
-389
lines changed

lib/bloc/crypto/api_crypto.dart

Lines changed: 44 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import 'dart:isolate';
21
import 'package:witnet/data_structures.dart';
32
import 'package:witnet/schema.dart';
43
import 'package:witnet/witnet.dart';
@@ -40,19 +39,12 @@ class ApiCrypto {
4039
Future<String> generateMnemonic(int wordCount, String language) async {
4140
try {
4241
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,
5345
});
5446
} catch (e) {
55-
print('Error $e');
47+
print('Error in generate Mnemonic $e');
5648
rethrow;
5749
}
5850
}
@@ -64,21 +56,11 @@ class ApiCrypto {
6456
return wallet.masterAccount!;
6557
}
6658
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
8264
});
8365
Account _account = Account(
8466
walletName: wallet.name, address: xpub.address, path: xpub.path!);
@@ -92,27 +74,16 @@ class ApiCrypto {
9274
Future<Wallet> initializeWallet() async {
9375
try {
9476
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,
11384
});
114-
115-
return dbWallet;
85+
clearInitialWalletData();
86+
return wallet;
11687
} catch (e) {
11788
rethrow;
11889
}
@@ -165,51 +136,31 @@ class ApiCrypto {
165136
}
166137
}
167138
}
168-
final receivePort = ReceivePort();
169139
CryptoIsolate cryptoIsolate = Locator.instance.get<CryptoIsolate>();
170-
cryptoIsolate.send(
140+
List<KeyedSignature> signatures = await cryptoIsolate.send(
171141
method: 'signTransaction',
172142
params: {
173143
'password': key,
174144
'signers': _signers,
175145
'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+
});
182147
return signatures;
183148
}
184149

185150
Future<String> hashPassword({required String password}) async {
186-
final receivePort = ReceivePort();
187151
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+
});
197155
}
198156

199157
Future<String> encryptXprv(
200158
{required String xprv, required String password}) async {
201-
final receivePort = ReceivePort();
202159
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+
});
213164
}
214165

215166
Future<String> verifiedXprv({required String xprv}) async {
@@ -225,18 +176,12 @@ class ApiCrypto {
225176

226177
Future<String> decryptXprv(
227178
{required String xprv, required String password}) async {
228-
final receivePort = ReceivePort();
229179
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>;
237184

238-
Map<String, String> response =
239-
await receivePort.first.then((value) => value as Map<String, String>);
240185
if (response.containsKey('xprv')) {
241186
return response['xprv']!;
242187
} else {
@@ -246,32 +191,20 @@ class ApiCrypto {
246191

247192
Future<bool> verifySheikahXprv(
248193
{required String xprv, required String password}) async {
249-
final receivePort = ReceivePort();
250194
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+
});
260199
}
261200

262201
Future<bool> verifyLocalXprv(
263202
{required String xprv, required String password}) async {
264-
final receivePort = ReceivePort();
265203
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+
});
275208
}
276209

277210
Future<Map<String, dynamic>> signMessage(
@@ -287,18 +220,11 @@ class ApiCrypto {
287220
currentWallet.xprv!: _account!.path,
288221
};
289222

290-
final receivePort = ReceivePort();
291223
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+
});
303229
}
304230
}

lib/bloc/crypto/crypto_bloc.dart

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import 'dart:async';
2-
import 'dart:convert';
32
import 'dart:isolate';
43
import 'dart:typed_data';
54
import 'package:bloc/bloc.dart';
@@ -8,6 +7,7 @@ import 'package:flutter_bloc/flutter_bloc.dart';
87
import 'package:my_wit_wallet/util/storage/cache/implementations/vtt_get_through_block_explorer.dart';
98
import 'package:my_wit_wallet/util/storage/database/adapters/transaction_adapter.dart';
109
import 'package:witnet/data_structures.dart';
10+
import 'package:my_wit_wallet/util/storage/log.dart';
1111
import 'package:witnet/constants.dart';
1212
import 'package:witnet/crypto.dart';
1313
import 'package:witnet/explorer.dart';
@@ -29,16 +29,6 @@ part 'crypto_event.dart';
2929
part 'crypto_state.dart';
3030
part 'crypto_isolate.dart';
3131

32-
Future<dynamic> isolateRunner(
33-
String method, Map<String, dynamic> params) async {
34-
ReceivePort response = ReceivePort();
35-
36-
/// send the request
37-
Locator.instance<CryptoIsolate>()
38-
.send(method: method, params: params, port: response.sendPort);
39-
return await response.first;
40-
}
41-
4232
class CryptoBloc extends Bloc<CryptoEvent, CryptoState> {
4333
ApiExplorer apiExplorer = Locator.instance.get<ApiExplorer>();
4434
ApiDatabase db = Locator.instance.get<ApiDatabase>();

0 commit comments

Comments
 (0)