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/point.js
ADDED
|
@@ -0,0 +1,375 @@
|
|
|
1
|
+
/* global describe,it */
|
|
2
|
+
'use strict'
|
|
3
|
+
import should from 'should'
|
|
4
|
+
import { Point } from '../lib/point'
|
|
5
|
+
import { Bn } from '../lib/bn'
|
|
6
|
+
|
|
7
|
+
describe('Point', function () {
|
|
8
|
+
it('should create a point', function () {
|
|
9
|
+
let p = new Point()
|
|
10
|
+
should.exist(p)
|
|
11
|
+
p = new Point()
|
|
12
|
+
should.exist(p)
|
|
13
|
+
;(p instanceof Point).should.equal(true)
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
it('should have the standard properties for a point', function () {
|
|
17
|
+
const p = new Point()
|
|
18
|
+
const props = ['curve', 'type', 'precomputed', 'x', 'y', 'inf']
|
|
19
|
+
|
|
20
|
+
// all enumerable, own properties should be in prop
|
|
21
|
+
for (const k in p) {
|
|
22
|
+
if (Object.prototype.hasOwnProperty.call(p, k)) {
|
|
23
|
+
props.indexOf(k).should.not.equal(-1)
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// all props should be properties of a point
|
|
28
|
+
props.forEach(function (k) {
|
|
29
|
+
;(p[k] === undefined).should.equal(false)
|
|
30
|
+
})
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
describe('#copyFrom', function () {
|
|
34
|
+
it('should copy G', function () {
|
|
35
|
+
const point = Point.getG()
|
|
36
|
+
let point2
|
|
37
|
+
;(function () {
|
|
38
|
+
point.copyFrom(point2)
|
|
39
|
+
}.should.throw()) // point2 is not a Point yet
|
|
40
|
+
const point3 = new Point()
|
|
41
|
+
point.copyFrom(point3)
|
|
42
|
+
point.toString().should.equal(point3.toString())
|
|
43
|
+
})
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
describe('#toJSON', function () {
|
|
47
|
+
it('should print G to JSON', function () {
|
|
48
|
+
const G = Point.getG()
|
|
49
|
+
const json = G.toJSON()
|
|
50
|
+
json.x.should.equal(G.x.toString())
|
|
51
|
+
json.y.should.equal(G.y.toString())
|
|
52
|
+
})
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
describe('#fromJSON', function () {
|
|
56
|
+
it('should recover G', function () {
|
|
57
|
+
new Point()
|
|
58
|
+
.fromJSON(Point.getG().toJSON())
|
|
59
|
+
.eq(Point.getG())
|
|
60
|
+
.should.equal(true)
|
|
61
|
+
})
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
describe('#toString', function () {
|
|
65
|
+
it('should convert G to a string', function () {
|
|
66
|
+
const G = Point.getG()
|
|
67
|
+
G.toString().should.equal(JSON.stringify(G.toJSON()))
|
|
68
|
+
})
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
describe('#fromString', function () {
|
|
72
|
+
it('should convert a json string to G', function () {
|
|
73
|
+
const G = Point.getG()
|
|
74
|
+
new Point()
|
|
75
|
+
.fromString(G.toString())
|
|
76
|
+
.eq(G)
|
|
77
|
+
.should.equal(true)
|
|
78
|
+
})
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
describe('#getX', function () {
|
|
82
|
+
it('should return a Bn', function () {
|
|
83
|
+
const p = new Point()
|
|
84
|
+
;(p.getX() instanceof Bn).should.equal(true)
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
it('should return 0', function () {
|
|
88
|
+
const p = new Point()
|
|
89
|
+
p
|
|
90
|
+
.getX()
|
|
91
|
+
.toString()
|
|
92
|
+
.should.equal('0')
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
it('should be convertable to a buffer', function () {
|
|
96
|
+
const p = new Point()
|
|
97
|
+
p
|
|
98
|
+
.getX()
|
|
99
|
+
.toBuffer({ size: 32 })
|
|
100
|
+
.length.should.equal(32)
|
|
101
|
+
})
|
|
102
|
+
})
|
|
103
|
+
|
|
104
|
+
describe('#getY', function () {
|
|
105
|
+
it('should return a Bn', function () {
|
|
106
|
+
const p = new Point()
|
|
107
|
+
;(p.getY() instanceof Bn).should.equal(true)
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
it('should return 0', function () {
|
|
111
|
+
const p = new Point()
|
|
112
|
+
p
|
|
113
|
+
.getY()
|
|
114
|
+
.toString()
|
|
115
|
+
.should.equal('0')
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
it('should be convertable to a buffer', function () {
|
|
119
|
+
const p = new Point()
|
|
120
|
+
p
|
|
121
|
+
.getY()
|
|
122
|
+
.toBuffer({ size: 32 })
|
|
123
|
+
.length.should.equal(32)
|
|
124
|
+
})
|
|
125
|
+
})
|
|
126
|
+
|
|
127
|
+
describe('#add', function () {
|
|
128
|
+
it('should get back a point', function () {
|
|
129
|
+
const p1 = Point.getG()
|
|
130
|
+
const p2 = Point.getG()
|
|
131
|
+
const p3 = p1.add(p2)
|
|
132
|
+
;(p3 instanceof Point).should.equal(true)
|
|
133
|
+
})
|
|
134
|
+
|
|
135
|
+
it('should accurately add g to itself', function () {
|
|
136
|
+
const p1 = Point.getG()
|
|
137
|
+
const p2 = Point.getG()
|
|
138
|
+
const p3 = p1.add(p2)
|
|
139
|
+
p3
|
|
140
|
+
.getX()
|
|
141
|
+
.toString()
|
|
142
|
+
.should.equal(
|
|
143
|
+
'89565891926547004231252920425935692360644145829622209833684329913297188986597'
|
|
144
|
+
)
|
|
145
|
+
p3
|
|
146
|
+
.getY()
|
|
147
|
+
.toString()
|
|
148
|
+
.should.equal(
|
|
149
|
+
'12158399299693830322967808612713398636155367887041628176798871954788371653930'
|
|
150
|
+
)
|
|
151
|
+
})
|
|
152
|
+
})
|
|
153
|
+
|
|
154
|
+
describe('#mul', function () {
|
|
155
|
+
it('should get back a point', function () {
|
|
156
|
+
const g = Point.getG()
|
|
157
|
+
const b = g.mul(new Bn(2))
|
|
158
|
+
;(b instanceof Point).should.equal(true)
|
|
159
|
+
})
|
|
160
|
+
|
|
161
|
+
it('should accurately multiply g by 2', function () {
|
|
162
|
+
const g = Point.getG()
|
|
163
|
+
const b = g.mul(new Bn(2))
|
|
164
|
+
b
|
|
165
|
+
.getX()
|
|
166
|
+
.toString()
|
|
167
|
+
.should.equal(
|
|
168
|
+
'89565891926547004231252920425935692360644145829622209833684329913297188986597'
|
|
169
|
+
)
|
|
170
|
+
b
|
|
171
|
+
.getY()
|
|
172
|
+
.toString()
|
|
173
|
+
.should.equal(
|
|
174
|
+
'12158399299693830322967808612713398636155367887041628176798871954788371653930'
|
|
175
|
+
)
|
|
176
|
+
})
|
|
177
|
+
|
|
178
|
+
it('should accurately multiply g by n-1', function () {
|
|
179
|
+
const g = Point.getG()
|
|
180
|
+
const n = Point.getN()
|
|
181
|
+
const b = g.mul(n.sub(1))
|
|
182
|
+
b
|
|
183
|
+
.getX()
|
|
184
|
+
.toString()
|
|
185
|
+
.should.equal(
|
|
186
|
+
'55066263022277343669578718895168534326250603453777594175500187360389116729240'
|
|
187
|
+
)
|
|
188
|
+
b
|
|
189
|
+
.getY()
|
|
190
|
+
.toString()
|
|
191
|
+
.should.equal(
|
|
192
|
+
'83121579216557378445487899878180864668798711284981320763518679672151497189239'
|
|
193
|
+
)
|
|
194
|
+
})
|
|
195
|
+
|
|
196
|
+
it('should accurate multiply these problematic values related to a bug in bn.js', function () {
|
|
197
|
+
// see these discussions:
|
|
198
|
+
// https://github.com/bitpay/bitcore/pull/894
|
|
199
|
+
// https://github.com/indutny/elliptic/issues/17
|
|
200
|
+
// https://github.com/indutny/elliptic/pull/18
|
|
201
|
+
// https://github.com/indutny/elliptic/pull/19
|
|
202
|
+
// https://github.com/indutny/bn.js/commit/3557d780b07ed0ed301e128f326f83c2226fb679
|
|
203
|
+
;(function () {
|
|
204
|
+
const nhex =
|
|
205
|
+
'6d1229a6b24c2e775c062870ad26bc261051e0198c67203167273c7c62538846'
|
|
206
|
+
const n = new Bn(nhex, 16)
|
|
207
|
+
const g1 = Point.getG() // precomputed g
|
|
208
|
+
const g2 = new Point().fromX(
|
|
209
|
+
false,
|
|
210
|
+
new Bn(
|
|
211
|
+
'79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798',
|
|
212
|
+
16
|
|
213
|
+
)
|
|
214
|
+
) // non-precomputed g
|
|
215
|
+
const p1 = g1.mul(n)
|
|
216
|
+
const p2 = g2.mul(n)
|
|
217
|
+
const pxhex =
|
|
218
|
+
'd6106302d2698d6a41e9c9a114269e7be7c6a0081317de444bb2980bf9265a01'
|
|
219
|
+
const pyhex =
|
|
220
|
+
'e05fb262e64b108991a29979809fcef9d3e70cafceb3248c922c17d83d66bc9d'
|
|
221
|
+
p1
|
|
222
|
+
.getX()
|
|
223
|
+
.toBuffer()
|
|
224
|
+
.toString('hex')
|
|
225
|
+
.should.equal(pxhex)
|
|
226
|
+
p1
|
|
227
|
+
.getY()
|
|
228
|
+
.toBuffer()
|
|
229
|
+
.toString('hex')
|
|
230
|
+
.should.equal(pyhex)
|
|
231
|
+
p2
|
|
232
|
+
.getX()
|
|
233
|
+
.toBuffer()
|
|
234
|
+
.toString('hex')
|
|
235
|
+
.should.equal(pxhex)
|
|
236
|
+
p2
|
|
237
|
+
.getY()
|
|
238
|
+
.toBuffer()
|
|
239
|
+
.toString('hex')
|
|
240
|
+
.should.equal(pyhex)
|
|
241
|
+
})()
|
|
242
|
+
;(function () {
|
|
243
|
+
const nhex =
|
|
244
|
+
'f2cc9d2b008927db94b89e04e2f6e70c180e547b3e5e564b06b8215d1c264b53'
|
|
245
|
+
const n = new Bn(nhex, 16)
|
|
246
|
+
const g1 = Point.getG() // precomputed g
|
|
247
|
+
const g2 = new Point().fromX(
|
|
248
|
+
false,
|
|
249
|
+
new Bn(
|
|
250
|
+
'79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798',
|
|
251
|
+
16
|
|
252
|
+
)
|
|
253
|
+
) // non-precomputed g
|
|
254
|
+
const p1 = g1.mul(n)
|
|
255
|
+
const p2 = g2.mul(n)
|
|
256
|
+
const pxhex =
|
|
257
|
+
'e275faa35bd1e88f5df6e8f9f6edb93bdf1d65f4915efc79fd7a726ec0c21700'
|
|
258
|
+
const pyhex =
|
|
259
|
+
'367216cb35b086e6686d69dddd822a8f4d52eb82ac5d9de18fdcd9bf44fa7df7'
|
|
260
|
+
p1
|
|
261
|
+
.getX()
|
|
262
|
+
.toBuffer()
|
|
263
|
+
.toString('hex')
|
|
264
|
+
.should.equal(pxhex)
|
|
265
|
+
p1
|
|
266
|
+
.getY()
|
|
267
|
+
.toBuffer()
|
|
268
|
+
.toString('hex')
|
|
269
|
+
.should.equal(pyhex)
|
|
270
|
+
p2
|
|
271
|
+
.getX()
|
|
272
|
+
.toBuffer()
|
|
273
|
+
.toString('hex')
|
|
274
|
+
.should.equal(pxhex)
|
|
275
|
+
p2
|
|
276
|
+
.getY()
|
|
277
|
+
.toBuffer()
|
|
278
|
+
.toString('hex')
|
|
279
|
+
.should.equal(pyhex)
|
|
280
|
+
})()
|
|
281
|
+
})
|
|
282
|
+
})
|
|
283
|
+
|
|
284
|
+
describe('#mulAdd', function () {
|
|
285
|
+
it('should get back a point', function () {
|
|
286
|
+
const p1 = Point.getG()
|
|
287
|
+
const bn1 = new Bn(5)
|
|
288
|
+
const p2 = Point.getG().add(p1)
|
|
289
|
+
const bn2 = new Bn(6)
|
|
290
|
+
p1
|
|
291
|
+
.mulAdd(bn1, p2, bn2)
|
|
292
|
+
.getX()
|
|
293
|
+
.toString()
|
|
294
|
+
.should.equal(
|
|
295
|
+
p1
|
|
296
|
+
.mul(bn1)
|
|
297
|
+
.add(p2.mul(bn2))
|
|
298
|
+
.getX()
|
|
299
|
+
.toString()
|
|
300
|
+
)
|
|
301
|
+
p1
|
|
302
|
+
.mulAdd(bn1, p2, bn2)
|
|
303
|
+
.getY()
|
|
304
|
+
.toString()
|
|
305
|
+
.should.equal(
|
|
306
|
+
p1
|
|
307
|
+
.mul(bn1)
|
|
308
|
+
.add(p2.mul(bn2))
|
|
309
|
+
.getY()
|
|
310
|
+
.toString()
|
|
311
|
+
)
|
|
312
|
+
})
|
|
313
|
+
})
|
|
314
|
+
|
|
315
|
+
describe('@getN', function () {
|
|
316
|
+
it('should return n', function () {
|
|
317
|
+
const bn = Point.getN()
|
|
318
|
+
;(bn instanceof Bn).should.equal(true)
|
|
319
|
+
})
|
|
320
|
+
})
|
|
321
|
+
|
|
322
|
+
describe('@fromX', function () {
|
|
323
|
+
it('should return g', function () {
|
|
324
|
+
const g = Point.getG()
|
|
325
|
+
const p = Point.fromX(false, g.getX())
|
|
326
|
+
g.eq(p).should.equal(true)
|
|
327
|
+
})
|
|
328
|
+
})
|
|
329
|
+
|
|
330
|
+
describe('#fromX', function () {
|
|
331
|
+
it('should return g', function () {
|
|
332
|
+
const g = Point.getG()
|
|
333
|
+
const p = new Point().fromX(false, g.getX())
|
|
334
|
+
g.eq(p).should.equal(true)
|
|
335
|
+
})
|
|
336
|
+
})
|
|
337
|
+
|
|
338
|
+
describe('#validate', function () {
|
|
339
|
+
it('should validate this valid point', function () {
|
|
340
|
+
const x = new Bn().fromBuffer(
|
|
341
|
+
Buffer.from(
|
|
342
|
+
'ac242d242d23be966085a2b2b893d989f824e06c9ad0395a8a52f055ba39abb2',
|
|
343
|
+
'hex'
|
|
344
|
+
)
|
|
345
|
+
)
|
|
346
|
+
const y = new Bn().fromBuffer(
|
|
347
|
+
Buffer.from(
|
|
348
|
+
'4836ab292c105a711ed10fcfd30999c31ff7c02456147747e03e739ad527c380',
|
|
349
|
+
'hex'
|
|
350
|
+
)
|
|
351
|
+
)
|
|
352
|
+
const p = new Point(x, y)
|
|
353
|
+
should.exist(p.validate())
|
|
354
|
+
})
|
|
355
|
+
|
|
356
|
+
it('should invalidate this invalid point', function () {
|
|
357
|
+
const x = new Bn().fromBuffer(
|
|
358
|
+
Buffer.from(
|
|
359
|
+
'ac242d242d23be966085a2b2b893d989f824e06c9ad0395a8a52f055ba39abb2',
|
|
360
|
+
'hex'
|
|
361
|
+
)
|
|
362
|
+
)
|
|
363
|
+
const y = new Bn().fromBuffer(
|
|
364
|
+
Buffer.from(
|
|
365
|
+
'0000000000000000000000000000000000000000000000000000000000000000',
|
|
366
|
+
'hex'
|
|
367
|
+
)
|
|
368
|
+
)
|
|
369
|
+
const p = new Point(x, y)
|
|
370
|
+
;(function () {
|
|
371
|
+
p.validate()
|
|
372
|
+
}.should.throw('Invalid y value of public key'))
|
|
373
|
+
})
|
|
374
|
+
})
|
|
375
|
+
})
|
package/test/priv-key.js
ADDED
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
/* global describe,it */
|
|
2
|
+
'use strict'
|
|
3
|
+
import { PrivKey } from '../lib/priv-key'
|
|
4
|
+
import { Bn } from '../lib/bn'
|
|
5
|
+
import { Point } from '../lib/point'
|
|
6
|
+
import should from 'should'
|
|
7
|
+
|
|
8
|
+
describe('PrivKey', function () {
|
|
9
|
+
const hex = '96c132224121b509b7d0a16245e957d9192609c5637c6228311287b1be21627a'
|
|
10
|
+
const buf = Buffer.from(hex, 'hex')
|
|
11
|
+
const enctestnet = 'cSdkPxkAjA4HDr5VHgsebAPDEh9Gyub4HK8UJr2DFGGqKKy4K5sG'
|
|
12
|
+
const enctu = '92jJzK4tbURm1C7udQXxeCBvXHoHJstDXRxAMouPG1k1XUaXdsu'
|
|
13
|
+
const encmainnet = 'L2Gkw3kKJ6N24QcDuH4XDqt9cTqsKTVNDGz1CRZhk9cq4auDUbJy'
|
|
14
|
+
const encmu = '5JxgQaFM1FMd38cd14e3mbdxsdSa9iM2BV6DHBYsvGzxkTNQ7Un'
|
|
15
|
+
|
|
16
|
+
it('should satisfy these basic API features', function () {
|
|
17
|
+
let privKey = new PrivKey()
|
|
18
|
+
should.exist(privKey)
|
|
19
|
+
privKey = new PrivKey()
|
|
20
|
+
should.exist(privKey)
|
|
21
|
+
|
|
22
|
+
new PrivKey().constructor.should.equal(new PrivKey().constructor)
|
|
23
|
+
new PrivKey.Testnet().constructor.should.equal(
|
|
24
|
+
new PrivKey.Testnet().constructor
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
PrivKey.Testnet.fromRandom()
|
|
28
|
+
.toString()[0]
|
|
29
|
+
.should.equal('c')
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
it('should create a 0 private key with this convenience method', function () {
|
|
33
|
+
const bn = new Bn(0)
|
|
34
|
+
const privKey = new PrivKey(bn)
|
|
35
|
+
privKey.bn.toString().should.equal(bn.toString())
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
it('should create a mainnet private key', function () {
|
|
39
|
+
const privKey = new PrivKey(Bn.fromBuffer(buf), true)
|
|
40
|
+
privKey.toString().should.equal(encmainnet)
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
it('should create an uncompressed testnet private key', function () {
|
|
44
|
+
const privKey = new PrivKey.Testnet(Bn.fromBuffer(buf), false)
|
|
45
|
+
privKey.toString().should.equal(enctu)
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
it('should create an uncompressed mainnet private key', function () {
|
|
49
|
+
const privKey = new PrivKey(Bn.fromBuffer(buf), false)
|
|
50
|
+
privKey.toString().should.equal(encmu)
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
describe('#fromObject', function () {
|
|
54
|
+
it('should set bn', function () {
|
|
55
|
+
should.exist(new PrivKey().fromObject({ bn: Bn.fromBuffer(buf) }).bn)
|
|
56
|
+
})
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
describe('#fromJSON', function () {
|
|
60
|
+
it('should input this address correctly', function () {
|
|
61
|
+
const privKey = new PrivKey()
|
|
62
|
+
privKey.fromString(encmu)
|
|
63
|
+
const privKey2 = new PrivKey()
|
|
64
|
+
privKey2.fromJSON(privKey.toHex())
|
|
65
|
+
privKey2.toWif().should.equal(encmu)
|
|
66
|
+
})
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
describe('#toString', function () {
|
|
70
|
+
it('should output this address correctly', function () {
|
|
71
|
+
const privKey = new PrivKey()
|
|
72
|
+
privKey.fromString(encmu)
|
|
73
|
+
privKey.toString().should.equal(encmu)
|
|
74
|
+
})
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
describe('#fromRandom', function () {
|
|
78
|
+
it('should set bn gt 0 and lt n, and should be compressed', function () {
|
|
79
|
+
const privKey = new PrivKey().fromRandom()
|
|
80
|
+
privKey.bn.gt(new Bn(0)).should.equal(true)
|
|
81
|
+
privKey.bn.lt(Point.getN()).should.equal(true)
|
|
82
|
+
privKey.compressed.should.equal(true)
|
|
83
|
+
})
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
describe('@fromRandom', function () {
|
|
87
|
+
it('should set bn gt 0 and lt n, and should be compressed', function () {
|
|
88
|
+
const privKey = PrivKey.fromRandom()
|
|
89
|
+
privKey.bn.gt(new Bn(0)).should.equal(true)
|
|
90
|
+
privKey.bn.lt(Point.getN()).should.equal(true)
|
|
91
|
+
privKey.compressed.should.equal(true)
|
|
92
|
+
})
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
describe('#toHex', function () {
|
|
96
|
+
it('should return a hex string', function () {
|
|
97
|
+
const privKey = new PrivKey().fromBn(new Bn(5))
|
|
98
|
+
privKey
|
|
99
|
+
.toHex()
|
|
100
|
+
.should.equal(
|
|
101
|
+
'80000000000000000000000000000000000000000000000000000000000000000501'
|
|
102
|
+
)
|
|
103
|
+
})
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
describe('#toBuffer', function () {
|
|
107
|
+
it('should return a buffer', function () {
|
|
108
|
+
const privKey = new PrivKey().fromBn(new Bn(5))
|
|
109
|
+
privKey
|
|
110
|
+
.toBuffer()
|
|
111
|
+
.toString('hex')
|
|
112
|
+
.should.equal(
|
|
113
|
+
'80000000000000000000000000000000000000000000000000000000000000000501'
|
|
114
|
+
)
|
|
115
|
+
})
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
describe('#fromHex', function () {
|
|
119
|
+
it('should return a hex string', function () {
|
|
120
|
+
const privKey = new PrivKey().fromHex(
|
|
121
|
+
'80000000000000000000000000000000000000000000000000000000000000000501'
|
|
122
|
+
)
|
|
123
|
+
privKey
|
|
124
|
+
.toHex()
|
|
125
|
+
.should.equal(
|
|
126
|
+
'80000000000000000000000000000000000000000000000000000000000000000501'
|
|
127
|
+
)
|
|
128
|
+
})
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
describe('#fromBuffer', function () {
|
|
132
|
+
it('should return a buffer', function () {
|
|
133
|
+
const privKey = new PrivKey().fromBuffer(
|
|
134
|
+
Buffer.from(
|
|
135
|
+
'80000000000000000000000000000000000000000000000000000000000000000501',
|
|
136
|
+
'hex'
|
|
137
|
+
)
|
|
138
|
+
)
|
|
139
|
+
privKey
|
|
140
|
+
.toBuffer()
|
|
141
|
+
.toString('hex')
|
|
142
|
+
.should.equal(
|
|
143
|
+
'80000000000000000000000000000000000000000000000000000000000000000501'
|
|
144
|
+
)
|
|
145
|
+
})
|
|
146
|
+
|
|
147
|
+
it('should throw an error if buffer is wrong length', function () {
|
|
148
|
+
;(function () {
|
|
149
|
+
new PrivKey().fromBuffer(
|
|
150
|
+
Buffer.from(
|
|
151
|
+
'8000000000000000000000000000000000000000000000000000000000000000050100',
|
|
152
|
+
'hex'
|
|
153
|
+
)
|
|
154
|
+
)
|
|
155
|
+
}.should.throw(
|
|
156
|
+
'Length of privKey buffer must be 33 (uncompressed pubKey) or 34 (compressed pubKey)'
|
|
157
|
+
))
|
|
158
|
+
;(function () {
|
|
159
|
+
new PrivKey().fromBuffer(
|
|
160
|
+
Buffer.from(
|
|
161
|
+
'8000000000000000000000000000000000000000000000000000000000000005',
|
|
162
|
+
'hex'
|
|
163
|
+
)
|
|
164
|
+
)
|
|
165
|
+
}.should.throw(
|
|
166
|
+
'Length of privKey buffer must be 33 (uncompressed pubKey) or 34 (compressed pubKey)'
|
|
167
|
+
))
|
|
168
|
+
})
|
|
169
|
+
|
|
170
|
+
it('should throw an error if buffer has wrong versionByteNum byte', function () {
|
|
171
|
+
;(function () {
|
|
172
|
+
new PrivKey().fromBuffer(
|
|
173
|
+
Buffer.from(
|
|
174
|
+
'90000000000000000000000000000000000000000000000000000000000000000501',
|
|
175
|
+
'hex'
|
|
176
|
+
)
|
|
177
|
+
)
|
|
178
|
+
}.should.throw('Invalid versionByteNum byte'))
|
|
179
|
+
})
|
|
180
|
+
})
|
|
181
|
+
|
|
182
|
+
describe('#toBn', function () {
|
|
183
|
+
it('should return a bn', function () {
|
|
184
|
+
const privKey = new PrivKey().fromBn(new Bn(5))
|
|
185
|
+
privKey
|
|
186
|
+
.toBn()
|
|
187
|
+
.eq(new Bn(5))
|
|
188
|
+
.should.equal(true)
|
|
189
|
+
})
|
|
190
|
+
})
|
|
191
|
+
|
|
192
|
+
describe('#fromBn', function () {
|
|
193
|
+
it('should create a privKey from a bignum', function () {
|
|
194
|
+
const privKey = new PrivKey().fromBn(new Bn(5))
|
|
195
|
+
privKey.bn.toString().should.equal('5')
|
|
196
|
+
})
|
|
197
|
+
})
|
|
198
|
+
|
|
199
|
+
describe('@fromBn', function () {
|
|
200
|
+
it('should create a privKey from a bignum', function () {
|
|
201
|
+
const privKey = PrivKey.fromBn(new Bn(5))
|
|
202
|
+
privKey.bn.toString().should.equal('5')
|
|
203
|
+
})
|
|
204
|
+
})
|
|
205
|
+
|
|
206
|
+
describe('#validate', function () {
|
|
207
|
+
it('should unvalidate these privKeys', function () {
|
|
208
|
+
const privKey = new PrivKey()
|
|
209
|
+
privKey.compressed = true
|
|
210
|
+
privKey.bn = Point.getN()
|
|
211
|
+
;(function () {
|
|
212
|
+
privKey.validate()
|
|
213
|
+
}.should.throw('Number must be less than N'))
|
|
214
|
+
privKey.bn = Point.getN().sub(1)
|
|
215
|
+
privKey.compressed = undefined
|
|
216
|
+
;(function () {
|
|
217
|
+
privKey.validate()
|
|
218
|
+
}.should.throw(
|
|
219
|
+
'Must specify whether the corresponding public key is compressed or not (true or false)'
|
|
220
|
+
))
|
|
221
|
+
privKey.compressed = true
|
|
222
|
+
privKey.validate().should.equal(privKey)
|
|
223
|
+
})
|
|
224
|
+
})
|
|
225
|
+
|
|
226
|
+
describe('#fromWif', function () {
|
|
227
|
+
it('should parse this compressed testnet address correctly', function () {
|
|
228
|
+
const privKey = new PrivKey()
|
|
229
|
+
privKey.fromWif(encmainnet)
|
|
230
|
+
privKey.toWif().should.equal(encmainnet)
|
|
231
|
+
})
|
|
232
|
+
})
|
|
233
|
+
|
|
234
|
+
describe('@fromWif', function () {
|
|
235
|
+
it('should parse this compressed testnet address correctly', function () {
|
|
236
|
+
const privKey = PrivKey.fromWif(encmainnet)
|
|
237
|
+
privKey.toWif().should.equal(encmainnet)
|
|
238
|
+
})
|
|
239
|
+
})
|
|
240
|
+
|
|
241
|
+
describe('#toWif', function () {
|
|
242
|
+
it('should parse this compressed testnet address correctly', function () {
|
|
243
|
+
const privKey = new PrivKey.Testnet()
|
|
244
|
+
privKey.fromWif(enctestnet)
|
|
245
|
+
privKey.toWif().should.equal(enctestnet)
|
|
246
|
+
})
|
|
247
|
+
})
|
|
248
|
+
|
|
249
|
+
describe('#fromString', function () {
|
|
250
|
+
it('should parse this uncompressed testnet address correctly', function () {
|
|
251
|
+
const privKey = new PrivKey.Testnet()
|
|
252
|
+
privKey.fromString(enctu)
|
|
253
|
+
privKey.toWif().should.equal(enctu)
|
|
254
|
+
})
|
|
255
|
+
})
|
|
256
|
+
|
|
257
|
+
describe('#toString', function () {
|
|
258
|
+
it('should parse this uncompressed mainnet address correctly', function () {
|
|
259
|
+
const privKey = new PrivKey()
|
|
260
|
+
privKey.fromString(encmu)
|
|
261
|
+
privKey.toString().should.equal(encmu)
|
|
262
|
+
})
|
|
263
|
+
})
|
|
264
|
+
})
|