forked from akrabi/ethverify.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
26 lines (19 loc) · 1008 Bytes
/
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
const prompt = require('prompt');
const { randomBytes } = require('crypto')
const ethutil = require('ethereumjs-util');
const secp256k1 = require('secp256k1');
prompt.start();
prompt.get(['publicAddress', 'privateKey'], function (err, result) {
const privateKeyBuffer = new Buffer(result.privateKey, 'hex');
const publicKeyBuffer = ethutil.privateToPublic(privateKeyBuffer);
// verify private key
console.log('Private key legal: ' + ethutil.isValidPrivate(privateKeyBuffer));
// verify public address
console.log('Wallet address matches public address derived from private key: ' + (ethutil.bufferToHex(ethutil.pubToAddress(publicKeyBuffer)).toUpperCase() === result.publicAddress.toUpperCase()));
// generate message to sign
const msg = randomBytes(32);
// sign the message
const sigObj = secp256k1.sign(msg, privateKeyBuffer);
// verify the signature
console.log('Signature verified: ' + secp256k1.verify(msg, sigObj.signature, Buffer.concat([ Buffer.from([4]), publicKeyBuffer ])));
});