mirror of
https://github.com/Such-Software/hash-wallet.git
synced 2026-12-02 12:14:09 +01:00
fix: sp input calc (#1756)
This commit is contained in:
@@ -784,6 +784,7 @@ abstract class ElectrumWalletBase
|
|||||||
Future<EstimatedTxResult> estimateTxForAmount(
|
Future<EstimatedTxResult> estimateTxForAmount(
|
||||||
int credentialsAmount,
|
int credentialsAmount,
|
||||||
List<BitcoinOutput> outputs,
|
List<BitcoinOutput> outputs,
|
||||||
|
List<BitcoinOutput> updatedOutputs,
|
||||||
int feeRate, {
|
int feeRate, {
|
||||||
int? inputsCount,
|
int? inputsCount,
|
||||||
String? memo,
|
String? memo,
|
||||||
@@ -812,6 +813,7 @@ abstract class ElectrumWalletBase
|
|||||||
return estimateTxForAmount(
|
return estimateTxForAmount(
|
||||||
credentialsAmount,
|
credentialsAmount,
|
||||||
outputs,
|
outputs,
|
||||||
|
updatedOutputs,
|
||||||
feeRate,
|
feeRate,
|
||||||
inputsCount: utxoDetails.utxos.length + 1,
|
inputsCount: utxoDetails.utxos.length + 1,
|
||||||
memo: memo,
|
memo: memo,
|
||||||
@@ -824,19 +826,30 @@ abstract class ElectrumWalletBase
|
|||||||
}
|
}
|
||||||
|
|
||||||
final changeAddress = await walletAddresses.getChangeAddress(
|
final changeAddress = await walletAddresses.getChangeAddress(
|
||||||
outputs: outputs,
|
outputs: updatedOutputs,
|
||||||
utxoDetails: utxoDetails,
|
utxoDetails: utxoDetails,
|
||||||
);
|
);
|
||||||
final address = RegexUtils.addressTypeFromStr(changeAddress, network);
|
final address = RegexUtils.addressTypeFromStr(changeAddress, network);
|
||||||
|
updatedOutputs.add(BitcoinOutput(
|
||||||
|
address: address,
|
||||||
|
value: BigInt.from(amountLeftForChangeAndFee),
|
||||||
|
isChange: true,
|
||||||
|
));
|
||||||
outputs.add(BitcoinOutput(
|
outputs.add(BitcoinOutput(
|
||||||
address: address,
|
address: address,
|
||||||
value: BigInt.from(amountLeftForChangeAndFee),
|
value: BigInt.from(amountLeftForChangeAndFee),
|
||||||
isChange: true,
|
isChange: true,
|
||||||
));
|
));
|
||||||
|
|
||||||
|
// calcFee updates the silent payment outputs to calculate the tx size accounting
|
||||||
|
// for taproot addresses, but if more inputs are needed to make up for fees,
|
||||||
|
// the silent payment outputs need to be recalculated for the new inputs
|
||||||
|
var temp = outputs.map((output) => output).toList();
|
||||||
int fee = await calcFee(
|
int fee = await calcFee(
|
||||||
utxos: utxoDetails.utxos,
|
utxos: utxoDetails.utxos,
|
||||||
outputs: outputs,
|
// Always take only not updated bitcoin outputs here so for every estimation
|
||||||
|
// the SP outputs are re-generated to the proper taproot addresses
|
||||||
|
outputs: temp,
|
||||||
network: network,
|
network: network,
|
||||||
memo: memo,
|
memo: memo,
|
||||||
feeRate: feeRate,
|
feeRate: feeRate,
|
||||||
@@ -844,18 +857,25 @@ abstract class ElectrumWalletBase
|
|||||||
vinOutpoints: utxoDetails.vinOutpoints,
|
vinOutpoints: utxoDetails.vinOutpoints,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
updatedOutputs.clear();
|
||||||
|
updatedOutputs.addAll(temp);
|
||||||
|
|
||||||
if (fee == 0) {
|
if (fee == 0) {
|
||||||
throw BitcoinTransactionNoFeeException();
|
throw BitcoinTransactionNoFeeException();
|
||||||
}
|
}
|
||||||
|
|
||||||
int amount = credentialsAmount;
|
int amount = credentialsAmount;
|
||||||
final lastOutput = outputs.last;
|
final lastOutput = updatedOutputs.last;
|
||||||
final amountLeftForChange = amountLeftForChangeAndFee - fee;
|
final amountLeftForChange = amountLeftForChangeAndFee - fee;
|
||||||
|
|
||||||
print(amountLeftForChangeAndFee);
|
|
||||||
|
|
||||||
if (!_isBelowDust(amountLeftForChange)) {
|
if (!_isBelowDust(amountLeftForChange)) {
|
||||||
// Here, lastOutput already is change, return the amount left without the fee to the user's address.
|
// Here, lastOutput already is change, return the amount left without the fee to the user's address.
|
||||||
|
updatedOutputs[updatedOutputs.length - 1] = BitcoinOutput(
|
||||||
|
address: lastOutput.address,
|
||||||
|
value: BigInt.from(amountLeftForChange),
|
||||||
|
isSilentPayment: lastOutput.isSilentPayment,
|
||||||
|
isChange: true,
|
||||||
|
);
|
||||||
outputs[outputs.length - 1] = BitcoinOutput(
|
outputs[outputs.length - 1] = BitcoinOutput(
|
||||||
address: lastOutput.address,
|
address: lastOutput.address,
|
||||||
value: BigInt.from(amountLeftForChange),
|
value: BigInt.from(amountLeftForChange),
|
||||||
@@ -864,6 +884,7 @@ abstract class ElectrumWalletBase
|
|||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
// If has change that is lower than dust, will end up with tx rejected by network rules, so estimate again without the added change
|
// If has change that is lower than dust, will end up with tx rejected by network rules, so estimate again without the added change
|
||||||
|
updatedOutputs.removeLast();
|
||||||
outputs.removeLast();
|
outputs.removeLast();
|
||||||
|
|
||||||
// Still has inputs to spend before failing
|
// Still has inputs to spend before failing
|
||||||
@@ -871,16 +892,18 @@ abstract class ElectrumWalletBase
|
|||||||
return estimateTxForAmount(
|
return estimateTxForAmount(
|
||||||
credentialsAmount,
|
credentialsAmount,
|
||||||
outputs,
|
outputs,
|
||||||
|
updatedOutputs,
|
||||||
feeRate,
|
feeRate,
|
||||||
inputsCount: utxoDetails.utxos.length + 1,
|
inputsCount: utxoDetails.utxos.length + 1,
|
||||||
memo: memo,
|
memo: memo,
|
||||||
|
hasSilentPayment: hasSilentPayment,
|
||||||
useUnconfirmed: useUnconfirmed ?? spendingAllConfirmedCoins,
|
useUnconfirmed: useUnconfirmed ?? spendingAllConfirmedCoins,
|
||||||
coinTypeToSpendFrom: coinTypeToSpendFrom,
|
coinTypeToSpendFrom: coinTypeToSpendFrom,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
final estimatedSendAll = await estimateSendAllTx(
|
final estimatedSendAll = await estimateSendAllTx(
|
||||||
outputs,
|
updatedOutputs,
|
||||||
feeRate,
|
feeRate,
|
||||||
memo: memo,
|
memo: memo,
|
||||||
coinTypeToSpendFrom: coinTypeToSpendFrom,
|
coinTypeToSpendFrom: coinTypeToSpendFrom,
|
||||||
@@ -913,10 +936,12 @@ abstract class ElectrumWalletBase
|
|||||||
if (spendingAllCoins) {
|
if (spendingAllCoins) {
|
||||||
throw BitcoinTransactionWrongBalanceException();
|
throw BitcoinTransactionWrongBalanceException();
|
||||||
} else {
|
} else {
|
||||||
|
updatedOutputs.removeLast();
|
||||||
outputs.removeLast();
|
outputs.removeLast();
|
||||||
return estimateTxForAmount(
|
return estimateTxForAmount(
|
||||||
credentialsAmount,
|
credentialsAmount,
|
||||||
outputs,
|
outputs,
|
||||||
|
updatedOutputs,
|
||||||
feeRate,
|
feeRate,
|
||||||
inputsCount: utxoDetails.utxos.length + 1,
|
inputsCount: utxoDetails.utxos.length + 1,
|
||||||
memo: memo,
|
memo: memo,
|
||||||
@@ -1029,6 +1054,9 @@ abstract class ElectrumWalletBase
|
|||||||
: feeRate(transactionCredentials.priority!);
|
: feeRate(transactionCredentials.priority!);
|
||||||
|
|
||||||
EstimatedTxResult estimatedTx;
|
EstimatedTxResult estimatedTx;
|
||||||
|
final updatedOutputs =
|
||||||
|
outputs.map((e) => BitcoinOutput(address: e.address, value: e.value)).toList();
|
||||||
|
|
||||||
if (sendAll) {
|
if (sendAll) {
|
||||||
estimatedTx = await estimateSendAllTx(
|
estimatedTx = await estimateSendAllTx(
|
||||||
outputs,
|
outputs,
|
||||||
@@ -1042,6 +1070,7 @@ abstract class ElectrumWalletBase
|
|||||||
estimatedTx = await estimateTxForAmount(
|
estimatedTx = await estimateTxForAmount(
|
||||||
credentialsAmount,
|
credentialsAmount,
|
||||||
outputs,
|
outputs,
|
||||||
|
updatedOutputs,
|
||||||
feeRateInt,
|
feeRateInt,
|
||||||
memo: memo,
|
memo: memo,
|
||||||
hasSilentPayment: hasSilentPayment,
|
hasSilentPayment: hasSilentPayment,
|
||||||
@@ -1052,7 +1081,7 @@ abstract class ElectrumWalletBase
|
|||||||
if (walletInfo.isHardwareWallet) {
|
if (walletInfo.isHardwareWallet) {
|
||||||
final transaction = await buildHardwareWalletTransaction(
|
final transaction = await buildHardwareWalletTransaction(
|
||||||
utxos: estimatedTx.utxos,
|
utxos: estimatedTx.utxos,
|
||||||
outputs: outputs,
|
outputs: updatedOutputs,
|
||||||
publicKeys: estimatedTx.publicKeys,
|
publicKeys: estimatedTx.publicKeys,
|
||||||
fee: BigInt.from(estimatedTx.fee),
|
fee: BigInt.from(estimatedTx.fee),
|
||||||
network: network,
|
network: network,
|
||||||
@@ -1092,7 +1121,7 @@ abstract class ElectrumWalletBase
|
|||||||
} else {
|
} else {
|
||||||
txb = BitcoinTransactionBuilder(
|
txb = BitcoinTransactionBuilder(
|
||||||
utxos: estimatedTx.utxos,
|
utxos: estimatedTx.utxos,
|
||||||
outputs: outputs,
|
outputs: updatedOutputs,
|
||||||
fee: BigInt.from(estimatedTx.fee),
|
fee: BigInt.from(estimatedTx.fee),
|
||||||
network: network,
|
network: network,
|
||||||
memo: estimatedTx.memo,
|
memo: estimatedTx.memo,
|
||||||
|
|||||||
Reference in New Issue
Block a user