Skip to content

Commit 10ced8d

Browse files
committed
fix(firo): tighten Spark mint fee accounting
1 parent 7a3b428 commit 10ced8d

1 file changed

Lines changed: 22 additions & 2 deletions

File tree

lib/wallets/wallet/wallet_mixin_interfaces/spark_interface.dart

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1695,8 +1695,11 @@ mixin SparkInterface<T extends ElectrumXCurrencyInterface>
16951695
if (subtractFeeFromAmount && nFeeRet > BigInt.zero) {
16961696
var remainingFee = nFeeRet;
16971697
var outputIndex = 0;
1698-
while (outputIndex < singleTxOutputs.length &&
1699-
remainingFee > BigInt.zero) {
1698+
while (singleTxOutputs.isNotEmpty && remainingFee > BigInt.zero) {
1699+
if (outputIndex >= singleTxOutputs.length) {
1700+
outputIndex = 0;
1701+
}
1702+
17001703
final outputsLeft = BigInt.from(
17011704
singleTxOutputs.length - outputIndex,
17021705
);
@@ -1717,6 +1720,9 @@ mixin SparkInterface<T extends ElectrumXCurrencyInterface>
17171720
}
17181721

17191722
if (singleTxOutputs.isEmpty) {
1723+
if (autoMintAll) {
1724+
throw Exception("UTXO value is too small to cover Spark mint fee");
1725+
}
17201726
valueAndUTXOs.remove(itr);
17211727
skipCoin = true;
17221728
break;
@@ -2086,6 +2092,20 @@ mixin SparkInterface<T extends ElectrumXCurrencyInterface>
20862092
rethrow;
20872093
}
20882094
final builtTx = txb.build();
2095+
final actualFee =
2096+
vin
2097+
.map((e) => BigInt.from(e.utxo.value))
2098+
.fold(BigInt.zero, (p, e) => p + e) -
2099+
vout
2100+
.map((e) => BigInt.from(e.$2))
2101+
.fold(BigInt.zero, (p, e) => p + e);
2102+
if (actualFee != nFeeRet) {
2103+
Logging.instance.e(
2104+
"Spark mint fee accounting mismatch: "
2105+
"expected=$nFeeRet, actual=$actualFee",
2106+
);
2107+
throw Exception("Spark mint fee accounting mismatch");
2108+
}
20892109

20902110
// TODO: see todo at top of this function
20912111
assert(outputs.length == 1);

0 commit comments

Comments
 (0)