I have a program made in python, which works very well and very fast, to create bitcoin (wif / addr) address pairs randomly.
My problem is that I would like to create a bitcoin address, but with a private key create in HEX format created with this little script:
def gen_HEX(nDecimal):
aHex = hex(nDecimal)
aHex = aHex[2:].upper()
aHex = ((64-len(aHex)) * '0') + aHex
return aHex
priv = gen_HEX(1400) # Or any other number
My code that generates the pairs of addresses at random is: (I put it in if someone can serve you as it works very well)
import ctypes
import hashlib
import time
import sys
ssl_library = ctypes.cdll.LoadLibrary('libssl.so')
def base58_encode(num):
alphabet = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
base_count = len(alphabet)
encode = ''
if (num < 0):
return ''
while (num >= base_count):
mod = num % base_count
encode = alphabet[mod] + encode
num = num // base_count
if (num):
encode = alphabet[num] + encode
return encode
def gen_ecdsa_pair():
NID_secp160k1 = 708
NID_secp256k1 = 714
k = ssl_library.EC_KEY_new_by_curve_name(NID_secp256k1)
if ssl_library.EC_KEY_generate_key(k) != 1:
raise Exception("internal error?")
bignum_private_key = ssl_library.EC_KEY_get0_private_key(k)
size = (ssl_library.BN_num_bits(bignum_private_key)+7)//8
storage = ctypes.create_string_buffer(size)
ssl_library.BN_bn2bin(bignum_private_key, storage)
private_key = storage.raw
size = ssl_library.i2o_ECPublicKey(k, 0)
storage = ctypes.create_string_buffer(size)
ssl_library.i2o_ECPublicKey(k, ctypes.byref(ctypes.pointer(storage)))
public_key = storage.raw
ssl_library.EC_KEY_free(k)
return public_key, private_key
def ecdsa_get_coordinates(public_key):
x = bytes(public_key[1:33])
y = bytes(public_key[33:65])
return x, y
def generate_address(public_key):
assert isinstance(public_key, bytes)
x, y = ecdsa_get_coordinates(public_key)
s = b'\x04' + x + y
hasher = hashlib.sha256()
hasher.update(s)
r = hasher.digest()
hasher = hashlib.new('ripemd160')
hasher.update(r)
r = hasher.digest()
return '1' + base58_check(r, version=0)
def base58_check(src, version=0):
src = bytes([version]) + src
hasher = hashlib.sha256()
hasher.update(src)
r = hasher.digest()
hasher = hashlib.sha256()
hasher.update(r)
r = hasher.digest()
checksum = r[:4]
s = src + checksum
return base58_encode(int.from_bytes(s, 'big'))
def create_addr_fast():
public_key, private_key = gen_ecdsa_pair()
hex_private_key = ''.join(["{:02x}".format(i) for i in private_key])
if len(hex_private_key) == 64:
wif = base58_check(private_key, version=128)
addr = generate_address(public_key)
return addr, wif
else:
return '', ''
addr, wif = create_addr_fast()
print ('ADDR: ' + addr)
print ('WIF: ' + wif)
Does anyone know that I have to modify so that the private key does not generate it randomly?
I have tried many things ... and it does not work
Thanks