-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
81 lines (79 loc) · 1.9 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
/*
Copyright sivabharathy.in All Rights Reserved.
*/
const crypto = require('crypto');
const { randomBytes } = require('crypto');
const { ec } = require('elliptic');
const EC = new ec('secp256k1');
/**
*
* @returns
*/
function generateWallet() {
const privateKey = randomBytes(32).toString('hex');
const keyPair = EC.keyFromPrivate(privateKey);
const publicKey = keyPair.getPublic('hex');
return { publicKey, privateKey };
}
/**
*
* @param {*} publicKey
* @returns Boolean
*/
function checkPublicKeyValid(publicKey) {
try {
// Attempt to create a keyPair from the provided public key
const keyPair = EC.keyFromPublic(publicKey, 'hex');
// Validate the key by getting its public key and comparing
const isValid = keyPair.getPublic('hex') === publicKey;
return { valid: isValid, public_key: publicKey }
} catch (error) {
// If an error occurs, the public key is invalid
return { valid: false, public_key: publicKey }
}
}
/**
*
* @param {*} privateKey
* @param {*} data
* @returns Object
*/
function signData(privateKey, data) {
const keyPair = EC.keyFromPrivate(privateKey);
const msgHash = crypto.createHash('sha256').update(data).digest();
const signature = keyPair.sign(msgHash);
// return signature with data signed
return {
data,
signature: {
r: signature.r.toString('hex'),
s: signature.s.toString('hex')
}
}
}
/**
*
* @param {*} publicKey
* @param {*} data
* @param {*} signature
* @returns
*/
function verifySignature(publicKey, data, signature) {
const keyPair = EC.keyFromPublic(publicKey, 'hex');
const msgHash = crypto.createHash('sha256').update(data).digest();
const isVerified = keyPair.verify(msgHash, {
r: signature.r,
s: signature.s
});
return {
data,
signature,
is_verified: isVerified
}
}
module.exports = {
generateWallet,
checkPublicKeyValid,
signData,
verifySignature
}