wownero-python/monero/address.py

117 lines
4.3 KiB
Python
Raw Normal View History

2017-11-24 02:05:16 +00:00
from binascii import hexlify, unhexlify
2017-12-10 01:54:09 +00:00
import re
2017-11-30 03:19:58 +00:00
import struct
2017-12-10 01:54:09 +00:00
import sys
2017-11-24 02:05:16 +00:00
from sha3 import keccak_256
from . import base58
2017-12-10 01:54:09 +00:00
_ADDR_REGEX = re.compile(r'^[123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]{95}$')
_IADDR_REGEX = re.compile(r'^[123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]{106}$')
if sys.version_info < (3,):
_integer_types = (int, long,)
else:
_integer_types = (int,)
2017-11-24 02:05:16 +00:00
class Address(object):
2017-11-30 02:43:34 +00:00
_valid_netbytes = (18, 53)
# NOTE: _valid_netbytes order is (real, testnet)
2017-11-24 02:05:16 +00:00
def __init__(self, address):
2017-11-29 03:37:01 +00:00
address = str(address)
2017-12-10 01:54:09 +00:00
if not _ADDR_REGEX.match(address):
raise ValueError("Address must be 95 characters long base58-encoded string, "
"is {addr} ({len} chars length)".format(addr=address, len=len(address)))
2017-11-24 02:05:16 +00:00
self._decode(address)
def _decode(self, address):
2017-11-30 03:19:58 +00:00
self._decoded = bytearray(unhexlify(base58.decode(address)))
2017-11-24 02:05:16 +00:00
checksum = self._decoded[-4:]
if checksum != keccak_256(self._decoded[:-4]).digest()[:4]:
raise ValueError("Invalid checksum")
2017-11-30 02:43:34 +00:00
if self._decoded[0] not in self._valid_netbytes:
raise ValueError("Invalid address netbyte {nb}. Allowed values are: {allowed}".format(
2017-11-30 03:25:42 +00:00
nb=self._decoded[0],
2017-11-30 02:43:34 +00:00
allowed=", ".join(map(lambda b: '%02x' % b, self._valid_netbytes))))
2017-11-24 02:05:16 +00:00
def is_testnet(self):
2017-11-30 02:43:34 +00:00
return self._decoded[0] == self._valid_netbytes[1]
2017-11-24 02:05:16 +00:00
def get_view_key(self):
return hexlify(self._decoded[33:65]).decode()
def get_spend_key(self):
return hexlify(self._decoded[1:33]).decode()
def with_payment_id(self, payment_id=0):
if isinstance(payment_id, (bytes, str)):
payment_id = int(payment_id, 16)
2017-12-10 01:54:09 +00:00
elif not isinstance(payment_id, _integer_types):
raise TypeError("payment_id must be either int or hexadecimal str or bytes, "
"is %r" % payment_id)
if payment_id.bit_length() > 64:
raise TypeError("Integrated payment_id cannot have more than 64 bits, "
"has %d" % payment_id.bit_length())
2017-11-24 02:05:16 +00:00
prefix = 54 if self.is_testnet() else 19
2017-11-30 03:19:58 +00:00
data = bytearray([prefix]) + self._decoded[1:65] + struct.pack('>Q', payment_id)
checksum = bytearray(keccak_256(data).digest()[:4])
2017-11-24 02:05:16 +00:00
return IntegratedAddress(base58.encode(hexlify(data + checksum)))
def __repr__(self):
return base58.encode(hexlify(self._decoded))
def __eq__(self, other):
if isinstance(other, Address):
return str(self) == str(other)
if isinstance(other, str):
return str(self) == other
return super()
2017-11-30 02:43:34 +00:00
class SubAddress(Address):
_valid_netbytes = (42, 63)
def with_payment_id(self):
2017-12-10 01:54:09 +00:00
raise TypeError("SubAddress cannot be integrated with payment ID")
2017-11-30 02:43:34 +00:00
2017-11-24 02:05:16 +00:00
class IntegratedAddress(Address):
2017-11-30 02:43:34 +00:00
_valid_netbytes = (19, 54)
2017-11-24 02:05:16 +00:00
def __init__(self, address):
2017-11-29 03:37:01 +00:00
address = str(address)
2017-12-10 01:54:09 +00:00
if not _IADDR_REGEX.match(address):
raise ValueError("Integrated address must be 106 characters long base58-encoded string, "
"is {addr} ({len} chars length)".format(addr=address, len=len(address)))
2017-11-24 02:05:16 +00:00
self._decode(address)
def get_payment_id(self):
return hexlify(self._decoded[65:-4]).decode()
def get_base_address(self):
prefix = 53 if self.is_testnet() else 18
2017-11-30 03:19:58 +00:00
data = bytearray([prefix]) + self._decoded[1:65]
2017-11-24 02:05:16 +00:00
checksum = keccak_256(data).digest()[:4]
return Address(base58.encode(hexlify(data + checksum)))
def address(addr):
2017-11-29 03:37:01 +00:00
addr = str(addr)
2017-12-10 01:54:09 +00:00
if _ADDR_REGEX.match(addr):
2017-11-30 03:19:58 +00:00
netbyte = bytearray(unhexlify(base58.decode(addr)))[0]
2017-11-30 02:43:34 +00:00
if netbyte in Address._valid_netbytes:
return Address(addr)
elif netbyte in SubAddress._valid_netbytes:
return SubAddress(addr)
raise ValueError("Invalid address netbyte {nb}. Allowed values are: {allowed}".format(
2017-11-30 03:19:58 +00:00
nb=hexlify(chr(netbyte)),
2017-11-30 02:43:34 +00:00
allowed=", ".join(map(
lambda b: '%02x' % b,
sorted(Address._valid_netbytes + SubAddress._valid_netbytes)))))
2017-12-10 01:54:09 +00:00
elif _IADDR_REGEX.match(addr):
2017-11-24 02:05:16 +00:00
return IntegratedAddress(addr)
2017-12-10 01:54:09 +00:00
raise ValueError("Address must be either 95 or 106 characters long base58-encoded string, "
"is {addr} ({len} chars length)".format(addr=address, len=len(address)))