bsv2 2.1.0 → 2.1.2

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