Bitcoinjs-lib: How to extract `public key` (not address) ?

Created on 30 May 2017  路  3Comments  路  Source: bitcoinjs/bitcoinjs-lib

Hi Guys

I need to extract the public key for multisig address generation

How to do it within this library? or do you know other libraries which can extract the public key from private key?

how to / question / docs

Most helpful comment

@mixdev thanks!
I hope you don't mind, but I've copied the contents of your gist below:

/*  
    How to recreate a Bitcoin wallet address from a public key 
    Ref: https://github.com/bitcoinjs/bitcoinjs-lib/issues/590
*/

var bitcoin = require('bitcoinjs-lib')
var assert = require('assert')
var bigi = require('bigi')
var crypto = require('crypto')

/* New hash */
var hash = bitcoin.crypto.sha256('correct horse battery staple')

var d = bigi.fromBuffer(hash)

/* Create a new Eleptic Curve key pair from hash */
var keyPair = new bitcoin.ECPair(d)

/* Extract public key buffer(compressed) */
var publicKeyBuffer = keyPair.getPublicKeyBuffer()

/* Copy the (compressed) pub key from the ECPair we have created above */
var publicKey = bitcoin.ECPair.fromPublicKeyBuffer(publicKeyBuffer)

/* Create a new keypair from the copied public key */
var newKeyPair = new bitcoin.ECPair(null, publicKey.Q, { compressed: true })

/* The old address */
console.log(keyPair.getAddress())

/* should be same as the new address */
console.log(newKeyPair.getAddress()) 

This and the multi-sig example should be more than enough to do what @askucher asked.

All 3 comments

Were these examples as found in README helpful?

Basically, <keyPair>.getPublicKeyBuffer() should give the compressed public key buffer. Check this (rather useless otherwise) code for for a complete example https://gist.github.com/mixdev/0b3862ccd198a9598ebffe00befa3b1d @

@mixdev thanks!
I hope you don't mind, but I've copied the contents of your gist below:

/*  
    How to recreate a Bitcoin wallet address from a public key 
    Ref: https://github.com/bitcoinjs/bitcoinjs-lib/issues/590
*/

var bitcoin = require('bitcoinjs-lib')
var assert = require('assert')
var bigi = require('bigi')
var crypto = require('crypto')

/* New hash */
var hash = bitcoin.crypto.sha256('correct horse battery staple')

var d = bigi.fromBuffer(hash)

/* Create a new Eleptic Curve key pair from hash */
var keyPair = new bitcoin.ECPair(d)

/* Extract public key buffer(compressed) */
var publicKeyBuffer = keyPair.getPublicKeyBuffer()

/* Copy the (compressed) pub key from the ECPair we have created above */
var publicKey = bitcoin.ECPair.fromPublicKeyBuffer(publicKeyBuffer)

/* Create a new keypair from the copied public key */
var newKeyPair = new bitcoin.ECPair(null, publicKey.Q, { compressed: true })

/* The old address */
console.log(keyPair.getAddress())

/* should be same as the new address */
console.log(newKeyPair.getAddress()) 

This and the multi-sig example should be more than enough to do what @askucher asked.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

zhaozhiming picture zhaozhiming  路  3Comments

hoshsadiq picture hoshsadiq  路  3Comments

itsMikeLowrey picture itsMikeLowrey  路  3Comments

panpan2 picture panpan2  路  3Comments

dakk picture dakk  路  3Comments