fix: null check

This commit is contained in:
Rafael Saes 2024-12-09 13:18:21 -03:00
parent a97b5525be
commit 8264d387be
2 changed files with 35 additions and 37 deletions

View file

@ -287,6 +287,18 @@ abstract class BitcoinWalletBase extends ElectrumWallet with Store {
hdWallets[CWBitcoinDerivationType.old_electrum] = hdWallets[CWBitcoinDerivationType.old_electrum] =
hdWallets[CWBitcoinDerivationType.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( return BitcoinWallet(

View file

@ -1229,19 +1229,6 @@ abstract class ElectrumWalletBase
@action @action
Future<void> addCoinInfo(BitcoinUnspent coin) async { Future<void> 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 // Check if the coin is already in the unspentCoinsInfo for the wallet
final existingCoinInfo = unspentCoinsInfo.values final existingCoinInfo = unspentCoinsInfo.values
.firstWhereOrNull((element) => element.walletId == walletInfo.id && element == coin); .firstWhereOrNull((element) => element.walletId == walletInfo.id && element == coin);
@ -1257,7 +1244,7 @@ abstract class ElectrumWalletBase
value: coin.value, value: coin.value,
vout: coin.vout, vout: coin.vout,
isChange: coin.isChange, isChange: coin.isChange,
isSilentPayment: coin is BitcoinSilentPaymentsUnspent, isSilentPayment: coin.address is BitcoinReceivedSPAddressRecord,
); );
await unspentCoinsInfo.add(newInfo); await unspentCoinsInfo.add(newInfo);
@ -1423,12 +1410,15 @@ abstract class ElectrumWalletBase
final bundle = await getTransactionExpanded(hash: txId); final bundle = await getTransactionExpanded(hash: txId);
final outputs = bundle.originalTransaction.outputs; 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 receiverAmount = outputs
final changeOutput = outputs.firstWhereOrNull((output) => changeAddresses.any((element) => .where(
element.address == (output) => !ownAddresses.contains(
BitcoinAddressUtils.addressFromOutputScript(output.scriptPubKey, network))); BitcoinAddressUtils.addressFromOutputScript(output.scriptPubKey, network),
),
)
.fold<int>(0, (sum, output) => sum + output.amount.toInt());
if (receiverAmount == 0) { if (receiverAmount == 0) {
throw Exception("Receiver output not found."); throw Exception("Receiver output not found.");
@ -1474,7 +1464,7 @@ abstract class ElectrumWalletBase
final outTransaction = inputTransaction.outputs[vout]; final outTransaction = inputTransaction.outputs[vout];
final address = final address =
BitcoinAddressUtils.addressFromOutputScript(outTransaction.scriptPubKey, network); BitcoinAddressUtils.addressFromOutputScript(outTransaction.scriptPubKey, network);
// allInputsAmount += outTransaction.amount.toInt(); allInputsAmount += outTransaction.amount.toInt();
final addressRecord = final addressRecord =
walletAddresses.allAddresses.firstWhere((element) => element.address == address); walletAddresses.allAddresses.firstWhere((element) => element.address == address);
@ -1566,24 +1556,20 @@ abstract class ElectrumWalletBase
for (final utxo in unusedUtxos) { for (final utxo in unusedUtxos) {
final address = RegexUtils.addressTypeFromStr(utxo.address, network); final address = RegexUtils.addressTypeFromStr(utxo.address, network);
final privkey = generateECPrivate( final privkey = ECPrivate.fromBip32(bip32: bip32);
hd: utxo.bitcoinAddressRecord.isHidden
? walletAddresses.sideHd
: walletAddresses.mainHd,
index: utxo.bitcoinAddressRecord.index,
network: network,
);
privateKeys.add(privkey); privateKeys.add(privkey);
utxos.add(UtxoWithAddress( utxos.add(
utxo: BitcoinUtxo( UtxoWithAddress(
txHash: utxo.hash, utxo: BitcoinUtxo(
value: BigInt.from(utxo.value), txHash: utxo.hash,
vout: utxo.vout, value: BigInt.from(utxo.value),
scriptType: _getScriptType(address)), vout: utxo.vout,
ownerDetails: scriptType: BitcoinAddressUtils.getScriptType(address)),
UtxoAddressDetails(publicKey: privkey.getPublic().toHex(), address: address), ownerDetails:
)); UtxoAddressDetails(publicKey: privkey.getPublic().toHex(), address: address),
),
);
allInputsAmount += utxo.value; allInputsAmount += utxo.value;
remainingFee -= utxo.value; remainingFee -= utxo.value;
@ -1636,7 +1622,7 @@ abstract class ElectrumWalletBase
} }
// Identify all change outputs // Identify all change outputs
final changeAddresses = walletAddresses.allAddresses.where((element) => element.isChange); final changeAddresses = walletAddresses.changeAddresses;
final List<BitcoinOutput> changeOutputs = outputs final List<BitcoinOutput> changeOutputs = outputs
.where((output) => changeAddresses .where((output) => changeAddresses
.any((element) => element.address == output.address.toAddress(network))) .any((element) => element.address == output.address.toAddress(network)))