-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
576 lines (528 loc) · 24.5 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
var assert = require('assert')
var ethUtils = require('../index.js')
var BN = require('bn.js')
describe('zeros function', function () {
it('should produce lots of 0s', function () {
var z60 = ethUtils.zeros(30)
var zs60 = '000000000000000000000000000000000000000000000000000000000000'
assert.equal(z60.toString('hex'), zs60)
})
})
describe('sha3', function () {
it('should produce a sha3', function () {
var msg = '0x3c9229289a6125f7fdf1885a77bb12c37a8d3b4962d936f7e3084dece32a3ca1'
var r = '82ff40c0a986c6a5cfad4ddf4c3aa6996f1a7837f9c398e17e5de5cbd5a12b28'
var hash = ethUtils.sha3(msg)
assert.equal(hash.toString('hex'), r)
})
})
describe('sha3 without hexprefix', function () {
it('should produce a sha3', function () {
var msg = '3c9229289a6125f7fdf1885a77bb12c37a8d3b4962d936f7e3084dece32a3ca1'
var r = '22ae1937ff93ec72c4d46ff3e854661e3363440acd6f6e4adf8f1a8978382251'
var hash = ethUtils.sha3(msg)
assert.equal(hash.toString('hex'), r)
})
})
describe('sha3-512', function () {
it('should produce a sha3', function () {
var msg = '0x3c9229289a6125f7fdf1885a77bb12c37a8d3b4962d936f7e3084dece32a3ca1'
var r = '36fdacd0339307068e9ed191773a6f11f6f9f99016bd50f87fd529ab7c87e1385f2b7ef1ac257cc78a12dcb3e5804254c6a7b404a6484966b831eadc721c3d24'
var hash = ethUtils.sha3(msg, 512)
assert.equal(hash.toString('hex'), r)
})
})
describe('sha256', function () {
it('should produce a sha256', function () {
var msg = '0x3c9229289a6125f7fdf1885a77bb12c37a8d3b4962d936f7e3084dece32a3ca1'
var r = '58bbda5e10bc11a32d808e40f9da2161a64f00b5557762a161626afe19137445'
var hash = ethUtils.sha256(msg)
assert.equal(hash.toString('hex'), r)
})
})
describe('ripemd160', function () {
it('should produce a ripemd160', function () {
var msg = '0x3c9229289a6125f7fdf1885a77bb12c37a8d3b4962d936f7e3084dece32a3ca1'
var r = '4bb0246cbfdfddbe605a374f1187204c896fabfd'
var hash = ethUtils.ripemd160(msg)
assert.equal(hash.toString('hex'), r)
})
it('should produce a padded ripemd160', function () {
var msg = '0x3c9229289a6125f7fdf1885a77bb12c37a8d3b4962d936f7e3084dece32a3ca1'
var r = '0000000000000000000000004bb0246cbfdfddbe605a374f1187204c896fabfd'
var hash = ethUtils.ripemd160(msg, true)
assert.equal(hash.toString('hex'), r)
})
})
describe('rlphash', function () {
it('should produce a sha3 of the rlp data', function () {
var msg = '0x3c9229289a6125f7fdf1885a77bb12c37a8d3b4962d936f7e3084dece32a3ca1'
var r = '33f491f24abdbdbf175e812b94e7ede338d1c7f01efb68574acd279a15a39cbe'
var hash = ethUtils.rlphash(msg)
assert.equal(hash.toString('hex'), r)
})
})
describe('unpad', function () {
it('should unpad a string', function () {
var str = '0000000006600'
var r = ethUtils.unpad(str)
assert.equal(r, '6600')
})
})
describe('unpad a hex string', function () {
it('should unpad a string', function () {
var str = '0x0000000006600'
var r = ethUtils.unpad(str)
assert.equal(r, '6600')
})
})
describe('pad', function () {
it('should left pad a Buffer', function () {
var buf = new Buffer([9, 9])
var padded = ethUtils.setLength(buf, 3)
assert.equal(padded.toString('hex'), '000909')
})
it('should left truncate a Buffer', function () {
var buf = new Buffer([9, 0, 9])
var padded = ethUtils.setLength(buf, 2)
assert.equal(padded.toString('hex'), '0009')
})
it('should left pad a Buffer - alias', function () {
var buf = new Buffer([9, 9])
var padded = ethUtils.setLengthLeft(buf, 3)
assert.equal(padded.toString('hex'), '000909')
})
})
describe('rpad', function () {
it('should right pad a Buffer', function () {
var buf = new Buffer([9, 9])
var padded = ethUtils.setLength(buf, 3, true)
assert.equal(padded.toString('hex'), '090900')
})
it('should right truncate a Buffer', function () {
var buf = new Buffer([9, 0, 9])
var padded = ethUtils.setLength(buf, 2, true)
assert.equal(padded.toString('hex'), '0900')
})
it('should right pad a Buffer - alias', function () {
var buf = new Buffer([9, 9])
var padded = ethUtils.setLengthRight(buf, 3)
assert.equal(padded.toString('hex'), '090900')
})
})
describe('bufferToHex', function () {
it('should convert a buffer to hex', function () {
var buf = new Buffer('5b9ac8', 'hex')
var hex = ethUtils.bufferToHex(buf)
assert.equal(hex, '0x5b9ac8')
})
})
describe('intToHex', function () {
it('should convert a int to hex', function () {
var i = 6003400
var hex = ethUtils.intToHex(i)
assert.equal(hex, '0x5b9ac8')
})
})
describe('intToBuffer', function () {
it('should convert a int to a buffer', function () {
var i = 6003400
var buf = ethUtils.intToBuffer(i)
assert.equal(buf.toString('hex'), '5b9ac8')
})
})
describe('bufferToInt', function () {
it('should convert a int to hex', function () {
var buf = new Buffer('5b9ac8', 'hex')
var i = ethUtils.bufferToInt(buf)
assert.equal(i, 6003400)
assert.equal(ethUtils.bufferToInt(new Buffer([])), 0)
})
it('should convert empty input to 0', function () {
assert.equal(ethUtils.bufferToInt(new Buffer([])), 0)
})
})
describe('fromSigned', function () {
it('should convert an unsigned (negative) buffer to a singed number', function () {
var neg = '-452312848583266388373324160190187140051835877600158453279131187530910662656'
var buf = new Buffer(32)
buf.fill(0)
buf[0] = 255
assert.equal(ethUtils.fromSigned(buf), neg)
})
it('should convert an unsigned (positive) buffer to a singed number', function () {
var neg = '452312848583266388373324160190187140051835877600158453279131187530910662656'
var buf = new Buffer(32)
buf.fill(0)
buf[0] = 1
assert.equal(ethUtils.fromSigned(buf), neg)
})
})
describe('toUnsigned', function () {
it('should convert a signed (negative) number to unsigned', function () {
var neg = '-452312848583266388373324160190187140051835877600158453279131187530910662656'
var hex = 'ff00000000000000000000000000000000000000000000000000000000000000'
var num = new BN(neg)
assert.equal(ethUtils.toUnsigned(num).toString('hex'), hex)
})
it('should convert a signed (positive) number to unsigned', function () {
var neg = '452312848583266388373324160190187140051835877600158453279131187530910662656'
var hex = '0100000000000000000000000000000000000000000000000000000000000000'
var num = new BN(neg)
assert.equal(ethUtils.toUnsigned(num).toString('hex'), hex)
})
})
describe('isValidPrivate', function () {
var SECP256K1_N = new ethUtils.BN('fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141', 16)
it('should fail on short input', function () {
var tmp = '0011223344'
assert.equal(ethUtils.isValidPrivate(new Buffer(tmp, 'hex')), false)
})
it('should fail on too big input', function () {
var tmp = '3a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d'
assert.equal(ethUtils.isValidPrivate(new Buffer(tmp, 'hex')), false)
})
it('should fail on invalid curve (zero)', function () {
var tmp = '0000000000000000000000000000000000000000000000000000000000000000'
assert.equal(ethUtils.isValidPrivate(new Buffer(tmp, 'hex')), false)
})
it('should fail on invalid curve (== N)', function () {
var tmp = SECP256K1_N.toString(16)
assert.equal(ethUtils.isValidPrivate(new Buffer(tmp, 'hex')), false)
})
it('should fail on invalid curve (>= N)', function () {
var tmp = SECP256K1_N.addn(1).toString(16)
assert.equal(ethUtils.isValidPrivate(new Buffer(tmp, 'hex')), false)
})
it('should work otherwise (< N)', function () {
var tmp = SECP256K1_N.subn(1).toString(16)
assert.equal(ethUtils.isValidPrivate(new Buffer(tmp, 'hex')), true)
})
})
describe('isValidPublic', function () {
it('should fail on too short input', function () {
var pubKey = '3a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae744'
pubKey = new Buffer(pubKey, 'hex')
assert.equal(ethUtils.isValidPublic(pubKey), false)
})
it('should fail on too big input', function () {
var pubKey = '3a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d00'
pubKey = new Buffer(pubKey, 'hex')
assert.equal(ethUtils.isValidPublic(pubKey), false)
})
it('should fail on SEC1 key', function () {
var pubKey = '043a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d'
pubKey = new Buffer(pubKey, 'hex')
assert.equal(ethUtils.isValidPublic(pubKey), false)
})
it('shouldn\'t fail on SEC1 key with sanitize enabled', function () {
var pubKey = '043a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d'
pubKey = new Buffer(pubKey, 'hex')
assert.equal(ethUtils.isValidPublic(pubKey, true), true)
})
it('should fail with an invalid SEC1 public key', function () {
var pubKey = '023a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d'
pubKey = new Buffer(pubKey, 'hex')
assert.equal(ethUtils.isValidPublic(pubKey, true), false)
})
it('should work with compressed keys with sanitize enabled', function () {
var pubKey = '033a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a'
pubKey = new Buffer(pubKey, 'hex')
assert.equal(ethUtils.isValidPublic(pubKey, true), true)
})
it('should work with sanitize enabled', function () {
var pubKey = '043a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d'
pubKey = new Buffer(pubKey, 'hex')
assert.equal(ethUtils.isValidPublic(pubKey, true), true)
})
it('should work otherwise', function () {
var pubKey = '3a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d'
pubKey = new Buffer(pubKey, 'hex')
assert.equal(ethUtils.isValidPublic(pubKey), true)
})
})
describe('importPublic', function () {
var pubKey = '3a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d'
it('should work with an Ethereum public key', function () {
var tmp = '3a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d'
assert.equal(ethUtils.importPublic(new Buffer(tmp, 'hex')).toString('hex'), pubKey)
})
it('should work with uncompressed SEC1 keys', function () {
var tmp = '043a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d'
assert.equal(ethUtils.importPublic(new Buffer(tmp, 'hex')).toString('hex'), pubKey)
})
it('should work with compressed SEC1 keys', function () {
var tmp = '033a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a'
assert.equal(ethUtils.importPublic(new Buffer(tmp, 'hex')).toString('hex'), pubKey)
})
})
describe('publicToAddress', function () {
it('should produce an address given a public key', function () {
var pubKey = '3a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d'
var address = '2f015c60e0be116b1f0cd534704db9c92118fb6a'
pubKey = new Buffer(pubKey, 'hex')
var r = ethUtils.publicToAddress(pubKey)
assert.equal(r.toString('hex'), address)
})
it('should produce an address given a SEC1 public key', function () {
var pubKey = '043a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d'
var address = '2f015c60e0be116b1f0cd534704db9c92118fb6a'
pubKey = new Buffer(pubKey, 'hex')
var r = ethUtils.publicToAddress(pubKey, true)
assert.equal(r.toString('hex'), address)
})
it('shouldn\'t produce an address given an invalid SEC1 public key', function () {
var pubKey = '023a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d'
pubKey = new Buffer(pubKey, 'hex')
assert.throws(function () {
ethUtils.publicToAddress(pubKey, true)
})
})
it('shouldn\'t produce an address given an invalid public key', function () {
var pubKey = '3a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae744'
pubKey = new Buffer(pubKey, 'hex')
assert.throws(function () {
ethUtils.publicToAddress(pubKey)
})
})
})
describe('publicToAddress 0x', function () {
it('should produce an address given a public key', function () {
var pubKey = '0x3a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d'
var address = '2f015c60e0be116b1f0cd534704db9c92118fb6a'
var r = ethUtils.publicToAddress(pubKey)
assert.equal(r.toString('hex'), address)
})
})
describe('privateToPublic', function () {
it('should produce a public key given a private key', function () {
var pubKey = '3a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d'
var privateKey = new Buffer([234, 84, 189, 197, 45, 22, 63, 136, 201, 58, 176, 97, 87, 130, 207, 113, 138, 46, 251, 158, 81, 167, 152, 154, 171, 27, 8, 6, 126, 156, 28, 95])
var r = ethUtils.privateToPublic(privateKey).toString('hex')
assert.equal(r.toString('hex'), pubKey)
})
it('shouldn\'t produce a public key given an invalid private key', function () {
var privateKey1 = new Buffer([234, 84, 189, 197, 45, 22, 63, 136, 201, 58, 176, 97, 87, 130, 207, 113, 138, 46, 251, 158, 81, 167, 152, 154, 171, 27, 8, 6, 126, 156, 28, 95, 42])
var privateKey2 = new Buffer([234, 84, 189, 197, 45, 22, 63, 136, 201, 58, 176, 97, 87, 130, 207, 113, 138, 46, 251, 158, 81, 167, 152, 154, 171, 27, 8, 6, 126, 156, 28])
assert.throws(function () {
ethUtils.privateToPublic(privateKey1)
})
assert.throws(function () {
ethUtils.privateToPublic(privateKey2)
})
})
})
describe('privateToAddress', function () {
it('should produce an address given a private key', function () {
var address = '2f015c60e0be116b1f0cd534704db9c92118fb6a'
// Our private key
var privateKey = new Buffer([234, 84, 189, 197, 45, 22, 63, 136, 201, 58, 176, 97, 87, 130, 207, 113, 138, 46, 251, 158, 81, 167, 152, 154, 171, 27, 8, 6, 126, 156, 28, 95])
var r = ethUtils.privateToAddress(privateKey).toString('hex')
assert.equal(r.toString('hex'), address)
})
})
describe('generateAddress', function () {
it('should produce an address given a public key', function () {
var add = ethUtils.generateAddress('990ccf8a0de58091c028d6ff76bb235ee67c1c39', 14).toString('hex')
assert.equal(add.toString('hex'), '936a4295d8d74e310c0c95f0a63e53737b998d12')
})
})
describe('generateAddress with hex prefix', function () {
it('should produce an address given a public key', function () {
var add = ethUtils.generateAddress('0x990ccf8a0de58091c028d6ff76bb235ee67c1c39', 14).toString('hex')
assert.equal(add.toString('hex'), 'd658a4b8247c14868f3c512fa5cbb6e458e4a989')
})
})
describe('generateAddress with nonce 0 (special case)', function () {
it('should produce an address given a public key', function () {
var add = ethUtils.generateAddress('0x990ccf8a0de58091c028d6ff76bb235ee67c1c39', 0).toString('hex')
assert.equal(add.toString('hex'), 'bfa69ba91385206bfdd2d8b9c1a5d6c10097a85b')
})
})
describe('hex prefix', function () {
var string = 'd658a4b8247c14868f3c512fa5cbb6e458e4a989'
it('should add', function () {
assert.equal(ethUtils.addHexPrefix(string), '0x' + string)
})
it('should return on non-string input', function () {
assert.equal(ethUtils.addHexPrefix(1), 1)
})
})
describe('isPrecompiled', function () {
it('should return true', function () {
assert.equal(ethUtils.isPrecompiled('0000000000000000000000000000000000000001'), true)
assert.equal(ethUtils.isPrecompiled('0000000000000000000000000000000000000002'), true)
assert.equal(ethUtils.isPrecompiled('0000000000000000000000000000000000000003'), true)
assert.equal(ethUtils.isPrecompiled('0000000000000000000000000000000000000004'), true)
})
it('should return false', function () {
assert.equal(ethUtils.isPrecompiled('0000000000000000000000000000000000000000'), false)
assert.equal(ethUtils.isPrecompiled('0000000000000000000000000000000000000005'), false)
assert.equal(ethUtils.isPrecompiled('1000000000000000000000000000000000000000'), false)
})
})
describe('toBuffer', function () {
it('should work', function () {
// Buffer
assert.deepEqual(ethUtils.toBuffer(new Buffer([])), new Buffer([]))
// Array
assert.deepEqual(ethUtils.toBuffer([]), new Buffer([]))
// String
assert.deepEqual(ethUtils.toBuffer('11'), new Buffer([49, 49]))
assert.deepEqual(ethUtils.toBuffer('0x11'), new Buffer([17]))
assert.deepEqual(ethUtils.toBuffer('1234').toString('hex'), '31323334')
assert.deepEqual(ethUtils.toBuffer('0x1234').toString('hex'), '1234')
// Number
assert.deepEqual(ethUtils.toBuffer(1), new Buffer([1]))
// null
assert.deepEqual(ethUtils.toBuffer(null), new Buffer([]))
// undefined
assert.deepEqual(ethUtils.toBuffer(), new Buffer([]))
// 'toArray'
assert.deepEqual(ethUtils.toBuffer(new BN(1)), new Buffer([1]))
})
it('should fail', function () {
assert.throws(function () {
ethUtils.toBuffer({ test: 1 })
})
})
})
describe('baToJSON', function () {
it('should turn a array of buffers into a pure json object', function () {
var ba = [new Buffer([0]), new Buffer([1]), [new Buffer([2])]]
assert.deepEqual(ethUtils.baToJSON(ba), ['0x00', '0x01', ['0x02']])
})
it('should turn a buffers into string', function () {
assert.deepEqual(ethUtils.baToJSON(new Buffer([0])), '0x00')
})
})
var echash = new Buffer('82ff40c0a986c6a5cfad4ddf4c3aa6996f1a7837f9c398e17e5de5cbd5a12b28', 'hex')
var ecprivkey = new Buffer('3c9229289a6125f7fdf1885a77bb12c37a8d3b4962d936f7e3084dece32a3ca1', 'hex')
describe('ecsign', function () {
it('should produce a signature', function () {
var sig = ethUtils.ecsign(echash, ecprivkey)
assert.deepEqual(sig.r, new Buffer('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex'))
assert.deepEqual(sig.s, new Buffer('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66', 'hex'))
assert.equal(sig.v, 27)
})
})
describe('ecrecover', function () {
it('should recover a public key', function () {
var r = new Buffer('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex')
var s = new Buffer('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66', 'hex')
var pubkey = ethUtils.ecrecover(echash, 27, r, s)
assert.deepEqual(pubkey, ethUtils.privateToPublic(ecprivkey))
})
it('should fail on an invalid signature (v = 21)', function () {
var r = new Buffer('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex')
var s = new Buffer('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66', 'hex')
assert.throws(function () {
ethUtils.ecrecover(echash, 21, r, s)
})
})
it('should fail on an invalid signature (v = 29)', function () {
var r = new Buffer('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex')
var s = new Buffer('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66', 'hex')
assert.throws(function () {
ethUtils.ecrecover(echash, 29, r, s)
})
})
it('should fail on an invalid signature (swapped points)', function () {
var r = new Buffer('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex')
var s = new Buffer('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66', 'hex')
assert.throws(function () {
ethUtils.ecrecover(echash, 27, s, r)
})
})
})
describe('isValidSignature', function () {
it('should fail on an invalid signature (shorter r))', function () {
var r = new Buffer('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1ab', 'hex')
var s = new Buffer('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66', 'hex')
assert.equal(ethUtils.isValidSignature(27, r, s), false)
})
it('should fail on an invalid signature (shorter s))', function () {
var r = new Buffer('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex')
var s = new Buffer('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca', 'hex')
assert.equal(ethUtils.isValidSignature(27, r, s), false)
})
it('should fail on an invalid signature (v = 21)', function () {
var r = new Buffer('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex')
var s = new Buffer('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66', 'hex')
assert.equal(ethUtils.isValidSignature(21, r, s), false)
})
it('should fail on an invalid signature (v = 29)', function () {
var r = new Buffer('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex')
var s = new Buffer('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66', 'hex')
assert.equal(ethUtils.isValidSignature(29, r, s), false)
})
it('should work otherwise', function () {
var r = new Buffer('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex')
var s = new Buffer('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66', 'hex')
assert.equal(ethUtils.isValidSignature(27, r, s), true)
})
// FIXME: add homestead test
})
var checksumAddresses = [
// All caps
'0x52908400098527886E0F7030069857D2E4169EE7',
'0x8617E340B3D01FA5F11F306F4090FD50E238070D',
// All Lower
'0xde709f2102306220921060314715629080e2fb77',
'0x27b1fdb04752bbc536007a920d24acb045561c26',
// Normal
'0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed',
'0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359',
'0xdbF03B407c01E7cD3CBea99509d93f8DDDC8C6FB',
'0xD1220A0cf47c7B9Be7A2E6BA89F429762e7b9aDb'
]
describe('.toChecksumAddress()', function () {
it('should work', function () {
for (var i = 0; i < checksumAddresses.length; i++) {
var tmp = checksumAddresses[i]
assert.equal(ethUtils.toChecksumAddress(tmp.toLowerCase()), tmp)
}
})
})
describe('.isValidChecksumAddress()', function () {
it('should return true', function () {
for (var i = 0; i < checksumAddresses.length; i++) {
assert.equal(ethUtils.isValidChecksumAddress(checksumAddresses[i]), true)
}
})
it('should validate', function () {
assert.equal(ethUtils.isValidChecksumAddress('0x2f015c60e0be116b1f0cd534704db9c92118fb6a'), false)
})
})
describe('.isValidAddress()', function () {
it('should return true', function () {
assert.equal(ethUtils.isValidAddress('0x2f015c60e0be116b1f0cd534704db9c92118fb6a'), true)
assert.equal(ethUtils.isValidAddress('0x52908400098527886E0F7030069857D2E4169EE7'), true)
})
it('should return false', function () {
assert.equal(ethUtils.isValidAddress('2f015c60e0be116b1f0cd534704db9c92118fb6a'), false)
assert.equal(ethUtils.isValidAddress('0x2f015c60e0be116b1f0cd534704db9c92118fb6'), false)
assert.equal(ethUtils.isValidAddress('0x2f015c60e0be116b1f0cd534704db9c92118fb6aa'), false)
assert.equal(ethUtils.isValidAddress('x2f015c60e0be116b1f0cd534704db9c92118fb6a'), false)
})
})
describe('message sig', function () {
const r = new Buffer('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex')
const s = new Buffer('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66', 'hex')
it('should return hex strings that the RPC can use', function () {
assert.equal(ethUtils.toRpcSig(27, r, s), '0x99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca6600')
assert.deepEqual(ethUtils.fromRpcSig('0x99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca6600'), {
v: 27,
r: r,
s: s
})
})
it('should throw on invalid length', function () {
assert.throws(function () {
ethUtils.fromRpcSig('')
})
assert.throws(function () {
ethUtils.fromRpcSig('0x99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca660042')
})
})
})