CW 781 replace all print statements with printV (#1733)

* replace all print statements with printV

* restore backup error message

* missing print statements, error fixes

* Update cw_core/lib/utils/print_verbose.dart [skip ci]

* Update cw_core/lib/utils/print_verbose.dart [skip ci]

* CW-846: Correctly display balance (#1848)

* Correctly display balance even with frozen coins

* remove package= from AndroidMainfest.xml

* update namespace

* print -> printV

---------

Co-authored-by: Omar Hatem <omarh.ismail1@gmail.com>
This commit is contained in:
cyan 2024-12-09 12:23:59 -06:00 committed by GitHub
parent c74194abf4
commit c78662fbfe
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
124 changed files with 578 additions and 343 deletions

View file

@ -6,6 +6,7 @@ import 'package:cw_bitcoin/utils.dart';
import 'package:cw_core/hardware/hardware_account_data.dart';
import 'package:ledger_bitcoin/ledger_bitcoin.dart';
import 'package:ledger_flutter_plus/ledger_flutter_plus.dart';
import 'package:cw_core/utils/print_verbose.dart';
class BitcoinHardwareWalletService {
BitcoinHardwareWalletService(this.ledgerConnection);

View file

@ -4,6 +4,7 @@ import 'dart:io';
import 'dart:typed_data';
import 'package:bitcoin_base/bitcoin_base.dart';
import 'package:cw_bitcoin/bitcoin_amount_format.dart';
import 'package:cw_core/utils/print_verbose.dart';
import 'package:flutter/foundation.dart';
import 'package:rxdart/rxdart.dart';
@ -117,17 +118,17 @@ class ElectrumClient {
_parseResponse(message);
}
} catch (e) {
print("socket.listen: $e");
printV("socket.listen: $e");
}
},
onError: (Object error) {
final errorMsg = error.toString();
print(errorMsg);
printV(errorMsg);
unterminatedString = '';
socket = null;
},
onDone: () {
print("SOCKET CLOSED!!!!!");
printV("SOCKET CLOSED!!!!!");
unterminatedString = '';
try {
if (host == socket?.address.host || socket == null) {
@ -136,7 +137,7 @@ class ElectrumClient {
socket = null;
}
} catch (e) {
print("onDone: $e");
printV("onDone: $e");
}
},
cancelOnError: true,
@ -181,7 +182,7 @@ class ElectrumClient {
unterminatedString = '';
}
} catch (e) {
print("parse $e");
printV("parse $e");
}
}
@ -403,7 +404,7 @@ class ElectrumClient {
} on RequestFailedTimeoutException catch (_) {
return null;
} catch (e) {
print("getCurrentBlockChainTip: ${e.toString()}");
printV("getCurrentBlockChainTip: ${e.toString()}");
return null;
}
}
@ -434,7 +435,7 @@ class ElectrumClient {
return subscription;
} catch (e) {
print("subscribe $e");
printV("subscribe $e");
return null;
}
}
@ -473,7 +474,7 @@ class ElectrumClient {
return completer.future;
} catch (e) {
print("callWithTimeout $e");
printV("callWithTimeout $e");
rethrow;
}
}

View file

@ -5,6 +5,7 @@ import 'package:cw_bitcoin/electrum_transaction_info.dart';
import 'package:cw_core/pathForWallet.dart';
import 'package:cw_core/transaction_history.dart';
import 'package:cw_core/utils/file.dart';
import 'package:cw_core/utils/print_verbose.dart';
import 'package:cw_core/wallet_info.dart';
import 'package:mobx/mobx.dart';
import 'package:cw_core/transaction_history.dart';
@ -51,7 +52,7 @@ abstract class ElectrumTransactionHistoryBase
final data = json.encode({'height': _height, 'transactions': txjson});
await encryptionFileUtils.write(path: path, password: _password, data: data);
} catch (e) {
print('Error while save bitcoin transaction history: ${e.toString()}');
printV('Error while save bitcoin transaction history: ${e.toString()}');
}
}
@ -88,7 +89,7 @@ abstract class ElectrumTransactionHistoryBase
_height = content['height'] as int;
} catch (e) {
print(e);
printV(e);
}
}

View file

@ -4,6 +4,8 @@ import 'dart:io';
import 'dart:isolate';
import 'package:bitcoin_base/bitcoin_base.dart';
import 'package:cw_bitcoin/litecoin_wallet_addresses.dart';
import 'package:cw_core/utils/print_verbose.dart';
import 'package:cw_bitcoin/bitcoin_wallet.dart';
import 'package:cw_bitcoin/litecoin_wallet.dart';
import 'package:shared_preferences/shared_preferences.dart';
@ -479,8 +481,8 @@ abstract class ElectrumWalletBase
syncStatus = SyncedSyncStatus();
}
} catch (e, stacktrace) {
print(stacktrace);
print("startSync $e");
printV(stacktrace);
printV("startSync $e");
syncStatus = FailedSyncStatus();
}
}
@ -506,7 +508,7 @@ abstract class ElectrumWalletBase
_feeRates = [slowFee, mediumFee, fastFee];
return;
} catch (e) {
print(e);
printV(e);
}
}
@ -588,8 +590,8 @@ abstract class ElectrumWalletBase
await electrumClient.connectToUri(node.uri, useSSL: node.useSSL);
} catch (e, stacktrace) {
print(stacktrace);
print("connectToNode $e");
printV(stacktrace);
printV("connectToNode $e");
syncStatus = FailedSyncStatus();
}
}
@ -1492,7 +1494,7 @@ abstract class ElectrumWalletBase
await unspentCoinsInfo.deleteAll(keys);
}
} catch (e) {
print("refreshUnspentCoinsInfo $e");
printV("refreshUnspentCoinsInfo $e");
}
}
@ -1935,7 +1937,7 @@ abstract class ElectrumWalletBase
return historiesWithDetails;
} catch (e) {
print("fetchTransactions $e");
printV("fetchTransactions $e");
return {};
}
}
@ -2059,7 +2061,7 @@ abstract class ElectrumWalletBase
}
Future<void> updateTransactions() async {
print("updateTransactions() called!");
printV("updateTransactions() called!");
try {
if (_isTransactionUpdating) {
return;
@ -2091,8 +2093,8 @@ abstract class ElectrumWalletBase
walletAddresses.updateReceiveAddresses();
_isTransactionUpdating = false;
} catch (e, stacktrace) {
print(stacktrace);
print(e);
printV(stacktrace);
printV(e);
_isTransactionUpdating = false;
}
}
@ -2110,13 +2112,13 @@ abstract class ElectrumWalletBase
try {
await _scripthashesUpdateSubject[sh]?.close();
} catch (e) {
print("failed to close: $e");
printV("failed to close: $e");
}
}
try {
_scripthashesUpdateSubject[sh] = await electrumClient.scripthashUpdate(sh);
} catch (e) {
print("failed scripthashUpdate: $e");
printV("failed scripthashUpdate: $e");
}
_scripthashesUpdateSubject[sh]?.listen((event) async {
try {
@ -2126,7 +2128,7 @@ abstract class ElectrumWalletBase
await _fetchAddressHistory(address, await getCurrentChainTip());
} catch (e, s) {
print("sub error: $e");
printV("sub error: $e");
_onError?.call(FlutterErrorDetails(
exception: e,
stack: s,
@ -2134,7 +2136,7 @@ abstract class ElectrumWalletBase
));
}
}, onError: (e, s) {
print("sub_listen error: $e $s");
printV("sub_listen error: $e $s");
});
}));
}
@ -2186,7 +2188,7 @@ abstract class ElectrumWalletBase
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");
printV("got null balance responses from the server, setting connection status to lost");
syncStatus = LostConnectionSyncStatus();
return balance[currency] ?? ElectrumBalance(confirmed: 0, unconfirmed: 0, frozen: 0);
}
@ -2213,7 +2215,7 @@ abstract class ElectrumWalletBase
}
Future<void> updateBalance() async {
print("updateBalance() called!");
printV("updateBalance() called!");
balance[currency] = await fetchBalances();
await save();
}
@ -2353,7 +2355,7 @@ abstract class ElectrumWalletBase
}
void _syncStatusReaction(SyncStatus syncStatus) async {
print("SYNC_STATUS_CHANGE: ${syncStatus}");
printV("SYNC_STATUS_CHANGE: ${syncStatus}");
if (syncStatus is SyncingSyncStatus) {
return;
}

View file

@ -3,6 +3,8 @@ import 'dart:io' show Platform;
import 'package:bitcoin_base/bitcoin_base.dart';
import 'package:blockchain_utils/blockchain_utils.dart';
import 'package:cw_bitcoin/bitcoin_address_record.dart';
import 'package:cw_bitcoin/electrum_wallet.dart';
import 'package:cw_core/utils/print_verbose.dart';
import 'package:cw_bitcoin/bitcoin_unspent.dart';
import 'package:cw_core/wallet_addresses.dart';
import 'package:cw_core/wallet_info.dart';
@ -193,7 +195,7 @@ abstract class ElectrumWalletAddressesBase extends WalletAddresses with Store {
receiveAddresses.remove(addressRecord);
receiveAddresses.insert(0, addressRecord);
} catch (e) {
print("ElectrumWalletAddressBase: set address ($addr): $e");
printV("ElectrumWalletAddressBase: set address ($addr): $e");
}
}
@ -483,7 +485,7 @@ abstract class ElectrumWalletAddressesBase extends WalletAddresses with Store {
await saveAddressesInBox();
} catch (e) {
print("updateAddresses $e");
printV("updateAddresses $e");
}
}

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/utils/print_verbose.dart';
import 'package:cw_core/node.dart';
import 'package:cw_mweb/mwebd.pbgrpc.dart';
import 'package:fixnum/fixnum.dart';
@ -283,7 +284,7 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
}
Future<void> waitForMwebAddresses() async {
print("waitForMwebAddresses() called!");
printV("waitForMwebAddresses() called!");
// ensure that we have the full 1000 mweb addresses generated before continuing:
// should no longer be needed, but leaving here just in case
await (walletAddresses as LitecoinWalletAddresses).ensureMwebAddressUpToIndexExists(1020);
@ -302,8 +303,8 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
@action
@override
Future<void> startSync() async {
print("startSync() called!");
print("STARTING SYNC - MWEB ENABLED: $mwebEnabled");
printV("startSync() called!");
printV("STARTING SYNC - MWEB ENABLED: $mwebEnabled");
if (!mwebEnabled) {
try {
// in case we're switching from a litecoin wallet that had mweb enabled
@ -317,33 +318,33 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
return;
}
print("STARTING SYNC - MWEB ENABLED: $mwebEnabled");
printV("STARTING SYNC - MWEB ENABLED: $mwebEnabled");
_syncTimer?.cancel();
try {
mwebSyncStatus = SyncronizingSyncStatus();
try {
await subscribeForUpdates();
} catch (e) {
print("failed to subcribe for updates: $e");
printV("failed to subcribe for updates: $e");
}
updateFeeRates();
_feeRatesTimer?.cancel();
_feeRatesTimer =
Timer.periodic(const Duration(minutes: 1), (timer) async => await updateFeeRates());
print("START SYNC FUNCS");
printV("START SYNC FUNCS");
await waitForMwebAddresses();
await processMwebUtxos();
await updateTransactions();
await updateUnspent();
await updateBalance();
print("DONE SYNC FUNCS");
} catch (e, s) {
print("mweb sync failed: $e $s");
mwebSyncStatus = FailedSyncStatus(error: "mweb sync failed: $e");
} catch (e) {
printV("failed to start mweb sync: $e");
syncStatus = FailedSyncStatus();
return;
}
_syncTimer?.cancel();
_syncTimer = Timer.periodic(const Duration(milliseconds: 3000), (timer) async {
if (mwebSyncStatus is FailedSyncStatus) {
_syncTimer?.cancel();
@ -401,7 +402,7 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
for (var coin in tx.unspents!) {
final utxo = mwebUtxosBox.get(coin.address);
if (utxo != null) {
print("deleting utxo ${coin.address} @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
printV("deleting utxo ${coin.address} @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
await mwebUtxosBox.delete(coin.address);
}
}
@ -428,7 +429,7 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
return;
}
} catch (e) {
print("error syncing: $e");
printV("error syncing: $e");
mwebSyncStatus = FailedSyncStatus(error: e.toString());
}
});
@ -437,12 +438,12 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
@action
@override
Future<void> stopSync() async {
print("stopSync() called!");
printV("stopSync() called!");
_syncTimer?.cancel();
_utxoStream?.cancel();
_feeRatesTimer?.cancel();
await CwMweb.stop();
print("stopped syncing!");
printV("stopped syncing!");
}
Future<void> initMwebUtxosBox() async {
@ -514,7 +515,7 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
}
Future<void> handleIncoming(MwebUtxo utxo) async {
print("handleIncoming() called!");
printV("handleIncoming() called!");
final status = await CwMweb.status(StatusRequest());
var date = DateTime.now();
var confirmations = 0;
@ -559,7 +560,7 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
final addressRecord = walletAddresses.allAddresses
.firstWhereOrNull((addressRecord) => addressRecord.address == utxo.address);
if (addressRecord == null) {
print("we don't have this address in the wallet! ${utxo.address}");
printV("we don't have this address in the wallet! ${utxo.address}");
return;
}
@ -580,13 +581,13 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
}
Future<void> processMwebUtxos() async {
print("processMwebUtxos() called!");
printV("processMwebUtxos() called!");
if (!mwebEnabled) {
return;
}
int restoreHeight = walletInfo.restoreHeight;
print("SCANNING FROM HEIGHT: $restoreHeight");
printV("SCANNING FROM HEIGHT: $restoreHeight");
final req = UtxosRequest(scanSecret: scanSecret, fromHeight: restoreHeight);
// process new utxos as they come in:
@ -621,7 +622,7 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
// but do update the utxo height if it's somehow different:
final existingUtxo = mwebUtxosBox.get(utxo.outputId);
if (existingUtxo!.height != utxo.height) {
print(
printV(
"updating utxo height for $utxo.outputId: ${existingUtxo.height} -> ${utxo.height}");
existingUtxo.height = utxo.height;
await mwebUtxosBox.put(utxo.outputId, existingUtxo);
@ -644,7 +645,7 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
await handleIncoming(utxo);
},
onError: (error) {
print("error in utxo stream: $error");
printV("error in utxo stream: $error");
mwebSyncStatus = FailedSyncStatus(error: error.toString());
},
cancelOnError: true,
@ -652,7 +653,7 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
}
Future<void> deleteSpentUtxos() async {
print("deleteSpentUtxos() called!");
printV("deleteSpentUtxos() called!");
final chainHeight = await electrumClient.getCurrentBlockChainTip();
final status = await CwMweb.status(StatusRequest());
if (chainHeight == null || status.blockHeaderHeight != chainHeight) return;
@ -676,7 +677,7 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
}
Future<void> checkMwebUtxosSpent() async {
print("checkMwebUtxosSpent() called!");
printV("checkMwebUtxosSpent() called!");
if (!mwebEnabled) {
return;
}
@ -791,7 +792,7 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
}
Future<void> updateUnspent() async {
print("updateUnspent() called!");
printV("updateUnspent() called!");
await checkMwebUtxosSpent();
await updateAllUnspents();
}
@ -822,7 +823,7 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
.firstWhereOrNull((addressRecord) => addressRecord.address == utxo.address);
if (addressRecord == null) {
print("utxo contains an address that is not in the wallet: ${utxo.address}");
printV("utxo contains an address that is not in the wallet: ${utxo.address}");
return;
}
final unspent = BitcoinUnspent(
@ -863,7 +864,7 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
mwebUtxosBox.values.forEach((utxo) {
bool isConfirmed = utxo.height > 0;
print(
printV(
"utxo: ${isConfirmed ? "confirmed" : "unconfirmed"} ${utxo.spent ? "spent" : "unspent"} ${utxo.outputId} ${utxo.height} ${utxo.value}");
if (isConfirmed) {
@ -1001,7 +1002,7 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
final sum1 = _sumOutputAmounts(outputs.map((e) => e.toOutput).toList()) + fee;
final sum2 = utxos.sumOfUtxosValue();
if (sum1 != sum2) {
print("@@@@@ WE HAD TO ADJUST THE FEE! @@@@@@@@");
printV("@@@@@ WE HAD TO ADJUST THE FEE! @@@@@@@@");
final diff = sum2 - sum1;
// add the difference to the fee (abs value):
fee += diff.abs();
@ -1166,7 +1167,7 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
addressRecord.balance -= utxo.value.toInt();
});
transaction.inputAddresses?.addAll(addresses);
print("isPegIn: $isPegIn, isPegOut: $isPegOut");
printV("isPegIn: $isPegIn, isPegOut: $isPegOut");
transaction.additionalInfo["isPegIn"] = isPegIn;
transaction.additionalInfo["isPegOut"] = isPegOut;
transactionHistory.addOne(transaction);
@ -1174,10 +1175,10 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
await updateBalance();
});
} catch (e, s) {
print(e);
print(s);
printV(e);
printV(s);
if (e.toString().contains("commit failed")) {
print(e);
printV(e);
throw Exception("Transaction commit failed (no peers responded), please try again.");
}
rethrow;

View file

@ -9,6 +9,7 @@ import 'package:cw_bitcoin/bitcoin_unspent.dart';
import 'package:cw_bitcoin/electrum_wallet.dart';
import 'package:cw_bitcoin/utils.dart';
import 'package:cw_bitcoin/electrum_wallet_addresses.dart';
import 'package:cw_core/utils/print_verbose.dart';
import 'package:cw_core/wallet_info.dart';
import 'package:cw_mweb/cw_mweb.dart';
import 'package:flutter/foundation.dart';
@ -35,7 +36,7 @@ abstract class LitecoinWalletAddressesBase extends ElectrumWalletAddresses with
for (int i = 0; i < mwebAddresses.length; i++) {
mwebAddrs.add(mwebAddresses[i].address);
}
print("initialized with ${mwebAddrs.length} mweb addresses");
printV("initialized with ${mwebAddrs.length} mweb addresses");
}
final Bip32Slip10Secp256k1? mwebHd;
@ -73,25 +74,25 @@ abstract class LitecoinWalletAddressesBase extends ElectrumWalletAddresses with
}
while (generating) {
print("generating.....");
printV("generating.....");
// this function was called multiple times in multiple places:
await Future.delayed(const Duration(milliseconds: 100));
}
print("Generating MWEB addresses up to index $index");
printV("Generating MWEB addresses up to index $index");
generating = true;
try {
while (mwebAddrs.length <= (index + 1)) {
final addresses =
await CwMweb.addresses(scan, spend, mwebAddrs.length, mwebAddrs.length + 50);
print("generated up to index ${mwebAddrs.length}");
printV("generated up to index ${mwebAddrs.length}");
// sleep for a bit to avoid making the main thread unresponsive:
await Future.delayed(Duration(milliseconds: 200));
mwebAddrs.addAll(addresses!);
}
} catch (_) {}
generating = false;
print("Done generating MWEB addresses len: ${mwebAddrs.length}");
printV("Done generating MWEB addresses len: ${mwebAddrs.length}");
// ensure mweb addresses are up to date:
// This is the Case if the Litecoin Wallet is a hardware Wallet
@ -109,7 +110,7 @@ abstract class LitecoinWalletAddressesBase extends ElectrumWalletAddresses with
))
.toList();
addMwebAddresses(addressRecords);
print("set ${addressRecords.length} mweb addresses");
printV("set ${addressRecords.length} mweb addresses");
}
}

View file

@ -2,6 +2,7 @@ import 'dart:typed_data';
import 'package:bitcoin_base/bitcoin_base.dart';
import 'package:convert/convert.dart';
import 'package:cw_core/utils/print_verbose.dart';
import 'package:ledger_bitcoin/psbt.dart';
class PSBTTransactionBuild {
@ -16,6 +17,10 @@ class PSBTTransactionBuild {
for (var i = 0; i < inputs.length; i++) {
final input = inputs[i];
printV(input.utxo.isP2tr());
printV(input.utxo.isSegwit());
printV(input.utxo.isP2shSegwit());
psbt.setInputPreviousTxId(i, Uint8List.fromList(hex.decode(input.utxo.txHash).reversed.toList()));
psbt.setInputOutputIndex(i, input.utxo.vout);
psbt.setInputSequence(i, enableRBF ? 0x1 : 0xffffffff);