mirror of
https://gitlab.com/soapbox-pub/mkstack.git
synced 2026-12-03 08:07:21 +00:00
Reject implausible fee rates in the onchain-bitcoin skill
`getFeeRates` coerced the remote response with `Math.ceil(data['1'] || 1)`.
`||` only catches falsy values, so a missing key was safe but a truthy
non-number — `{"1": "5"}`, `{"1": {"fee": 5}}`, what an API-version
mismatch produces — yielded NaN.
NaN then passed every downstream guard, because they are all `<` / `>=`
comparisons and every comparison with NaN is false: no change output was
added, the balance check passed, nothing threw, and the UI showed
"Fee 0" because `!NaN` is true. The wallet built a valid, broadcastable
transaction paying its entire balance to miners.
Sanitize each rate to a finite number in [1, 5000], and make `estimateFee`
throw on a non-finite rate rather than propagate NaN into the builder.
This commit is contained in:
2 files changed
+51
-5
No files matched your search
@@ -251,6 +251,17 @@ Key facts about `buildUnsignedPsbt`:
|
||||
- Fee uses `ceil((numInputs · 57.5 + numOutputs · 43 + 10.5) · feeRate)`.
|
||||
- Change is added back to the sender's own Taproot address **only** if it's ≥ 546 sats (dust limit). Below dust, change is donated to fees and the tx has 1 output instead of 2.
|
||||
- Throws `Insufficient funds` if `amount + fee > total UTXO value`.
|
||||
- Throws `Invalid fee rate` if `feeRate` is not a finite number ≥ 1.
|
||||
|
||||
That last guard is load-bearing. A `NaN` fee rate produces a `NaN` fee, and
|
||||
every check downstream of the fee — the dust comparison, the balance check, the
|
||||
"is there a fee to show" check in the UI — is a `<` or `>=` comparison, and
|
||||
*every* comparison with NaN is false. So NaN doesn't fail: it silently skips the
|
||||
change output, passes the balance check, renders as "Fee 0", and builds a
|
||||
perfectly valid transaction that pays the wallet's entire balance to miners.
|
||||
`getFeeRates` sanitizes the remote response for the same reason — the old
|
||||
`Math.ceil(data['1'] || 1)` caught a *missing* key but turned a truthy
|
||||
non-number like `{"1": "5"}` into NaN.
|
||||
|
||||
For send-max, use `maxSendable(totalBalance, numInputs, feeRate)` — it correctly subtracts a 1-output fee.
|
||||
|
||||
|
||||
@@ -446,14 +446,42 @@ export async function getFeeRates(): Promise<FeeRates> {
|
||||
const data = await response.json();
|
||||
|
||||
return {
|
||||
fastestFee: Math.ceil(data['1'] || 1),
|
||||
halfHourFee: Math.ceil(data['3'] || 1),
|
||||
hourFee: Math.ceil(data['6'] || 1),
|
||||
economyFee: Math.ceil(data['144'] || 1),
|
||||
minimumFee: Math.ceil(data['504'] || 1),
|
||||
fastestFee: sanitizeFeeRate(data?.['1']),
|
||||
halfHourFee: sanitizeFeeRate(data?.['3']),
|
||||
hourFee: sanitizeFeeRate(data?.['6']),
|
||||
economyFee: sanitizeFeeRate(data?.['144']),
|
||||
minimumFee: sanitizeFeeRate(data?.['504']),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Highest fee rate we will accept from a remote endpoint, in sat/vB. Well
|
||||
* above any real congestion (mainnet peaks have not passed ~2000 sat/vB) and
|
||||
* far below anything that could quietly consume a wallet.
|
||||
*/
|
||||
const MAX_PLAUSIBLE_FEE_RATE = 5_000;
|
||||
|
||||
/**
|
||||
* Coerce a fee rate from an Esplora `/fee-estimates` response to a usable
|
||||
* sat/vB number, falling back to 1 for anything implausible.
|
||||
*
|
||||
* The endpoint is remote, and this used to be `Math.ceil(data['1'] || 1)`.
|
||||
* `||` only catches *falsy* values, so a missing key was safe but a truthy
|
||||
* non-number — `{"1": "5"}`, `{"1": {"fee": 5}}`, the sort of thing an
|
||||
* API-version mismatch produces — yielded `NaN`. NaN then passed every
|
||||
* downstream guard, because they are all `<` / `>=` comparisons and every
|
||||
* comparison with NaN is false: no change output was added, nothing threw, the
|
||||
* balance check passed, and the UI rendered "Fee 0" because `!NaN` is true.
|
||||
* The result was a valid, broadcastable transaction that paid the wallet's
|
||||
* entire remaining balance to miners.
|
||||
*/
|
||||
function sanitizeFeeRate(value: unknown): number {
|
||||
if (typeof value !== 'number' || !Number.isFinite(value)) return 1;
|
||||
if (value < 1) return 1;
|
||||
if (value > MAX_PLAUSIBLE_FEE_RATE) return MAX_PLAUSIBLE_FEE_RATE;
|
||||
return Math.ceil(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Estimate the fee for a P2TR transaction in satoshis.
|
||||
*
|
||||
@@ -462,6 +490,13 @@ export async function getFeeRates(): Promise<FeeRates> {
|
||||
* @param feeRate Fee rate in sat/vB.
|
||||
*/
|
||||
export function estimateFee(numInputs: number, numOutputs: number, feeRate: number): number {
|
||||
// Assert rather than infer. A non-finite rate produces a NaN fee, and every
|
||||
// guard downstream of this is a `<` or `>=` comparison, all of which are
|
||||
// false for NaN — so the failure surfaces as a transaction that hands the
|
||||
// wallet's balance to miners rather than as an error.
|
||||
if (!Number.isFinite(feeRate) || feeRate < 1) {
|
||||
throw new Error(`Invalid fee rate: ${feeRate} sat/vB.`);
|
||||
}
|
||||
const vBytes = numInputs * VBYTES_PER_INPUT + numOutputs * VBYTES_PER_OUTPUT + VBYTES_OVERHEAD;
|
||||
return Math.ceil(vBytes * feeRate);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user