@smartledger/bsv 5.5.0 → 5.5.2

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 (93) hide show
  1. package/.mocharc.json +5 -0
  2. package/CHANGELOG.md +40 -0
  3. package/README.md +38 -38
  4. package/bsv-covenant.min.js +2 -2
  5. package/bsv-gdaf.min.js +2 -2
  6. package/bsv-ltp.min.js +2 -2
  7. package/bsv-smartcontract.min.js +2 -2
  8. package/bsv.bundle.js +2 -2
  9. package/bsv.min.js +2 -2
  10. package/docs/MODULE_REFERENCE_COMPLETE.md +27 -27
  11. package/docs/advanced/UTXO_MANAGER_GUIDE.md +1 -1
  12. package/docs/getting-started/INSTALLATION.md +23 -23
  13. package/docs/getting-started/QUICK_START.md +7 -7
  14. package/docs/migration/FROM_BSV_1_5_6.md +5 -5
  15. package/lib/smart_contract/covenant_helpers.js +3 -3
  16. package/lib/smart_contract/index.js +4 -5
  17. package/package.json +5 -1
  18. package/test/address.js +629 -0
  19. package/test/block/block.js +239 -0
  20. package/test/block/blockheader.js +270 -0
  21. package/test/block/merkleblock.js +207 -0
  22. package/test/cli/smoke.js +261 -0
  23. package/test/credentials-test.js +329 -0
  24. package/test/crypto/bn.js +168 -0
  25. package/test/crypto/ecdsa.js +328 -0
  26. package/test/crypto/elliptic-fixed.js +61 -0
  27. package/test/crypto/hash.browser.js +121 -0
  28. package/test/crypto/hash.js +122 -0
  29. package/test/crypto/point.js +193 -0
  30. package/test/crypto/random.js +32 -0
  31. package/test/crypto/security.js +127 -0
  32. package/test/crypto/shamir.js +169 -0
  33. package/test/crypto/signature.js +318 -0
  34. package/test/data/bip69.json +215 -0
  35. package/test/data/bitcoind/base58_keys_invalid.json +152 -0
  36. package/test/data/bitcoind/base58_keys_valid.json +452 -0
  37. package/test/data/bitcoind/blocks.json +27 -0
  38. package/test/data/bitcoind/script_tests.json +2244 -0
  39. package/test/data/bitcoind/sig_canonical.json +7 -0
  40. package/test/data/bitcoind/sig_noncanonical.json +22 -0
  41. package/test/data/bitcoind/tx_invalid.json +177 -0
  42. package/test/data/bitcoind/tx_valid.json +224 -0
  43. package/test/data/blk86756-testnet.dat +0 -0
  44. package/test/data/blk86756-testnet.js +12 -0
  45. package/test/data/blk86756-testnet.json +684 -0
  46. package/test/data/ecdsa.json +230 -0
  47. package/test/data/merkleblocks.js +486 -0
  48. package/test/data/messages.json +22 -0
  49. package/test/data/sighash.json +1004 -0
  50. package/test/data/tx_creation.json +85 -0
  51. package/test/ecies/bitcore-ecies.js +170 -0
  52. package/test/ecies/electrum-ecies.js +206 -0
  53. package/test/encoding/base58.js +131 -0
  54. package/test/encoding/base58check.js +136 -0
  55. package/test/encoding/bufferreader.js +328 -0
  56. package/test/encoding/bufferwriter.js +160 -0
  57. package/test/encoding/varint.js +104 -0
  58. package/test/gdaf/canonicalize.js +140 -0
  59. package/test/hdkeys.js +365 -0
  60. package/test/hdprivatekey.js +330 -0
  61. package/test/hdpublickey.js +293 -0
  62. package/test/index.js +16 -0
  63. package/test/ltp/right.js +198 -0
  64. package/test/message/message.js +190 -0
  65. package/test/mnemonic/data/fixtures.json +300 -0
  66. package/test/mnemonic/mnemonic.js +245 -0
  67. package/test/mnemonic/mocha.opts +1 -0
  68. package/test/mnemonic/pbkdf2.test.js +43 -0
  69. package/test/networks.js +162 -0
  70. package/test/opcode.js +161 -0
  71. package/test/privatekey.js +430 -0
  72. package/test/publickey.js +411 -0
  73. package/test/script/genesis_limits.js +80 -0
  74. package/test/script/interpreter.js +723 -0
  75. package/test/script/script.js +1256 -0
  76. package/test/script/string_ops.js +72 -0
  77. package/test/smart_contract/covenants.js +247 -0
  78. package/test/smart_contract/dsl_debugger.js +103 -0
  79. package/test/smart_contract/token_generalized.js +187 -0
  80. package/test/transaction/deserialize.js +33 -0
  81. package/test/transaction/input/input.js +92 -0
  82. package/test/transaction/input/multisig.js +174 -0
  83. package/test/transaction/input/multisigscripthash.js +111 -0
  84. package/test/transaction/input/publickey.js +68 -0
  85. package/test/transaction/input/publickeyhash.js +59 -0
  86. package/test/transaction/output.js +185 -0
  87. package/test/transaction/sighash.js +71 -0
  88. package/test/transaction/signature.js +127 -0
  89. package/test/transaction/transaction.js +1299 -0
  90. package/test/transaction/unspentoutput.js +92 -0
  91. package/test/util/js.js +76 -0
  92. package/test/util/preconditions.js +79 -0
  93. package/test/vcjwt/interop.js +126 -0
@@ -0,0 +1,168 @@
1
+ 'use strict'
2
+
3
+ var should = require('chai').should()
4
+ var bsv = require('../..')
5
+ var BN = bsv.crypto.BN
6
+
7
+ describe('BN', function () {
8
+ it('should create a bn', function () {
9
+ var bn = new BN(50)
10
+ should.exist(bn)
11
+ bn.toString().should.equal('50')
12
+ })
13
+
14
+ it('should parse this number', function () {
15
+ var bn = new BN(999970000)
16
+ bn.toString().should.equal('999970000')
17
+ })
18
+
19
+ it('should parse numbers below and at bn.js internal word size', function () {
20
+ var bn = new BN(Math.pow(2, 26) - 1)
21
+ bn.toString().should.equal((Math.pow(2, 26) - 1).toString())
22
+ bn = new BN(Math.pow(2, 26))
23
+ bn.toString().should.equal((Math.pow(2, 26)).toString())
24
+ })
25
+
26
+ describe('#add', function () {
27
+ it('should add two small numbers together', function () {
28
+ var bn1 = new BN(50)
29
+ var bn2 = new BN(75)
30
+ var bn3 = bn1.add(bn2)
31
+ bn3.toString().should.equal('125')
32
+ })
33
+ })
34
+
35
+ describe('#sub', function () {
36
+ it('should subtract a small number', function () {
37
+ var bn1 = new BN(50)
38
+ var bn2 = new BN(25)
39
+ var bn3 = bn1.sub(bn2)
40
+ bn3.toString().should.equal('25')
41
+ })
42
+ })
43
+
44
+ describe('#gt', function () {
45
+ it('should say 1 is greater than 0', function () {
46
+ var bn1 = new BN(1)
47
+ var bn0 = new BN(0)
48
+ bn1.gt(bn0).should.equal(true)
49
+ })
50
+
51
+ it('should say a big number is greater than a small big number', function () {
52
+ var bn1 = new BN('24023452345398529485723980457')
53
+ var bn0 = new BN('34098234283412341234049357')
54
+ bn1.gt(bn0).should.equal(true)
55
+ })
56
+
57
+ it('should say a big number is great than a standard number', function () {
58
+ var bn1 = new BN('24023452345398529485723980457')
59
+ var bn0 = new BN(5)
60
+ bn1.gt(bn0).should.equal(true)
61
+ })
62
+ })
63
+
64
+ describe('to/from ScriptNumBuffer', function () {
65
+ [0, 1, 10, 256, 1000, 65536, 65537, -1, -1000, -65536, -65537].forEach(function (n) {
66
+ it('rountrips correctly for ' + n, function () {
67
+ BN.fromScriptNumBuffer(new BN(n).toScriptNumBuffer()).toNumber().should.equal(n)
68
+ })
69
+ })
70
+ })
71
+
72
+ describe('#fromString', function () {
73
+ it('should make BN from a string', function () {
74
+ BN.fromString('5').toString().should.equal('5')
75
+ })
76
+ it('should work with hex string', function () {
77
+ BN.fromString('7fffff0000000000000000000000000000000000000000000000000000000000', 16)
78
+ .toString(16).should.equal('7fffff0000000000000000000000000000000000000000000000000000000000')
79
+ })
80
+ })
81
+
82
+ describe('#toString', function () {
83
+ it('should make a string', function () {
84
+ new BN(5).toString().should.equal('5')
85
+ })
86
+ })
87
+
88
+ describe('@fromBuffer', function () {
89
+ it('should work with big endian', function () {
90
+ var bn = BN.fromBuffer(Buffer.from('0001', 'hex'), {
91
+ endian: 'big'
92
+ })
93
+ bn.toString().should.equal('1')
94
+ })
95
+
96
+ it('should work with big endian 256', function () {
97
+ var bn = BN.fromBuffer(Buffer.from('0100', 'hex'), {
98
+ endian: 'big'
99
+ })
100
+ bn.toString().should.equal('256')
101
+ })
102
+
103
+ it('should work with little endian if we specify the size', function () {
104
+ var bn = BN.fromBuffer(Buffer.from('0100', 'hex'), {
105
+ size: 2,
106
+ endian: 'little'
107
+ })
108
+ bn.toString().should.equal('1')
109
+ })
110
+ })
111
+
112
+ describe('#toBuffer', function () {
113
+ it('should create a 4 byte buffer', function () {
114
+ var bn = new BN(1)
115
+ bn.toBuffer({
116
+ size: 4
117
+ }).toString('hex').should.equal('00000001')
118
+ })
119
+
120
+ it('should create a 4 byte buffer in little endian', function () {
121
+ var bn = new BN(1)
122
+ bn.toBuffer({
123
+ size: 4,
124
+ endian: 'little'
125
+ }).toString('hex').should.equal('01000000')
126
+ })
127
+
128
+ it('should create a 2 byte buffer even if you ask for a 1 byte', function () {
129
+ var bn = new BN('ff00', 16)
130
+ bn.toBuffer({
131
+ size: 1
132
+ }).toString('hex').should.equal('ff00')
133
+ })
134
+
135
+ it('should create a 4 byte buffer even if you ask for a 1 byte', function () {
136
+ var bn = new BN('ffffff00', 16)
137
+ bn.toBuffer({
138
+ size: 4
139
+ }).toString('hex').should.equal('ffffff00')
140
+ })
141
+ })
142
+
143
+ describe('@fromHex', function () {
144
+ it('should work with big endian', function () {
145
+ var bn = BN.fromHex('0001', {
146
+ endian: 'big'
147
+ })
148
+ bn.toString().should.equal('1')
149
+ })
150
+
151
+ it('should work with little endian if we specify the size', function () {
152
+ var bn = BN.fromHex('0100', {
153
+ size: 2,
154
+ endian: 'little'
155
+ })
156
+ bn.toString().should.equal('1')
157
+ })
158
+ })
159
+
160
+ describe('#toHex', function () {
161
+ it('should create a 4 byte he string', function () {
162
+ var bn = new BN(1)
163
+ bn.toHex({
164
+ size: 4
165
+ }).should.equal('00000001')
166
+ })
167
+ })
168
+ })
@@ -0,0 +1,328 @@
1
+ 'use strict'
2
+
3
+ var ECDSA = require('../../lib/crypto/ecdsa')
4
+ var Hash = require('../../lib/crypto/hash')
5
+ var Privkey = require('../../lib/privatekey')
6
+ var Pubkey = require('../../lib/publickey')
7
+ var Signature = require('../../lib/crypto/signature')
8
+ var BN = require('../../lib/crypto/bn')
9
+ var point = require('../../lib/crypto/point')
10
+ var should = require('chai').should()
11
+ var vectors = require('../data/ecdsa')
12
+
13
+ describe('ECDSA', function () {
14
+ it('instantiation', function () {
15
+ var ecdsa = new ECDSA()
16
+ should.exist(ecdsa)
17
+ })
18
+
19
+ var ecdsa = new ECDSA()
20
+ ecdsa.hashbuf = Hash.sha256(Buffer.from('test data'))
21
+ ecdsa.privkey = new Privkey(BN.fromBuffer(
22
+ Buffer.from('fee0a1f7afebf9d2a5a80c0c98a31c709681cce195cbcd06342b517970c0be1e', 'hex')
23
+ ))
24
+ ecdsa.privkey2pubkey()
25
+
26
+ describe('#set', function () {
27
+ it('sets hashbuf', function () {
28
+ should.exist(ECDSA().set({
29
+ hashbuf: ecdsa.hashbuf
30
+ }).hashbuf)
31
+ })
32
+ })
33
+
34
+ describe('#calci', function () {
35
+ it('calculates i correctly', function () {
36
+ ecdsa.randomK()
37
+ ecdsa.sign()
38
+ ecdsa.calci()
39
+ should.exist(ecdsa.sig.i)
40
+ })
41
+
42
+ it('calulates this known i', function () {
43
+ var hashbuf = Hash.sha256(Buffer.from('some data'))
44
+ var r = new BN('71706645040721865894779025947914615666559616020894583599959600180037551395766', 10)
45
+ var s = new BN('109412465507152403114191008482955798903072313614214706891149785278625167723646', 10)
46
+ var ecdsa = new ECDSA({
47
+ privkey: new Privkey(BN.fromBuffer(Hash.sha256(Buffer.from('test')))),
48
+ hashbuf: hashbuf,
49
+ sig: new Signature({
50
+ r: r,
51
+ s: s
52
+ })
53
+ })
54
+
55
+ ecdsa.calci()
56
+ ecdsa.sig.i.should.equal(1)
57
+ })
58
+ })
59
+
60
+ describe('#fromString', function () {
61
+ it('round trip with fromString', function () {
62
+ var str = ecdsa.toString()
63
+ var ecdsa2 = ECDSA.fromString(str)
64
+ should.exist(ecdsa2.hashbuf)
65
+ should.exist(ecdsa2.privkey)
66
+ })
67
+ })
68
+
69
+ describe('#randomK', function () {
70
+ it('should generate a new random k when called twice in a row', function () {
71
+ ecdsa.randomK()
72
+ var k1 = ecdsa.k
73
+ ecdsa.randomK()
74
+ var k2 = ecdsa.k;
75
+ (k1.cmp(k2) === 0).should.equal(false)
76
+ })
77
+
78
+ it('should generate a random k that is (almost always) greater than this relatively small number', function () {
79
+ ecdsa.randomK()
80
+ var k1 = ecdsa.k
81
+ var k2 = new BN(Math.pow(2, 32)).mul(new BN(Math.pow(2, 32))).mul(new BN(Math.pow(2, 32)))
82
+ k2.gt(k1).should.equal(false)
83
+ })
84
+ })
85
+
86
+ describe('#deterministicK', function () {
87
+ it('should generate the same deterministic k', function () {
88
+ ecdsa.deterministicK()
89
+ ecdsa.k.toBuffer().toString('hex')
90
+ .should.equal('fcce1de7a9bcd6b2d3defade6afa1913fb9229e3b7ddf4749b55c4848b2a196e')
91
+ })
92
+ it('should generate the same deterministic k if badrs is set', function () {
93
+ ecdsa.deterministicK(0)
94
+ ecdsa.k.toBuffer().toString('hex')
95
+ .should.equal('fcce1de7a9bcd6b2d3defade6afa1913fb9229e3b7ddf4749b55c4848b2a196e')
96
+ ecdsa.deterministicK(1)
97
+ ecdsa.k.toBuffer().toString('hex')
98
+ .should.not.equal('fcce1de7a9bcd6b2d3defade6afa1913fb9229e3b7ddf4749b55c4848b2a196e')
99
+ ecdsa.k.toBuffer().toString('hex')
100
+ .should.equal('727fbcb59eb48b1d7d46f95a04991fc512eb9dbf9105628e3aec87428df28fd8')
101
+ })
102
+ it('should compute this test vector correctly', function () {
103
+ // test fixture from bitcoinjs
104
+ // https://github.com/bitcoinjs/bitcoinjs-lib/blob/10630873ebaa42381c5871e20336fbfb46564ac8/test/fixtures/ecdsa.json#L6
105
+ var ecdsa = new ECDSA()
106
+ ecdsa.hashbuf = Hash.sha256(Buffer.from('Everything should be made as simple as possible, but not simpler.'))
107
+ ecdsa.privkey = new Privkey(new BN(1))
108
+ ecdsa.privkey2pubkey()
109
+ ecdsa.deterministicK()
110
+ ecdsa.k.toBuffer().toString('hex')
111
+ .should.equal('ec633bd56a5774a0940cb97e27a9e4e51dc94af737596a0c5cbb3d30332d92a5')
112
+ ecdsa.sign()
113
+ ecdsa.sig.r.toString()
114
+ .should.equal('23362334225185207751494092901091441011938859014081160902781146257181456271561')
115
+ ecdsa.sig.s.toString()
116
+ .should.equal('50433721247292933944369538617440297985091596895097604618403996029256432099938')
117
+ })
118
+ })
119
+
120
+ describe('#toPublicKey', function () {
121
+ it('should calculate the correct public key', function () {
122
+ ecdsa.k = new BN('114860389168127852803919605627759231199925249596762615988727970217268189974335', 10)
123
+ ecdsa.sign()
124
+ ecdsa.sig.i = 0
125
+ var pubkey = ecdsa.toPublicKey()
126
+ pubkey.point.eq(ecdsa.pubkey.point).should.equal(true)
127
+ })
128
+
129
+ it('should calculate the correct public key for this signature with low s', function () {
130
+ ecdsa.k = new BN('114860389168127852803919605627759231199925249596762615988727970217268189974335', 10)
131
+ ecdsa.sig = Signature.fromString('3045022100ec3cfe0e335791ad278b4ec8eac93d0347' +
132
+ 'a97877bb1d54d35d189e225c15f6650220278cf15b05ce47fb37d2233802899d94c774d5480bba9f0f2d996baa13370c43')
133
+ ecdsa.sig.i = 0
134
+ var pubkey = ecdsa.toPublicKey()
135
+ pubkey.point.eq(ecdsa.pubkey.point).should.equal(true)
136
+ })
137
+
138
+ it('should calculate the correct public key for this signature with high s', function () {
139
+ ecdsa.k = new BN('114860389168127852803919605627759231199925249596762615988727970217268189974335', 10)
140
+ ecdsa.sign()
141
+ ecdsa.sig = Signature.fromString('3046022100ec3cfe0e335791ad278b4ec8eac93d0347' +
142
+ 'a97877bb1d54d35d189e225c15f665022100d8730ea4fa31b804c82ddcc7fd766269f33a079ea38e012c9238f2e2bcff34fe')
143
+ ecdsa.sig.i = 1
144
+ var pubkey = ecdsa.toPublicKey()
145
+ pubkey.point.eq(ecdsa.pubkey.point).should.equal(true)
146
+ })
147
+ })
148
+
149
+ describe('#sigError', function () {
150
+ it('should return an error if the hash is invalid', function () {
151
+ var ecdsa = new ECDSA()
152
+ ecdsa.sigError().should.equal('hashbuf must be a 32 byte buffer')
153
+ })
154
+
155
+ it('should return an error if r, s are invalid', function () {
156
+ var ecdsa = new ECDSA()
157
+ ecdsa.hashbuf = Hash.sha256(Buffer.from('test'))
158
+ var pk = Pubkey.fromDER(Buffer.from('041ff0fe0f7b15ffaa85ff9f4744d539139c252a49' +
159
+ '710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341', 'hex'))
160
+ ecdsa.pubkey = pk
161
+ ecdsa.sig = new Signature()
162
+ ecdsa.sig.r = new BN(0)
163
+ ecdsa.sig.s = new BN(0)
164
+ ecdsa.sigError().should.equal('r and s not in range')
165
+ })
166
+
167
+ it('should return an error if the signature is incorrect', function () {
168
+ ecdsa.sig = Signature.fromString('3046022100e9915e6236695f093a4128ac2a956c40' +
169
+ 'ed971531de2f4f41ba05fac7e2bd019c02210094e6a4a769cc7f2a8ab3db696c7cd8d56bcdbfff860a8c81de4bc6a798b90827')
170
+ ecdsa.sig.r = ecdsa.sig.r.add(new BN(1))
171
+ ecdsa.sigError().should.equal('Invalid signature')
172
+ })
173
+ })
174
+
175
+ describe('#sign', function () {
176
+ it('should create a valid signature', function () {
177
+ ecdsa.randomK()
178
+ ecdsa.sign()
179
+ ecdsa.verify().verified.should.equal(true)
180
+ })
181
+
182
+ it('should should throw an error if hashbuf is not 32 bytes', function () {
183
+ var ecdsa2 = ECDSA().set({
184
+ hashbuf: ecdsa.hashbuf.slice(0, 31),
185
+ privkey: ecdsa.privkey
186
+ })
187
+ ecdsa2.randomK()
188
+ ecdsa2.sign.bind(ecdsa2).should.throw('hashbuf must be a 32 byte buffer')
189
+ })
190
+
191
+ it('should default to deterministicK', function () {
192
+ var ecdsa2 = new ECDSA(ecdsa)
193
+ ecdsa2.k = undefined
194
+ var called = 0
195
+ var deterministicK = ecdsa2.deterministicK.bind(ecdsa2)
196
+ ecdsa2.deterministicK = function () {
197
+ deterministicK()
198
+ called++
199
+ }
200
+ ecdsa2.sign()
201
+ called.should.equal(1)
202
+ })
203
+
204
+ it('should generate right K', function () {
205
+ var msg1 = Buffer.from('52204d20fd0131ae1afd173fd80a3a746d2dcc0cddced8c9dc3d61cc7ab6e966', 'hex')
206
+ var msg2 = [].reverse.call(Buffer.from(msg1))
207
+ var pk = Buffer.from('16f243e962c59e71e54189e67e66cf2440a1334514c09c00ddcc21632bac9808', 'hex')
208
+ var signature1 = ECDSA.sign(msg1, Privkey.fromBuffer(pk)).toBuffer().toString('hex')
209
+ var signature2 = ECDSA.sign(msg2, Privkey.fromBuffer(pk), 'little').toBuffer().toString('hex')
210
+ signature1.should.equal(signature2)
211
+ })
212
+ })
213
+
214
+ describe('#toString', function () {
215
+ it('should convert this to a string', function () {
216
+ var str = ecdsa.toString();
217
+ (typeof str === 'string').should.equal(true)
218
+ })
219
+ })
220
+
221
+ describe('signing and verification', function () {
222
+ describe('@sign', function () {
223
+ it('should produce a signature', function () {
224
+ var sig = ECDSA.sign(ecdsa.hashbuf, ecdsa.privkey)
225
+ ;(sig instanceof Signature).should.equal(true)
226
+ })
227
+
228
+ it('should produce a signaturei', function () {
229
+ var sig = ECDSA.signWithCalcI(ecdsa.hashbuf, ecdsa.privkey)
230
+ ;(sig instanceof Signature).should.equal(true)
231
+ should.exist(sig.i)
232
+ sig.toCompact().toString('base64').should.equal('IF7ucRrxMczOWYg28NkA+krY4aXx9XgT7KVglwpLEqqyIUtqBrrU5mSDQqOm0ETZ47KdQEMv121aWkxO7P1hbGk=')
233
+ })
234
+
235
+ it('should produce a signature', function () {
236
+ var sig = ECDSA.signRandomK(ecdsa.hashbuf, ecdsa.privkey)
237
+ ;(sig instanceof Signature).should.equal(true)
238
+ var sig2 = ECDSA.signRandomK(ecdsa.hashbuf, ecdsa.privkey)
239
+ ;(sig2 instanceof Signature).should.equal(true)
240
+ sig.toString().should.not.equal(sig2.toString())
241
+ })
242
+
243
+ it('should produce a signature, and be different when called twice', function () {
244
+ ecdsa.signRandomK()
245
+ should.exist(ecdsa.sig)
246
+ var ecdsa2 = ECDSA(ecdsa)
247
+ ecdsa2.signRandomK()
248
+ ecdsa.sig.toString().should.not.equal(ecdsa2.sig.toString())
249
+ })
250
+ })
251
+
252
+ describe('#verify', function () {
253
+ it('should verify a signature that was just signed', function () {
254
+ ecdsa.sig = Signature.fromString('3046022100e9915e6236695f093a4128ac2a956c' +
255
+ '40ed971531de2f4f41ba05fac7e2bd019c02210094e6a4a769cc7f2a8ab3db696c7cd8d56bcdbfff860a8c81de4bc6a798b90827')
256
+ ecdsa.verify().verified.should.equal(true)
257
+ })
258
+ it('should verify this known good signature', function () {
259
+ ecdsa.signRandomK()
260
+ ecdsa.verify().verified.should.equal(true)
261
+ })
262
+ it('should verify a valid signature, and unverify an invalid signature', function () {
263
+ var sig = ECDSA.sign(ecdsa.hashbuf, ecdsa.privkey)
264
+ ECDSA.verify(ecdsa.hashbuf, sig, ecdsa.pubkey).should.equal(true)
265
+ var fakesig = new Signature(sig.r.add(new BN(1)), sig.s)
266
+ ECDSA.verify(ecdsa.hashbuf, fakesig, ecdsa.pubkey).should.equal(false)
267
+ })
268
+ it('should work with big and little endian', function () {
269
+ var sig = ECDSA.sign(ecdsa.hashbuf, ecdsa.privkey, 'big')
270
+ ECDSA.verify(ecdsa.hashbuf, sig, ecdsa.pubkey, 'big').should.equal(true)
271
+ ECDSA.verify(ecdsa.hashbuf, sig, ecdsa.pubkey, 'little').should.equal(false)
272
+ sig = ECDSA.sign(ecdsa.hashbuf, ecdsa.privkey, 'little')
273
+ ECDSA.verify(ecdsa.hashbuf, sig, ecdsa.pubkey, 'big').should.equal(false)
274
+ ECDSA.verify(ecdsa.hashbuf, sig, ecdsa.pubkey, 'little').should.equal(true)
275
+ })
276
+ })
277
+
278
+ describe('vectors', function () {
279
+ vectors.valid.forEach(function (obj, i) {
280
+ it('should validate valid vector ' + i, function () {
281
+ var ecdsa = ECDSA().set({
282
+ privkey: new Privkey(BN.fromBuffer(Buffer.from(obj.d, 'hex'))),
283
+ k: BN.fromBuffer(Buffer.from(obj.k, 'hex')),
284
+ hashbuf: Hash.sha256(Buffer.from(obj.message)),
285
+ sig: new Signature().set({
286
+ r: new BN(obj.signature.r),
287
+ s: new BN(obj.signature.s),
288
+ i: obj.i
289
+ })
290
+ })
291
+ var ecdsa2 = ECDSA(ecdsa)
292
+ ecdsa2.k = undefined
293
+ ecdsa2.sign()
294
+ ecdsa2.calci()
295
+ ecdsa2.k.toString().should.equal(ecdsa.k.toString())
296
+ ecdsa2.sig.toString().should.equal(ecdsa.sig.toString())
297
+ ecdsa2.sig.i.should.equal(ecdsa.sig.i)
298
+ ecdsa.verify().verified.should.equal(true)
299
+ })
300
+ })
301
+
302
+ vectors.invalid.sigError.forEach(function (obj, i) {
303
+ it('should validate invalid.sigError vector ' + i + ': ' + obj.description, function () {
304
+ var ecdsa = ECDSA().set({
305
+ pubkey: Pubkey.fromPoint(point.fromX(true, 1)),
306
+ sig: new Signature(new BN(obj.signature.r), new BN(obj.signature.s)),
307
+ hashbuf: Hash.sha256(Buffer.from(obj.message))
308
+ })
309
+ ecdsa.sigError().should.equal(obj.exception)
310
+ })
311
+ })
312
+
313
+ vectors.deterministicK.forEach(function (obj, i) {
314
+ it('should validate deterministicK vector ' + i, function () {
315
+ var hashbuf = Hash.sha256(Buffer.from(obj.message))
316
+ var privkey = Privkey(BN.fromBuffer(Buffer.from(obj.privkey, 'hex')), 'mainnet')
317
+ var ecdsa = ECDSA({
318
+ privkey: privkey,
319
+ hashbuf: hashbuf
320
+ })
321
+ ecdsa.deterministicK(0).k.toString('hex').should.equal(obj.k_bad00)
322
+ ecdsa.deterministicK(1).k.toString('hex').should.equal(obj.k_bad01)
323
+ ecdsa.deterministicK(15).k.toString('hex').should.equal(obj.k_bad15)
324
+ })
325
+ })
326
+ })
327
+ })
328
+ })
@@ -0,0 +1,61 @@
1
+ 'use strict'
2
+
3
+ /* global describe, it */
4
+
5
+ var assert = require('assert')
6
+ var ec = require('../../lib/crypto/elliptic-fixed')
7
+
8
+ describe('elliptic-fixed (hardened secp256k1)', function () {
9
+ var key = ec.keyFromPrivate(
10
+ '0101010101010101010101010101010101010101010101010101010101010101', 'hex'
11
+ )
12
+ var msg = 'deadbeef'.repeat(8) // 32-byte hash, hex
13
+
14
+ it('produces low-S canonical signatures', function () {
15
+ for (var i = 0; i < 20; i++) {
16
+ var m = (i.toString(16).padStart(2, '0')).repeat(32)
17
+ var sig = ec.sign(m, key)
18
+ var halfN = ec.curve.n.ushrn(1)
19
+ assert.ok(sig.s.cmp(halfN) <= 0, 's must be <= n/2 (low-S)')
20
+ }
21
+ })
22
+
23
+ it('keeps recoveryParam consistent so the pubkey recovers correctly', function () {
24
+ // Regression guard: a manual s-flip without flipping recoveryParam (the
25
+ // pre-fix behaviour) recovers the WRONG public key whenever s was high —
26
+ // which is ~50% of signatures. Use buffer messages for both sign and
27
+ // recover so the encoding is identical on each side.
28
+ var pub = key.getPublic()
29
+ for (var i = 1; i <= 40; i++) {
30
+ var m = Buffer.from(i.toString(16).padStart(2, '0').repeat(32), 'hex')
31
+ var sig = ec.sign(m, key)
32
+ assert.notStrictEqual(sig.recoveryParam, null)
33
+ var recovered = ec.recoverPubKey(m, sig, sig.recoveryParam)
34
+ assert.ok(recovered.eq(pub), 'recovered pubkey must match signer (iter ' + i + ')')
35
+ }
36
+ })
37
+
38
+ it('still verifies its own canonical signatures', function () {
39
+ var sig = ec.sign(msg, key)
40
+ assert.strictEqual(ec.verify(msg, sig, key), true)
41
+ })
42
+
43
+ it('rejects zero r or s', function () {
44
+ var sig = ec.sign(msg, key)
45
+ var BN = sig.r.constructor // bn.js
46
+ assert.strictEqual(ec.verify(msg, { r: new BN(0), s: sig.s }, key), false)
47
+ assert.strictEqual(ec.verify(msg, { r: sig.r, s: new BN(0) }, key), false)
48
+ })
49
+
50
+ it('rejects out-of-range r or s (>= n)', function () {
51
+ var sig = ec.sign(msg, key)
52
+ assert.strictEqual(ec.verify(msg, { r: ec.curve.n, s: sig.s }, key), false)
53
+ assert.strictEqual(ec.verify(msg, { r: sig.r, s: ec.curve.n }, key), false)
54
+ })
55
+
56
+ it('returns false (never throws) on malformed signatures', function () {
57
+ assert.strictEqual(ec.verify(msg, null, key), false)
58
+ assert.strictEqual(ec.verify(msg, {}, key), false)
59
+ assert.strictEqual(ec.verify(msg, { r: 'x', s: 'y' }, key), false)
60
+ })
61
+ })
@@ -0,0 +1,121 @@
1
+ 'use strict'
2
+
3
+ require('chai').should()
4
+ var Hash = require('../../lib/crypto/hash.browser')
5
+
6
+ describe('Hash', function () {
7
+ var buf = Buffer.from([0, 1, 2, 3, 253, 254, 255])
8
+ var str = 'test string'
9
+
10
+ describe('@sha1', function () {
11
+ it('calculates the hash of this buffer correctly', function () {
12
+ var hash = Hash.sha1(buf)
13
+ hash.toString('hex').should.equal('de69b8a4a5604d0486e6420db81e39eb464a17b2')
14
+ hash = Hash.sha1(Buffer.alloc(0))
15
+ hash.toString('hex').should.equal('da39a3ee5e6b4b0d3255bfef95601890afd80709')
16
+ })
17
+
18
+ it('throws an error when the input is not a buffer', function () {
19
+ Hash.sha1.bind(Hash, str).should.throw('Invalid Argument')
20
+ })
21
+ })
22
+
23
+ describe('#sha256', function () {
24
+ it('calculates the hash of this buffer correctly', function () {
25
+ var hash = Hash.sha256(buf)
26
+ hash.toString('hex').should.equal('6f2c7b22fd1626998287b3636089087961091de80311b9279c4033ec678a83e8')
27
+ })
28
+
29
+ it('fails when the input is not a buffer', function () {
30
+ Hash.sha256.bind(Hash, str).should.throw('Invalid Argument')
31
+ })
32
+ })
33
+
34
+ describe('#sha256hmac', function () {
35
+ it('computes this known big key correctly', function () {
36
+ var key = Buffer.from('b613679a0814d9ec772f95d778c35fc5ff1697c493715653c6c712144292c5ad' +
37
+ 'b613679a0814d9ec772f95d778c35fc5ff1697c493715653c6c712144292c5ad' +
38
+ 'b613679a0814d9ec772f95d778c35fc5ff1697c493715653c6c712144292c5ad' +
39
+ 'b613679a0814d9ec772f95d778c35fc5ff1697c493715653c6c712144292c5ad')
40
+ var data = Buffer.from('')
41
+ Hash.sha256hmac(data, key).toString('hex')
42
+ .should.equal('fb1f87218671f1c0c4593a88498e02b6dfe8afd814c1729e89a1f1f6600faa23')
43
+ })
44
+
45
+ it('computes this known empty test vector correctly', function () {
46
+ var key = Buffer.from('')
47
+ var data = Buffer.from('')
48
+ Hash.sha256hmac(data, key).toString('hex')
49
+ .should.equal('b613679a0814d9ec772f95d778c35fc5ff1697c493715653c6c712144292c5ad')
50
+ })
51
+
52
+ it('computes this known non-empty test vector correctly', function () {
53
+ var key = Buffer.from('key')
54
+ var data = Buffer.from('The quick brown fox jumps over the lazy dog')
55
+ Hash.sha256hmac(data, key).toString('hex')
56
+ .should.equal('f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8')
57
+ })
58
+ })
59
+
60
+ describe('#sha256sha256', function () {
61
+ it('calculates the hash of this buffer correctly', function () {
62
+ var hash = Hash.sha256sha256(buf)
63
+ hash.toString('hex').should.equal('be586c8b20dee549bdd66018c7a79e2b67bb88b7c7d428fa4c970976d2bec5ba')
64
+ })
65
+
66
+ it('fails when the input is not a buffer', function () {
67
+ Hash.sha256sha256.bind(Hash, str).should.throw('Invalid Argument')
68
+ })
69
+ })
70
+
71
+ describe('#sha256ripemd160', function () {
72
+ it('calculates the hash of this buffer correctly', function () {
73
+ var hash = Hash.sha256ripemd160(buf)
74
+ hash.toString('hex').should.equal('7322e2bd8535e476c092934e16a6169ca9b707ec')
75
+ })
76
+
77
+ it('fails when the input is not a buffer', function () {
78
+ Hash.sha256ripemd160.bind(Hash, str).should.throw('Invalid Argument')
79
+ })
80
+ })
81
+
82
+ describe('#ripemd160', function () {
83
+ it('calculates the hash of this buffer correctly', function () {
84
+ var hash = Hash.ripemd160(buf)
85
+ hash.toString('hex').should.equal('fa0f4565ff776fee0034c713cbf48b5ec06b7f5c')
86
+ })
87
+
88
+ it('fails when the input is not a buffer', function () {
89
+ Hash.ripemd160.bind(Hash, str).should.throw('Invalid Argument')
90
+ })
91
+ })
92
+
93
+ describe('#sha512', function () {
94
+ it('calculates the hash of this buffer correctly', function () {
95
+ var hash = Hash.sha512(buf)
96
+ hash.toString('hex')
97
+ .should.equal('c0530aa32048f4904ae162bc14b9eb535eab6c465e960130005fedd' +
98
+ 'b71613e7d62aea75f7d3333ba06e805fc8e45681454524e3f8050969fe5a5f7f2392e31d0')
99
+ })
100
+
101
+ it('fails when the input is not a buffer', function () {
102
+ Hash.sha512.bind(Hash, str).should.throw('Invalid Argument')
103
+ })
104
+ })
105
+
106
+ describe('#sha512hmac', function () {
107
+ it('calculates this known empty test vector correctly', function () {
108
+ var hex = 'b936cee86c9f87aa5d3c6f2e84cb5a4239a5fe50480a6ec66b70ab5b1f4a' +
109
+ 'c6730c6c515421b327ec1d69402e53dfb49ad7381eb067b338fd7b0cb22247225d47'
110
+ Hash.sha512hmac(Buffer.from([]), Buffer.from([])).toString('hex').should.equal(hex)
111
+ })
112
+
113
+ it('calculates this known non-empty test vector correctly', function () {
114
+ var hex = 'c40bd7c15aa493b309c940e08a73ffbd28b2e4cb729eb94480d727e4df577' +
115
+ 'b13cc403a78e6150d83595f3b17c4cc331f12ca5952691de3735a63c1d4c69a2bac'
116
+ var data = Buffer.from('test1')
117
+ var key = Buffer.from('test2')
118
+ Hash.sha512hmac(data, key).toString('hex').should.equal(hex)
119
+ })
120
+ })
121
+ })