-
-
Save circulosmeos/ef6497fd3344c2c2508b92bb9831173f to your computer and use it in GitHub Desktop.
#!/usr/bin/env python | |
# https://en.bitcoin.it/wiki/Protocol_documentation#Addresses | |
import hashlib | |
import base58 | |
# ECDSA bitcoin Public Key | |
pubkey = '0450863ad64a87ae8a2fe83c1af1a8403cb53f53e486d8511dad8a04887e5b23522cd470243453a299fa9e77237716103abc11a1df38855ed6f2ee187e9c582ba6' | |
# See 'compressed form' at https://en.bitcoin.it/wiki/Protocol_documentation#Signatures | |
compress_pubkey = False | |
def hash160(hex_str): | |
sha = hashlib.sha256() | |
rip = hashlib.new('ripemd160') | |
sha.update(hex_str) | |
rip.update( sha.digest() ) | |
print ( "key_hash = \t" + rip.hexdigest() ) | |
return rip.hexdigest() # .hexdigest() is hex ASCII | |
if (compress_pubkey): | |
if (ord(bytearray.fromhex(pubkey[-2:])) % 2 == 0): | |
pubkey_compressed = '02' | |
else: | |
pubkey_compressed = '03' | |
pubkey_compressed += pubkey[2:66] | |
hex_str = bytearray.fromhex(pubkey_compressed) | |
else: | |
hex_str = bytearray.fromhex(pubkey) | |
# Obtain key: | |
key_hash = '00' + hash160(hex_str) | |
# Obtain signature: | |
sha = hashlib.sha256() | |
sha.update( bytearray.fromhex(key_hash) ) | |
checksum = sha.digest() | |
sha = hashlib.sha256() | |
sha.update(checksum) | |
checksum = sha.hexdigest()[0:8] | |
print ( "checksum = \t" + sha.hexdigest() ) | |
print ( "key_hash + checksum = \t" + key_hash + ' ' + checksum ) | |
print ( "bitcoin address = \t" + (base58.b58encode( bytes(bytearray.fromhex(key_hash + checksum)) )).decode('utf-8') ) |
love it
I have a little discrepancy on final Wallet and I didn't find the divergence between your solution and me:
You have a Pubk = 0450863ad64a87ae8a2fe83c1af1a8403cb53f53e486d8511dad8a04887e5b23522cd470243453a299fa9e77237716103abc11a1df38855ed6f2ee187e9c582ba6
You have a final address: 16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM
But with same Pubk y generate this address: 1PMycacnJaSqwwJqjawXBErnLsZ7RkXUAs
The code that I'm computing is this:
public_key = '0450863ad64a87ae8a2fe83c1af1a8403cb53f53e486d8511dad8a04887e5b23522cd470243453a299fa9e77237716103abc11a1df38855ed6f2ee187e9c582ba6'
Checking if the last byte is odd or even
if (ord(bytearray.fromhex(public_key[-2:])) % 2 == 0):
public_key_compressed = '02'
else:
public_key_compressed = '03'
Add bytes 0x02 to the X of the key if even or 0x03 if odd
public_key_compressed += public_key[2:66]
Converting to bytearray for SHA-256 hashing
hex_str = bytearray.fromhex(public_key_compressed)
sha = hashlib.sha256()
sha.update(hex_str)
sha.hexdigest() # .hexdigest() is hex ASCII
rip = hashlib.new('ripemd160')
rip.update(sha.digest())
key_hash = rip.hexdigest()
modified_key_hash = "00" + key_hash
sha = hashlib.sha256()
hex_str = bytearray.fromhex(modified_key_hash)
sha.update(hex_str)
sha_2 = hashlib.sha256()
sha_2.update(sha.digest())
checksum = sha_2.hexdigest()[:8]
byte_25_address = modified_key_hash + checksum
Solution1: address = base58.b58encode(bytes(bytearray.fromhex(byte_25_address))).decode('utf-8')
Solution2: key_bytes = codecs.decode(modified_key_hash, 'hex')
address = base58.b58encode_check(key_bytes).decode('utf-8')
But none of these are the same that generate your code
I have a little discrepancy on final Wallet and I didn't find the divergence between your solution and me:
You have a Pubk = 0450863ad64a87ae8a2fe83c1af1a8403cb53f53e486d8511dad8a04887e5b23522cd470243453a299fa9e77237716103abc11a1df38855ed6f2ee187e9c582ba6 You have a final address: 16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM
But with same Pubk y generate this address: 1PMycacnJaSqwwJqjawXBErnLsZ7RkXUAs
...
Checking if the last byte is odd or even
if (ord(bytearray.fromhex(public_key[-2:])) % 2 == 0): public_key_compressed = '02' else: public_key_compressed = '03'
Add bytes 0x02 to the X of the key if even or 0x03 if odd
public_key_compressed += public_key[2:66]
Converting to bytearray for SHA-256 hashing
hex_str = bytearray.fromhex(public_key_compressed)
...
I know it is old but is worth mentioning that the public key is already uncompressed format (04 +point.X+point.Y). thus you should skip the theses steps "Checking if the last byte is odd or even" and "Add bytes 0x02 to the X of the key if even or 0x03 if odd" so you just use directly without adding 02 or 03 to it.
Compressed public keys have 02 or 03 depending if the point.Y is even or odd. But in uncompressed format we add 04. that is why it already has 04. In the code above line we have compress_pubkey = False
. meaning that is uncompressed. and a verification at line 22: if (compress_pubkey):
and then hex_str = bytearray.fromhex(pubkey)
.
you are trying to generate a key with 02 or 03 + 04 at the beginning, that is why the return is wrong. Your hex_str should get from bytearray.fromhex(pubkey)
.
Thanks for your comment!
import os
import hashlib
import base58
Função para gerar um endereço Bitcoin a partir de uma chave privada (simplificada)
def private_key_to_btc_address(private_key_hex):
# Hash SHA-256 da chave privada
sha256_key = hashlib.sha256(bytes.fromhex(private_key_hex)).digest()
# RIPEMD-160 do hash SHA-256
ripemd160_key = hashlib.new('ripemd160', sha256_key).digest()
# Prefixo de rede Bitcoin (0x00 para Mainnet)
prefixed_key = b'\x00' + ripemd160_key
# Duplo SHA-256 para checksum
checksum = hashlib.sha256(hashlib.sha256(prefixed_key).digest()).digest()[:4]
# Endereço final
binary_address = prefixed_key + checksum
address = base58.b58encode(binary_address).decode('utf-8')
return address
Endereço-alvo para comparação
target_address = "14u4nA5sugaswb6SZgn5av2vuChdMnD9E5"
Simulação de força bruta com geração aleatória de chaves
attempts = 1000 # Número de tentativas simuladas
found = False
for i in range(attempts):
# Gerar uma chave privada aleatória de 32 bytes
private_key = os.urandom(32).hex()
generated_address = private_key_to_btc_address(private_key)
if generated_address == target_address:
found = True
result = (private_key, generated_address)
break
found, result if found else "Nenhuma correspondência encontrada nas tentativas simuladas."
awesome