<p>Some key points here:</p>
<ul>
<li>The private key cannot reach 2**256 because you must respect the order of the curve, which is n = 0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141. Set the interval to [1, n).</li>
<li><em>&quot;add prefix &quot;00&quot; to uncompressed address to indicate it is an uncompressed address&quot;</em>. It's not because of that. It's to indicate that the address is used for mainnet. If testnet, use 0x6f. (Only p2pkh)</li>
<li>The uncompressed public key has the format <strong>'04' + x + y</strong> = 65 bytes = 130 hex characters. I haven't seen any mention of it in your code. Make sure the <em>variable public_key_hex</em> follows the format.</li>
<li>The compressed public key has the format <strong>'02' + x</strong> or <strong>'03' + x</strong>, depending on the y is even or odd, respectively. (33 bytes = 66 hex chars)</li>
</ul>

CC BY-SA 4.0
2022-12-27T15:41:17.420Z
2022-12-27T15:41:17.420Z
phantom
<p>I'm trying to write a script that can generate a private key and the compressed and uncompressed addresses. It seems to work however the private key doesn't correspond to the addresses generated. Can't seem to find where I'm going wrong though...</p>
<pre class="lang-python prettyprint-override"><code>
import hashlib
import base58
import secrets
import ecdsa

# generate private key using secure random number generator
private_key = secrets.randbelow(2**256)

# convert private key to hexadecimal format
private_key_hex = hex(private_key)[2:]

# generate public key using private key and Elliptic Curve Digital Signature Algorithm (ECDSA)
public_key = ecdsa.SigningKey.from_string(bytes.fromhex(private_key_hex), curve=ecdsa.SECP256k1).verifying_key

# convert public key to hexadecimal format
public_key_hex = public_key.to_string().hex()

# generate uncompressed bitcoin address by hashing public key with SHA-256 and RIPEMD-160
uncompressed_address = hashlib.new('ripemd160', hashlib.sha256(bytes.fromhex(public_key_hex)).digest()).hexdigest()

# add prefix &quot;00&quot; to uncompressed address to indicate it is an uncompressed address
uncompressed_address = &quot;00&quot; + uncompressed_address

# generate checksum by hashing address with SHA-256 twice
checksum = hashlib.sha256(hashlib.sha256(bytes.fromhex(uncompressed_address)).digest()).hexdigest()[:8]

# add checksum to address
uncompressed_address += checksum

# encode address in base58 format
uncompressed_address = base58.b58encode(bytes.fromhex(uncompressed_address))

# generate compressed bitcoin address by adding prefix &quot;03&quot; to public key if it is odd, or &quot;02&quot; if it is even
if public_key_hex[-1] in [&quot;1&quot;, &quot;3&quot;, &quot;5&quot;, &quot;7&quot;, &quot;9&quot;, &quot;b&quot;, &quot;d&quot;, &quot;f&quot;]:
    compressed_address = &quot;03&quot; + public_key_hex[:66]
else:
    compressed_address = &quot;02&quot; + public_key_hex[:66]

# hash compressed address with SHA-256 and RIPEMD-160
compressed_address = hashlib.new('ripemd160', hashlib.sha256(bytes.fromhex(compressed_address)).digest()).hexdigest()

# add prefix &quot;00&quot; to compressed address to indicate it is a compressed address
compressed_address = &quot;00&quot; + compressed_address

# generate checksum by hashing address with SHA-256 twice
checksum = hashlib.sha256(hashlib.sha256(bytes.fromhex(compressed_address)).digest()).hexdigest()[:8]

# add checksum to address
compressed_address += checksum

# encode address in base58 format
compressed_address = base58.b58encode(bytes.fromhex(compressed_address))

print(&quot;Private key:&quot;, private_key_hex)
print(&quot;Uncompressed address:&quot;, uncompressed_address)
print(&quot;Compressed address:&quot;, compressed_address)
</code></pre>

<bitcoin-core><private-key><ecdsa><python-bitcoinlib>
Keep getting wrong bitcoin addresses
fredsta98
# Keep getting wrong bitcoin addresses

(Asked by: username_0 on 2022-12-27)

I'm trying to write a script that can generate a private key and the compressed and uncompressed addresses. It seems to work however the private key doesn't correspond to the addresses generated. Can't seem to find where I'm going wrong though...

import hashlib
import base58
import secrets
import ecdsa

# generate private key using secure random number generator
private_key = secrets.randbelow(2**256)

# convert private key to hexadecimal format
private_key_hex = hex(private_key)[2:]

# generate public key using private key and Elliptic Curve Digital Signature Algorithm (ECDSA)
public_key = ecdsa.SigningKey.from_string(bytes.fromhex(private_key_hex), curve=ecdsa.SECP256k1).verifying_key

# convert public key to hexadecimal format
public_key_hex = public_key.to_string().hex()

# generate uncompressed bitcoin address by hashing public key with SHA-256 and RIPEMD-160
uncompressed_address = hashlib.new('ripemd160', hashlib.sha256(bytes.fromhex(public_key_hex)).digest()).hexdigest()

# add prefix "00" to uncompressed address to indicate it is an uncompressed address
uncompressed_address = "00" + uncompressed_address

# generate checksum by hashing address with SHA-256 twice
checksum = hashlib.sha256(hashlib.sha256(bytes.fromhex(uncompressed_address)).digest()).hexdigest()[:8]

# add checksum to address
uncompressed_address += checksum

# encode address in base58 format
uncompressed_address = base58.b58encode(bytes.fromhex(uncompressed_address))

# generate compressed bitcoin address by adding prefix "03" to public key if it is odd, or "02" if it is even
if public_key_hex[-1] in ["1", "3", "5", "7", "9", "b", "d", "f"]:
    compressed_address = "03" + public_key_hex[:66]
else:
    compressed_address = "02" + public_key_hex[:66]

# hash compressed address with SHA-256 and RIPEMD-160
compressed_address = hashlib.new('ripemd160', hashlib.sha256(bytes.fromhex(compressed_address)).digest()).hexdigest()

# add prefix "00" to compressed address to indicate it is a compressed address
compressed_address = "00" + compressed_address

# generate checksum by hashing address with SHA-256 twice
checksum = hashlib.sha256(hashlib.sha256(bytes.fromhex(compressed_address)).digest()).hexdigest()[:8]

# add checksum to address
compressed_address += checksum

# encode address in base58 format
compressed_address = base58.b58encode(bytes.fromhex(compressed_address))

print("Private key:", private_key_hex)
print("Uncompressed address:", uncompressed_address)
print("Compressed address:", compressed_address)

**Tags:** bitcoin-core, private-key, ecdsa, python-bitcoinlib


---

## 1 answers

### Answer by username_1 on 2022-12-27. Score: 2 | Accepted answer

Some key points here:

* The private key cannot reach 2\*\*256 because you must respect the order of the curve, which is n = 0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141. Set the interval to [1, n).
* *"add prefix "00" to uncompressed address to indicate it is an uncompressed address"*. It's not because of that. It's to indicate that the address is used for mainnet. If testnet, use 0x6f. (Only p2pkh)
* The uncompressed public key has the format **'04' + x + y** = 65 bytes = 130 hex characters. I haven't seen any mention of it in your code. Make sure the *variable public\_key\_hex* follows the format.
* The compressed public key has the format **'02' + x** or **'03' + x**, depending on the y is even or odd, respectively. (33 bytes = 66 hex chars)


---

