smartledger-bsv 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. package/LICENSE +36 -0
  2. package/README.md +305 -0
  3. package/SECURITY.md +75 -0
  4. package/bsv-ecies.min.js +12 -0
  5. package/bsv-message.min.js +10 -0
  6. package/bsv-mnemonic.min.js +12 -0
  7. package/bsv.d.ts +440 -0
  8. package/bsv.min.js +37 -0
  9. package/ecies/index.js +1 -0
  10. package/index.js +101 -0
  11. package/lib/address.js +526 -0
  12. package/lib/block/block.js +277 -0
  13. package/lib/block/blockheader.js +294 -0
  14. package/lib/block/index.js +4 -0
  15. package/lib/block/merkleblock.js +316 -0
  16. package/lib/crypto/bn.js +278 -0
  17. package/lib/crypto/ecdsa.js +330 -0
  18. package/lib/crypto/elliptic-fixed.js +74 -0
  19. package/lib/crypto/hash.browser.js +171 -0
  20. package/lib/crypto/hash.js +2 -0
  21. package/lib/crypto/hash.node.js +171 -0
  22. package/lib/crypto/point.js +217 -0
  23. package/lib/crypto/random.js +37 -0
  24. package/lib/crypto/signature.js +410 -0
  25. package/lib/crypto/smartledger_verify.js +109 -0
  26. package/lib/ecies/bitcore-ecies.js +163 -0
  27. package/lib/ecies/electrum-ecies.js +175 -0
  28. package/lib/ecies/errors.js +16 -0
  29. package/lib/ecies/index.js +1 -0
  30. package/lib/encoding/base58.js +108 -0
  31. package/lib/encoding/base58check.js +112 -0
  32. package/lib/encoding/bufferreader.js +200 -0
  33. package/lib/encoding/bufferwriter.js +150 -0
  34. package/lib/encoding/varint.js +71 -0
  35. package/lib/errors/index.js +57 -0
  36. package/lib/errors/spec.js +184 -0
  37. package/lib/hdprivatekey.js +655 -0
  38. package/lib/hdpublickey.js +509 -0
  39. package/lib/message/index.js +4 -0
  40. package/lib/message/message.js +181 -0
  41. package/lib/mnemonic/errors.js +18 -0
  42. package/lib/mnemonic/index.js +4 -0
  43. package/lib/mnemonic/mnemonic.js +304 -0
  44. package/lib/mnemonic/pbkdf2.js +68 -0
  45. package/lib/mnemonic/words/chinese.js +5 -0
  46. package/lib/mnemonic/words/english.js +5 -0
  47. package/lib/mnemonic/words/french.js +5 -0
  48. package/lib/mnemonic/words/index.js +8 -0
  49. package/lib/mnemonic/words/italian.js +5 -0
  50. package/lib/mnemonic/words/japanese.js +5 -0
  51. package/lib/mnemonic/words/spanish.js +5 -0
  52. package/lib/networks.js +392 -0
  53. package/lib/opcode.js +248 -0
  54. package/lib/privatekey.js +373 -0
  55. package/lib/publickey.js +387 -0
  56. package/lib/script/index.js +3 -0
  57. package/lib/script/interpreter.js +1807 -0
  58. package/lib/script/script.js +1153 -0
  59. package/lib/transaction/index.js +7 -0
  60. package/lib/transaction/input/index.js +6 -0
  61. package/lib/transaction/input/input.js +202 -0
  62. package/lib/transaction/input/multisig.js +220 -0
  63. package/lib/transaction/input/multisigscripthash.js +189 -0
  64. package/lib/transaction/input/publickey.js +96 -0
  65. package/lib/transaction/input/publickeyhash.js +103 -0
  66. package/lib/transaction/output.js +192 -0
  67. package/lib/transaction/sighash.js +288 -0
  68. package/lib/transaction/signature.js +88 -0
  69. package/lib/transaction/transaction.js +1208 -0
  70. package/lib/transaction/unspentoutput.js +97 -0
  71. package/lib/util/_.js +44 -0
  72. package/lib/util/js.js +91 -0
  73. package/lib/util/preconditions.js +34 -0
  74. package/message/index.js +1 -0
  75. package/mnemonic/index.js +1 -0
  76. package/package.json +86 -0
@@ -0,0 +1,175 @@
1
+ 'use strict'
2
+
3
+ var bsv = require('../../')
4
+
5
+ var PublicKey = bsv.PublicKey
6
+ var PrivateKey = bsv.PrivateKey
7
+ var Hash = bsv.crypto.Hash
8
+ var $ = bsv.util.preconditions
9
+ var bitcoreECIES = require('./bitcore-ecies')
10
+ var errors = require('./errors')
11
+ var aesjs = require('aes-js')
12
+ var CBC = aesjs.ModeOfOperation.cbc
13
+
14
+ var AESCBC = function AESCBC () {}
15
+
16
+ AESCBC.encrypt = function (messagebuf, keybuf, ivbuf) {
17
+ $.checkArgument(messagebuf)
18
+ $.checkArgument(keybuf)
19
+ $.checkArgument(ivbuf)
20
+ $.checkArgument(keybuf.length === 16, 'keybuf length must be 16')
21
+ $.checkArgument(ivbuf.length === 16, 'ivbuf length must be 16')
22
+ messagebuf = aesjs.padding.pkcs7.pad(messagebuf)
23
+ var aesCbc = new CBC(keybuf, ivbuf)
24
+ var encryptedBytes = aesCbc.encrypt(messagebuf)
25
+ return Buffer.from(encryptedBytes)
26
+ }
27
+
28
+ AESCBC.decrypt = function (encbuf, keybuf, ivbuf) {
29
+ $.checkArgument(encbuf)
30
+ $.checkArgument(keybuf)
31
+ $.checkArgument(ivbuf)
32
+ $.checkArgument(keybuf.length === 16, 'keybuf length must be 16')
33
+ $.checkArgument(ivbuf.length === 16, 'ivbuf length must be 16')
34
+ var aesCbc = new CBC(keybuf, ivbuf)
35
+ var decryptedBytes = aesCbc.decrypt(encbuf)
36
+ return Buffer.from(aesjs.padding.pkcs7.strip(decryptedBytes))
37
+ }
38
+
39
+ // Electrum BIE1 ECIES
40
+ // Difference from Original Bitcore ECIES:
41
+ // BIE1:
42
+ // 1.Secret S is compressed P(33 bytes), while Bitcore's Secret is Px(32 bytes). See ivkEkM.
43
+ // 2.use AES-128-CBC instead of AES-256-CBC.
44
+ // 3.IV is retrived from first 16 bytes of Hashed Secret Buffer. See iv
45
+ // 4.key Encryption(kE) is retrived from 17-32 bytes of Hashed Secret Buffer, instead of 1-32 bytes. See kE
46
+ // 5.a magic word 'BIE1' is used to identify message type.
47
+ // 6.ephemeral key is introduced, so you can encrypt messages only with public key.
48
+ // 7.HMAC is whole message.
49
+ // Notice:
50
+ // Electrum does not support shortTag nor noKey
51
+ // Do NOT use those 2 options if you are to send a message to Electrum
52
+ // Encrypted message is NOT recoverable if you use ephemeral key
53
+ // Security:
54
+ //
55
+
56
+ // Default algorithm is set to BIE1, however original Bitcore ECIES is still preserved.
57
+ var ECIES = function ECIES (opts, algorithm = 'BIE1') {
58
+ if (algorithm !== 'BIE1') throw new errors.UnsupportAlgorithm(algorithm)
59
+ if (!(this instanceof ECIES)) {
60
+ return new ECIES(opts, algorithm)
61
+ }
62
+ // use ephemeral key if privateKey is not set.
63
+ this._privateKey = new bsv.PrivateKey()
64
+ this.opts = opts || {}
65
+ this.opts.ephemeralKey = true
66
+ }
67
+
68
+ ECIES.prototype.privateKey = function (privateKey) {
69
+ $.checkArgument(PrivateKey.isValid(privateKey), 'no private key provided')
70
+
71
+ this._privateKey = PrivateKey(privateKey.toHex()) || null
72
+ this.opts.ephemeralKey = false
73
+
74
+ return this
75
+ }
76
+
77
+ ECIES.prototype.publicKey = function (publicKey) {
78
+ $.checkArgument(PublicKey.isValid(publicKey), 'no public key provided')
79
+
80
+ this._publicKey = PublicKey(publicKey.toString()) || null
81
+ if (this._publicKey != null) this.opts.fixedPublicKey = true
82
+
83
+ return this
84
+ }
85
+
86
+ var defineProperty = function (name, getter) {
87
+ var cachedName = '_' + name
88
+ Object.defineProperty(ECIES.prototype, name, {
89
+ configurable: false,
90
+ enumerable: true,
91
+ get: function () {
92
+ var value = this[cachedName]
93
+ value = this[cachedName] = getter.apply(this)
94
+ return value
95
+ }
96
+ })
97
+ }
98
+
99
+ defineProperty('Rbuf', function () {
100
+ return this._privateKey.publicKey.toDER(true)
101
+ })
102
+
103
+ defineProperty('ivkEkM', function () {
104
+ var r = this._privateKey.bn
105
+ var KB = this._publicKey.point
106
+ var P = KB.mul(r)
107
+ var S = PublicKey(P)
108
+ var Sbuf = S.toBuffer()
109
+ return Hash.sha512(Sbuf)
110
+ })
111
+
112
+ defineProperty('iv', function () {
113
+ return this.ivkEkM.slice(0, 16)
114
+ })
115
+
116
+ defineProperty('kE', function () {
117
+ return this.ivkEkM.slice(16, 32)
118
+ })
119
+
120
+ defineProperty('kM', function () {
121
+ return this.ivkEkM.slice(32, 64)
122
+ })
123
+
124
+ // Encrypts the message (String or Buffer).
125
+ ECIES.prototype.encrypt = function (message) {
126
+ if (!Buffer.isBuffer(message)) message = Buffer.from(message)
127
+ var ciphertext = AESCBC.encrypt(message, this.kE, this.iv)
128
+ var encbuf
129
+ var BIE1 = Buffer.from('BIE1')
130
+ if (this.opts.noKey && !this.opts.ephemeralKey) {
131
+ encbuf = Buffer.concat([BIE1, ciphertext])
132
+ } else {
133
+ encbuf = Buffer.concat([BIE1, this.Rbuf, ciphertext])
134
+ }
135
+ var hmac = Hash.sha256hmac(encbuf, this.kM)
136
+ if (this.opts.shortTag) hmac = hmac.slice(0, 4)
137
+ return Buffer.concat([encbuf, hmac])
138
+ }
139
+
140
+ ECIES.prototype.decrypt = function (encbuf) {
141
+ $.checkArgument(Buffer.isBuffer(encbuf), 'ciphetext must be a buffer')
142
+ var tagLength = 32
143
+ var offset = 4
144
+ if (this.opts.shortTag) {
145
+ tagLength = 4
146
+ }
147
+ var magic = encbuf.slice(0, 4)
148
+ if (!magic.equals(Buffer.from('BIE1'))) {
149
+ throw new errors.DecryptionError('Invalid Magic')
150
+ }
151
+ if (!this.opts.noKey) {
152
+ var pub
153
+ // BIE1 use compressed public key, length is always 33.
154
+ pub = encbuf.slice(4, 37)
155
+ if (this.opts.fixedPublicKey) console.log('Notice: Overriding PublicKey in message. Consider use "noKey" option if you are not sending message to electrum and do not want to use ephemeral key')
156
+ else this._publicKey = PublicKey.fromDER(pub)
157
+ offset = 37
158
+ }
159
+
160
+ var ciphertext = encbuf.slice(offset, encbuf.length - tagLength)
161
+ var hmac = encbuf.slice(encbuf.length - tagLength, encbuf.length)
162
+
163
+ var hmac2 = Hash.sha256hmac(encbuf.slice(0, encbuf.length - tagLength), this.kM)
164
+ if (this.opts.shortTag) hmac2 = hmac2.slice(0, 4)
165
+
166
+ if (!hmac.equals(hmac2)) {
167
+ throw new errors.DecryptionError('Invalid checksum')
168
+ }
169
+
170
+ return AESCBC.decrypt(ciphertext, this.kE, this.iv)
171
+ }
172
+
173
+ ECIES.bitcoreECIES = bitcoreECIES
174
+
175
+ module.exports = ECIES
@@ -0,0 +1,16 @@
1
+ 'use strict'
2
+
3
+ var spec = {
4
+ name: 'ECIES',
5
+ message: 'Internal Error on bsv-ecies Module {0}',
6
+ errors: [{
7
+ name: 'DecryptionError',
8
+ message: 'Invalid Message: {0}'
9
+ },
10
+ {
11
+ name: 'UnsupportAlgorithm',
12
+ message: 'Unsupport Algorithm: {0}'
13
+ }]
14
+ }
15
+
16
+ module.exports = require('../../').errors.extend(spec)
@@ -0,0 +1 @@
1
+ module.exports = require('./electrum-ecies')
@@ -0,0 +1,108 @@
1
+ 'use strict'
2
+
3
+ var _ = require('../util/_')
4
+ var bs58 = require('bs58')
5
+ var buffer = require('buffer')
6
+
7
+ /**
8
+ * The alphabet for the Bitcoin-specific Base 58 encoding distinguishes between
9
+ * lower case L and upper case i - neither of those characters are allowed to
10
+ * prevent accidentaly miscopying of letters.
11
+ */
12
+ var ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'.split('')
13
+
14
+ /**
15
+ * A Base58 object can encode/decoded Base 58, which is used primarily for
16
+ * string-formatted Bitcoin addresses and private keys. Addresses and private
17
+ * keys actually use an additional checksum, and so they actually use the
18
+ * Base58Check class.
19
+ *
20
+ * @param {object} obj Can be a string or buffer.
21
+ */
22
+ var Base58 = function Base58 (obj) {
23
+ if (!(this instanceof Base58)) {
24
+ return new Base58(obj)
25
+ }
26
+ if (Buffer.isBuffer(obj)) {
27
+ var buf = obj
28
+ this.fromBuffer(buf)
29
+ } else if (typeof obj === 'string') {
30
+ var str = obj
31
+ this.fromString(str)
32
+ }
33
+ }
34
+
35
+ Base58.validCharacters = function validCharacters (chars) {
36
+ if (buffer.Buffer.isBuffer(chars)) {
37
+ chars = chars.toString()
38
+ }
39
+ return _.every(_.map(chars, function (char) { return _.includes(ALPHABET, char) }))
40
+ }
41
+
42
+ Base58.prototype.set = function (obj) {
43
+ this.buf = obj.buf || this.buf || undefined
44
+ return this
45
+ }
46
+
47
+ /**
48
+ * Encode a buffer to Bsae 58.
49
+ *
50
+ * @param {Buffer} buf Any buffer to be encoded.
51
+ * @returns {string} A Base 58 encoded string.
52
+ */
53
+ Base58.encode = function (buf) {
54
+ if (!buffer.Buffer.isBuffer(buf)) {
55
+ throw new Error('Input should be a buffer')
56
+ }
57
+ return bs58.encode(buf)
58
+ }
59
+
60
+ /**
61
+ * Decode a Base 58 string to a buffer.
62
+ *
63
+ * @param {string} str A Base 58 encoded string.
64
+ * @returns {Buffer} The decoded buffer.
65
+ */
66
+ Base58.decode = function (str) {
67
+ if (typeof str !== 'string') {
68
+ throw new Error('Input should be a string')
69
+ }
70
+ return Buffer.from(bs58.decode(str))
71
+ }
72
+
73
+ Base58.prototype.fromBuffer = function (buf) {
74
+ this.buf = buf
75
+ return this
76
+ }
77
+
78
+ Base58.fromBuffer = function (buf) {
79
+ return new Base58().fromBuffer(buf)
80
+ }
81
+
82
+ Base58.fromHex = function (hex) {
83
+ return Base58.fromBuffer(Buffer.from(hex, 'hex'))
84
+ }
85
+
86
+ Base58.prototype.fromString = function (str) {
87
+ var buf = Base58.decode(str)
88
+ this.buf = buf
89
+ return this
90
+ }
91
+
92
+ Base58.fromString = function (str) {
93
+ return new Base58().fromString(str)
94
+ }
95
+
96
+ Base58.prototype.toBuffer = function () {
97
+ return this.buf
98
+ }
99
+
100
+ Base58.prototype.toHex = function () {
101
+ return this.toBuffer().toString('hex')
102
+ }
103
+
104
+ Base58.prototype.toString = function () {
105
+ return Base58.encode(this.buf)
106
+ }
107
+
108
+ module.exports = Base58
@@ -0,0 +1,112 @@
1
+ 'use strict'
2
+
3
+ var _ = require('../util/_')
4
+ var Base58 = require('./base58')
5
+ var buffer = require('buffer')
6
+ var sha256sha256 = require('../crypto/hash').sha256sha256
7
+
8
+ /**
9
+ * A Base58check object can encode/decodd Base 58, which is used primarily for
10
+ * string-formatted Bitcoin addresses and private keys. This is the same as
11
+ * Base58, except that it includes a checksum to prevent accidental mistypings.
12
+ *
13
+ * @param {object} obj Can be a string or buffer.
14
+ */
15
+ var Base58Check = function Base58Check (obj) {
16
+ if (!(this instanceof Base58Check)) { return new Base58Check(obj) }
17
+ if (Buffer.isBuffer(obj)) {
18
+ var buf = obj
19
+ this.fromBuffer(buf)
20
+ } else if (typeof obj === 'string') {
21
+ var str = obj
22
+ this.fromString(str)
23
+ }
24
+ }
25
+
26
+ Base58Check.prototype.set = function (obj) {
27
+ this.buf = obj.buf || this.buf || undefined
28
+ return this
29
+ }
30
+
31
+ Base58Check.validChecksum = function validChecksum (data, checksum) {
32
+ if (_.isString(data)) {
33
+ data = buffer.Buffer.from(Base58.decode(data))
34
+ }
35
+ if (_.isString(checksum)) {
36
+ checksum = buffer.Buffer.from(Base58.decode(checksum))
37
+ }
38
+ if (!checksum) {
39
+ checksum = data.slice(-4)
40
+ data = data.slice(0, -4)
41
+ }
42
+ return Base58Check.checksum(data).toString('hex') === checksum.toString('hex')
43
+ }
44
+
45
+ Base58Check.decode = function (s) {
46
+ if (typeof s !== 'string') { throw new Error('Input must be a string') }
47
+
48
+ var buf = Buffer.from(Base58.decode(s))
49
+
50
+ if (buf.length < 4) { throw new Error('Input string too short') }
51
+
52
+ var data = buf.slice(0, -4)
53
+ var csum = buf.slice(-4)
54
+
55
+ var hash = sha256sha256(data)
56
+ var hash4 = hash.slice(0, 4)
57
+
58
+ if (csum.toString('hex') !== hash4.toString('hex')) { throw new Error('Checksum mismatch') }
59
+
60
+ return data
61
+ }
62
+
63
+ Base58Check.checksum = function (buffer) {
64
+ return sha256sha256(buffer).slice(0, 4)
65
+ }
66
+
67
+ Base58Check.encode = function (buf) {
68
+ if (!Buffer.isBuffer(buf)) { throw new Error('Input must be a buffer') }
69
+ var checkedBuf = Buffer.alloc(buf.length + 4)
70
+ var hash = Base58Check.checksum(buf)
71
+ buf.copy(checkedBuf)
72
+ hash.copy(checkedBuf, buf.length)
73
+ return Base58.encode(checkedBuf)
74
+ }
75
+
76
+ Base58Check.prototype.fromBuffer = function (buf) {
77
+ this.buf = buf
78
+ return this
79
+ }
80
+
81
+ Base58Check.fromBuffer = function (buf) {
82
+ return new Base58Check().fromBuffer(buf)
83
+ }
84
+
85
+ Base58Check.fromHex = function (hex) {
86
+ return Base58Check.fromBuffer(Buffer.from(hex, 'hex'))
87
+ }
88
+
89
+ Base58Check.prototype.fromString = function (str) {
90
+ var buf = Base58Check.decode(str)
91
+ this.buf = buf
92
+ return this
93
+ }
94
+
95
+ Base58Check.fromString = function (str) {
96
+ var buf = Base58Check.decode(str)
97
+ return new Base58(buf)
98
+ }
99
+
100
+ Base58Check.prototype.toBuffer = function () {
101
+ return this.buf
102
+ }
103
+
104
+ Base58Check.prototype.toHex = function () {
105
+ return this.toBuffer().toString('hex')
106
+ }
107
+
108
+ Base58Check.prototype.toString = function () {
109
+ return Base58Check.encode(this.buf)
110
+ }
111
+
112
+ module.exports = Base58Check
@@ -0,0 +1,200 @@
1
+ 'use strict'
2
+
3
+ var _ = require('../util/_')
4
+ var $ = require('../util/preconditions')
5
+ var BN = require('../crypto/bn')
6
+
7
+ var BufferReader = function BufferReader (buf) {
8
+ if (!(this instanceof BufferReader)) {
9
+ return new BufferReader(buf)
10
+ }
11
+ if (_.isUndefined(buf)) {
12
+ return
13
+ }
14
+ if (Buffer.isBuffer(buf)) {
15
+ this.set({
16
+ buf: buf
17
+ })
18
+ } else if (_.isString(buf)) {
19
+ var b = Buffer.from(buf, 'hex')
20
+ if (b.length * 2 !== buf.length) { throw new TypeError('Invalid hex string') }
21
+
22
+ this.set({
23
+ buf: b
24
+ })
25
+ } else if (_.isObject(buf)) {
26
+ var obj = buf
27
+ this.set(obj)
28
+ } else {
29
+ throw new TypeError('Unrecognized argument for BufferReader')
30
+ }
31
+ }
32
+
33
+ BufferReader.prototype.set = function (obj) {
34
+ this.buf = obj.buf || this.buf || undefined
35
+ this.pos = obj.pos || this.pos || 0
36
+ return this
37
+ }
38
+
39
+ BufferReader.prototype.eof = function () {
40
+ return this.pos >= this.buf.length
41
+ }
42
+
43
+ BufferReader.prototype.finished = BufferReader.prototype.eof
44
+
45
+ BufferReader.prototype.read = function (len) {
46
+ $.checkArgument(!_.isUndefined(len), 'Must specify a length')
47
+ var buf = this.buf.slice(this.pos, this.pos + len)
48
+ this.pos = this.pos + len
49
+ return buf
50
+ }
51
+
52
+ BufferReader.prototype.readAll = function () {
53
+ var buf = this.buf.slice(this.pos, this.buf.length)
54
+ this.pos = this.buf.length
55
+ return buf
56
+ }
57
+
58
+ BufferReader.prototype.readUInt8 = function () {
59
+ var val = this.buf.readUInt8(this.pos)
60
+ this.pos = this.pos + 1
61
+ return val
62
+ }
63
+
64
+ BufferReader.prototype.readUInt16BE = function () {
65
+ var val = this.buf.readUInt16BE(this.pos)
66
+ this.pos = this.pos + 2
67
+ return val
68
+ }
69
+
70
+ BufferReader.prototype.readUInt16LE = function () {
71
+ var val = this.buf.readUInt16LE(this.pos)
72
+ this.pos = this.pos + 2
73
+ return val
74
+ }
75
+
76
+ BufferReader.prototype.readUInt32BE = function () {
77
+ var val = this.buf.readUInt32BE(this.pos)
78
+ this.pos = this.pos + 4
79
+ return val
80
+ }
81
+
82
+ BufferReader.prototype.readUInt32LE = function () {
83
+ var val = this.buf.readUInt32LE(this.pos)
84
+ this.pos = this.pos + 4
85
+ return val
86
+ }
87
+
88
+ BufferReader.prototype.readInt32LE = function () {
89
+ var val = this.buf.readInt32LE(this.pos)
90
+ this.pos = this.pos + 4
91
+ return val
92
+ }
93
+
94
+ BufferReader.prototype.readUInt64BEBN = function () {
95
+ var buf = this.buf.slice(this.pos, this.pos + 8)
96
+ var bn = BN.fromBuffer(buf)
97
+ this.pos = this.pos + 8
98
+ return bn
99
+ }
100
+
101
+ BufferReader.prototype.readUInt64LEBN = function () {
102
+ var second = this.buf.readUInt32LE(this.pos)
103
+ var first = this.buf.readUInt32LE(this.pos + 4)
104
+ var combined = (first * 0x100000000) + second
105
+ // Instantiating an instance of BN with a number is faster than with an
106
+ // array or string. However, the maximum safe number for a double precision
107
+ // floating point is 2 ^ 52 - 1 (0x1fffffffffffff), thus we can safely use
108
+ // non-floating point numbers less than this amount (52 bits). And in the case
109
+ // that the number is larger, we can instatiate an instance of BN by passing
110
+ // an array from the buffer (slower) and specifying the endianness.
111
+ var bn
112
+ if (combined <= 0x1fffffffffffff) {
113
+ bn = new BN(combined)
114
+ } else {
115
+ var data = Array.prototype.slice.call(this.buf, this.pos, this.pos + 8)
116
+ bn = new BN(data, 10, 'le')
117
+ }
118
+ this.pos = this.pos + 8
119
+ return bn
120
+ }
121
+
122
+ BufferReader.prototype.readVarintNum = function () {
123
+ var first = this.readUInt8()
124
+ switch (first) {
125
+ case 0xFD:
126
+ return this.readUInt16LE()
127
+ case 0xFE:
128
+ return this.readUInt32LE()
129
+ case 0xFF:
130
+ var bn = this.readUInt64LEBN()
131
+ var n = bn.toNumber()
132
+ if (n <= Math.pow(2, 53)) {
133
+ return n
134
+ } else {
135
+ throw new Error('number too large to retain precision - use readVarintBN')
136
+ }
137
+ // break // unreachable
138
+ default:
139
+ return first
140
+ }
141
+ }
142
+
143
+ /**
144
+ * reads a length prepended buffer
145
+ */
146
+ BufferReader.prototype.readVarLengthBuffer = function () {
147
+ var len = this.readVarintNum()
148
+ var buf = this.read(len)
149
+ $.checkState(buf.length === len, 'Invalid length while reading varlength buffer. ' +
150
+ 'Expected to read: ' + len + ' and read ' + buf.length)
151
+ return buf
152
+ }
153
+
154
+ BufferReader.prototype.readVarintBuf = function () {
155
+ var first = this.buf.readUInt8(this.pos)
156
+ switch (first) {
157
+ case 0xFD:
158
+ return this.read(1 + 2)
159
+ case 0xFE:
160
+ return this.read(1 + 4)
161
+ case 0xFF:
162
+ return this.read(1 + 8)
163
+ default:
164
+ return this.read(1)
165
+ }
166
+ }
167
+
168
+ BufferReader.prototype.readVarintBN = function () {
169
+ var first = this.readUInt8()
170
+ switch (first) {
171
+ case 0xFD:
172
+ return new BN(this.readUInt16LE())
173
+ case 0xFE:
174
+ return new BN(this.readUInt32LE())
175
+ case 0xFF:
176
+ return this.readUInt64LEBN()
177
+ default:
178
+ return new BN(first)
179
+ }
180
+ }
181
+
182
+ BufferReader.prototype.reverse = function () {
183
+ var buf = Buffer.alloc(this.buf.length)
184
+ for (var i = 0; i < buf.length; i++) {
185
+ buf[i] = this.buf[this.buf.length - 1 - i]
186
+ }
187
+ this.buf = buf
188
+ return this
189
+ }
190
+
191
+ BufferReader.prototype.readReverse = function (len) {
192
+ if (_.isUndefined(len)) {
193
+ len = this.buf.length
194
+ }
195
+ var buf = this.buf.slice(this.pos, this.pos + len)
196
+ this.pos = this.pos + len
197
+ return Buffer.from(buf).reverse()
198
+ }
199
+
200
+ module.exports = BufferReader