Skip to content

Instantly share code, notes, and snippets.

@tgoldenberg
Last active February 21, 2021 17:34
Show Gist options
  • Save tgoldenberg/646cfdf11dfd40c2066f1b81ca438027 to your computer and use it in GitHub Desktop.
Save tgoldenberg/646cfdf11dfd40c2066f1b81ca438027 to your computer and use it in GitHub Desktop.
Example of Elliptic Curve Digital Signature Algorithm
const RIPEMD160 = require('ripemd160');
const EC = require('elliptic').ec;
const sha256 = require('js-sha256');
const ec = new EC('secp256k1'); // Bitcoin uses Elliptic Curve Digital Signing Algorithm (ECDSA) to verify transactions
let message = 'I want to send this money'; // This would normally be the transaction hash with amount of money, publicScriptKey
let publicKey = '040c1fbe8b870d636823963ff21ba6e5250571848714203a08ae8700e0d97a1d7bb94ae888af72ac9a4fe02d558599d27c8986398902fa9ac0c1654f5839db1af5';
let privateKey = '2ec6d2808dc7b9d11e49516cfc747435feae8e9872d63c92bc5bde68a894199f';
// hash message - this method hashes the transaction header with SHA256 and then with RIPEMD160
function Hash(msg) {
let result = sha256(msg);
return new RIPEMD160().update(msg).digest('hex');
}
function createSignature(message, privateKey) {
const messageHash = Hash(message);
const privateKeyPair = ec.keyFromPrivate(privateKey);
const signature = ec.sign(messageHash, privateKeyPair); // generate a signature on the hashed message with the private key
return signature;
}
function verifySignature(message, publicAddress, signature) {
const messageHash = Hash(message);
const publicKeyPair = ec.keyFromPublic(publicAddress, 'hex'); // use the accessible public key to verify the signature
const isVerified = publicKeyPair.verify(messageHash, signature);
return isVerified;
}
function testVerification(publicKey, privateKey, message) {
const signature = createSignature(message, privateKey);
console.log('> Signature: ', signature.toDER('hex'));
const isVerified = verifySignature(message, publicKey, signature);
console.log('> Is verified: ', isVerified);
}
testVerification(publicKey, privateKey, message); // true
let wrongPublicKey = '04487bd002b2b61a1bbc89b3c05cebf73039d4722c96877308ee4905c10f155d71f03dca22650a2aea193416dd5071260b3fca82ab5a254163371e5929fb28c0f2';
testVerification(wrongPublicKey, privateKey, message); // false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment