mirror of
https://github.com/dashpay/dash.git
synced 2024-12-25 12:02:48 +01:00
17acd6b472
643aad17fa
Enable additional flake8 rules (practicalswift)f020aca297
Minor Python cleanups to make flake8 pass with the new rules enabled (practicalswift) Pull request description: Enabled rules: ``` * E242: tab after ',' * E266: too many leading '#' for block comment * E401: multiple imports on one line * E402: module level import not at top of file * E701: multiple statements on one line (colon) * E901: SyntaxError: invalid syntax * E902: TokenError: EOF in multi-line string * F821: undefined name 'Foo' * W293: blank line contains whitespace * W606: 'async' and 'await' are reserved keywords starting with Python 3.7 ``` Note to reviewers: * In general we don't allow whitespace cleanups to existing code, but in order to allow for enabling Travis checking for these rules a few smaller whitespace cleanups had to made as part of this PR. * Use [this `?w=1` link](https://github.com/bitcoin/bitcoin/pull/12987/files?w=1) to show a diff without whitespace changes. Before this commit: ``` $ flake8 -qq --statistics --ignore=B,C,E,F,I,N,W --select=E112,E113,E115,E116,E125,E131,E133,E223,E224,E242,E266,E271,E272,E273,E274,E275,E304,E306,E401,E402,E502,E701,E702,E703,E714,E721,E741,E742,E743,F401,E901,E902,F402,F404,F406,F407,F601,F602,F621,F622,F631,F701,F702,F703,F704,F705,F706,F707,F811,F812,F821,F822,F823,F831,F841,W292,W293,W504,W601,W602,W603,W604,W605,W606 . 5 E266 too many leading '#' for block comment 4 E401 multiple imports on one line 6 E402 module level import not at top of file 5 E701 multiple statements on one line (colon) 1 F812 list comprehension redefines 'n' from line 159 4 F821 undefined name 'ConnectionRefusedError' 28 W293 blank line contains whitespace ``` After this commit: ``` $ flake8 -qq --statistics --ignore=B,C,E,F,I,N,W --select=E112,E113,E115,E116,E125,E131,E133,E223,E224,E242,E266,E271,E272,E273,E274,E275,E304,E306,E401,E402,E502,E701,E702,E703,E714,E721,E741,E742,E743,F401,E901,E902,F402,F404,F406,F407,F601,F602,F621,F622,F631,F701,F702,F703,F704,F705,F706,F707,F811,F812,F821,F822,F823,F831,F841,W292,W293,W504,W601,W602,W603,W604,W605,W606 . $ ``` Tree-SHA512: fc7d5e752298a50d4248afc620ee2c173135b4ca008e48e02913ac968e5a24a5fd5396926047ec62f1d580d537434ccae01f249bb2f3338fa59dc630bf97ca7a Signed-off-by: pasta <pasta@dashboost.org>
116 lines
3.0 KiB
Python
116 lines
3.0 KiB
Python
# Copyright (c) 2012 The Bitcoin Core developers
|
|
# Distributed under the MIT software license, see the accompanying
|
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
'''
|
|
Bitcoin base58 encoding and decoding.
|
|
|
|
Based on https://bitcointalk.org/index.php?topic=1026.0 (public domain)
|
|
'''
|
|
import hashlib
|
|
|
|
# for compatibility with following code...
|
|
class SHA256:
|
|
new = hashlib.sha256
|
|
|
|
if str != bytes:
|
|
# Python 3.x
|
|
def ord(c):
|
|
return c
|
|
def chr(n):
|
|
return bytes( (n,) )
|
|
|
|
__b58chars = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
|
|
__b58base = len(__b58chars)
|
|
b58chars = __b58chars
|
|
|
|
def b58encode(v):
|
|
""" encode v, which is a string of bytes, to base58.
|
|
"""
|
|
long_value = 0
|
|
for (i, c) in enumerate(v[::-1]):
|
|
if isinstance(c, str):
|
|
c = ord(c)
|
|
long_value += (256**i) * c
|
|
|
|
result = ''
|
|
while long_value >= __b58base:
|
|
div, mod = divmod(long_value, __b58base)
|
|
result = __b58chars[mod] + result
|
|
long_value = div
|
|
result = __b58chars[long_value] + result
|
|
|
|
# Bitcoin does a little leading-zero-compression:
|
|
# leading 0-bytes in the input become leading-1s
|
|
nPad = 0
|
|
for c in v:
|
|
if c == 0:
|
|
nPad += 1
|
|
else:
|
|
break
|
|
|
|
return (__b58chars[0]*nPad) + result
|
|
|
|
def b58decode(v, length = None):
|
|
""" decode v into a string of len bytes
|
|
"""
|
|
long_value = 0
|
|
for i, c in enumerate(v[::-1]):
|
|
pos = __b58chars.find(c)
|
|
assert pos != -1
|
|
long_value += pos * (__b58base**i)
|
|
|
|
result = bytes()
|
|
while long_value >= 256:
|
|
div, mod = divmod(long_value, 256)
|
|
result = chr(mod) + result
|
|
long_value = div
|
|
result = chr(long_value) + result
|
|
|
|
nPad = 0
|
|
for c in v:
|
|
if c == __b58chars[0]:
|
|
nPad += 1
|
|
continue
|
|
break
|
|
|
|
result = bytes(nPad) + result
|
|
if length is not None and len(result) != length:
|
|
return None
|
|
|
|
return result
|
|
|
|
def checksum(v):
|
|
"""Return 32-bit checksum based on SHA256"""
|
|
return SHA256.new(SHA256.new(v).digest()).digest()[0:4]
|
|
|
|
def b58encode_chk(v):
|
|
"""b58encode a string, with 32-bit checksum"""
|
|
return b58encode(v + checksum(v))
|
|
|
|
def b58decode_chk(v):
|
|
"""decode a base58 string, check and remove checksum"""
|
|
result = b58decode(v)
|
|
if result is None:
|
|
return None
|
|
if result[-4:] == checksum(result[:-4]):
|
|
return result[:-4]
|
|
else:
|
|
return None
|
|
|
|
def get_bcaddress_version(strAddress):
|
|
""" Returns None if strAddress is invalid. Otherwise returns integer version of address. """
|
|
addr = b58decode_chk(strAddress)
|
|
if addr is None or len(addr)!=21:
|
|
return None
|
|
version = addr[0]
|
|
return ord(version)
|
|
|
|
if __name__ == '__main__':
|
|
# Test case (from http://gitorious.org/bitcoin/python-base58.git)
|
|
assert get_bcaddress_version('15VjRaDX9zpbA8LVnbrCAFzrVzN7ixHNsC') is 0
|
|
_ohai = 'o hai'.encode('ascii')
|
|
_tmp = b58encode(_ohai)
|
|
assert _tmp == 'DYB3oMS'
|
|
assert b58decode(_tmp, 5) == _ohai
|
|
print("Tests passed")
|