bsv2 2.0.24 → 2.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (170) hide show
  1. package/dist/bsv2.mjs +36061 -4
  2. package/package.json +35 -39
  3. package/.babelrc.test +0 -10
  4. package/.editorconfig +0 -4
  5. package/.env.defaults +0 -1
  6. package/.env.schema +0 -1
  7. package/.husky/pre-commit +0 -1
  8. package/.mocharc.yml +0 -4
  9. package/.nvmrc +0 -1
  10. package/.travis.yml +0 -7
  11. package/benchmark.js +0 -50
  12. package/bin/bsv.js +0 -11
  13. package/bin/network.js +0 -92
  14. package/build/index.html +0 -20
  15. package/dist/bsv2.bundle.js +0 -8
  16. package/dist/bsv2.cjs.js +0 -2
  17. package/dist/bsv2.cjs.js.LICENSE.txt +0 -1
  18. package/dist/bsv2.module.js +0 -2
  19. package/entry.js +0 -82
  20. package/index.js +0 -102
  21. package/karma.conf.js +0 -78
  22. package/lib/ach.js +0 -59
  23. package/lib/address.js +0 -264
  24. package/lib/aes.js +0 -57
  25. package/lib/aescbc.js +0 -38
  26. package/lib/base-58-check.js +0 -83
  27. package/lib/base-58.js +0 -59
  28. package/lib/bip-32.js +0 -472
  29. package/lib/bip-39-en-wordlist.js +0 -2054
  30. package/lib/bip-39-en.js +0 -10
  31. package/lib/bip-39-es-wordlist.js +0 -2055
  32. package/lib/bip-39-es.js +0 -10
  33. package/lib/bip-39-jp-wordlist.js +0 -2053
  34. package/lib/bip-39-jp.js +0 -10
  35. package/lib/bip-39-words.js +0 -5
  36. package/lib/bip-39.js +0 -285
  37. package/lib/block-header.js +0 -79
  38. package/lib/block.js +0 -132
  39. package/lib/bn.js +0 -467
  40. package/lib/br.js +0 -172
  41. package/lib/bsm.js +0 -127
  42. package/lib/bw.js +0 -188
  43. package/lib/cbc.js +0 -140
  44. package/lib/cmp.js +0 -37
  45. package/lib/config.js +0 -50
  46. package/lib/constants.js +0 -150
  47. package/lib/ecdsa.js +0 -495
  48. package/lib/ecies.js +0 -151
  49. package/lib/hash-cache.js +0 -41
  50. package/lib/hash.js +0 -191
  51. package/lib/interp.js +0 -2013
  52. package/lib/inv.js +0 -49
  53. package/lib/key-pair.js +0 -133
  54. package/lib/mainnet-address.js +0 -5
  55. package/lib/mainnet-bip-32.js +0 -5
  56. package/lib/mainnet-key-pair.js +0 -5
  57. package/lib/mainnet-priv-key.js +0 -5
  58. package/lib/merkle.js +0 -92
  59. package/lib/msg.js +0 -184
  60. package/lib/op-code.js +0 -398
  61. package/lib/point.js +0 -134
  62. package/lib/priv-key.js +0 -157
  63. package/lib/pub-key.js +0 -230
  64. package/lib/random.js +0 -28
  65. package/lib/script.js +0 -826
  66. package/lib/sig-operations.js +0 -120
  67. package/lib/sig.js +0 -399
  68. package/lib/struct.js +0 -389
  69. package/lib/testnet-address.js +0 -5
  70. package/lib/testnet-bip-32.js +0 -5
  71. package/lib/testnet-key-pair.js +0 -5
  72. package/lib/testnet-priv-key.js +0 -5
  73. package/lib/tx-builder.js +0 -593
  74. package/lib/tx-in.js +0 -172
  75. package/lib/tx-out-map.js +0 -57
  76. package/lib/tx-out.js +0 -73
  77. package/lib/tx-verifier.js +0 -209
  78. package/lib/tx.js +0 -430
  79. package/lib/var-int.js +0 -73
  80. package/lib/version.js +0 -92
  81. package/lib/workers-result.js +0 -65
  82. package/lib/workers.js +0 -77
  83. package/load-babel.js +0 -3
  84. package/mtest/test.html +0 -15
  85. package/mtest/test.js +0 -2
  86. package/mtest/test.mjs +0 -2
  87. package/test/ach.js +0 -172
  88. package/test/address.js +0 -464
  89. package/test/aes.js +0 -96
  90. package/test/aescbc.js +0 -65
  91. package/test/base-58-check.js +0 -128
  92. package/test/base-58.js +0 -113
  93. package/test/bip-32.js +0 -721
  94. package/test/bip-39.js +0 -282
  95. package/test/block-header.js +0 -152
  96. package/test/block.js +0 -243
  97. package/test/bn.js +0 -643
  98. package/test/br.js +0 -353
  99. package/test/bsm.js +0 -141
  100. package/test/bw.js +0 -292
  101. package/test/cbc.js +0 -335
  102. package/test/cmp.js +0 -56
  103. package/test/data/bip69.json +0 -215
  104. package/test/data/bitcoind/base58_keys_invalid.json +0 -152
  105. package/test/data/bitcoind/base58_keys_valid.json +0 -452
  106. package/test/data/bitcoind/blocks.json +0 -27
  107. package/test/data/bitcoind/script_tests.json +0 -2244
  108. package/test/data/bitcoind/sig_canonical.json +0 -7
  109. package/test/data/bitcoind/sig_noncanonical.json +0 -22
  110. package/test/data/bitcoind/tx_invalid.json +0 -177
  111. package/test/data/bitcoind/tx_valid.json +0 -224
  112. package/test/data/blk86756-testnet.dat +0 -0
  113. package/test/data/blk86756-testnet.js +0 -12
  114. package/test/data/blk86756-testnet.json +0 -684
  115. package/test/data/ecdsa.json +0 -230
  116. package/test/data/messages.json +0 -22
  117. package/test/data/sighash.json +0 -1004
  118. package/test/data/tx_creation.json +0 -85
  119. package/test/ecdsa.js +0 -748
  120. package/test/ecies.js +0 -238
  121. package/test/global.js +0 -13
  122. package/test/hash-cache.js +0 -52
  123. package/test/hash.js +0 -354
  124. package/test/index.js +0 -10
  125. package/test/interp.js +0 -512
  126. package/test/inv.js +0 -65
  127. package/test/key-pair.js +0 -243
  128. package/test/merkle.js +0 -179
  129. package/test/msg.js +0 -295
  130. package/test/op-code.js +0 -79
  131. package/test/point.js +0 -375
  132. package/test/priv-key.js +0 -264
  133. package/test/pub-key.js +0 -549
  134. package/test/random.js +0 -31
  135. package/test/script.js +0 -1340
  136. package/test/sig-operations.js +0 -149
  137. package/test/sig.js +0 -662
  138. package/test/struct.js +0 -552
  139. package/test/tx-builder.js +0 -2104
  140. package/test/tx-in.js +0 -198
  141. package/test/tx-out-map.js +0 -104
  142. package/test/tx-out.js +0 -162
  143. package/test/tx-verifier.js +0 -246
  144. package/test/tx.js +0 -539
  145. package/test/var-int.js +0 -132
  146. package/test/vectors/aes.json +0 -10392
  147. package/test/vectors/aescbc.json +0 -620
  148. package/test/vectors/bip39.json +0 -502
  149. package/test/vectors/bip69.json +0 -215
  150. package/test/vectors/bitcoin-abc/script_tests.json +0 -48
  151. package/test/vectors/bitcoin-abc/sighash.json +0 -7004
  152. package/test/vectors/bitcoin-sv/script_tests.json +0 -248
  153. package/test/vectors/bitcoind/script_invalid.json +0 -1151
  154. package/test/vectors/bitcoind/script_valid.json +0 -1462
  155. package/test/vectors/bitcoind/sighash.json +0 -3503
  156. package/test/vectors/bitcoind/tx_invalid.json +0 -883
  157. package/test/vectors/bitcoind/tx_valid.json +0 -1150
  158. package/test/vectors/coolest-tx-ever-sent.json +0 -8
  159. package/test/vectors/ecdsa.json +0 -300
  160. package/test/vectors/hash.json +0 -6133
  161. package/test/vectors/kdf.json +0 -47
  162. package/test/vectors/largesttx.json +0 -5
  163. package/test/vectors/largesttxblock.json +0 -6
  164. package/test/vectors/sig.json +0 -79
  165. package/test/vectors/sighash-single-bug.json +0 -8
  166. package/test/version.js +0 -81
  167. package/test/workers-result.js +0 -89
  168. package/test/workers.js +0 -91
  169. package/test-index.js +0 -3
  170. package/webpack.config.js +0 -115
package/test/ecies.js DELETED
@@ -1,238 +0,0 @@
1
- /* global describe,it */
2
- 'use strict'
3
- import { Ecies } from '../lib/ecies'
4
- import should from 'should'
5
- import { KeyPair } from '../lib/key-pair'
6
- import { PrivKey } from '../lib/priv-key'
7
- import { Hash } from '../lib/hash'
8
-
9
- describe('#Ecies', function () {
10
- it('should make a new Ecies object', function () {
11
- should.exist(Ecies)
12
- })
13
-
14
- const fromkey = new KeyPair().fromRandom()
15
- const tokey = new KeyPair().fromRandom()
16
- const messageBuf = Hash.sha256(
17
- Buffer.from('my message is the hash of this string')
18
- )
19
-
20
- describe('@bitcoreEncrypt', function () {
21
- it('should return a buffer', function () {
22
- const encBuf = Ecies.bitcoreEncrypt(messageBuf, tokey.pubKey, fromkey)
23
- Buffer.isBuffer(encBuf).should.equal(true)
24
- })
25
-
26
- it('should return a buffer if fromkey is not present', function () {
27
- const encBuf = Ecies.bitcoreEncrypt(messageBuf, tokey.pubKey)
28
- Buffer.isBuffer(encBuf).should.equal(true)
29
- })
30
- })
31
-
32
- describe('@asyncBitcoreEncrypt', function () {
33
- it('should return a buffer', async function () {
34
- const encBuf = await Ecies.asyncBitcoreEncrypt(messageBuf, tokey.pubKey, fromkey)
35
- Buffer.isBuffer(encBuf).should.equal(true)
36
- })
37
-
38
- it('should return a buffer if fromkey is not present', async function () {
39
- const encBuf = await Ecies.asyncBitcoreEncrypt(messageBuf, tokey.pubKey)
40
- Buffer.isBuffer(encBuf).should.equal(true)
41
- })
42
- })
43
-
44
- describe('@bitcoreDecrypt', function () {
45
- it('should decrypt that which was encrypted', function () {
46
- const encBuf = Ecies.bitcoreEncrypt(messageBuf, tokey.pubKey, fromkey)
47
- const messageBuf2 = Ecies.bitcoreDecrypt(encBuf, tokey.privKey)
48
- messageBuf2.toString('hex').should.equal(messageBuf.toString('hex'))
49
- })
50
-
51
- it('should decrypt that which was encrypted if fromKeyPair was randomly generated', function () {
52
- const encBuf = Ecies.bitcoreEncrypt(messageBuf, tokey.pubKey)
53
- const messageBuf2 = Ecies.bitcoreDecrypt(encBuf, tokey.privKey)
54
- messageBuf2.toString('hex').should.equal(messageBuf.toString('hex'))
55
- })
56
- })
57
-
58
- describe('@asyncDecrypt', function () {
59
- it('should decrypt that which was encrypted', async function () {
60
- const encBuf = await Ecies.asyncBitcoreEncrypt(messageBuf, tokey.pubKey, fromkey)
61
- const messageBuf2 = await Ecies.asyncBitcoreDecrypt(encBuf, tokey.privKey)
62
- messageBuf2.toString('hex').should.equal(messageBuf.toString('hex'))
63
- })
64
-
65
- it('should decrypt that which was encrypted if fromKeyPair was randomly generated', async function () {
66
- const encBuf = await Ecies.asyncBitcoreEncrypt(messageBuf, tokey.pubKey)
67
- const messageBuf2 = await Ecies.asyncBitcoreDecrypt(encBuf, tokey.privKey)
68
- messageBuf2.toString('hex').should.equal(messageBuf.toString('hex'))
69
- })
70
- })
71
-
72
- describe('Electrum Ecies', function () {
73
- const alicePrivKey = PrivKey.fromString('L1Ejc5dAigm5XrM3mNptMEsNnHzS7s51YxU7J61ewGshZTKkbmzJ')
74
- const aliceKeyPair = KeyPair.fromPrivKey(alicePrivKey)
75
- const bobPrivKey = PrivKey.fromString('KxfxrUXSMjJQcb3JgnaaA6MqsrKQ1nBSxvhuigdKRyFiEm6BZDgG')
76
- const bobKeyPair = KeyPair.fromPrivKey(bobPrivKey)
77
-
78
- it('should do these test vectors correctly', function () {
79
- const message = Buffer.from('this is my test message')
80
-
81
- Ecies.electrumDecrypt(Buffer.from('QklFMQOGFyMXLo9Qv047K3BYJhmnJgt58EC8skYP/R2QU/U0yXXHOt6L3tKmrXho6yj6phfoiMkBOhUldRPnEI4fSZXbiaH4FsxKIOOvzolIFVAS0FplUmib2HnlAM1yP/iiPsU=', 'base64'), alicePrivKey).toString().should.equal(message.toString())
82
- Ecies.electrumDecrypt(Buffer.from('QklFMQM55QTWSSsILaluEejwOXlrBs1IVcEB4kkqbxDz4Fap53XHOt6L3tKmrXho6yj6phfoiMkBOhUldRPnEI4fSZXbvZJHgyAzxA6SoujduvJXv+A9ri3po9veilrmc8p6dwo=', 'base64'), bobPrivKey).toString().should.equal(message.toString())
83
-
84
- Ecies.electrumEncrypt(message, bobKeyPair.pubKey, aliceKeyPair).toString('base64').should.equal('QklFMQM55QTWSSsILaluEejwOXlrBs1IVcEB4kkqbxDz4Fap53XHOt6L3tKmrXho6yj6phfoiMkBOhUldRPnEI4fSZXbvZJHgyAzxA6SoujduvJXv+A9ri3po9veilrmc8p6dwo=')
85
- Ecies.electrumEncrypt(message, aliceKeyPair.pubKey, bobKeyPair).toString('base64').should.equal('QklFMQOGFyMXLo9Qv047K3BYJhmnJgt58EC8skYP/R2QU/U0yXXHOt6L3tKmrXho6yj6phfoiMkBOhUldRPnEI4fSZXbiaH4FsxKIOOvzolIFVAS0FplUmib2HnlAM1yP/iiPsU=')
86
- })
87
-
88
- it('should encrypt and decrypt symmetrically with matching strings in ECDH noKey mode', function () {
89
- const message = Buffer.from('this is my ECDH test message')
90
- const ecdhMessageEncryptedBob = Ecies.electrumEncrypt(message, bobKeyPair.pubKey, aliceKeyPair, true)
91
- const ecdhMessageEncryptedAlice = Ecies.electrumEncrypt(message, aliceKeyPair.pubKey, bobKeyPair, true)
92
- ecdhMessageEncryptedBob.toString("base64").should.equal(ecdhMessageEncryptedAlice.toString("base64"))
93
- Ecies.electrumDecrypt(ecdhMessageEncryptedAlice, bobPrivKey, aliceKeyPair.pubKey).toString().should.equal('this is my ECDH test message')
94
- Ecies.electrumDecrypt(ecdhMessageEncryptedBob, alicePrivKey, bobKeyPair.pubKey).toString().should.equal('this is my ECDH test message')
95
- })
96
- /*
97
- const message = 'attack at dawn'
98
- const encrypted = 'QklFMQM55QTWSSsILaluEejwOXlrBs1IVcEB4kkqbxDz4Fap56+ajq0hzmnaQJXwUMZ/DUNgEx9i2TIhCA1mpBFIfxWZy+sH6H+sqqfX3sPHsGu0ug=='
99
- const encBuf = Buffer.from(encrypted, 'base64')
100
-
101
- it('correctly encrypts a message', function () {
102
- const ciphertext = alice.encrypt(message)
103
- Buffer.isBuffer(ciphertext).should.equal(true)
104
- ciphertext.toString('base64').should.equal(encrypted)
105
- })
106
-
107
- it('correctly decrypts a message', function () {
108
- const decrypted = bob
109
- .decrypt(encBuf)
110
- .toString()
111
- decrypted.should.equal(message)
112
- })
113
-
114
- it('correctly recovers a message', function () {
115
- const decrypted = aliceReloaded
116
- .decrypt(encBuf)
117
- .toString()
118
- decrypted.should.equal(message)
119
- })
120
-
121
- it('retrieves senders PubKey from the encypted buffer', function () {
122
- const bob2 = Ecies().PrivKey(bobPrivKey)
123
- const decrypted = bob2.decrypt(encBuf).toString()
124
- bob2._PubKey.toDER().should.deep.equal(alicePrivKey.PubKey.toDER())
125
- decrypted.should.equal(message)
126
- })
127
-
128
- const message1 = 'This is message from first sender'
129
- const message2 = 'This is message from second sender'
130
-
131
- it('decrypt messages from different senders', function () {
132
- const sender1 = Ecies().PubKey(bobPrivKey.PubKey)
133
- const sender2 = Ecies().PubKey(bobPrivKey.PubKey)
134
- const bob2 = Ecies().PrivKey(bobPrivKey)
135
- const decrypted1 = bob2.decrypt(sender1.encrypt(message1)).toString()
136
- const decrypted2 = bob2.decrypt(sender2.encrypt(message2)).toString()
137
- decrypted1.should.equal(message1)
138
- decrypted2.should.equal(message2)
139
- })
140
-
141
- it('roundtrips', function () {
142
- const secret = 'some secret message!!!'
143
- const encrypted = alice.encrypt(secret)
144
- const decrypted = bob
145
- .decrypt(encrypted)
146
- .toString()
147
- decrypted.should.equal(secret)
148
- })
149
-
150
- it('roundtrips (no public key)', function () {
151
- alice.opts.noKey = true
152
- bob.opts.noKey = true
153
- const secret = 'some secret message!!!'
154
- const encrypted = alice.encrypt(secret)
155
- const decrypted = bob
156
- .decrypt(encrypted)
157
- .toString()
158
- decrypted.should.equal(secret)
159
- })
160
-
161
- it('roundtrips (short tag)', function () {
162
- alice.opts.shortTag = true
163
- bob.opts.shortTag = true
164
- const secret = 'some secret message!!!'
165
- const encrypted = alice.encrypt(secret)
166
- const decrypted = bob
167
- .decrypt(encrypted)
168
- .toString()
169
- decrypted.should.equal(secret)
170
- })
171
-
172
- it('roundtrips (no public key & short tag)', function () {
173
- alice.opts.noKey = true
174
- alice.opts.shortTag = true
175
- bob.opts.noKey = true
176
- bob.opts.shortTag = true
177
- const secret = 'some secret message!!!'
178
- const encrypted = alice.encrypt(secret)
179
- const decrypted = bob
180
- .decrypt(encrypted)
181
- .toString()
182
- decrypted.should.equal(secret)
183
- })
184
-
185
- it('errors', function () {
186
- should.exist(bsv.errors.Ecies)
187
- })
188
-
189
- it('correctly fails if trying to decrypt a bad message', function () {
190
- const encrypted = Buffer.from(encBuf)
191
- encrypted[encrypted.length - 1] = 2
192
- ;(function () {
193
- return bob.decrypt(encrypted)
194
- }).should.throw('Invalid checksum')
195
- })
196
-
197
- it('decrypting uncompressed keys', function () {
198
- const secret = 'test'
199
-
200
- // test uncompressed
201
- const alicePrivKey = bsv.PrivKey.fromObject({
202
- bn: '1fa76f9c799ca3a51e2c7c901d3ba8e24f6d870beccf8df56faf30120b38f360',
203
- compressed: false,
204
- network: 'livenet'
205
- })
206
- const alicePubKey = bsv.PubKey.fromPrivKey(alicePrivKey) // alicePrivKey.PubKey
207
- alicePrivKey.compressed.should.equal(false)
208
-
209
- const cypher1 = Ecies().PrivKey(alicePrivKey).PubKey(alicePubKey)
210
- const encrypted = cypher1.encrypt(secret)
211
-
212
- const cypher2 = Ecies().PrivKey(alicePrivKey).PubKey(alicePubKey)
213
- const decrypted = cypher2.decrypt(encrypted)
214
- secret.should.equal(decrypted.toString())
215
- })
216
-
217
- it('decrypting compressed keys', function () {
218
- const secret = 'test'
219
-
220
- // test compressed
221
- const alicePrivKey = bsv.PrivKey.fromObject({
222
- bn: '1fa76f9c799ca3a51e2c7c901d3ba8e24f6d870beccf8df56faf30120b38f360',
223
- compressed: true,
224
- network: 'livenet'
225
- })
226
- const alicePubKey = bsv.PubKey.fromPrivKey(alicePrivKey) // alicePrivKey.PubKey
227
- alicePrivKey.compressed.should.equal(true)
228
-
229
- const cypher1 = Ecies().PrivKey(alicePrivKey).PubKey(alicePubKey)
230
- const encrypted = cypher1.encrypt(secret)
231
-
232
- const cypher2 = Ecies().PrivKey(alicePrivKey).PubKey(alicePubKey)
233
- const decrypted = cypher2.decrypt(encrypted)
234
- secret.should.equal(decrypted.toString())
235
- })
236
- */
237
- })
238
- })
package/test/global.js DELETED
@@ -1,13 +0,0 @@
1
- /* global after */
2
- /**
3
- * Global "before" and "after" to run before and after all tests. These can be
4
- * used to establish and also tear down global database connections, network
5
- * connections, and worker connections. It's important not to leave thins
6
- * hanging when the tests are done running so that the tests end properly.
7
- */
8
- 'use strict'
9
- import { Workers } from '../lib/workers'
10
-
11
- after(function () {
12
- Workers.endGlobalWorkers()
13
- })
@@ -1,52 +0,0 @@
1
- /* global describe,it */
2
- 'use strict'
3
- import { HashCache } from '../lib/hash-cache'
4
- import should from 'should'
5
-
6
- describe('HashCache', function () {
7
- const prevoutsHashBuf = Buffer.from('01'.repeat(32), 'hex')
8
- const sequenceHashBuf = Buffer.from('02'.repeat(32), 'hex')
9
- const outputsHashBuf = Buffer.from('03'.repeat(32), 'hex')
10
-
11
- it('should satisfy this basic API', function () {
12
- const hashCache = new HashCache(prevoutsHashBuf, sequenceHashBuf, outputsHashBuf)
13
- should.exist(hashCache)
14
- hashCache.prevoutsHashBuf.length.should.equal(32)
15
- hashCache.sequenceHashBuf.length.should.equal(32)
16
- hashCache.outputsHashBuf.length.should.equal(32)
17
- })
18
-
19
- describe('#fromBuffer', function () {
20
- it('should parse this known message', function () {
21
- const hashCache = new HashCache().fromBuffer(new HashCache(prevoutsHashBuf, sequenceHashBuf, outputsHashBuf).toBuffer())
22
- hashCache.toHex().should.equal('7b22707265766f75747348617368427566223a2230313031303130313031303130313031303130313031303130313031303130313031303130313031303130313031303130313031303130313031303130313031222c2273657175656e636548617368427566223a2230323032303230323032303230323032303230323032303230323032303230323032303230323032303230323032303230323032303230323032303230323032222c226f75747075747348617368427566223a2230333033303330333033303330333033303330333033303330333033303330333033303330333033303330333033303330333033303330333033303330333033227d')
23
- })
24
- })
25
-
26
- describe('#toBuffer', function () {
27
- it('should parse this known message', function () {
28
- const hashCache = new HashCache(prevoutsHashBuf, sequenceHashBuf, outputsHashBuf)
29
- hashCache
30
- .toBuffer()
31
- .toString('hex')
32
- .should.equal('7b22707265766f75747348617368427566223a2230313031303130313031303130313031303130313031303130313031303130313031303130313031303130313031303130313031303130313031303130313031222c2273657175656e636548617368427566223a2230323032303230323032303230323032303230323032303230323032303230323032303230323032303230323032303230323032303230323032303230323032222c226f75747075747348617368427566223a2230333033303330333033303330333033303330333033303330333033303330333033303330333033303330333033303330333033303330333033303330333033227d')
33
- })
34
- })
35
-
36
- describe('#fromJSON', function () {
37
- it('should parse this known json hashCache', function () {
38
- new HashCache()
39
- .fromJSON(JSON.parse('{"prevoutsHashBuf":"0101010101010101010101010101010101010101010101010101010101010101","sequenceHashBuf":"0202020202020202020202020202020202020202020202020202020202020202","outputsHashBuf":"0303030303030303030303030303030303030303030303030303030303030303"}'))
40
- .toHex()
41
- .should.equal('7b22707265766f75747348617368427566223a2230313031303130313031303130313031303130313031303130313031303130313031303130313031303130313031303130313031303130313031303130313031222c2273657175656e636548617368427566223a2230323032303230323032303230323032303230323032303230323032303230323032303230323032303230323032303230323032303230323032303230323032222c226f75747075747348617368427566223a2230333033303330333033303330333033303330333033303330333033303330333033303330333033303330333033303330333033303330333033303330333033227d')
42
- })
43
- })
44
-
45
- describe('#toJSON', function () {
46
- it('should create this known message', function () {
47
- JSON.stringify(new HashCache(prevoutsHashBuf, sequenceHashBuf, outputsHashBuf).toJSON()).should.equal(
48
- '{"prevoutsHashBuf":"0101010101010101010101010101010101010101010101010101010101010101","sequenceHashBuf":"0202020202020202020202020202020202020202020202020202020202020202","outputsHashBuf":"0303030303030303030303030303030303030303030303030303030303030303"}'
49
- )
50
- })
51
- })
52
- })
package/test/hash.js DELETED
@@ -1,354 +0,0 @@
1
- /* global describe,it */
2
- 'use strict'
3
- import 'should'
4
- import { Hash } from '../lib/hash'
5
- import vectors from './vectors/hash'
6
-
7
- describe('Hash', function () {
8
- const buf = Buffer.from([0, 1, 2, 3, 253, 254, 255])
9
- const str = 'test string'
10
-
11
- it('should have the blockSize for some hash functions', function () {
12
- Hash.sha1.blockSize.should.equal(512)
13
- Hash.sha256.blockSize.should.equal(512)
14
- Hash.sha512.blockSize.should.equal(1024)
15
- })
16
-
17
- describe('@hmac', function () {
18
- it('should throw errors in these cases', function () {
19
- ;(function () {
20
- Hash.hmac(
21
- 'non-supported-hash-function',
22
- Buffer.from([]),
23
- Buffer.from([])
24
- )
25
- }.should.throw('invalid choice of hash function'))
26
- ;(function () {
27
- Hash.hmac('sha512', Buffer.from([]), '')
28
- }.should.throw('data and key must be buffers'))
29
- ;(function () {
30
- Hash.hmac('sha512', '', Buffer.from([]))
31
- }.should.throw('data and key must be buffers'))
32
- })
33
- })
34
-
35
- describe('@sha1', function () {
36
- it('should calculate the hash of this buffer correctly', function () {
37
- let hash = Hash.sha1(buf)
38
- hash
39
- .toString('hex')
40
- .should.equal('de69b8a4a5604d0486e6420db81e39eb464a17b2')
41
- hash = Hash.sha1(Buffer.alloc(0))
42
- hash
43
- .toString('hex')
44
- .should.equal('da39a3ee5e6b4b0d3255bfef95601890afd80709')
45
- })
46
-
47
- it('should throw an error when the input is not a buffer', function () {
48
- ;(function () {
49
- Hash.sha1(str)
50
- }.should.throw('sha1 hash must be of a buffer'))
51
- })
52
- })
53
-
54
- describe('@asyncSha1', function () {
55
- it('should compute the same as sha1', async function () {
56
- const sha1buf1 = Hash.sha1(buf)
57
- const sha1buf2 = await Hash.asyncSha1(buf)
58
- sha1buf1.toString('hex').should.equal(sha1buf2.toString('hex'))
59
- })
60
-
61
- it('should compute the same as sha1 twice in a row', async function () {
62
- const sha1buf1 = Hash.sha1(buf)
63
- const sha1buf1b = Hash.sha1(buf)
64
- const sha1buf2 = await Hash.asyncSha1(buf)
65
- const sha1buf2b = await Hash.asyncSha1(buf)
66
- sha1buf1.toString('hex').should.equal(sha1buf2.toString('hex'))
67
- sha1buf1b.toString('hex').should.equal(sha1buf2b.toString('hex'))
68
- })
69
- })
70
-
71
- describe('@sha1Hmac', function () {
72
- // http://tools.ietf.org/html/rfc2202.html
73
-
74
- it('should calculate this known empty test vector correctly', function () {
75
- const hex = 'b617318655057264e28bc0b6fb378c8ef146be00'
76
- Hash.sha1Hmac(
77
- Buffer.from('Hi There'),
78
- Buffer.from('0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b', 'hex')
79
- )
80
- .toString('hex')
81
- .should.equal(hex)
82
- })
83
- })
84
-
85
- describe('@asyncSha1Hmac', function () {
86
- it('should compute the same as sha1Hmac', async function () {
87
- const data = Buffer.from('Hi There')
88
- const key = Buffer.from('0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b', 'hex')
89
- const sha1Hmacbuf1 = Hash.sha1Hmac(data, key)
90
- const sha1Hmacbuf2 = await Hash.asyncSha1Hmac(data, key)
91
- sha1Hmacbuf1.toString('hex').should.equal(sha1Hmacbuf2.toString('hex'))
92
- })
93
- })
94
-
95
- describe('@sha256', function () {
96
- it('should calculate the hash of this buffer correctly', function () {
97
- const hash = Hash.sha256(buf)
98
- hash
99
- .toString('hex')
100
- .should.equal(
101
- '6f2c7b22fd1626998287b3636089087961091de80311b9279c4033ec678a83e8'
102
- )
103
- })
104
-
105
- it('should throw an error when the input is not a buffer', function () {
106
- ;(function () {
107
- Hash.sha256(str)
108
- }.should.throw('sha256 hash must be of a buffer'))
109
- })
110
- })
111
-
112
- describe('@asyncSha256', function () {
113
- it('should compute the same as sha256', async function () {
114
- const sha256buf1 = Hash.sha1(buf)
115
- const sha256buf2 = await Hash.asyncSha1(buf)
116
- sha256buf1.toString('hex').should.equal(sha256buf2.toString('hex'))
117
- })
118
-
119
- it('should compute a hash for 5mb data', async function () {
120
- this.timeout(10000)
121
- const data = Buffer.alloc(5e6)
122
- data.fill(0)
123
- const hash1 = Hash.sha256(data)
124
- const hash2 = await Hash.asyncSha256(data)
125
- hash1.toString('hex').should.equal(hash2.toString('hex'))
126
- })
127
- })
128
-
129
- describe('@sha256Hmac', function () {
130
- it('should compute this known empty test vector correctly', function () {
131
- const key = Buffer.from('')
132
- const data = Buffer.from('')
133
- Hash.sha256Hmac(data, key)
134
- .toString('hex')
135
- .should.equal(
136
- 'b613679a0814d9ec772f95d778c35fc5ff1697c493715653c6c712144292c5ad'
137
- )
138
- })
139
-
140
- it('should compute this known non-empty test vector correctly', function () {
141
- const key = Buffer.from('key')
142
- const data = Buffer.from('The quick brown fox jumps over the lazy dog')
143
- Hash.sha256Hmac(data, key)
144
- .toString('hex')
145
- .should.equal(
146
- 'f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8'
147
- )
148
- })
149
- })
150
-
151
- describe('@asyncSha256Hmac', function () {
152
- it('should compute the same as sha256Hmac', async function () {
153
- const data = Buffer.from('Hi There')
154
- const key = Buffer.from('0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b', 'hex')
155
- const sha256Hmacbuf1 = Hash.sha256Hmac(data, key)
156
- const sha256Hmacbuf2 = await Hash.asyncSha256Hmac(data, key)
157
- sha256Hmacbuf1
158
- .toString('hex')
159
- .should.equal(sha256Hmacbuf2.toString('hex'))
160
- })
161
- })
162
-
163
- describe('@sha256Sha256', function () {
164
- it('should calculate the hash of this buffer correctly', function () {
165
- const hash = Hash.sha256Sha256(buf)
166
- hash
167
- .toString('hex')
168
- .should.equal(
169
- 'be586c8b20dee549bdd66018c7a79e2b67bb88b7c7d428fa4c970976d2bec5ba'
170
- )
171
- })
172
-
173
- it('should throw an error when the input is not a buffer', function () {
174
- ;(function () {
175
- Hash.sha256Sha256(str)
176
- }.should.throw(
177
- 'sha256Sha256 hash must be of a buffer: Error: sha256 hash must be of a buffer'
178
- ))
179
- })
180
- })
181
-
182
- describe('@asyncSha256Sha256', function () {
183
- it('should compute the same as sha256Sha256', async function () {
184
- const sha256Sha256buf1 = Hash.sha256Sha256(buf)
185
- const sha256Sha256buf2 = await Hash.asyncSha256Sha256(buf)
186
- sha256Sha256buf1
187
- .toString('hex')
188
- .should.equal(sha256Sha256buf2.toString('hex'))
189
- })
190
- })
191
-
192
- describe('@sha256Ripemd160', function () {
193
- it('should calculate the hash of this buffer correctly', function () {
194
- const hash = Hash.sha256Ripemd160(buf)
195
- hash
196
- .toString('hex')
197
- .should.equal('7322e2bd8535e476c092934e16a6169ca9b707ec')
198
- })
199
-
200
- it('should throw an error when the input is not a buffer', function () {
201
- ;(function () {
202
- Hash.sha256Ripemd160(str)
203
- }.should.throw(
204
- 'sha256Ripemd160 hash must be of a buffer: Error: sha256 hash must be of a buffer'
205
- ))
206
- })
207
- })
208
-
209
- describe('@asyncSha256Ripemd160', function () {
210
- it('should compute the same as sha256Sha256', async function () {
211
- const sha256Ripemd160buf1 = Hash.sha256Ripemd160(buf)
212
- const sha256Ripemd160buf2 = await Hash.asyncSha256Ripemd160(buf)
213
- sha256Ripemd160buf1
214
- .toString('hex')
215
- .should.equal(sha256Ripemd160buf2.toString('hex'))
216
- })
217
- })
218
-
219
- describe('@ripemd160', function () {
220
- it('should calculate the hash of this buffer correctly', function () {
221
- const hash = Hash.ripemd160(buf)
222
- hash
223
- .toString('hex')
224
- .should.equal('fa0f4565ff776fee0034c713cbf48b5ec06b7f5c')
225
- })
226
-
227
- it('should throw an error when the input is not a buffer', function () {
228
- ;(function () {
229
- Hash.ripemd160(str)
230
- }.should.throw('ripemd160 hash must be of a buffer'))
231
- })
232
- })
233
-
234
- describe('@asyncRipemd160', function () {
235
- it('should compute the same as ripemd160', async function () {
236
- const ripemd160buf1 = Hash.ripemd160(buf)
237
- const ripemd160buf2 = await Hash.asyncRipemd160(buf)
238
- ripemd160buf1.toString('hex').should.equal(ripemd160buf2.toString('hex'))
239
- })
240
- })
241
-
242
- describe('@sha512', function () {
243
- it('should calculate the hash of this buffer correctly', function () {
244
- const hash = Hash.sha512(buf)
245
- hash
246
- .toString('hex')
247
- .should.equal(
248
- 'c0530aa32048f4904ae162bc14b9eb535eab6c465e960130005feddb71613e7d62aea75f7d3333ba06e805fc8e45681454524e3f8050969fe5a5f7f2392e31d0'
249
- )
250
- })
251
-
252
- it('should throw an error when the input is not a buffer', function () {
253
- ;(function () {
254
- Hash.sha512(str)
255
- }.should.throw('sha512 hash must be of a buffer'))
256
- })
257
- })
258
-
259
- describe('@asyncSha512', function () {
260
- it('should compute the same as sha512', async function () {
261
- const sha512buf1 = Hash.sha512(buf)
262
- const sha512buf2 = await Hash.asyncSha512(buf)
263
- sha512buf1.toString('hex').should.equal(sha512buf2.toString('hex'))
264
- })
265
- })
266
-
267
- describe('@sha512Hmac', function () {
268
- it('should calculate this value where key size is the same as block size', function () {
269
- const key = Buffer.alloc(Hash.sha512.blockSize / 8)
270
- key.fill(0)
271
- const data = Buffer.from([])
272
- // test vector calculated with node's createHmac
273
- const hex =
274
- 'b936cee86c9f87aa5d3c6f2e84cb5a4239a5fe50480a6ec66b70ab5b1f4ac6730c6c515421b327ec1d69402e53dfb49ad7381eb067b338fd7b0cb22247225d47'
275
- Hash.sha512Hmac(data, key)
276
- .toString('hex')
277
- .should.equal(hex)
278
- })
279
-
280
- it('should calculate this known empty test vector correctly', function () {
281
- const hex =
282
- 'b936cee86c9f87aa5d3c6f2e84cb5a4239a5fe50480a6ec66b70ab5b1f4ac6730c6c515421b327ec1d69402e53dfb49ad7381eb067b338fd7b0cb22247225d47'
283
- Hash.sha512Hmac(Buffer.from([]), Buffer.from([]))
284
- .toString('hex')
285
- .should.equal(hex)
286
- })
287
-
288
- it('should calculate this known non-empty test vector correctly', function () {
289
- const hex =
290
- 'c40bd7c15aa493b309c940e08a73ffbd28b2e4cb729eb94480d727e4df577b13cc403a78e6150d83595f3b17c4cc331f12ca5952691de3735a63c1d4c69a2bac'
291
- const data = Buffer.from('test1')
292
- const key = Buffer.from('test2')
293
- Hash.sha512Hmac(data, key)
294
- .toString('hex')
295
- .should.equal(hex)
296
- })
297
- })
298
-
299
- describe('@asyncSha512Hmac', function () {
300
- it('should compute the same as sha512Hmac', async function () {
301
- const data = Buffer.from('Hi There')
302
- const key = Buffer.from('0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b', 'hex')
303
- const sha512Hmacbuf1 = Hash.sha512Hmac(data, key)
304
- const sha512Hmacbuf2 = await Hash.asyncSha512Hmac(data, key)
305
- sha512Hmacbuf1
306
- .toString('hex')
307
- .should.equal(sha512Hmacbuf2.toString('hex'))
308
- })
309
- })
310
-
311
- describe('vectors', function () {
312
- vectors.sha1.forEach(function (vector, i) {
313
- it('should pass sjcl sha1 test vector ' + i, function () {
314
- const data = Buffer.from(vector[0])
315
- Hash.sha1(data)
316
- .toString('hex')
317
- .should.equal(vector[1])
318
- })
319
- })
320
-
321
- vectors.sha256.forEach(function (vector, i) {
322
- it('should pass sjcl sha256 test vector ' + i, function () {
323
- const data = Buffer.from(vector[0])
324
- Hash.sha256(data)
325
- .toString('hex')
326
- .should.equal(vector[1])
327
- })
328
- })
329
-
330
- vectors.sha512.forEach(function (vector, i) {
331
- it('should pass sjcl sha512 test vector ' + i, function () {
332
- const data = Buffer.from(vector[0])
333
- Hash.sha512(data)
334
- .toString('hex')
335
- .should.equal(vector[1])
336
- })
337
- })
338
-
339
- vectors.hmac.forEach(function (vector, i) {
340
- it('should pass standard hmac test vector ' + i, function () {
341
- const keyBuf = Buffer.from(vector.key, 'hex')
342
- const dataBuf = Buffer.from(vector.data, 'hex')
343
- Hash.sha256Hmac(dataBuf, keyBuf)
344
- .toString('hex')
345
- .substr(0, vector.sha256hmac.length)
346
- .should.equal(vector.sha256hmac)
347
- Hash.sha512Hmac(dataBuf, keyBuf)
348
- .toString('hex')
349
- .substr(0, vector.sha512hmac.length)
350
- .should.equal(vector.sha512hmac)
351
- })
352
- })
353
- })
354
- })
package/test/index.js DELETED
@@ -1,10 +0,0 @@
1
- /* global describe,it */
2
- 'use strict'
3
- import should from 'should'
4
- import * as bsv from '../entry'
5
-
6
- describe('bsv', function () {
7
- it('should pass this sanity check on loading the main package', function () {
8
- should.exist(bsv)
9
- })
10
- })