This commit is contained in:
Michał Sałaban 2020-01-13 10:42:50 +01:00
parent 9c92d13e37
commit 13e0581994
5 changed files with 20 additions and 43 deletions

View File

@ -1,18 +1,13 @@
from binascii import hexlify, unhexlify
import re
from sha3 import keccak_256
import six
import struct
import sys
from . import base58
from . import ed25519
from . import numbers
if sys.version_info < (3,): # pragma: no cover
_str_types = (str, bytes, unicode)
else: # pragma: no cover
_str_types = (str, bytes)
_ADDR_REGEX = re.compile(r'^[123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]{95}$')
_IADDR_REGEX = re.compile(r'^[123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]{106}$')
@ -78,8 +73,8 @@ class BaseAddress(object):
def __eq__(self, other):
if isinstance(other, BaseAddress):
return str(self) == str(other)
if isinstance(other, _str_types):
return str(self) == other
elif isinstance(other, six.text_type) or isinstance(other, six.string_types):
return str(self) == six.ensure_str(other)
return super(BaseAddress, self).__eq__(other)
def __hash__(self):

View File

@ -32,21 +32,15 @@ arithmetic, so we cannot handle secrets without risking their disclosure.
"""
import binascii
import operator
import six
import sys
if sys.version_info >= (3,): # pragma: no cover
indexbytes = operator.getitem
intlist2bytes = bytes
int2byte = operator.methodcaller("to_bytes", 1, "big")
else: # pragma: no cover
int2byte = chr
range = xrange
def indexbytes(buf, i):
return ord(buf[i])
def intlist2bytes(l):
return b"".join(chr(c) for c in l)
@ -192,7 +186,7 @@ def scalarmult_B(e):
def encodeint(y):
bits = [(y >> i) & 1 for i in range(b)]
return b''.join([
int2byte(sum([bits[i * 8 + j] << j for j in range(8)]))
six.int2byte(sum([bits[i * 8 + j] << j for j in range(8)]))
for i in range(b//8)
])
@ -204,13 +198,13 @@ def encodepoint(P):
y = (y * zi) % q
bits = [(y >> i) & 1 for i in range(b - 1)] + [x & 1]
return b''.join([
int2byte(sum([bits[i * 8 + j] << j for j in range(8)]))
six.int2byte(sum([bits[i * 8 + j] << j for j in range(8)]))
for i in range(b // 8)
])
def bit(h, i):
return (indexbytes(h, i // 8) >> (i % 8)) & 1
return (six.indexbytes(h, i // 8) >> (i % 8)) & 1
def isoncurve(P):

View File

@ -1,20 +1,13 @@
from decimal import Decimal
import sys
import six
PICONERO = Decimal('0.000000000001')
EMPTY_KEY = '0' * 64
if sys.version_info < (3,): # pragma: no cover
_integer_types = (int, long,)
_str_types = (str, bytes, unicode)
else: # pragma: no cover
_integer_types = (int,)
_str_types = (str, bytes)
def to_atomic(amount):
"""Convert Monero decimal to atomic integer of piconero."""
if not isinstance(amount, (Decimal, float) + _integer_types):
if not isinstance(amount, (Decimal, float) + six.integer_types):
raise ValueError("Amount '{}' doesn't have numeric type. Only Decimal, int, long and "
"float (not recommended) are accepted as amounts.")
return int(amount * 10**12)
@ -43,9 +36,9 @@ class PaymentID(object):
def __init__(self, payment_id):
if isinstance(payment_id, PaymentID):
payment_id = int(payment_id)
if isinstance(payment_id, _str_types):
if isinstance(payment_id, six.text_type) or isinstance(payment_id, six.string_types):
payment_id = int(payment_id, 16)
elif not isinstance(payment_id, _integer_types):
elif not isinstance(payment_id, six.integer_types):
raise TypeError("payment_id must be either int or hexadecimal str or bytes, "
"is {0}".format(type(payment_id)))
if payment_id.bit_length() > 256:
@ -68,8 +61,8 @@ class PaymentID(object):
def __eq__(self, other):
if isinstance(other, PaymentID):
return int(self) == int(other)
elif isinstance(other, _integer_types):
elif isinstance(other, six.integer_types):
return int(self) == other
elif isinstance(other, _str_types):
return str(self) == other
elif isinstance(other, six.text_type) or isinstance(other, six.string_types):
return str(self) == six.ensure_str(other)
return super(PaymentID, self).__eq__(other)

View File

@ -1,5 +1,5 @@
import re
import sys
import six
import warnings
from .address import address
from .numbers import PaymentID
@ -93,12 +93,6 @@ class Transaction(object):
return self.hash
if sys.version_info < (3,): # pragma: no cover
_str_types = (str, bytes, unicode)
else: # pragma: no cover
_str_types = (str, bytes)
class PaymentManager(object):
"""
A payment query manager, handling either incoming or outgoing payments of
@ -185,7 +179,8 @@ class PaymentFilter(object):
if _local_address is None:
self.local_addresses = []
else:
if isinstance(_local_address, _str_types):
if isinstance(_local_address, six.string_types) \
or isinstance(_local_address, six.text_type):
local_addresses = [_local_address]
else:
try:
@ -197,7 +192,7 @@ class PaymentFilter(object):
if _tx_id is None:
self.tx_ids = []
else:
if isinstance(_tx_id, _str_types):
if isinstance(_tx_id, six.string_types) or isinstance(_tx_id, six.text_type):
tx_ids = [_tx_id]
else:
try:
@ -209,7 +204,7 @@ class PaymentFilter(object):
if _payment_id is None:
self.payment_ids = []
else:
if isinstance(_payment_id, _str_types):
if isinstance(_payment_id, six.string_types) or isinstance(_payment_id, six.text_type):
payment_ids = [_payment_id]
else:
try:

View File

@ -1,3 +1,3 @@
pysha3
requests
six
six>=1.12.0