-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchain-util.js
More file actions
42 lines (37 loc) · 1.64 KB
/
chain-util.js
File metadata and controls
42 lines (37 loc) · 1.64 KB
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
const EC = require('elliptic').ec;
const uuidV1 = require('uuid/v1'); // used to generate unique id for each user based on TimeStamp
const ec = EC('secp256k1'); // standards of effiecient cryptography p- prime 256 bits k - koblet name of mathematician in cryptography
//ec used for wallets for generating private and public keys.
const SHA256 = require('crypto-js/sha256'); // for generating unique SHA256 hash acc. to data.
const EthCrypto = require('eth-crypto');
const identity = EthCrypto.createIdentity();
class ChainUtil{
static genKeyPair(){
return identity;
} // generate a unique public key and private key pair.
static id(){
return uuidV1();
} // return unique acc. to TimeStamp.
static hash(data){
return SHA256(JSON.stringify(data)).toString();
} // stringify the data passed and then pass it in function SHA256 which will return unique hash value
//toString converts number into string
// static verifySignature(publicKey, signature, data){
// return ec.keyFromPublic(publicKey, 'hex').verify(data, signature);
// }// this function is used verify the signature, hex written here used to tell ec that that public key is in hex form and keyFromPublic is function of class EC .verify defins it method passing data and signature which is generated by ec function.
static verifySignature(publicKey, signature, data){
// return ec.keyFromPublic(publicKey, 'hex').verify(data, signature);
const signer = EthCrypto.recoverPublicKey(
signature, // signature
data // message hash
);
if(signer === publicKey){
return true;
}
else{
return false
}
}
}
module.exports = ChainUtil;
//Exports the class