@smartledger/bsv 8.3.1 → 9.1.0
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/CHANGELOG.md +228 -0
- package/README.md +152 -1031
- package/STABILITY.md +134 -0
- package/bsv-gdaf.min.js +58 -53
- package/bsv-ltp.min.js +23 -18
- package/bsv-script-helper.min.js +1 -1
- package/bsv-smartcontract.min.js +21 -21
- package/bsv.bundle.js +58 -53
- package/bsv.d.ts +0 -4
- package/bsv.min.js +58 -53
- package/docs/BRC220_BATCH_LEAF_AMENDMENT.md +82 -7
- package/docs/MODULE_REFERENCE_COMPLETE.md +27 -27
- package/docs/README.md +2 -1
- package/docs/THREAT_MODEL.md +10 -2
- package/docs/advanced/UTXO_MANAGER_GUIDE.md +1 -1
- package/docs/getting-started/INSTALLATION.md +23 -23
- package/docs/getting-started/QUICK_START.md +7 -7
- package/docs/migration/FROM_BSV_1_5_6.md +5 -5
- package/docs/proposals/10.0.0-package-split.md +131 -0
- package/index.js +15 -26
- package/lib/block/merkleblock.js +19 -5
- package/lib/covenant/helpers.js +34 -35
- package/lib/covenant/pushtx.js +2 -2
- package/lib/custom-script-helper.js +0 -16
- package/lib/hdprivatekey.js +15 -0
- package/lib/ordinals/ordlock.js +1 -1
- package/lib/script/interpreter.js +109 -1
- package/lib/smart_contract/builder.js +14 -0
- package/lib/smart_contract/debugger.js +2 -2
- package/lib/smart_contract/dsl.js +2 -2
- package/lib/smart_contract/index.js +0 -3
- package/lib/smart_contract/locks.js +1 -41
- package/lib/smart_contract/pels.js +1 -1
- package/lib/smart_contract/token.js +1 -1
- package/lib/util/deprecate.js +159 -0
- package/package.json +19 -82
- package/tools/gen-brc220-batch-vector.js +205 -0
- package/version.js +1 -1
- package/.mocharc.json +0 -5
- package/test/address.js +0 -629
- package/test/block/block.js +0 -239
- package/test/block/blockheader.js +0 -270
- package/test/block/merkleblock.js +0 -207
- package/test/build/bundle_crypto_shim.js +0 -104
- package/test/build/bundle_externals.js +0 -125
- package/test/build/bundle_smoke.js +0 -55
- package/test/build/esbuild_main.js +0 -65
- package/test/build/esm_wrapper.js +0 -73
- package/test/build/exports_resolution.js +0 -84
- package/test/build/version_sync.js +0 -14
- package/test/cli/smoke.js +0 -261
- package/test/consensus/base58-vectors.js +0 -120
- package/test/consensus/sv-script-vectors.js +0 -81
- package/test/consensus/sv-sighash-vectors.js +0 -51
- package/test/consensus/sv-tx-vectors.js +0 -48
- package/test/credentials-test.js +0 -332
- package/test/crypto/backend_selection.js +0 -126
- package/test/crypto/bn.js +0 -168
- package/test/crypto/ecdsa.js +0 -426
- package/test/crypto/elliptic-fixed.js +0 -61
- package/test/crypto/hash.browser.js +0 -121
- package/test/crypto/hash.js +0 -122
- package/test/crypto/point.js +0 -193
- package/test/crypto/random.js +0 -105
- package/test/crypto/security.js +0 -176
- package/test/crypto/shamir.js +0 -177
- package/test/crypto/shamir_rngcap.js +0 -29
- package/test/crypto/signature.js +0 -399
- package/test/data/bip69.json +0 -215
- package/test/data/bitcoin-sv/README.md +0 -170
- package/test/data/bitcoin-sv/base58_encode_decode.json +0 -14
- package/test/data/bitcoin-sv/base58_keys_invalid.json +0 -152
- package/test/data/bitcoin-sv/base58_keys_valid.json +0 -452
- package/test/data/bitcoin-sv/script_tests.json +0 -2591
- package/test/data/bitcoin-sv/sighash.json +0 -1003
- package/test/data/bitcoin-sv/tx_invalid.json +0 -285
- package/test/data/bitcoin-sv/tx_valid.json +0 -367
- 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/merkleblocks.js +0 -486
- package/test/data/messages.json +0 -22
- package/test/data/sighash.json +0 -1004
- package/test/data/tx_creation.json +0 -85
- package/test/didweb/relationships.js +0 -144
- package/test/ecies/bitcore-ecies.js +0 -178
- package/test/ecies/electrum-ecies.js +0 -206
- package/test/encoding/base58.js +0 -131
- package/test/encoding/base58check.js +0 -145
- package/test/encoding/bufferreader.js +0 -328
- package/test/encoding/bufferwriter.js +0 -160
- package/test/encoding/varint.js +0 -104
- package/test/gdaf/anchor_no_key_leak.js +0 -146
- package/test/gdaf/anchor_spv.js +0 -163
- package/test/gdaf/canonicalization.js +0 -106
- package/test/gdaf/canonicalize.js +0 -140
- package/test/gdaf/zk_prover.js +0 -204
- package/test/hdkeys.js +0 -365
- package/test/hdprivatekey.js +0 -339
- package/test/hdpublickey.js +0 -293
- package/test/index.js +0 -16
- package/test/ltp/ids.js +0 -74
- package/test/ltp/right.js +0 -198
- package/test/ltp/verify_failclosed.js +0 -134
- package/test/message/message.js +0 -190
- package/test/mnemonic/data/fixtures.json +0 -300
- package/test/mnemonic/mnemonic.js +0 -277
- package/test/mnemonic/mocha.opts +0 -1
- package/test/mnemonic/pbkdf2.test.js +0 -43
- package/test/networks.js +0 -207
- package/test/notaryhash/batch_leaf.js +0 -140
- package/test/notaryhash/certificate.js +0 -249
- package/test/notaryhash/encoding.js +0 -186
- package/test/notaryhash/interop.js +0 -112
- package/test/notaryhash/merkle.js +0 -181
- package/test/notaryhash/script.js +0 -270
- package/test/notaryhash/verify.js +0 -342
- package/test/opcode.js +0 -186
- package/test/ordinals/bsv20.js +0 -337
- package/test/ordinals/inscription.js +0 -319
- package/test/ordinals/ordlock.js +0 -613
- package/test/privatekey.js +0 -540
- package/test/publickey.js +0 -411
- package/test/regressions.js +0 -215
- package/test/script/chronicle.js +0 -543
- package/test/script/defaults.js +0 -160
- package/test/script/genesis_limits.js +0 -203
- package/test/script/interpreter.js +0 -776
- package/test/script/script.js +0 -1259
- package/test/script/string_ops.js +0 -88
- package/test/security/fail_closed_contracts.js +0 -104
- package/test/security/threat_model_coverage.js +0 -31
- package/test/smart_contract/covenants.js +0 -245
- package/test/smart_contract/dsl_debugger.js +0 -101
- package/test/smart_contract/extract_field.js +0 -61
- package/test/smart_contract/nonenforcing_guard.js +0 -44
- package/test/smart_contract/ordinal_transfer.js +0 -81
- package/test/smart_contract/preimage.js +0 -98
- package/test/smart_contract/sighash_marketplace.js +0 -108
- package/test/smart_contract/token_generalized.js +0 -185
- package/test/spv/headerchain.js +0 -86
- package/test/spv/merkleproof.js +0 -133
- package/test/statuslist/failclosed.js +0 -78
- package/test/transaction/deserialize.js +0 -33
- package/test/transaction/input/input.js +0 -92
- package/test/transaction/input/multisig.js +0 -174
- package/test/transaction/input/multisigscripthash.js +0 -111
- package/test/transaction/input/publickey.js +0 -68
- package/test/transaction/input/publickeyhash.js +0 -59
- package/test/transaction/output.js +0 -185
- package/test/transaction/sighash.js +0 -91
- package/test/transaction/signature.js +0 -127
- package/test/transaction/transaction.js +0 -1299
- package/test/transaction/unspentoutput.js +0 -97
- package/test/types/dts_drift.js +0 -124
- package/test/types/surface_honesty.js +0 -102
- package/test/util/id.js +0 -72
- package/test/util/js.js +0 -76
- package/test/util/preconditions.js +0 -79
- package/test/vcjwt/interop.js +0 -126
|
@@ -1,270 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
var bsv = require('../..')
|
|
4
|
-
var BN = require('../../lib/crypto/bn')
|
|
5
|
-
var BufferReader = bsv.encoding.BufferReader
|
|
6
|
-
var BufferWriter = bsv.encoding.BufferWriter
|
|
7
|
-
|
|
8
|
-
var BlockHeader = bsv.BlockHeader
|
|
9
|
-
var fs = require('fs')
|
|
10
|
-
var should = require('chai').should()
|
|
11
|
-
|
|
12
|
-
// https://test-insight.bitpay.com/block/000000000b99b16390660d79fcc138d2ad0c89a0d044c4201a02bdf1f61ffa11
|
|
13
|
-
var dataRawBlockBuffer = fs.readFileSync('test/data/blk86756-testnet.dat')
|
|
14
|
-
var dataRawBlockBinary = fs.readFileSync('test/data/blk86756-testnet.dat', 'binary')
|
|
15
|
-
var dataRawId = '000000000b99b16390660d79fcc138d2ad0c89a0d044c4201a02bdf1f61ffa11'
|
|
16
|
-
var data = require('../data/blk86756-testnet')
|
|
17
|
-
|
|
18
|
-
describe('BlockHeader', function () {
|
|
19
|
-
var version = data.version
|
|
20
|
-
var prevblockidbuf = Buffer.from(data.prevblockidhex, 'hex')
|
|
21
|
-
var merklerootbuf = Buffer.from(data.merkleroothex, 'hex')
|
|
22
|
-
var time = data.time
|
|
23
|
-
var bits = data.bits
|
|
24
|
-
var nonce = data.nonce
|
|
25
|
-
var bh = new BlockHeader({
|
|
26
|
-
version: version,
|
|
27
|
-
prevHash: prevblockidbuf,
|
|
28
|
-
merkleRoot: merklerootbuf,
|
|
29
|
-
time: time,
|
|
30
|
-
bits: bits,
|
|
31
|
-
nonce: nonce
|
|
32
|
-
})
|
|
33
|
-
var bhhex = data.blockheaderhex
|
|
34
|
-
var bhbuf = Buffer.from(bhhex, 'hex')
|
|
35
|
-
|
|
36
|
-
it('should make a new blockheader', function () {
|
|
37
|
-
BlockHeader(bhbuf).toBuffer().toString('hex').should.equal(bhhex)
|
|
38
|
-
})
|
|
39
|
-
|
|
40
|
-
it('should not make an empty block', function () {
|
|
41
|
-
(function () {
|
|
42
|
-
BlockHeader()
|
|
43
|
-
}).should.throw('Unrecognized argument for BlockHeader')
|
|
44
|
-
})
|
|
45
|
-
|
|
46
|
-
describe('#constructor', function () {
|
|
47
|
-
it('should set all the variables', function () {
|
|
48
|
-
var bh = new BlockHeader({
|
|
49
|
-
version: version,
|
|
50
|
-
prevHash: prevblockidbuf,
|
|
51
|
-
merkleRoot: merklerootbuf,
|
|
52
|
-
time: time,
|
|
53
|
-
bits: bits,
|
|
54
|
-
nonce: nonce
|
|
55
|
-
})
|
|
56
|
-
should.exist(bh.version)
|
|
57
|
-
should.exist(bh.prevHash)
|
|
58
|
-
should.exist(bh.merkleRoot)
|
|
59
|
-
should.exist(bh.time)
|
|
60
|
-
should.exist(bh.bits)
|
|
61
|
-
should.exist(bh.nonce)
|
|
62
|
-
})
|
|
63
|
-
|
|
64
|
-
it('will throw an error if the argument object hash property doesn\'t match', function () {
|
|
65
|
-
(function () {
|
|
66
|
-
new BlockHeader({ //eslint-disable-line
|
|
67
|
-
hash: '000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f',
|
|
68
|
-
version: version,
|
|
69
|
-
prevHash: prevblockidbuf,
|
|
70
|
-
merkleRoot: merklerootbuf,
|
|
71
|
-
time: time,
|
|
72
|
-
bits: bits,
|
|
73
|
-
nonce: nonce
|
|
74
|
-
})
|
|
75
|
-
}).should.throw('Argument object hash property does not match block hash.')
|
|
76
|
-
})
|
|
77
|
-
})
|
|
78
|
-
|
|
79
|
-
describe('version', function () {
|
|
80
|
-
it('is interpreted as an int32le', function () {
|
|
81
|
-
var hex = 'ffffffff00000000000000000000000000000000000000000000000000000000000000004141414141414141414141414141414141414141414141414141414141414141010000000200000003000000'
|
|
82
|
-
var header = BlockHeader.fromBuffer(Buffer.from(hex, 'hex'))
|
|
83
|
-
header.version.should.equal(-1)
|
|
84
|
-
header.timestamp.should.equal(1)
|
|
85
|
-
})
|
|
86
|
-
})
|
|
87
|
-
|
|
88
|
-
describe('#fromObject', function () {
|
|
89
|
-
it('should set all the variables', function () {
|
|
90
|
-
var bh = BlockHeader.fromObject({
|
|
91
|
-
version: version,
|
|
92
|
-
prevHash: prevblockidbuf.toString('hex'),
|
|
93
|
-
merkleRoot: merklerootbuf.toString('hex'),
|
|
94
|
-
time: time,
|
|
95
|
-
bits: bits,
|
|
96
|
-
nonce: nonce
|
|
97
|
-
})
|
|
98
|
-
should.exist(bh.version)
|
|
99
|
-
should.exist(bh.prevHash)
|
|
100
|
-
should.exist(bh.merkleRoot)
|
|
101
|
-
should.exist(bh.time)
|
|
102
|
-
should.exist(bh.bits)
|
|
103
|
-
should.exist(bh.nonce)
|
|
104
|
-
})
|
|
105
|
-
})
|
|
106
|
-
|
|
107
|
-
describe('#toJSON', function () {
|
|
108
|
-
it('should set all the variables', function () {
|
|
109
|
-
var json = bh.toJSON()
|
|
110
|
-
should.exist(json.version)
|
|
111
|
-
should.exist(json.prevHash)
|
|
112
|
-
should.exist(json.merkleRoot)
|
|
113
|
-
should.exist(json.time)
|
|
114
|
-
should.exist(json.bits)
|
|
115
|
-
should.exist(json.nonce)
|
|
116
|
-
})
|
|
117
|
-
})
|
|
118
|
-
|
|
119
|
-
describe('#fromJSON', function () {
|
|
120
|
-
it('should parse this known json string', function () {
|
|
121
|
-
var jsonString = JSON.stringify({
|
|
122
|
-
version: version,
|
|
123
|
-
prevHash: prevblockidbuf,
|
|
124
|
-
merkleRoot: merklerootbuf,
|
|
125
|
-
time: time,
|
|
126
|
-
bits: bits,
|
|
127
|
-
nonce: nonce
|
|
128
|
-
})
|
|
129
|
-
|
|
130
|
-
var json = new BlockHeader(JSON.parse(jsonString))
|
|
131
|
-
should.exist(json.version)
|
|
132
|
-
should.exist(json.prevHash)
|
|
133
|
-
should.exist(json.merkleRoot)
|
|
134
|
-
should.exist(json.time)
|
|
135
|
-
should.exist(json.bits)
|
|
136
|
-
should.exist(json.nonce)
|
|
137
|
-
})
|
|
138
|
-
})
|
|
139
|
-
|
|
140
|
-
describe('#fromString/#toString', function () {
|
|
141
|
-
it('should output/input a block hex string', function () {
|
|
142
|
-
var b = BlockHeader.fromString(bhhex)
|
|
143
|
-
b.toString().should.equal(bhhex)
|
|
144
|
-
})
|
|
145
|
-
})
|
|
146
|
-
|
|
147
|
-
describe('#fromBuffer', function () {
|
|
148
|
-
it('should parse this known buffer', function () {
|
|
149
|
-
BlockHeader.fromBuffer(bhbuf).toBuffer().toString('hex').should.equal(bhhex)
|
|
150
|
-
})
|
|
151
|
-
})
|
|
152
|
-
|
|
153
|
-
describe('#fromBufferReader', function () {
|
|
154
|
-
it('should parse this known buffer', function () {
|
|
155
|
-
BlockHeader.fromBufferReader(BufferReader(bhbuf)).toBuffer().toString('hex').should.equal(bhhex)
|
|
156
|
-
})
|
|
157
|
-
})
|
|
158
|
-
|
|
159
|
-
describe('#toBuffer', function () {
|
|
160
|
-
it('should output this known buffer', function () {
|
|
161
|
-
BlockHeader.fromBuffer(bhbuf).toBuffer().toString('hex').should.equal(bhhex)
|
|
162
|
-
})
|
|
163
|
-
})
|
|
164
|
-
|
|
165
|
-
describe('#toBufferWriter', function () {
|
|
166
|
-
it('should output this known buffer', function () {
|
|
167
|
-
BlockHeader.fromBuffer(bhbuf).toBufferWriter().concat().toString('hex').should.equal(bhhex)
|
|
168
|
-
})
|
|
169
|
-
|
|
170
|
-
it('doesn\'t create a bufferWriter if one provided', function () {
|
|
171
|
-
var writer = new BufferWriter()
|
|
172
|
-
var blockHeader = BlockHeader.fromBuffer(bhbuf)
|
|
173
|
-
blockHeader.toBufferWriter(writer).should.equal(writer)
|
|
174
|
-
})
|
|
175
|
-
})
|
|
176
|
-
|
|
177
|
-
describe('#inspect', function () {
|
|
178
|
-
it('should return the correct inspect of the genesis block', function () {
|
|
179
|
-
var block = BlockHeader.fromRawBlock(dataRawBlockBinary)
|
|
180
|
-
block.inspect().should.equal('<BlockHeader ' + dataRawId + '>')
|
|
181
|
-
})
|
|
182
|
-
})
|
|
183
|
-
|
|
184
|
-
describe('#fromRawBlock', function () {
|
|
185
|
-
it('should instantiate from a raw block binary', function () {
|
|
186
|
-
var x = BlockHeader.fromRawBlock(dataRawBlockBinary)
|
|
187
|
-
x.version.should.equal(2)
|
|
188
|
-
new BN(x.bits).toString('hex').should.equal('1c3fffc0')
|
|
189
|
-
})
|
|
190
|
-
|
|
191
|
-
it('should instantiate from raw block buffer', function () {
|
|
192
|
-
var x = BlockHeader.fromRawBlock(dataRawBlockBuffer)
|
|
193
|
-
x.version.should.equal(2)
|
|
194
|
-
new BN(x.bits).toString('hex').should.equal('1c3fffc0')
|
|
195
|
-
})
|
|
196
|
-
})
|
|
197
|
-
|
|
198
|
-
describe('#validTimestamp', function () {
|
|
199
|
-
var x = BlockHeader.fromRawBlock(dataRawBlockBuffer)
|
|
200
|
-
|
|
201
|
-
it('should validate timpstamp as true', function () {
|
|
202
|
-
var valid = x.validTimestamp(x)
|
|
203
|
-
valid.should.equal(true)
|
|
204
|
-
})
|
|
205
|
-
|
|
206
|
-
it('should validate timestamp as false', function () {
|
|
207
|
-
x.time = Math.round(new Date().getTime() / 1000) + BlockHeader.Constants.MAX_TIME_OFFSET + 100
|
|
208
|
-
var valid = x.validTimestamp(x)
|
|
209
|
-
valid.should.equal(false)
|
|
210
|
-
})
|
|
211
|
-
})
|
|
212
|
-
|
|
213
|
-
describe('#validProofOfWork', function () {
|
|
214
|
-
it('should validate proof-of-work as true', function () {
|
|
215
|
-
var x = BlockHeader.fromRawBlock(dataRawBlockBuffer)
|
|
216
|
-
var valid = x.validProofOfWork(x)
|
|
217
|
-
valid.should.equal(true)
|
|
218
|
-
})
|
|
219
|
-
|
|
220
|
-
it('should validate proof of work as false because incorrect proof of work', function () {
|
|
221
|
-
var x = BlockHeader.fromRawBlock(dataRawBlockBuffer)
|
|
222
|
-
var nonce = x.nonce
|
|
223
|
-
x.nonce = 0
|
|
224
|
-
var valid = x.validProofOfWork(x)
|
|
225
|
-
valid.should.equal(false)
|
|
226
|
-
x.nonce = nonce
|
|
227
|
-
})
|
|
228
|
-
})
|
|
229
|
-
|
|
230
|
-
describe('#getDifficulty', function () {
|
|
231
|
-
it('should get the correct difficulty for block 86756', function () {
|
|
232
|
-
var x = BlockHeader.fromRawBlock(dataRawBlockBuffer)
|
|
233
|
-
x.bits.should.equal(0x1c3fffc0)
|
|
234
|
-
x.getDifficulty().should.equal(4)
|
|
235
|
-
})
|
|
236
|
-
|
|
237
|
-
it('should get the correct difficulty for testnet block 552065', function () {
|
|
238
|
-
var x = new BlockHeader({
|
|
239
|
-
bits: 0x1b00c2a8
|
|
240
|
-
})
|
|
241
|
-
x.getDifficulty().should.equal(86187.62562209)
|
|
242
|
-
})
|
|
243
|
-
|
|
244
|
-
it('should get the correct difficulty for livenet block 373043', function () {
|
|
245
|
-
var x = new BlockHeader({
|
|
246
|
-
bits: 0x18134dc1
|
|
247
|
-
})
|
|
248
|
-
x.getDifficulty().should.equal(56957648455.01001)
|
|
249
|
-
})
|
|
250
|
-
|
|
251
|
-
it('should get the correct difficulty for livenet block 340000', function () {
|
|
252
|
-
var x = new BlockHeader({
|
|
253
|
-
bits: 0x1819012f
|
|
254
|
-
})
|
|
255
|
-
x.getDifficulty().should.equal(43971662056.08958)
|
|
256
|
-
})
|
|
257
|
-
|
|
258
|
-
it('should use exponent notation if difficulty is larger than Javascript number', function () {
|
|
259
|
-
var x = new BlockHeader({
|
|
260
|
-
bits: 0x0900c2a8
|
|
261
|
-
})
|
|
262
|
-
x.getDifficulty().should.equal(1.9220482782645836 * 1e48)
|
|
263
|
-
})
|
|
264
|
-
})
|
|
265
|
-
|
|
266
|
-
it('coverage: caches the "_id" property', function () {
|
|
267
|
-
var blockHeader = BlockHeader.fromRawBlock(dataRawBlockBuffer)
|
|
268
|
-
blockHeader.id.should.equal(blockHeader.id)
|
|
269
|
-
})
|
|
270
|
-
})
|
|
@@ -1,207 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
var should = require('chai').should()
|
|
4
|
-
|
|
5
|
-
var bsv = require('../..')
|
|
6
|
-
var MerkleBlock = bsv.MerkleBlock
|
|
7
|
-
var BufferReader = bsv.encoding.BufferReader
|
|
8
|
-
var BufferWriter = bsv.encoding.BufferWriter
|
|
9
|
-
var Transaction = bsv.Transaction
|
|
10
|
-
var data = require('../data/merkleblocks.js')
|
|
11
|
-
var transactionVector = require('../data/tx_creation')
|
|
12
|
-
|
|
13
|
-
describe('MerkleBlock', function () {
|
|
14
|
-
var blockhex = data.HEX[0]
|
|
15
|
-
var blockbuf = Buffer.from(blockhex, 'hex')
|
|
16
|
-
var blockJSON = JSON.stringify(data.JSON[0])
|
|
17
|
-
var blockObject = JSON.parse(JSON.stringify(data.JSON[0]))
|
|
18
|
-
|
|
19
|
-
describe('#constructor', function () {
|
|
20
|
-
it('should make a new merkleblock from buffer', function () {
|
|
21
|
-
var b = MerkleBlock(blockbuf)
|
|
22
|
-
b.toBuffer().toString('hex').should.equal(blockhex)
|
|
23
|
-
})
|
|
24
|
-
|
|
25
|
-
it('should make a new merkleblock from object', function () {
|
|
26
|
-
var b = MerkleBlock(blockObject)
|
|
27
|
-
b.toObject().should.deep.equal(blockObject)
|
|
28
|
-
})
|
|
29
|
-
|
|
30
|
-
it('should make a new merkleblock from JSON', function () {
|
|
31
|
-
var b = MerkleBlock(JSON.parse(blockJSON))
|
|
32
|
-
JSON.stringify(b).should.equal(blockJSON)
|
|
33
|
-
})
|
|
34
|
-
|
|
35
|
-
it('should not make an empty block', function () {
|
|
36
|
-
(function () {
|
|
37
|
-
return new MerkleBlock()
|
|
38
|
-
}).should.throw('Unrecognized argument for MerkleBlock')
|
|
39
|
-
})
|
|
40
|
-
})
|
|
41
|
-
|
|
42
|
-
describe('#fromObject', function () {
|
|
43
|
-
it('should set these known values', function () {
|
|
44
|
-
var block = MerkleBlock.fromObject(JSON.parse(blockJSON))
|
|
45
|
-
should.exist(block.header)
|
|
46
|
-
should.exist(block.numTransactions)
|
|
47
|
-
should.exist(block.hashes)
|
|
48
|
-
should.exist(block.flags)
|
|
49
|
-
})
|
|
50
|
-
|
|
51
|
-
it('should set these known values', function () {
|
|
52
|
-
var block = MerkleBlock(JSON.parse(blockJSON))
|
|
53
|
-
should.exist(block.header)
|
|
54
|
-
should.exist(block.numTransactions)
|
|
55
|
-
should.exist(block.hashes)
|
|
56
|
-
should.exist(block.flags)
|
|
57
|
-
})
|
|
58
|
-
|
|
59
|
-
it('accepts an object as argument', function () {
|
|
60
|
-
var block = MerkleBlock(blockbuf)
|
|
61
|
-
should.exist(MerkleBlock.fromObject(block.toObject()))
|
|
62
|
-
})
|
|
63
|
-
})
|
|
64
|
-
|
|
65
|
-
describe('#toJSON', function () {
|
|
66
|
-
it('should recover these known values', function () {
|
|
67
|
-
var block = new MerkleBlock(JSON.parse(blockJSON))
|
|
68
|
-
var b = JSON.parse(JSON.stringify(block))
|
|
69
|
-
should.exist(block.header)
|
|
70
|
-
should.exist(block.numTransactions)
|
|
71
|
-
should.exist(block.hashes)
|
|
72
|
-
should.exist(block.flags)
|
|
73
|
-
should.exist(b.header)
|
|
74
|
-
should.exist(b.numTransactions)
|
|
75
|
-
should.exist(b.hashes)
|
|
76
|
-
should.exist(b.flags)
|
|
77
|
-
})
|
|
78
|
-
})
|
|
79
|
-
|
|
80
|
-
describe('#fromBuffer', function () {
|
|
81
|
-
it('should make a block from this known buffer', function () {
|
|
82
|
-
var block = MerkleBlock.fromBuffer(blockbuf)
|
|
83
|
-
block.toBuffer().toString('hex').should.equal(blockhex)
|
|
84
|
-
})
|
|
85
|
-
})
|
|
86
|
-
|
|
87
|
-
describe('#fromBufferReader', function () {
|
|
88
|
-
it('should make a block from this known buffer', function () {
|
|
89
|
-
var block = MerkleBlock.fromBufferReader(BufferReader(blockbuf))
|
|
90
|
-
block.toBuffer().toString('hex').should.equal(blockhex)
|
|
91
|
-
})
|
|
92
|
-
})
|
|
93
|
-
|
|
94
|
-
describe('#toBuffer', function () {
|
|
95
|
-
it('should recover a block from this known buffer', function () {
|
|
96
|
-
var block = MerkleBlock.fromBuffer(blockbuf)
|
|
97
|
-
block.toBuffer().toString('hex').should.equal(blockhex)
|
|
98
|
-
})
|
|
99
|
-
})
|
|
100
|
-
|
|
101
|
-
describe('#toBufferWriter', function () {
|
|
102
|
-
it('should recover a block from this known buffer', function () {
|
|
103
|
-
var block = MerkleBlock.fromBuffer(blockbuf)
|
|
104
|
-
block.toBufferWriter().concat().toString('hex').should.equal(blockhex)
|
|
105
|
-
})
|
|
106
|
-
|
|
107
|
-
it('doesn\'t create a bufferWriter if one provided', function () {
|
|
108
|
-
var writer = new BufferWriter()
|
|
109
|
-
var block = MerkleBlock.fromBuffer(blockbuf)
|
|
110
|
-
block.toBufferWriter(writer).should.equal(writer)
|
|
111
|
-
})
|
|
112
|
-
})
|
|
113
|
-
|
|
114
|
-
describe('#validMerkleTree', function () {
|
|
115
|
-
it('should validate good merkleblocks', function () {
|
|
116
|
-
data.JSON.forEach(function (data) {
|
|
117
|
-
var b = MerkleBlock(data)
|
|
118
|
-
b.validMerkleTree().should.equal(true)
|
|
119
|
-
})
|
|
120
|
-
})
|
|
121
|
-
|
|
122
|
-
it('should not validate merkleblocks with too many hashes', function () {
|
|
123
|
-
var b = MerkleBlock(data.JSON[0])
|
|
124
|
-
// Add too many hashes
|
|
125
|
-
var i = 0
|
|
126
|
-
while (i <= b.numTransactions) {
|
|
127
|
-
b.hashes.push('bad' + i++)
|
|
128
|
-
}
|
|
129
|
-
b.validMerkleTree().should.equal(false)
|
|
130
|
-
})
|
|
131
|
-
|
|
132
|
-
it('should not validate merkleblocks with too few bit flags', function () {
|
|
133
|
-
var b = MerkleBlock(JSON.parse(blockJSON))
|
|
134
|
-
b.flags.pop()
|
|
135
|
-
b.validMerkleTree().should.equal(false)
|
|
136
|
-
})
|
|
137
|
-
})
|
|
138
|
-
|
|
139
|
-
describe('#filteredTxsHash', function () {
|
|
140
|
-
it('should validate good merkleblocks', function () {
|
|
141
|
-
var hashOfFilteredTx = '6f64fd5aa9dd01f74c03656d376625cf80328d83d9afebe60cc68b8f0e245bd9'
|
|
142
|
-
var b = MerkleBlock(data.JSON[3])
|
|
143
|
-
b.filteredTxsHash()[0].should.equal(hashOfFilteredTx)
|
|
144
|
-
})
|
|
145
|
-
|
|
146
|
-
it('should fail with merkleblocks with too many hashes', function () {
|
|
147
|
-
var b = MerkleBlock(data.JSON[0])
|
|
148
|
-
// Add too many hashes
|
|
149
|
-
var i = 0
|
|
150
|
-
while (i <= b.numTransactions) {
|
|
151
|
-
b.hashes.push('bad' + i++)
|
|
152
|
-
}
|
|
153
|
-
(function () {
|
|
154
|
-
b.filteredTxsHash()
|
|
155
|
-
}).should.throw('This MerkleBlock contain an invalid Merkle Tree')
|
|
156
|
-
})
|
|
157
|
-
|
|
158
|
-
it('should fail with merkleblocks with too few bit flags', function () {
|
|
159
|
-
var b = MerkleBlock(JSON.parse(blockJSON))
|
|
160
|
-
b.flags.pop();
|
|
161
|
-
(function () {
|
|
162
|
-
b.filteredTxsHash()
|
|
163
|
-
}).should.throw('This MerkleBlock contain an invalid Merkle Tree')
|
|
164
|
-
})
|
|
165
|
-
})
|
|
166
|
-
|
|
167
|
-
describe('#hasTransaction', function () {
|
|
168
|
-
it('should find transactions via hash string', function () {
|
|
169
|
-
var jsonData = data.JSON[0]
|
|
170
|
-
var txId = Buffer.from(jsonData.hashes[1], 'hex').toString('hex')
|
|
171
|
-
var b = MerkleBlock(jsonData)
|
|
172
|
-
b.hasTransaction(txId).should.equal(true)
|
|
173
|
-
b.hasTransaction(txId + 'abcd').should.equal(false)
|
|
174
|
-
})
|
|
175
|
-
|
|
176
|
-
it('should find transactions via Transaction object', function () {
|
|
177
|
-
var jsonData = data.JSON[0]
|
|
178
|
-
var txBuf = Buffer.from(data.TXHEX[0][0], 'hex')
|
|
179
|
-
var tx = new Transaction().fromBuffer(txBuf)
|
|
180
|
-
var b = MerkleBlock(jsonData)
|
|
181
|
-
b.hasTransaction(tx).should.equal(true)
|
|
182
|
-
})
|
|
183
|
-
|
|
184
|
-
it('should not find non-existant Transaction object', function () {
|
|
185
|
-
// Reuse another transaction already in data/ dir
|
|
186
|
-
var serialized = transactionVector[0][7]
|
|
187
|
-
var tx = new Transaction().fromBuffer(Buffer.from(serialized, 'hex'))
|
|
188
|
-
var b = MerkleBlock(data.JSON[0])
|
|
189
|
-
b.hasTransaction(tx).should.equal(false)
|
|
190
|
-
})
|
|
191
|
-
|
|
192
|
-
it('should not match with merkle nodes', function () {
|
|
193
|
-
var b = MerkleBlock(data.JSON[0])
|
|
194
|
-
|
|
195
|
-
var hashData = [
|
|
196
|
-
['3612262624047ee87660be1a707519a443b1c1ce3d248cbfc6c15870f6c5daa2', false],
|
|
197
|
-
['019f5b01d4195ecbc9398fbf3c3b1fa9bb3183301d7a1fb3bd174fcfa40a2b65', true],
|
|
198
|
-
['41ed70551dd7e841883ab8f0b16bf04176b7d1480e4f0af9f3d4c3595768d068', false],
|
|
199
|
-
['20d2a7bc994987302e5b1ac80fc425fe25f8b63169ea78e68fbaaefa59379bbf', false]
|
|
200
|
-
]
|
|
201
|
-
|
|
202
|
-
hashData.forEach(function check (d) {
|
|
203
|
-
b.hasTransaction(d[0]).should.equal(d[1])
|
|
204
|
-
})
|
|
205
|
-
})
|
|
206
|
-
})
|
|
207
|
-
})
|
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
/* global describe, it, before */
|
|
4
|
-
|
|
5
|
-
// The browser `crypto` polyfill is our own three-function shim, not crypto-browserify.
|
|
6
|
-
//
|
|
7
|
-
// Two things have to hold, and neither is visible to a test that stays inside lib/.
|
|
8
|
-
//
|
|
9
|
-
// 1. `elliptic` must not re-enter the bundles. It carries an advisory with no patched
|
|
10
|
-
// version, and it is reachable from crypto-browserify via browserify-sign and
|
|
11
|
-
// create-ecdh. build/esbuild.js used to keep it out with a hand-written onResolve
|
|
12
|
-
// stub; removing crypto-browserify removed the need for the stub, and this file
|
|
13
|
-
// replaces it. A stub is silent when it stops matching — an assertion is not.
|
|
14
|
-
//
|
|
15
|
-
// 2. The shim must keep implementing what the bundles actually call. If someone adds a
|
|
16
|
-
// `crypto.pbkdf2` call to a bundled module, resolution still succeeds and the member
|
|
17
|
-
// is simply `undefined`, so the failure surfaces far from its cause.
|
|
18
|
-
//
|
|
19
|
-
// The known-answer test exists because "the bundle builds" and "the bundle hashes
|
|
20
|
-
// correctly" are different claims, and only the second one matters to a caller.
|
|
21
|
-
|
|
22
|
-
require('chai').should()
|
|
23
|
-
var crypto = require('crypto')
|
|
24
|
-
var esbuild = require('../../build/esbuild.js')
|
|
25
|
-
var shim = require('../../build/esbuild/crypto-shim.js')
|
|
26
|
-
|
|
27
|
-
// Reachable from crypto-browserify; `elliptic` is the one with no fix available.
|
|
28
|
-
var BANNED = ['elliptic', 'browserify-sign', 'create-ecdh', 'crypto-browserify']
|
|
29
|
-
|
|
30
|
-
describe('browser crypto shim', function () {
|
|
31
|
-
this.timeout(300000)
|
|
32
|
-
|
|
33
|
-
var pkgsByBundle = {}
|
|
34
|
-
|
|
35
|
-
before(function () {
|
|
36
|
-
return Promise.all(esbuild.BUNDLES.map(function (cfg) {
|
|
37
|
-
return esbuild.buildOne(cfg, { metafile: true, write: false, logLevel: 'silent' })
|
|
38
|
-
.then(function (result) {
|
|
39
|
-
pkgsByBundle[cfg.file] = Object.keys(result.metafile.inputs)
|
|
40
|
-
.map(function (p) {
|
|
41
|
-
var m = /node_modules\/((?:@[^/]+\/)?[^/]+)\//.exec(p)
|
|
42
|
-
return m && m[1]
|
|
43
|
-
})
|
|
44
|
-
.filter(Boolean)
|
|
45
|
-
})
|
|
46
|
-
}))
|
|
47
|
-
})
|
|
48
|
-
|
|
49
|
-
it('built every bundle', function () {
|
|
50
|
-
esbuild.BUNDLES.length.should.be.above(0)
|
|
51
|
-
Object.keys(pkgsByBundle).length.should.equal(esbuild.BUNDLES.length)
|
|
52
|
-
})
|
|
53
|
-
|
|
54
|
-
// Guard against the graph being empty for an unrelated reason, which would make the
|
|
55
|
-
// "no banned packages" assertion below pass without checking anything.
|
|
56
|
-
it('resolved a non-trivial dependency graph', function () {
|
|
57
|
-
Object.keys(pkgsByBundle).forEach(function (file) {
|
|
58
|
-
pkgsByBundle[file].length.should.be.above(0, file + ' has no node_modules inputs')
|
|
59
|
-
})
|
|
60
|
-
})
|
|
61
|
-
|
|
62
|
-
it('pulls no part of the elliptic chain into any bundle', function () {
|
|
63
|
-
var offenders = []
|
|
64
|
-
Object.keys(pkgsByBundle).forEach(function (file) {
|
|
65
|
-
BANNED.forEach(function (pkg) {
|
|
66
|
-
if (pkgsByBundle[file].indexOf(pkg) !== -1) offenders.push(file + ' → ' + pkg)
|
|
67
|
-
})
|
|
68
|
-
})
|
|
69
|
-
offenders.should.deep.equal([])
|
|
70
|
-
})
|
|
71
|
-
|
|
72
|
-
it('exports exactly the members the bundles call', function () {
|
|
73
|
-
Object.keys(shim).sort().should.deep.equal(['createHash', 'createHmac', 'randomBytes'])
|
|
74
|
-
})
|
|
75
|
-
|
|
76
|
-
// Deliberately absent: secrets.js-grempe prefers getRandomValues over randomBytes when
|
|
77
|
-
// it is present, so exporting it would silently move the Shamir CSPRNG to another code
|
|
78
|
-
// path. crypto-browserify did not expose it either.
|
|
79
|
-
it('does not expose getRandomValues', function () {
|
|
80
|
-
;(shim.getRandomValues === undefined).should.equal(true)
|
|
81
|
-
})
|
|
82
|
-
|
|
83
|
-
it('hashes identically to node crypto', function () {
|
|
84
|
-
['', 'abc', 'The quick brown fox jumps over the lazy dog'].forEach(function (input) {
|
|
85
|
-
;['sha1', 'sha256', 'sha512', 'ripemd160'].forEach(function (alg) {
|
|
86
|
-
shim.createHash(alg).update(input).digest('hex')
|
|
87
|
-
.should.equal(crypto.createHash(alg).update(input).digest('hex'), alg + ' of ' + JSON.stringify(input))
|
|
88
|
-
})
|
|
89
|
-
})
|
|
90
|
-
})
|
|
91
|
-
|
|
92
|
-
it('hmacs identically to node crypto', function () {
|
|
93
|
-
shim.createHmac('sha512', Buffer.from('key')).update('message').digest('hex')
|
|
94
|
-
.should.equal(crypto.createHmac('sha512', Buffer.from('key')).update('message').digest('hex'))
|
|
95
|
-
})
|
|
96
|
-
|
|
97
|
-
it('draws distinct random bytes of the requested length', function () {
|
|
98
|
-
var a = shim.randomBytes(32)
|
|
99
|
-
var b = shim.randomBytes(32)
|
|
100
|
-
a.length.should.equal(32)
|
|
101
|
-
b.length.should.equal(32)
|
|
102
|
-
a.toString('hex').should.not.equal(b.toString('hex'))
|
|
103
|
-
})
|
|
104
|
-
})
|