Skip to content

Instantly share code, notes, and snippets.

@max-mapper
Created May 17, 2019 18:30
Show Gist options
  • Save max-mapper/5694e846b34e97c64f60419bb9712686 to your computer and use it in GitHub Desktop.
Save max-mapper/5694e846b34e97c64f60419bb9712686 to your computer and use it in GitHub Desktop.
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)
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