Skip to content

Instantly share code, notes, and snippets.



Created May 17, 2019
What would you like to do?
secp256k1 encrypt/decrypt with bip32 keys
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)
var dec = vc.decrypt(enc, vcPriv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment