bsv2 2.0.13

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 (212) hide show
  1. package/.babelrc.test +10 -0
  2. package/.editorconfig +4 -0
  3. package/.env.defaults +1 -0
  4. package/.env.schema +1 -0
  5. package/.mocharc.yml +4 -0
  6. package/.nvmrc +1 -0
  7. package/.travis.yml +7 -0
  8. package/LICENSE +39 -0
  9. package/README.md +53 -0
  10. package/benchmark.js +50 -0
  11. package/bin/bsv.js +11 -0
  12. package/bin/network.js +92 -0
  13. package/build/bsv.js +3 -0
  14. package/build/index.html +20 -0
  15. package/dist/bsv.bundle.js +3 -0
  16. package/dist/bsv.bundle.js.LICENSE.txt +10 -0
  17. package/dist/bsv.bundle.js.map +1 -0
  18. package/dist/bsv.cjs.js +3 -0
  19. package/dist/bsv.cjs.js.LICENSE.txt +1 -0
  20. package/dist/bsv.cjs.js.map +1 -0
  21. package/dist/bsv.min.js +2 -0
  22. package/dist/bsv.module.js +3 -0
  23. package/dist/bsv.module.js.LICENSE.txt +1 -0
  24. package/dist/bsv.module.js.map +1 -0
  25. package/doc/assets/anchor.js +350 -0
  26. package/doc/assets/bass-addons.css +12 -0
  27. package/doc/assets/bass.css +544 -0
  28. package/doc/assets/fonts/EOT/SourceCodePro-Bold.eot +0 -0
  29. package/doc/assets/fonts/EOT/SourceCodePro-Regular.eot +0 -0
  30. package/doc/assets/fonts/LICENSE.txt +93 -0
  31. package/doc/assets/fonts/OTF/SourceCodePro-Bold.otf +0 -0
  32. package/doc/assets/fonts/OTF/SourceCodePro-Regular.otf +0 -0
  33. package/doc/assets/fonts/TTF/SourceCodePro-Bold.ttf +0 -0
  34. package/doc/assets/fonts/TTF/SourceCodePro-Regular.ttf +0 -0
  35. package/doc/assets/fonts/WOFF/OTF/SourceCodePro-Bold.otf.woff +0 -0
  36. package/doc/assets/fonts/WOFF/OTF/SourceCodePro-Regular.otf.woff +0 -0
  37. package/doc/assets/fonts/WOFF/TTF/SourceCodePro-Bold.ttf.woff +0 -0
  38. package/doc/assets/fonts/WOFF/TTF/SourceCodePro-Regular.ttf.woff +0 -0
  39. package/doc/assets/fonts/WOFF2/OTF/SourceCodePro-Bold.otf.woff2 +0 -0
  40. package/doc/assets/fonts/WOFF2/OTF/SourceCodePro-Regular.otf.woff2 +0 -0
  41. package/doc/assets/fonts/WOFF2/TTF/SourceCodePro-Bold.ttf.woff2 +0 -0
  42. package/doc/assets/fonts/WOFF2/TTF/SourceCodePro-Regular.ttf.woff2 +0 -0
  43. package/doc/assets/fonts/source-code-pro.css +23 -0
  44. package/doc/assets/github.css +123 -0
  45. package/doc/assets/site.js +168 -0
  46. package/doc/assets/split.css +15 -0
  47. package/doc/assets/split.js +784 -0
  48. package/doc/assets/style.css +147 -0
  49. package/doc/index.html +9387 -0
  50. package/docs/block.html +36 -0
  51. package/docs/ecies.html +81 -0
  52. package/docs/index.html +84 -0
  53. package/docs/networks.html +43 -0
  54. package/docs/script.html +101 -0
  55. package/docs/transaction.html +150 -0
  56. package/docs/unspentoutput.html +32 -0
  57. package/entry.js +82 -0
  58. package/index.js +102 -0
  59. package/jsdoc.json +5 -0
  60. package/karma.conf.js +78 -0
  61. package/lib/ach.js +59 -0
  62. package/lib/address.js +264 -0
  63. package/lib/aes.js +57 -0
  64. package/lib/aescbc.js +38 -0
  65. package/lib/base-58-check.js +83 -0
  66. package/lib/base-58.js +59 -0
  67. package/lib/bip-32.js +472 -0
  68. package/lib/bip-39-en-wordlist.js +2054 -0
  69. package/lib/bip-39-en.js +10 -0
  70. package/lib/bip-39-es-wordlist.js +2055 -0
  71. package/lib/bip-39-es.js +10 -0
  72. package/lib/bip-39-jp-wordlist.js +2053 -0
  73. package/lib/bip-39-jp.js +10 -0
  74. package/lib/bip-39-words.js +5 -0
  75. package/lib/bip-39.js +275 -0
  76. package/lib/block-header.js +79 -0
  77. package/lib/block.js +132 -0
  78. package/lib/bn.js +467 -0
  79. package/lib/br.js +172 -0
  80. package/lib/bsm.js +127 -0
  81. package/lib/bw.js +188 -0
  82. package/lib/cbc.js +140 -0
  83. package/lib/cmp.js +37 -0
  84. package/lib/config.js +50 -0
  85. package/lib/constants.js +150 -0
  86. package/lib/ecdsa.js +495 -0
  87. package/lib/ecies.js +151 -0
  88. package/lib/hash-cache.js +41 -0
  89. package/lib/hash.js +191 -0
  90. package/lib/interp.js +1853 -0
  91. package/lib/inv.js +49 -0
  92. package/lib/key-pair.js +133 -0
  93. package/lib/mainnet-address.js +5 -0
  94. package/lib/mainnet-bip-32.js +5 -0
  95. package/lib/mainnet-key-pair.js +5 -0
  96. package/lib/mainnet-priv-key.js +5 -0
  97. package/lib/merkle.js +92 -0
  98. package/lib/msg.js +184 -0
  99. package/lib/op-code.js +398 -0
  100. package/lib/point.js +134 -0
  101. package/lib/priv-key.js +157 -0
  102. package/lib/pub-key.js +230 -0
  103. package/lib/rabin.js +300 -0
  104. package/lib/random.js +28 -0
  105. package/lib/script.js +826 -0
  106. package/lib/sig-operations.js +120 -0
  107. package/lib/sig.js +399 -0
  108. package/lib/struct.js +389 -0
  109. package/lib/testnet-address.js +5 -0
  110. package/lib/testnet-bip-32.js +5 -0
  111. package/lib/testnet-key-pair.js +5 -0
  112. package/lib/testnet-priv-key.js +5 -0
  113. package/lib/tx-builder.js +593 -0
  114. package/lib/tx-in.js +172 -0
  115. package/lib/tx-out-map.js +57 -0
  116. package/lib/tx-out.js +73 -0
  117. package/lib/tx-verifier.js +209 -0
  118. package/lib/tx.js +429 -0
  119. package/lib/var-int.js +73 -0
  120. package/lib/version.js +92 -0
  121. package/lib/workers-result.js +65 -0
  122. package/lib/workers.js +77 -0
  123. package/load-babel.js +3 -0
  124. package/mkdocs.yml +37 -0
  125. package/package.json +138 -0
  126. package/test/ach.js +172 -0
  127. package/test/address.js +464 -0
  128. package/test/aes.js +96 -0
  129. package/test/aescbc.js +65 -0
  130. package/test/base-58-check.js +128 -0
  131. package/test/base-58.js +113 -0
  132. package/test/bip-32.js +721 -0
  133. package/test/bip-39.js +282 -0
  134. package/test/block-header.js +152 -0
  135. package/test/block.js +243 -0
  136. package/test/bn.js +643 -0
  137. package/test/br.js +353 -0
  138. package/test/bsm.js +141 -0
  139. package/test/bw.js +292 -0
  140. package/test/cbc.js +335 -0
  141. package/test/cmp.js +56 -0
  142. package/test/data/bip69.json +215 -0
  143. package/test/data/bitcoind/base58_keys_invalid.json +152 -0
  144. package/test/data/bitcoind/base58_keys_valid.json +452 -0
  145. package/test/data/bitcoind/blocks.json +27 -0
  146. package/test/data/bitcoind/script_tests.json +2244 -0
  147. package/test/data/bitcoind/sig_canonical.json +7 -0
  148. package/test/data/bitcoind/sig_noncanonical.json +22 -0
  149. package/test/data/bitcoind/tx_invalid.json +177 -0
  150. package/test/data/bitcoind/tx_valid.json +224 -0
  151. package/test/data/blk86756-testnet.dat +0 -0
  152. package/test/data/blk86756-testnet.js +12 -0
  153. package/test/data/blk86756-testnet.json +684 -0
  154. package/test/data/ecdsa.json +230 -0
  155. package/test/data/messages.json +22 -0
  156. package/test/data/sighash.json +1004 -0
  157. package/test/data/tx_creation.json +85 -0
  158. package/test/ecdsa.js +748 -0
  159. package/test/ecies.js +238 -0
  160. package/test/global.js +13 -0
  161. package/test/hash-cache.js +52 -0
  162. package/test/hash.js +354 -0
  163. package/test/index.js +10 -0
  164. package/test/interp.js +512 -0
  165. package/test/inv.js +65 -0
  166. package/test/key-pair.js +243 -0
  167. package/test/merkle.js +179 -0
  168. package/test/msg.js +295 -0
  169. package/test/op-code.js +79 -0
  170. package/test/point.js +375 -0
  171. package/test/priv-key.js +264 -0
  172. package/test/pub-key.js +549 -0
  173. package/test/random.js +31 -0
  174. package/test/script.js +1340 -0
  175. package/test/sig-operations.js +149 -0
  176. package/test/sig.js +662 -0
  177. package/test/struct.js +552 -0
  178. package/test/tx-builder.js +2104 -0
  179. package/test/tx-in.js +198 -0
  180. package/test/tx-out-map.js +104 -0
  181. package/test/tx-out.js +162 -0
  182. package/test/tx-verifier.js +246 -0
  183. package/test/tx.js +539 -0
  184. package/test/var-int.js +132 -0
  185. package/test/vectors/aes.json +10392 -0
  186. package/test/vectors/aescbc.json +620 -0
  187. package/test/vectors/bip39.json +502 -0
  188. package/test/vectors/bip69.json +215 -0
  189. package/test/vectors/bitcoin-abc/script_tests.json +48 -0
  190. package/test/vectors/bitcoin-abc/sighash.json +7004 -0
  191. package/test/vectors/bitcoin-sv/script_tests.json +248 -0
  192. package/test/vectors/bitcoind/script_invalid.json +1151 -0
  193. package/test/vectors/bitcoind/script_valid.json +1462 -0
  194. package/test/vectors/bitcoind/sighash.json +3503 -0
  195. package/test/vectors/bitcoind/tx_invalid.json +883 -0
  196. package/test/vectors/bitcoind/tx_valid.json +1150 -0
  197. package/test/vectors/coolest-tx-ever-sent.json +8 -0
  198. package/test/vectors/ecdsa.json +300 -0
  199. package/test/vectors/hash.json +6133 -0
  200. package/test/vectors/kdf.json +47 -0
  201. package/test/vectors/largesttx.json +5 -0
  202. package/test/vectors/largesttxblock.json +6 -0
  203. package/test/vectors/sig.json +79 -0
  204. package/test/vectors/sighash-single-bug.json +8 -0
  205. package/test/version.js +81 -0
  206. package/test/workers-result.js +89 -0
  207. package/test/workers.js +91 -0
  208. package/test-index.js +3 -0
  209. package/test1.html +88 -0
  210. package/webpack.config.js +60 -0
  211. package/webpack.config.js_entry +57 -0
  212. package/yarn-error.log +6199 -0
package/lib/bn.js ADDED
@@ -0,0 +1,467 @@
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 ADDED
@@ -0,0 +1,172 @@
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 }