creating and restoring a wallet

This commit is contained in:
Serhii 2023-08-14 13:36:25 +03:00
parent ec1d565658
commit afcfab9796
22 changed files with 332 additions and 323 deletions

View file

@ -0,0 +1,3 @@
-
uri: bitcoincash.stackwallet.com:50002
is_default: true

View file

@ -2,7 +2,9 @@ import 'dart:async';
import 'dart:convert'; import 'dart:convert';
import 'dart:io'; import 'dart:io';
import 'dart:math'; import 'dart:math';
import 'package:bitcoin_flutter/bitcoin_flutter.dart';
import 'package:cw_core/unspent_coins_info.dart'; import 'package:cw_core/unspent_coins_info.dart';
import 'package:cw_core/wallet_type.dart';
import 'package:hive/hive.dart'; import 'package:hive/hive.dart';
import 'package:cw_bitcoin/electrum_wallet_addresses.dart'; import 'package:cw_bitcoin/electrum_wallet_addresses.dart';
import 'package:mobx/mobx.dart'; import 'package:mobx/mobx.dart';
@ -34,13 +36,15 @@ import 'package:cw_bitcoin/electrum.dart';
import 'package:hex/hex.dart'; import 'package:hex/hex.dart';
import 'package:cw_core/crypto_currency.dart'; import 'package:cw_core/crypto_currency.dart';
import 'package:collection/collection.dart'; import 'package:collection/collection.dart';
import 'package:bip32/bip32.dart';
part 'electrum_wallet.g.dart'; part 'electrum_wallet.g.dart';
class ElectrumWallet = ElectrumWalletBase with _$ElectrumWallet; class ElectrumWallet = ElectrumWalletBase with _$ElectrumWallet;
abstract class ElectrumWalletBase extends WalletBase<ElectrumBalance, abstract class ElectrumWalletBase
ElectrumTransactionHistory, ElectrumTransactionInfo> with Store { extends WalletBase<ElectrumBalance, ElectrumTransactionHistory, ElectrumTransactionInfo>
with Store {
ElectrumWalletBase( ElectrumWalletBase(
{required String password, {required String password,
required WalletInfo walletInfo, required WalletInfo walletInfo,
@ -52,27 +56,43 @@ abstract class ElectrumWalletBase extends WalletBase<ElectrumBalance,
ElectrumClient? electrumClient, ElectrumClient? electrumClient,
ElectrumBalance? initialBalance, ElectrumBalance? initialBalance,
CryptoCurrency? currency}) CryptoCurrency? currency})
: hd = bitcoin.HDWallet.fromSeed(seedBytes, network: networkType) : hd = currency == CryptoCurrency.bch
.derivePath("m/0'/0"), ? bitcoinCashHDWallet(seedBytes)
: bitcoin.HDWallet.fromSeed(seedBytes, network: networkType).derivePath("m/0'/0"),
syncStatus = NotConnectedSyncStatus(), syncStatus = NotConnectedSyncStatus(),
_password = password, _password = password,
_feeRates = <int>[], _feeRates = <int>[],
_isTransactionUpdating = false, _isTransactionUpdating = false,
unspentCoins = [], unspentCoins = [],
_scripthashesUpdateSubject = {}, _scripthashesUpdateSubject = {},
balance = ObservableMap<CryptoCurrency, ElectrumBalance>.of( balance = ObservableMap<CryptoCurrency, ElectrumBalance>.of(currency != null
currency != null ? {
? {currency: initialBalance ?? const ElectrumBalance(confirmed: 0, unconfirmed: 0, currency:
frozen: 0)} initialBalance ?? const ElectrumBalance(confirmed: 0, unconfirmed: 0, frozen: 0)
}
: {}), : {}),
this.unspentCoinsInfo = unspentCoinsInfo, this.unspentCoinsInfo = unspentCoinsInfo,
super(walletInfo) { super(walletInfo) {
this.electrumClient = electrumClient ?? ElectrumClient(); this.electrumClient = electrumClient ?? ElectrumClient();
this.walletInfo = walletInfo; this.walletInfo = walletInfo;
transactionHistory = transactionHistory = ElectrumTransactionHistory(walletInfo: walletInfo, password: password);
ElectrumTransactionHistory(walletInfo: walletInfo, password: password);
} }
static bitcoin.NetworkType bitcoinCashNetworkType = bitcoin.NetworkType(
messagePrefix: '\x18Bitcoin Signed Message:\n',
bech32: 'bc',
bip32: bitcoin.Bip32Type(
public: 0x0488b21e,
private: 0x0488ade4,
),
pubKeyHash: 0x00,
scriptHash: 0x05,
wif: 0x80);
static HDWallet bitcoinCashHDWallet(Uint8List seedBytes) =>
bitcoin.HDWallet.fromSeed(seedBytes, network: bitcoinCashNetworkType)
.derivePath("m/44'/145'/0'/0/0");
static int estimatedTransactionSize(int inputsCount, int outputsCounts) => static int estimatedTransactionSize(int inputsCount, int outputsCounts) =>
inputsCount * 146 + outputsCounts * 33 + 8; inputsCount * 146 + outputsCounts * 33 + 8;
@ -110,8 +130,8 @@ abstract class ElectrumWalletBase extends WalletBase<ElectrumBalance,
bitcoin.NetworkType networkType; bitcoin.NetworkType networkType;
@override @override
BitcoinWalletKeys get keys => BitcoinWalletKeys( BitcoinWalletKeys get keys =>
wif: hd.wif!, privateKey: hd.privKey!, publicKey: hd.pubKey!); BitcoinWalletKeys(wif: hd.wif!, privateKey: hd.privKey!, publicKey: hd.pubKey!);
String _password; String _password;
List<BitcoinUnspent> unspentCoins; List<BitcoinUnspent> unspentCoins;
@ -132,15 +152,17 @@ abstract class ElectrumWalletBase extends WalletBase<ElectrumBalance,
Future<void> startSync() async { Future<void> startSync() async {
try { try {
syncStatus = AttemptingSyncStatus(); syncStatus = AttemptingSyncStatus();
if (walletInfo.type != WalletType.bitcoinCash) { //TODO: BCH: remove this check when supported
await walletAddresses.discoverAddresses(); await walletAddresses.discoverAddresses();
}
await updateTransactions(); await updateTransactions();
_subscribeForUpdates(); _subscribeForUpdates();
await updateUnspent(); await updateUnspent();
await updateBalance(); await updateBalance();
_feeRates = await electrumClient.feeRates(); _feeRates = await electrumClient.feeRates();
Timer.periodic(const Duration(minutes: 1), Timer.periodic(
(timer) async => _feeRates = await electrumClient.feeRates()); const Duration(minutes: 1), (timer) async => _feeRates = await electrumClient.feeRates());
syncStatus = SyncedSyncStatus(); syncStatus = SyncedSyncStatus();
} catch (e, stacktrace) { } catch (e, stacktrace) {
@ -169,8 +191,7 @@ abstract class ElectrumWalletBase extends WalletBase<ElectrumBalance,
} }
@override @override
Future<PendingBitcoinTransaction> createTransaction( Future<PendingBitcoinTransaction> createTransaction(Object credentials) async {
Object credentials) async {
const minAmount = 546; const minAmount = 546;
final transactionCredentials = credentials as BitcoinTransactionCredentials; final transactionCredentials = credentials as BitcoinTransactionCredentials;
final inputs = <BitcoinUnspent>[]; final inputs = <BitcoinUnspent>[];
@ -204,13 +225,11 @@ abstract class ElectrumWalletBase extends WalletBase<ElectrumBalance,
var fee = 0; var fee = 0;
if (hasMultiDestination) { if (hasMultiDestination) {
if (outputs.any((item) => item.sendAll if (outputs.any((item) => item.sendAll || item.formattedCryptoAmount! <= 0)) {
|| item.formattedCryptoAmount! <= 0)) {
throw BitcoinTransactionWrongBalanceException(currency); throw BitcoinTransactionWrongBalanceException(currency);
} }
credentialsAmount = outputs.fold(0, (acc, value) => credentialsAmount = outputs.fold(0, (acc, value) => acc + value.formattedCryptoAmount!);
acc + value.formattedCryptoAmount!);
if (allAmount - credentialsAmount < minAmount) { if (allAmount - credentialsAmount < minAmount) {
throw BitcoinTransactionWrongBalanceException(currency); throw BitcoinTransactionWrongBalanceException(currency);
@ -227,9 +246,7 @@ abstract class ElectrumWalletBase extends WalletBase<ElectrumBalance,
} }
} else { } else {
final output = outputs.first; final output = outputs.first;
credentialsAmount = !output.sendAll credentialsAmount = !output.sendAll ? output.formattedCryptoAmount! : 0;
? output.formattedCryptoAmount!
: 0;
if (credentialsAmount > allAmount) { if (credentialsAmount > allAmount) {
throw BitcoinTransactionWrongBalanceException(currency); throw BitcoinTransactionWrongBalanceException(currency);
@ -303,19 +320,12 @@ abstract class ElectrumWalletBase extends WalletBase<ElectrumBalance,
}); });
outputs.forEach((item) { outputs.forEach((item) {
final outputAmount = hasMultiDestination final outputAmount = hasMultiDestination ? item.formattedCryptoAmount : amount;
? item.formattedCryptoAmount final outputAddress = item.isParsedAddress ? item.extractedAddress! : item.address;
: amount; txb.addOutput(addressToOutputScript(outputAddress, networkType), outputAmount!);
final outputAddress = item.isParsedAddress
? item.extractedAddress!
: item.address;
txb.addOutput(
addressToOutputScript(outputAddress, networkType),
outputAmount!);
}); });
final estimatedSize = final estimatedSize = estimatedTransactionSize(inputs.length, outputs.length + 1);
estimatedTransactionSize(inputs.length, outputs.length + 1);
var feeAmount = 0; var feeAmount = 0;
if (transactionCredentials.feeRate != null) { if (transactionCredentials.feeRate != null) {
@ -364,34 +374,29 @@ abstract class ElectrumWalletBase extends WalletBase<ElectrumBalance,
} }
return 0; return 0;
} catch(_) { } catch (_) {
return 0; return 0;
} }
} }
int feeAmountForPriority(BitcoinTransactionPriority priority, int inputsCount, int feeAmountForPriority(
int outputsCount) => BitcoinTransactionPriority priority, int inputsCount, int outputsCount) =>
feeRate(priority) * estimatedTransactionSize(inputsCount, outputsCount); feeRate(priority) * estimatedTransactionSize(inputsCount, outputsCount);
int feeAmountWithFeeRate(int feeRate, int inputsCount, int feeAmountWithFeeRate(int feeRate, int inputsCount, int outputsCount) =>
int outputsCount) =>
feeRate * estimatedTransactionSize(inputsCount, outputsCount); feeRate * estimatedTransactionSize(inputsCount, outputsCount);
@override @override
int calculateEstimatedFee(TransactionPriority? priority, int? amount, int calculateEstimatedFee(TransactionPriority? priority, int? amount, {int? outputsCount}) {
{int? outputsCount}) {
if (priority is BitcoinTransactionPriority) { if (priority is BitcoinTransactionPriority) {
return calculateEstimatedFeeWithFeeRate( return calculateEstimatedFeeWithFeeRate(feeRate(priority), amount,
feeRate(priority),
amount,
outputsCount: outputsCount); outputsCount: outputsCount);
} }
return 0; return 0;
} }
int calculateEstimatedFeeWithFeeRate(int feeRate, int? amount, int calculateEstimatedFeeWithFeeRate(int feeRate, int? amount, {int? outputsCount}) {
{int? outputsCount}) {
int inputsCount = 0; int inputsCount = 0;
if (amount != null) { if (amount != null) {
@ -420,8 +425,7 @@ abstract class ElectrumWalletBase extends WalletBase<ElectrumBalance,
// If send all, then we have no change value // If send all, then we have no change value
final _outputsCount = outputsCount ?? (amount != null ? 2 : 1); final _outputsCount = outputsCount ?? (amount != null ? 2 : 1);
return feeAmountWithFeeRate( return feeAmountWithFeeRate(feeRate, inputsCount, _outputsCount);
feeRate, inputsCount, _outputsCount);
} }
@override @override
@ -436,8 +440,7 @@ abstract class ElectrumWalletBase extends WalletBase<ElectrumBalance,
final currentWalletPath = await pathForWallet(name: walletInfo.name, type: type); final currentWalletPath = await pathForWallet(name: walletInfo.name, type: type);
final currentWalletFile = File(currentWalletPath); final currentWalletFile = File(currentWalletPath);
final currentDirPath = final currentDirPath = await pathForWalletDir(name: walletInfo.name, type: type);
await pathForWalletDir(name: walletInfo.name, type: type);
final currentTransactionsFile = File('$currentDirPath/$transactionsHistoryFileName'); final currentTransactionsFile = File('$currentDirPath/$transactionsHistoryFileName');
// Copies current wallet files into new wallet name's dir and files // Copies current wallet files into new wallet name's dir and files
@ -474,18 +477,15 @@ abstract class ElectrumWalletBase extends WalletBase<ElectrumBalance,
} catch (_) {} } catch (_) {}
} }
Future<String> makePath() async => Future<String> makePath() async => pathForWallet(name: walletInfo.name, type: walletInfo.type);
pathForWallet(name: walletInfo.name, type: walletInfo.type);
Future<void> updateUnspent() async { Future<void> updateUnspent() async {
final unspent = await Future.wait(walletAddresses final unspent = await Future.wait(walletAddresses.addresses.map((address) => electrumClient
.addresses.map((address) => electrumClient
.getListUnspentWithAddress(address.address, networkType) .getListUnspentWithAddress(address.address, networkType)
.then((unspent) => unspent .then((unspent) => unspent.map((unspent) {
.map((unspent) {
try { try {
return BitcoinUnspent.fromJSON(address, unspent); return BitcoinUnspent.fromJSON(address, unspent);
} catch(_) { } catch (_) {
return null; return null;
} }
}).whereNotNull()))); }).whereNotNull())));
@ -498,8 +498,8 @@ abstract class ElectrumWalletBase extends WalletBase<ElectrumBalance,
if (unspentCoins.isNotEmpty) { if (unspentCoins.isNotEmpty) {
unspentCoins.forEach((coin) { unspentCoins.forEach((coin) {
final coinInfoList = unspentCoinsInfo.values.where((element) => final coinInfoList = unspentCoinsInfo.values
element.walletId.contains(id) && element.hash.contains(coin.hash)); .where((element) => element.walletId.contains(id) && element.hash.contains(coin.hash));
if (coinInfoList.isNotEmpty) { if (coinInfoList.isNotEmpty) {
final coinInfo = coinInfoList.first; final coinInfo = coinInfoList.first;
@ -534,8 +534,8 @@ abstract class ElectrumWalletBase extends WalletBase<ElectrumBalance,
Future<void> _refreshUnspentCoinsInfo() async { Future<void> _refreshUnspentCoinsInfo() async {
try { try {
final List<dynamic> keys = <dynamic>[]; final List<dynamic> keys = <dynamic>[];
final currentWalletUnspentCoins = unspentCoinsInfo.values final currentWalletUnspentCoins =
.where((element) => element.walletId.contains(id)); unspentCoinsInfo.values.where((element) => element.walletId.contains(id));
if (currentWalletUnspentCoins.isNotEmpty) { if (currentWalletUnspentCoins.isNotEmpty) {
currentWalletUnspentCoins.forEach((element) { currentWalletUnspentCoins.forEach((element) {
@ -571,11 +571,7 @@ abstract class ElectrumWalletBase extends WalletBase<ElectrumBalance,
ins.add(tx); ins.add(tx);
} }
return ElectrumTransactionBundle( return ElectrumTransactionBundle(original, ins: ins, time: time, confirmations: confirmations);
original,
ins: ins,
time: time,
confirmations: confirmations);
} }
Future<ElectrumTransactionInfo?> fetchTransactionInfo( Future<ElectrumTransactionInfo?> fetchTransactionInfo(
@ -583,13 +579,9 @@ abstract class ElectrumWalletBase extends WalletBase<ElectrumBalance,
try { try {
final tx = await getTransactionExpanded(hash: hash, height: height); final tx = await getTransactionExpanded(hash: hash, height: height);
final addresses = walletAddresses.addresses.map((addr) => addr.address).toSet(); final addresses = walletAddresses.addresses.map((addr) => addr.address).toSet();
return ElectrumTransactionInfo.fromElectrumBundle( return ElectrumTransactionInfo.fromElectrumBundle(tx, walletInfo.type, networkType,
tx, addresses: addresses, height: height);
walletInfo.type, } catch (_) {
networkType,
addresses: addresses,
height: height);
} catch(_) {
return null; return null;
} }
} }
@ -602,10 +594,8 @@ abstract class ElectrumWalletBase extends WalletBase<ElectrumBalance,
final sh = scriptHash(addressRecord.address, networkType: networkType); final sh = scriptHash(addressRecord.address, networkType: networkType);
addressHashes[sh] = addressRecord; addressHashes[sh] = addressRecord;
}); });
final histories = final histories = addressHashes.keys.map((scriptHash) =>
addressHashes.keys.map((scriptHash) => electrumClient electrumClient.getHistory(scriptHash).then((history) => {scriptHash: history}));
.getHistory(scriptHash)
.then((history) => {scriptHash: history}));
final historyResults = await Future.wait(histories); final historyResults = await Future.wait(histories);
historyResults.forEach((history) { historyResults.forEach((history) {
history.entries.forEach((historyItem) { history.entries.forEach((historyItem) {
@ -616,19 +606,16 @@ abstract class ElectrumWalletBase extends WalletBase<ElectrumBalance,
} }
}); });
}); });
final historiesWithDetails = await Future.wait( final historiesWithDetails = await Future.wait(normalizedHistories.map((transaction) {
normalizedHistories
.map((transaction) {
try { try {
return fetchTransactionInfo( return fetchTransactionInfo(
hash: transaction['tx_hash'] as String, hash: transaction['tx_hash'] as String, height: transaction['height'] as int);
height: transaction['height'] as int); } catch (_) {
} catch(_) {
return Future.value(null); return Future.value(null);
} }
})); }));
return historiesWithDetails.fold<Map<String, ElectrumTransactionInfo>>( return historiesWithDetails
<String, ElectrumTransactionInfo>{}, (acc, tx) { .fold<Map<String, ElectrumTransactionInfo>>(<String, ElectrumTransactionInfo>{}, (acc, tx) {
if (tx == null) { if (tx == null) {
return acc; return acc;
} }
@ -680,8 +667,11 @@ abstract class ElectrumWalletBase extends WalletBase<ElectrumBalance,
Future<ElectrumBalance> _fetchBalances() async { Future<ElectrumBalance> _fetchBalances() async {
final addresses = walletAddresses.addresses.toList(); final addresses = walletAddresses.addresses.toList();
final balanceFutures = <Future<Map<String, dynamic>>>[]; final balanceFutures = <Future<Map<String, dynamic>>>[];
var counter = addresses.length;
for (var i = 0; i < addresses.length; i++) { if (walletInfo.type == WalletType.bitcoinCash) counter = 1; //TODO: BCH: remove this check when supported
for (var i = 0; i < counter; i++) {
final addressRecord = addresses[i]; final addressRecord = addresses[i];
final sh = scriptHash(addressRecord.address, networkType: networkType); final sh = scriptHash(addressRecord.address, networkType: networkType);
final balanceFuture = electrumClient.getBalance(sh); final balanceFuture = electrumClient.getBalance(sh);
@ -691,8 +681,10 @@ abstract class ElectrumWalletBase extends WalletBase<ElectrumBalance,
var totalFrozen = 0; var totalFrozen = 0;
unspentCoinsInfo.values.forEach((info) { unspentCoinsInfo.values.forEach((info) {
unspentCoins.forEach((element) { unspentCoins.forEach((element) {
if (element.hash == info.hash && info.isFrozen && element.address.address == info.address if (element.hash == info.hash &&
&& element.value == info.value) { info.isFrozen &&
element.address.address == info.address &&
element.value == info.value) {
totalFrozen += element.value; totalFrozen += element.value;
} }
}); });
@ -715,8 +707,8 @@ abstract class ElectrumWalletBase extends WalletBase<ElectrumBalance,
} }
} }
return ElectrumBalance(confirmed: totalConfirmed, unconfirmed: totalUnconfirmed, return ElectrumBalance(
frozen: totalFrozen); confirmed: totalConfirmed, unconfirmed: totalUnconfirmed, frozen: totalFrozen);
} }
Future<void> updateBalance() async { Future<void> updateBalance() async {
@ -727,9 +719,7 @@ abstract class ElectrumWalletBase extends WalletBase<ElectrumBalance,
String getChangeAddress() { String getChangeAddress() {
const minCountOfHiddenAddresses = 5; const minCountOfHiddenAddresses = 5;
final random = Random(); final random = Random();
var addresses = walletAddresses.addresses var addresses = walletAddresses.addresses.where((addr) => addr.isHidden).toList();
.where((addr) => addr.isHidden)
.toList();
if (addresses.length < minCountOfHiddenAddresses) { if (addresses.length < minCountOfHiddenAddresses) {
addresses = walletAddresses.addresses.toList(); addresses = walletAddresses.addresses.toList();

View file

@ -78,6 +78,8 @@ class Node extends HiveObject with Keyable {
return Uri.http(uriRaw, ''); return Uri.http(uriRaw, '');
case WalletType.ethereum: case WalletType.ethereum:
return Uri.https(uriRaw, ''); return Uri.https(uriRaw, '');
case WalletType.bitcoinCash:
return createUriFromElectrumAddress(uriRaw);
default: default:
throw Exception('Unexpected type ${type.toString()} for Node uri'); throw Exception('Unexpected type ${type.toString()} for Node uri');
} }
@ -129,6 +131,8 @@ class Node extends HiveObject with Keyable {
return requestMoneroNode(); return requestMoneroNode();
case WalletType.ethereum: case WalletType.ethereum:
return requestElectrumServer(); return requestElectrumServer();
case WalletType.bitcoinCash:
return requestElectrumServer();
default: default:
return false; return false;
} }

View file

@ -68,7 +68,7 @@ WalletType deserializeFromInt(int raw) {
return WalletType.haven; return WalletType.haven;
case 4: case 4:
return WalletType.ethereum; return WalletType.ethereum;
case 4: case 5:
return WalletType.bitcoinCash; return WalletType.bitcoinCash;
default: default:
throw Exception('Unexpected token: $raw for WalletType deserializeFromInt'); throw Exception('Unexpected token: $raw for WalletType deserializeFromInt');

View file

@ -672,5 +672,5 @@ packages:
source: hosted source: hosted
version: "3.1.1" version: "3.1.1"
sdks: sdks:
dart: ">=2.19.0 <3.0.0" dart: ">=2.19.0 <4.0.0"
flutter: ">=3.0.0" flutter: ">=3.0.0"

View file

@ -20,149 +20,17 @@ class CWBitcoinCash extends BitcoinCash {
}) => }) =>
BitcoinCashNewWalletCredentials(name: name, walletInfo: walletInfo); BitcoinCashNewWalletCredentials(name: name, walletInfo: walletInfo);
// @override @override
// TransactionPriority getMediumTransactionPriority() => BitcoinTransactionPriority.medium; WalletCredentials createBitcoinCashRestoreWalletFromSeedCredentials(
// {required String name, required String mnemonic, required String password}) =>
// @override BitcoinCashRestoreWalletFromSeedCredentials(
// WalletCredentials createBitcoinRestoreWalletFromSeedCredentials({ name: name, mnemonic: mnemonic, password: password);
// required String name,
// required String mnemonic, @override
// required String password}) TransactionPriority deserializeBitcoinCashTransactionPriority(int raw) =>
// => BitcoinRestoreWalletFromSeedCredentials(name: name, mnemonic: mnemonic, password: password); BitcoinCashTransactionPriority.deserialize(raw: raw);
//
// @override @override
// WalletCredentials createBitcoinRestoreWalletFromWIFCredentials({ TransactionPriority getDefaultTransactionPriority() =>
// required String name, throw UnimplementedError('getDefaultTransactionPriority');
// required String password,
// required String wif,
// WalletInfo? walletInfo})
// => BitcoinRestoreWalletFromWIFCredentials(name: name, password: password, wif: wif, walletInfo: walletInfo);
//
// @override
// WalletCredentials createBitcoinNewWalletCredentials({
// required String name,
// WalletInfo? walletInfo})
// => BitcoinNewWalletCredentials(name: name, walletInfo: walletInfo);
//
//
// @override
// Map<String, String> getWalletKeys(Object wallet) {
// final bitcoinWallet = wallet as ElectrumWallet;
// final keys = bitcoinWallet.keys;
//
// return <String, String>{
// 'wif': keys.wif,
// 'privateKey': keys.privateKey,
// 'publicKey': keys.publicKey
// };
// }
//
// @override
// List<TransactionPriority> getTransactionPriorities()
// => BitcoinTransactionPriority.all;
//
// List<TransactionPriority> getLitecoinTransactionPriorities()
// => LitecoinTransactionPriority.all;
//
// @override
// TransactionPriority deserializeBitcoinTransactionPriority(int raw)
// => BitcoinTransactionPriority.deserialize(raw: raw);
//
// @override
// TransactionPriority deserializeLitecoinTransactionPriority(int raw)
// => LitecoinTransactionPriority.deserialize(raw: raw);
//
// @override
// int getFeeRate(Object wallet, TransactionPriority priority) {
// final bitcoinWallet = wallet as ElectrumWallet;
// return bitcoinWallet.feeRate(priority);
// }
//
// @override
// Future<void> generateNewAddress(Object wallet) async {
// final bitcoinWallet = wallet as ElectrumWallet;
// await bitcoinWallet.walletAddresses.generateNewAddress();
// }
//
// @override
// Object createBitcoinTransactionCredentials(List<Output> outputs, {required TransactionPriority priority, int? feeRate})
// => BitcoinTransactionCredentials(
// outputs.map((out) => OutputInfo(
// fiatAmount: out.fiatAmount,
// cryptoAmount: out.cryptoAmount,
// address: out.address,
// note: out.note,
// sendAll: out.sendAll,
// extractedAddress: out.extractedAddress,
// isParsedAddress: out.isParsedAddress,
// formattedCryptoAmount: out.formattedCryptoAmount))
// .toList(),
// priority: priority as BitcoinTransactionPriority,
// feeRate: feeRate);
//
// @override
// Object createBitcoinTransactionCredentialsRaw(List<OutputInfo> outputs, {TransactionPriority? priority, required int feeRate})
// => BitcoinTransactionCredentials(
// outputs,
// priority: priority != null ? priority as BitcoinTransactionPriority : null,
// feeRate: feeRate);
//
// @override
// List<String> getAddresses(Object wallet) {
// final bitcoinWallet = wallet as ElectrumWallet;
// return bitcoinWallet.walletAddresses.addresses
// .map((BitcoinAddressRecord addr) => addr.address)
// .toList();
// }
//
// @override
// String getAddress(Object wallet) {
// final bitcoinWallet = wallet as ElectrumWallet;
// return bitcoinWallet.walletAddresses.address;
// }
//
// @override
// String formatterBitcoinAmountToString({required int amount})
// => bitcoinAmountToString(amount: amount);
//
// @override
// double formatterBitcoinAmountToDouble({required int amount})
// => bitcoinAmountToDouble(amount: amount);
//
// @override
// int formatterStringDoubleToBitcoinAmount(String amount)
// => stringDoubleToBitcoinAmount(amount);
//
// @override
// String bitcoinTransactionPriorityWithLabel(TransactionPriority priority, int rate)
// => (priority as BitcoinTransactionPriority).labelWithRate(rate);
//
// void updateUnspents(Object wallet) async {
// final bitcoinWallet = wallet as ElectrumWallet;
// await bitcoinWallet.updateUnspent();
// }
//
// WalletService createBitcoinWalletService(Box<WalletInfo> walletInfoSource, Box<UnspentCoinsInfo> unspentCoinSource) {
// return BitcoinWalletService(walletInfoSource, unspentCoinSource);
// }
//
// WalletService createLitecoinWalletService(Box<WalletInfo> walletInfoSource, Box<UnspentCoinsInfo> unspentCoinSource) {
// return LitecoinWalletService(walletInfoSource, unspentCoinSource);
// }
//
// @override
// TransactionPriority getBitcoinTransactionPriorityMedium()
// => BitcoinTransactionPriority.medium;
//
// @override
// TransactionPriority getLitecoinTransactionPriorityMedium()
// => LitecoinTransactionPriority.medium;
//
// @override
// TransactionPriority getBitcoinTransactionPrioritySlow()
// => BitcoinTransactionPriority.slow;
//
// @override
// TransactionPriority getLitecoinTransactionPrioritySlow()
// => LitecoinTransactionPriority.slow;
} }

View file

@ -28,6 +28,8 @@ class SeedValidator extends Validator<MnemonicItem> {
return haven!.getMoneroWordList(language); return haven!.getMoneroWordList(language);
case WalletType.ethereum: case WalletType.ethereum:
return ethereum!.getEthereumWordList(language); return ethereum!.getEthereumWordList(language);
case WalletType.bitcoinCash:
return getBitcoinWordList(language);
default: default:
return []; return [];
} }

View file

@ -2,6 +2,7 @@ import 'package:cake_wallet/anonpay/anonpay_api.dart';
import 'package:cake_wallet/anonpay/anonpay_info_base.dart'; import 'package:cake_wallet/anonpay/anonpay_info_base.dart';
import 'package:cake_wallet/anonpay/anonpay_invoice_info.dart'; import 'package:cake_wallet/anonpay/anonpay_invoice_info.dart';
import 'package:cake_wallet/buy/onramper/onramper_buy_provider.dart'; import 'package:cake_wallet/buy/onramper/onramper_buy_provider.dart';
import 'package:cake_wallet/bitcoin_cash/bitcoin_cash.dart';
import 'package:cake_wallet/buy/payfura/payfura_buy_provider.dart'; import 'package:cake_wallet/buy/payfura/payfura_buy_provider.dart';
import 'package:cake_wallet/core/yat_service.dart'; import 'package:cake_wallet/core/yat_service.dart';
import 'package:cake_wallet/entities/background_tasks.dart'; import 'package:cake_wallet/entities/background_tasks.dart';
@ -759,6 +760,8 @@ Future setup({
return bitcoin!.createLitecoinWalletService(_walletInfoSource, _unspentCoinsInfoSource!); return bitcoin!.createLitecoinWalletService(_walletInfoSource, _unspentCoinsInfoSource!);
case WalletType.ethereum: case WalletType.ethereum:
return ethereum!.createEthereumWalletService(_walletInfoSource); return ethereum!.createEthereumWalletService(_walletInfoSource);
case WalletType.bitcoinCash:
return bitcoinCash!.createBitcoinCashWalletService(_walletInfoSource, _unspentCoinsInfoSource!);
default: default:
throw Exception('Unexpected token: ${param1.toString()} for generating of WalletService'); throw Exception('Unexpected token: ${param1.toString()} for generating of WalletService');
} }

View file

@ -27,6 +27,7 @@ const cakeWalletBitcoinElectrumUri = 'electrum.cakewallet.com:50002';
const cakeWalletLitecoinElectrumUri = 'ltc-electrum.cakewallet.com:50002'; const cakeWalletLitecoinElectrumUri = 'ltc-electrum.cakewallet.com:50002';
const havenDefaultNodeUri = 'nodes.havenprotocol.org:443'; const havenDefaultNodeUri = 'nodes.havenprotocol.org:443';
const ethereumDefaultNodeUri = 'ethereum.publicnode.com'; const ethereumDefaultNodeUri = 'ethereum.publicnode.com';
const cakeWalletBitcoinCashDefaultNodeUri = 'bitcoincash.stackwallet.com:50002';
Future defaultSettingsMigration( Future defaultSettingsMigration(
{required int version, {required int version,
@ -84,6 +85,8 @@ Future defaultSettingsMigration(
sharedPreferences: sharedPreferences, nodes: nodes); sharedPreferences: sharedPreferences, nodes: nodes);
await changeHavenCurrentNodeToDefault( await changeHavenCurrentNodeToDefault(
sharedPreferences: sharedPreferences, nodes: nodes); sharedPreferences: sharedPreferences, nodes: nodes);
await changeBitcoinCashCurrentNodeToDefault(
sharedPreferences: sharedPreferences, nodes: nodes);
break; break;
case 2: case 2:
@ -163,6 +166,11 @@ Future defaultSettingsMigration(
await changeEthereumCurrentNodeToDefault( await changeEthereumCurrentNodeToDefault(
sharedPreferences: sharedPreferences, nodes: nodes); sharedPreferences: sharedPreferences, nodes: nodes);
break; break;
case 22:
await addBitcoinCashElectrumServerList(nodes: nodes);
await changeBitcoinCurrentElectrumServerToDefault(
sharedPreferences: sharedPreferences, nodes: nodes);
break;
default: default:
break; break;
@ -255,6 +263,12 @@ Node? getEthereumDefaultNode({required Box<Node> nodes}) {
?? nodes.values.firstWhereOrNull((node) => node.type == WalletType.ethereum); ?? nodes.values.firstWhereOrNull((node) => node.type == WalletType.ethereum);
} }
Node? getBitcoinCashDefaultElectrumServer({required Box<Node> nodes}) {
return nodes.values.firstWhereOrNull(
(Node node) => node.uriRaw == cakeWalletBitcoinCashDefaultNodeUri)
?? nodes.values.firstWhereOrNull((node) => node.type == WalletType.bitcoinCash);
}
Node getMoneroDefaultNode({required Box<Node> nodes}) { Node getMoneroDefaultNode({required Box<Node> nodes}) {
final timeZone = DateTime.now().timeZoneOffset.inHours; final timeZone = DateTime.now().timeZoneOffset.inHours;
var nodeUri = ''; var nodeUri = '';
@ -293,6 +307,15 @@ Future<void> changeLitecoinCurrentElectrumServerToDefault(
await sharedPreferences.setInt(PreferencesKey.currentLitecoinElectrumSererIdKey, serverId); await sharedPreferences.setInt(PreferencesKey.currentLitecoinElectrumSererIdKey, serverId);
} }
Future<void> changeBitcoinCashCurrentNodeToDefault(
{required SharedPreferences sharedPreferences,
required Box<Node> nodes}) async {
final server = getBitcoinCashDefaultElectrumServer(nodes: nodes);
final serverId = server?.key as int ?? 0;
await sharedPreferences.setInt(PreferencesKey.currentBitcoinCashNodeIdKey, serverId);
}
Future<void> changeHavenCurrentNodeToDefault( Future<void> changeHavenCurrentNodeToDefault(
{required SharedPreferences sharedPreferences, {required SharedPreferences sharedPreferences,
required Box<Node> nodes}) async { required Box<Node> nodes}) async {
@ -351,6 +374,15 @@ Future<void> addLitecoinElectrumServerList({required Box<Node> nodes}) async {
} }
} }
Future<void> addBitcoinCashElectrumServerList({required Box<Node> nodes}) async {
final serverList = await loadBitcoinCashElectrumServerList();
for (var node in serverList) {
if (nodes.values.firstWhereOrNull((element) => element.uriRaw == node.uriRaw) == null) {
await nodes.add(node);
}
}
}
Future<void> addHavenNodeList({required Box<Node> nodes}) async { Future<void> addHavenNodeList({required Box<Node> nodes}) async {
final nodeList = await loadDefaultHavenNodes(); final nodeList = await loadDefaultHavenNodes();
for (var node in nodeList) { for (var node in nodeList) {
@ -453,6 +485,8 @@ Future<void> checkCurrentNodes(
.getInt(PreferencesKey.currentHavenNodeIdKey); .getInt(PreferencesKey.currentHavenNodeIdKey);
final currentEthereumNodeId = sharedPreferences final currentEthereumNodeId = sharedPreferences
.getInt(PreferencesKey.currentEthereumNodeIdKey); .getInt(PreferencesKey.currentEthereumNodeIdKey);
final currentBitcoinCashNodeId = sharedPreferences
.getInt(PreferencesKey.currentBitcoinCashNodeIdKey);
final currentMoneroNode = nodeSource.values.firstWhereOrNull( final currentMoneroNode = nodeSource.values.firstWhereOrNull(
(node) => node.key == currentMoneroNodeId); (node) => node.key == currentMoneroNodeId);
final currentBitcoinElectrumServer = nodeSource.values.firstWhereOrNull( final currentBitcoinElectrumServer = nodeSource.values.firstWhereOrNull(
@ -463,6 +497,8 @@ Future<void> checkCurrentNodes(
(node) => node.key == currentHavenNodeId); (node) => node.key == currentHavenNodeId);
final currentEthereumNodeServer = nodeSource.values.firstWhereOrNull( final currentEthereumNodeServer = nodeSource.values.firstWhereOrNull(
(node) => node.key == currentEthereumNodeId); (node) => node.key == currentEthereumNodeId);
final currentBitcoinCashNodeServer = nodeSource.values.firstWhereOrNull(
(node) => node.key == currentBitcoinCashNodeId);
if (currentMoneroNode == null) { if (currentMoneroNode == null) {
final newCakeWalletNode = final newCakeWalletNode =
@ -503,6 +539,13 @@ Future<void> checkCurrentNodes(
await sharedPreferences.setInt( await sharedPreferences.setInt(
PreferencesKey.currentEthereumNodeIdKey, node.key as int); PreferencesKey.currentEthereumNodeIdKey, node.key as int);
} }
if (currentBitcoinCashNodeServer == null) {
final node = Node(uri: cakeWalletBitcoinCashDefaultNodeUri, type: WalletType.bitcoinCash);
await nodeSource.add(node);
await sharedPreferences.setInt(
PreferencesKey.currentBitcoinCashNodeIdKey, node.key as int);
}
} }
Future<void> resetBitcoinElectrumServer( Future<void> resetBitcoinElectrumServer(

View file

@ -86,15 +86,33 @@ Future<List<Node>> loadDefaultEthereumNodes() async {
return nodes; return nodes;
} }
Future<List<Node>> loadBitcoinCashElectrumServerList() async {
final serverListRaw =
await rootBundle.loadString('assets/bitcoin_cash_electrum_server_list.yml');
final loadedServerList = loadYaml(serverListRaw) as YamlList;
final serverList = <Node>[];
for (final raw in loadedServerList) {
if (raw is Map) {
final node = Node.fromMap(Map<String, Object>.from(raw));
node.type = WalletType.bitcoinCash;
serverList.add(node);
}
}
return serverList;
}
Future resetToDefault(Box<Node> nodeSource) async { Future resetToDefault(Box<Node> nodeSource) async {
final moneroNodes = await loadDefaultNodes(); final moneroNodes = await loadDefaultNodes();
final bitcoinElectrumServerList = await loadBitcoinElectrumServerList(); final bitcoinElectrumServerList = await loadBitcoinElectrumServerList();
final litecoinElectrumServerList = await loadLitecoinElectrumServerList(); final litecoinElectrumServerList = await loadLitecoinElectrumServerList();
final bitcoinCashElectrumServerList = await loadBitcoinCashElectrumServerList();
final havenNodes = await loadDefaultHavenNodes(); final havenNodes = await loadDefaultHavenNodes();
final nodes = final nodes = moneroNodes +
moneroNodes +
bitcoinElectrumServerList + bitcoinElectrumServerList +
litecoinElectrumServerList + litecoinElectrumServerList +
bitcoinCashElectrumServerList +
havenNodes; havenNodes;
await nodeSource.clear(); await nodeSource.clear();

View file

@ -7,6 +7,7 @@ class PreferencesKey {
static const currentHavenNodeIdKey = 'current_node_id_xhv'; static const currentHavenNodeIdKey = 'current_node_id_xhv';
static const currentEthereumNodeIdKey = 'current_node_id_eth'; static const currentEthereumNodeIdKey = 'current_node_id_eth';
static const currentFiatCurrencyKey = 'current_fiat_currency'; static const currentFiatCurrencyKey = 'current_fiat_currency';
static const currentBitcoinCashNodeIdKey = 'current_node_id_bch';
static const currentTransactionPriorityKeyLegacy = 'current_fee_priority'; static const currentTransactionPriorityKeyLegacy = 'current_fee_priority';
static const currentBalanceDisplayModeKey = 'current_balance_display_mode'; static const currentBalanceDisplayModeKey = 'current_balance_display_mode';
static const shouldSaveRecipientAddressKey = 'save_recipient_address'; static const shouldSaveRecipientAddressKey = 'save_recipient_address';

View file

@ -153,7 +153,7 @@ Future<void> initializeAppConfigs() async {
transactionDescriptions: transactionDescriptions, transactionDescriptions: transactionDescriptions,
secureStorage: secureStorage, secureStorage: secureStorage,
anonpayInvoiceInfo: anonpayInvoiceInfo, anonpayInvoiceInfo: anonpayInvoiceInfo,
initialMigrationVersion: 21); initialMigrationVersion: 22);
} }
Future<void> initialSetup( Future<void> initialSetup(

View file

@ -30,7 +30,8 @@ class MenuWidgetState extends State<MenuWidget> {
this.bitcoinIcon = Image.asset('assets/images/bitcoin_menu.png'), this.bitcoinIcon = Image.asset('assets/images/bitcoin_menu.png'),
this.litecoinIcon = Image.asset('assets/images/litecoin_menu.png'), this.litecoinIcon = Image.asset('assets/images/litecoin_menu.png'),
this.havenIcon = Image.asset('assets/images/haven_menu.png'), this.havenIcon = Image.asset('assets/images/haven_menu.png'),
this.ethereumIcon = Image.asset('assets/images/eth_icon.png'); this.ethereumIcon = Image.asset('assets/images/eth_icon.png'),
this.bitcoinCashIcon = Image.asset('assets/images/bch_icon.png');
final largeScreen = 731; final largeScreen = 731;
@ -48,6 +49,7 @@ class MenuWidgetState extends State<MenuWidget> {
Image litecoinIcon; Image litecoinIcon;
Image havenIcon; Image havenIcon;
Image ethereumIcon; Image ethereumIcon;
Image bitcoinCashIcon;
@override @override
void initState() { void initState() {
@ -217,6 +219,8 @@ class MenuWidgetState extends State<MenuWidget> {
return havenIcon; return havenIcon;
case WalletType.ethereum: case WalletType.ethereum:
return ethereumIcon; return ethereumIcon;
case WalletType.bitcoinCash:
return bitcoinCashIcon;
default: default:
throw Exception('No icon for ${type.toString()}'); throw Exception('No icon for ${type.toString()}');
} }

View file

@ -41,16 +41,20 @@ class WalletListBody extends StatefulWidget {
} }
class WalletListBodyState extends State<WalletListBody> { class WalletListBodyState extends State<WalletListBody> {
final moneroIcon = Image.asset('assets/images/monero_logo.png', height: 24, width: 24); final nonWalletTypeIconPath = 'assets/images/close.png';
final bitcoinIcon = Image.asset('assets/images/bitcoin.png', height: 24, width: 24);
final litecoinIcon = Image.asset('assets/images/litecoin_icon.png', height: 24, width: 24);
final nonWalletTypeIcon = Image.asset('assets/images/close.png', height: 24, width: 24);
final havenIcon = Image.asset('assets/images/haven_logo.png', height: 24, width: 24);
final ethereumIcon = Image.asset('assets/images/eth_icon.png', height: 24, width: 24);
final scrollController = ScrollController();
final double tileHeight = 60; final double tileHeight = 60;
Flushbar<void>? _progressBar; Flushbar<void>? _progressBar;
Image getIconByWalletType(WalletType type, bool isEnabled) {
if (!isEnabled) {
return Image.asset(nonWalletTypeIconPath, height: 24, width: 24);
}
final path = walletTypeToCryptoCurrency(type).iconPath ?? nonWalletTypeIconPath;
print('path: $path type: $type');
return Image.asset(path, height: 24, width: 24);
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final newWalletImage = final newWalletImage =
@ -100,9 +104,7 @@ class WalletListBodyState extends State<WalletListBody> {
child: Row( child: Row(
crossAxisAlignment: CrossAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[ children: <Widget>[
wallet.isEnabled getIconByWalletType(wallet.type, wallet.isEnabled),
? _imageFor(type: wallet.type)
: nonWalletTypeIcon,
SizedBox(width: 10), SizedBox(width: 10),
Flexible( Flexible(
child: Text( child: Text(
@ -221,23 +223,6 @@ class WalletListBodyState extends State<WalletListBody> {
); );
} }
Image _imageFor({required WalletType type}) {
switch (type) {
case WalletType.bitcoin:
return bitcoinIcon;
case WalletType.monero:
return moneroIcon;
case WalletType.litecoin:
return litecoinIcon;
case WalletType.haven:
return havenIcon;
case WalletType.ethereum:
return ethereumIcon;
default:
return nonWalletTypeIcon;
}
}
Future<void> _loadWallet(WalletListItem wallet) async { Future<void> _loadWallet(WalletListItem wallet) async {
await widget.authService.authenticateAction( await widget.authService.authenticateAction(
context, context,

View file

@ -1,6 +1,7 @@
import 'dart:io'; import 'dart:io';
import 'package:cake_wallet/bitcoin/bitcoin.dart'; import 'package:cake_wallet/bitcoin/bitcoin.dart';
import 'package:cake_wallet/bitcoin_cash/bitcoin_cash.dart';
import 'package:cake_wallet/entities/cake_2fa_preset_options.dart'; import 'package:cake_wallet/entities/cake_2fa_preset_options.dart';
import 'package:cake_wallet/entities/background_tasks.dart'; import 'package:cake_wallet/entities/background_tasks.dart';
import 'package:cake_wallet/entities/exchange_api_mode.dart'; import 'package:cake_wallet/entities/exchange_api_mode.dart';
@ -80,7 +81,8 @@ abstract class SettingsStoreBase with Store {
TransactionPriority? initialMoneroTransactionPriority, TransactionPriority? initialMoneroTransactionPriority,
TransactionPriority? initialHavenTransactionPriority, TransactionPriority? initialHavenTransactionPriority,
TransactionPriority? initialLitecoinTransactionPriority, TransactionPriority? initialLitecoinTransactionPriority,
TransactionPriority? initialEthereumTransactionPriority}) TransactionPriority? initialEthereumTransactionPriority,
TransactionPriority? initialBitcoinCashTransactionPriority})
: nodes = ObservableMap<WalletType, Node>.of(nodes), : nodes = ObservableMap<WalletType, Node>.of(nodes),
_sharedPreferences = sharedPreferences, _sharedPreferences = sharedPreferences,
_backgroundTasks = backgroundTasks, _backgroundTasks = backgroundTasks,
@ -138,6 +140,10 @@ abstract class SettingsStoreBase with Store {
priority[WalletType.ethereum] = initialEthereumTransactionPriority; priority[WalletType.ethereum] = initialEthereumTransactionPriority;
} }
if (initialBitcoinTransactionPriority != null) {
priority[WalletType.bitcoinCash] = initialBitcoinTransactionPriority;
}
reaction( reaction(
(_) => fiatCurrency, (_) => fiatCurrency,
(FiatCurrency fiatCurrency) => sharedPreferences.setString( (FiatCurrency fiatCurrency) => sharedPreferences.setString(
@ -166,6 +172,9 @@ abstract class SettingsStoreBase with Store {
case WalletType.ethereum: case WalletType.ethereum:
key = PreferencesKey.ethereumTransactionPriority; key = PreferencesKey.ethereumTransactionPriority;
break; break;
case WalletType.bitcoinCash:
key = PreferencesKey.bitcoinTransactionPriority;
break;
default: default:
key = null; key = null;
} }
@ -495,6 +504,7 @@ abstract class SettingsStoreBase with Store {
TransactionPriority? havenTransactionPriority; TransactionPriority? havenTransactionPriority;
TransactionPriority? litecoinTransactionPriority; TransactionPriority? litecoinTransactionPriority;
TransactionPriority? ethereumTransactionPriority; TransactionPriority? ethereumTransactionPriority;
TransactionPriority? bitcoinCashTransactionPriority;
if (sharedPreferences.getInt(PreferencesKey.havenTransactionPriority) != null) { if (sharedPreferences.getInt(PreferencesKey.havenTransactionPriority) != null) {
havenTransactionPriority = monero?.deserializeMoneroTransactionPriority( havenTransactionPriority = monero?.deserializeMoneroTransactionPriority(
@ -508,12 +518,17 @@ abstract class SettingsStoreBase with Store {
ethereumTransactionPriority = bitcoin?.deserializeLitecoinTransactionPriority( ethereumTransactionPriority = bitcoin?.deserializeLitecoinTransactionPriority(
sharedPreferences.getInt(PreferencesKey.ethereumTransactionPriority)!); sharedPreferences.getInt(PreferencesKey.ethereumTransactionPriority)!);
} }
if (sharedPreferences.getInt(PreferencesKey.bitcoinTransactionPriority) != null) {
bitcoinCashTransactionPriority = bitcoin?.deserializeLitecoinTransactionPriority(
sharedPreferences.getInt(PreferencesKey.bitcoinTransactionPriority)!);
}
moneroTransactionPriority ??= monero?.getDefaultTransactionPriority(); moneroTransactionPriority ??= monero?.getDefaultTransactionPriority();
bitcoinTransactionPriority ??= bitcoin?.getMediumTransactionPriority(); bitcoinTransactionPriority ??= bitcoin?.getMediumTransactionPriority();
havenTransactionPriority ??= monero?.getDefaultTransactionPriority(); havenTransactionPriority ??= monero?.getDefaultTransactionPriority();
litecoinTransactionPriority ??= bitcoin?.getLitecoinTransactionPriorityMedium(); litecoinTransactionPriority ??= bitcoin?.getLitecoinTransactionPriorityMedium();
ethereumTransactionPriority ??= ethereum?.getDefaultTransactionPriority(); ethereumTransactionPriority ??= ethereum?.getDefaultTransactionPriority();
bitcoinCashTransactionPriority ??= bitcoinCash?.getDefaultTransactionPriority();
final currentBalanceDisplayMode = BalanceDisplayMode.deserialize( final currentBalanceDisplayMode = BalanceDisplayMode.deserialize(
raw: sharedPreferences.getInt(PreferencesKey.currentBalanceDisplayModeKey)!); raw: sharedPreferences.getInt(PreferencesKey.currentBalanceDisplayModeKey)!);
@ -592,6 +607,8 @@ abstract class SettingsStoreBase with Store {
sharedPreferences.getInt(PreferencesKey.currentBitcoinElectrumSererIdKey); sharedPreferences.getInt(PreferencesKey.currentBitcoinElectrumSererIdKey);
final litecoinElectrumServerId = final litecoinElectrumServerId =
sharedPreferences.getInt(PreferencesKey.currentLitecoinElectrumSererIdKey); sharedPreferences.getInt(PreferencesKey.currentLitecoinElectrumSererIdKey);
final bitcoinCashElectrumServerId =
sharedPreferences.getInt(PreferencesKey.currentBitcoinCashNodeIdKey);
final havenNodeId = sharedPreferences.getInt(PreferencesKey.currentHavenNodeIdKey); final havenNodeId = sharedPreferences.getInt(PreferencesKey.currentHavenNodeIdKey);
final ethereumNodeId = sharedPreferences.getInt(PreferencesKey.currentEthereumNodeIdKey); final ethereumNodeId = sharedPreferences.getInt(PreferencesKey.currentEthereumNodeIdKey);
final moneroNode = nodeSource.get(nodeId); final moneroNode = nodeSource.get(nodeId);
@ -599,6 +616,7 @@ abstract class SettingsStoreBase with Store {
final litecoinElectrumServer = nodeSource.get(litecoinElectrumServerId); final litecoinElectrumServer = nodeSource.get(litecoinElectrumServerId);
final havenNode = nodeSource.get(havenNodeId); final havenNode = nodeSource.get(havenNodeId);
final ethereumNode = nodeSource.get(ethereumNodeId); final ethereumNode = nodeSource.get(ethereumNodeId);
final bitcoinCashElectrumServer = nodeSource.get(bitcoinCashElectrumServerId);
final packageInfo = await PackageInfo.fromPlatform(); final packageInfo = await PackageInfo.fromPlatform();
final deviceName = await _getDeviceName() ?? ''; final deviceName = await _getDeviceName() ?? '';
final shouldShowYatPopup = sharedPreferences.getBool(PreferencesKey.shouldShowYatPopup) ?? true; final shouldShowYatPopup = sharedPreferences.getBool(PreferencesKey.shouldShowYatPopup) ?? true;
@ -625,6 +643,10 @@ abstract class SettingsStoreBase with Store {
nodes[WalletType.ethereum] = ethereumNode; nodes[WalletType.ethereum] = ethereumNode;
} }
if (bitcoinCashElectrumServer != null) {
nodes[WalletType.bitcoinCash] = bitcoinCashElectrumServer;
}
final savedSyncMode = SyncMode.all.firstWhere((element) { final savedSyncMode = SyncMode.all.firstWhere((element) {
return element.type.index == (sharedPreferences.getInt(PreferencesKey.syncModeKey) ?? 1); return element.type.index == (sharedPreferences.getInt(PreferencesKey.syncModeKey) ?? 1);
}); });
@ -708,6 +730,11 @@ abstract class SettingsStoreBase with Store {
sharedPreferences.getInt(PreferencesKey.ethereumTransactionPriority)!) ?? sharedPreferences.getInt(PreferencesKey.ethereumTransactionPriority)!) ??
priority[WalletType.ethereum]!; priority[WalletType.ethereum]!;
} }
if (sharedPreferences.getInt(PreferencesKey.bitcoinTransactionPriority) != null) {
priority[WalletType.bitcoinCash] = bitcoinCash?.deserializeBitcoinCashTransactionPriority(
sharedPreferences.getInt(PreferencesKey.bitcoinTransactionPriority)!) ??
priority[WalletType.bitcoinCash]!;
}
balanceDisplayMode = BalanceDisplayMode.deserialize( balanceDisplayMode = BalanceDisplayMode.deserialize(
raw: sharedPreferences.getInt(PreferencesKey.currentBalanceDisplayModeKey)!); raw: sharedPreferences.getInt(PreferencesKey.currentBalanceDisplayModeKey)!);
@ -787,6 +814,8 @@ abstract class SettingsStoreBase with Store {
sharedPreferences.getInt(PreferencesKey.currentBitcoinElectrumSererIdKey); sharedPreferences.getInt(PreferencesKey.currentBitcoinElectrumSererIdKey);
final litecoinElectrumServerId = final litecoinElectrumServerId =
sharedPreferences.getInt(PreferencesKey.currentLitecoinElectrumSererIdKey); sharedPreferences.getInt(PreferencesKey.currentLitecoinElectrumSererIdKey);
final bitcoinCashElectrumServerId =
sharedPreferences.getInt(PreferencesKey.currentBitcoinCashNodeIdKey);
final havenNodeId = sharedPreferences.getInt(PreferencesKey.currentHavenNodeIdKey); final havenNodeId = sharedPreferences.getInt(PreferencesKey.currentHavenNodeIdKey);
final ethereumNodeId = sharedPreferences.getInt(PreferencesKey.currentEthereumNodeIdKey); final ethereumNodeId = sharedPreferences.getInt(PreferencesKey.currentEthereumNodeIdKey);
final moneroNode = nodeSource.get(nodeId); final moneroNode = nodeSource.get(nodeId);
@ -794,6 +823,7 @@ abstract class SettingsStoreBase with Store {
final litecoinElectrumServer = nodeSource.get(litecoinElectrumServerId); final litecoinElectrumServer = nodeSource.get(litecoinElectrumServerId);
final havenNode = nodeSource.get(havenNodeId); final havenNode = nodeSource.get(havenNodeId);
final ethereumNode = nodeSource.get(ethereumNodeId); final ethereumNode = nodeSource.get(ethereumNodeId);
final bitcoinCashNode = nodeSource.get(bitcoinCashElectrumServerId);
if (moneroNode != null) { if (moneroNode != null) {
nodes[WalletType.monero] = moneroNode; nodes[WalletType.monero] = moneroNode;
@ -814,6 +844,10 @@ abstract class SettingsStoreBase with Store {
if (ethereumNode != null) { if (ethereumNode != null) {
nodes[WalletType.ethereum] = ethereumNode; nodes[WalletType.ethereum] = ethereumNode;
} }
if (bitcoinCashNode != null) {
nodes[WalletType.bitcoinCash] = bitcoinCashNode;
}
} }
Future<void> _saveCurrentNode(Node node, WalletType walletType) async { Future<void> _saveCurrentNode(Node node, WalletType walletType) async {
@ -835,6 +869,9 @@ abstract class SettingsStoreBase with Store {
case WalletType.ethereum: case WalletType.ethereum:
await _sharedPreferences.setInt(PreferencesKey.currentEthereumNodeIdKey, node.key as int); await _sharedPreferences.setInt(PreferencesKey.currentEthereumNodeIdKey, node.key as int);
break; break;
case WalletType.bitcoinCash:
await _sharedPreferences.setInt(PreferencesKey.currentBitcoinCashNodeIdKey, node.key as int);
break;
default: default:
break; break;
} }

View file

@ -66,6 +66,9 @@ abstract class NodeListViewModelBase with Store {
case WalletType.ethereum: case WalletType.ethereum:
node = getEthereumDefaultNode(nodes: _nodeSource)!; node = getEthereumDefaultNode(nodes: _nodeSource)!;
break; break;
case WalletType.bitcoinCash:
node = getBitcoinCashDefaultElectrumServer(nodes: _nodeSource)!;
break;
default: default:
throw Exception('Unexpected wallet type: ${_appStore.wallet!.type}'); throw Exception('Unexpected wallet type: ${_appStore.wallet!.type}');
} }

View file

@ -110,6 +110,24 @@ class EthereumURI extends PaymentURI {
} }
} }
class BitcoinCashURI extends PaymentURI {
BitcoinCashURI({
required String amount,
required String address})
: super(amount: amount, address: address);
@override
String toString() {
var base = address;
if (amount.isNotEmpty) {
base += '?amount=${amount.replaceAll(',', '.')}';
}
return base;
}
}
abstract class WalletAddressListViewModelBase with Store { abstract class WalletAddressListViewModelBase with Store {
WalletAddressListViewModelBase({ WalletAddressListViewModelBase({
required AppStore appStore, required AppStore appStore,
@ -172,6 +190,10 @@ abstract class WalletAddressListViewModelBase with Store {
return EthereumURI(amount: amount, address: address.address); return EthereumURI(amount: amount, address: address.address);
} }
if (_wallet.type == WalletType.bitcoinCash) {
return BitcoinCashURI(amount: amount, address: address.address);
}
throw Exception('Unexpected type: ${type.toString()}'); throw Exception('Unexpected type: ${type.toString()}');
} }

View file

@ -120,6 +120,8 @@ abstract class WalletKeysViewModelBase with Store {
return 'haven-wallet'; return 'haven-wallet';
case WalletType.ethereum: case WalletType.ethereum:
return 'ethereum-wallet'; return 'ethereum-wallet';
case WalletType.bitcoinCash:
return 'bitcoinCash-wallet';
default: default:
throw Exception('Unexpected wallet type: ${_appStore.wallet!.toString()}'); throw Exception('Unexpected wallet type: ${_appStore.wallet!.toString()}');
} }

View file

@ -1,6 +1,7 @@
import 'package:cake_wallet/view_model/restore/restore_wallet.dart'; import 'package:cake_wallet/view_model/restore/restore_wallet.dart';
import 'package:cake_wallet/ethereum/ethereum.dart'; import 'package:cake_wallet/ethereum/ethereum.dart';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:cake_wallet/bitcoin_cash/bitcoin_cash.dart';
import 'package:hive/hive.dart'; import 'package:hive/hive.dart';
import 'package:mobx/mobx.dart'; import 'package:mobx/mobx.dart';
import 'package:cake_wallet/monero/monero.dart'; import 'package:cake_wallet/monero/monero.dart';
@ -45,8 +46,10 @@ abstract class WalletNewVMBase extends WalletCreationVM with Store {
name: name, language: options as String); name: name, language: options as String);
case WalletType.ethereum: case WalletType.ethereum:
return ethereum!.createEthereumNewWalletCredentials(name: name); return ethereum!.createEthereumNewWalletCredentials(name: name);
case WalletType.bitcoinCash:
return bitcoinCash!.createBitcoinCashNewWalletCredentials(name: name);
default: default:
throw Exception('Unexpected type: ${type.toString()}');; throw Exception('Unexpected type: ${type.toString()}');
} }
} }

View file

@ -3,6 +3,7 @@ import 'package:cake_wallet/core/mnemonic_length.dart';
import 'package:cake_wallet/view_model/restore/restore_wallet.dart'; import 'package:cake_wallet/view_model/restore/restore_wallet.dart';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:cake_wallet/ethereum/ethereum.dart'; import 'package:cake_wallet/ethereum/ethereum.dart';
import 'package:cake_wallet/bitcoin_cash/bitcoin_cash.dart';
import 'package:hive/hive.dart'; import 'package:hive/hive.dart';
import 'package:mobx/mobx.dart'; import 'package:mobx/mobx.dart';
import 'package:cake_wallet/store/app_store.dart'; import 'package:cake_wallet/store/app_store.dart';
@ -91,6 +92,11 @@ abstract class WalletRestoreViewModelBase extends WalletCreationVM with Store {
name: name, name: name,
mnemonic: seed, mnemonic: seed,
password: password); password: password);
case WalletType.bitcoinCash:
return bitcoinCash!.createBitcoinCashRestoreWalletFromSeedCredentials(
name: name,
mnemonic: seed,
password: password);
default: default:
break; break;
} }

View file

@ -121,6 +121,7 @@ flutter:
- assets/bitcoin_electrum_server_list.yml - assets/bitcoin_electrum_server_list.yml
- assets/litecoin_electrum_server_list.yml - assets/litecoin_electrum_server_list.yml
- assets/ethereum_server_list.yml - assets/ethereum_server_list.yml
- assets/bitcoin_cash_electrum_server_list.yml
- assets/text/ - assets/text/
- assets/faq/ - assets/faq/
- assets/animation/ - assets/animation/

View file

@ -564,6 +564,8 @@ Future<void> generateBitcoinCash(bool hasImplementation) async {
const bitcoinCashCommonHeaders = """ const bitcoinCashCommonHeaders = """
import 'dart:typed_data'; import 'dart:typed_data';
import 'package:cw_bitcoin_cash/cw_bitcoin_cash.dart';
import 'package:cw_core/transaction_priority.dart';
import 'package:cw_core/unspent_coins_info.dart'; import 'package:cw_core/unspent_coins_info.dart';
import 'package:cw_core/wallet_credentials.dart'; import 'package:cw_core/wallet_credentials.dart';
import 'package:cw_core/wallet_info.dart'; import 'package:cw_core/wallet_info.dart';
@ -577,9 +579,21 @@ import 'package:cw_bitcoin_cash/cw_bitcoin_cash.dart';
const bitcoinCashContent = """ const bitcoinCashContent = """
abstract class BitcoinCash { abstract class BitcoinCash {
String getMnemonic(int? strength); String getMnemonic(int? strength);
Uint8List getSeedFromMnemonic(String seed); Uint8List getSeedFromMnemonic(String seed);
WalletService createBitcoinCashWalletService(Box<WalletInfo> walletInfoSource, Box<UnspentCoinsInfo> unspentCoinSource);
WalletCredentials createBitcoinCashNewWalletCredentials({required String name, WalletInfo? walletInfo}); WalletService createBitcoinCashWalletService(
Box<WalletInfo> walletInfoSource, Box<UnspentCoinsInfo> unspentCoinSource);
WalletCredentials createBitcoinCashNewWalletCredentials(
{required String name, WalletInfo? walletInfo});
WalletCredentials createBitcoinCashRestoreWalletFromSeedCredentials(
{required String name, required String mnemonic, required String password});
TransactionPriority deserializeBitcoinCashTransactionPriority(int raw);
TransactionPriority getDefaultTransactionPriority();
} }
"""; """;