-
Notifications
You must be signed in to change notification settings - Fork 6
/
base58.py
200 lines (153 loc) · 4.97 KB
/
base58.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# base58.py
# Copyright (c) 2009-2010 Satoshi Nakamoto
# Copyright (c) 2009-2012 The Bitcoin Developers
# Copyright (C) 2013-2015 by CJP
#
# This file is part of Bitcoin Cash Off The Grid (BCCOTG).
#
# BCCOTG is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# BCCOTG is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with BCCOTG. If not, see <http://www.gnu.org/licenses/>.
#
# Additional permission under GNU GPL version 3 section 7
#
# If you modify this Program, or any covered work, by linking or combining it
# with the OpenSSL library (or a modified version of that library),
# containing parts covered by the terms of the OpenSSL License and the SSLeay
# License, the licensors of this Program grant you additional permission to
# convey the resulting work. Corresponding Source for a non-source form of
# such a combination shall include the source code for the parts of the
# OpenSSL library used as well as that of the covered work.
import binascii
import struct
from crypto import SHA256, RIPEMD160
base58Chars = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
def encodeBase58(data):
"""
Base58-encodes the given data, without checksum or version number.
Arguments:
data: str; the to-be-encoded data.
Return value:
str; the encoded data.
"""
# Convert big endian data to bignum
bignum = int('00' + data.encode("hex"), 16) #00 is necessary in case of empty string
ret = ""
while bignum > 0:
bignum, remainder = divmod(bignum, 58)
ret = ret + base58Chars[remainder]
# Leading zeroes encoded as base58 zeros
for i in range(len(data)):
if data[i] != '\0':
break
ret = ret + base58Chars[0]
# Convert little endian string to big endian
ret = ret[::-1]
return ret
def decodeBase58(data):
"""
Base58-decodes the given data, without checksum or version number.
Arguments:
data: str; the to-be-decoded data.
Return value:
str; the decoded data.
Exceptions:
ValueError: data contains an illegal character
"""
#Leading zeroes:
zeroes = ""
while len(data) > 0 and data[0] == base58Chars[0]:
zeroes += '\0'
data = data[1:]
#Big endian base58 decoding:
bignum = 0
for c in data:
digit = base58Chars.index(c)
bignum = 58*bignum + digit
#To big endian:
#First to hex:
ret = "%x" % bignum
#Add leading zero to force even-length: (unhexlify doesn't like odd-length)
ret = "0"*(len(ret) & 1) + ret
#Then to binary string:
ret = binascii.unhexlify(ret)
#Skip zeroes:
while len(ret) > 0 and ret[0] == '\0':
ret = ret[1:]
return zeroes + ret
def encodeBase58Check_noVersion(data):
"""
Base58-encodes the given data, with checksum but without version number.
Arguments:
data: str; the to-be-encoded data.
Return value:
str; the encoded data.
"""
# add 4-byte hash check to the end
checksum = SHA256(SHA256(data))[:4]
return encodeBase58(data + checksum)
def decodeBase58Check_noVersion(data):
"""
Base58-decodes the given data, with checksum but without version number.
Arguments:
data: str; the to-be-decoded data.
Return value:
str; the decoded data.
Exceptions:
Exception: checksum failed
ValueError: data contains an illegal character
"""
decoded = decodeBase58(data)
checksum = decoded[-4:]
rest = decoded[:-4]
if checksum != SHA256(SHA256(rest))[:4]:
raise Exception("Checksum failed")
return rest
def encodeBase58Check(data, version):
"""
Base58-encodes the given data, with checksum and version number.
Arguments:
data: str; the to-be-encoded data.
version: int, the version number. Example values:
PUBKEY_ADDRESS = 0
SCRIPT_ADDRESS = 5
PUBKEY_ADDRESS_TEST = 111
PRIVKEY = 128
SCRIPT_ADDRESS_TEST = 196
AMIKO = 23 (not used anywhere)
Return value:
str; the encoded data.
"""
return encodeBase58Check_noVersion(
struct.pack('B', version) + data)
def decodeBase58Check(data, version):
"""
Base58-decodes the given data, with checksum and version number.
Arguments:
data: str; the to-be-decoded data.
version: int, the version number. Example values:
PUBKEY_ADDRESS = 0
SCRIPT_ADDRESS = 5
PUBKEY_ADDRESS_TEST = 111
PRIVKEY = 128
SCRIPT_ADDRESS_TEST = 196
AMIKO = 23 (not used anywhere)
Return value:
str; the decoded data.
Exceptions:
Exception: checksum failed, or version number mismatch
ValueError: data contains an illegal character
"""
decoded = decodeBase58Check_noVersion(data)
if version != struct.unpack('B', decoded[0])[0]:
raise Exception("Version mismatch")
return decoded[1:]