2023-04-14 04:39:08 +00:00
|
|
|
import 'package:cake_wallet/store/app_store.dart';
|
2023-05-24 21:00:54 +00:00
|
|
|
import 'package:cw_core/transaction_direction.dart';
|
|
|
|
import 'package:cw_core/transaction_info.dart';
|
2021-12-24 12:37:24 +00:00
|
|
|
import 'package:cw_core/wallet_type.dart';
|
2020-07-06 20:09:03 +00:00
|
|
|
import 'package:mobx/mobx.dart';
|
|
|
|
import 'package:cake_wallet/generated/i18n.dart';
|
2021-12-24 12:37:24 +00:00
|
|
|
import 'package:cw_core/wallet_base.dart';
|
2020-07-06 20:09:03 +00:00
|
|
|
import 'package:cake_wallet/src/screens/transaction_details/standart_list_item.dart';
|
2021-12-30 17:20:00 +00:00
|
|
|
import 'package:cake_wallet/monero/monero.dart';
|
2022-03-30 15:57:04 +00:00
|
|
|
import 'package:cake_wallet/haven/haven.dart';
|
2023-03-15 15:30:06 +00:00
|
|
|
import 'package:cw_monero/api/wallet.dart' as monero_wallet;
|
2020-07-06 20:09:03 +00:00
|
|
|
|
|
|
|
part 'wallet_keys_view_model.g.dart';
|
|
|
|
|
|
|
|
class WalletKeysViewModel = WalletKeysViewModelBase with _$WalletKeysViewModel;
|
|
|
|
|
|
|
|
abstract class WalletKeysViewModelBase with Store {
|
2023-04-14 04:39:08 +00:00
|
|
|
WalletKeysViewModelBase(this._appStore)
|
|
|
|
: title = _appStore.wallet!.type == WalletType.bitcoin ||
|
2023-04-28 02:33:19 +00:00
|
|
|
_appStore.wallet!.type == WalletType.litecoin ||
|
|
|
|
_appStore.wallet!.type == WalletType.ethereum
|
2022-06-23 13:36:50 +00:00
|
|
|
? S.current.wallet_seed
|
|
|
|
: S.current.wallet_keys,
|
2023-04-14 04:39:08 +00:00
|
|
|
_restoreHeight = _appStore.wallet!.walletInfo.restoreHeight,
|
2023-05-24 21:00:54 +00:00
|
|
|
_restoreHeightByTransactions = 0,
|
2022-06-23 13:36:50 +00:00
|
|
|
items = ObservableList<StandartListItem>() {
|
2023-04-14 04:39:08 +00:00
|
|
|
_populateItems();
|
|
|
|
|
|
|
|
reaction((_) => _appStore.wallet, (WalletBase? _wallet) {
|
|
|
|
_populateItems();
|
|
|
|
});
|
2023-05-24 21:00:54 +00:00
|
|
|
|
|
|
|
if (_appStore.wallet!.type == WalletType.monero || _appStore.wallet!.type == WalletType.haven) {
|
|
|
|
final accountTransactions = _getWalletTransactions(_appStore.wallet!);
|
|
|
|
if (accountTransactions.isNotEmpty) {
|
|
|
|
final incomingAccountTransactions =
|
|
|
|
accountTransactions.where((tx) => tx.direction == TransactionDirection.incoming);
|
|
|
|
if (incomingAccountTransactions.isNotEmpty) {
|
|
|
|
incomingAccountTransactions.toList().sort((a, b) => a.date.compareTo(b.date));
|
|
|
|
_restoreHeightByTransactions = _getRestoreHeightByTransactions(
|
|
|
|
_appStore.wallet!.type, incomingAccountTransactions.first.date);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-04-14 04:39:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
final ObservableList<StandartListItem> items;
|
|
|
|
|
|
|
|
final String title;
|
|
|
|
|
|
|
|
final AppStore _appStore;
|
|
|
|
|
|
|
|
final int _restoreHeight;
|
|
|
|
|
2023-05-24 21:00:54 +00:00
|
|
|
int _restoreHeightByTransactions;
|
|
|
|
|
2023-04-14 04:39:08 +00:00
|
|
|
void _populateItems() {
|
|
|
|
items.clear();
|
|
|
|
|
|
|
|
if (_appStore.wallet!.type == WalletType.monero) {
|
|
|
|
final keys = monero!.getKeys(_appStore.wallet!);
|
|
|
|
|
2021-12-30 17:20:00 +00:00
|
|
|
items.addAll([
|
2022-10-12 17:09:57 +00:00
|
|
|
if (keys['publicSpendKey'] != null)
|
|
|
|
StandartListItem(title: S.current.spend_key_public, value: keys['publicSpendKey']!),
|
|
|
|
if (keys['privateSpendKey'] != null)
|
|
|
|
StandartListItem(title: S.current.spend_key_private, value: keys['privateSpendKey']!),
|
|
|
|
if (keys['publicViewKey'] != null)
|
|
|
|
StandartListItem(title: S.current.view_key_public, value: keys['publicViewKey']!),
|
|
|
|
if (keys['privateViewKey'] != null)
|
|
|
|
StandartListItem(title: S.current.view_key_private, value: keys['privateViewKey']!),
|
2023-04-14 04:39:08 +00:00
|
|
|
StandartListItem(title: S.current.wallet_seed, value: _appStore.wallet!.seed),
|
2022-03-30 15:57:04 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2023-04-14 04:39:08 +00:00
|
|
|
if (_appStore.wallet!.type == WalletType.haven) {
|
|
|
|
final keys = haven!.getKeys(_appStore.wallet!);
|
|
|
|
|
2022-03-30 15:57:04 +00:00
|
|
|
items.addAll([
|
2022-10-12 17:09:57 +00:00
|
|
|
if (keys['publicSpendKey'] != null)
|
|
|
|
StandartListItem(title: S.current.spend_key_public, value: keys['publicSpendKey']!),
|
|
|
|
if (keys['privateSpendKey'] != null)
|
|
|
|
StandartListItem(title: S.current.spend_key_private, value: keys['privateSpendKey']!),
|
|
|
|
if (keys['publicViewKey'] != null)
|
|
|
|
StandartListItem(title: S.current.view_key_public, value: keys['publicViewKey']!),
|
|
|
|
if (keys['privateViewKey'] != null)
|
|
|
|
StandartListItem(title: S.current.view_key_private, value: keys['privateViewKey']!),
|
2023-04-14 04:39:08 +00:00
|
|
|
StandartListItem(title: S.current.wallet_seed, value: _appStore.wallet!.seed),
|
2021-12-30 17:20:00 +00:00
|
|
|
]);
|
2020-07-06 20:09:03 +00:00
|
|
|
}
|
|
|
|
|
2023-04-14 04:39:08 +00:00
|
|
|
if (_appStore.wallet!.type == WalletType.bitcoin ||
|
2023-04-28 02:33:19 +00:00
|
|
|
_appStore.wallet!.type == WalletType.litecoin ||
|
|
|
|
_appStore.wallet!.type == WalletType.ethereum) {
|
2020-07-06 20:09:03 +00:00
|
|
|
items.addAll([
|
2023-04-14 04:39:08 +00:00
|
|
|
StandartListItem(title: S.current.wallet_seed, value: _appStore.wallet!.seed),
|
2020-07-06 20:09:03 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-24 21:00:54 +00:00
|
|
|
Future<int?> _currentHeight() async {
|
2023-04-14 04:39:08 +00:00
|
|
|
if (_appStore.wallet!.type == WalletType.haven) {
|
2023-03-15 15:30:06 +00:00
|
|
|
return await haven!.getCurrentHeight();
|
|
|
|
}
|
2023-04-14 04:39:08 +00:00
|
|
|
if (_appStore.wallet!.type == WalletType.monero) {
|
2023-03-15 15:30:06 +00:00
|
|
|
return monero_wallet.getCurrentHeight();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-04-21 18:36:47 +00:00
|
|
|
String get _scheme {
|
2023-04-14 04:39:08 +00:00
|
|
|
switch (_appStore.wallet!.type) {
|
2023-03-15 15:30:06 +00:00
|
|
|
case WalletType.monero:
|
2023-04-21 18:36:47 +00:00
|
|
|
return 'monero-wallet';
|
2023-03-15 15:30:06 +00:00
|
|
|
case WalletType.bitcoin:
|
2023-04-21 18:36:47 +00:00
|
|
|
return 'bitcoin-wallet';
|
2023-03-15 15:30:06 +00:00
|
|
|
case WalletType.litecoin:
|
2023-04-21 18:36:47 +00:00
|
|
|
return 'litecoin-wallet';
|
2023-03-15 15:30:06 +00:00
|
|
|
case WalletType.haven:
|
2023-04-21 18:36:47 +00:00
|
|
|
return 'haven-wallet';
|
2023-03-15 15:30:06 +00:00
|
|
|
default:
|
2023-04-14 04:39:08 +00:00
|
|
|
throw Exception('Unexpected wallet type: ${_appStore.wallet!.toString()}');
|
2023-03-15 15:30:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<String?> get restoreHeight async {
|
2023-05-24 21:00:54 +00:00
|
|
|
if (_restoreHeightByTransactions != 0)
|
|
|
|
return getRoundedRestoreHeight(_restoreHeightByTransactions);
|
|
|
|
if (_restoreHeight != 0) return _restoreHeight.toString();
|
|
|
|
|
|
|
|
final currentHeight = await _currentHeight();
|
|
|
|
if (currentHeight == null) return null;
|
|
|
|
|
|
|
|
return getRoundedRestoreHeight(currentHeight);
|
2023-03-15 15:30:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<Map<String, String>> get _queryParams async {
|
|
|
|
final restoreHeightResult = await restoreHeight;
|
|
|
|
return {
|
2023-04-14 04:39:08 +00:00
|
|
|
'seed': _appStore.wallet!.seed,
|
2023-03-15 15:30:06 +00:00
|
|
|
if (restoreHeightResult != null) ...{'height': restoreHeightResult}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-05-24 21:00:54 +00:00
|
|
|
Future<Uri> get url async => Uri(
|
|
|
|
scheme: _scheme,
|
|
|
|
queryParameters: await _queryParams,
|
|
|
|
);
|
|
|
|
|
|
|
|
List<TransactionInfo> _getWalletTransactions(WalletBase wallet) {
|
|
|
|
if (wallet.type == WalletType.monero) {
|
|
|
|
return monero!.getTransactionHistory(wallet).transactions.values.toList();
|
|
|
|
} else if (wallet.type == WalletType.haven) {
|
|
|
|
return haven!.getTransactionHistory(wallet).transactions.values.toList();
|
|
|
|
}
|
|
|
|
return [];
|
2023-03-15 15:30:06 +00:00
|
|
|
}
|
2023-05-24 21:00:54 +00:00
|
|
|
|
|
|
|
int _getRestoreHeightByTransactions(WalletType type, DateTime date) {
|
|
|
|
if (type == WalletType.monero) {
|
|
|
|
return monero!.getHeigthByDate(date: date);
|
|
|
|
} else if (type == WalletType.haven) {
|
|
|
|
return haven!.getHeightByDate(date: date);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
String getRoundedRestoreHeight(int height) => ((height / 1000).floor() * 1000).toString();
|
2020-07-06 20:09:03 +00:00
|
|
|
}
|