Created
May 17, 2019 18:30
-
-
Save max-mapper/5694e846b34e97c64f60419bb9712686 to your computer and use it in GitHub Desktop.
secp256k1 encrypt/decrypt with bip32 keys
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 KeyEncoder = require('key-encoder') | |
var VirgilCrypto = require('virgil-crypto').VirgilCrypto | |
var HDKey = require('hdkey') | |
const secp256k1 = require('secp256k1') | |
var keyEncoder = new KeyEncoder('secp256k1') | |
var hdKey = HDKey.fromMasterSeed(Buffer.from(SEED, 'hex')) | |
var childKey = hdKey.derive(PATH) | |
var childPrivateKey = childKey.privateKey.toString('hex') | |
var childPublicKey = secp256k1.publicKeyConvert(childKey.publicKey, false).toString('hex') | |
var pemPublicKey = keyEncoder.encodePublic(childPublicKey, 'raw', 'pem') | |
var pemPrivateKey = keyEncoder.encodePrivate(childPrivateKey, 'raw', 'pem') | |
var pubKey64 = new Buffer(pemPublicKey).toString('base64') | |
var privKey64 = new Buffer(pemPrivateKey).toString('base64') | |
const vc = new VirgilCrypto() | |
var vcPriv = vc.importPrivateKey(privKey64) | |
var vcPub = vc.importPublicKey(pubKey64) | |
var enc = vc.encrypt('hello', vcPub) | |
console.log(enc) | |
var dec = vc.decrypt(enc, vcPriv) | |
console.log(dec.toString()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment