<p><strong>Update</strong>:</p>

<p>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 <a href="https://en.bitcoin.it/wiki/Wallet_import_format" rel="nofollow noreferrer">Wallet Input Format (WIF)</a> which is used to transfer private keys between software applications. To convert to hex:</p>

<ol>
<li>decode using Base58Check</li>
<li>drop 1st byte</li>
<li>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</li>
</ol>

<p>Example:</p>

<p>WIF: <code>5J2zgrqrMFcnWALe5foVPzaA8VLNJV8zoZn37WfQCu8DYDCVmXE</code></p>

<ol>
<li>Base58Check decoded: <code>801cdb52e9a9fa3e089c7b8713218df4a7924b2109b159df480960ddfe73e221c3</code></li>
<li>Drop 1st byte:
<code>1cdb52e9a9fa3e089c7b8713218df4a7924b2109b159df480960ddfe73e221c3</code></li>
<li>N/A</li>
</ol>

<p><strong>old</strong>:</p>

<p>Given a public key, e.g. </p>

<p><code>04a097026e876544a0e40f9ca836435560af4470e161bf60c23465dcb3151c947d1cbe052875211972107e25fca8dd939f1c6e749a43862673ec5cf7a8567f2d95</code></p>

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

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

<p><code>03a097026e876544a0e40f9ca836435560af4470e161bf60c23465dcb3151c947d</code></p>

<h2>Javascript</h2>

<p>You can use <a href="https://github.com/bitcoinjs/bitcoinjs-lib" rel="nofollow noreferrer"><code>bitcoinjs-lib</code></a> to convert:</p>

<pre><code>const bitcoin = require('bitcoinjs-lib')

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

'03a097026e876544a0e40f9ca836435560af4470e161bf60c23465dcb3151c947d'
</code></pre>

CC BY-SA 4.0
2019-07-26T20:49:18.260Z
OP was asking about converting private keys.
Pieter Wuille
CC BY-SA 4.0
2019-07-26T20:24:37.080Z
2019-07-27T20:27:38.803Z
2019-07-27T20:27:38.803Z
JBaczuk
<p>I forked a pretty old brain wallet generator: <a href="https://dvdbng.github.io/memwallet/" rel="nofollow noreferrer">https://dvdbng.github.io/memwallet/</a> -- 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? </p>

CC BY-SA 4.0
2019-07-27T01:34:52.490Z
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](https://en.bitcoin.it/wiki/Wallet_import_format) adds a byte '01' to the end of an encoded private key to indicate its public key should be compressed. Related (but no code) https://bitcoin.stackexchange.com/questions/43090/why-are-compressed-private-keys-typically-longer-than-their-uncompressed-counter
dave_thompson_085
<private-key><segregated-witness><javascript>
Javascript library or code that can convert an "uncompressed" bitcoin private key to a compressed one
Patoshi パトシ
# Javascript library or code that can convert an "uncompressed" bitcoin private key to a compressed one

(Asked by: username_0 on 2019-07-26)

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?

**Tags:** private-key, segregated-witness, javascript

> **Comments:**
> **username_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](https://en.bitcoin.it/wiki/Wallet_import_format) adds a byte '01' to the end of an encoded private key to indicate its public key should be compressed. Related (but no code) https://bitcoin.stackexchange.com/questions/43090/why-are-compressed-private-keys-typically-longer-than-their-uncompressed-counter  

---

## 1 answers

### Answer by username_2 on 2019-07-26. Score: 1 | Accepted answer

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:

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

Example:
WIF: 5J2zgrqrMFcnWALe5foVPzaA8VLNJV8zoZn37WfQCu8DYDCVmXE

Base58Check decoded: 801cdb52e9a9fa3e089c7b8713218df4a7924b2109b159df480960ddfe73e221c3
Drop 1st byte:
1cdb52e9a9fa3e089c7b8713218df4a7924b2109b159df480960ddfe73e221c3
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², 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'

> **Comments:**
> **username_3**: OP was asking about converting private keys.  

---

