bitcoinjs / coinselect

An unspent transaction output (UTXO) selection module for bitcoin.
MIT License
180 stars 101 forks source link

Update utils.js #63

Closed Turtus closed 3 years ago

Turtus commented 3 years ago

for anyone who do need segwit inputs actualized https://github.com/bitcoinjs/coinselect/issues/56

jaonoctus commented 3 years ago

The best option for this P2WPKH (native segwit singlesig) workaround seems to be not change anything on the algo,

but add script.length = 27 for each input and script.length = 22 for each output, instead.

function inputBytes (input) {
  return TX_INPUT_BASE + (input.script ? input.script.length : TX_INPUT_PUBKEYHASH)
  // 41 + 27 = 68
}

function outputBytes (output) {
  return TX_OUTPUT_BASE + (output.script ? output.script.length : TX_OUTPUT_PUBKEYHASH)
  // 9 + 22 = 31
}

image

Total size should be 109.5 vbytes for 1 input and 1 output for example. with this approach, the transactionBytes function will return 109. the missing 0.5 is from the Overhead (TX_EMPTY_SIZE = 4 + 1 + 1 + 4 = 10)

https://bitcoinops.org/en/tools/calc-size/

I'm not sure about P2WSH (multisig or some other weired custom script) or P2TR (taproot) or nested segwit (3...addresses) tho 🤔

Turtus commented 3 years ago

dno problem - ive already using script "with predefined" length not to patch the lib, but actual version is sooo not supporting segwit that anything will be improvement)

Overtorment commented 3 years ago

fyi, this is how its approached in bluewallet: https://github.com/BlueWallet/BlueWallet/pull/3810