From 8264d387be6f4ea24906b123159d4e56d225c084 Mon Sep 17 00:00:00 2001 From: Rafael Saes Date: Mon, 9 Dec 2024 13:18:21 -0300 Subject: [PATCH] fix: null check --- cw_bitcoin/lib/bitcoin_wallet.dart | 12 ++++++ cw_bitcoin/lib/electrum_wallet.dart | 60 +++++++++++------------------ 2 files changed, 35 insertions(+), 37 deletions(-) diff --git a/cw_bitcoin/lib/bitcoin_wallet.dart b/cw_bitcoin/lib/bitcoin_wallet.dart index 20a399a88..a7e442e9d 100644 --- a/cw_bitcoin/lib/bitcoin_wallet.dart +++ b/cw_bitcoin/lib/bitcoin_wallet.dart @@ -287,6 +287,18 @@ abstract class BitcoinWalletBase extends ElectrumWallet with Store { hdWallets[CWBitcoinDerivationType.old_electrum] = hdWallets[CWBitcoinDerivationType.electrum]!; } + + switch (walletInfo.derivationInfo?.derivationType) { + case DerivationType.bip39: + seedBytes = await Bip39SeedGenerator.generateFromString(mnemonic, passphrase); + hdWallets[CWBitcoinDerivationType.bip39] = Bip32Slip10Secp256k1.fromSeed(seedBytes); + break; + case DerivationType.electrum: + default: + seedBytes = await ElectrumV2SeedGenerator.generateFromString(mnemonic, passphrase); + hdWallets[CWBitcoinDerivationType.electrum] = Bip32Slip10Secp256k1.fromSeed(seedBytes); + break; + } } return BitcoinWallet( diff --git a/cw_bitcoin/lib/electrum_wallet.dart b/cw_bitcoin/lib/electrum_wallet.dart index 19199302a..6630d43cf 100644 --- a/cw_bitcoin/lib/electrum_wallet.dart +++ b/cw_bitcoin/lib/electrum_wallet.dart @@ -1229,19 +1229,6 @@ abstract class ElectrumWalletBase @action Future addCoinInfo(BitcoinUnspent coin) async { - final newInfo = UnspentCoinsInfo( - walletId: id, - hash: coin.hash, - isFrozen: coin.isFrozen, - isSending: coin.isSending, - noteRaw: coin.note, - address: coin.bitcoinAddressRecord.address, - value: coin.value, - vout: coin.vout, - isChange: coin.isChange, - isSilentPayment: coin.bitcoinAddressRecord is BitcoinReceivedSPAddressRecord, - ); - // Check if the coin is already in the unspentCoinsInfo for the wallet final existingCoinInfo = unspentCoinsInfo.values .firstWhereOrNull((element) => element.walletId == walletInfo.id && element == coin); @@ -1257,7 +1244,7 @@ abstract class ElectrumWalletBase value: coin.value, vout: coin.vout, isChange: coin.isChange, - isSilentPayment: coin is BitcoinSilentPaymentsUnspent, + isSilentPayment: coin.address is BitcoinReceivedSPAddressRecord, ); await unspentCoinsInfo.add(newInfo); @@ -1423,12 +1410,15 @@ abstract class ElectrumWalletBase final bundle = await getTransactionExpanded(hash: txId); final outputs = bundle.originalTransaction.outputs; - final changeAddresses = walletAddresses.allAddresses.where((element) => element.isChange); + final ownAddresses = walletAddresses.allAddresses.map((addr) => addr.address).toSet(); - // look for a change address in the outputs - final changeOutput = outputs.firstWhereOrNull((output) => changeAddresses.any((element) => - element.address == - BitcoinAddressUtils.addressFromOutputScript(output.scriptPubKey, network))); + final receiverAmount = outputs + .where( + (output) => !ownAddresses.contains( + BitcoinAddressUtils.addressFromOutputScript(output.scriptPubKey, network), + ), + ) + .fold(0, (sum, output) => sum + output.amount.toInt()); if (receiverAmount == 0) { throw Exception("Receiver output not found."); @@ -1474,7 +1464,7 @@ abstract class ElectrumWalletBase final outTransaction = inputTransaction.outputs[vout]; final address = BitcoinAddressUtils.addressFromOutputScript(outTransaction.scriptPubKey, network); - // allInputsAmount += outTransaction.amount.toInt(); + allInputsAmount += outTransaction.amount.toInt(); final addressRecord = walletAddresses.allAddresses.firstWhere((element) => element.address == address); @@ -1566,24 +1556,20 @@ abstract class ElectrumWalletBase for (final utxo in unusedUtxos) { final address = RegexUtils.addressTypeFromStr(utxo.address, network); - final privkey = generateECPrivate( - hd: utxo.bitcoinAddressRecord.isHidden - ? walletAddresses.sideHd - : walletAddresses.mainHd, - index: utxo.bitcoinAddressRecord.index, - network: network, - ); + final privkey = ECPrivate.fromBip32(bip32: bip32); privateKeys.add(privkey); - utxos.add(UtxoWithAddress( - utxo: BitcoinUtxo( - txHash: utxo.hash, - value: BigInt.from(utxo.value), - vout: utxo.vout, - scriptType: _getScriptType(address)), - ownerDetails: - UtxoAddressDetails(publicKey: privkey.getPublic().toHex(), address: address), - )); + utxos.add( + UtxoWithAddress( + utxo: BitcoinUtxo( + txHash: utxo.hash, + value: BigInt.from(utxo.value), + vout: utxo.vout, + scriptType: BitcoinAddressUtils.getScriptType(address)), + ownerDetails: + UtxoAddressDetails(publicKey: privkey.getPublic().toHex(), address: address), + ), + ); allInputsAmount += utxo.value; remainingFee -= utxo.value; @@ -1636,7 +1622,7 @@ abstract class ElectrumWalletBase } // Identify all change outputs - final changeAddresses = walletAddresses.allAddresses.where((element) => element.isChange); + final changeAddresses = walletAddresses.changeAddresses; final List changeOutputs = outputs .where((output) => changeAddresses .any((element) => element.address == output.address.toAddress(network)))