When generate WIF private key , base58check('80'.decode('hex')) not prefix as '5'

0

0

i try to generate WIF private key with python.

import base58
import hashlib

def b58(hex):
    return base58.b58encode(hex)

def sha256(hex):
    return hashlib.sha256(hex).digest()

def main():
    k = sha256('private_key')
    extend = '80' + k.encode('hex')
    sha1 = sha256(extend.decode('hex'))
    sha2 = sha256(sha1)
    print b58(extend.decode('hex') + sha2[:8])

if __name__ == '__main__':
    main()

the result's prefix is not '5' but 'V'

i try to just base58encode 0x80 and the result is 3D ???

where is mistake in this code?

LeePanda

Posted 2019-10-28T16:53:54.597

Reputation: 3

Answers

0

By comparing all variables with this tool, I've found that the mistake is in this line:

print b58(extend.decode('hex') + sha2[:8])

Remember that sha2 is a byte array and not a hex string. You can replace the 8 with a 4 to make your code work.

You can also simplify the code even further to:

def b58(hex):
    return base58.b58encode_check(hex)

def sha256(hex):
    return hashlib.sha256(hex).digest()

def main():
    k = sha256('private_key')
    extend = '80'.decode('hex') + k
    print b58(extend)

MCCCS

Posted 2019-10-28T16:53:54.597

Reputation: 5 827

thank you. i got thisLeePanda 2019-10-29T01:08:06.663

If you feel like it, you can show other that this answer was helpful by clicking on the green check mark.MCCCS 2019-10-29T17:07:13.137