Bitcoinjs-lib: how to get a public key?

Created on 15 Dec 2016  路  7Comments  路  Source: bitcoinjs/bitcoinjs-lib

The basic examples show how to get the private key/public address, but how do I get the public key?

how to / question / docs

Most helpful comment

let kp = bitcoin.ECPair.makeRandom()

console.log(kp.getPublicKeyBuffer())
// this is the serialized public key,  potentially ready for use in other ECC libraries

console.log(kp.Q)
// the ECPoint used by bitcoinjs-lib... see https://www.npmjs.com/package/ecurve

All 7 comments

@harryio what do you need the public key for? (I ask simply so I can determine what format you might need)

@dcousens um I want to encrypt some data, so that it could be later decrypted using the private key..

let kp = bitcoin.ECPair.makeRandom()

console.log(kp.getPublicKeyBuffer())
// this is the serialized public key,  potentially ready for use in other ECC libraries

console.log(kp.Q)
// the ECPoint used by bitcoinjs-lib... see https://www.npmjs.com/package/ecurve

thanks, please close the issue

@dcousens can I convert the buffer to hex and store it in the database for later use?

harryio of course you can, check buffer docs at nodejs.org

let kp = bitcoin.ECPair.makeRandom()
console.log(kp.getPublicKeyBuffer().toString('hex'))

@harrio: > um I want to encrypt some data, so that it could be later decrypted using the private key..

What are you trying to do here? There are more sound ways to implement encryption (not that you should) than by using this libraries public/private key, it already seems the wrong way to go.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ghost picture ghost  路  3Comments

hoshsadiq picture hoshsadiq  路  3Comments

thrastarson picture thrastarson  路  3Comments

namnv04 picture namnv04  路  3Comments

Beardcoding picture Beardcoding  路  3Comments