error handling

This commit is contained in:
Matthew Fosse 2024-10-28 10:42:23 -07:00
parent f14dfc6aea
commit 767b507a8d
2 changed files with 67 additions and 73 deletions

View file

@ -52,10 +52,9 @@ part 'electrum_wallet.g.dart';
class ElectrumWallet = ElectrumWalletBase with _$ElectrumWallet; class ElectrumWallet = ElectrumWalletBase with _$ElectrumWallet;
abstract class ElectrumWalletBase extends WalletBase< abstract class ElectrumWalletBase
ElectrumBalance, extends WalletBase<ElectrumBalance, ElectrumTransactionHistory, ElectrumTransactionInfo>
ElectrumTransactionHistory, with Store, WalletKeysFile {
ElectrumTransactionInfo> with Store, WalletKeysFile {
ElectrumWalletBase({ ElectrumWalletBase({
required String password, required String password,
required WalletInfo walletInfo, required WalletInfo walletInfo,
@ -71,8 +70,8 @@ abstract class ElectrumWalletBase extends WalletBase<
ElectrumBalance? initialBalance, ElectrumBalance? initialBalance,
CryptoCurrency? currency, CryptoCurrency? currency,
this.alwaysScan, this.alwaysScan,
}) : accountHD = getAccountHDWallet( }) : accountHD =
currency, network, seedBytes, xpub, walletInfo.derivationInfo), getAccountHDWallet(currency, network, seedBytes, xpub, walletInfo.derivationInfo),
syncStatus = NotConnectedSyncStatus(), syncStatus = NotConnectedSyncStatus(),
_password = password, _password = password,
_feeRates = <int>[], _feeRates = <int>[],
@ -107,12 +106,8 @@ abstract class ElectrumWalletBase extends WalletBase<
sharedPrefs.complete(SharedPreferences.getInstance()); sharedPrefs.complete(SharedPreferences.getInstance());
} }
static Bip32Slip10Secp256k1 getAccountHDWallet( static Bip32Slip10Secp256k1 getAccountHDWallet(CryptoCurrency? currency, BasedUtxoNetwork network,
CryptoCurrency? currency, Uint8List? seedBytes, String? xpub, DerivationInfo? derivationInfo) {
BasedUtxoNetwork network,
Uint8List? seedBytes,
String? xpub,
DerivationInfo? derivationInfo) {
if (seedBytes == null && xpub == null) { if (seedBytes == null && xpub == null) {
throw Exception( throw Exception(
"To create a Wallet you need either a seed or an xpub. This should not happen"); "To create a Wallet you need either a seed or an xpub. This should not happen");
@ -123,9 +118,8 @@ abstract class ElectrumWalletBase extends WalletBase<
case CryptoCurrency.btc: case CryptoCurrency.btc:
case CryptoCurrency.ltc: case CryptoCurrency.ltc:
case CryptoCurrency.tbtc: case CryptoCurrency.tbtc:
return Bip32Slip10Secp256k1.fromSeed(seedBytes, getKeyNetVersion(network)) return Bip32Slip10Secp256k1.fromSeed(seedBytes, getKeyNetVersion(network)).derivePath(
.derivePath(_hardenedDerivationPath( _hardenedDerivationPath(derivationInfo?.derivationPath ?? electrum_path))
derivationInfo?.derivationPath ?? electrum_path))
as Bip32Slip10Secp256k1; as Bip32Slip10Secp256k1;
case CryptoCurrency.bch: case CryptoCurrency.bch:
return bitcoinCashHDWallet(seedBytes); return bitcoinCashHDWallet(seedBytes);
@ -134,13 +128,11 @@ abstract class ElectrumWalletBase extends WalletBase<
} }
} }
return Bip32Slip10Secp256k1.fromExtendedKey( return Bip32Slip10Secp256k1.fromExtendedKey(xpub!, getKeyNetVersion(network));
xpub!, getKeyNetVersion(network));
} }
static Bip32Slip10Secp256k1 bitcoinCashHDWallet(Uint8List seedBytes) => static Bip32Slip10Secp256k1 bitcoinCashHDWallet(Uint8List seedBytes) =>
Bip32Slip10Secp256k1.fromSeed(seedBytes).derivePath("m/44'/145'/0'") Bip32Slip10Secp256k1.fromSeed(seedBytes).derivePath("m/44'/145'/0'") as Bip32Slip10Secp256k1;
as Bip32Slip10Secp256k1;
static int estimatedTransactionSize(int inputsCount, int outputsCounts) => static int estimatedTransactionSize(int inputsCount, int outputsCounts) =>
inputsCount * 68 + outputsCounts * 34 + 10; inputsCount * 68 + outputsCounts * 34 + 10;
@ -357,7 +349,7 @@ abstract class ElectrumWalletBase extends WalletBase<
isSingleScan: doSingleScan ?? false, isSingleScan: doSingleScan ?? false,
)); ));
_receiveStream?.cancel(); await _receiveStream?.cancel();
_receiveStream = receivePort.listen((var message) async { _receiveStream = receivePort.listen((var message) async {
if (message is Map<String, ElectrumTransactionInfo>) { if (message is Map<String, ElectrumTransactionInfo>) {
for (final map in message.entries) { for (final map in message.entries) {
@ -652,9 +644,8 @@ abstract class ElectrumWalletBase extends WalletBase<
ECPrivate? privkey; ECPrivate? privkey;
bool? isSilentPayment = false; bool? isSilentPayment = false;
final hd = utx.bitcoinAddressRecord.isHidden final hd =
? walletAddresses.sideHd utx.bitcoinAddressRecord.isHidden ? walletAddresses.sideHd : walletAddresses.mainHd;
: walletAddresses.mainHd;
if (utx.bitcoinAddressRecord is BitcoinSilentPaymentAddressRecord) { if (utx.bitcoinAddressRecord is BitcoinSilentPaymentAddressRecord) {
final unspentAddress = utx.bitcoinAddressRecord as BitcoinSilentPaymentAddressRecord; final unspentAddress = utx.bitcoinAddressRecord as BitcoinSilentPaymentAddressRecord;
@ -1234,8 +1225,7 @@ abstract class ElectrumWalletBase extends WalletBase<
} }
} }
void setLedgerConnection(ledger.LedgerConnection connection) => void setLedgerConnection(ledger.LedgerConnection connection) => throw UnimplementedError();
throw UnimplementedError();
Future<BtcTransaction> buildHardwareWalletTransaction({ Future<BtcTransaction> buildHardwareWalletTransaction({
required List<BitcoinBaseOutput> outputs, required List<BitcoinBaseOutput> outputs,
@ -1594,9 +1584,7 @@ abstract class ElectrumWalletBase extends WalletBase<
final btcAddress = RegexUtils.addressTypeFromStr(addressRecord.address, network); final btcAddress = RegexUtils.addressTypeFromStr(addressRecord.address, network);
final privkey = generateECPrivate( final privkey = generateECPrivate(
hd: addressRecord.isHidden hd: addressRecord.isHidden ? walletAddresses.sideHd : walletAddresses.mainHd,
? walletAddresses.sideHd
: walletAddresses.mainHd,
index: addressRecord.index, index: addressRecord.index,
network: network); network: network);
@ -1778,8 +1766,7 @@ abstract class ElectrumWalletBase extends WalletBase<
if (height != null) { if (height != null) {
if (time == null && height > 0) { if (time == null && height > 0) {
time = (getDateByBitcoinHeight(height).millisecondsSinceEpoch / 1000) time = (getDateByBitcoinHeight(height).millisecondsSinceEpoch / 1000).round();
.round();
} }
if (confirmations == null) { if (confirmations == null) {
@ -2044,6 +2031,8 @@ abstract class ElectrumWalletBase extends WalletBase<
library: this.runtimeType.toString(), library: this.runtimeType.toString(),
)); ));
} }
}, onError: (e, s) {
print("sub_listen error: $e $s");
}); });
})); }));
} }

View file

@ -582,7 +582,8 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
if (responseStream == null) { if (responseStream == null) {
throw Exception("failed to get utxos stream!"); 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: // we're processing utxos, so our balance could still be innacurate:
if (mwebSyncStatus is! SyncronizingSyncStatus && mwebSyncStatus is! SyncingSyncStatus) { if (mwebSyncStatus is! SyncronizingSyncStatus && mwebSyncStatus is! SyncingSyncStatus) {
mwebSyncStatus = SyncronizingSyncStatus(); mwebSyncStatus = SyncronizingSyncStatus();
@ -628,9 +629,13 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
await mwebUtxosBox.put(utxo.outputId, utxo); await mwebUtxosBox.put(utxo.outputId, utxo);
await handleIncoming(utxo); await handleIncoming(utxo);
}, onError: (error) { },
onError: (error) {
print("error in utxo stream: $error"); print("error in utxo stream: $error");
}); mwebSyncStatus = FailedSyncStatus(error: error.toString());
},
cancelOnError: true,
);
} }
Future<void> checkMwebUtxosSpent() async { Future<void> checkMwebUtxosSpent() async {