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.
- package/.babelrc.test +10 -0
- package/.editorconfig +4 -0
- package/.env.defaults +1 -0
- package/.env.schema +1 -0
- package/.mocharc.yml +4 -0
- package/.nvmrc +1 -0
- package/.travis.yml +7 -0
- package/LICENSE +39 -0
- package/README.md +53 -0
- package/benchmark.js +50 -0
- package/bin/bsv.js +11 -0
- package/bin/network.js +92 -0
- package/build/bsv.js +3 -0
- package/build/index.html +20 -0
- package/dist/bsv.bundle.js +3 -0
- package/dist/bsv.bundle.js.LICENSE.txt +10 -0
- package/dist/bsv.bundle.js.map +1 -0
- package/dist/bsv.cjs.js +3 -0
- package/dist/bsv.cjs.js.LICENSE.txt +1 -0
- package/dist/bsv.cjs.js.map +1 -0
- package/dist/bsv.min.js +2 -0
- package/dist/bsv.module.js +3 -0
- package/dist/bsv.module.js.LICENSE.txt +1 -0
- package/dist/bsv.module.js.map +1 -0
- package/doc/assets/anchor.js +350 -0
- package/doc/assets/bass-addons.css +12 -0
- package/doc/assets/bass.css +544 -0
- 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 +93 -0
- 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 +23 -0
- package/doc/assets/github.css +123 -0
- package/doc/assets/site.js +168 -0
- package/doc/assets/split.css +15 -0
- package/doc/assets/split.js +784 -0
- package/doc/assets/style.css +147 -0
- package/doc/index.html +9387 -0
- package/docs/block.html +36 -0
- package/docs/ecies.html +81 -0
- package/docs/index.html +84 -0
- package/docs/networks.html +43 -0
- package/docs/script.html +101 -0
- package/docs/transaction.html +150 -0
- package/docs/unspentoutput.html +32 -0
- package/entry.js +82 -0
- package/index.js +102 -0
- package/jsdoc.json +5 -0
- package/karma.conf.js +78 -0
- package/lib/ach.js +59 -0
- package/lib/address.js +264 -0
- package/lib/aes.js +57 -0
- package/lib/aescbc.js +38 -0
- package/lib/base-58-check.js +83 -0
- package/lib/base-58.js +59 -0
- package/lib/bip-32.js +472 -0
- package/lib/bip-39-en-wordlist.js +2054 -0
- package/lib/bip-39-en.js +10 -0
- package/lib/bip-39-es-wordlist.js +2055 -0
- package/lib/bip-39-es.js +10 -0
- package/lib/bip-39-jp-wordlist.js +2053 -0
- package/lib/bip-39-jp.js +10 -0
- package/lib/bip-39-words.js +5 -0
- package/lib/bip-39.js +275 -0
- package/lib/block-header.js +79 -0
- package/lib/block.js +132 -0
- package/lib/bn.js +467 -0
- package/lib/br.js +172 -0
- package/lib/bsm.js +127 -0
- package/lib/bw.js +188 -0
- package/lib/cbc.js +140 -0
- package/lib/cmp.js +37 -0
- package/lib/config.js +50 -0
- package/lib/constants.js +150 -0
- package/lib/ecdsa.js +495 -0
- package/lib/ecies.js +151 -0
- package/lib/hash-cache.js +41 -0
- package/lib/hash.js +191 -0
- package/lib/interp.js +1853 -0
- package/lib/inv.js +49 -0
- package/lib/key-pair.js +133 -0
- package/lib/mainnet-address.js +5 -0
- package/lib/mainnet-bip-32.js +5 -0
- package/lib/mainnet-key-pair.js +5 -0
- package/lib/mainnet-priv-key.js +5 -0
- package/lib/merkle.js +92 -0
- package/lib/msg.js +184 -0
- package/lib/op-code.js +398 -0
- package/lib/point.js +134 -0
- package/lib/priv-key.js +157 -0
- package/lib/pub-key.js +230 -0
- package/lib/rabin.js +300 -0
- package/lib/random.js +28 -0
- package/lib/script.js +826 -0
- package/lib/sig-operations.js +120 -0
- package/lib/sig.js +399 -0
- package/lib/struct.js +389 -0
- package/lib/testnet-address.js +5 -0
- package/lib/testnet-bip-32.js +5 -0
- package/lib/testnet-key-pair.js +5 -0
- package/lib/testnet-priv-key.js +5 -0
- package/lib/tx-builder.js +593 -0
- package/lib/tx-in.js +172 -0
- package/lib/tx-out-map.js +57 -0
- package/lib/tx-out.js +73 -0
- package/lib/tx-verifier.js +209 -0
- package/lib/tx.js +429 -0
- package/lib/var-int.js +73 -0
- package/lib/version.js +92 -0
- package/lib/workers-result.js +65 -0
- package/lib/workers.js +77 -0
- package/load-babel.js +3 -0
- package/mkdocs.yml +37 -0
- package/package.json +138 -0
- package/test/ach.js +172 -0
- package/test/address.js +464 -0
- package/test/aes.js +96 -0
- package/test/aescbc.js +65 -0
- package/test/base-58-check.js +128 -0
- package/test/base-58.js +113 -0
- package/test/bip-32.js +721 -0
- package/test/bip-39.js +282 -0
- package/test/block-header.js +152 -0
- package/test/block.js +243 -0
- package/test/bn.js +643 -0
- package/test/br.js +353 -0
- package/test/bsm.js +141 -0
- package/test/bw.js +292 -0
- package/test/cbc.js +335 -0
- package/test/cmp.js +56 -0
- package/test/data/bip69.json +215 -0
- package/test/data/bitcoind/base58_keys_invalid.json +152 -0
- package/test/data/bitcoind/base58_keys_valid.json +452 -0
- package/test/data/bitcoind/blocks.json +27 -0
- package/test/data/bitcoind/script_tests.json +2244 -0
- package/test/data/bitcoind/sig_canonical.json +7 -0
- package/test/data/bitcoind/sig_noncanonical.json +22 -0
- package/test/data/bitcoind/tx_invalid.json +177 -0
- package/test/data/bitcoind/tx_valid.json +224 -0
- package/test/data/blk86756-testnet.dat +0 -0
- package/test/data/blk86756-testnet.js +12 -0
- package/test/data/blk86756-testnet.json +684 -0
- package/test/data/ecdsa.json +230 -0
- package/test/data/messages.json +22 -0
- package/test/data/sighash.json +1004 -0
- package/test/data/tx_creation.json +85 -0
- package/test/ecdsa.js +748 -0
- package/test/ecies.js +238 -0
- package/test/global.js +13 -0
- package/test/hash-cache.js +52 -0
- package/test/hash.js +354 -0
- package/test/index.js +10 -0
- package/test/interp.js +512 -0
- package/test/inv.js +65 -0
- package/test/key-pair.js +243 -0
- package/test/merkle.js +179 -0
- package/test/msg.js +295 -0
- package/test/op-code.js +79 -0
- package/test/point.js +375 -0
- package/test/priv-key.js +264 -0
- package/test/pub-key.js +549 -0
- package/test/random.js +31 -0
- package/test/script.js +1340 -0
- package/test/sig-operations.js +149 -0
- package/test/sig.js +662 -0
- package/test/struct.js +552 -0
- package/test/tx-builder.js +2104 -0
- package/test/tx-in.js +198 -0
- package/test/tx-out-map.js +104 -0
- package/test/tx-out.js +162 -0
- package/test/tx-verifier.js +246 -0
- package/test/tx.js +539 -0
- package/test/var-int.js +132 -0
- package/test/vectors/aes.json +10392 -0
- package/test/vectors/aescbc.json +620 -0
- package/test/vectors/bip39.json +502 -0
- package/test/vectors/bip69.json +215 -0
- package/test/vectors/bitcoin-abc/script_tests.json +48 -0
- package/test/vectors/bitcoin-abc/sighash.json +7004 -0
- package/test/vectors/bitcoin-sv/script_tests.json +248 -0
- package/test/vectors/bitcoind/script_invalid.json +1151 -0
- package/test/vectors/bitcoind/script_valid.json +1462 -0
- package/test/vectors/bitcoind/sighash.json +3503 -0
- package/test/vectors/bitcoind/tx_invalid.json +883 -0
- package/test/vectors/bitcoind/tx_valid.json +1150 -0
- package/test/vectors/coolest-tx-ever-sent.json +8 -0
- package/test/vectors/ecdsa.json +300 -0
- package/test/vectors/hash.json +6133 -0
- package/test/vectors/kdf.json +47 -0
- package/test/vectors/largesttx.json +5 -0
- package/test/vectors/largesttxblock.json +6 -0
- package/test/vectors/sig.json +79 -0
- package/test/vectors/sighash-single-bug.json +8 -0
- package/test/version.js +81 -0
- package/test/workers-result.js +89 -0
- package/test/workers.js +91 -0
- package/test-index.js +3 -0
- package/test1.html +88 -0
- package/webpack.config.js +60 -0
- package/webpack.config.js_entry +57 -0
- package/yarn-error.log +6199 -0
package/test/bn.js
ADDED
|
@@ -0,0 +1,643 @@
|
|
|
1
|
+
/* global describe,it */
|
|
2
|
+
'use strict'
|
|
3
|
+
import should from 'should'
|
|
4
|
+
import assert from 'assert'
|
|
5
|
+
import { Bn } from '../lib/bn'
|
|
6
|
+
|
|
7
|
+
describe('Bn', function () {
|
|
8
|
+
it('should create a bn', function () {
|
|
9
|
+
let bn = new Bn(50)
|
|
10
|
+
should.exist(bn)
|
|
11
|
+
bn.toString().should.equal('50')
|
|
12
|
+
bn = new Bn(50)
|
|
13
|
+
bn.toString().should.equal('50')
|
|
14
|
+
;(bn instanceof Bn).should.equal(true)
|
|
15
|
+
bn = new Bn('ff00', 16)
|
|
16
|
+
bn.toString(16).should.equal('ff00')
|
|
17
|
+
bn = new Bn('ff00', 16)
|
|
18
|
+
bn.toString(16).should.equal('ff00')
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
it('should parse this number', function () {
|
|
22
|
+
const bn = new Bn(999970000)
|
|
23
|
+
bn.toString().should.equal('999970000')
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
it('should parse numbers below and at bn.js internal word size', function () {
|
|
27
|
+
let bn = new Bn(Math.pow(2, 26) - 1)
|
|
28
|
+
bn.toString().should.equal((Math.pow(2, 26) - 1).toString())
|
|
29
|
+
bn = new Bn(Math.pow(2, 26))
|
|
30
|
+
bn.toString().should.equal(Math.pow(2, 26).toString())
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
it('should correctly square the number related to a bug in bn.js', function () {
|
|
34
|
+
/**
|
|
35
|
+
* These test vectors are related to this bn.js commit:
|
|
36
|
+
* https://github.com/indutny/bn.js/commit/3557d780b07ed0ed301e128f326f83c2226fb679
|
|
37
|
+
*
|
|
38
|
+
* More information:
|
|
39
|
+
* https://github.com/bitpay/bitcore/pull/894
|
|
40
|
+
* https://github.com/indutny/elliptic/issues/17
|
|
41
|
+
* https://github.com/indutny/elliptic/pull/18
|
|
42
|
+
* https://github.com/indutny/elliptic/pull/19
|
|
43
|
+
*/
|
|
44
|
+
const p = Bn._prime('k256').p
|
|
45
|
+
const red = Bn.red('k256')
|
|
46
|
+
|
|
47
|
+
const n = new Bn(
|
|
48
|
+
'9cd8cb48c3281596139f147c1364a3ede88d3f310fdb0eb98c924e599ca1b3c9',
|
|
49
|
+
16
|
|
50
|
+
)
|
|
51
|
+
const expected = n.sqr().mod(p)
|
|
52
|
+
const actual = n
|
|
53
|
+
.toRed(red)
|
|
54
|
+
.redSqr()
|
|
55
|
+
.fromRed()
|
|
56
|
+
|
|
57
|
+
assert.strict.equal(actual.toString(16), expected.toString(16))
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
it('should correctly square these numbers related to a bug in OpenSSL - CVE-2014-3570', function () {
|
|
61
|
+
/**
|
|
62
|
+
* Bitcoin developer Peter Wuile discovered a bug in OpenSSL in the course
|
|
63
|
+
* of developing libsecp256k. The OpenSSL security advisory is here:
|
|
64
|
+
*
|
|
65
|
+
* https://www.openssl.org/news/secadv_20150108.txt
|
|
66
|
+
*
|
|
67
|
+
* Greg Maxwell has a description of the bug and how it was found here:
|
|
68
|
+
*
|
|
69
|
+
* https://www.reddit.com/r/Bitcoin/comments/2rrxq7/on_why_010s_release_notes_say_we_have_reason_to/
|
|
70
|
+
* https://www.reddit.com/r/programming/comments/2rrc64/openssl_security_advisory_new_openssl_releases/
|
|
71
|
+
*
|
|
72
|
+
* The OpenSSL fix is here:
|
|
73
|
+
*
|
|
74
|
+
* https://github.com/openssl/openssl/commit/a7a44ba55cb4f884c6bc9ceac90072dea38e66d0
|
|
75
|
+
*
|
|
76
|
+
* This test uses the same big numbers that were problematic in OpenSSL.
|
|
77
|
+
* The check is to ensure that squaring a number and multiplying it by
|
|
78
|
+
* itself result in the same number. As an additional precaution, we check
|
|
79
|
+
* this multiplication normally as well as mod(p).
|
|
80
|
+
*/
|
|
81
|
+
const p = Bn._prime('k256').p
|
|
82
|
+
|
|
83
|
+
let n = new Bn(
|
|
84
|
+
'80000000000000008000000000000001FFFFFFFFFFFFFFFE0000000000000000',
|
|
85
|
+
16
|
|
86
|
+
)
|
|
87
|
+
let sqr = n.sqr()
|
|
88
|
+
let nn = n.mul(n)
|
|
89
|
+
nn.toString().should.equal(sqr.toString())
|
|
90
|
+
sqr = n.sqr().mod(p)
|
|
91
|
+
nn = n.mul(n).mod(p)
|
|
92
|
+
nn.toString().should.equal(sqr.toString())
|
|
93
|
+
|
|
94
|
+
n = new Bn(
|
|
95
|
+
'80000000000000000000000080000001FFFFFFFE000000000000000000000000',
|
|
96
|
+
16
|
|
97
|
+
)
|
|
98
|
+
sqr = n.sqr()
|
|
99
|
+
nn = n.mul(n)
|
|
100
|
+
nn.toString().should.equal(sqr.toString())
|
|
101
|
+
sqr = n.sqr().mod(p)
|
|
102
|
+
nn = n.mul(n).mod(p)
|
|
103
|
+
nn.toString().should.equal(sqr.toString())
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
describe('#copy', function () {
|
|
107
|
+
it('should copy 5', function () {
|
|
108
|
+
const bn = new Bn('5')
|
|
109
|
+
let bn2
|
|
110
|
+
;(function () {
|
|
111
|
+
bn.copy(bn2)
|
|
112
|
+
}.should.throw()) // bn2 is not a Bn yet
|
|
113
|
+
const bn3 = new Bn()
|
|
114
|
+
bn.copy(bn3)
|
|
115
|
+
bn3.toString().should.equal('5')
|
|
116
|
+
})
|
|
117
|
+
})
|
|
118
|
+
|
|
119
|
+
describe('#neg', function () {
|
|
120
|
+
it('should produce a negative', function () {
|
|
121
|
+
const bn = new Bn(1).neg()
|
|
122
|
+
;(bn instanceof Bn).should.equal(true)
|
|
123
|
+
bn.toString().should.equal('-1')
|
|
124
|
+
})
|
|
125
|
+
})
|
|
126
|
+
|
|
127
|
+
describe('#add', function () {
|
|
128
|
+
it('should add two small numbers together', function () {
|
|
129
|
+
const bn1 = new Bn(50)
|
|
130
|
+
const bn2 = new Bn(75)
|
|
131
|
+
const bn3 = bn1.add(bn2)
|
|
132
|
+
bn3.toString().should.equal('125')
|
|
133
|
+
})
|
|
134
|
+
})
|
|
135
|
+
|
|
136
|
+
describe('#sub', function () {
|
|
137
|
+
it('should subtract a small number', function () {
|
|
138
|
+
const bn1 = new Bn(50)
|
|
139
|
+
const bn2 = new Bn(25)
|
|
140
|
+
const bn3 = bn1.sub(bn2)
|
|
141
|
+
bn3.toString().should.equal('25')
|
|
142
|
+
})
|
|
143
|
+
})
|
|
144
|
+
|
|
145
|
+
describe('#mul', function () {
|
|
146
|
+
it('should mul a small number', function () {
|
|
147
|
+
const bn1 = new Bn(50)
|
|
148
|
+
const bn2 = new Bn(25)
|
|
149
|
+
const bn3 = bn1.mul(bn2)
|
|
150
|
+
bn3.toString().should.equal('1250')
|
|
151
|
+
})
|
|
152
|
+
})
|
|
153
|
+
|
|
154
|
+
describe('#mod', function () {
|
|
155
|
+
it('should mod a small number', function () {
|
|
156
|
+
const bn1 = new Bn(50)
|
|
157
|
+
const bn2 = new Bn(25)
|
|
158
|
+
const bn3 = bn1.mod(bn2)
|
|
159
|
+
bn3.toString().should.equal('0')
|
|
160
|
+
})
|
|
161
|
+
|
|
162
|
+
it('should mod a small number', function () {
|
|
163
|
+
const bn1 = new Bn(-50)
|
|
164
|
+
const bn2 = new Bn(25)
|
|
165
|
+
const bn3 = bn1.mod(bn2)
|
|
166
|
+
bn3.toString().should.equal('0')
|
|
167
|
+
})
|
|
168
|
+
|
|
169
|
+
it('should mod a small number', function () {
|
|
170
|
+
const bn1 = new Bn(50)
|
|
171
|
+
const bn2 = new Bn(47)
|
|
172
|
+
const bn3 = bn1.mod(bn2)
|
|
173
|
+
bn3.toString().should.equal('3')
|
|
174
|
+
})
|
|
175
|
+
|
|
176
|
+
it('should mod a small number', function () {
|
|
177
|
+
const bn1 = new Bn(-50)
|
|
178
|
+
const bn2 = new Bn(47)
|
|
179
|
+
const bn3 = bn1.mod(bn2)
|
|
180
|
+
bn3.toString().should.equal('-3')
|
|
181
|
+
})
|
|
182
|
+
})
|
|
183
|
+
|
|
184
|
+
describe('#umod', function () {
|
|
185
|
+
it('should mod a small number', function () {
|
|
186
|
+
const bn1 = new Bn(50)
|
|
187
|
+
const bn2 = new Bn(25)
|
|
188
|
+
const bn3 = bn1.umod(bn2)
|
|
189
|
+
bn3.toString().should.equal('0')
|
|
190
|
+
})
|
|
191
|
+
|
|
192
|
+
it('should mod a small number', function () {
|
|
193
|
+
const bn1 = new Bn(50)
|
|
194
|
+
const bn2 = new Bn(47)
|
|
195
|
+
const bn3 = bn1.umod(bn2)
|
|
196
|
+
bn3.toString().should.equal('3')
|
|
197
|
+
})
|
|
198
|
+
})
|
|
199
|
+
|
|
200
|
+
describe('#invm', function () {
|
|
201
|
+
it('should invm a small number', function () {
|
|
202
|
+
const bn1 = new Bn(50)
|
|
203
|
+
const bn2 = new Bn(25)
|
|
204
|
+
const bn3 = bn1.invm(bn2)
|
|
205
|
+
bn3.toString().should.equal('0')
|
|
206
|
+
})
|
|
207
|
+
|
|
208
|
+
it('should invm a small number', function () {
|
|
209
|
+
const bn1 = new Bn(50)
|
|
210
|
+
const bn2 = new Bn(47)
|
|
211
|
+
const bn3 = bn1.invm(bn2)
|
|
212
|
+
bn3.toString().should.equal('16')
|
|
213
|
+
})
|
|
214
|
+
})
|
|
215
|
+
|
|
216
|
+
describe('#div', function () {
|
|
217
|
+
it('should div a small number', function () {
|
|
218
|
+
const bn1 = new Bn(50)
|
|
219
|
+
const bn2 = new Bn(25)
|
|
220
|
+
const bn3 = bn1.div(bn2)
|
|
221
|
+
bn3.toString().should.equal('2')
|
|
222
|
+
})
|
|
223
|
+
|
|
224
|
+
it('should div a small number', function () {
|
|
225
|
+
const bn1 = new Bn(50)
|
|
226
|
+
const bn2 = new Bn(47)
|
|
227
|
+
const bn3 = bn1.div(bn2)
|
|
228
|
+
bn3.toString().should.equal('1')
|
|
229
|
+
})
|
|
230
|
+
})
|
|
231
|
+
|
|
232
|
+
describe('#cmp', function () {
|
|
233
|
+
it('should know A=B', function () {
|
|
234
|
+
new Bn(5).cmp(5).should.equal(0)
|
|
235
|
+
new Bn(5).cmp(4).should.equal(1)
|
|
236
|
+
new Bn(5).cmp(6).should.equal(-1)
|
|
237
|
+
})
|
|
238
|
+
})
|
|
239
|
+
|
|
240
|
+
describe('#eq', function () {
|
|
241
|
+
it('should know A=B', function () {
|
|
242
|
+
new Bn(5).eq(5).should.equal(true)
|
|
243
|
+
new Bn(5).eq(4).should.equal(false)
|
|
244
|
+
})
|
|
245
|
+
})
|
|
246
|
+
|
|
247
|
+
describe('#neq', function () {
|
|
248
|
+
it('should know A!=B', function () {
|
|
249
|
+
new Bn(5).neq(5).should.equal(false)
|
|
250
|
+
new Bn(5).neq(4).should.equal(true)
|
|
251
|
+
})
|
|
252
|
+
})
|
|
253
|
+
|
|
254
|
+
describe('#gt', function () {
|
|
255
|
+
it('should say 1 is greater than 0', function () {
|
|
256
|
+
const bn1 = new Bn(1)
|
|
257
|
+
const bn0 = new Bn(0)
|
|
258
|
+
bn1.gt(bn0).should.equal(true)
|
|
259
|
+
})
|
|
260
|
+
|
|
261
|
+
it('should say a big number is greater than a small big number', function () {
|
|
262
|
+
const bn1 = new Bn('24023452345398529485723980457')
|
|
263
|
+
const bn0 = new Bn('34098234283412341234049357')
|
|
264
|
+
bn1.gt(bn0).should.equal(true)
|
|
265
|
+
})
|
|
266
|
+
|
|
267
|
+
it('should say a big number is great than a standard number', function () {
|
|
268
|
+
const bn1 = new Bn('24023452345398529485723980457')
|
|
269
|
+
const bn0 = new Bn(5)
|
|
270
|
+
bn1.gt(bn0).should.equal(true)
|
|
271
|
+
})
|
|
272
|
+
})
|
|
273
|
+
|
|
274
|
+
describe('#geq', function () {
|
|
275
|
+
it('should know that A >= B', function () {
|
|
276
|
+
new Bn(6).geq(5).should.equal(true)
|
|
277
|
+
new Bn(5).geq(5).should.equal(true)
|
|
278
|
+
new Bn(4).geq(5).should.equal(false)
|
|
279
|
+
})
|
|
280
|
+
})
|
|
281
|
+
|
|
282
|
+
describe('#lt', function () {
|
|
283
|
+
it('should know A < B', function () {
|
|
284
|
+
new Bn(5).lt(6).should.equal(true)
|
|
285
|
+
new Bn(5).lt(4).should.equal(false)
|
|
286
|
+
})
|
|
287
|
+
})
|
|
288
|
+
|
|
289
|
+
describe('#leq', function () {
|
|
290
|
+
it('should know A <= B', function () {
|
|
291
|
+
new Bn(5).leq(6).should.equal(true)
|
|
292
|
+
new Bn(5).leq(5).should.equal(true)
|
|
293
|
+
new Bn(5).leq(4).should.equal(false)
|
|
294
|
+
})
|
|
295
|
+
})
|
|
296
|
+
|
|
297
|
+
describe('#fromJSON', function () {
|
|
298
|
+
it('should make Bn from a string', function () {
|
|
299
|
+
new Bn()
|
|
300
|
+
.fromJSON('5')
|
|
301
|
+
.toString()
|
|
302
|
+
.should.equal('5')
|
|
303
|
+
})
|
|
304
|
+
})
|
|
305
|
+
|
|
306
|
+
describe('#toJSON', function () {
|
|
307
|
+
it('should make string from a Bn', function () {
|
|
308
|
+
new Bn(5).toJSON().should.equal('5')
|
|
309
|
+
new Bn()
|
|
310
|
+
.fromJSON('5')
|
|
311
|
+
.toJSON()
|
|
312
|
+
.should.equal('5')
|
|
313
|
+
})
|
|
314
|
+
})
|
|
315
|
+
|
|
316
|
+
describe('#fromString', function () {
|
|
317
|
+
it('should make Bn from a string', function () {
|
|
318
|
+
new Bn()
|
|
319
|
+
.fromString('5')
|
|
320
|
+
.toString()
|
|
321
|
+
.should.equal('5')
|
|
322
|
+
})
|
|
323
|
+
})
|
|
324
|
+
|
|
325
|
+
describe('#toString', function () {
|
|
326
|
+
it('should make a string', function () {
|
|
327
|
+
new Bn(5).toString().should.equal('5')
|
|
328
|
+
})
|
|
329
|
+
})
|
|
330
|
+
|
|
331
|
+
describe('@fromBuffer', function () {
|
|
332
|
+
it('should work with big endian', function () {
|
|
333
|
+
const bn = Bn.fromBuffer(Buffer.from('0001', 'hex'), { endian: 'big' })
|
|
334
|
+
bn.toString().should.equal('1')
|
|
335
|
+
})
|
|
336
|
+
|
|
337
|
+
it('should work with big endian 256', function () {
|
|
338
|
+
const bn = Bn.fromBuffer(Buffer.from('0100', 'hex'), { endian: 'big' })
|
|
339
|
+
bn.toString().should.equal('256')
|
|
340
|
+
})
|
|
341
|
+
|
|
342
|
+
it('should work with little endian if we specify the size', function () {
|
|
343
|
+
const bn = Bn.fromBuffer(Buffer.from('0100', 'hex'), {
|
|
344
|
+
size: 2,
|
|
345
|
+
endian: 'little'
|
|
346
|
+
})
|
|
347
|
+
bn.toString().should.equal('1')
|
|
348
|
+
})
|
|
349
|
+
})
|
|
350
|
+
|
|
351
|
+
describe('#fromHex', function () {
|
|
352
|
+
it('should create bn from known hex', function () {
|
|
353
|
+
const bn = new Bn().fromHex('0100', { size: 2, endian: 'little' })
|
|
354
|
+
bn.toString().should.equal('1')
|
|
355
|
+
})
|
|
356
|
+
})
|
|
357
|
+
|
|
358
|
+
describe('#fromBuffer', function () {
|
|
359
|
+
it('should work as a prototype method', function () {
|
|
360
|
+
const bn = new Bn().fromBuffer(Buffer.from('0100', 'hex'), {
|
|
361
|
+
size: 2,
|
|
362
|
+
endian: 'little'
|
|
363
|
+
})
|
|
364
|
+
bn.toString().should.equal('1')
|
|
365
|
+
})
|
|
366
|
+
})
|
|
367
|
+
|
|
368
|
+
describe('#toHex', function () {
|
|
369
|
+
it('should create a hex string of 4 byte buffer', function () {
|
|
370
|
+
const bn = new Bn(1)
|
|
371
|
+
bn.toHex({ size: 4 }).should.equal('00000001')
|
|
372
|
+
})
|
|
373
|
+
})
|
|
374
|
+
|
|
375
|
+
describe('#toBuffer', function () {
|
|
376
|
+
it('should convert zero to empty buffer', function () {
|
|
377
|
+
new Bn(0).toBuffer().length.should.equal(0)
|
|
378
|
+
})
|
|
379
|
+
|
|
380
|
+
it('should create a 4 byte buffer', function () {
|
|
381
|
+
const bn = new Bn(1)
|
|
382
|
+
bn
|
|
383
|
+
.toBuffer({ size: 4 })
|
|
384
|
+
.toString('hex')
|
|
385
|
+
.should.equal('00000001')
|
|
386
|
+
})
|
|
387
|
+
|
|
388
|
+
it('should create a 4 byte buffer in little endian', function () {
|
|
389
|
+
const bn = new Bn(1)
|
|
390
|
+
bn
|
|
391
|
+
.toBuffer({ size: 4, endian: 'little' })
|
|
392
|
+
.toString('hex')
|
|
393
|
+
.should.equal('01000000')
|
|
394
|
+
})
|
|
395
|
+
|
|
396
|
+
it('should create a 2 byte buffer even if you ask for a 1 byte', function () {
|
|
397
|
+
const bn = new Bn('ff00', 16)
|
|
398
|
+
bn
|
|
399
|
+
.toBuffer({ size: 1 })
|
|
400
|
+
.toString('hex')
|
|
401
|
+
.should.equal('ff00')
|
|
402
|
+
})
|
|
403
|
+
|
|
404
|
+
it('should create a 4 byte buffer even if you ask for a 1 byte', function () {
|
|
405
|
+
const bn = new Bn('ffffff00', 16)
|
|
406
|
+
bn
|
|
407
|
+
.toBuffer({ size: 4 })
|
|
408
|
+
.toString('hex')
|
|
409
|
+
.should.equal('ffffff00')
|
|
410
|
+
})
|
|
411
|
+
})
|
|
412
|
+
|
|
413
|
+
describe('#toBits', function () {
|
|
414
|
+
it('should convert these known Bns to bits', function () {
|
|
415
|
+
new Bn()
|
|
416
|
+
.fromHex('00')
|
|
417
|
+
.toBits()
|
|
418
|
+
.should.equal(0x00000000)
|
|
419
|
+
new Bn()
|
|
420
|
+
.fromHex('01')
|
|
421
|
+
.toBits()
|
|
422
|
+
.should.equal(0x01000001)
|
|
423
|
+
new Bn()
|
|
424
|
+
.fromHex('0101')
|
|
425
|
+
.toBits()
|
|
426
|
+
.should.equal(0x02000101)
|
|
427
|
+
new Bn()
|
|
428
|
+
.fromHex('010101')
|
|
429
|
+
.toBits()
|
|
430
|
+
.should.equal(0x03010101)
|
|
431
|
+
new Bn()
|
|
432
|
+
.fromHex('01010101')
|
|
433
|
+
.toBits()
|
|
434
|
+
.should.equal(0x04010101)
|
|
435
|
+
new Bn()
|
|
436
|
+
.fromHex('0101010101')
|
|
437
|
+
.toBits()
|
|
438
|
+
.should.equal(0x05010101)
|
|
439
|
+
new Bn()
|
|
440
|
+
.fromHex('010101010101')
|
|
441
|
+
.toBits()
|
|
442
|
+
.should.equal(0x06010101)
|
|
443
|
+
new Bn()
|
|
444
|
+
.fromNumber(-1)
|
|
445
|
+
.toBits()
|
|
446
|
+
.should.equal(0x01800001)
|
|
447
|
+
})
|
|
448
|
+
})
|
|
449
|
+
|
|
450
|
+
describe('#fromBits', function () {
|
|
451
|
+
it('should convert these known bits to Bns', function () {
|
|
452
|
+
new Bn()
|
|
453
|
+
.fromBits(0x01003456)
|
|
454
|
+
.toHex()
|
|
455
|
+
.should.equal('')
|
|
456
|
+
new Bn()
|
|
457
|
+
.fromBits(0x02003456)
|
|
458
|
+
.toHex()
|
|
459
|
+
.should.equal('34')
|
|
460
|
+
new Bn()
|
|
461
|
+
.fromBits(0x03003456)
|
|
462
|
+
.toHex()
|
|
463
|
+
.should.equal('3456')
|
|
464
|
+
new Bn()
|
|
465
|
+
.fromBits(0x04003456)
|
|
466
|
+
.toHex()
|
|
467
|
+
.should.equal('345600')
|
|
468
|
+
new Bn()
|
|
469
|
+
.fromBits(0x05003456)
|
|
470
|
+
.toHex()
|
|
471
|
+
.should.equal('34560000')
|
|
472
|
+
new Bn()
|
|
473
|
+
.fromBits(0x05f03456)
|
|
474
|
+
.lt(0)
|
|
475
|
+
.should.equal(true) // sign bit set
|
|
476
|
+
;(function () {
|
|
477
|
+
new Bn().fromBits(0x05f03456, { strict: true }) // sign bit set
|
|
478
|
+
}.should.throw('negative bit set'))
|
|
479
|
+
new Bn()
|
|
480
|
+
.fromBits(0x04923456)
|
|
481
|
+
.lt(0)
|
|
482
|
+
.should.equal(true)
|
|
483
|
+
})
|
|
484
|
+
})
|
|
485
|
+
|
|
486
|
+
describe('#toSm', function () {
|
|
487
|
+
it('should convert to Sm', function () {
|
|
488
|
+
let buf
|
|
489
|
+
buf = new Bn().toSm()
|
|
490
|
+
buf.toString('hex').should.equal('')
|
|
491
|
+
buf = new Bn(5).toSm()
|
|
492
|
+
buf.toString('hex').should.equal('05')
|
|
493
|
+
buf = new Bn(-5).toSm()
|
|
494
|
+
buf.toString('hex').should.equal('85')
|
|
495
|
+
buf = new Bn(128).toSm()
|
|
496
|
+
buf.toString('hex').should.equal('0080')
|
|
497
|
+
buf = new Bn(-128).toSm()
|
|
498
|
+
buf.toString('hex').should.equal('8080')
|
|
499
|
+
buf = new Bn(127).toSm()
|
|
500
|
+
buf.toString('hex').should.equal('7f')
|
|
501
|
+
buf = new Bn(-127).toSm()
|
|
502
|
+
buf.toString('hex').should.equal('ff')
|
|
503
|
+
buf = new Bn(128).toSm({ endian: 'little' })
|
|
504
|
+
buf.toString('hex').should.equal('8000')
|
|
505
|
+
buf = new Bn(-128).toSm({ endian: 'little' })
|
|
506
|
+
buf.toString('hex').should.equal('8080')
|
|
507
|
+
})
|
|
508
|
+
})
|
|
509
|
+
|
|
510
|
+
describe('#fromSm', function () {
|
|
511
|
+
it('should convert from Sm', function () {
|
|
512
|
+
let buf
|
|
513
|
+
buf = Buffer.from([0])
|
|
514
|
+
new Bn()
|
|
515
|
+
.fromSm(buf)
|
|
516
|
+
.cmp(0)
|
|
517
|
+
.should.equal(0)
|
|
518
|
+
buf = Buffer.from('05', 'hex')
|
|
519
|
+
new Bn()
|
|
520
|
+
.fromSm(buf)
|
|
521
|
+
.cmp(5)
|
|
522
|
+
.should.equal(0)
|
|
523
|
+
buf = Buffer.from('85', 'hex')
|
|
524
|
+
new Bn()
|
|
525
|
+
.fromSm(buf)
|
|
526
|
+
.cmp(-5)
|
|
527
|
+
.should.equal(0)
|
|
528
|
+
buf = Buffer.from('0080', 'hex')
|
|
529
|
+
new Bn()
|
|
530
|
+
.fromSm(buf)
|
|
531
|
+
.cmp(128)
|
|
532
|
+
.should.equal(0)
|
|
533
|
+
buf = Buffer.from('8080', 'hex')
|
|
534
|
+
new Bn()
|
|
535
|
+
.fromSm(buf)
|
|
536
|
+
.cmp(-128)
|
|
537
|
+
.should.equal(0)
|
|
538
|
+
buf = Buffer.from('8000', 'hex')
|
|
539
|
+
new Bn()
|
|
540
|
+
.fromSm(buf, { endian: 'little' })
|
|
541
|
+
.cmp(128)
|
|
542
|
+
.should.equal(0)
|
|
543
|
+
buf = Buffer.from('8080', 'hex')
|
|
544
|
+
new Bn()
|
|
545
|
+
.fromSm(buf, { endian: 'little' })
|
|
546
|
+
.cmp(-128)
|
|
547
|
+
.should.equal(0)
|
|
548
|
+
buf = Buffer.from('0080', 'hex') // negative zero
|
|
549
|
+
new Bn()
|
|
550
|
+
.fromSm(buf, { endian: 'little' })
|
|
551
|
+
.cmp(0)
|
|
552
|
+
.should.equal(0)
|
|
553
|
+
})
|
|
554
|
+
})
|
|
555
|
+
|
|
556
|
+
describe('#toScriptNumBuffer', function () {
|
|
557
|
+
it('should output a little endian Sm number', function () {
|
|
558
|
+
const bn = new Bn(-23434234)
|
|
559
|
+
bn
|
|
560
|
+
.toScriptNumBuffer()
|
|
561
|
+
.toString('hex')
|
|
562
|
+
.should.equal(bn.toSm({ endian: 'little' }).toString('hex'))
|
|
563
|
+
})
|
|
564
|
+
})
|
|
565
|
+
|
|
566
|
+
describe('#fromScriptNumBuffer', function () {
|
|
567
|
+
it('should parse this normal number', function () {
|
|
568
|
+
new Bn()
|
|
569
|
+
.fromScriptNumBuffer(Buffer.from('01', 'hex'))
|
|
570
|
+
.toNumber()
|
|
571
|
+
.should.equal(1)
|
|
572
|
+
new Bn()
|
|
573
|
+
.fromScriptNumBuffer(Buffer.from('0080', 'hex'))
|
|
574
|
+
.toNumber()
|
|
575
|
+
.should.equal(0)
|
|
576
|
+
new Bn()
|
|
577
|
+
.fromScriptNumBuffer(Buffer.from('0180', 'hex'))
|
|
578
|
+
.toNumber()
|
|
579
|
+
.should.equal(-1)
|
|
580
|
+
})
|
|
581
|
+
|
|
582
|
+
it('should throw an error for a number over 4 bytes', function () {
|
|
583
|
+
;(function () {
|
|
584
|
+
new Bn()
|
|
585
|
+
.fromScriptNumBuffer(Buffer.from('8100000000', 'hex'))
|
|
586
|
+
.toNumber()
|
|
587
|
+
.should.equal(-1)
|
|
588
|
+
}.should.throw('script number overflow'))
|
|
589
|
+
})
|
|
590
|
+
|
|
591
|
+
it('should throw an error for number that is not a minimal size representation', function () {
|
|
592
|
+
// invalid
|
|
593
|
+
;(function () {
|
|
594
|
+
new Bn().fromScriptNumBuffer(Buffer.from('80000000', 'hex'), true)
|
|
595
|
+
}.should.throw('non-minimally encoded script number'))
|
|
596
|
+
;(function () {
|
|
597
|
+
new Bn().fromScriptNumBuffer(Buffer.from('800000', 'hex'), true)
|
|
598
|
+
}.should.throw('non-minimally encoded script number'))
|
|
599
|
+
;(function () {
|
|
600
|
+
new Bn().fromScriptNumBuffer(Buffer.from('00', 'hex'), true)
|
|
601
|
+
}.should.throw('non-minimally encoded script number'))
|
|
602
|
+
|
|
603
|
+
// valid
|
|
604
|
+
new Bn()
|
|
605
|
+
.fromScriptNumBuffer(Buffer.from('8000', 'hex'), true)
|
|
606
|
+
.toString()
|
|
607
|
+
.should.equal('128')
|
|
608
|
+
new Bn()
|
|
609
|
+
.fromScriptNumBuffer(Buffer.from('0081', 'hex'), true)
|
|
610
|
+
.toString()
|
|
611
|
+
.should.equal('-256')
|
|
612
|
+
new Bn()
|
|
613
|
+
.fromScriptNumBuffer(Buffer.from('', 'hex'), true)
|
|
614
|
+
.toString()
|
|
615
|
+
.should.equal('0')
|
|
616
|
+
new Bn()
|
|
617
|
+
.fromScriptNumBuffer(Buffer.from('01', 'hex'), true)
|
|
618
|
+
.toString()
|
|
619
|
+
.should.equal('1')
|
|
620
|
+
|
|
621
|
+
// invalid, but flag not set
|
|
622
|
+
new Bn()
|
|
623
|
+
.fromScriptNumBuffer(Buffer.from('00000000', 'hex'))
|
|
624
|
+
.toString()
|
|
625
|
+
.should.equal('0')
|
|
626
|
+
})
|
|
627
|
+
})
|
|
628
|
+
|
|
629
|
+
describe('#fromNumber', function () {
|
|
630
|
+
it('should convert from a number', function () {
|
|
631
|
+
new Bn()
|
|
632
|
+
.fromNumber(5)
|
|
633
|
+
.toNumber()
|
|
634
|
+
.should.equal(5)
|
|
635
|
+
})
|
|
636
|
+
})
|
|
637
|
+
|
|
638
|
+
describe('#toNumber', function () {
|
|
639
|
+
it('it should convert to a number', function () {
|
|
640
|
+
new Bn(5).toNumber().should.equal(5)
|
|
641
|
+
})
|
|
642
|
+
})
|
|
643
|
+
})
|