0

I forked a pretty old brain wallet generator: https://dvdbng.github.io/memwallet/ -- Currently it generates uncompressed bitcoin private keys. Is there a javascript library that can convert an uncompressed bitcoin private key to a compressed one? Or what is the required fundamental knowledge to know in order to compress a bitcoin private key?

1
  • ECC/Bitcoin private keys are neither compressed nor uncompressed. They are sometimes stored with a flag indicating whether the public key will be compressed; in particular WIF adds a byte '01' to the end of an encoded private key to indicate its public key should be compressed. Related (but no code) bitcoin.stackexchange.com/questions/43090/… Jul 27, 2019 at 1:34

1 Answer 1

1

Update:

Oops sorry I read "public key". A private key can be converted to a public key that is either compressed on uncompressed, but raw EC private keys are always 32 bytes. That website appears to be generating what is called Wallet Input Format (WIF) which is used to transfer private keys between software applications. To convert to hex:

  1. decode using Base58Check
  2. drop 1st byte
  3. If the WIF started with K or L, then also drop the last byte (0x01) which denotes the private key corresponds to a compressed public key

Example:

WIF: 5J2zgrqrMFcnWALe5foVPzaA8VLNJV8zoZn37WfQCu8DYDCVmXE

  1. Base58Check decoded: 801cdb52e9a9fa3e089c7b8713218df4a7924b2109b159df480960ddfe73e221c3
  2. Drop 1st byte: 1cdb52e9a9fa3e089c7b8713218df4a7924b2109b159df480960ddfe73e221c3
  3. N/A

old:

Given a public key, e.g.

04a097026e876544a0e40f9ca836435560af4470e161bf60c23465dcb3151c947d1cbe052875211972107e25fca8dd939f1c6e749a43862673ec5cf7a8567f2d95

The 0x04 designates this as an uncompressed public key, where the next 32 bytes are the x value and the final 32 bytes are the y value of the point on the elliptic curve, respectfully.

To convert from an uncompressed public key to a compressed public key, you can omit the y value because the y value can be solved for using the equation of the elliptic curve: y² = x³ + 7, given x. Since the equation solves for , y could be either positive or negative. So, 0x02 is prepended for positive y values, and 0x03 is prepended for negative ones. If the y coordinate is even, then it corresponds to a positive number. If odd, then it is negative. Since the y value ends in 0xee, which is even, and therefore positive, the compressed version of the public key becomes:

03a097026e876544a0e40f9ca836435560af4470e161bf60c23465dcb3151c947d

Javascript

You can use bitcoinjs-lib to convert:

const bitcoin = require('bitcoinjs-lib')

const pubkeyBuf = Buffer.from('04a097026e876544a0e40f9ca836435560af4470e161bf60c23465dcb3151c947d1cbe052875211972107e25fca8dd939f1c6e749a43862673ec5cf7a8567f2d95', 'hex')
const pubkey = bitcoin.ECPair.fromPublicKey(pubkeyBuf)
pubkey.publicKey.toString('hex')

'03a097026e876544a0e40f9ca836435560af4470e161bf60c23465dcb3151c947d'
1
  • OP was asking about converting private keys. Jul 26, 2019 at 20:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.