-
Notifications
You must be signed in to change notification settings - Fork 7
/
wif-converter.py
executable file
·162 lines (118 loc) · 4.15 KB
/
wif-converter.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/python
#
# WIF converter from private ECDSA keys and vice versa, tailored for Swipp
# Some code and ideas taken from https://github.com/crcarlo/btcwif
#
# Copyright (c) 2017 Carlo Cervellin
# Copyright (c) 2017-2018 The Swipp developers
# Distributed under the MIT/X11 software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
import errno
import hashlib
import sys
# base58 alphabet
alphabet = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
def sha256(hex_string):
byte_array = bytearray.fromhex(hex_string)
m = hashlib.sha256()
m.update(byte_array)
return m.hexdigest()
def b58encode(hex_string):
num = int(hex_string, 16)
encode = ""
base_count = len(alphabet)
while (num > 0) :
num, res = divmod(num,base_count)
encode = alphabet[res] + encode
return encode
def b58decode(v):
if not isinstance(v, str):
v = v.decode('ascii')
decimal = 0
for char in v:
decimal = decimal * 58 + alphabet.index(char)
return hex(decimal)[2:] # (remove "0x" prefix)
def privToWif(priv, testnet=False, verbose=False) :
_priv = priv.lower()
if verbose:
print("Private key: "+_priv)
priv_add_wprefix = ("ef" if testnet else "9b") + _priv
if verbose:
print("Private with prefix at beginning: " + priv_add_wprefix)
first_sha256 = sha256(priv_add_wprefix)
if verbose:
print("SHA256: " + first_sha256.upper())
seconf_sha256 = sha256(first_sha256)
if verbose:
print("SHA256: " + seconf_sha256.upper())
first_4_bytes = seconf_sha256[0:8]
if verbose:
print("First 4 bytes: " + first_4_bytes)
resulting_hex = priv_add_wprefix + first_4_bytes
if verbose:
print("Resulting WIF in HEX: " + resulting_hex)
result_wif = b58encode(resulting_hex)
if verbose:
print("Resulting WIF: " + result_wif)
return result_wif
def wifToPriv(wif, verbose=False) :
if not wifChecksum(wif):
raise Exception("The WIF is not correct (does not pass checksum)")
if verbose:
print("WIF: " + wif)
byte_str = b58decode(wif)
if verbose:
print("WIF base58 decoded: " + byte_str)
byte_str_drop_last_4bytes = byte_str[0:-8]
if verbose:
print("Decoded WIF drop last 4 bytes: " + byte_str_drop_last_4bytes)
byte_str_drop_first_byte = byte_str_drop_last_4bytes[2:]
if verbose:
print("ECDSA private key: " + byte_str_drop_first_byte)
return byte_str_drop_first_byte
def wifChecksum(wif, verbose=False) :
if verbose:
print("WIF: " + wif)
byte_str = b58decode(wif)
if verbose:
print("WIF base58 decoded: " + byte_str)
byte_str_drop_last_4bytes = byte_str[0:-8]
if verbose:
print("Decoded WIF drop last 4 bytes: " + byte_str_drop_last_4bytes)
sha_256_1 = sha256(byte_str_drop_last_4bytes)
if verbose:
print("SHA256 1: " + sha_256_1)
sha_256_2 = sha256(sha_256_1)
if verbose:
print("SHA256 2: " + sha_256_2)
first_4_bytes = sha_256_2[0:8]
if verbose:
print("First 4 bytes: " + first_4_bytes)
last_4_bytes_WIF = byte_str[-8:]
if verbose:
print("Last 4 bytes of WIF: " + last_4_bytes_WIF)
bytes_check = False
if first_4_bytes == last_4_bytes_WIF:
bytes_check = True
if verbose:
print("4 bytes check: " + str(bytes_check))
check_sum = False
if bytes_check and (byte_str[0:2] == "ef" or byte_str[0:2] == "9b"):
check_sum = True
if verbose:
print("Checksum: " + str(check_sum))
return check_sum
if __name__ == "__main__":
if len(sys.argv) < 2:
sys.stderr.write("Please specify a keyfile to load\n")
sys.exit(1)
with open(sys.argv[1], 'r') as keyfile:
contents = keyfile.read()
start = contents.find("priv:")
end = contents.find("pub:")
if start == -1:
sys.stderr.write("Invalid key file\n")
sys.exit(errno.EINVAL)
key = contents[len("priv:") + start:end]
key = key.replace(":", "").replace("\n", "").replace(" ", "")
privToWif(key, len(sys.argv) >= 3 and sys.argv[2] == "testnet", True)