Potential fix for missing polyseeds in the future (#1468)

Konstantin Ullrich committed May 28, 2024 at 15:32 UTC faa49d21e8ab99303d756e98447c908669141515
4 files changed +93 -69
cw_monero/ios/Classes/monero_api.cpp
+11
@@ -399,6 +399,7 @@ extern "C"
399 return false;
400 }
401
402 + wallet->store(std::string(path));
403 change_current_wallet(wallet);
404 return true;
405 }
@@ -464,6 +465,16 @@ extern "C"
465 return strdup(get_current_wallet()->address(account_index, address_index).c_str());
466 }
467
468 + char *get_cache_attribute(char *name)
469 + {
470 + return strdup(get_current_wallet()->getCacheAttribute(std::string(name)).c_str());
471 + }
472 +
473 + bool set_cache_attribute(char *name, char *value)
474 + {
475 + get_current_wallet()->setCacheAttribute(std::string(name), std::string(value));
476 + return true;
477 + }
478
479 const char *seed()
480 {
cw_monero/lib/api/signatures.dart
+4
@@ -154,3 +154,7 @@ typedef freeze_coin = Void Function(Int32 index);
154 typedef thaw_coin = Void Function(Int32 index);
155
156 typedef sign_message = Pointer<Utf8> Function(Pointer<Utf8> message, Pointer<Utf8> address);
157 +
158 +typedef get_cache_attribute = Pointer<Utf8> Function(Pointer<Utf8> name);
159 +
160 +typedef set_cache_attribute = Int8 Function(Pointer<Utf8> name, Pointer<Utf8> value);
cw_monero/lib/api/types.dart
+4
@@ -154,3 +154,7 @@ typedef FreezeCoin = void Function(int);
154 typedef ThawCoin = void Function(int);
155
156 typedef SignMessage = Pointer<Utf8> Function(Pointer<Utf8>, Pointer<Utf8>);
157 +
158 +typedef GetCacheAttribute = Pointer<Utf8> Function(Pointer<Utf8>);
159 +
160 +typedef SetCacheAttribute = int Function(Pointer<Utf8>, Pointer<Utf8>);
cw_monero/lib/api/wallet.dart
+74 -69
@@ -1,26 +1,24 @@
1 import 'dart:async';
2 import 'dart:ffi';
3 -import 'package:ffi/ffi.dart';
4 -import 'package:cw_monero/api/structs/ut8_box.dart';
3 +
4 import 'package:cw_monero/api/convert_utf8_to_string.dart';
5 +import 'package:cw_monero/api/exceptions/setup_wallet_exception.dart';
6 +import 'package:cw_monero/api/monero_api.dart';
7 import 'package:cw_monero/api/signatures.dart';
8 +import 'package:cw_monero/api/structs/ut8_box.dart';
9 import 'package:cw_monero/api/types.dart';
8 -import 'package:cw_monero/api/monero_api.dart';
9 -import 'package:cw_monero/api/exceptions/setup_wallet_exception.dart';
10 +import 'package:ffi/ffi.dart';
11 import 'package:flutter/foundation.dart';
12
13 int _boolToInt(bool value) => value ? 1 : 0;
14
14 -final getFileNameNative = moneroApi
15 - .lookup<NativeFunction<get_filename>>('get_filename')
16 - .asFunction<GetFilename>();
15 +final getFileNameNative =
16 + moneroApi.lookup<NativeFunction<get_filename>>('get_filename').asFunction<GetFilename>();
17
18 -final getSeedNative =
19 - moneroApi.lookup<NativeFunction<get_seed>>('seed').asFunction<GetSeed>();
18 +final getSeedNative = moneroApi.lookup<NativeFunction<get_seed>>('seed').asFunction<GetSeed>();
19
21 -final getAddressNative = moneroApi
22 - .lookup<NativeFunction<get_address>>('get_address')
23 - .asFunction<GetAddress>();
20 +final getAddressNative =
21 + moneroApi.lookup<NativeFunction<get_address>>('get_address').asFunction<GetAddress>();
22
23 final getFullBalanceNative = moneroApi
24 .lookup<NativeFunction<get_full_balanace>>('get_full_balance')
@@ -38,41 +36,34 @@ final getNodeHeightNative = moneroApi
36 .lookup<NativeFunction<get_node_height>>('get_node_height')
37 .asFunction<GetNodeHeight>();
38
41 -final isConnectedNative = moneroApi
42 - .lookup<NativeFunction<is_connected>>('is_connected')
43 - .asFunction<IsConnected>();
39 +final isConnectedNative =
40 + moneroApi.lookup<NativeFunction<is_connected>>('is_connected').asFunction<IsConnected>();
41
45 -final setupNodeNative = moneroApi
46 - .lookup<NativeFunction<setup_node>>('setup_node')
47 - .asFunction<SetupNode>();
42 +final setupNodeNative =
43 + moneroApi.lookup<NativeFunction<setup_node>>('setup_node').asFunction<SetupNode>();
44
49 -final startRefreshNative = moneroApi
50 - .lookup<NativeFunction<start_refresh>>('start_refresh')
51 - .asFunction<StartRefresh>();
45 +final startRefreshNative =
46 + moneroApi.lookup<NativeFunction<start_refresh>>('start_refresh').asFunction<StartRefresh>();
47
48 final connecToNodeNative = moneroApi
49 .lookup<NativeFunction<connect_to_node>>('connect_to_node')
50 .asFunction<ConnectToNode>();
51
52 final setRefreshFromBlockHeightNative = moneroApi
58 - .lookup<NativeFunction<set_refresh_from_block_height>>(
59 - 'set_refresh_from_block_height')
53 + .lookup<NativeFunction<set_refresh_from_block_height>>('set_refresh_from_block_height')
54 .asFunction<SetRefreshFromBlockHeight>();
55
56 final setRecoveringFromSeedNative = moneroApi
63 - .lookup<NativeFunction<set_recovering_from_seed>>(
64 - 'set_recovering_from_seed')
57 + .lookup<NativeFunction<set_recovering_from_seed>>('set_recovering_from_seed')
58 .asFunction<SetRecoveringFromSeed>();
59
67 -final storeNative =
68 - moneroApi.lookup<NativeFunction<store_c>>('store').asFunction<Store>();
60 +final storeNative = moneroApi.lookup<NativeFunction<store_c>>('store').asFunction<Store>();
61
62 final setPasswordNative =
63 moneroApi.lookup<NativeFunction<set_password>>('set_password').asFunction<SetPassword>();
64
73 -final setListenerNative = moneroApi
74 - .lookup<NativeFunction<set_listener>>('set_listener')
75 - .asFunction<SetListener>();
65 +final setListenerNative =
66 + moneroApi.lookup<NativeFunction<set_listener>>('set_listener').asFunction<SetListener>();
67
68 final getSyncingHeightNative = moneroApi
69 .lookup<NativeFunction<get_syncing_height>>('get_syncing_height')
@@ -83,8 +74,7 @@ final isNeededToRefreshNative = moneroApi
74 .asFunction<IsNeededToRefresh>();
75
76 final isNewTransactionExistNative = moneroApi
86 - .lookup<NativeFunction<is_new_transaction_exist>>(
87 - 'is_new_transaction_exist')
77 + .lookup<NativeFunction<is_new_transaction_exist>>('is_new_transaction_exist')
78 .asFunction<IsNewTransactionExist>();
79
80 final getSecretViewKeyNative = moneroApi
@@ -107,9 +97,8 @@ final closeCurrentWalletNative = moneroApi
97 .lookup<NativeFunction<close_current_wallet>>('close_current_wallet')
98 .asFunction<CloseCurrentWallet>();
99
110 -final onStartupNative = moneroApi
111 - .lookup<NativeFunction<on_startup>>('on_startup')
112 - .asFunction<OnStartup>();
100 +final onStartupNative =
101 + moneroApi.lookup<NativeFunction<on_startup>>('on_startup').asFunction<OnStartup>();
102
103 final rescanBlockchainAsyncNative = moneroApi
104 .lookup<NativeFunction<rescan_blockchain>>('rescan_blockchain')
@@ -123,13 +112,19 @@ final setTrustedDaemonNative = moneroApi
112 .lookup<NativeFunction<set_trusted_daemon>>('set_trusted_daemon')
113 .asFunction<SetTrustedDaemon>();
114
126 -final trustedDaemonNative = moneroApi
127 - .lookup<NativeFunction<trusted_daemon>>('trusted_daemon')
128 - .asFunction<TrustedDaemon>();
115 +final trustedDaemonNative =
116 + moneroApi.lookup<NativeFunction<trusted_daemon>>('trusted_daemon').asFunction<TrustedDaemon>();
117 +
118 +final signMessageNative =
119 + moneroApi.lookup<NativeFunction<sign_message>>('sign_message').asFunction<SignMessage>();
120 +
121 +final getCacheAttributeNative = moneroApi
122 + .lookup<NativeFunction<get_cache_attribute>>('get_cache_attribute')
123 + .asFunction<GetCacheAttribute>();
124
130 -final signMessageNative = moneroApi
131 - .lookup<NativeFunction<sign_message>>('sign_message')
132 - .asFunction<SignMessage>();
125 +final setCacheAttributeNative = moneroApi
126 + .lookup<NativeFunction<set_cache_attribute>>('set_cache_attribute')
127 + .asFunction<SetCacheAttribute>();
128
129 int getSyncingHeight() => getSyncingHeightNative();
130
@@ -144,11 +139,9 @@ String getSeed() => convertUTF8ToString(pointer: getSeedNative());
139 String getAddress({int accountIndex = 0, int addressIndex = 0}) =>
140 convertUTF8ToString(pointer: getAddressNative(accountIndex, addressIndex));
141
147 -int getFullBalance({int accountIndex = 0}) =>
148 - getFullBalanceNative(accountIndex);
142 +int getFullBalance({int accountIndex = 0}) => getFullBalanceNative(accountIndex);
143
150 -int getUnlockedBalance({int accountIndex = 0}) =>
151 - getUnlockedBalanceNative(accountIndex);
144 +int getUnlockedBalance({int accountIndex = 0}) => getUnlockedBalanceNative(accountIndex);
145
146 int getCurrentHeight() => getCurrentHeightNative();
147
@@ -187,7 +180,7 @@ bool setupNodeSync(
180 passwordPointer,
181 _boolToInt(useSSL),
182 _boolToInt(isLightWallet),
190 - socksProxyAddressPointer,
183 + socksProxyAddressPointer,
184 errorMessagePointer) !=
185 0;
186
@@ -202,8 +195,7 @@ bool setupNodeSync(
195 }
196
197 if (!isSetupNode) {
205 - throw SetupWalletException(
206 - message: convertUTF8ToString(pointer: errorMessagePointer));
198 + throw SetupWalletException(message: convertUTF8ToString(pointer: errorMessagePointer));
199 }
200
201 return isSetupNode;
@@ -213,8 +205,7 @@ void startRefreshSync() => startRefreshNative();
205
206 Future<bool> connectToNode() async => connecToNodeNative() != 0;
207
216 -void setRefreshFromBlockHeight({required int height}) =>
217 - setRefreshFromBlockHeightNative(height);
208 +void setRefreshFromBlockHeight({required int height}) => setRefreshFromBlockHeightNative(height);
209
210 void setRecoveringFromSeed({required bool isRecovery}) =>
211 setRecoveringFromSeedNative(_boolToInt(isRecovery));
@@ -230,7 +221,7 @@ void setPasswordSync(String password) {
221 final errorMessagePointer = calloc<Utf8Box>();
222 final changed = setPasswordNative(passwordPointer, errorMessagePointer) != 0;
223 calloc.free(passwordPointer);
233 -
224 +
225 if (!changed) {
226 final message = errorMessagePointer.ref.getValue();
227 calloc.free(errorMessagePointer);
@@ -242,24 +233,19 @@ void setPasswordSync(String password) {
233
234 void closeCurrentWallet() => closeCurrentWalletNative();
235
245 -String getSecretViewKey() =>
246 - convertUTF8ToString(pointer: getSecretViewKeyNative());
236 +String getSecretViewKey() => convertUTF8ToString(pointer: getSecretViewKeyNative());
237
248 -String getPublicViewKey() =>
249 - convertUTF8ToString(pointer: getPublicViewKeyNative());
238 +String getPublicViewKey() => convertUTF8ToString(pointer: getPublicViewKeyNative());
239
251 -String getSecretSpendKey() =>
252 - convertUTF8ToString(pointer: getSecretSpendKeyNative());
240 +String getSecretSpendKey() => convertUTF8ToString(pointer: getSecretSpendKeyNative());
241
254 -String getPublicSpendKey() =>
255 - convertUTF8ToString(pointer: getPublicSpendKeyNative());
242 +String getPublicSpendKey() => convertUTF8ToString(pointer: getPublicSpendKeyNative());
243
244 class SyncListener {
258 - SyncListener(this.onNewBlock, this.onNewTransaction)
259 - : _cachedBlockchainHeight = 0,
260 - _lastKnownBlockHeight = 0,
261 - _initialSyncHeight = 0;
262 -
245 + SyncListener(this.onNewBlock, this.onNewTransaction)
246 + : _cachedBlockchainHeight = 0,
247 + _lastKnownBlockHeight = 0,
248 + _initialSyncHeight = 0;
249
250 void Function(int, int, double) onNewBlock;
251 void Function() onNewTransaction;
@@ -281,8 +267,7 @@ class SyncListener {
267 _cachedBlockchainHeight = 0;
268 _lastKnownBlockHeight = 0;
269 _initialSyncHeight = 0;
284 - _updateSyncInfoTimer ??=
285 - Timer.periodic(Duration(milliseconds: 1200), (_) async {
270 + _updateSyncInfoTimer ??= Timer.periodic(Duration(milliseconds: 1200), (_) async {
271 if (isNewTransactionExist()) {
272 onNewTransaction();
273 }
@@ -321,8 +306,8 @@ class SyncListener {
306 void stop() => _updateSyncInfoTimer?.cancel();
307 }
308
324 -SyncListener setListeners(void Function(int, int, double) onNewBlock,
325 - void Function() onNewTransaction) {
309 +SyncListener setListeners(
310 + void Function(int, int, double) onNewBlock, void Function() onNewTransaction) {
311 final listener = SyncListener(onNewBlock, onNewTransaction);
312 setListenerNative();
313 return listener;
@@ -364,7 +349,7 @@ Future<void> setupNode(
349 bool isLightWallet = false}) =>
350 compute<Map<String, Object?>, void>(_setupNodeSync, {
351 'address': address,
367 - 'login': login ,
352 + 'login': login,
353 'password': password,
354 'useSSL': useSSL,
355 'isLightWallet': isLightWallet,
@@ -397,3 +382,23 @@ String signMessage(String message, {String address = ""}) {
382
383 return signature;
384 }
385 +
386 +bool setCacheAttribute(String name, String value) {
387 + final namePointer = name.toNativeUtf8();
388 + final valuePointer = value.toNativeUtf8();
389 +
390 + final isSet = setCacheAttributeNative(namePointer, valuePointer);
391 + calloc.free(namePointer);
392 + calloc.free(valuePointer);
393 +
394 + return isSet == 1;
395 +}
396 +
397 +String getCacheAttribute(String name) {
398 + final namePointer = name.toNativeUtf8();
399 +
400 + final value = convertUTF8ToString(pointer: getCacheAttributeNative(namePointer));
401 + calloc.free(namePointer);
402 +
403 + return value;
404 +}