bitcoinjs / bitcoinjs-lib

A javascript Bitcoin library for node.js and browsers.
MIT License
5.65k stars 2.09k forks source link

Feature request: Dust threshold value based on output script #2009

Open arik-so opened 9 months ago

arik-so commented 9 months ago

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?

junderw commented 9 months ago

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 commented 9 months ago

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 commented 9 months ago

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 commented 9 months ago

Check out the PR.

arik-so commented 9 months ago

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

junderw commented 9 months ago

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.

arik-so commented 6 months ago

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

junderw commented 6 months ago

No it'll be closed when we merge it.