-
Notifications
You must be signed in to change notification settings - Fork 1
/
private2wif.py
39 lines (29 loc) · 1.46 KB
/
private2wif.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import hashlib
import base58
import binascii
from generating_private_key import Key_Generator
import codecs
# 1. Take a private Key
kg = Key_Generator()
kg.seed_input("This is a truly random string, how are you, feeling bitcoinist")
private_key = kg.generate_key()
#private_key = '0C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D'
# 2. Add a 0x80 byte in front of it for mainnet addresses or 0xef for testnet addresses. Also add a 0x01 byte at the end if the private key will correspond to a compressed public key
extended_key = "80" + private_key
# 3. Perform SHA256 hash on the extended key.
#first_sha256 = hashlib.sha256(binascii.unhexlify(extended_key)).hexdigest()
first_sha256 = hashlib.sha256(codecs.decode(extended_key,'hex')).hexdigest()
# 4. Perform SHA256 hash on result of SHA256 hash
# second_sha256 = hashlib.sha256(binascii.unhexlify(first_sha256)).hexdigest()
second_sha256 = hashlib.sha256(codecs.decode(first_sha256,'hex')).hexdigest()
# 5. Take first 4 bytes of the second SHA256 hash, this is the checksum
checksum = second_sha256[:8]
# 6. Add the four checksum bytes from point 5 at the end of the extended key from point 2
final_key = extended_key+checksum
# Wallet Import Format = base 58 encoding final_key
#WIF = base58.b58encode(binascii.unhexlify(final_key))
WIF = base58.b58encode(codecs.decode(final_key,'hex'))
WIF_string = WIF.decode('utf-8')
print(WIF)
#print(WIF_string)
# I refered this site https://en.bitcoin.it/wiki/Wallet_import_format