bsv2 2.0.24 → 2.1.1

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 (170) hide show
  1. package/dist/bsv2.mjs +36061 -4
  2. package/package.json +35 -39
  3. package/.babelrc.test +0 -10
  4. package/.editorconfig +0 -4
  5. package/.env.defaults +0 -1
  6. package/.env.schema +0 -1
  7. package/.husky/pre-commit +0 -1
  8. package/.mocharc.yml +0 -4
  9. package/.nvmrc +0 -1
  10. package/.travis.yml +0 -7
  11. package/benchmark.js +0 -50
  12. package/bin/bsv.js +0 -11
  13. package/bin/network.js +0 -92
  14. package/build/index.html +0 -20
  15. package/dist/bsv2.bundle.js +0 -8
  16. package/dist/bsv2.cjs.js +0 -2
  17. package/dist/bsv2.cjs.js.LICENSE.txt +0 -1
  18. package/dist/bsv2.module.js +0 -2
  19. package/entry.js +0 -82
  20. package/index.js +0 -102
  21. package/karma.conf.js +0 -78
  22. package/lib/ach.js +0 -59
  23. package/lib/address.js +0 -264
  24. package/lib/aes.js +0 -57
  25. package/lib/aescbc.js +0 -38
  26. package/lib/base-58-check.js +0 -83
  27. package/lib/base-58.js +0 -59
  28. package/lib/bip-32.js +0 -472
  29. package/lib/bip-39-en-wordlist.js +0 -2054
  30. package/lib/bip-39-en.js +0 -10
  31. package/lib/bip-39-es-wordlist.js +0 -2055
  32. package/lib/bip-39-es.js +0 -10
  33. package/lib/bip-39-jp-wordlist.js +0 -2053
  34. package/lib/bip-39-jp.js +0 -10
  35. package/lib/bip-39-words.js +0 -5
  36. package/lib/bip-39.js +0 -285
  37. package/lib/block-header.js +0 -79
  38. package/lib/block.js +0 -132
  39. package/lib/bn.js +0 -467
  40. package/lib/br.js +0 -172
  41. package/lib/bsm.js +0 -127
  42. package/lib/bw.js +0 -188
  43. package/lib/cbc.js +0 -140
  44. package/lib/cmp.js +0 -37
  45. package/lib/config.js +0 -50
  46. package/lib/constants.js +0 -150
  47. package/lib/ecdsa.js +0 -495
  48. package/lib/ecies.js +0 -151
  49. package/lib/hash-cache.js +0 -41
  50. package/lib/hash.js +0 -191
  51. package/lib/interp.js +0 -2013
  52. package/lib/inv.js +0 -49
  53. package/lib/key-pair.js +0 -133
  54. package/lib/mainnet-address.js +0 -5
  55. package/lib/mainnet-bip-32.js +0 -5
  56. package/lib/mainnet-key-pair.js +0 -5
  57. package/lib/mainnet-priv-key.js +0 -5
  58. package/lib/merkle.js +0 -92
  59. package/lib/msg.js +0 -184
  60. package/lib/op-code.js +0 -398
  61. package/lib/point.js +0 -134
  62. package/lib/priv-key.js +0 -157
  63. package/lib/pub-key.js +0 -230
  64. package/lib/random.js +0 -28
  65. package/lib/script.js +0 -826
  66. package/lib/sig-operations.js +0 -120
  67. package/lib/sig.js +0 -399
  68. package/lib/struct.js +0 -389
  69. package/lib/testnet-address.js +0 -5
  70. package/lib/testnet-bip-32.js +0 -5
  71. package/lib/testnet-key-pair.js +0 -5
  72. package/lib/testnet-priv-key.js +0 -5
  73. package/lib/tx-builder.js +0 -593
  74. package/lib/tx-in.js +0 -172
  75. package/lib/tx-out-map.js +0 -57
  76. package/lib/tx-out.js +0 -73
  77. package/lib/tx-verifier.js +0 -209
  78. package/lib/tx.js +0 -430
  79. package/lib/var-int.js +0 -73
  80. package/lib/version.js +0 -92
  81. package/lib/workers-result.js +0 -65
  82. package/lib/workers.js +0 -77
  83. package/load-babel.js +0 -3
  84. package/mtest/test.html +0 -15
  85. package/mtest/test.js +0 -2
  86. package/mtest/test.mjs +0 -2
  87. package/test/ach.js +0 -172
  88. package/test/address.js +0 -464
  89. package/test/aes.js +0 -96
  90. package/test/aescbc.js +0 -65
  91. package/test/base-58-check.js +0 -128
  92. package/test/base-58.js +0 -113
  93. package/test/bip-32.js +0 -721
  94. package/test/bip-39.js +0 -282
  95. package/test/block-header.js +0 -152
  96. package/test/block.js +0 -243
  97. package/test/bn.js +0 -643
  98. package/test/br.js +0 -353
  99. package/test/bsm.js +0 -141
  100. package/test/bw.js +0 -292
  101. package/test/cbc.js +0 -335
  102. package/test/cmp.js +0 -56
  103. package/test/data/bip69.json +0 -215
  104. package/test/data/bitcoind/base58_keys_invalid.json +0 -152
  105. package/test/data/bitcoind/base58_keys_valid.json +0 -452
  106. package/test/data/bitcoind/blocks.json +0 -27
  107. package/test/data/bitcoind/script_tests.json +0 -2244
  108. package/test/data/bitcoind/sig_canonical.json +0 -7
  109. package/test/data/bitcoind/sig_noncanonical.json +0 -22
  110. package/test/data/bitcoind/tx_invalid.json +0 -177
  111. package/test/data/bitcoind/tx_valid.json +0 -224
  112. package/test/data/blk86756-testnet.dat +0 -0
  113. package/test/data/blk86756-testnet.js +0 -12
  114. package/test/data/blk86756-testnet.json +0 -684
  115. package/test/data/ecdsa.json +0 -230
  116. package/test/data/messages.json +0 -22
  117. package/test/data/sighash.json +0 -1004
  118. package/test/data/tx_creation.json +0 -85
  119. package/test/ecdsa.js +0 -748
  120. package/test/ecies.js +0 -238
  121. package/test/global.js +0 -13
  122. package/test/hash-cache.js +0 -52
  123. package/test/hash.js +0 -354
  124. package/test/index.js +0 -10
  125. package/test/interp.js +0 -512
  126. package/test/inv.js +0 -65
  127. package/test/key-pair.js +0 -243
  128. package/test/merkle.js +0 -179
  129. package/test/msg.js +0 -295
  130. package/test/op-code.js +0 -79
  131. package/test/point.js +0 -375
  132. package/test/priv-key.js +0 -264
  133. package/test/pub-key.js +0 -549
  134. package/test/random.js +0 -31
  135. package/test/script.js +0 -1340
  136. package/test/sig-operations.js +0 -149
  137. package/test/sig.js +0 -662
  138. package/test/struct.js +0 -552
  139. package/test/tx-builder.js +0 -2104
  140. package/test/tx-in.js +0 -198
  141. package/test/tx-out-map.js +0 -104
  142. package/test/tx-out.js +0 -162
  143. package/test/tx-verifier.js +0 -246
  144. package/test/tx.js +0 -539
  145. package/test/var-int.js +0 -132
  146. package/test/vectors/aes.json +0 -10392
  147. package/test/vectors/aescbc.json +0 -620
  148. package/test/vectors/bip39.json +0 -502
  149. package/test/vectors/bip69.json +0 -215
  150. package/test/vectors/bitcoin-abc/script_tests.json +0 -48
  151. package/test/vectors/bitcoin-abc/sighash.json +0 -7004
  152. package/test/vectors/bitcoin-sv/script_tests.json +0 -248
  153. package/test/vectors/bitcoind/script_invalid.json +0 -1151
  154. package/test/vectors/bitcoind/script_valid.json +0 -1462
  155. package/test/vectors/bitcoind/sighash.json +0 -3503
  156. package/test/vectors/bitcoind/tx_invalid.json +0 -883
  157. package/test/vectors/bitcoind/tx_valid.json +0 -1150
  158. package/test/vectors/coolest-tx-ever-sent.json +0 -8
  159. package/test/vectors/ecdsa.json +0 -300
  160. package/test/vectors/hash.json +0 -6133
  161. package/test/vectors/kdf.json +0 -47
  162. package/test/vectors/largesttx.json +0 -5
  163. package/test/vectors/largesttxblock.json +0 -6
  164. package/test/vectors/sig.json +0 -79
  165. package/test/vectors/sighash-single-bug.json +0 -8
  166. package/test/version.js +0 -81
  167. package/test/workers-result.js +0 -89
  168. package/test/workers.js +0 -91
  169. package/test-index.js +0 -3
  170. package/webpack.config.js +0 -115
package/lib/bn.js DELETED
@@ -1,467 +0,0 @@
1
- /**
2
- * Big Number
3
- * ==========
4
- *
5
- * Since javascript numbers are only precise up to 53 bits, and bitcoin is
6
- * based on cryptography that uses 256 bit numbers, we must use a big number
7
- * library. The library we use at the moment is Fedor Indutny's bn.js library.
8
- * Since big numbers are extremely useful, we provide some very basic wrappers
9
- * for his big number class and expose it. The wrappers merely allow you to do,
10
- * say, bn.cmp(num) instead of just bn.cmp(bn), which is nice. The primary way
11
- * to use this is:
12
- * const bn = Bn(str) // str is base 10
13
- * const bn = Bn(num)
14
- * const bn = Bn().fromBuffer(buf)
15
- * const bn = Bn().fromSm(buf); // sign+magnitude format, first bit is sign
16
- *
17
- * For little endian, pass in an options value:
18
- * const bn = Bn().fromBuffer(buf, {endian: 'little'})
19
- * const bn = Bn().fromSm(buf, {endian: 'little'})
20
- *
21
- * Getting output:
22
- * const str = Bn().toString() // produces base 10 string
23
- * const buf = Bn().toBuffer() // produces buffer representation
24
- * const buf = Bn().toBuffer({size: 32}) //produced 32 byte buffer
25
- */
26
- 'use strict'
27
-
28
- import _Bn from 'bn.js'
29
-
30
- function Bn (n, base, ...rest) {
31
- if (!(this instanceof Bn)) {
32
- return new Bn(n, base, ...rest)
33
- }
34
- _Bn.call(this, n, base, ...rest)
35
- }
36
-
37
- Object.keys(_Bn).forEach(function (key) {
38
- Bn[key] = _Bn[key]
39
- })
40
- Bn.prototype = Object.create(_Bn.prototype)
41
- Bn.prototype.constructor = Bn
42
-
43
- function reverseBuf (buf) {
44
- const buf2 = Buffer.alloc(buf.length)
45
- for (let i = 0; i < buf.length; i++) {
46
- buf2[i] = buf[buf.length - 1 - i]
47
- }
48
- return buf2
49
- }
50
-
51
- Bn.prototype.fromHex = function (hex, opts) {
52
- return this.fromBuffer(Buffer.from(hex, 'hex'), opts)
53
- }
54
-
55
- Bn.prototype.toHex = function (opts) {
56
- return this.toBuffer(opts).toString('hex')
57
- }
58
-
59
- Bn.prototype.toJSON = function () {
60
- return this.toString()
61
- }
62
-
63
- Bn.prototype.fromJSON = function (str) {
64
- const bn = Bn(str)
65
- bn.copy(this)
66
- return this
67
- }
68
-
69
- Bn.prototype.fromNumber = function (n) {
70
- const bn = Bn(n)
71
- bn.copy(this)
72
- return this
73
- }
74
-
75
- Bn.prototype.toNumber = function () {
76
- return parseInt(this.toString(10), 10)
77
- }
78
-
79
- Bn.prototype.fromString = function (str, base) {
80
- const bn = Bn(str, base)
81
- bn.copy(this)
82
- return this
83
- }
84
-
85
- Bn.fromBuffer = function (buf, opts = { endian: 'big' }) {
86
- if (opts.endian === 'little') {
87
- buf = reverseBuf(buf)
88
- }
89
- const hex = buf.toString('hex')
90
- const bn = new Bn(hex, 16)
91
- return bn
92
- }
93
-
94
- Bn.prototype.fromBuffer = function (buf, opts) {
95
- const bn = Bn.fromBuffer(buf, opts)
96
- bn.copy(this)
97
-
98
- return this
99
- }
100
-
101
- Bn.prototype.toBuffer = function (opts = { size: undefined, endian: 'big' }) {
102
- let buf
103
- if (opts.size) {
104
- const hex = this.toString(16, 2)
105
- const natlen = hex.length / 2
106
- buf = Buffer.from(hex, 'hex')
107
-
108
- if (natlen === opts.size) {
109
- // pass
110
- } else if (natlen > opts.size) {
111
- buf = buf.slice(natlen - buf.length, buf.length)
112
- } else if (natlen < opts.size) {
113
- const rbuf = Buffer.alloc(opts.size)
114
- for (let i = 0; i < buf.length; i++) {
115
- rbuf[rbuf.length - 1 - i] = buf[buf.length - 1 - i]
116
- }
117
- for (let i = 0; i < opts.size - natlen; i++) {
118
- rbuf[i] = 0
119
- }
120
- buf = rbuf
121
- }
122
- } else {
123
- const hex = this.toString(16, 2)
124
- buf = Buffer.from(hex, 'hex')
125
- }
126
-
127
- if (opts.endian === 'little') {
128
- buf = reverseBuf(buf)
129
- }
130
- const longzero = Buffer.from([0])
131
- if (Buffer.compare(buf, longzero) === 0) {
132
- return Buffer.from([])
133
- }
134
- return buf
135
- }
136
-
137
- Bn.prototype.toFastBuffer = Bn.prototype.toBuffer
138
-
139
- Bn.fromFastBuffer = Bn.fromBuffer
140
- Bn.prototype.fromFastBuffer = Bn.prototype.fromBuffer
141
-
142
- /**
143
- * Signed magnitude buffer. Most significant bit represents sign (0 = positive,
144
- * 1 = negative).
145
- */
146
- Bn.prototype.fromSm = function (buf, opts = { endian: 'big' }) {
147
- if (buf.length === 0) {
148
- this.fromBuffer(Buffer.from([0]))
149
- }
150
-
151
- const endian = opts.endian
152
- if (endian === 'little') {
153
- buf = reverseBuf(buf)
154
- }
155
-
156
- if (buf[0] & 0x80) {
157
- buf[0] = buf[0] & 0x7f
158
- this.fromBuffer(buf)
159
- this.neg().copy(this)
160
- } else {
161
- this.fromBuffer(buf)
162
- }
163
- return this
164
- }
165
-
166
- Bn.prototype.toSm = function (opts = { endian: 'big' }) {
167
- const endian = opts.endian
168
-
169
- let buf
170
- if (this.cmp(0) === -1) {
171
- buf = this.neg().toBuffer()
172
- if (buf[0] & 0x80) {
173
- buf = Buffer.concat([Buffer.from([0x80]), buf])
174
- } else {
175
- buf[0] = buf[0] | 0x80
176
- }
177
- } else {
178
- buf = this.toBuffer()
179
- if (buf[0] & 0x80) {
180
- buf = Buffer.concat([Buffer.from([0x00]), buf])
181
- }
182
- }
183
-
184
- if ((buf.length === 1) & (buf[0] === 0)) {
185
- buf = Buffer.from([])
186
- }
187
-
188
- if (endian === 'little') {
189
- buf = reverseBuf(buf)
190
- }
191
-
192
- return buf
193
- }
194
-
195
- /**
196
- * Produce a Bn from the "bits" value in a blockheader. Analagous to Bitcoin
197
- * Core's uint256 SetCompact method. bits is assumed to be UInt32.
198
- */
199
- Bn.prototype.fromBits = function (bits, opts = { strict: false }) {
200
- // To performed bitwise operations in javascript, we need to convert to a
201
- // signed 32 bit value.
202
- let buf = Buffer.alloc(4)
203
- buf.writeUInt32BE(bits, 0)
204
- bits = buf.readInt32BE(0)
205
- if (opts.strict && bits & 0x00800000) {
206
- throw new Error('negative bit set')
207
- }
208
- const nsize = bits >> 24
209
- const nword = bits & 0x007fffff
210
- buf = Buffer.alloc(4)
211
- buf.writeInt32BE(nword)
212
- if (nsize <= 3) {
213
- buf = buf.slice(1, nsize + 1)
214
- } else {
215
- const fill = Buffer.alloc(nsize - 3)
216
- fill.fill(0)
217
- buf = Buffer.concat([buf, fill])
218
- }
219
- this.fromBuffer(buf)
220
- if (bits & 0x00800000) {
221
- Bn(0)
222
- .sub(this)
223
- .copy(this)
224
- }
225
- return this
226
- }
227
-
228
- /**
229
- * Convert Bn to the "bits" value in a blockheader. Analagous to Bitcoin
230
- * Core's uint256 GetCompact method. bits is a UInt32.
231
- */
232
- Bn.prototype.toBits = function () {
233
- let buf
234
- if (this.lt(0)) {
235
- buf = this.neg().toBuffer()
236
- } else {
237
- buf = this.toBuffer()
238
- }
239
- let nsize = buf.length
240
- let nword
241
- if (nsize > 3) {
242
- nword = Buffer.concat([Buffer.from([0]), buf.slice(0, 3)]).readUInt32BE(0)
243
- } else if (nsize <= 3) {
244
- const blank = Buffer.alloc(3 - nsize + 1)
245
- blank.fill(0)
246
- nword = Buffer.concat([blank, buf.slice(0, nsize)]).readUInt32BE(0)
247
- }
248
- if (nword & 0x00800000) {
249
- // The most significant bit denotes sign. Do not want unless number is
250
- // actually negative.
251
- nword >>= 8
252
- nsize++
253
- }
254
- if (this.lt(0)) {
255
- nword |= 0x00800000
256
- }
257
- const bits = (nsize << 24) | nword
258
- // convert bits to UInt32 before returning
259
- buf = Buffer.alloc(4)
260
- buf.writeInt32BE(bits, 0)
261
- return buf.readUInt32BE(0)
262
- }
263
-
264
- // This is analogous to the constructor for CScriptNum in bitcoind. Many ops
265
- // in bitcoind's script interpreter use CScriptNum, which is not really a
266
- // proper bignum. Instead, an error is thrown if trying to input a number
267
- // bigger than 4 bytes. We copy that behavior here. There is one exception -
268
- // in CHECKLOCKTIMEVERIFY, the numbers are allowed to be up to 5 bytes long.
269
- // We allow for setting that variable here for use in CHECKLOCKTIMEVERIFY.
270
- Bn.prototype.fromScriptNumBuffer = function (
271
- buf,
272
- fRequireMinimal,
273
- nMaxNumSize,
274
- big_int = false
275
- ) {
276
-
277
- if (nMaxNumSize === undefined) {
278
- nMaxNumSize = 4
279
- }
280
- if (buf.length > nMaxNumSize) {
281
- throw new Error(`script number overflow nMaxNumSize: ${nMaxNumSize}`)
282
- }
283
-
284
-
285
- if (fRequireMinimal && buf.length > 0) {
286
- // Check that the number is encoded with the minimum possible
287
- // number of bytes.
288
- //
289
- // If the most-significant-byte - excluding the sign bit - is zero
290
- // then we're not minimal. Note how this test also rejects the
291
- // negative-zero encoding, 0x80.
292
- if ((buf[buf.length - 1] & 0x7f) === 0) {
293
- // One exception: if there's more than one byte and the most
294
- // significant bit of the second-most-significant-byte is set
295
- // it would conflict with the sign bit. An example of this case
296
- // is +-255, which encode to 0xff00 and 0xff80 respectively.
297
- // (big-endian).
298
- if (buf.length <= 1 || (buf[buf.length - 2] & 0x80) === 0) {
299
- throw new Error('non-minimally encoded script number')
300
- }
301
- }
302
- }
303
- // TODO?
304
- // https://github.com/bitcoin-sv/bitcoin-sv/blob/4d3444909a5a98f129d3d2991bf8f0f7f15d3969/src/script/script_num.cpp#L34
305
- if (buf.byteLength===0){
306
- if(big_int){
307
- //m_value = bint{0};
308
- }
309
- }else if(buf.byteLength <= nMaxNumSize){
310
- if (big_int){
311
- // m_value = bsv::bint::deserialize(span);
312
-
313
- }else{
314
- // m_value = bsv::deserialize<int64_t>(span.begin(), span.end());
315
-
316
-
317
- }
318
-
319
- }
320
-
321
- return this.fromSm(buf, { endian: 'little' })
322
- }
323
-
324
- // The corollary to the above, with the notable exception that we do not throw
325
- // an error if the output is larger than four bytes. (Which can happen if
326
- // performing a numerical operation that results in an overflow to more than 4
327
- // bytes).
328
- Bn.prototype.toScriptNumBuffer = function (buf) {
329
- return this.toSm({ endian: 'little' })
330
- }
331
-
332
- Bn.prototype.neg = function () {
333
- const _neg = _Bn.prototype.neg.call(this)
334
- const neg = Object.create(Bn.prototype)
335
- _neg.copy(neg)
336
- return neg
337
- }
338
-
339
- Bn.prototype.add = function (bn) {
340
- const _bn = _Bn.prototype.add.call(this, bn)
341
- bn = Object.create(Bn.prototype)
342
- _bn.copy(bn)
343
- return bn
344
- }
345
-
346
- Bn.prototype.sub = function (bn) {
347
- const _bn = _Bn.prototype.sub.call(this, bn)
348
- bn = Object.create(Bn.prototype)
349
- _bn.copy(bn)
350
- return bn
351
- }
352
-
353
- Bn.prototype.mul = function (bn) {
354
- const _bn = _Bn.prototype.mul.call(this, bn)
355
- bn = Object.create(Bn.prototype)
356
- _bn.copy(bn)
357
- return bn
358
- }
359
-
360
- /**
361
- * to be used if this is positive.
362
- */
363
- Bn.prototype.mod = function (bn) {
364
- const _bn = _Bn.prototype.mod.call(this, bn)
365
- bn = Object.create(Bn.prototype)
366
- _bn.copy(bn)
367
- return bn
368
- }
369
-
370
- /**
371
- * to be used if this is negative.
372
- */
373
- Bn.prototype.umod = function (bn) {
374
- const _bn = _Bn.prototype.umod.call(this, bn)
375
- bn = Object.create(Bn.prototype)
376
- _bn.copy(bn)
377
- return bn
378
- }
379
-
380
- Bn.prototype.invm = function (bn) {
381
- const _bn = _Bn.prototype.invm.call(this, bn)
382
- bn = Object.create(Bn.prototype)
383
- _bn.copy(bn)
384
- return bn
385
- }
386
-
387
- Bn.prototype.div = function (bn) {
388
- const _bn = _Bn.prototype.div.call(this, bn)
389
- bn = Object.create(Bn.prototype)
390
- _bn.copy(bn)
391
- return bn
392
- }
393
-
394
- Bn.prototype.ushln = function (bits) {
395
- const _bn = _Bn.prototype.ushln.call(this, bits)
396
- const bn = Object.create(Bn.prototype)
397
- _bn.copy(bn)
398
- return bn
399
- }
400
-
401
- Bn.prototype.ushrn = function (bits) {
402
- const _bn = _Bn.prototype.ushrn.call(this, bits)
403
- const bn = Object.create(Bn.prototype)
404
- _bn.copy(bn)
405
- return bn
406
- }
407
-
408
- Bn.prototype.cmp = function (bn) {
409
- return _Bn.prototype.cmp.call(this, bn)
410
- }
411
-
412
- /**
413
- * All the standard big number operations operate on other big numbers. e.g.,
414
- * bn1.add(bn2). But it is frequenly valuble to add numbers or strings, e.g.
415
- * bn.add(5) or bn.add('5'). The decorator wraps all methods where this would
416
- * be convenient and makes that possible.
417
- */
418
- function decorate (name) {
419
- Bn.prototype['_' + name] = Bn.prototype[name]
420
- const f = function (b) {
421
- if (typeof b === 'string') {
422
- b = new Bn(b)
423
- } else if (typeof b === 'number') {
424
- b = new Bn(b.toString())
425
- }
426
- return this['_' + name](b)
427
- }
428
- Bn.prototype[name] = f
429
- }
430
-
431
- Bn.prototype.eq = function (b) {
432
- return this.cmp(b) === 0
433
- }
434
-
435
- Bn.prototype.neq = function (b) {
436
- return this.cmp(b) !== 0
437
- }
438
-
439
- Bn.prototype.gt = function (b) {
440
- return this.cmp(b) > 0
441
- }
442
-
443
- Bn.prototype.geq = function (b) {
444
- return this.cmp(b) >= 0
445
- }
446
-
447
- Bn.prototype.lt = function (b) {
448
- return this.cmp(b) < 0
449
- }
450
-
451
- Bn.prototype.leq = function (b) {
452
- return this.cmp(b) <= 0
453
- }
454
-
455
- decorate('add')
456
- decorate('sub')
457
- decorate('mul')
458
- decorate('mod')
459
- decorate('invm')
460
- decorate('div')
461
- decorate('cmp')
462
- decorate('gt')
463
- decorate('geq')
464
- decorate('lt')
465
- decorate('leq')
466
-
467
- export { Bn }
package/lib/br.js DELETED
@@ -1,172 +0,0 @@
1
- /**
2
- * Buffer Reader
3
- * =============
4
- *
5
- * This is a convenience class for reading VarInts and other basic types from a
6
- * buffer. This class is most useful for reading VarInts, and also for signed
7
- * or unsigned integers of various types. It can also read a buffer in reverse
8
- * order, which is useful in bitcoin which uses little endian numbers a lot so
9
- * you find that you must reverse things. You probably want to use it like:
10
- * varInt = new Br(buf).readnew VarInt()
11
- */
12
- 'use strict'
13
-
14
- import { Bn } from './bn'
15
-
16
- class Br {
17
- constructor (buf) {
18
- this.fromObject({ buf })
19
- }
20
-
21
- fromObject (obj) {
22
- this.buf = obj.buf || this.buf || undefined
23
- this.pos = obj.pos || this.pos || 0
24
- return this
25
- }
26
-
27
- eof () {
28
- return this.pos >= this.buf.length
29
- }
30
-
31
- read (len = this.buf.length) {
32
- const buf = this.buf.slice(this.pos, this.pos + len)
33
- this.pos = this.pos + len
34
- return buf
35
- }
36
-
37
- readReverse (len = this.buf.length) {
38
- const buf = this.buf.slice(this.pos, this.pos + len)
39
- this.pos = this.pos + len
40
- const buf2 = Buffer.alloc(buf.length)
41
- for (let i = 0; i < buf2.length; i++) {
42
- buf2[i] = buf[buf.length - 1 - i]
43
- }
44
- return buf2
45
- }
46
-
47
- readUInt8 () {
48
- const val = this.buf.readUInt8(this.pos)
49
- this.pos = this.pos + 1
50
- return val
51
- }
52
-
53
- readInt8 () {
54
- const val = this.buf.readInt8(this.pos)
55
- this.pos = this.pos + 1
56
- return val
57
- }
58
-
59
- readUInt16BE () {
60
- const val = this.buf.readUInt16BE(this.pos)
61
- this.pos = this.pos + 2
62
- return val
63
- }
64
-
65
- readInt16BE () {
66
- const val = this.buf.readInt16BE(this.pos)
67
- this.pos = this.pos + 2
68
- return val
69
- }
70
-
71
- readUInt16LE () {
72
- const val = this.buf.readUInt16LE(this.pos)
73
- this.pos = this.pos + 2
74
- return val
75
- }
76
-
77
- readInt16LE () {
78
- const val = this.buf.readInt16LE(this.pos)
79
- this.pos = this.pos + 2
80
- return val
81
- }
82
-
83
- readUInt32BE () {
84
- const val = this.buf.readUInt32BE(this.pos)
85
- this.pos = this.pos + 4
86
- return val
87
- }
88
-
89
- readInt32BE () {
90
- const val = this.buf.readInt32BE(this.pos)
91
- this.pos = this.pos + 4
92
- return val
93
- }
94
-
95
- readUInt32LE () {
96
- const val = this.buf.readUInt32LE(this.pos)
97
- this.pos = this.pos + 4
98
- return val
99
- }
100
-
101
- readInt32LE () {
102
- const val = this.buf.readInt32LE(this.pos)
103
- this.pos = this.pos + 4
104
- return val
105
- }
106
-
107
- readUInt64BEBn () {
108
- const buf = this.buf.slice(this.pos, this.pos + 8)
109
- const bn = new Bn().fromBuffer(buf)
110
- this.pos = this.pos + 8
111
- return bn
112
- }
113
-
114
- readUInt64LEBn () {
115
- const buf = this.readReverse(8)
116
- const bn = new Bn().fromBuffer(buf)
117
- return bn
118
- }
119
-
120
- readVarIntNum () {
121
- const first = this.readUInt8()
122
- let bn, n
123
- switch (first) {
124
- case 0xfd:
125
- return this.readUInt16LE()
126
- case 0xfe:
127
- return this.readUInt32LE()
128
- case 0xff:
129
- bn = this.readUInt64LEBn()
130
- n = bn.toNumber()
131
- if (n <= Math.pow(2, 53)) {
132
- return n
133
- } else {
134
- throw new Error(
135
- 'number too large to retain precision - use readVarIntBn'
136
- )
137
- }
138
- default:
139
- return first
140
- }
141
- }
142
-
143
- readVarIntBuf () {
144
- const first = this.buf.readUInt8(this.pos)
145
- switch (first) {
146
- case 0xfd:
147
- return this.read(1 + 2)
148
- case 0xfe:
149
- return this.read(1 + 4)
150
- case 0xff:
151
- return this.read(1 + 8)
152
- default:
153
- return this.read(1)
154
- }
155
- }
156
-
157
- readVarIntBn () {
158
- const first = this.readUInt8()
159
- switch (first) {
160
- case 0xfd:
161
- return new Bn(this.readUInt16LE())
162
- case 0xfe:
163
- return new Bn(this.readUInt32LE())
164
- case 0xff:
165
- return this.readUInt64LEBn()
166
- default:
167
- return new Bn(first)
168
- }
169
- }
170
- }
171
-
172
- export { Br }