Open def1701 opened 1 year ago
The database we’re using includes bitcoin addresses in P2PKH (address starts with 1), P2SH (address start with 3), and P2WPKH (address start with bc1) formats.
The algorithm we’re using only generates P2PKH addresses and has been really optimized to do it as fast as possible. Since only addresses that start with 1 get generated, all the other bitcoin address types can be ignored when we load the database.
We could make an extension to include the other types. But P2PKH is considered the legacy address format that older forgotten whale bitcoin wallets are in. And doing the other formats would take a bunch of time
Got it. Thanks for the clarification and the program. Running great. I've added a few things to show progress that don't seem to slow anything down. Checked for various ways to speed it up, but it seems you've done a good job already. :-)
I figure I am guaranteed to find a code within the next 1000 million years.
Testing this: (sorry the formatting was lost in this post) The public_key_to_p2pkh_address function generates a P2PKH address by hashing the public key using RIPEMD-160, prefixing the resulting hash with '00', and then base58 encoding the result.
The public_key_to_p2sh_address function generates a P2SH address by creating a redeem script that pushes the hash of the public key onto the stack, and then base58 encoding the redeem script with a prefix of '05'.
The public_key_to_p2wpkh_address function generates a P2WPKH address by hashing the public key using RIPEMD-160, prefixing the resulting hash with '0014', and then base58 encoding the result with a prefix of 'bc'.
import binascii import hashlib import os import base58 from fastecdsa import keys, curve from bitcoinlib.encoding import b58encode_check from bitcoinlib.keys import HDKey, Key
def generate_private_key(): return binascii.hexlify(os.urandom(32)).decode('utf-8').upper()
def private_key_to_public_key(private_key, fastecdsa): if fastecdsa: key = keys.get_public_key(int('0x' + private_key, 0), curve.secp256k1) return '04' + (hex(key.x)[2:] + hex(key.y)[2:]).zfill(128) else: pk = Key(privkey=bytes.fromhex(private_key)) return pk.pubkey.serialize(compressed=False).hex()
def public_key_to_p2pkh_address(public_key): alphabet = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz' var = hashlib.new('ripemd160') encoding = binascii.unhexlify(public_key.encode()) var.update(hashlib.sha256(encoding).digest()) var_encoded = ('00' + var.hexdigest()).encode() digest = hashlib.sha256(binascii.unhexlify(var_encoded)).digest() var_hex = '00' + var.hexdigest() + hashlib.sha256(digest).hexdigest()[0:8] count = [char != '0' for char in var_hex].index(True) // 2 n = int(var_hex, 16) output = [] while n > 0: n, remainder = divmod(n, 58) output.append(alphabet[remainder]) for i in range(count): output.append(alphabet[0]) return ''.join(output[::-1])
def public_key_to_p2sh_address(public_key): redeem_script = '76a914' + hashlib.new('ripemd160', hashlib.sha256(binascii.unhexlify(public_key)).digest()).hexdigest() + '88ac' return b58encode_check(bytes.fromhex('05' + redeem_script)).decode('utf-8')
def public_key_to_p2wpkh_address(public_key): witness_program = hashlib.new('ripemd160', hashlib.sha256(binascii.unhexlify(public_key)).digest()).hexdigest() return b58encode_check(bytes.fromhex('0014' + witness_program)).decode('utf-8')
Y
In the "main", the section that creates the array ("database") is set to only pick up addresses that start with 1. if address.startswith('1'): database.add(address[-args['substring']:])
Found it wondering why the record count did not match the documentation. Easy to comment it out and use the entire data set.