bitcoinjs / bitcoinjs-lib

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

Support string as value for outputs? #1125

Closed APshenkin closed 6 years ago

APshenkin commented 6 years ago

Hi!

Is it possible to pass values as strings or Buffers? I want to add outputs value that are not in UINT53.

I checked this issue https://github.com/bitcoinjs/bitcoinjs-lib/issues/562. There is a suggestion to set valueBuffer for outs, but it doesn't work. Any suggestions? Thanks!

junderw commented 6 years ago

String str? parseInt(str) % Math.pow(2,53) (if your string is hex: parseInt(str, 16)) Buffer buf? buf.readUIntBE(0, buf.length) % Math.pow(2,53)

Bitcoin's max satoshi amount fits perfectly within JavaScripts maximum precision number set.

APshenkin commented 6 years ago

@junderw Thank you!

dcousens commented 6 years ago

Uh, this won't help if your value is greater than 53bits....

junderw commented 6 years ago

Bitcoin's max satoshi amount fits perfectly within JavaScripts maximum precision number set.

(Implication: this only works within the limits of the maximum satoshi value of Bitcoin)

But for explicitness sake:

@APshenkin any amount over 9007199254740991 (53 bits) will lose precision in JavaScript so is a no go. Also, any value over 2100000000000000 (21 * 1e6 * 1e8) is impossible to send in Bitcoin (technically it's smaller than that) since it's the max amount of Bitcoins that will ever be created.

if you want to be super careful: % Math.pow(2,53) at the end will chop of any bits over 53... you lose info, so maybe you might want to do something like import a BigInt library and check...

But tbh, for a simple sanity check, making sure the value is correct:

  1. parseInt(val) === val should be true
  2. val >= 0 should be true
  3. val <= (21 * 1e6 * 1e8) should be true
dcousens commented 6 years ago

@junderw

I want to add outputs value that are not in UINT53.

He isn't worried about blowing the limit, he wants to. We don't provide support for that.

dcousens commented 6 years ago

Related to https://github.com/bitcoinjs/bitcoinjs-lib/issues/1126