Forked from bmatusiak/bitcoin-encrypt-decrypt-sign-verify.js
Created
December 23, 2019 16:55
-
-
Save zilveer/afbbdc960b66d1e8bfaa911dc0f21d07 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var crypto = require('crypto'); | |
var bitcoin = require('bitcoinjs-lib') | |
var bitcoinMessage = require('bitcoinjs-message') | |
function doVerify() { | |
var wif; | |
var keyPair; | |
wif = "KznJAT7suZ7UhX2kp2FxCjhJR54SGVfdySeLriwpeHtQFs7TcLBU"; | |
if (!wif) { | |
keyPair = bitcoin.ECPair.makeRandom(); | |
wif = keyPair.toWIF(); | |
console.log("WIF", wif); | |
} | |
else { | |
keyPair = bitcoin.ECPair.fromWIF(wif) | |
var p2pkh_Obj = bitcoin.payments.p2pkh({ | |
pubkey: keyPair.publicKey | |
}); | |
var address = p2pkh_Obj.address; | |
var privateKey = keyPair.privateKey | |
var message = makeRandomMessage(); | |
var signature = bitcoinMessage.sign(message, privateKey, keyPair.compressed).toString('base64') | |
var verify = bitcoinMessage.verify(message, address, signature); | |
console.log("WIF", wif) | |
console.log("address", address); | |
console.log("message", message) | |
console.log("signature", signature) | |
console.log("verify", verify) | |
} | |
function makeRandomMessage() { | |
var kp = bitcoin.ECPair.makeRandom(); | |
var p2pkh_Obj = bitcoin.payments.p2pkh({ | |
pubkey: kp.publicKey | |
}); | |
return p2pkh_Obj.address; | |
} | |
} | |
function doCryptoTest() { | |
var text = "this is a message"; | |
var Alice_keyPair = bitcoin.ECPair.makeRandom(); | |
var Alice_wif = Alice_keyPair.toWIF(); | |
var Bob_keyPair = bitcoin.ECPair.makeRandom(); | |
var Bob_wif = Bob_keyPair.toWIF(); | |
var pubB = new Buffer(Bob_keyPair.publicKey.toString("hex"), 'hex'); | |
var ecdhA = crypto.createECDH('secp256k1'); | |
ecdhA.generateKeys('hex', 'compressed'); | |
ecdhA.setPrivateKey(Alice_keyPair.privateKey.toString("hex"), 'hex'); | |
var secretA = ecdhA.computeSecret(pubB, 'hex').toString('hex'); | |
console.log("secretA", secretA) | |
var pubA = new Buffer(Alice_keyPair.publicKey.toString("hex"), 'hex'); | |
var ecdhB = crypto.createECDH('secp256k1'); | |
ecdhB.generateKeys('hex', 'compressed'); | |
ecdhB.setPrivateKey(Bob_keyPair.privateKey.toString("hex"), 'hex'); | |
var secretB = ecdhB.computeSecret(pubA, 'hex').toString('hex'); | |
console.log("secretB", secretB) | |
//Alice encrypts message to Bob using AES | |
var cipher = crypto.createCipher('aes-256-ctr', secretA); | |
var ecrypted = cipher.update(text, 'utf8', 'hex'); | |
ecrypted += cipher.final('hex'); | |
console.log(ecrypted) | |
var decipher = crypto.createCipher('aes-256-ctr', secretB); | |
var Dcrypted = decipher.update(ecrypted, 'hex', 'utf8'); | |
Dcrypted += decipher.final('utf8'); | |
console.log(Dcrypted) | |
} | |
doVerify(); | |
doCryptoTest() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment