<p>if you want to convert a compressed WIF to an uncompressed WIF, you would need to:</p>
<ol>
<li>decode the compressed WIF</li>
<li>extract the private key</li>
<li>remove the suffix</li>
<li>recompute the checksum</li>
<li>encode it back in base58</li>
</ol>
<p>Here's one way you can implement it:</p>
<pre><code>def decompress_WIF(compressed_wif):
    hex_data = binascii.b2a_hex(base58.b58decode(compressed_wif)).decode()
    if len(hex_data) != 76:
        raise ValueError(&quot;Invalid length for a compressed WIF&quot;)
    prefix = hex_data[:2]
    suffix = hex_data[-10:-8]
    original_checksum = hex_data[-8:]
    xpriv = hex_data[2:-10]
    if suffix != &quot;01&quot;:
        raise ValueError(&quot;The provided WIF is not a compressed key&quot;)
    if checksum(prefix + xpriv + suffix) != original_checksum:
        raise ValueError(&quot;Invalid checksum in the provided WIF&quot;)
    new_checksum = checksum(prefix + xpriv)
    uncompressed_wif = base58.b58encode(binascii.a2b_hex(prefix + xpriv + new_checksum)).decode()
    return uncompressed_wif
</code></pre>
<p>Note that this code does not check the version byte at all</p>

CC BY-SA 4.0
2023-09-17T16:18:45.197Z
It complains about `NameError: name 'checksum' is not defined`
uak
CC BY-SA 4.0
2023-09-17T16:56:12.563Z
@uak Of course, you need to define it!
You can recode the checksum function, here's one example:
`
def checksum(data):
    return sha256(sha256(binascii.a2b_hex(data)).digest()).hexdigest()[:8]`
leslie
CC BY-SA 4.0
2023-09-17T00:00:14.630Z
2023-09-17T00:00:14.630Z
leslie
<p>I'm trying to convert a compressed WIF generated by <a href="https://github.com/fortesp/bitcoinaddress" rel="nofollow noreferrer">bitcoinaddress</a> python Library but I couldn't find a function to do the task so I collected one. I would like someone to review my code and maybe remove unnecessary steps and enhance it.</p>
<pre class="lang-python prettyprint-override"><code># Script to convert compressed WIF to private key then to uncompressed WIF.

import hashlib
import base58
import binascii
from binascii import hexlify, unhexlify


def doubleSha256(hex):
    &quot;&quot;&quot;Double sha256 function&quot;&quot;&quot;
    bin = binascii.unhexlify(hex)
    # perform SHA-256 hash on the private_key
    hash_1st = hashlib.sha256(bin).digest()
    # perform SHA-256 on the previous SHA-256 hash
    hash_2nd = hashlib.sha256(hash_1st).digest()
    return binascii.hexlify(hash_2nd)


# Testnet WIF
private_key_WIF = &quot;cVbZ8ovhye9AoAHFsqobCf7LxbXDAECy9Kb8TZdfsDYMZGBUyCnm&quot;  # compressed
# ~ private_key_WIF_uncompressed = &quot;93QEdCzUrzMRsnbx2YuF6MsNjxQA6iWSrv9e2wX4NM4UmYzUsLn&quot;

first_decode = base58.b58decode(private_key_WIF)
private_key_full = binascii.hexlify(first_decode)

# Check if the key is mainnet or testnet
if private_key_full.startswith(b&quot;80&quot;):
    testnet = False
elif private_key_full.startswith(b&quot;ef&quot;):
    testnet = True

private_key_full = private_key_full[2:-8]

if private_key_full.endswith(b&quot;01&quot;):
    private_key_full = private_key_full[:-2]


if not testnet:
    # prepended mainnet version byte to private key
    text = b&quot;80&quot; + private_key_full
elif testnet:
    # prepended testnet version byte to private key
    text = b&quot;ef&quot; + private_key_full

# Perfrom double sha256 on string
double_sha256 = doubleSha256(text)


# create a checksum using the first 4 bytes of the previous SHA-256 hash
checksum = double_sha256[0:8]

# append the 4 checksum bytes to the private_key
private_key_with_checksum = text+checksum

# convert mainnet_private_key + checksum into base58 encoded string
uncompressed_wif = base58.b58encode(binascii.unhexlify(private_key_with_checksum))
print(f&quot;{uncompressed_wif=}&quot;)
</code></pre>
<p>Resources:</p>
<ul>
<li>People having issues with converting WIF to uncompressed WIF <a href="https://github.com/fortesp/bitcoinaddress/issues/21" rel="nofollow noreferrer">https://github.com/fortesp/bitcoinaddress/issues/21</a></li>
<li><a href="https://www.herongyang.com/Bitcoin/Block-Data-Calculate-Double-SHA256-with-Python.html" rel="nofollow noreferrer">https://www.herongyang.com/Bitcoin/Block-Data-Calculate-Double-SHA256-with-Python.html</a></li>
<li><a href="https://bitcoin.stackexchange.com/questions/17232/how-to-uncompress-a-compressed-private-key">How to uncompress a compressed private key?</a></li>
<li><a href="https://en.bitcoin.it/wiki/Wallet_import_format" rel="nofollow noreferrer">https://en.bitcoin.it/wiki/Wallet_import_format</a></li>
<li><a href="https://bitcoin.stackexchange.com/questions/9244/private-key-to-wif">Private key to WIF</a></li>
<li><a href="https://bitcoin.stackexchange.com/questions/58114/convert-wif-to-private-key">Convert WIF to private key</a></li>
</ul>

<python><private-key-import><wallet-import-format>
Converting compressed WIF to uncompressed WIF so it works with bitcoinaddress Python library
uak
# Converting compressed WIF to uncompressed WIF so it works with bitcoinaddress Python library

(Asked by: username_0 on 2023-09-05)

I'm trying to convert a compressed WIF generated by bitcoinaddress python Library but I couldn't find a function to do the task so I collected one. I would like someone to review my code and maybe remove unnecessary steps and enhance it.
# Script to convert compressed WIF to private key then to uncompressed WIF.

import hashlib
import base58
import binascii
from binascii import hexlify, unhexlify


def doubleSha256(hex):
    """Double sha256 function"""
    bin = binascii.unhexlify(hex)
    # perform SHA-256 hash on the private_key
    hash_1st = hashlib.sha256(bin).digest()
    # perform SHA-256 on the previous SHA-256 hash
    hash_2nd = hashlib.sha256(hash_1st).digest()
    return binascii.hexlify(hash_2nd)


# Testnet WIF
private_key_WIF = "cVbZ8ovhye9AoAHFsqobCf7LxbXDAECy9Kb8TZdfsDYMZGBUyCnm"  # compressed
# ~ private_key_WIF_uncompressed = "93QEdCzUrzMRsnbx2YuF6MsNjxQA6iWSrv9e2wX4NM4UmYzUsLn"

first_decode = base58.b58decode(private_key_WIF)
private_key_full = binascii.hexlify(first_decode)

# Check if the key is mainnet or testnet
if private_key_full.startswith(b"80"):
    testnet = False
elif private_key_full.startswith(b"ef"):
    testnet = True

private_key_full = private_key_full[2:-8]

if private_key_full.endswith(b"01"):
    private_key_full = private_key_full[:-2]


if not testnet:
    # prepended mainnet version byte to private key
    text = b"80" + private_key_full
elif testnet:
    # prepended testnet version byte to private key
    text = b"ef" + private_key_full

# Perfrom double sha256 on string
double_sha256 = doubleSha256(text)


# create a checksum using the first 4 bytes of the previous SHA-256 hash
checksum = double_sha256[0:8]

# append the 4 checksum bytes to the private_key
private_key_with_checksum = text+checksum

# convert mainnet_private_key + checksum into base58 encoded string
uncompressed_wif = base58.b58encode(binascii.unhexlify(private_key_with_checksum))
print(f"{uncompressed_wif=}")

Resources:

People having issues with converting WIF to uncompressed WIF https://github.com/fortesp/bitcoinaddress/issues/21
https://www.herongyang.com/Bitcoin/Block-Data-Calculate-Double-SHA256-with-Python.html
How to uncompress a compressed private key?
https://en.bitcoin.it/wiki/Wallet_import_format
Private key to WIF
Convert WIF to private key

**Tags:** python, private-key-import, wallet-import-format


---

## 1 answers

### Answer by username_1 on 2023-09-17. Score: 0

if you want to convert a compressed WIF to an uncompressed WIF, you would need to:

decode the compressed WIF
extract the private key
remove the suffix
recompute the checksum
encode it back in base58

Here's one way you can implement it:
def decompress_WIF(compressed_wif):
    hex_data = binascii.b2a_hex(base58.b58decode(compressed_wif)).decode()
    if len(hex_data) != 76:
        raise ValueError("Invalid length for a compressed WIF")
    prefix = hex_data[:2]
    suffix = hex_data[-10:-8]
    original_checksum = hex_data[-8:]
    xpriv = hex_data[2:-10]
    if suffix != "01":
        raise ValueError("The provided WIF is not a compressed key")
    if checksum(prefix + xpriv + suffix) != original_checksum:
        raise ValueError("Invalid checksum in the provided WIF")
    new_checksum = checksum(prefix + xpriv)
    uncompressed_wif = base58.b58encode(binascii.a2b_hex(prefix + xpriv + new_checksum)).decode()
    return uncompressed_wif

Note that this code does not check the version byte at all

> **Comments:**
> **username_0**: It complains about `NameError: name 'checksum' is not defined`  
> **username_1**: @username_0 Of course, you need to define it! You can recode the checksum function, here's one example: ` def checksum(data):     return sha256(sha256(binascii.a2b_hex(data)).digest()).hexdigest()[:8]`  

---

