-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexampleSignatureValidation.js
38 lines (35 loc) · 1.18 KB
/
exampleSignatureValidation.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
var crypto = require("crypto");
var eccrypto = require("eccrypto");
const CoinKey = require("coinkey");
const coinkey = new CoinKey.createRandom();
var privateKey = coinkey.privateKey;
var publicKey = coinkey.publicKey;
// A new random 32-byte private key.
// var privateKey = Buffer.from(
// "7ba3308fbaac93d46f31177abe0e050634af9b1bc4602f0a309c529874a7a536",
// // "2fd7cab0970c692b4151d77a6aeebcae2a3284556cbfc6f182c571eccfc2424f",
// "hex"
// );
// // Corresponding uncompressed (65-byte) public key.
// // var publicKey = eccrypto.getPublic(privateKey);
// var publicKey = Buffer.from(
// "031887e01f35d0663a01edbbeef0eb945a2cf1e9318442a585c5905e0d886bc34a",
// "hex"
// );
var buf = Buffer.from(
"05de69f5d37f41340eed3230f03d2394dde5e497738a76f027b7d962a0cbdf39",
"hex"
);
// Always hash you message to sign!
var msg = crypto.createHash("sha256").update(buf).digest();
eccrypto.sign(privateKey, msg).then(function (sig) {
console.log("Signature in DER format (hex):", sig.toString("hex"));
eccrypto
.verify(publicKey, msg, sig)
.then(function () {
console.log("Signature is OK");
})
.catch(function () {
console.log("Signature is BAD");
});
});