-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcrypto.js
55 lines (43 loc) · 1.76 KB
/
crypto.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
/**
* Crypto lib tests.
*/
'use strict';
require('chai').should();
const cosmos = require('../index.js');
const crypto = require('crypto');
const MNEMONIC = 'cluster unveil differ bright define prosper hunt warrior fetch rough host fringe worry mention gospel enlist elder laugh segment funny avoid regular market fortune';
const PUBLIC_KEY = '037613839479ae3b2a00be35ec9a97a27c53ef551788de7ef51628970458b3d38f';
const PRIVATE_KEY = '8ce79a8c09c0e899715d33ef091eca92e1d77fbbd335bc8adfc1af0a29945a46';
describe('lib/crypto', () => {
let keys;
let signature;
const bytes = crypto.randomBytes(12);
const json = {
data: crypto.randomBytes(128)
};
it('Should generate keys from mnemonic', () => {
keys = cosmos.crypto.getKeysFromMnemonic(MNEMONIC);
keys.publicKey.toString('hex').should.equal(PUBLIC_KEY);
keys.privateKey.toString('hex').should.equal(PRIVATE_KEY);
});
it('Should sign bytes with private key', () => {
signature = cosmos.crypto.sign(bytes, keys.privateKey);
signature.length.should.be.equal(64);
});
it('Should verify signature', () => {
const verify = cosmos.crypto.verify(bytes, signature, keys.publicKey);
verify.should.be.true;
});
it('Should sign JSON with private key', () => {
signature = cosmos.crypto.signJson(json, keys.privateKey);
signature.length.should.be.equal(64);
});
it('Should verify signature on JSON', () => {
const verify = cosmos.crypto.verifyJson(json, signature, keys.publicKey);
verify.should.be.true;
});
it('Should reject wrong signature', () => {
const verify = cosmos.crypto.verifyJson(json, crypto.randomBytes(64), keys.publicKey);
verify.should.be.false;
});
});