bitcoinjs-lib icon indicating copy to clipboard operation
bitcoinjs-lib copied to clipboard

Feature request: Dust threshold value based on output script

Open arik-so opened this issue 1 year ago • 8 comments
trafficstars

Sometimes when parsing transactions I need to check whether an output value is too low for a given output type. Would you consider adding some sort of utility, maybe to bitcoin.address, that would allow me to look up the dust threshold based on the output script? Perhaps bitcoin.address.dustThresholdFromOutputScript() or something similar?

arik-so avatar Dec 02 '23 18:12 arik-so

Ooh, that sounds like a good idea.

I'll see if I can whip something up later.

If anyone wants to work on it let me know.

junderw avatar Dec 02 '23 19:12 junderw

Basically:

  1. If it's provably unspendable (OP_RETURN et al) then 0 sats is ok
  2. If it's segwit then use 67 bytes if not, use 148 bytes
  3. Add the size of the output, which is script size + size of varint for scriptSize + 8 (for i64 amount)
  4. Multiply bytes by 3 to get sats (round up). (MIN_RELAY_TX_FEE = 1000 assumed)

https://github.com/bitcoin/bitcoin/blob/160d23677ad799cf9b493eaa923b2ac080c3fb8e/src/policy/policy.cpp#L26-L63

junderw avatar Dec 02 '23 23:12 junderw

Questions:

  1. Add a parameter to represent MIN_RELAY? (I checked and it's not based off of the eject level, it's based off of the min_relay parameter (which defaults to 1000)
  2. If we do add a parameter... should we do sat/kvB? (1000) or sat/vB (1)? (defaults in parens)

junderw avatar Dec 02 '23 23:12 junderw

Check out the PR.

junderw avatar Dec 03 '23 01:12 junderw

Wow, that was fast! Do answer your second question: I think sat/vByte is probably the most convenient.

arik-so avatar Dec 03 '23 01:12 arik-so

I guess it really doesn't matter. We can change it later.

Just check it out and make sure it jives with the Core code.

junderw avatar Dec 03 '23 01:12 junderw

By the way, thank you so much for implementing this! Shall I close this issue?

arik-so avatar Mar 01 '24 01:03 arik-so

No it'll be closed when we merge it.

junderw avatar Mar 01 '24 03:03 junderw