Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: rhog on August 31, 2024, 02:42:31 PM



Title: Python script to get Bitcoin address or any crypto you want
Post by: rhog on August 31, 2024, 02:42:31 PM
Code:
import random
import binascii
import ecdsa
import hashlib
from base58 import b58encode

def secret_to_address(secret, legacy=False):
    pubk_pair = from_secret_pubk_point(secret)
    compressed_pubk, pubk = _pubk_to_compressed_pubk(*pubk_pair)
    address = _pubk_to_address(pubk) if legacy else _pubk_to_address(compressed_pubk)

    return address


def from_secret_pubk_point(secret):
    CURVE = ecdsa.SECP256k1

    sk = ecdsa.SigningKey.from_secret_exponent(secret, curve=CURVE)
    pubk_vk = sk.verifying_key  # the point
    pubk = binascii.b2a_hex(pubk_vk.to_string()).decode('ascii')

    pubk_x = pubk[:64]
    pubk_y = pubk[64:]

    return pubk_x, pubk_y


def _pubk_to_compressed_pubk(pubk_x, pubk_y):
    EVEN_PREFIX = '02'
    UNEVEN_PREFIX = '03'
    LEGACY_PREFIX = '04'
    y_parity = ord(bytearray.fromhex(pubk_y[-2:])) % 2
    prefix = EVEN_PREFIX if y_parity==0 else UNEVEN_PREFIX
    compressed_pubk = prefix + pubk_x

    pubk = LEGACY_PREFIX + pubk_x + pubk_y

    return compressed_pubk, pubk


def _pubk_to_address(pubk):
    pubk_array = bytearray.fromhex(pubk)

    sha = hashlib.sha256()  
    sha.update(pubk_array)
    rip = hashlib.new('ripemd160')
    rip.update(sha.digest())
    PREFIX = '00'
    key_hash = PREFIX + rip.hexdigest()
    sha = hashlib.sha256()
    sha.update(bytearray.fromhex(key_hash))
    checksum = sha.digest()
    sha = hashlib.sha256()
    sha.update(checksum)
    checksum = sha.hexdigest()[0:8]
    address_hex = key_hash + checksum
    
    bs = bytes(bytearray.fromhex(address_hex))
    address = b58encode(bs).decode('utf-8')

    return address


def secret_to_wif(secret):
    PREFIX = "80"
        
    hex_string = hex(secret)[2:].zfill(64)
    pre_hash = PREFIX + hex_string

    hash_1 = hashlib.sha256(binascii.unhexlify(pre_hash)).hexdigest()
    hash_2 = hashlib.sha256(binascii.unhexlify(hash_1)).hexdigest()
    checksum = hash_2[:8]

    pre_hash_checksum = pre_hash + checksum
    from_hex_string = int(pre_hash_checksum, 16)
    def _get(idx):
        ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
        m = 58**idx
        idx = from_hex_string // m % 58
        return ALPHABET[idx]
    IDXS = range(51)  # sufficiently long
    wif_str = "".join(map(_get, IDXS))

    # Reverse
    rev_wif_str = wif_str[::-1].lstrip('1')

    return rev_wif_str
q=0
if __name__ == "__main__":
    inxd=random.randint(10000000000,115792089237316195423570985008687907852837564279074904382605163141518161494336)
    address_legacy = secret_to_address(inxd, True)
    address = secret_to_address(inxd)
    wif = secret_to_wif(inxd)
    print(f'''Wif: {wif}\nAddress Legacy: {address_legacy}\nAddress: {address}\nIndex: {inxd}''')

I have another code that can generate any characteristic name you want in any position in the portfolio, if you want, let me know and I'll post it here
Examples: 1Nx8dUJf^^MoNey^^N4tffTNxYnbVFNmjeAWrH

IF you want take a dogecoin address you change the value of the "PREFIX" variable of function "_pubk_to_address" to 1E AND variable "PREFIX" of function "secret_to_wif" to "9e"

If the script already exists on the forum, forgive me ~_~


This is my bitcoin wallet 1BW2nioZSMS5VFkiebM3eGhxRH2v1idsih, if you want to donate some satoshis =3


Title: Re: Python script to get Bitcoin address or any crypto you want
Post by: nc50lc on September 01, 2024, 07:40:59 AM
def secret_to_wif(secret):
    PREFIX = "80"
       
    hex_string = hex(secret)[2:].zfill(64)
    pre_hash = PREFIX + hex_string
The "Address" in the result is generated from the compressed public key, yet the "WIF" has no compressed pubKey flag (0x01).
Although you included "Address Legacy" which is the actual pair of the WIF in the result, the term used is just confusing since those are both "legacy addresses".

You should name them accordingly and include the WIF with 0x01 flag.
e.g:
Code:
Address (Compressed): 1PMNFXcWEdJxWdEeHPC3YRZwmphmL5rULn
WIF: L2YjUUxPdD55hmAnjT7UVaW2owY4dVtBwRvoH1tM63g1Dc6768iz

Address (Uncompressed): 14c2Y66TWB1fKq24cMrVtx9UrUSiifJjNs
WIF: 5K2JKv1KGTJEkg1QW6v88qNXmcvFsQmDuCzW4dT8ivd2QnzU1b4

Index: 71905415171631158767425490770033565803263757038737608778444906751809395954820

As a side note: you should enclose the script inside [code][/code] tags so it wont be affected by the forum's post format.


-------------//-------------
I have another code that can generate any characteristic name you want in any position in the portfolio, if you want, let me know and I'll post it here
Examples: 1Nx8dUJf^^MoNey^^N4tffTNxYnbVFNmjeAWrH
Vanitysearch can already do that with: ./vanitysearch bc1q****test**** or ./vanitysearch bc1q********test