-
Notifications
You must be signed in to change notification settings - Fork 0
/
chain-util.js
47 lines (42 loc) · 1.21 KB
/
chain-util.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
const EC = require('elliptic').ec;
const ec = new EC('secp256k1');
const uuidV1 = require('uuid/v1');
const SHA3 = require('crypto-js/sha3');
/**
* A utility class for maintining the chain
*/
class ChainUtil {
/**
* Generate a new private public key pair
* @return {object} A secure key pair object
*/
static generateKeyPair() {
return ec.genKeyPair();
}
/**
* Generate a universally unique id based from the current time
* @return {string} A universally unique ID
*/
static id() {
return uuidV1();
}
/**
* Generate a hash for some given data
* @param {any} data The data we want to hash
* @return {string} The hash of the data
*/
static hash(data) {
return SHA3(JSON.stringify(data)).toString();
}
/**
* Verify a signature is valid
* @param {string} publicKey The public key
* @param {string} signature The signature to verify
* @param {string} dataHash The data we expect to find if valid
* @return {boolean} A true/false value representing validity
*/
static verfiySignature(publicKey, signature, dataHash) {
return ec.keyFromPublic(publicKey, 'hex').verify(dataHash, signature);
}
}
module.exports = ChainUtil;