fix: do not create change or split outputs below Bitcoin Core dust limit - #1
limpbrains wants to merge 1 commit into
Conversation
b377a17 to
237d3bc
Compare
237d3bc to
98899e7
Compare
…mit, add changeScript and txExtraBytes options
98899e7 to
8f2fcc7
Compare
GladosBlueWallet
left a comment
There was a problem hiding this comment.
Test subject, your dust patch does what the commit message promises—how refreshingly rare. utils.isDust and relayDustThreshold now enforce Bitcoin Core dust limits (plus the existing spend-cost check) in finalize and split; sub-dust scraps get incinerated into fees instead of spawning relay-rejected outputs. changeScript and txExtraBytes propagate through every algorithm; checkOptions returns {} on bad input. blackjack keeps its separate 148 * feeRate overpay tolerance—two experiments, one facility. Three-argument callers stay compatible outside the sub-dust window. README and index.d.ts document the new behavior. Tests hit real selection/finalize paths with concrete values, not mocks. Diff trace: no regressions. Proceed. For now.
Problem
Change created when remainder >
148 * feeRate. At 1 sat/vB that is 149 sats. Bitcoin Core dust limit is 546 (p2pkh) / 540 (p2sh) / 294 (p2wpkh) / 330 (p2wsh, p2tr). Tx with such change rejected by nodes asdust. Same forsplit(send max) outputs. Known upstream, never fixed: bitcoinjs#86, bitcoinjs#16.Fix
utils.isDust(value, output, feeRate): dust ifvalue <= 148 * feeRate(not worth spending, strict as before) orvalue < Core dust limitfor output type (inclusive, same as CoreIsDust). Core limit = same math asGetDustThreshold(),dustrelayfee3 sat/vB. No script = p2pkh. Used byfinalize(change) andsplit.optionsfor all algos +finalize:changeScript: { length }- real size of change output + its dust limit. Omitted = p2pkh, old behaviour.txExtraBytes: number- tx bytes lib does not know about, e.g. 1 for segwit marker & flag.{ length: 34 }) = no solution{}. Silently ignoring them burned whole change as fee.utils.transactionBytesreturns NaN for them.changeScript.lengthover 10000 (CoreMAX_SCRIPT_SIZE) rejected too.blackjackoverpay tolerance stays148 * feeRate. Tying it to dust limit made it burn up to 546 sats when solution with change was cheaper (test added).index.d.ts, README updated.Backwards compatible: 3-arg calls behave the same outside sub-dust window. Below-dust change goes to fee instead.
Check
npm test: 258 pass,standardclean, coverage 100%. No old fixture changed.feeRate * size.splitwith exact script lengths: 668 cases old solved and new did not, all 668 were real dust (0 relayable).Notes
accumulative.