0
import hashlib

b58chars = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'

def hex_open_key_to_hex_hesh160(hex_open_key):
h160 = hashlib.new('ripemd160')
h160.update(hashlib.sha256(('04'+hex_open_key).decode('hex')).hexdigest().decode('hex'))
return h160.hexdigest()

def hex_hesh160_to_hex_addr_v0(hex_hesh160):
return '00'+hex_hesh160+hashlib.sha256(hashlib.sha256(('00'+hex_hesh160).decode('hex')).hexdigest().decode('hex')).hexdigest()[0:8]

def hex_addr_v0_to_hex_hesh160(hex_addr_v0):
return hex_addr_v0[2:-8]


def hex_to_base58(hex_data):
base58 = ''
int_data = int(hex_data, 16)
while int_data >= len(b58chars):
    base58 = b58chars[int_data%len(b58chars)] + base58
    int_data = int_data/len(b58chars)
base58 = b58chars[int_data%len(b58chars)] + base58
for i in xrange(len(hex_data)/2):
    if hex_data[i*2:i*2+2] == '00':
        base58 = '1' + base58
    else:
        break
return base58

def base58_to_hex(base58):
hex_data = ''
int_data = 0
for i in xrange(-1, -len(base58)-1, -1):
    int_data += (b58chars.index(base58[i]))*58**(-i-1)
hex_data = hex(int_data)[2:-1]
for i in xrange(len(base58)):
    if base58[i] == '1':
        hex_data = '00' + hex_data
    else:
        break
return hex_data

hex_open_key = '5bd8834885082d9e9775f2084610bea79d2bd7acde2fc28b4dba85b0902ac786ef3a1fa082da527a3a51bcd71104b4e6ef91b62b2e93bcfdbc4ac7c35e9fddb'
print hex_to_base58(hex_hesh160_to_hex_addr_v0(hex_open_key_to_hex_hesh160(hex_open_key)))

above code to get bitcoin public key to address, some public key error happens

private key is 0x5c58d

public key pairs

x =  0x5bd8834885082d9e9775f2084610bea79d2bd7acde2fc28b4dba85b0902ac786L
y1 =  0xef3a1fa082da527a3a51bcd71104b4e6ef91b62b2e93bcfdbc4ac7c35e9fddbL
y2  =  0xf10c5e05f7d25ad85c5ae4328eefb4b19106e49d4d16c430243b5382ca15fe54L

public key x and y1 make an error

5bd8834885082d9e9775f2084610bea79d2bd7acde2fc28b4dba85b0902ac786ef3a1fa082da527a3a51bcd71104b4e6ef91b62b2e93bcfdbc4ac7c35e9fddb

Traceback (most recent call last):
   File "<module3>", line 45, in <module>
   File "<module3>", line 7, in hex_open_key_to_hex_hesh160
   File "C:\Python27\lib\encodings\hex_codec.py", line 42, in hex_decode
   output = binascii.a2b_hex(input)
TypeError: Odd-length string

public key x and y2 no error

5bd8834885082d9e9775f2084610bea79d2bd7acde2fc28b4dba85b0902ac786f10c5e05f7d25ad85c5ae4328eefb4b19106e49d4d16c430243b5382ca15fe54

address
    17MnDMuqhiTnQ1Yc38H2RYdSHkfUq6wmrq
1

You get the error because the hexstring you provided is 127 characters long. You need to pad with zeroes.

Even if you pad with zeroes 05bd8834885082d9e9775f2084610bea79d2bd7acde2fc28b4dba85b0902ac786ef3a1fa082da527a3a51bcd71104b4e6ef91b62b2e93bcfdbc4ac7c35e9fddb is not a valid public key. It must start with 04.

The coordinates y1, y2 of your public key also do not lie on the Curve. I do not know where you got your key from but it is invalid.

The Public Key corresponding to Private Key 0x5bd8834885082d9e9775f2084610bea79d2bd7acde2fc28b4dba85b0902ac786 is 04b302d50e6afaea3eb3194fced5b12ba45aa170d2d717d5a598511a96187a7a90e8c84b1f57a822bda9ecf111374c9e92fe0d1fb5a192a45790f437828d17f009 and the P2PKH address is 14pFAXD2uQpdpToL5LS4oh1bsSGXnVpA2r

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.