Merge branch 'main' into zano-pr

This commit is contained in:
cyan 2024-11-07 18:04:17 +01:00 committed by GitHub
commit 4081c5109f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
247 changed files with 4725 additions and 1266 deletions

BIN
assets/images/flags/abw.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 607 B

BIN
assets/images/flags/afg.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

BIN
assets/images/flags/ago.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

BIN
assets/images/flags/aia.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

BIN
assets/images/flags/and.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

BIN
assets/images/flags/asm.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 503 B

BIN
assets/images/flags/atf.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

BIN
assets/images/flags/atg.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 963 B

BIN
assets/images/flags/aut.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 125 B

BIN
assets/images/flags/aze.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 717 B

BIN
assets/images/flags/bel.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 127 B

BIN
assets/images/flags/bes.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 236 B

BIN
assets/images/flags/bhr.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 567 B

BIN
assets/images/flags/blz.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

BIN
assets/images/flags/bmu.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

BIN
assets/images/flags/bol.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 668 B

BIN
assets/images/flags/brn.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

BIN
assets/images/flags/btn.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

BIN
assets/images/flags/bvt.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 147 B

BIN
assets/images/flags/bwa.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 134 B

BIN
assets/images/flags/cck.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

BIN
assets/images/flags/cmr.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 402 B

BIN
assets/images/flags/cok.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

BIN
assets/images/flags/cpv.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 297 B

BIN
assets/images/flags/cri.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 136 B

BIN
assets/images/flags/cuw.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 481 B

BIN
assets/images/flags/cxr.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

BIN
assets/images/flags/cyp.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

BIN
assets/images/flags/dji.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 387 B

BIN
assets/images/flags/dma.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

BIN
assets/images/flags/dza.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 518 B

BIN
assets/images/flags/ecu.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

BIN
assets/images/flags/est.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 130 B

BIN
assets/images/flags/eth.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

BIN
assets/images/flags/fin.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 131 B

BIN
assets/images/flags/fji.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

BIN
assets/images/flags/flk.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

BIN
assets/images/flags/fro.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 147 B

BIN
assets/images/flags/fsm.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 340 B

BIN
assets/images/flags/gab.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 130 B

BIN
assets/images/flags/geo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 187 B

BIN
assets/images/flags/ggi.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 892 B

BIN
assets/images/flags/ggy.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 192 B

BIN
assets/images/flags/glp.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

BIN
assets/images/flags/gmb.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 278 B

BIN
assets/images/flags/grc.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 436 B

BIN
assets/images/flags/grd.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 892 B

BIN
assets/images/flags/grl.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 407 B

BIN
assets/images/flags/guf.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 581 B

BIN
assets/images/flags/gum.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

BIN
assets/images/flags/guy.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 635 B

BIN
assets/images/flags/hmd.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 780 B

BIN
assets/images/flags/iot.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

BIN
assets/images/flags/irl.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 127 B

BIN
assets/images/flags/jam.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 526 B

BIN
assets/images/flags/jey.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 914 B

BIN
assets/images/flags/jor.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB

BIN
assets/images/flags/kaz.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

BIN
assets/images/flags/ken.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2 KiB

BIN
assets/images/flags/kir.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 KiB

BIN
assets/images/flags/kwt.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 372 B

BIN
assets/images/flags/lbn.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 960 B

BIN
assets/images/flags/lie.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 844 B

BIN
assets/images/flags/lka.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

BIN
assets/images/flags/ltu.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 130 B

BIN
assets/images/flags/lux.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 130 B

BIN
assets/images/flags/lva.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 124 B

BIN
assets/images/flags/mco.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 204 B

BIN
assets/images/flags/mlt.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 485 B

BIN
assets/images/flags/mnp.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2 KiB

BIN
assets/images/flags/mrt.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 509 B

BIN
assets/images/flags/msr.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

BIN
assets/images/flags/mtq.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

BIN
assets/images/flags/mwi.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 754 B

BIN
assets/images/flags/myt.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2 KiB

BIN
assets/images/flags/ner.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 494 B

BIN
assets/images/flags/nfk.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

BIN
assets/images/flags/niu.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

BIN
assets/images/flags/omn.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 711 B

BIN
assets/images/flags/per.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 123 B

BIN
assets/images/flags/plw.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 608 B

BIN
assets/images/flags/pri.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

BIN
assets/images/flags/pyf.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

BIN
assets/images/flags/qat.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2 KiB

BIN
assets/images/flags/slb.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 403 B

BIN
assets/images/flags/slv.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 642 B

BIN
assets/images/flags/svk.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 521 B

BIN
assets/images/flags/svn.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

BIN
assets/images/flags/tkm.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

BIN
assets/images/flags/ton.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 139 B

BIN
assets/images/flags/tuv.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

BIN
assets/images/flags/ury.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1,017 B

BIN
assets/images/flags/vat.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 851 B

BIN
assets/images/flags/vir.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

BIN
assets/images/flags/vut.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

View file

@ -124,6 +124,7 @@ class ElectrumClient {
final errorMsg = error.toString();
print(errorMsg);
unterminatedString = '';
socket = null;
},
onDone: () {
print("SOCKET CLOSED!!!!!");
@ -132,6 +133,7 @@ class ElectrumClient {
if (host == socket?.address.host || socket == null) {
_setConnectionStatus(ConnectionStatus.disconnected);
socket?.destroy();
socket = null;
}
} catch (e) {
print("onDone: $e");

View file

@ -26,7 +26,10 @@ class ElectrumBalance extends Balance {
return ElectrumBalance(
confirmed: decoded['confirmed'] as int? ?? 0,
unconfirmed: decoded['unconfirmed'] as int? ?? 0,
frozen: decoded['frozen'] as int? ?? 0);
frozen: decoded['frozen'] as int? ?? 0,
secondConfirmed: decoded['secondConfirmed'] as int? ?? 0,
secondUnconfirmed: decoded['secondUnconfirmed'] as int? ?? 0,
);
}
int confirmed;
@ -36,8 +39,7 @@ class ElectrumBalance extends Balance {
int secondUnconfirmed = 0;
@override
String get formattedAvailableBalance =>
bitcoinAmountToString(amount: confirmed - frozen);
String get formattedAvailableBalance => bitcoinAmountToString(amount: confirmed - frozen);
@override
String get formattedAdditionalBalance => bitcoinAmountToString(amount: unconfirmed);

View file

@ -41,6 +41,7 @@ class ElectrumTransactionInfo extends TransactionInfo {
String? to,
this.unspents,
this.isReceivedSilentPayment = false,
Map<String, dynamic>? additionalInfo,
}) {
this.id = id;
this.height = height;
@ -54,6 +55,7 @@ class ElectrumTransactionInfo extends TransactionInfo {
this.isReplaced = isReplaced;
this.confirmations = confirmations;
this.to = to;
this.additionalInfo = additionalInfo ?? {};
}
factory ElectrumTransactionInfo.fromElectrumVerbose(Map<String, Object> obj, WalletType type,
@ -212,6 +214,7 @@ class ElectrumTransactionInfo extends TransactionInfo {
BitcoinSilentPaymentsUnspent.fromJSON(null, unspent as Map<String, dynamic>))
.toList(),
isReceivedSilentPayment: data['isReceivedSilentPayment'] as bool? ?? false,
additionalInfo: data['additionalInfo'] as Map<String, dynamic>?,
);
}
@ -246,7 +249,8 @@ class ElectrumTransactionInfo extends TransactionInfo {
isReplaced: isReplaced ?? false,
inputAddresses: inputAddresses,
outputAddresses: outputAddresses,
confirmations: info.confirmations);
confirmations: info.confirmations,
additionalInfo: additionalInfo);
}
Map<String, dynamic> toJson() {
@ -265,10 +269,11 @@ class ElectrumTransactionInfo extends TransactionInfo {
m['inputAddresses'] = inputAddresses;
m['outputAddresses'] = outputAddresses;
m['isReceivedSilentPayment'] = isReceivedSilentPayment;
m['additionalInfo'] = additionalInfo;
return m;
}
String toString() {
return 'ElectrumTransactionInfo(id: $id, height: $height, amount: $amount, fee: $fee, direction: $direction, date: $date, isPending: $isPending, isReplaced: $isReplaced, confirmations: $confirmations, to: $to, unspent: $unspents, inputAddresses: $inputAddresses, outputAddresses: $outputAddresses)';
return 'ElectrumTransactionInfo(id: $id, height: $height, amount: $amount, fee: $fee, direction: $direction, date: $date, isPending: $isPending, isReplaced: $isReplaced, confirmations: $confirmations, to: $to, unspent: $unspents, inputAddresses: $inputAddresses, outputAddresses: $outputAddresses, additionalInfo: $additionalInfo)';
}
}

View file

@ -5,6 +5,7 @@ import 'dart:isolate';
import 'package:bitcoin_base/bitcoin_base.dart';
import 'package:cw_bitcoin/bitcoin_wallet.dart';
import 'package:cw_bitcoin/litecoin_wallet.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:blockchain_utils/blockchain_utils.dart';
import 'package:collection/collection.dart';
@ -52,10 +53,9 @@ part 'electrum_wallet.g.dart';
class ElectrumWallet = ElectrumWalletBase with _$ElectrumWallet;
abstract class ElectrumWalletBase extends WalletBase<
ElectrumBalance,
ElectrumTransactionHistory,
ElectrumTransactionInfo> with Store, WalletKeysFile {
abstract class ElectrumWalletBase
extends WalletBase<ElectrumBalance, ElectrumTransactionHistory, ElectrumTransactionInfo>
with Store, WalletKeysFile {
ElectrumWalletBase({
required String password,
required WalletInfo walletInfo,
@ -71,8 +71,8 @@ abstract class ElectrumWalletBase extends WalletBase<
ElectrumBalance? initialBalance,
CryptoCurrency? currency,
this.alwaysScan,
}) : accountHD = getAccountHDWallet(
currency, network, seedBytes, xpub, walletInfo.derivationInfo),
}) : accountHD =
getAccountHDWallet(currency, network, seedBytes, xpub, walletInfo.derivationInfo),
syncStatus = NotConnectedSyncStatus(),
_password = password,
_feeRates = <int>[],
@ -107,12 +107,8 @@ abstract class ElectrumWalletBase extends WalletBase<
sharedPrefs.complete(SharedPreferences.getInstance());
}
static Bip32Slip10Secp256k1 getAccountHDWallet(
CryptoCurrency? currency,
BasedUtxoNetwork network,
Uint8List? seedBytes,
String? xpub,
DerivationInfo? derivationInfo) {
static Bip32Slip10Secp256k1 getAccountHDWallet(CryptoCurrency? currency, BasedUtxoNetwork network,
Uint8List? seedBytes, String? xpub, DerivationInfo? derivationInfo) {
if (seedBytes == null && xpub == null) {
throw Exception(
"To create a Wallet you need either a seed or an xpub. This should not happen");
@ -123,9 +119,8 @@ abstract class ElectrumWalletBase extends WalletBase<
case CryptoCurrency.btc:
case CryptoCurrency.ltc:
case CryptoCurrency.tbtc:
return Bip32Slip10Secp256k1.fromSeed(seedBytes, getKeyNetVersion(network))
.derivePath(_hardenedDerivationPath(
derivationInfo?.derivationPath ?? electrum_path))
return Bip32Slip10Secp256k1.fromSeed(seedBytes, getKeyNetVersion(network)).derivePath(
_hardenedDerivationPath(derivationInfo?.derivationPath ?? electrum_path))
as Bip32Slip10Secp256k1;
case CryptoCurrency.bch:
return bitcoinCashHDWallet(seedBytes);
@ -134,13 +129,11 @@ abstract class ElectrumWalletBase extends WalletBase<
}
}
return Bip32Slip10Secp256k1.fromExtendedKey(
xpub!, getKeyNetVersion(network));
return Bip32Slip10Secp256k1.fromExtendedKey(xpub!, getKeyNetVersion(network));
}
static Bip32Slip10Secp256k1 bitcoinCashHDWallet(Uint8List seedBytes) =>
Bip32Slip10Secp256k1.fromSeed(seedBytes).derivePath("m/44'/145'/0'")
as Bip32Slip10Secp256k1;
Bip32Slip10Secp256k1.fromSeed(seedBytes).derivePath("m/44'/145'/0'") as Bip32Slip10Secp256k1;
static int estimatedTransactionSize(int inputsCount, int outputsCounts) =>
inputsCount * 68 + outputsCounts * 34 + 10;
@ -250,7 +243,7 @@ abstract class ElectrumWalletBase extends WalletBase<
}
if (tip > walletInfo.restoreHeight) {
_setListeners(walletInfo.restoreHeight, chainTipParam: _currentChainTip);
_setListeners(walletInfo.restoreHeight, chainTipParam: currentChainTip);
}
} else {
alwaysScan = false;
@ -265,23 +258,23 @@ abstract class ElectrumWalletBase extends WalletBase<
}
}
int? _currentChainTip;
int? currentChainTip;
Future<int> getCurrentChainTip() async {
if ((_currentChainTip ?? 0) > 0) {
return _currentChainTip!;
if ((currentChainTip ?? 0) > 0) {
return currentChainTip!;
}
_currentChainTip = await electrumClient.getCurrentBlockChainTip() ?? 0;
currentChainTip = await electrumClient.getCurrentBlockChainTip() ?? 0;
return _currentChainTip!;
return currentChainTip!;
}
Future<int> getUpdatedChainTip() async {
final newTip = await electrumClient.getCurrentBlockChainTip();
if (newTip != null && newTip > (_currentChainTip ?? 0)) {
_currentChainTip = newTip;
if (newTip != null && newTip > (currentChainTip ?? 0)) {
currentChainTip = newTip;
}
return _currentChainTip ?? 0;
return currentChainTip ?? 0;
}
@override
@ -357,7 +350,7 @@ abstract class ElectrumWalletBase extends WalletBase<
isSingleScan: doSingleScan ?? false,
));
_receiveStream?.cancel();
await _receiveStream?.cancel();
_receiveStream = receivePort.listen((var message) async {
if (message is Map<String, ElectrumTransactionInfo>) {
for (final map in message.entries) {
@ -618,7 +611,7 @@ abstract class ElectrumWalletBase extends WalletBase<
bool spendsUnconfirmedTX = false;
int leftAmount = credentialsAmount;
final availableInputs = unspentCoins.where((utx) {
var availableInputs = unspentCoins.where((utx) {
if (!utx.isSending || utx.isFrozen) {
return false;
}
@ -634,6 +627,9 @@ abstract class ElectrumWalletBase extends WalletBase<
}).toList();
final unconfirmedCoins = availableInputs.where((utx) => utx.confirmations == 0).toList();
// sort the unconfirmed coins so that mweb coins are first:
availableInputs.sort((a, b) => a.bitcoinAddressRecord.type == SegwitAddresType.mweb ? -1 : 1);
for (int i = 0; i < availableInputs.length; i++) {
final utx = availableInputs[i];
if (!spendsUnconfirmedTX) spendsUnconfirmedTX = utx.confirmations == 0;
@ -652,9 +648,8 @@ abstract class ElectrumWalletBase extends WalletBase<
ECPrivate? privkey;
bool? isSilentPayment = false;
final hd = utx.bitcoinAddressRecord.isHidden
? walletAddresses.sideHd
: walletAddresses.mainHd;
final hd =
utx.bitcoinAddressRecord.isHidden ? walletAddresses.sideHd : walletAddresses.mainHd;
if (utx.bitcoinAddressRecord is BitcoinSilentPaymentAddressRecord) {
final unspentAddress = utx.bitcoinAddressRecord as BitcoinSilentPaymentAddressRecord;
@ -1233,8 +1228,7 @@ abstract class ElectrumWalletBase extends WalletBase<
}
}
void setLedgerConnection(ledger.LedgerConnection connection) =>
throw UnimplementedError();
void setLedgerConnection(ledger.LedgerConnection connection) => throw UnimplementedError();
Future<BtcTransaction> buildHardwareWalletTransaction({
required List<BitcoinBaseOutput> outputs,
@ -1593,9 +1587,7 @@ abstract class ElectrumWalletBase extends WalletBase<
final btcAddress = RegexUtils.addressTypeFromStr(addressRecord.address, network);
final privkey = generateECPrivate(
hd: addressRecord.isHidden
? walletAddresses.sideHd
: walletAddresses.mainHd,
hd: addressRecord.isHidden ? walletAddresses.sideHd : walletAddresses.mainHd,
index: addressRecord.index,
network: network);
@ -1777,8 +1769,7 @@ abstract class ElectrumWalletBase extends WalletBase<
if (height != null) {
if (time == null && height > 0) {
time = (getDateByBitcoinHeight(height).millisecondsSinceEpoch / 1000)
.round();
time = (getDateByBitcoinHeight(height).millisecondsSinceEpoch / 1000).round();
}
if (confirmations == null) {
@ -1847,6 +1838,7 @@ abstract class ElectrumWalletBase extends WalletBase<
.map((type) => fetchTransactionsForAddressType(historiesWithDetails, type)));
} else if (type == WalletType.litecoin) {
await Future.wait(LITECOIN_ADDRESS_TYPES
.where((type) => type != SegwitAddresType.mweb)
.map((type) => fetchTransactionsForAddressType(historiesWithDetails, type)));
}
@ -1958,6 +1950,20 @@ abstract class ElectrumWalletBase extends WalletBase<
// Got a new transaction fetched, add it to the transaction history
// instead of waiting all to finish, and next time it will be faster
if (this is LitecoinWallet) {
// if we have a peg out transaction with the same value
// that matches this received transaction, mark it as being from a peg out:
for (final tx2 in transactionHistory.transactions.values) {
final heightDiff = ((tx2.height ?? 0) - (tx.height ?? 0)).abs();
// this isn't a perfect matching algorithm since we don't have the right input/output information from these transaction models (the addresses are in different formats), but this should be more than good enough for now as it's extremely unlikely a user receives the EXACT same amount from 2 different sources and one of them is a peg out and the other isn't WITHIN 5 blocks of each other
if (tx2.additionalInfo["isPegOut"] == true &&
tx2.amount == tx.amount &&
heightDiff <= 5) {
tx.additionalInfo["fromPegOut"] = true;
}
}
}
transactionHistory.addOne(tx);
await transactionHistory.save();
}
@ -1984,18 +1990,28 @@ abstract class ElectrumWalletBase extends WalletBase<
if (_isTransactionUpdating) {
return;
}
await getCurrentChainTip();
currentChainTip = await getUpdatedChainTip();
bool updated = false;
transactionHistory.transactions.values.forEach((tx) {
if (tx.unspents != null &&
tx.unspents!.isNotEmpty &&
tx.height != null &&
tx.height! > 0 &&
(_currentChainTip ?? 0) > 0) {
tx.confirmations = _currentChainTip! - tx.height! + 1;
if ((tx.height ?? 0) > 0 && (currentChainTip ?? 0) > 0) {
var confirmations = currentChainTip! - tx.height! + 1;
if (confirmations < 0) {
// if our chain tip is outdated then it could lead to negative confirmations so this is just a failsafe:
confirmations = 0;
}
if (confirmations != tx.confirmations) {
updated = true;
tx.confirmations = confirmations;
transactionHistory.addOne(tx);
}
}
});
if (updated) {
await transactionHistory.save();
}
_isTransactionUpdating = true;
await fetchTransactions();
walletAddresses.updateReceiveAddresses();
@ -2043,6 +2059,8 @@ abstract class ElectrumWalletBase extends WalletBase<
library: this.runtimeType.toString(),
));
}
}, onError: (e, s) {
print("sub_listen error: $e $s");
});
}));
}
@ -2092,6 +2110,13 @@ abstract class ElectrumWalletBase extends WalletBase<
final balances = await Future.wait(balanceFutures);
if (balances.isNotEmpty && balances.first['confirmed'] == null) {
// if we got null balance responses from the server, set our connection status to lost and return our last known balance:
print("got null balance responses from the server, setting connection status to lost");
syncStatus = LostConnectionSyncStatus();
return balance[currency] ?? ElectrumBalance(confirmed: 0, unconfirmed: 0, frozen: 0);
}
for (var i = 0; i < balances.length; i++) {
final addressRecord = addresses[i];
final balance = balances[i];
@ -2197,10 +2222,10 @@ abstract class ElectrumWalletBase extends WalletBase<
Future<void> _setInitialHeight() async {
if (_chainTipUpdateSubject != null) return;
_currentChainTip = await getUpdatedChainTip();
currentChainTip = await getUpdatedChainTip();
if ((_currentChainTip == null || _currentChainTip! == 0) && walletInfo.restoreHeight == 0) {
await walletInfo.updateRestoreHeight(_currentChainTip!);
if ((currentChainTip == null || currentChainTip! == 0) && walletInfo.restoreHeight == 0) {
await walletInfo.updateRestoreHeight(currentChainTip!);
}
_chainTipUpdateSubject = electrumClient.chainTipSubscribe();
@ -2209,7 +2234,7 @@ abstract class ElectrumWalletBase extends WalletBase<
final height = int.tryParse(event['height'].toString());
if (height != null) {
_currentChainTip = height;
currentChainTip = height;
if (alwaysScan == true && syncStatus is SyncedSyncStatus) {
_setListeners(walletInfo.restoreHeight);
@ -2223,7 +2248,6 @@ abstract class ElectrumWalletBase extends WalletBase<
@action
void _onConnectionStatusChange(ConnectionStatus status) {
switch (status) {
case ConnectionStatus.connected:
if (syncStatus is NotConnectedSyncStatus ||
@ -2270,8 +2294,6 @@ abstract class ElectrumWalletBase extends WalletBase<
Timer(Duration(seconds: 5), () {
if (this.syncStatus is NotConnectedSyncStatus ||
this.syncStatus is LostConnectionSyncStatus) {
if (node == null) return;
this.electrumClient.connectToUri(
node!.uri,
useSSL: node!.useSSL ?? false,

View file

@ -9,6 +9,7 @@ import 'package:crypto/crypto.dart';
import 'package:cw_bitcoin/bitcoin_transaction_credentials.dart';
import 'package:cw_core/cake_hive.dart';
import 'package:cw_core/mweb_utxo.dart';
import 'package:cw_core/node.dart';
import 'package:cw_mweb/mwebd.pbgrpc.dart';
import 'package:fixnum/fixnum.dart';
import 'package:bip39/bip39.dart' as bip39;
@ -47,6 +48,7 @@ import 'package:cw_mweb/cw_mweb.dart';
import 'package:bitcoin_base/src/crypto/keypair/sign_utils.dart';
import 'package:pointycastle/ecc/api.dart';
import 'package:pointycastle/ecc/curves/secp256k1.dart';
import 'package:shared_preferences/shared_preferences.dart';
part 'litecoin_wallet.g.dart';
@ -85,8 +87,8 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
alwaysScan: alwaysScan,
) {
if (seedBytes != null) {
mwebHd = Bip32Slip10Secp256k1.fromSeed(seedBytes).derivePath(
"m/1000'") as Bip32Slip10Secp256k1;
mwebHd =
Bip32Slip10Secp256k1.fromSeed(seedBytes).derivePath("m/1000'") as Bip32Slip10Secp256k1;
mwebEnabled = alwaysScan ?? false;
} else {
mwebHd = null;
@ -287,6 +289,16 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
await (walletAddresses as LitecoinWalletAddresses).ensureMwebAddressUpToIndexExists(1020);
}
@action
@override
Future<void> connectToNode({required Node node}) async {
await super.connectToNode(node: node);
final prefs = await SharedPreferences.getInstance();
final mwebNodeUri = prefs.getString("mwebNodeUri") ?? "ltc-electrum.cakewallet.com:9333";
await CwMweb.setNodeUriOverride(mwebNodeUri);
}
@action
@override
Future<void> startSync() async {
@ -349,6 +361,9 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
return;
}
// update the current chain tip so that confirmation calculations are accurate:
currentChainTip = nodeHeight;
final resp = await CwMweb.status(StatusRequest());
try {
@ -361,22 +376,46 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
} else if (resp.mwebUtxosHeight < nodeHeight) {
mwebSyncStatus = SyncingSyncStatus(1, 0.999);
} else {
bool confirmationsUpdated = false;
if (resp.mwebUtxosHeight > walletInfo.restoreHeight) {
await walletInfo.updateRestoreHeight(resp.mwebUtxosHeight);
await checkMwebUtxosSpent();
// update the confirmations for each transaction:
for (final transaction in transactionHistory.transactions.values) {
if (transaction.isPending) continue;
int txHeight = transaction.height ?? resp.mwebUtxosHeight;
final confirmations = (resp.mwebUtxosHeight - txHeight) + 1;
if (transaction.confirmations == confirmations) continue;
if (transaction.confirmations == 0) {
updateBalance();
for (final tx in transactionHistory.transactions.values) {
if (tx.height == null || tx.height == 0) {
// update with first confirmation on next block since it hasn't been confirmed yet:
tx.height = resp.mwebUtxosHeight;
continue;
}
transaction.confirmations = confirmations;
transactionHistory.addOne(transaction);
final confirmations = (resp.mwebUtxosHeight - tx.height!) + 1;
// if the confirmations haven't changed, skip updating:
if (tx.confirmations == confirmations) continue;
// if an outgoing tx is now confirmed, delete the utxo from the box (delete the unspent coin):
if (confirmations >= 2 &&
tx.direction == TransactionDirection.outgoing &&
tx.unspents != null) {
for (var coin in tx.unspents!) {
final utxo = mwebUtxosBox.get(coin.address);
if (utxo != null) {
print("deleting utxo ${coin.address} @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
await mwebUtxosBox.delete(coin.address);
}
}
}
tx.confirmations = confirmations;
tx.isPending = false;
transactionHistory.addOne(tx);
confirmationsUpdated = true;
}
if (confirmationsUpdated) {
await transactionHistory.save();
await updateTransactions();
}
}
// prevent unnecessary reaction triggers:
@ -501,13 +540,12 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
outputAddresses: [utxo.outputId],
isReplaced: false,
);
}
// don't update the confirmations if the tx is updated by electrum:
if (tx.confirmations == 0 || utxo.height != 0) {
} else {
if (tx.confirmations != confirmations || tx.height != utxo.height) {
tx.height = utxo.height;
tx.isPending = utxo.height == 0;
tx.confirmations = confirmations;
tx.isPending = utxo.height == 0;
}
}
bool isNew = transactionHistory.transactions[tx.id] == null;
@ -557,7 +595,8 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
if (responseStream == null) {
throw Exception("failed to get utxos stream!");
}
_utxoStream = responseStream.listen((Utxo sUtxo) async {
_utxoStream = responseStream.listen(
(Utxo sUtxo) async {
// we're processing utxos, so our balance could still be innacurate:
if (mwebSyncStatus is! SyncronizingSyncStatus && mwebSyncStatus is! SyncingSyncStatus) {
mwebSyncStatus = SyncronizingSyncStatus();
@ -603,10 +642,41 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
await mwebUtxosBox.put(utxo.outputId, utxo);
await handleIncoming(utxo);
});
},
onError: (error) {
print("error in utxo stream: $error");
mwebSyncStatus = FailedSyncStatus(error: error.toString());
},
cancelOnError: true,
);
}
Future<void> deleteSpentUtxos() async {
print("deleteSpentUtxos() called!");
final chainHeight = await electrumClient.getCurrentBlockChainTip();
final status = await CwMweb.status(StatusRequest());
if (chainHeight == null || status.blockHeaderHeight != chainHeight) return;
if (status.mwebUtxosHeight != chainHeight) return; // we aren't synced
// delete any spent utxos with >= 2 confirmations:
final spentOutputIds = mwebUtxosBox.values
.where((utxo) => utxo.spent && (chainHeight - utxo.height) >= 2)
.map((utxo) => utxo.outputId)
.toList();
if (spentOutputIds.isEmpty) return;
final resp = await CwMweb.spent(SpentRequest(outputId: spentOutputIds));
final spent = resp.outputId;
if (spent.isEmpty) return;
for (final outputId in spent) {
await mwebUtxosBox.delete(outputId);
}
}
Future<void> checkMwebUtxosSpent() async {
print("checkMwebUtxosSpent() called!");
if (!mwebEnabled) {
return;
}
@ -620,15 +690,17 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
updatedAny = await isConfirmed(tx) || updatedAny;
}
await deleteSpentUtxos();
// get output ids of all the mweb utxos that have > 0 height:
final outputIds =
mwebUtxosBox.values.where((utxo) => utxo.height > 0).map((utxo) => utxo.outputId).toList();
final outputIds = mwebUtxosBox.values
.where((utxo) => utxo.height > 0 && !utxo.spent)
.map((utxo) => utxo.outputId)
.toList();
final resp = await CwMweb.spent(SpentRequest(outputId: outputIds));
final spent = resp.outputId;
if (spent.isEmpty) {
return;
}
if (spent.isEmpty) return;
final status = await CwMweb.status(StatusRequest());
final height = await electrumClient.getCurrentBlockChainTip();
@ -739,7 +811,7 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
mwebUtxosBox.keys.forEach((dynamic oId) {
final String outputId = oId as String;
final utxo = mwebUtxosBox.get(outputId);
if (utxo == null) {
if (utxo == null || utxo.spent) {
return;
}
if (utxo.address.isEmpty) {
@ -789,15 +861,23 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
int unconfirmedMweb = 0;
try {
mwebUtxosBox.values.forEach((utxo) {
if (utxo.height > 0) {
bool isConfirmed = utxo.height > 0;
print(
"utxo: ${isConfirmed ? "confirmed" : "unconfirmed"} ${utxo.spent ? "spent" : "unspent"} ${utxo.outputId} ${utxo.height} ${utxo.value}");
if (isConfirmed) {
confirmedMweb += utxo.value.toInt();
} else {
}
if (isConfirmed && utxo.spent) {
unconfirmedMweb -= utxo.value.toInt();
}
if (!isConfirmed && !utxo.spent) {
unconfirmedMweb += utxo.value.toInt();
}
});
if (unconfirmedMweb > 0) {
unconfirmedMweb = -1 * (confirmedMweb - unconfirmedMweb);
}
} catch (_) {}
for (var addressRecord in walletAddresses.allAddresses) {
@ -829,7 +909,6 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
// update the txCount for each address using the tx history, since we can't rely on mwebd
// to have an accurate count, we should just keep it in sync with what we know from the tx history:
for (final tx in transactionHistory.transactions.values) {
// if (tx.isPending) continue;
if (tx.inputAddresses == null || tx.outputAddresses == null) {
continue;
}
@ -908,7 +987,26 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
// https://github.com/ltcmweb/mwebd?tab=readme-ov-file#fee-estimation
final preOutputSum =
outputs.fold<BigInt>(BigInt.zero, (acc, output) => acc + output.toOutput.amount);
final fee = utxos.sumOfUtxosValue() - preOutputSum;
var fee = utxos.sumOfUtxosValue() - preOutputSum;
// determines if the fee is correct:
BigInt _sumOutputAmounts(List<TxOutput> outputs) {
BigInt sum = BigInt.zero;
for (final e in outputs) {
sum += e.amount;
}
return sum;
}
final sum1 = _sumOutputAmounts(outputs.map((e) => e.toOutput).toList()) + fee;
final sum2 = utxos.sumOfUtxosValue();
if (sum1 != sum2) {
print("@@@@@ WE HAD TO ADJUST THE FEE! @@@@@@@@");
final diff = sum2 - sum1;
// add the difference to the fee (abs value):
fee += diff.abs();
}
final txb =
BitcoinTransactionBuilder(utxos: utxos, outputs: outputs, fee: fee, network: network);
final resp = await CwMweb.create(CreateRequest(
@ -949,8 +1047,7 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
if (!mwebEnabled) {
tx.changeAddressOverride =
(await (walletAddresses as LitecoinWalletAddresses)
.getChangeAddress(isPegIn: false))
(await (walletAddresses as LitecoinWalletAddresses).getChangeAddress(isPegIn: false))
.address;
return tx;
}
@ -969,15 +1066,25 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
bool hasMwebInput = false;
bool hasMwebOutput = false;
bool hasRegularOutput = false;
for (final output in transactionCredentials.outputs) {
if (output.extractedAddress?.toLowerCase().contains("mweb") ?? false) {
final address = output.address.toLowerCase();
final extractedAddress = output.extractedAddress?.toLowerCase();
if (address.contains("mweb")) {
hasMwebOutput = true;
break;
}
if (output.address.toLowerCase().contains("mweb")) {
if (!address.contains("mweb")) {
hasRegularOutput = true;
}
if (extractedAddress != null && extractedAddress.isNotEmpty) {
if (extractedAddress.contains("mweb")) {
hasMwebOutput = true;
break;
}
if (!extractedAddress.contains("mweb")) {
hasRegularOutput = true;
}
}
}
@ -989,9 +1096,9 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
}
bool isPegIn = !hasMwebInput && hasMwebOutput;
bool isPegOut = hasMwebInput && hasRegularOutput;
bool isRegular = !hasMwebInput && !hasMwebOutput;
tx.changeAddressOverride =
(await (walletAddresses as LitecoinWalletAddresses)
tx.changeAddressOverride = (await (walletAddresses as LitecoinWalletAddresses)
.getChangeAddress(isPegIn: isPegIn || isRegular))
.address;
if (!hasMwebInput && !hasMwebOutput) {
@ -1046,8 +1153,11 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
final addresses = <String>{};
transaction.inputAddresses?.forEach((id) async {
final utxo = mwebUtxosBox.get(id);
await mwebUtxosBox.delete(id); // gets deleted in checkMwebUtxosSpent
// await mwebUtxosBox.delete(id); // gets deleted in checkMwebUtxosSpent
if (utxo == null) return;
// mark utxo as spent so we add it to the unconfirmed balance (as negative):
utxo.spent = true;
await mwebUtxosBox.put(id, utxo);
final addressRecord = walletAddresses.allAddresses
.firstWhere((addressRecord) => addressRecord.address == utxo.address);
if (!addresses.contains(utxo.address)) {
@ -1056,7 +1166,9 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
addressRecord.balance -= utxo.value.toInt();
});
transaction.inputAddresses?.addAll(addresses);
print("isPegIn: $isPegIn, isPegOut: $isPegOut");
transaction.additionalInfo["isPegIn"] = isPegIn;
transaction.additionalInfo["isPegOut"] = isPegOut;
transactionHistory.addOne(transaction);
await updateUnspent();
await updateBalance();
@ -1240,8 +1352,8 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
@override
void setLedgerConnection(LedgerConnection connection) {
_ledgerConnection = connection;
_litecoinLedgerApp =
LitecoinLedgerApp(_ledgerConnection!, derivationPath: walletInfo.derivationInfo!.derivationPath!);
_litecoinLedgerApp = LitecoinLedgerApp(_ledgerConnection!,
derivationPath: walletInfo.derivationInfo!.derivationPath!);
}
@override
@ -1277,19 +1389,17 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
if (maybeChangePath != null) changePath ??= maybeChangePath.derivationPath;
}
final rawHex = await _litecoinLedgerApp!.createTransaction(
inputs: readyInputs,
outputs: outputs
.map((e) => TransactionOutput.fromBigInt(
(e as BitcoinOutput).value, Uint8List.fromList(e.address.toScriptPubKey().toBytes())))
.map((e) => TransactionOutput.fromBigInt((e as BitcoinOutput).value,
Uint8List.fromList(e.address.toScriptPubKey().toBytes())))
.toList(),
changePath: changePath,
sigHashType: 0x01,
additionals: ["bech32"],
isSegWit: true,
useTrustedInputForSegwit: true
);
useTrustedInputForSegwit: true);
return BtcTransaction.fromRaw(rawHex);
}

Some files were not shown because too many files have changed in this diff Show more