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/key-pair.js
ADDED
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
/* global describe,it */
|
|
2
|
+
'use strict'
|
|
3
|
+
import { KeyPair } from '../lib/key-pair'
|
|
4
|
+
import { PrivKey } from '../lib/priv-key'
|
|
5
|
+
import { PubKey } from '../lib/pub-key'
|
|
6
|
+
import { Bn } from '../lib/bn'
|
|
7
|
+
import should from 'should'
|
|
8
|
+
|
|
9
|
+
describe('KeyPair', function () {
|
|
10
|
+
it('should satisfy this basic API', function () {
|
|
11
|
+
let key = new KeyPair()
|
|
12
|
+
should.exist(key)
|
|
13
|
+
key = new KeyPair()
|
|
14
|
+
should.exist(key)
|
|
15
|
+
|
|
16
|
+
KeyPair.Mainnet.should.equal(KeyPair.Mainnet)
|
|
17
|
+
KeyPair.Testnet.should.equal(KeyPair.Testnet)
|
|
18
|
+
new KeyPair.Mainnet()
|
|
19
|
+
.fromRandom()
|
|
20
|
+
.privKey.constructor.should.equal(PrivKey.Mainnet)
|
|
21
|
+
new KeyPair.Testnet()
|
|
22
|
+
.fromRandom()
|
|
23
|
+
.privKey.constructor.should.equal(PrivKey.Testnet)
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
it('should make a key with a priv and pub', function () {
|
|
27
|
+
const priv = new PrivKey()
|
|
28
|
+
const pub = new PubKey()
|
|
29
|
+
const key = new KeyPair(priv, pub)
|
|
30
|
+
should.exist(key)
|
|
31
|
+
should.exist(key.privKey)
|
|
32
|
+
should.exist(key.pubKey)
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
describe('#fromJSON', function () {
|
|
36
|
+
it('should make a keyPair from this json', function () {
|
|
37
|
+
const privKey = new PrivKey().fromRandom()
|
|
38
|
+
const pubKey = new PubKey().fromPrivKey(privKey)
|
|
39
|
+
const keyPair = new KeyPair().fromJSON({
|
|
40
|
+
privKey: privKey.toJSON(),
|
|
41
|
+
pubKey: pubKey.toJSON()
|
|
42
|
+
})
|
|
43
|
+
keyPair.privKey.toString().should.equal(privKey.toString())
|
|
44
|
+
keyPair.pubKey.toString().should.equal(pubKey.toString())
|
|
45
|
+
})
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
describe('#toJSON', function () {
|
|
49
|
+
it('should make json from this keyPair', function () {
|
|
50
|
+
const json = new KeyPair().fromRandom().toJSON()
|
|
51
|
+
should.exist(json.privKey)
|
|
52
|
+
should.exist(json.pubKey)
|
|
53
|
+
const keyPair = new KeyPair().fromJSON(json)
|
|
54
|
+
keyPair
|
|
55
|
+
.toJSON()
|
|
56
|
+
.privKey.toString()
|
|
57
|
+
.should.equal(json.privKey.toString())
|
|
58
|
+
keyPair
|
|
59
|
+
.toJSON()
|
|
60
|
+
.pubKey.toString()
|
|
61
|
+
.should.equal(json.pubKey.toString())
|
|
62
|
+
})
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
describe('#fromFastBuffer', function () {
|
|
66
|
+
it('should convert from a fast buffer', function () {
|
|
67
|
+
let keyPair = new KeyPair().fromRandom()
|
|
68
|
+
const privKey1 = keyPair.privKey
|
|
69
|
+
const pubKey1 = keyPair.pubKey
|
|
70
|
+
const buf = keyPair.toFastBuffer()
|
|
71
|
+
keyPair = new KeyPair().fromFastBuffer(buf)
|
|
72
|
+
const privKey2 = keyPair.privKey
|
|
73
|
+
const pubKey2 = keyPair.pubKey
|
|
74
|
+
privKey1.toString().should.equal(privKey2.toString())
|
|
75
|
+
pubKey1.toString().should.equal(pubKey2.toString())
|
|
76
|
+
})
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
describe('#toFastBuffer', function () {
|
|
80
|
+
it('should convert to a fast buffer', function () {
|
|
81
|
+
let keyPair, buf
|
|
82
|
+
|
|
83
|
+
keyPair = new KeyPair().fromRandom()
|
|
84
|
+
keyPair.pubKey = undefined
|
|
85
|
+
buf = keyPair.toFastBuffer()
|
|
86
|
+
buf.length.should.greaterThan(32)
|
|
87
|
+
|
|
88
|
+
keyPair = new KeyPair().fromRandom()
|
|
89
|
+
keyPair.privKey = undefined
|
|
90
|
+
buf = keyPair.toFastBuffer()
|
|
91
|
+
buf.length.should.greaterThan(64)
|
|
92
|
+
|
|
93
|
+
keyPair = new KeyPair().fromRandom()
|
|
94
|
+
buf = keyPair.toFastBuffer()
|
|
95
|
+
buf.length.should.greaterThan(32 + 64)
|
|
96
|
+
})
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
describe('#fromString', function () {
|
|
100
|
+
it('should convert to and from a string', function () {
|
|
101
|
+
const keyPair = new KeyPair().fromRandom()
|
|
102
|
+
const str = keyPair.toString()
|
|
103
|
+
new KeyPair()
|
|
104
|
+
.fromString(str)
|
|
105
|
+
.toString()
|
|
106
|
+
.should.equal(str)
|
|
107
|
+
})
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
describe('#toPublic', function () {
|
|
111
|
+
it('should set the private key to undefined', function () {
|
|
112
|
+
const keyPair = new KeyPair().fromRandom()
|
|
113
|
+
const publicKeyPair = keyPair.toPublic()
|
|
114
|
+
|
|
115
|
+
should.exist(publicKeyPair)
|
|
116
|
+
should.exist(publicKeyPair.pubKey)
|
|
117
|
+
should.not.exist(publicKeyPair.privKey)
|
|
118
|
+
})
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
describe('#fromPrivKey', function () {
|
|
122
|
+
it('should make a new key from a privKey', function () {
|
|
123
|
+
should.exist(new KeyPair().fromPrivKey(new PrivKey().fromRandom()).pubKey)
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
it('should convert this known PrivKey to known PubKey', function () {
|
|
127
|
+
const privhex =
|
|
128
|
+
'906977a061af29276e40bf377042ffbde414e496ae2260bbf1fa9d085637bfff'
|
|
129
|
+
const pubhex =
|
|
130
|
+
'02a1633cafcc01ebfb6d78e39f687a1f0995c62fc95f51ead10a02ee0be551b5dc'
|
|
131
|
+
const privKey = new PrivKey().fromBn(Bn(Buffer.from(privhex, 'hex')))
|
|
132
|
+
const key = new KeyPair().fromPrivKey(privKey)
|
|
133
|
+
key.pubKey.toString().should.equal(pubhex)
|
|
134
|
+
})
|
|
135
|
+
|
|
136
|
+
it('should convert this known PrivKey to known PubKey and preserve compressed=false', function () {
|
|
137
|
+
const privhex =
|
|
138
|
+
'906977a061af29276e40bf377042ffbde414e496ae2260bbf1fa9d085637bfff'
|
|
139
|
+
const privKey = new PrivKey().fromBn(Bn(Buffer.from(privhex, 'hex')))
|
|
140
|
+
privKey.compressed = false
|
|
141
|
+
const key = new KeyPair().fromPrivKey(privKey)
|
|
142
|
+
key.pubKey.compressed.should.equal(false)
|
|
143
|
+
})
|
|
144
|
+
})
|
|
145
|
+
|
|
146
|
+
describe('@fromPrivKey', function () {
|
|
147
|
+
it('should make a new key from a privKey', function () {
|
|
148
|
+
should.exist(KeyPair.fromPrivKey(new PrivKey().fromRandom()).pubKey)
|
|
149
|
+
})
|
|
150
|
+
|
|
151
|
+
it('should convert this known PrivKey to known PubKey', function () {
|
|
152
|
+
const privhex =
|
|
153
|
+
'906977a061af29276e40bf377042ffbde414e496ae2260bbf1fa9d085637bfff'
|
|
154
|
+
const pubhex =
|
|
155
|
+
'02a1633cafcc01ebfb6d78e39f687a1f0995c62fc95f51ead10a02ee0be551b5dc'
|
|
156
|
+
const privKey = new PrivKey().fromBn(Bn(Buffer.from(privhex, 'hex')))
|
|
157
|
+
const key = KeyPair.fromPrivKey(privKey)
|
|
158
|
+
key.pubKey.toString().should.equal(pubhex)
|
|
159
|
+
})
|
|
160
|
+
|
|
161
|
+
it('should convert this known PrivKey to known PubKey and preserve compressed=false', function () {
|
|
162
|
+
const privhex =
|
|
163
|
+
'906977a061af29276e40bf377042ffbde414e496ae2260bbf1fa9d085637bfff'
|
|
164
|
+
const privKey = new PrivKey().fromBn(Bn(Buffer.from(privhex, 'hex')))
|
|
165
|
+
privKey.compressed = false
|
|
166
|
+
const key = KeyPair.fromPrivKey(privKey)
|
|
167
|
+
key.pubKey.compressed.should.equal(false)
|
|
168
|
+
})
|
|
169
|
+
})
|
|
170
|
+
|
|
171
|
+
describe('#asyncFromPrivKey', function () {
|
|
172
|
+
it('should convert a privKey same as .fromPrivKey', async function () {
|
|
173
|
+
const privKey = new PrivKey().fromRandom()
|
|
174
|
+
const keyPair = new KeyPair().fromPrivKey(privKey)
|
|
175
|
+
const keyPair2 = await new KeyPair().asyncFromPrivKey(privKey)
|
|
176
|
+
keyPair.pubKey.toString().should.equal(keyPair2.pubKey.toString())
|
|
177
|
+
})
|
|
178
|
+
})
|
|
179
|
+
|
|
180
|
+
describe('@asyncFromPrivKey', function () {
|
|
181
|
+
it('should convert a privKey same as .fromPrivKey', async function () {
|
|
182
|
+
const privKey = new PrivKey().fromRandom()
|
|
183
|
+
const keyPair = KeyPair.fromPrivKey(privKey)
|
|
184
|
+
const keyPair2 = await KeyPair.asyncFromPrivKey(privKey)
|
|
185
|
+
keyPair.pubKey.toString().should.equal(keyPair2.pubKey.toString())
|
|
186
|
+
})
|
|
187
|
+
})
|
|
188
|
+
|
|
189
|
+
describe('#fromRandom', function () {
|
|
190
|
+
it('should make a new priv and pub, should be compressed, mainnet', function () {
|
|
191
|
+
const key = new KeyPair()
|
|
192
|
+
key.fromRandom()
|
|
193
|
+
should.exist(key.privKey)
|
|
194
|
+
should.exist(key.pubKey)
|
|
195
|
+
key.privKey.bn.gt(Bn(0)).should.equal(true)
|
|
196
|
+
key.pubKey.point
|
|
197
|
+
.getX()
|
|
198
|
+
.gt(Bn(0))
|
|
199
|
+
.should.equal(true)
|
|
200
|
+
key.pubKey.point
|
|
201
|
+
.getY()
|
|
202
|
+
.gt(Bn(0))
|
|
203
|
+
.should.equal(true)
|
|
204
|
+
key.privKey.compressed.should.equal(true)
|
|
205
|
+
key.pubKey.compressed.should.equal(true)
|
|
206
|
+
})
|
|
207
|
+
})
|
|
208
|
+
|
|
209
|
+
describe('@fromRandom', function () {
|
|
210
|
+
it('should make a new priv and pub, should be compressed, mainnet', function () {
|
|
211
|
+
const key = KeyPair.fromRandom()
|
|
212
|
+
should.exist(key.privKey)
|
|
213
|
+
should.exist(key.pubKey)
|
|
214
|
+
key.privKey.bn.gt(Bn(0)).should.equal(true)
|
|
215
|
+
key.pubKey.point
|
|
216
|
+
.getX()
|
|
217
|
+
.gt(Bn(0))
|
|
218
|
+
.should.equal(true)
|
|
219
|
+
key.pubKey.point
|
|
220
|
+
.getY()
|
|
221
|
+
.gt(Bn(0))
|
|
222
|
+
.should.equal(true)
|
|
223
|
+
key.privKey.compressed.should.equal(true)
|
|
224
|
+
key.pubKey.compressed.should.equal(true)
|
|
225
|
+
})
|
|
226
|
+
})
|
|
227
|
+
|
|
228
|
+
describe('#asyncFromRandom', function () {
|
|
229
|
+
it('should have a privKey and pubKey and compute same as pubKey methods', async function () {
|
|
230
|
+
const keyPair = await new KeyPair().asyncFromRandom()
|
|
231
|
+
const pubKey = new PubKey().fromPrivKey(keyPair.privKey)
|
|
232
|
+
pubKey.toString().should.equal(keyPair.pubKey.toString())
|
|
233
|
+
})
|
|
234
|
+
})
|
|
235
|
+
|
|
236
|
+
describe('@asyncFromRandom', function () {
|
|
237
|
+
it('should have a privKey and pubKey and compute same as pubKey methods', async function () {
|
|
238
|
+
const keyPair = await KeyPair.asyncFromRandom()
|
|
239
|
+
const pubKey = new PubKey().fromPrivKey(keyPair.privKey)
|
|
240
|
+
pubKey.toString().should.equal(keyPair.pubKey.toString())
|
|
241
|
+
})
|
|
242
|
+
})
|
|
243
|
+
})
|
package/test/merkle.js
ADDED
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
/* global describe,it */
|
|
2
|
+
'use strict'
|
|
3
|
+
import { Merkle } from '../lib/merkle'
|
|
4
|
+
import should from 'should'
|
|
5
|
+
|
|
6
|
+
describe('Merkle', function () {
|
|
7
|
+
it('should satisfy this basic API', function () {
|
|
8
|
+
const merkle = new Merkle()
|
|
9
|
+
should.exist(merkle)
|
|
10
|
+
should.exist(merkle)
|
|
11
|
+
})
|
|
12
|
+
|
|
13
|
+
describe('hash', function () {
|
|
14
|
+
it('should hash these buffers', function () {
|
|
15
|
+
const merkle1 = new Merkle(undefined, Buffer.alloc(0))
|
|
16
|
+
const merkle2 = new Merkle(undefined, Buffer.alloc(0))
|
|
17
|
+
const merkle = new Merkle(undefined, undefined, merkle1, merkle2)
|
|
18
|
+
let hashBuf = merkle.hash()
|
|
19
|
+
hashBuf.length.should.equal(32)
|
|
20
|
+
hashBuf
|
|
21
|
+
.toString('hex')
|
|
22
|
+
.should.equal(
|
|
23
|
+
'352b71f195e85adbaefdcd6d7380d87067865d9a17c44d38982bb8a40bd0b393'
|
|
24
|
+
)
|
|
25
|
+
// and a second time ...
|
|
26
|
+
hashBuf = merkle.hash()
|
|
27
|
+
hashBuf
|
|
28
|
+
.toString('hex')
|
|
29
|
+
.should.equal(
|
|
30
|
+
'352b71f195e85adbaefdcd6d7380d87067865d9a17c44d38982bb8a40bd0b393'
|
|
31
|
+
)
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
it('should hash this buffer', function () {
|
|
35
|
+
const merkle = new Merkle(undefined, Buffer.alloc(0))
|
|
36
|
+
const hashBuf = merkle.hash()
|
|
37
|
+
hashBuf.length.should.equal(32)
|
|
38
|
+
hashBuf
|
|
39
|
+
.toString('hex')
|
|
40
|
+
.should.equal(
|
|
41
|
+
'5df6e0e2761359d30a8275058e299fcc0381534545f55cf43e41983f5d4c9456'
|
|
42
|
+
)
|
|
43
|
+
})
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
describe('#fromBuffers', function () {
|
|
47
|
+
it('should find this merkle root from three buffers', function () {
|
|
48
|
+
const bufs = [Buffer.alloc(0), Buffer.alloc(0), Buffer.alloc(0)]
|
|
49
|
+
const merkle = new Merkle().fromBuffers(bufs)
|
|
50
|
+
const hashBuf = merkle.hash()
|
|
51
|
+
hashBuf.length.should.equal(32)
|
|
52
|
+
hashBuf
|
|
53
|
+
.toString('hex')
|
|
54
|
+
.should.equal(
|
|
55
|
+
'647fedb4d19e11915076dd60fa72a8e03eb33f6dec87a4f0662b0c1f378a81cb'
|
|
56
|
+
)
|
|
57
|
+
merkle.leavesNum().should.equal(4)
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
it('should find this merkle root from four buffers', function () {
|
|
61
|
+
const bufs = [
|
|
62
|
+
Buffer.alloc(0),
|
|
63
|
+
Buffer.alloc(0),
|
|
64
|
+
Buffer.alloc(0),
|
|
65
|
+
Buffer.alloc(0)
|
|
66
|
+
]
|
|
67
|
+
const merkle = new Merkle().fromBuffers(bufs)
|
|
68
|
+
const hashBuf = merkle.hash()
|
|
69
|
+
hashBuf.length.should.equal(32)
|
|
70
|
+
hashBuf
|
|
71
|
+
.toString('hex')
|
|
72
|
+
.should.equal(
|
|
73
|
+
'647fedb4d19e11915076dd60fa72a8e03eb33f6dec87a4f0662b0c1f378a81cb'
|
|
74
|
+
)
|
|
75
|
+
merkle.leavesNum().should.equal(4)
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
it('should find this merkle root from 9 buffers', function () {
|
|
79
|
+
const bufs = []
|
|
80
|
+
for (let i = 0; i < 9; i++) {
|
|
81
|
+
bufs[i] = Buffer.alloc(0)
|
|
82
|
+
}
|
|
83
|
+
const merkle = new Merkle().fromBuffers(bufs)
|
|
84
|
+
const hashBuf = merkle.hash()
|
|
85
|
+
hashBuf.length.should.equal(32)
|
|
86
|
+
hashBuf
|
|
87
|
+
.toString('hex')
|
|
88
|
+
.should.equal(
|
|
89
|
+
'9f187f4339d07e1963d404f31d28e4557cd72a320085d188f26c943fc604281e'
|
|
90
|
+
)
|
|
91
|
+
merkle.leavesNum().should.equal(16)
|
|
92
|
+
})
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
describe('@fromBuffers', function () {
|
|
96
|
+
it('should find this merkle root from three buffers', function () {
|
|
97
|
+
const bufs = [Buffer.alloc(0), Buffer.alloc(0), Buffer.alloc(0)]
|
|
98
|
+
const merkle = Merkle.fromBuffers(bufs)
|
|
99
|
+
const hashBuf = merkle.hash()
|
|
100
|
+
hashBuf.length.should.equal(32)
|
|
101
|
+
hashBuf
|
|
102
|
+
.toString('hex')
|
|
103
|
+
.should.equal(
|
|
104
|
+
'647fedb4d19e11915076dd60fa72a8e03eb33f6dec87a4f0662b0c1f378a81cb'
|
|
105
|
+
)
|
|
106
|
+
merkle.leavesNum().should.equal(4)
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
it('should find this merkle root from four buffers', function () {
|
|
110
|
+
const bufs = [
|
|
111
|
+
Buffer.alloc(0),
|
|
112
|
+
Buffer.alloc(0),
|
|
113
|
+
Buffer.alloc(0),
|
|
114
|
+
Buffer.alloc(0)
|
|
115
|
+
]
|
|
116
|
+
const merkle = Merkle.fromBuffers(bufs)
|
|
117
|
+
const hashBuf = merkle.hash()
|
|
118
|
+
hashBuf.length.should.equal(32)
|
|
119
|
+
hashBuf
|
|
120
|
+
.toString('hex')
|
|
121
|
+
.should.equal(
|
|
122
|
+
'647fedb4d19e11915076dd60fa72a8e03eb33f6dec87a4f0662b0c1f378a81cb'
|
|
123
|
+
)
|
|
124
|
+
merkle.leavesNum().should.equal(4)
|
|
125
|
+
})
|
|
126
|
+
|
|
127
|
+
it('should find this merkle root from 9 buffers', function () {
|
|
128
|
+
const bufs = []
|
|
129
|
+
for (let i = 0; i < 9; i++) {
|
|
130
|
+
bufs[i] = Buffer.alloc(0)
|
|
131
|
+
}
|
|
132
|
+
const merkle = Merkle.fromBuffers(bufs)
|
|
133
|
+
const hashBuf = merkle.hash()
|
|
134
|
+
hashBuf.length.should.equal(32)
|
|
135
|
+
hashBuf
|
|
136
|
+
.toString('hex')
|
|
137
|
+
.should.equal(
|
|
138
|
+
'9f187f4339d07e1963d404f31d28e4557cd72a320085d188f26c943fc604281e'
|
|
139
|
+
)
|
|
140
|
+
merkle.leavesNum().should.equal(16)
|
|
141
|
+
})
|
|
142
|
+
})
|
|
143
|
+
|
|
144
|
+
describe('#fromBufferArrays', function () {
|
|
145
|
+
it('should find this merkle root from two buffers', function () {
|
|
146
|
+
const bufs1 = [Buffer.alloc(0)]
|
|
147
|
+
const bufs2 = [Buffer.alloc(0)]
|
|
148
|
+
const merkle = new Merkle().fromBufferArrays(bufs1, bufs2)
|
|
149
|
+
const hashBuf = merkle.hash()
|
|
150
|
+
hashBuf.length.should.equal(32)
|
|
151
|
+
})
|
|
152
|
+
|
|
153
|
+
it('should find this merkle root from four buffers', function () {
|
|
154
|
+
const bufs1 = [Buffer.alloc(0), Buffer.alloc(0)]
|
|
155
|
+
const bufs2 = [Buffer.alloc(0), Buffer.alloc(0)]
|
|
156
|
+
const merkle = new Merkle().fromBufferArrays(bufs1, bufs2)
|
|
157
|
+
const hashBuf = merkle.hash()
|
|
158
|
+
hashBuf.length.should.equal(32)
|
|
159
|
+
})
|
|
160
|
+
})
|
|
161
|
+
|
|
162
|
+
describe('@fromBufferArrays', function () {
|
|
163
|
+
it('should find this merkle root from two buffers', function () {
|
|
164
|
+
const bufs1 = [Buffer.alloc(0)]
|
|
165
|
+
const bufs2 = [Buffer.alloc(0)]
|
|
166
|
+
const merkle = Merkle.fromBufferArrays(bufs1, bufs2)
|
|
167
|
+
const hashBuf = merkle.hash()
|
|
168
|
+
hashBuf.length.should.equal(32)
|
|
169
|
+
})
|
|
170
|
+
|
|
171
|
+
it('should find this merkle root from four buffers', function () {
|
|
172
|
+
const bufs1 = [Buffer.alloc(0), Buffer.alloc(0)]
|
|
173
|
+
const bufs2 = [Buffer.alloc(0), Buffer.alloc(0)]
|
|
174
|
+
const merkle = Merkle.fromBufferArrays(bufs1, bufs2)
|
|
175
|
+
const hashBuf = merkle.hash()
|
|
176
|
+
hashBuf.length.should.equal(32)
|
|
177
|
+
})
|
|
178
|
+
})
|
|
179
|
+
})
|