bsv2 2.1.0 → 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.
- package/dist/bsv2.mjs +1 -1
- package/package.json +3 -2
- package/.babel.test +0 -10
- package/.editorconfig +0 -4
- package/.env.defaults +0 -1
- package/.env.schema +0 -1
- package/.husky/pre-commit +0 -1
- package/.mocharc.yml +0 -4
- package/.nvmrc +0 -1
- package/.travis.yml +0 -7
- package/babel.config.cjs +0 -44
- package/benchmark.js +0 -42
- package/bin/bsv.js +0 -11
- package/bin/network.js +0 -93
- package/build/index.html +0 -20
- package/dist/bsv2.umd.js +0 -28
- package/doc/assets/anchor.js +0 -350
- package/doc/assets/bass-addons.css +0 -12
- package/doc/assets/bass.css +0 -544
- package/doc/assets/fonts/EOT/SourceCodePro-Bold.eot +0 -0
- package/doc/assets/fonts/EOT/SourceCodePro-Regular.eot +0 -0
- package/doc/assets/fonts/LICENSE.txt +0 -93
- package/doc/assets/fonts/OTF/SourceCodePro-Bold.otf +0 -0
- package/doc/assets/fonts/OTF/SourceCodePro-Regular.otf +0 -0
- package/doc/assets/fonts/TTF/SourceCodePro-Bold.ttf +0 -0
- package/doc/assets/fonts/TTF/SourceCodePro-Regular.ttf +0 -0
- package/doc/assets/fonts/WOFF/OTF/SourceCodePro-Bold.otf.woff +0 -0
- package/doc/assets/fonts/WOFF/OTF/SourceCodePro-Regular.otf.woff +0 -0
- package/doc/assets/fonts/WOFF/TTF/SourceCodePro-Bold.ttf.woff +0 -0
- package/doc/assets/fonts/WOFF/TTF/SourceCodePro-Regular.ttf.woff +0 -0
- package/doc/assets/fonts/WOFF2/OTF/SourceCodePro-Bold.otf.woff2 +0 -0
- package/doc/assets/fonts/WOFF2/OTF/SourceCodePro-Regular.otf.woff2 +0 -0
- package/doc/assets/fonts/WOFF2/TTF/SourceCodePro-Bold.ttf.woff2 +0 -0
- package/doc/assets/fonts/WOFF2/TTF/SourceCodePro-Regular.ttf.woff2 +0 -0
- package/doc/assets/fonts/source-code-pro.css +0 -23
- package/doc/assets/github.css +0 -123
- package/doc/assets/site.js +0 -168
- package/doc/assets/split.css +0 -15
- package/doc/assets/split.js +0 -782
- package/doc/assets/style.css +0 -147
- package/doc/index.html +0 -9461
- package/entry.js +0 -81
- package/eslint.config.mjs +0 -8
- package/index.js +0 -98
- package/karma.conf.ts +0 -77
- package/lib/ach.js +0 -59
- package/lib/address.js +0 -266
- package/lib/aes.js +0 -57
- package/lib/aescbc.js +0 -38
- package/lib/base-58-check.js +0 -83
- package/lib/base-58.js +0 -59
- package/lib/bip-32.js +0 -472
- package/lib/bip-39-en-wordlist.js +0 -2054
- package/lib/bip-39-en.js +0 -10
- package/lib/bip-39-es-wordlist.js +0 -2055
- package/lib/bip-39-es.js +0 -10
- package/lib/bip-39-jp-wordlist.js +0 -2053
- package/lib/bip-39-jp.js +0 -10
- package/lib/bip-39-words.js +0 -5
- package/lib/bip-39.js +0 -286
- package/lib/block-header.js +0 -79
- package/lib/block.js +0 -132
- package/lib/bn.js +0 -462
- package/lib/br.js +0 -172
- package/lib/bsm.js +0 -127
- package/lib/bw.js +0 -188
- package/lib/cbc.js +0 -140
- package/lib/cmp.js +0 -37
- package/lib/config.js +0 -50
- package/lib/constants.js +0 -150
- package/lib/ecdsa.js +0 -496
- package/lib/ecies.js +0 -151
- package/lib/hash-cache.js +0 -41
- package/lib/hash.js +0 -191
- package/lib/interp.js +0 -2017
- package/lib/inv.js +0 -49
- package/lib/key-pair.js +0 -133
- package/lib/mainnet-address.js +0 -5
- package/lib/mainnet-bip-32.js +0 -5
- package/lib/mainnet-key-pair.js +0 -5
- package/lib/mainnet-priv-key.js +0 -5
- package/lib/merkle.js +0 -92
- package/lib/msg.js +0 -184
- package/lib/op-code.js +0 -400
- package/lib/point.js +0 -134
- package/lib/priv-key.js +0 -157
- package/lib/pub-key.js +0 -230
- package/lib/random.js +0 -28
- package/lib/script.js +0 -828
- package/lib/sig-operations.js +0 -120
- package/lib/sig.js +0 -399
- package/lib/struct.js +0 -392
- package/lib/testnet-address.js +0 -5
- package/lib/testnet-bip-32.js +0 -5
- package/lib/testnet-key-pair.js +0 -5
- package/lib/testnet-priv-key.js +0 -5
- package/lib/tx-builder.js +0 -593
- package/lib/tx-in.js +0 -172
- package/lib/tx-out-map.js +0 -57
- package/lib/tx-out.js +0 -73
- package/lib/tx-verifier.js +0 -209
- package/lib/tx.js +0 -430
- package/lib/var-int.js +0 -73
- package/lib/version.js +0 -92
- package/lib/workers-result.js +0 -65
- package/lib/workers.js +0 -77
- package/load-babel.js +0 -5
- package/mtest/test.html +0 -17
- package/mtest/test.js +0 -2
- package/mtest/test.mjs +0 -2
- package/mtest/webpack.config.js +0 -115
- package/out/Sig.html +0 -541
- package/out/bip-32.js.html +0 -523
- package/out/bip-39.js.html +0 -336
- package/out/block.js.html +0 -183
- package/out/bn.js.html +0 -512
- package/out/ecdsa.js.html +0 -546
- package/out/fonts/OpenSans-Bold-webfont.eot +0 -0
- package/out/fonts/OpenSans-Bold-webfont.svg +0 -1830
- package/out/fonts/OpenSans-Bold-webfont.woff +0 -0
- package/out/fonts/OpenSans-BoldItalic-webfont.eot +0 -0
- package/out/fonts/OpenSans-BoldItalic-webfont.svg +0 -1830
- package/out/fonts/OpenSans-BoldItalic-webfont.woff +0 -0
- package/out/fonts/OpenSans-Italic-webfont.eot +0 -0
- package/out/fonts/OpenSans-Italic-webfont.svg +0 -1830
- package/out/fonts/OpenSans-Italic-webfont.woff +0 -0
- package/out/fonts/OpenSans-Light-webfont.eot +0 -0
- package/out/fonts/OpenSans-Light-webfont.svg +0 -1831
- package/out/fonts/OpenSans-Light-webfont.woff +0 -0
- package/out/fonts/OpenSans-LightItalic-webfont.eot +0 -0
- package/out/fonts/OpenSans-LightItalic-webfont.svg +0 -1835
- package/out/fonts/OpenSans-LightItalic-webfont.woff +0 -0
- package/out/fonts/OpenSans-Regular-webfont.eot +0 -0
- package/out/fonts/OpenSans-Regular-webfont.svg +0 -1831
- package/out/fonts/OpenSans-Regular-webfont.woff +0 -0
- package/out/global.html +0 -220
- package/out/index.html +0 -128
- package/out/interp.js.html +0 -2064
- package/out/merkle.js.html +0 -143
- package/out/msg.js.html +0 -235
- package/out/priv-key.js.html +0 -208
- package/out/pub-key.js.html +0 -281
- package/out/script.js.html +0 -876
- package/out/scripts/linenumber.js +0 -25
- package/out/scripts/prettify/Apache-License-2.0.txt +0 -202
- package/out/scripts/prettify/lang-css.js +0 -2
- package/out/scripts/prettify/prettify.js +0 -28
- package/out/sig-operations.js.html +0 -171
- package/out/sig.js.html +0 -450
- package/out/struct.js.html +0 -440
- package/out/styles/jsdoc-default.css +0 -358
- package/out/styles/prettify-jsdoc.css +0 -111
- package/out/styles/prettify-tomorrow.css +0 -132
- package/out/tx-builder.js.html +0 -644
- package/out/tx-in.js.html +0 -223
- package/out/tx-verifier.js.html +0 -260
- package/out/tx.js.html +0 -481
- package/test/ach.js +0 -172
- package/test/address.js +0 -464
- package/test/aes.js +0 -96
- package/test/aescbc.js +0 -65
- package/test/base-58-check.js +0 -128
- package/test/base-58.js +0 -113
- package/test/bip-32.js +0 -721
- package/test/bip-39.js +0 -282
- package/test/block-header.js +0 -152
- package/test/block.js +0 -243
- package/test/bn.js +0 -643
- package/test/br.js +0 -353
- package/test/bsm.js +0 -141
- package/test/bw.js +0 -292
- package/test/cbc.js +0 -335
- package/test/cmp.js +0 -56
- package/test/data/bip69.json +0 -215
- package/test/data/bitcoind/base58_keys_invalid.json +0 -152
- package/test/data/bitcoind/base58_keys_valid.json +0 -452
- package/test/data/bitcoind/blocks.json +0 -27
- package/test/data/bitcoind/script_tests.json +0 -2244
- package/test/data/bitcoind/sig_canonical.json +0 -7
- package/test/data/bitcoind/sig_noncanonical.json +0 -22
- package/test/data/bitcoind/tx_invalid.json +0 -177
- package/test/data/bitcoind/tx_valid.json +0 -224
- package/test/data/blk86756-testnet.dat +0 -0
- package/test/data/blk86756-testnet.js +0 -12
- package/test/data/blk86756-testnet.json +0 -684
- package/test/data/ecdsa.json +0 -230
- package/test/data/messages.json +0 -22
- package/test/data/sighash.json +0 -1004
- package/test/data/tx_creation.json +0 -85
- package/test/ecdsa.js +0 -748
- package/test/ecies.js +0 -238
- package/test/global.js +0 -13
- package/test/hash-cache.js +0 -52
- package/test/hash.js +0 -354
- package/test/index.js +0 -10
- package/test/interp.js +0 -512
- package/test/inv.js +0 -65
- package/test/key-pair.js +0 -243
- package/test/merkle.js +0 -179
- package/test/msg.js +0 -295
- package/test/op-code.js +0 -79
- package/test/point.js +0 -375
- package/test/priv-key.js +0 -264
- package/test/pub-key.js +0 -549
- package/test/random.js +0 -31
- package/test/script.js +0 -1340
- package/test/sig-operations.js +0 -149
- package/test/sig.js +0 -662
- package/test/struct.js +0 -552
- package/test/tx-builder.js +0 -2104
- package/test/tx-in.js +0 -198
- package/test/tx-out-map.js +0 -104
- package/test/tx-out.js +0 -162
- package/test/tx-verifier.js +0 -246
- package/test/tx.js +0 -539
- package/test/var-int.js +0 -132
- package/test/vectors/aes.json +0 -10392
- package/test/vectors/aescbc.json +0 -620
- package/test/vectors/bip39.json +0 -502
- package/test/vectors/bip69.json +0 -215
- package/test/vectors/bitcoin-abc/script_tests.json +0 -48
- package/test/vectors/bitcoin-abc/sighash.json +0 -7004
- package/test/vectors/bitcoin-sv/script_tests.json +0 -248
- package/test/vectors/bitcoind/script_invalid.json +0 -1151
- package/test/vectors/bitcoind/script_valid.json +0 -1462
- package/test/vectors/bitcoind/sighash.json +0 -3503
- package/test/vectors/bitcoind/tx_invalid.json +0 -883
- package/test/vectors/bitcoind/tx_valid.json +0 -1150
- package/test/vectors/coolest-tx-ever-sent.json +0 -8
- package/test/vectors/ecdsa.json +0 -300
- package/test/vectors/hash.json +0 -6133
- package/test/vectors/kdf.json +0 -47
- package/test/vectors/largesttx.json +0 -5
- package/test/vectors/largesttxblock.json +0 -6
- package/test/vectors/sig.json +0 -79
- package/test/vectors/sighash-single-bug.json +0 -8
- package/test/version.js +0 -81
- package/test/workers-result.js +0 -89
- package/test/workers.js +0 -91
- package/test-index.js +0 -3
- package/vite.config.js +0 -36
package/lib/bn.js
DELETED
|
@@ -1,462 +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
|
-
bigInt = false
|
|
275
|
-
) {
|
|
276
|
-
if (nMaxNumSize === undefined) {
|
|
277
|
-
nMaxNumSize = 4
|
|
278
|
-
}
|
|
279
|
-
if (buf.length > nMaxNumSize) {
|
|
280
|
-
throw new Error(`script number overflow nMaxNumSize: ${nMaxNumSize}`)
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
if (fRequireMinimal && buf.length > 0) {
|
|
284
|
-
// Check that the number is encoded with the minimum possible
|
|
285
|
-
// number of bytes.
|
|
286
|
-
//
|
|
287
|
-
// If the most-significant-byte - excluding the sign bit - is zero
|
|
288
|
-
// then we're not minimal. Note how this test also rejects the
|
|
289
|
-
// negative-zero encoding, 0x80.
|
|
290
|
-
if ((buf[buf.length - 1] & 0x7f) === 0) {
|
|
291
|
-
// One exception: if there's more than one byte and the most
|
|
292
|
-
// significant bit of the second-most-significant-byte is set
|
|
293
|
-
// it would conflict with the sign bit. An example of this case
|
|
294
|
-
// is +-255, which encode to 0xff00 and 0xff80 respectively.
|
|
295
|
-
// (big-endian).
|
|
296
|
-
if (buf.length <= 1 || (buf[buf.length - 2] & 0x80) === 0) {
|
|
297
|
-
throw new Error('non-minimally encoded script number')
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
}
|
|
301
|
-
// TODO?
|
|
302
|
-
// https://github.com/bitcoin-sv/bitcoin-sv/blob/4d3444909a5a98f129d3d2991bf8f0f7f15d3969/src/script/script_num.cpp#L34
|
|
303
|
-
if (buf.byteLength === 0) {
|
|
304
|
-
if (bigInt) {
|
|
305
|
-
// m_value = bint{0};
|
|
306
|
-
}
|
|
307
|
-
} else if (buf.byteLength <= nMaxNumSize) {
|
|
308
|
-
if (bigInt) {
|
|
309
|
-
// m_value = bsv::bint::deserialize(span);
|
|
310
|
-
} else {
|
|
311
|
-
// m_value = bsv::deserialize<int64_t>(span.begin(), span.end());
|
|
312
|
-
}
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
return this.fromSm(buf, { endian: 'little' })
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
// The corollary to the above, with the notable exception that we do not throw
|
|
319
|
-
// an error if the output is larger than four bytes. (Which can happen if
|
|
320
|
-
// performing a numerical operation that results in an overflow to more than 4
|
|
321
|
-
// bytes).
|
|
322
|
-
|
|
323
|
-
Bn.prototype.toScriptNumBuffer = function (buf) {
|
|
324
|
-
return this.toSm({ endian: 'little' })
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
Bn.prototype.neg = function () {
|
|
328
|
-
const _neg = _Bn.prototype.neg.call(this)
|
|
329
|
-
const neg = Object.create(Bn.prototype)
|
|
330
|
-
_neg.copy(neg)
|
|
331
|
-
return neg
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
Bn.prototype.add = function (bn) {
|
|
335
|
-
const _bn = _Bn.prototype.add.call(this, bn)
|
|
336
|
-
bn = Object.create(Bn.prototype)
|
|
337
|
-
_bn.copy(bn)
|
|
338
|
-
return bn
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
Bn.prototype.sub = function (bn) {
|
|
342
|
-
const _bn = _Bn.prototype.sub.call(this, bn)
|
|
343
|
-
bn = Object.create(Bn.prototype)
|
|
344
|
-
_bn.copy(bn)
|
|
345
|
-
return bn
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
Bn.prototype.mul = function (bn) {
|
|
349
|
-
const _bn = _Bn.prototype.mul.call(this, bn)
|
|
350
|
-
bn = Object.create(Bn.prototype)
|
|
351
|
-
_bn.copy(bn)
|
|
352
|
-
return bn
|
|
353
|
-
}
|
|
354
|
-
|
|
355
|
-
/**
|
|
356
|
-
* to be used if this is positive.
|
|
357
|
-
*/
|
|
358
|
-
Bn.prototype.mod = function (bn) {
|
|
359
|
-
const _bn = _Bn.prototype.mod.call(this, bn)
|
|
360
|
-
bn = Object.create(Bn.prototype)
|
|
361
|
-
_bn.copy(bn)
|
|
362
|
-
return bn
|
|
363
|
-
}
|
|
364
|
-
|
|
365
|
-
/**
|
|
366
|
-
* to be used if this is negative.
|
|
367
|
-
*/
|
|
368
|
-
Bn.prototype.umod = function (bn) {
|
|
369
|
-
const _bn = _Bn.prototype.umod.call(this, bn)
|
|
370
|
-
bn = Object.create(Bn.prototype)
|
|
371
|
-
_bn.copy(bn)
|
|
372
|
-
return bn
|
|
373
|
-
}
|
|
374
|
-
|
|
375
|
-
Bn.prototype.invm = function (bn) {
|
|
376
|
-
const _bn = _Bn.prototype.invm.call(this, bn)
|
|
377
|
-
bn = Object.create(Bn.prototype)
|
|
378
|
-
_bn.copy(bn)
|
|
379
|
-
return bn
|
|
380
|
-
}
|
|
381
|
-
|
|
382
|
-
Bn.prototype.div = function (bn) {
|
|
383
|
-
const _bn = _Bn.prototype.div.call(this, bn)
|
|
384
|
-
bn = Object.create(Bn.prototype)
|
|
385
|
-
_bn.copy(bn)
|
|
386
|
-
return bn
|
|
387
|
-
}
|
|
388
|
-
|
|
389
|
-
Bn.prototype.ushln = function (bits) {
|
|
390
|
-
const _bn = _Bn.prototype.ushln.call(this, bits)
|
|
391
|
-
const bn = Object.create(Bn.prototype)
|
|
392
|
-
_bn.copy(bn)
|
|
393
|
-
return bn
|
|
394
|
-
}
|
|
395
|
-
|
|
396
|
-
Bn.prototype.ushrn = function (bits) {
|
|
397
|
-
const _bn = _Bn.prototype.ushrn.call(this, bits)
|
|
398
|
-
const bn = Object.create(Bn.prototype)
|
|
399
|
-
_bn.copy(bn)
|
|
400
|
-
return bn
|
|
401
|
-
}
|
|
402
|
-
|
|
403
|
-
Bn.prototype.cmp = function (bn) {
|
|
404
|
-
return _Bn.prototype.cmp.call(this, bn)
|
|
405
|
-
}
|
|
406
|
-
|
|
407
|
-
/**
|
|
408
|
-
* All the standard big number operations operate on other big numbers. e.g.,
|
|
409
|
-
* bn1.add(bn2). But it is frequenly valuble to add numbers or strings, e.g.
|
|
410
|
-
* bn.add(5) or bn.add('5'). The decorator wraps all methods where this would
|
|
411
|
-
* be convenient and makes that possible.
|
|
412
|
-
*/
|
|
413
|
-
function decorate (name) {
|
|
414
|
-
Bn.prototype['_' + name] = Bn.prototype[name]
|
|
415
|
-
const f = function (b) {
|
|
416
|
-
if (typeof b === 'string') {
|
|
417
|
-
b = new Bn(b)
|
|
418
|
-
} else if (typeof b === 'number') {
|
|
419
|
-
b = new Bn(b.toString())
|
|
420
|
-
}
|
|
421
|
-
return this['_' + name](b)
|
|
422
|
-
}
|
|
423
|
-
Bn.prototype[name] = f
|
|
424
|
-
}
|
|
425
|
-
|
|
426
|
-
Bn.prototype.eq = function (b) {
|
|
427
|
-
return this.cmp(b) === 0
|
|
428
|
-
}
|
|
429
|
-
|
|
430
|
-
Bn.prototype.neq = function (b) {
|
|
431
|
-
return this.cmp(b) !== 0
|
|
432
|
-
}
|
|
433
|
-
|
|
434
|
-
Bn.prototype.gt = function (b) {
|
|
435
|
-
return this.cmp(b) > 0
|
|
436
|
-
}
|
|
437
|
-
|
|
438
|
-
Bn.prototype.geq = function (b) {
|
|
439
|
-
return this.cmp(b) >= 0
|
|
440
|
-
}
|
|
441
|
-
|
|
442
|
-
Bn.prototype.lt = function (b) {
|
|
443
|
-
return this.cmp(b) < 0
|
|
444
|
-
}
|
|
445
|
-
|
|
446
|
-
Bn.prototype.leq = function (b) {
|
|
447
|
-
return this.cmp(b) <= 0
|
|
448
|
-
}
|
|
449
|
-
|
|
450
|
-
decorate('add')
|
|
451
|
-
decorate('sub')
|
|
452
|
-
decorate('mul')
|
|
453
|
-
decorate('mod')
|
|
454
|
-
decorate('invm')
|
|
455
|
-
decorate('div')
|
|
456
|
-
decorate('cmp')
|
|
457
|
-
decorate('gt')
|
|
458
|
-
decorate('geq')
|
|
459
|
-
decorate('lt')
|
|
460
|
-
decorate('leq')
|
|
461
|
-
|
|
462
|
-
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 }
|