From e03f0ce4d3cf3e072a4ed4ef4e72655421cf065a Mon Sep 17 00:00:00 2001 From: OleksandrSobol Date: Thu, 8 Jul 2021 17:52:48 +0300 Subject: [PATCH] CAKE-343 | added wallet_addresses.dart, monero_wallet_addresses.dart, electrum_wallet_addresses.dart, wallet_addresses_credentials.dart, monero_wallet_addresses_credentials.dart and electrum_wallet_addresses_credentials.dart to the app; applied ElectrumWalletAddresses to electrum_wallet.dart and MoneroWalletAddresses to monero_wallet.dart; fixed contact_list_view_model.dart, other view models and services --- lib/bitcoin/bitcoin_wallet_service.dart | 1 - lib/bitcoin/electrum_wallet.dart | 16 +++++++- lib/bitcoin/electrum_wallet_addresses.dart | 32 ++++++++++++++++ ...electrum_wallet_addresses_credentials.dart | 7 ++++ lib/bitcoin/litecoin_wallet_service.dart | 1 - lib/core/wallet_base.dart | 10 ----- lib/entities/wallet_addresses.dart | 30 +++++++++++++++ .../wallet_addresses_credentials.dart | 1 + lib/monero/monero_wallet.dart | 37 +++++++++--------- lib/monero/monero_wallet_addresses.dart | 38 +++++++++++++++++++ .../monero_wallet_addresses_credentials.dart | 9 +++++ lib/monero/monero_wallet_service.dart | 1 - .../contact_list/contact_list_view_model.dart | 21 +++++----- ...ero_account_edit_or_create_view_model.dart | 3 +- ...let_address_edit_or_create_view_model.dart | 2 - lib/view_model/wallet_creation_vm.dart | 1 - 16 files changed, 159 insertions(+), 51 deletions(-) create mode 100644 lib/bitcoin/electrum_wallet_addresses.dart create mode 100644 lib/bitcoin/electrum_wallet_addresses_credentials.dart create mode 100644 lib/entities/wallet_addresses.dart create mode 100644 lib/entities/wallet_addresses_credentials.dart create mode 100644 lib/monero/monero_wallet_addresses.dart create mode 100644 lib/monero/monero_wallet_addresses_credentials.dart diff --git a/lib/bitcoin/bitcoin_wallet_service.dart b/lib/bitcoin/bitcoin_wallet_service.dart index 2cd3310fd..aefe0fadf 100644 --- a/lib/bitcoin/bitcoin_wallet_service.dart +++ b/lib/bitcoin/bitcoin_wallet_service.dart @@ -44,7 +44,6 @@ class BitcoinWalletService extends WalletService< final wallet = await BitcoinWalletBase.open( password: password, name: name, walletInfo: walletInfo); await wallet.init(); - await wallet.updateAddressesInfo(); return wallet; } diff --git a/lib/bitcoin/electrum_wallet.dart b/lib/bitcoin/electrum_wallet.dart index bb0ee3c3e..d0b1c9a6c 100644 --- a/lib/bitcoin/electrum_wallet.dart +++ b/lib/bitcoin/electrum_wallet.dart @@ -1,5 +1,7 @@ import 'dart:async'; import 'dart:convert'; +import 'package:cake_wallet/bitcoin/electrum_wallet_addresses.dart'; +import 'package:cake_wallet/bitcoin/electrum_wallet_addresses_credentials.dart'; import 'package:mobx/mobx.dart'; import 'package:rxdart/subjects.dart'; import 'package:flutter/foundation.dart'; @@ -63,6 +65,8 @@ abstract class ElectrumWalletBase extends WalletBase @@ -93,6 +97,9 @@ abstract class ElectrumWalletBase extends WalletBase hd.base58; + ElectrumWalletAddresses _walletAddressInBox; + ElectrumWalletAddressesCredentials _walletAddressInBoxCredentials; + @override String get seed => mnemonic; @@ -113,6 +120,7 @@ abstract class ElectrumWalletBase extends WalletBase @@ -466,4 +473,9 @@ abstract class ElectrumWalletBase extends WalletBase _updateWalletAddressInBox() async { + _walletAddressInBoxCredentials.address = address; + await _walletAddressInBox.update(_walletAddressInBoxCredentials); + } } diff --git a/lib/bitcoin/electrum_wallet_addresses.dart b/lib/bitcoin/electrum_wallet_addresses.dart new file mode 100644 index 000000000..ac4895ae8 --- /dev/null +++ b/lib/bitcoin/electrum_wallet_addresses.dart @@ -0,0 +1,32 @@ +import 'package:cake_wallet/bitcoin/electrum_wallet_addresses_credentials.dart'; +import 'package:cake_wallet/entities/wallet_addresses.dart'; +import 'package:cake_wallet/entities/wallet_addresses_credentials.dart'; +import 'package:cake_wallet/entities/wallet_info.dart'; + +class ElectrumWalletAddresses extends WalletAddresses { + ElectrumWalletAddresses(WalletInfo walletInfo) : super(walletInfo); + + @override + Future update(WalletAddressesCredentials credentials) async { + try { + if (credentials == null) { + return; + } + + final _credentials = credentials as ElectrumWalletAddressesCredentials; + final _address = _credentials.address; + final _label = ''; + + if (_address == null || _address.isEmpty) { + return; + } + + addresses.clear(); + addresses[_address] = _label; + + await save(); + } catch (e) { + print(e.toString()); + } + } +} \ No newline at end of file diff --git a/lib/bitcoin/electrum_wallet_addresses_credentials.dart b/lib/bitcoin/electrum_wallet_addresses_credentials.dart new file mode 100644 index 000000000..30a41f8ff --- /dev/null +++ b/lib/bitcoin/electrum_wallet_addresses_credentials.dart @@ -0,0 +1,7 @@ +import 'package:cake_wallet/entities/wallet_addresses_credentials.dart'; + +class ElectrumWalletAddressesCredentials extends WalletAddressesCredentials { + ElectrumWalletAddressesCredentials(); + + String address; +} \ No newline at end of file diff --git a/lib/bitcoin/litecoin_wallet_service.dart b/lib/bitcoin/litecoin_wallet_service.dart index 071baf4af..053fd785f 100644 --- a/lib/bitcoin/litecoin_wallet_service.dart +++ b/lib/bitcoin/litecoin_wallet_service.dart @@ -45,7 +45,6 @@ class LitecoinWalletService extends WalletService< final wallet = await LitecoinWalletBase.open( password: password, name: name, walletInfo: walletInfo); await wallet.init(); - await wallet.updateAddressesInfo(); return wallet; } diff --git a/lib/core/wallet_base.dart b/lib/core/wallet_base.dart index 9e7b89e8d..f6ed64142 100644 --- a/lib/core/wallet_base.dart +++ b/lib/core/wallet_base.dart @@ -6,7 +6,6 @@ import 'package:cake_wallet/entities/wallet_info.dart'; import 'package:cake_wallet/core/pending_transaction.dart'; import 'package:cake_wallet/core/transaction_history.dart'; import 'package:cake_wallet/entities/currency_for_wallet_type.dart'; -import 'package:cake_wallet/entities/monero_transaction_priority.dart'; import 'package:cake_wallet/entities/crypto_currency.dart'; import 'package:cake_wallet/entities/sync_status.dart'; import 'package:cake_wallet/entities/node.dart'; @@ -66,13 +65,4 @@ abstract class WalletBase< Future rescan({int height}); void close(); - - Future updateAddressesInfo() async { - try { - walletInfo.address = address; - await walletInfo.save(); - } catch (e) { - print(e.toString()); - } - } } diff --git a/lib/entities/wallet_addresses.dart b/lib/entities/wallet_addresses.dart new file mode 100644 index 000000000..81b4fdea7 --- /dev/null +++ b/lib/entities/wallet_addresses.dart @@ -0,0 +1,30 @@ +import 'package:cake_wallet/entities/wallet_addresses_credentials.dart'; +import 'package:cake_wallet/entities/wallet_info.dart'; + +abstract class WalletAddresses { + WalletAddresses(this.walletInfo) { + addresses = walletInfo?.addresses ?? {}; + } + + final WalletInfo walletInfo; + + Map addresses; + + Future update(WalletAddressesCredentials credentials); + + Future save() async { + try { + if (walletInfo == null) { + return; + } + + walletInfo.addresses = addresses; + + if (walletInfo.isInBox) { + await walletInfo.save(); + } + } catch (e) { + print(e.toString()); + } + } +} \ No newline at end of file diff --git a/lib/entities/wallet_addresses_credentials.dart b/lib/entities/wallet_addresses_credentials.dart new file mode 100644 index 000000000..fe3d2c927 --- /dev/null +++ b/lib/entities/wallet_addresses_credentials.dart @@ -0,0 +1 @@ +abstract class WalletAddressesCredentials {} \ No newline at end of file diff --git a/lib/monero/monero_wallet.dart b/lib/monero/monero_wallet.dart index c9ea9e03e..77cd2f53f 100644 --- a/lib/monero/monero_wallet.dart +++ b/lib/monero/monero_wallet.dart @@ -3,6 +3,8 @@ import 'package:cake_wallet/entities/transaction_priority.dart'; import 'package:cake_wallet/monero/monero_amount_format.dart'; import 'package:cake_wallet/monero/monero_transaction_creation_exception.dart'; import 'package:cake_wallet/monero/monero_transaction_info.dart'; +import 'package:cake_wallet/monero/monero_wallet_addresses.dart'; +import 'package:cake_wallet/monero/monero_wallet_addresses_credentials.dart'; import 'package:flutter/foundation.dart'; import 'package:mobx/mobx.dart'; import 'package:cw_monero/transaction_history.dart' @@ -47,6 +49,8 @@ abstract class MoneroWalletBase extends WalletBase account, (Account account) { balance = MoneroBalance( fullBalance: monero_wallet.getFullBalance(accountIndex: account.id), @@ -99,6 +103,8 @@ abstract class MoneroWalletBase extends WalletBase init() async { accountList.update(); @@ -110,6 +116,7 @@ abstract class MoneroWalletBase extends WalletBase save() async { + await _updateWalletAllAddressesInBox(); + final now = DateTime.now().millisecondsSinceEpoch; if (now - _lastSaveTimestamp < Duration(seconds: 10).inMilliseconds) { @@ -446,25 +455,13 @@ abstract class MoneroWalletBase extends WalletBase updateAddressesInfo() async { - final Map _addresses = {}; - final _subaddressList = MoneroSubaddressList(); - - accountList.accounts.forEach((account) { - _subaddressList.update(accountIndex: account.id); - _subaddressList.subaddresses.forEach((subaddress) { - _addresses[subaddress.address] = subaddress.label; - }); - }); - - try { - walletInfo.addresses = _addresses; - walletInfo.address = address; - - await walletInfo.save(); - } catch (e) { - print(e.toString()); - } + Future _updateWalletAllAddressesInBox() async { + _walletAddressesCredentials + ..accountList = accountList + ..subaddressList = subaddressList; + await _walletAllAddresses.update(_walletAddressesCredentials); + subaddressList.update(accountIndex: account.id ?? 0); + subaddress = subaddressList.subaddresses.first; + address = subaddress.address; } } diff --git a/lib/monero/monero_wallet_addresses.dart b/lib/monero/monero_wallet_addresses.dart new file mode 100644 index 000000000..55680e1c9 --- /dev/null +++ b/lib/monero/monero_wallet_addresses.dart @@ -0,0 +1,38 @@ +import 'package:cake_wallet/entities/wallet_addresses.dart'; +import 'package:cake_wallet/entities/wallet_addresses_credentials.dart'; +import 'package:cake_wallet/entities/wallet_info.dart'; +import 'monero_wallet_addresses_credentials.dart'; + +class MoneroWalletAddresses extends WalletAddresses { + MoneroWalletAddresses(WalletInfo walletInfo) : super(walletInfo); + + @override + Future update(WalletAddressesCredentials credentials) async { + try { + if (credentials == null) { + return; + } + + final _credentials = credentials as MoneroWalletAddressesCredentials; + final _accountList = _credentials.accountList; + final _subaddressList = _credentials.subaddressList; + + if (_accountList == null || _subaddressList == null) { + return; + } + + addresses.clear(); + + _accountList.accounts.forEach((account) { + _subaddressList.update(accountIndex: account.id); + _subaddressList.subaddresses.forEach((subaddress) { + addresses[subaddress.address] = subaddress.label; + }); + }); + + await save(); + } catch (e) { + print(e.toString()); + } + } +} \ No newline at end of file diff --git a/lib/monero/monero_wallet_addresses_credentials.dart b/lib/monero/monero_wallet_addresses_credentials.dart new file mode 100644 index 000000000..0808cb127 --- /dev/null +++ b/lib/monero/monero_wallet_addresses_credentials.dart @@ -0,0 +1,9 @@ +import 'package:cake_wallet/entities/wallet_addresses_credentials.dart'; +import 'package:cake_wallet/monero/monero_account_list.dart'; +import 'package:cake_wallet/monero/monero_subaddress_list.dart'; + +class MoneroWalletAddressesCredentials extends WalletAddressesCredentials { + + MoneroAccountList accountList; + MoneroSubaddressList subaddressList; +} \ No newline at end of file diff --git a/lib/monero/monero_wallet_service.dart b/lib/monero/monero_wallet_service.dart index 17b02dcbb..7795b8700 100644 --- a/lib/monero/monero_wallet_service.dart +++ b/lib/monero/monero_wallet_service.dart @@ -126,7 +126,6 @@ class MoneroWalletService extends WalletService< } await wallet.init(); - await wallet.updateAddressesInfo(); return wallet; } catch (e) { diff --git a/lib/view_model/contact_list/contact_list_view_model.dart b/lib/view_model/contact_list/contact_list_view_model.dart index b5143cffa..1a8bbc021 100644 --- a/lib/view_model/contact_list/contact_list_view_model.dart +++ b/lib/view_model/contact_list/contact_list_view_model.dart @@ -18,18 +18,17 @@ abstract class ContactListViewModelBase with Store { : contacts = ObservableList(), walletContacts = [] { walletInfoSource.values.forEach((info) { - if (info.address?.isNotEmpty ?? false) { - if (info.addresses != null) { - info.addresses?.forEach((address, label) { - walletContacts.add(WalletContact( - address, - info.name + ' ($label)', - walletTypeToCryptoCurrency(info.type))); - }); - } else { - walletContacts.add(WalletContact(info.address, info.name, + if (info.addresses?.isNotEmpty ?? false) { + info.addresses?.forEach((address, label) { + final name = label.isNotEmpty + ? info.name + ' ($label)' + : info.name; + + walletContacts.add(WalletContact( + address, + name, walletTypeToCryptoCurrency(info.type))); - } + }); } }); diff --git a/lib/view_model/monero_account_list/monero_account_edit_or_create_view_model.dart b/lib/view_model/monero_account_list/monero_account_edit_or_create_view_model.dart index cf999e414..a86fa2773 100644 --- a/lib/view_model/monero_account_list/monero_account_edit_or_create_view_model.dart +++ b/lib/view_model/monero_account_list/monero_account_edit_or_create_view_model.dart @@ -42,8 +42,7 @@ abstract class MoneroAccountEditOrCreateViewModelBase with Store { await _moneroAccountList.addAccount(label: label); } - await _wallet.updateAddressesInfo(); - + await _wallet.save(); state = ExecutedSuccessfullyState(); } catch (e) { state = FailureState(e.toString()); diff --git a/lib/view_model/wallet_address_list/wallet_address_edit_or_create_view_model.dart b/lib/view_model/wallet_address_list/wallet_address_edit_or_create_view_model.dart index 6b6873cd5..b018c542d 100644 --- a/lib/view_model/wallet_address_list/wallet_address_edit_or_create_view_model.dart +++ b/lib/view_model/wallet_address_list/wallet_address_edit_or_create_view_model.dart @@ -54,8 +54,6 @@ abstract class WalletAddressEditOrCreateViewModelBase with Store { await _createNew(); } - await _wallet.updateAddressesInfo(); - state = AddressSavedSuccessfully(); } catch (e) { state = AddressEditOrCreateStateFailure(error: e.toString()); diff --git a/lib/view_model/wallet_creation_vm.dart b/lib/view_model/wallet_creation_vm.dart index 54cad29c9..58ee24087 100644 --- a/lib/view_model/wallet_creation_vm.dart +++ b/lib/view_model/wallet_creation_vm.dart @@ -54,7 +54,6 @@ abstract class WalletCreationVMBase with Store { await _walletInfoSource.add(walletInfo); _appStore.changeCurrentWallet(wallet); _appStore.authenticationStore.allowed(); - await wallet.updateAddressesInfo(); state = ExecutedSuccessfullyState(); } catch (e) { state = FailureState(e.toString());