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:
- decode using Base58Check
- drop 1st byte
- 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
- Base58Check decoded:
- Drop 1st byte:
Given a public key, e.g.
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:
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'