CakeWallet/lib/view_model/wallet_keys_view_model.dart
Omar Hatem a34cf24897
Remove haven (#2085)
* decred: Add decred. (#1322)

* multi: Add initial decred screens. (#1165)

Use a mock libwallet for now.

* cw_decred: add libdcrwallet dependency and link library for android, ios and macos (#1240)

* change cw_decred from package to plugin

* add libdcrwallet dependency and link library for android, ios and macos

* remove spvwallet, make some libdcrwallet fns async, light refactor

* libdcrwallet: use json payload returns

* use specific libwallet commit hash

* decred: fix Rename wallet.

---------

Co-authored-by: JoeGruff <joegruffins@gmail.com>

* decred: Add sync.

* decred: Add send transaction.

* decred: Fix fee estimation.

* decred: List transactions.

* decred: Add rescan.

* decred: Sign message.

* decred: Add new addr and addrs.

* decred: Add change wallet pass.

* decred: Add restore from seed.

* decred: Add watching only wallets.

* decred: Enable mainnet.

* decred: Allow using blank node address.

This allows a persistent peer to be unset, falling back to decred
seeders.

* decred: Rescan from wallet birthday.

* add and update macos build scripts, update build readme, gitignore macos project.pbxproj

Signed-off-by: Philemon Ukane <ukanephilemon@gmail.com>

* multi: hide decred rescan page if it's not ready

-  move hasRescan method to WalletBase and implement for decred

Signed-off-by: Philemon Ukane <ukanephilemon@gmail.com>

* cw_decred: fix bug where decred wallets are not loaded after app restart

Signed-off-by: Philemon Ukane <ukanephilemon@gmail.com>

* add buy and sell for decred via onramp

Signed-off-by: Philemon Ukane <ukanephilemon@gmail.com>

* bug-fix: account for other send outputs that are part of the same tx

Signed-off-by: Philemon Ukane <ukanephilemon@gmail.com>

* decred: Return address with no peers.

* decred: Update pubspec.

* decred: Add verify message.

* upgrade hive_generator dep in cw_decred

* decred: Clean up code.

---------

Signed-off-by: Philemon Ukane <ukanephilemon@gmail.com>
Co-authored-by: Wisdom Arerosuoghene <wisdom.arerosuoghene@gmail.com>
Co-authored-by: Philemon Ukane <ukanephilemon@gmail.com>

* fix extracted addresses not used
fix conflicts with main

* remove print [skip ci]

* minor formatting

* fix initial migration version

* add build decred script to workflow

* install go before build decred
fix switch cases

* trial 2 to fix decred build

* re-install go

* revert build script change

* refactor/clean nodes functions

* Fix address book issue
Fix send ALL (to be continued with the fees point)

* Fix transactions display issues
Add missing file

* Fix unconfirmed balance not displayed
Change Wallet order
Minor cleanup

* Fix workflow

* Fix workflow

* Fix workflow

* test

* hardcode path for now

* fix + cleanup decred build script to work on mac and linux

* Update decred build script

* Run actions on pull requests, extract commit message

* run after checkout

* add safe directory

* Get commit message from base.sha instead of last commit

* base -> head

* Do not merge main branch into pr

* [skip slack] [run tests] clone by sha

* Proper name for decred library in the build script

* Throw an error when ANDROID_HOME or ANDROID_NDK_VERSION is missing

* Fix conflicts with main

* minor code enhancement

* decred: Add used address history.  (#1941)

* decred: Update pubspec.

* decred testnet

* decred: Add used address history.

* decred: Remove default node list.

* populate transaction history before sync begins

* decred: Add some awaits.

* decred: Fix send all.

* decred: Add clang export to build script.

* decred: Update logo colors.

* cleanup cw_decred.dart

* make decred wallet addresses selectable in receive page

* decred: Always set default addr when used.

* decred: Add back default node list.

* decred: Allow creating addresses manually.

---------

Co-authored-by: Wisdom Arerosuoghene <wisdom.arerosuoghene@gmail.com>
Co-authored-by: Omar Hatem <omarh.ismail1@gmail.com>

* minor fixes and cleanup

* minor fix, feel free to test now

* - Fix transaction details
- Fix Nodes
- Add processing sync status

* Add decred info card

* push missing file

* Add missing text for decred info card

* minor: change docs link [skip ci]

* decred: Update derivation info. (#2013)

* decred: Update derivation info.

* decred: Allow unsynced unused addresses.

* decred: Update dcrwallet dep to 4.3.0.

* Merge main and fix conflicts

* Merge main and fix conflicts

* decred: Fix background sync panic. (#2080)

* decred: Run libwallet in isolate. (#2077)

* decred: Fix contact save inquiry. (#2083)

Also fix tx time and the fee shown on pending transactions.

* Disable send button in view only decred wallets

* - Fix frozen coins
- Add URI support
- Fix fees in tx details
- Handle empty coins send
- Handle wallets in address book

* Merge main

* remove print [skip ci]

* Fix restore from QR

* minor improvement for QR restore

* Remove Haven Wallet

* Remove haven scripts

* minor fixes [skip ci]

* decred: Get slip44 addrs before sync completes. (#2092)

* - Fix loading wallet more than one time
- Fix minor UI issue

* fix merge issue

* fix merge issue

---------

Signed-off-by: Philemon Ukane <ukanephilemon@gmail.com>
Co-authored-by: JoeGruffins <34998433+JoeGruffins@users.noreply.github.com>
Co-authored-by: Wisdom Arerosuoghene <wisdom.arerosuoghene@gmail.com>
Co-authored-by: Philemon Ukane <ukanephilemon@gmail.com>
Co-authored-by: Czarek Nakamoto <cyjan@mrcyjanek.net>
2025-03-21 04:52:05 +02:00

320 lines
11 KiB
Dart

import 'package:cake_wallet/generated/i18n.dart';
import 'package:cake_wallet/monero/monero.dart';
import 'package:cake_wallet/src/screens/transaction_details/standart_list_item.dart';
import 'package:cake_wallet/store/app_store.dart';
import 'package:cake_wallet/wownero/wownero.dart';
import 'package:cake_wallet/zano/zano.dart';
import 'package:cw_core/transaction_direction.dart';
import 'package:cw_core/transaction_info.dart';
import 'package:cw_core/wallet_base.dart';
import 'package:cw_core/wallet_type.dart';
import 'package:cw_monero/monero_wallet.dart';
import 'package:flutter/foundation.dart';
import 'package:mobx/mobx.dart';
import 'package:cake_wallet/decred/decred.dart';
import 'package:polyseed/polyseed.dart';
part 'wallet_keys_view_model.g.dart';
class WalletKeysViewModel = WalletKeysViewModelBase with _$WalletKeysViewModel;
abstract class WalletKeysViewModelBase with Store {
WalletKeysViewModelBase(this._appStore)
: title = S.current.wallet_keys,
_wallet = _appStore.wallet!,
_walletName = _appStore.wallet!.type.name,
_restoreHeight = _appStore.wallet!.walletInfo.restoreHeight,
_restoreHeightByTransactions = 0,
items = ObservableList<StandartListItem>() {
_populateKeysItems();
reaction((_) => _appStore.wallet, (WalletBase? _wallet) {
_populateKeysItems();
});
if (_wallet.type == WalletType.monero ||
_wallet.type == WalletType.haven ||
_wallet.type == WalletType.wownero) {
final accountTransactions = _getWalletTransactions(_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(_wallet.type, incomingAccountTransactions.first.date);
}
}
}
}
final ObservableList<StandartListItem> items;
final String title;
final WalletBase _wallet;
final String _walletName;
final AppStore _appStore;
final int _restoreHeight;
int _restoreHeightByTransactions;
AppStore get appStore => _appStore;
String get seed => _wallet.seed != null ? _wallet.seed! : '';
bool get isLegacySeedOnly =>
(_wallet.type == WalletType.monero || _wallet.type == WalletType.wownero) &&
_wallet.seed != null &&
!Polyseed.isValidSeed(_wallet.seed!);
String get legacySeed {
if ((_wallet.type == WalletType.monero || _wallet.type == WalletType.wownero) &&
_wallet.seed != null &&
Polyseed.isValidSeed(_wallet.seed!)) {
final langName = PolyseedLang.getByPhrase(_wallet.seed!).nameEnglish;
if (_wallet.type == WalletType.monero) {
return (_wallet as MoneroWalletBase).seedLegacy(langName);
} else if (_wallet.type == WalletType.wownero) {
return wownero!.getLegacySeed(_wallet, langName);
}
}
return '';
}
String get legacyRestoreHeight {
if (_wallet.type == WalletType.monero) {
return monero!.getRestoreHeight(_wallet)?.toString() ?? '';
}
if (_wallet.type == WalletType.wownero) {
return wownero!.getRestoreHeight(_wallet)?.toString() ?? '';
}
return '';
}
@observable
bool obscurePassphrase = true;
String get passphrase {
return _wallet.passphrase ?? '';
}
/// The Regex split the words based on any whitespace character.
///
/// Either standard ASCII space (U+0020) or the full-width space character (U+3000) used by the Japanese.
List<String> get seedSplit => seed.isNotEmpty ? seed.split(RegExp(r'\s+')) : [];
List<String> get legacySeedSplit => legacySeed.isNotEmpty ? legacySeed.split(RegExp(r'\s+')) : [];
void _populateKeysItems() {
items.clear();
Map<String, String>? keys;
switch (_wallet.type) {
case WalletType.monero:
keys = monero!.getKeys(_wallet);
break;
case WalletType.wownero:
keys = wownero!.getKeys(_wallet);
break;
case WalletType.zano:
keys = zano!.getKeys(_wallet);
break;
case WalletType.ethereum:
case WalletType.polygon:
case WalletType.solana:
case WalletType.tron:
items.addAll([
if (_wallet.privateKey != null)
StandartListItem(
key: ValueKey('${_walletName}_wallet_private_key_item_key'),
title: S.current.private_key,
value: _wallet.privateKey!,
),
]);
break;
case WalletType.nano:
case WalletType.banano:
// we always have the hex version of the seed and private key:
items.addAll([
if (_wallet.hexSeed != null)
StandartListItem(
key: ValueKey('${_walletName}_wallet_hex_seed_key'),
title: S.current.seed_hex_form,
value: _wallet.hexSeed!,
),
if (_wallet.privateKey != null)
StandartListItem(
key: ValueKey('${_walletName}_wallet_private_key_item_key'),
title: S.current.private_key,
value: _wallet.privateKey!,
),
]);
break;
case WalletType.decred:
final pubkey = decred!.pubkey(_appStore.wallet!);
items.addAll([
StandartListItem(title: S.current.view_key_public, value: pubkey),
]);
break;
case WalletType.bitcoin:
case WalletType.litecoin:
case WalletType.bitcoinCash:
case WalletType.none:
case WalletType.haven:
// final keys = bitcoin!.getWalletKeys(_appStore.wallet!);
//
// items.addAll([
// if (keys['wif'] != null)
// StandartListItem(title: "WIF", value: keys['wif']!),
// if (keys['privateKey'] != null)
// StandartListItem(title: S.current.private_key, value: keys['privateKey']!),
// if (keys['publicKey'] != null)
// StandartListItem(title: S.current.public_key, value: keys['publicKey']!),
// ]);
break;
}
if (keys != null) {
items.addAll([
if (keys['primaryAddress'] != null)
StandartListItem(
key: ValueKey('${_walletName}_wallet_primary_address_item_key'),
title: S.current.primary_address,
value: keys['primaryAddress']!),
if (keys['publicSpendKey'] != null)
StandartListItem(
key: ValueKey('${_walletName}_wallet_public_spend_key_item_key'),
title: S.current.spend_key_public,
value: keys['publicSpendKey']!,
),
if (keys['privateSpendKey'] != null)
StandartListItem(
key: ValueKey('${_walletName}_wallet_private_spend_key_item_key'),
title: S.current.spend_key_private,
value: keys['privateSpendKey']!,
),
if (keys['publicViewKey'] != null)
StandartListItem(
key: ValueKey('${_walletName}_wallet_public_view_key_item_key'),
title: S.current.view_key_public,
value: keys['publicViewKey']!,
),
if (keys['privateViewKey'] != null)
StandartListItem(
key: ValueKey('${_walletName}_wallet_private_view_key_item_key'),
title: S.current.view_key_private,
value: keys['privateViewKey']!,
),
]);
}
}
Future<int?> _currentHeight() async {
if (_wallet.type == WalletType.monero) {
return await monero!.getCurrentHeight();
}
if (_wallet.type == WalletType.wownero) {
return await wownero!.getCurrentHeight();
}
return null;
}
String get _scheme {
switch (_wallet.type) {
case WalletType.monero:
return 'monero-wallet';
case WalletType.bitcoin:
return 'bitcoin-wallet';
case WalletType.litecoin:
return 'litecoin-wallet';
case WalletType.haven:
return 'haven-wallet';
case WalletType.ethereum:
return 'ethereum-wallet';
case WalletType.bitcoinCash:
return 'bitcoincash-wallet';
case WalletType.nano:
return 'nano-wallet';
case WalletType.banano:
return 'banano-wallet';
case WalletType.polygon:
return 'polygon-wallet';
case WalletType.solana:
return 'solana-wallet';
case WalletType.tron:
return 'tron-wallet';
case WalletType.wownero:
return 'wownero-wallet';
case WalletType.zano:
return 'zano-wallet';
case WalletType.decred:
return 'decred-wallet';
default:
throw Exception('Unexpected wallet type: ${_wallet.type.toString()}');
}
}
Future<String?> get restoreHeight async {
if (_wallet.type == WalletType.monero) {
return monero!.getRestoreHeight(_wallet)?.toString();
}
if (_wallet.type == WalletType.wownero) {
return wownero!.getRestoreHeight(_wallet)?.toString();
}
if (_restoreHeightByTransactions != 0)
return getRoundedRestoreHeight(_restoreHeightByTransactions);
if (_restoreHeight != 0) return _restoreHeight.toString();
final currentHeight = await _currentHeight();
if (currentHeight == null) return null;
return getRoundedRestoreHeight(currentHeight);
}
Future<Map<String, String>> get _queryParams async {
final restoreHeightResult = await restoreHeight;
return {
if (_wallet.seed != null) 'seed': _wallet.seed!,
if (_wallet.seed == null && _wallet.hexSeed != null) 'hexSeed': _wallet.hexSeed!,
if (_wallet.seed == null && _wallet.privateKey != null) 'private_key': _wallet.privateKey!,
if (restoreHeightResult != null) ...{'height': restoreHeightResult},
if (_wallet.passphrase != null) 'passphrase': _wallet.passphrase!
};
}
Future<Map<String, String>> get _queryParamsForLegacy async {
final restoreHeightResult = await restoreHeight;
return {
if (legacySeed.isNotEmpty) 'seed': legacySeed,
if (restoreHeightResult != null) ...{'height': restoreHeightResult},
if ((_wallet.passphrase ?? '') != '') 'passphrase': _wallet.passphrase!
};
}
Future<Uri> getUrl(bool isLegacySeed) async => Uri(
scheme: _scheme,
queryParameters: isLegacySeed ? await _queryParamsForLegacy : await _queryParams,
);
List<TransactionInfo> _getWalletTransactions(WalletBase wallet) {
if (wallet.type == WalletType.monero) {
return monero!.getTransactionHistory(wallet).transactions.values.toList();
} else if (wallet.type == WalletType.wownero) {
return wownero!.getTransactionHistory(wallet).transactions.values.toList();
}
return [];
}
int _getRestoreHeightByTransactions(WalletType type, DateTime date) {
if (type == WalletType.monero) {
return monero!.getHeightByDate(date: date);
} else if (type == WalletType.wownero) {
return wownero!.getHeightByDate(date: date);
}
return 0;
}
String getRoundedRestoreHeight(int height) => ((height / 1000).floor() * 1000).toString();
}