-
Notifications
You must be signed in to change notification settings - Fork 86
/
convert_address.py
103 lines (83 loc) · 2.74 KB
/
convert_address.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
import hashlib, sys
b58ab = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
def b58csum(s):
return hashlib.sha256(hashlib.sha256(s).digest()).digest()[0:4]
def b58decode(s, checksum=True):
idx = 0
while s[idx] == "1":
idx += 1
n = 0
for c in s[idx:]:
n = n * 58 + b58ab.index(c)
res = long2byte(n)
res = idx * "\x00" + res
if checksum:
res, cs = res[:-4], res[-4:]
assert cs == b58csum(res), "base58 checksum failed"
return res
def b58encode(s, checksum=True):
if checksum:
s += b58csum(s)
idx = 0
while s[idx] == "\x00":
idx += 1
n = byte2long(s)
res = ""
while n > 0:
res = b58ab[n % 58] + res
n /= 58
return "1" * idx + res
def byte2long(s):
res = 0
for c in s:
res = (res << 8) | ord(c)
return res
def long2byte(n, sz=None):
res = ""
while n > 0:
res = chr(n & 0xff) + res
n >>= 8
if sz is not None:
res = res.rjust(sz, "\x00")
return res
cointypes = {
"BTC": (chr(0), chr(5)),
"BBC": (chr(0x19), chr(0x55)),
"BTCH": (chr(60), chr(85)),
"BTF": (chr(36), chr(40)),
"BTW": (chr(73), chr(31)),
"BTG": (chr(38), chr(23)),
"BCX": (chr(75), chr(63)),
"BPA": (chr(55), chr(80)),
"BTH": (chr(40), chr(5)),
"BTP": (chr(0x38), chr(5)),
"CDY": (chr(0x1c), chr(0x58)),
"BTSQ": (chr(63), chr(58)),
"BTCP": ("\x13\x25", "\x13\xaf"),
"BCA": (chr(23), chr(10)),
"BCI": (chr(102), chr(23)),
"GOD": (chr(97), chr(23)),
"MBC": (chr(26), chr(51)),
"CLAM": (chr(137), chr(13)),
"BCLD": (chr(137), chr(140)),
}
if len(sys.argv) != 3:
print "Small converter script that converts base58 addresses from one kind to another."
print
print "Usage: convert_address.py <address to convert> <ticker symbol of coin>"
print "Example: convert_address.py 1HKqKTMpBTZZ8H5zcqYEWYBaaWELrDEXeE BTCP"
print
print "Usage: convert_address.py <address to convert> <random address of another kind>"
print "Example: convert_address.py 1HKqKTMpBTZZ8H5zcqYEWYBaaWELrDEXeE XSmfx3pzAtVm4ujeBwyUenX9p5GbHwZF6s"
else:
if len(sys.argv[2]) <= 4:
srctype = b58decode(sys.argv[1])[:-20]
srcraw = b58decode(sys.argv[1])[-20:]
if srctype == "\x05":
identifier = cointypes[sys.argv[2]][1]
else:
identifier = cointypes[sys.argv[2]][0]
else:
identifier = b58decode(sys.argv[2])[:-20]
srcraw = b58decode(sys.argv[1])[-20:]
print b58encode(identifier + srcraw)