mirror of
https://github.com/cake-tech/cake_wallet.git
synced 2024-12-22 19:49:22 +00:00
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
This commit is contained in:
parent
c72245d904
commit
e03f0ce4d3
16 changed files with 159 additions and 51 deletions
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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<ElectrumBalance,
|
|||
ElectrumTransactionHistory(walletInfo: walletInfo, password: password);
|
||||
_unspent = [];
|
||||
_scripthashesUpdateSubject = {};
|
||||
_walletAddressInBox = ElectrumWalletAddresses(walletInfo);
|
||||
_walletAddressInBoxCredentials = ElectrumWalletAddressesCredentials();
|
||||
}
|
||||
|
||||
static int estimatedTransactionSize(int inputsCount, int outputsCounts) =>
|
||||
|
@ -93,6 +97,9 @@ abstract class ElectrumWalletBase extends WalletBase<ElectrumBalance,
|
|||
|
||||
String get xpub => hd.base58;
|
||||
|
||||
ElectrumWalletAddresses _walletAddressInBox;
|
||||
ElectrumWalletAddressesCredentials _walletAddressInBoxCredentials;
|
||||
|
||||
@override
|
||||
String get seed => mnemonic;
|
||||
|
||||
|
@ -113,6 +120,7 @@ abstract class ElectrumWalletBase extends WalletBase<ElectrumBalance,
|
|||
await generateAddresses();
|
||||
address = addresses[_accountIndex].address;
|
||||
await transactionHistory.init();
|
||||
await _updateWalletAddressInBox();
|
||||
}
|
||||
|
||||
@action
|
||||
|
@ -125,8 +133,6 @@ abstract class ElectrumWalletBase extends WalletBase<ElectrumBalance,
|
|||
|
||||
address = addresses[_accountIndex].address;
|
||||
|
||||
await updateAddressesInfo();
|
||||
|
||||
await save();
|
||||
}
|
||||
|
||||
|
@ -364,6 +370,7 @@ abstract class ElectrumWalletBase extends WalletBase<ElectrumBalance,
|
|||
final path = await makePath();
|
||||
await write(path: path, password: _password, data: toJSON());
|
||||
await transactionHistory.save();
|
||||
await _updateWalletAddressInBox();
|
||||
}
|
||||
|
||||
bitcoin.ECPair keyPairFor({@required int index}) =>
|
||||
|
@ -466,4 +473,9 @@ abstract class ElectrumWalletBase extends WalletBase<ElectrumBalance,
|
|||
balance = await _fetchBalances();
|
||||
await save();
|
||||
}
|
||||
|
||||
Future<void> _updateWalletAddressInBox() async {
|
||||
_walletAddressInBoxCredentials.address = address;
|
||||
await _walletAddressInBox.update(_walletAddressInBoxCredentials);
|
||||
}
|
||||
}
|
||||
|
|
32
lib/bitcoin/electrum_wallet_addresses.dart
Normal file
32
lib/bitcoin/electrum_wallet_addresses.dart
Normal file
|
@ -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<void> 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());
|
||||
}
|
||||
}
|
||||
}
|
7
lib/bitcoin/electrum_wallet_addresses_credentials.dart
Normal file
7
lib/bitcoin/electrum_wallet_addresses_credentials.dart
Normal file
|
@ -0,0 +1,7 @@
|
|||
import 'package:cake_wallet/entities/wallet_addresses_credentials.dart';
|
||||
|
||||
class ElectrumWalletAddressesCredentials extends WalletAddressesCredentials {
|
||||
ElectrumWalletAddressesCredentials();
|
||||
|
||||
String address;
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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<void> rescan({int height});
|
||||
|
||||
void close();
|
||||
|
||||
Future<void> updateAddressesInfo() async {
|
||||
try {
|
||||
walletInfo.address = address;
|
||||
await walletInfo.save();
|
||||
} catch (e) {
|
||||
print(e.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
30
lib/entities/wallet_addresses.dart
Normal file
30
lib/entities/wallet_addresses.dart
Normal file
|
@ -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<String, String> addresses;
|
||||
|
||||
Future<void> update(WalletAddressesCredentials credentials);
|
||||
|
||||
Future<void> save() async {
|
||||
try {
|
||||
if (walletInfo == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
walletInfo.addresses = addresses;
|
||||
|
||||
if (walletInfo.isInBox) {
|
||||
await walletInfo.save();
|
||||
}
|
||||
} catch (e) {
|
||||
print(e.toString());
|
||||
}
|
||||
}
|
||||
}
|
1
lib/entities/wallet_addresses_credentials.dart
Normal file
1
lib/entities/wallet_addresses_credentials.dart
Normal file
|
@ -0,0 +1 @@
|
|||
abstract class WalletAddressesCredentials {}
|
|
@ -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<MoneroBalance,
|
|||
_isSavingAfterSync = false;
|
||||
_isSavingAfterNewTransaction = false;
|
||||
_isTransactionUpdating = false;
|
||||
_walletAllAddresses = MoneroWalletAddresses(walletInfo);
|
||||
_walletAddressesCredentials = MoneroWalletAddressesCredentials();
|
||||
_onAccountChangeReaction = reaction((_) => account, (Account account) {
|
||||
balance = MoneroBalance(
|
||||
fullBalance: monero_wallet.getFullBalance(accountIndex: account.id),
|
||||
|
@ -99,6 +103,8 @@ abstract class MoneroWalletBase extends WalletBase<MoneroBalance,
|
|||
bool _isSavingAfterNewTransaction;
|
||||
bool _isTransactionUpdating;
|
||||
int _lastSaveTimestamp;
|
||||
MoneroWalletAddresses _walletAllAddresses;
|
||||
MoneroWalletAddressesCredentials _walletAddressesCredentials;
|
||||
|
||||
Future<void> init() async {
|
||||
accountList.update();
|
||||
|
@ -110,6 +116,7 @@ abstract class MoneroWalletBase extends WalletBase<MoneroBalance,
|
|||
unlockedBalance:
|
||||
monero_wallet.getUnlockedBalance(accountIndex: account.id));
|
||||
address = subaddress.address;
|
||||
await _updateWalletAllAddressesInBox();
|
||||
_setListeners();
|
||||
await updateTransactions();
|
||||
|
||||
|
@ -238,6 +245,8 @@ abstract class MoneroWalletBase extends WalletBase<MoneroBalance,
|
|||
|
||||
@override
|
||||
Future<void> 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<MoneroBalance,
|
|||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> updateAddressesInfo() async {
|
||||
final Map<String, String> _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<void> _updateWalletAllAddressesInBox() async {
|
||||
_walletAddressesCredentials
|
||||
..accountList = accountList
|
||||
..subaddressList = subaddressList;
|
||||
await _walletAllAddresses.update(_walletAddressesCredentials);
|
||||
subaddressList.update(accountIndex: account.id ?? 0);
|
||||
subaddress = subaddressList.subaddresses.first;
|
||||
address = subaddress.address;
|
||||
}
|
||||
}
|
||||
|
|
38
lib/monero/monero_wallet_addresses.dart
Normal file
38
lib/monero/monero_wallet_addresses.dart
Normal file
|
@ -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<void> 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());
|
||||
}
|
||||
}
|
||||
}
|
9
lib/monero/monero_wallet_addresses_credentials.dart
Normal file
9
lib/monero/monero_wallet_addresses_credentials.dart
Normal file
|
@ -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;
|
||||
}
|
|
@ -126,7 +126,6 @@ class MoneroWalletService extends WalletService<
|
|||
}
|
||||
|
||||
await wallet.init();
|
||||
await wallet.updateAddressesInfo();
|
||||
|
||||
return wallet;
|
||||
} catch (e) {
|
||||
|
|
|
@ -18,18 +18,17 @@ abstract class ContactListViewModelBase with Store {
|
|||
: contacts = ObservableList<ContactRecord>(),
|
||||
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)));
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
|
|
|
@ -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());
|
||||
|
|
|
@ -54,8 +54,6 @@ abstract class WalletAddressEditOrCreateViewModelBase with Store {
|
|||
await _createNew();
|
||||
}
|
||||
|
||||
await _wallet.updateAddressesInfo();
|
||||
|
||||
state = AddressSavedSuccessfully();
|
||||
} catch (e) {
|
||||
state = AddressEditOrCreateStateFailure(error: e.toString());
|
||||
|
|
|
@ -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());
|
||||
|
|
Loading…
Reference in a new issue