1

I had created a script that creates a bitcoin private key, public key and address, for checking if all works fine I create one and check the private key on https://walletgenerator.net/, and it outputs a different address, every time I do that my script create a different addres that what walletgenerator returns.

Python script:

import secrets

import hashlib import ecdsa import codecs import base64

class Address(): def init(self): self.private_key = self.create_private_key()
self.public_key = self.private_key_to_public(self.private_key) self.address = self.public_key_to_address(self.public_key)

@staticmethod
def create_private_key():
    bits = secrets.randbits(256)
    bits_hex = hex(bits)
    private_key = bits_hex[2:]
    return private_key


@staticmethod
def private_key_to_public(private_key):
    verify_key = ecdsa.SigningKey.from_string(codecs.decode(private_key, "hex"), curve=ecdsa.SECP256k1).verifying_key
    key_hex = codecs.encode(verify_key.to_string(), "hex")
    bitcoin_type = b'04'
    public_key = bitcoin_type + key_hex
    return public_key
    

@staticmethod
def public_key_to_address(public_key):
    public_key_bytes = codecs.decode(public_key, "hex")
    
    sha256_bpk = hashlib.sha256(public_key_bytes)
    sha256_bpk_digest = sha256_bpk.digest()
    
    ripemd160_bpk = hashlib.new("ripemd160")
    ripemd160_bpk.update(sha256_bpk_digest)
    ripemd160_bpk_digest = ripemd160_bpk.digest()
    ripemd160_bpk_hex = codecs.encode(ripemd160_bpk_digest, "hex")
    network_byte = b'00'
    network_bitcoin_public_key = network_byte + ripemd160_bpk_hex

    network_bitcoin_public_key_bytes = codecs.decode(network_bitcoin_public_key, 'hex')
    # Double SHA256 to get checksum
    sha256_nbpk = hashlib.sha256(network_bitcoin_public_key_bytes)
    sha256_nbpk_digest = sha256_nbpk.digest()
    sha256_2_nbpk = hashlib.sha256(sha256_nbpk_digest)
    sha256_2_nbpk_digest = sha256_2_nbpk.digest()
    sha256_2_hex = codecs.encode(sha256_2_nbpk_digest, "hex")
    checksum = sha256_2_hex[:8]

    address_hex = (network_bitcoin_public_key + checksum).decode('utf-8')
    return Address.base58(address_hex)

@staticmethod
def base58(address_hex):
    alphabet = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
    b58_string = ''
    # Get the number of leading zeros
    leading_zeros = len(address_hex) - len(address_hex.lstrip('0'))
    # Convert hex to decimal
    address_int = int(address_hex, 16)
    # Append digits to the start of string
    while address_int > 0:
        digit = address_int % 58
        digit_char = alphabet[digit]
        b58_string = digit_char + b58_string
        address_int //= 58
    # Add ‘1’ for each 2 leading zeros
    ones = leading_zeros // 2
    for _ in range(ones):
        b58_string = '1' + b58_string
    return b58_string

Doing

foo = Address()
print(foo.private_key, foo.public_key, foo.address)

returns

# 5598c164e8bcec667589267cdaf0f14a6b9399ae0b2a6b10972602a6feaf682d 
# b'04cbeefab6ecfa84f5f120503de4574952f2d1fa5b11b8f6c80a13df02e6512090e6fede6dde3e65256c484162e38 
# 39cf599c3d08a236432e8e307784b741ffca5' 18NFnELB9YpRiEbLMmRLKymYpvxC9yzkC2

And then, if I check the private key 5598c164e8bcec667589267cdaf0f14a6b9399ae0b2a6b10972602a6feaf682d on https://walletgenerator.net/, it returns:

Public address: 1LBLgjq3ntVdb8MqB41rHReWnrKgT41Lwv
Public address compressed: 1LBLgjq3ntVdb8MqB41rHReWnrKgT41Lwv
  • walletgenerator.net is probably a phishing site – Tony Sanak Jul 13 at 20:38
0

Your python scripts generate uncompressed public keys, and they seem to calculate correctly.
When I use my own scripts, the results are:

Private key: ---------------------------------------
5598c164e8bcec667589267cdaf0f14a6b9399ae0b2a6b10972602a6feaf682d
Private WIF: ---------------------------------------
Kz66jawQ5eb7pQYJx3tTNmFQBS46x1rg92To2js3bY1SKrPxVWvL
Public key: ----------------------------------------
04cbeefab6ecfa84f5f120503de4574952f2d1fa5b11b8f6c80a13df02e6512090e6fede6dde3e65256c484162e3839cf599c3d08a236432e8e307784b741ffca5
pubkeyhash: ----------------------------------------
50cdf408e049ddab4bd8ebda8a6500e93831a633
BTC Address: ---------------------------------------
18NFnELB9YpRiEbLMmRLKymYpvxC9yzkC2

No difference to your results.

But there seems to be a bug in walletgenerator.net's calculation:
They claim that the uncompressed address was the same as the compressed address, which cannot be correct. Which tab did you use on their site, was it Wallet Details?
As a compressed bitcoin address, I get 1KJKsAzDeH1127x6w8e2xZyzRdvFkiLhme when I use my own scripts, and my Electrum client confirms that result (when I use the private key import feature).

All that does not match the results of walletgenerator.net, so my conclusion is that their site is buggy.

| improve this answer | |
  • Thank you @citizenfu, yes, I tried to check the values on Wallet Details. It's a good thing hear from you that it is not my script what's wrong – Stack Jul 13 at 13:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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