Files
purple-electrumwallet/electrum/util.py
T

1621 lines
52 KiB
Python
Raw Normal View History

2016-02-23 11:36:42 +01:00
# Electrum - lightweight Bitcoin client
# Copyright (C) 2011 Thomas Voegtlin
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation files
# (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
2017-01-22 21:25:24 +03:00
import binascii
2013-09-14 21:53:56 +02:00
import os, sys, re, json
from collections import defaultdict, OrderedDict
from typing import (NamedTuple, Union, TYPE_CHECKING, Tuple, Optional, Callable, Any,
Sequence, Dict, Generic, TypeVar, List, Iterable)
from datetime import datetime
2018-05-09 19:30:18 +02:00
import decimal
2015-05-27 16:34:42 +09:00
from decimal import Decimal
2015-08-26 16:41:12 +09:00
import traceback
2015-02-25 10:01:59 +01:00
import urllib
2015-03-13 23:04:29 +01:00
import threading
2018-01-07 19:26:59 +01:00
import hmac
import stat
from locale import localeconv
import asyncio
import urllib.request, urllib.parse, urllib.error
2018-09-12 16:57:12 +02:00
import builtins
import json
import time
2018-11-26 21:21:02 +01:00
from typing import NamedTuple, Optional
import ssl
2018-07-26 21:08:25 +02:00
import ipaddress
from ipaddress import IPv4Address, IPv6Address
import random
import secrets
2021-03-04 16:44:13 +01:00
import functools
from abc import abstractmethod, ABC
2017-01-22 21:25:24 +03:00
import attr
import aiohttp
from aiohttp_socks import ProxyConnector, ProxyType
2020-04-15 21:41:33 +02:00
import aiorpcx
from aiorpcx import TaskGroup
2018-12-13 23:11:59 +01:00
import certifi
import dns.resolver
from .i18n import _
2019-04-26 18:52:26 +02:00
from .logging import get_logger, Logger
2017-01-22 21:25:24 +03:00
2018-10-22 16:41:25 +02:00
if TYPE_CHECKING:
from .network import Network
from .interface import Interface
from .simple_config import SimpleConfig
2018-10-22 16:41:25 +02:00
2019-04-26 18:52:26 +02:00
_logger = get_logger(__name__)
def inv_dict(d):
return {v: k for k, v in d.items()}
2018-12-13 23:11:59 +01:00
ca_path = certifi.where()
2018-05-05 12:42:17 +02:00
base_units = {'BTC':8, 'mBTC':5, 'bits':2, 'sat':0}
base_units_inverse = inv_dict(base_units)
base_units_list = ['BTC', 'mBTC', 'bits', 'sat'] # list(dict) does not guarantee order
2018-09-05 18:30:53 +02:00
DECIMAL_POINT_DEFAULT = 5 # mBTC
class UnknownBaseUnit(Exception): pass
2018-05-05 12:42:17 +02:00
def decimal_point_to_base_unit_name(dp: int) -> str:
# e.g. 8 -> "BTC"
try:
return base_units_inverse[dp]
except KeyError:
2018-09-05 18:30:53 +02:00
raise UnknownBaseUnit(dp) from None
2018-05-05 12:42:17 +02:00
def base_unit_name_to_decimal_point(unit_name: str) -> int:
# e.g. "BTC" -> 8
try:
return base_units[unit_name]
except KeyError:
2018-09-05 18:30:53 +02:00
raise UnknownBaseUnit(unit_name) from None
2018-05-05 12:42:17 +02:00
2015-12-16 11:53:37 +01:00
class NotEnoughFunds(Exception):
def __str__(self):
return _("Insufficient funds")
2017-12-30 01:10:20 +01:00
class NoDynamicFeeEstimates(Exception):
def __str__(self):
return _('Dynamic fee estimates not available')
class MultipleSpendMaxTxOutputs(Exception):
def __str__(self):
return _('At most one output can be set to spend max')
2014-12-03 22:35:05 +01:00
class InvalidPassword(Exception):
def __str__(self):
return _("Incorrect password")
2020-09-13 16:55:37 +02:00
class AddTransactionException(Exception):
pass
class UnrelatedTransactionException(AddTransactionException):
def __str__(self):
return _("Transaction is unrelated to this wallet.")
class FileImportFailed(Exception):
2018-02-15 18:07:00 +01:00
def __init__(self, message=''):
self.message = str(message)
def __str__(self):
2018-02-15 18:07:00 +01:00
return _("Failed to import from file.") + "\n" + self.message
2018-02-15 18:07:00 +01:00
class FileExportFailed(Exception):
2018-02-21 18:55:37 +01:00
def __init__(self, message=''):
self.message = str(message)
def __str__(self):
2018-02-21 18:55:37 +01:00
return _("Failed to export to file.") + "\n" + self.message
class WalletFileException(Exception): pass
class BitcoinException(Exception): pass
2018-11-08 19:46:15 +01:00
class UserFacingException(Exception):
"""Exception that contains information intended to be shown to the user."""
2019-12-10 03:19:56 +01:00
class InvoiceError(UserFacingException): pass
2018-11-08 19:46:15 +01:00
# Throw this exception to unwind the stack like when an error occurs.
# However unlike other exceptions the user won't be informed.
class UserCancelled(Exception):
'''An exception that is suppressed from the user'''
pass
# note: this is not a NamedTuple as then its json encoding cannot be customized
class Satoshis(object):
__slots__ = ('value',)
def __new__(cls, value):
self = super(Satoshis, cls).__new__(cls)
# note: 'value' sometimes has msat precision
self.value = value
return self
def __repr__(self):
return f'Satoshis({self.value})'
2018-02-15 14:59:05 +01:00
def __str__(self):
# note: precision is truncated to satoshis here
return format_satoshis(self.value)
2018-02-15 14:59:05 +01:00
2018-12-08 04:12:07 +01:00
def __eq__(self, other):
return self.value == other.value
def __ne__(self, other):
return not (self == other)
def __add__(self, other):
return Satoshis(self.value + other.value)
# note: this is not a NamedTuple as then its json encoding cannot be customized
class Fiat(object):
__slots__ = ('value', 'ccy')
def __new__(cls, value: Optional[Decimal], ccy: str):
self = super(Fiat, cls).__new__(cls)
self.ccy = ccy
if not isinstance(value, (Decimal, type(None))):
raise TypeError(f"value should be Decimal or None, not {type(value)}")
self.value = value
return self
def __repr__(self):
return 'Fiat(%s)'% self.__str__()
2018-02-15 14:59:05 +01:00
def __str__(self):
2018-11-18 16:46:07 +01:00
if self.value is None or self.value.is_nan():
2018-02-15 14:59:05 +01:00
return _('No Data')
else:
return "{:.2f}".format(self.value)
2018-02-15 14:59:05 +01:00
2019-04-21 01:55:46 +02:00
def to_ui_string(self):
if self.value is None or self.value.is_nan():
return _('No Data')
else:
return "{:.2f}".format(self.value) + ' ' + self.ccy
2018-12-08 04:12:07 +01:00
def __eq__(self, other):
2020-11-18 23:50:08 +01:00
if not isinstance(other, Fiat):
return False
if self.ccy != other.ccy:
return False
if isinstance(self.value, Decimal) and isinstance(other.value, Decimal) \
and self.value.is_nan() and other.value.is_nan():
return True
return self.value == other.value
2018-12-08 04:12:07 +01:00
def __ne__(self, other):
return not (self == other)
def __add__(self, other):
assert self.ccy == other.ccy
return Fiat(self.value + other.value, self.ccy)
2013-09-14 21:53:56 +02:00
class MyEncoder(json.JSONEncoder):
def default(self, obj):
# note: this does not get called for namedtuples :( https://bugs.python.org/issue30343
2019-10-23 17:09:41 +02:00
from .transaction import Transaction, TxOutput
from .lnutil import UpdateAddHtlc
if isinstance(obj, UpdateAddHtlc):
return obj.to_tuple()
2014-04-11 20:20:52 -04:00
if isinstance(obj, Transaction):
2019-10-23 17:09:41 +02:00
return obj.serialize()
if isinstance(obj, TxOutput):
return obj.to_legacy_tuple()
2018-02-15 14:59:05 +01:00
if isinstance(obj, Satoshis):
return str(obj)
if isinstance(obj, Fiat):
return str(obj)
if isinstance(obj, Decimal):
return str(obj)
if isinstance(obj, datetime):
return obj.isoformat(' ')[:-3]
2018-05-31 20:18:39 +02:00
if isinstance(obj, set):
return list(obj)
2020-02-04 13:35:58 +01:00
if isinstance(obj, bytes): # for nametuples in lnchannel
return obj.hex()
if hasattr(obj, 'to_json') and callable(obj.to_json):
return obj.to_json()
return super(MyEncoder, self).default(obj)
2013-09-14 21:53:56 +02:00
2019-04-26 18:52:26 +02:00
class ThreadJob(Logger):
2015-09-06 21:40:00 +09:00
"""A job that is run periodically from a thread's main loop. run() is
called from that thread's context.
"""
2015-08-26 16:41:12 +09:00
2019-04-26 18:52:26 +02:00
def __init__(self):
Logger.__init__(self)
2015-08-26 16:41:12 +09:00
def run(self):
"""Called periodically from the thread"""
pass
2013-09-14 21:53:56 +02:00
2015-11-13 23:11:43 +09:00
class DebugMem(ThreadJob):
'''A handy class for debugging GC memory leaks'''
def __init__(self, classes, interval=30):
2019-04-26 18:52:26 +02:00
ThreadJob.__init__(self)
2015-11-13 23:11:43 +09:00
self.next_time = 0
self.classes = classes
self.interval = interval
def mem_stats(self):
import gc
2019-04-26 18:52:26 +02:00
self.logger.info("Start memscan")
2015-11-13 23:11:43 +09:00
gc.collect()
objmap = defaultdict(list)
for obj in gc.get_objects():
for class_ in self.classes:
if isinstance(obj, class_):
objmap[class_].append(obj)
for class_, objs in objmap.items():
2019-04-26 18:52:26 +02:00
self.logger.info(f"{class_.__name__}: {len(objs)}")
self.logger.info("Finish memscan")
2015-11-13 23:11:43 +09:00
def run(self):
if time.time() > self.next_time:
self.mem_stats()
self.next_time = time.time() + self.interval
2019-04-26 18:52:26 +02:00
class DaemonThread(threading.Thread, Logger):
2015-03-13 23:04:29 +01:00
""" daemon thread that terminates cleanly """
LOGGING_SHORTCUT = 'd'
2015-03-13 23:04:29 +01:00
def __init__(self):
threading.Thread.__init__(self)
2019-04-26 18:52:26 +02:00
Logger.__init__(self)
2015-03-13 23:04:29 +01:00
self.parent_thread = threading.currentThread()
self.running = False
self.running_lock = threading.Lock()
2015-08-26 16:41:12 +09:00
self.job_lock = threading.Lock()
self.jobs = []
self.stopped_event = threading.Event() # set when fully stopped
2015-08-26 16:41:12 +09:00
2015-09-05 17:18:09 +09:00
def add_jobs(self, jobs):
2015-08-26 16:41:12 +09:00
with self.job_lock:
2015-09-05 17:18:09 +09:00
self.jobs.extend(jobs)
2015-08-26 16:41:12 +09:00
def run_jobs(self):
# Don't let a throwing job disrupt the thread, future runs of
# itself, or other jobs. This is useful protection against
# malformed or malicious server responses
with self.job_lock:
for job in self.jobs:
try:
job.run()
2017-01-30 12:36:56 +03:00
except Exception as e:
2019-04-26 18:52:26 +02:00
self.logger.exception('')
2015-08-26 16:41:12 +09:00
2015-09-05 17:18:09 +09:00
def remove_jobs(self, jobs):
2015-08-26 16:41:12 +09:00
with self.job_lock:
2015-09-05 17:18:09 +09:00
for job in jobs:
self.jobs.remove(job)
2015-03-13 23:04:29 +01:00
def start(self):
with self.running_lock:
self.running = True
return threading.Thread.start(self)
def is_running(self):
with self.running_lock:
return self.running and self.parent_thread.is_alive()
def stop(self):
with self.running_lock:
self.running = False
2016-06-04 12:58:29 +02:00
def on_stop(self):
if 'ANDROID_DATA' in os.environ:
import jnius
jnius.detach()
2019-04-26 18:52:26 +02:00
self.logger.info("jnius detach")
self.logger.info("stopped")
self.stopped_event.set()
2015-03-13 23:04:29 +01:00
2014-04-17 17:20:07 +02:00
def print_stderr(*args):
2012-12-18 11:56:27 +01:00
args = [str(item) for item in args]
sys.stderr.write(" ".join(args) + "\n")
sys.stderr.flush()
2012-11-23 18:48:56 +01:00
def print_msg(*args):
2012-08-18 09:35:02 +01:00
# Stringify args
args = [str(item) for item in args]
2012-12-18 11:56:27 +01:00
sys.stdout.write(" ".join(args) + "\n")
sys.stdout.flush()
2012-07-07 09:24:52 -07:00
def json_encode(obj):
2013-09-14 21:07:54 +02:00
try:
2013-09-14 21:53:56 +02:00
s = json.dumps(obj, sort_keys = True, indent = 4, cls=MyEncoder)
2013-09-14 21:07:54 +02:00
except TypeError:
s = repr(obj)
return s
def json_decode(x):
try:
2017-02-28 22:58:35 +01:00
return json.loads(x, parse_float=Decimal)
except:
return x
2015-03-17 11:29:17 +01:00
2020-09-24 07:14:21 +02:00
def json_normalize(x):
# note: The return value of commands, when going through the JSON-RPC interface,
# is json-encoded. The encoder used there cannot handle some types, e.g. electrum.util.Satoshis.
# note: We should not simply do "json_encode(x)" here, as then later x would get doubly json-encoded.
# see #5868
return json_decode(json_encode(x))
2018-01-07 19:26:59 +01:00
# taken from Django Source Code
def constant_time_compare(val1, val2):
"""Return True if the two strings are equal, False otherwise."""
return hmac.compare_digest(to_bytes(val1, 'utf8'), to_bytes(val2, 'utf8'))
2015-03-17 11:29:17 +01:00
# decorator that prints execution time
2019-04-27 03:42:31 +02:00
_profiler_logger = _logger.getChild('profiler')
2015-03-17 11:29:17 +01:00
def profiler(func):
2018-08-02 15:38:01 +02:00
def do_profile(args, kw_args):
2018-12-20 17:09:58 +01:00
name = func.__qualname__
2015-03-17 11:29:17 +01:00
t0 = time.time()
2015-12-20 15:39:57 +09:00
o = func(*args, **kw_args)
2015-03-17 11:29:17 +01:00
t = time.time() - t0
2019-04-27 03:42:31 +02:00
_profiler_logger.debug(f"{name} {t:,.4f}")
2015-03-17 11:29:17 +01:00
return o
2018-08-02 15:38:01 +02:00
return lambda *args, **kw_args: do_profile(args, kw_args)
2015-03-17 11:29:17 +01:00
2020-02-12 19:13:18 +01:00
def android_ext_dir():
from android.storage import primary_external_storage_path
return primary_external_storage_path()
2020-02-12 19:13:18 +01:00
def android_backup_dir():
d = os.path.join(android_ext_dir(), 'org.electrum.electrum')
2020-02-12 19:13:18 +01:00
if not os.path.exists(d):
os.mkdir(d)
return d
def android_data_dir():
import jnius
2016-06-01 01:56:34 +05:30
PythonActivity = jnius.autoclass('org.kivy.android.PythonActivity')
return PythonActivity.mActivity.getFilesDir().getPath() + '/data'
def ensure_sparse_file(filename):
# On modern Linux, no need to do anything.
# On Windows, need to explicitly mark file.
if os.name == "nt":
try:
os.system('fsutil sparse setflag "{}" 1'.format(filename))
except Exception as e:
2019-04-26 18:52:26 +02:00
_logger.info(f'error marking file {filename} as sparse: {e}')
def get_headers_dir(config):
2018-11-26 17:52:30 +01:00
return config.path
2015-03-17 11:29:17 +01:00
2017-01-22 21:25:24 +03:00
2018-04-29 18:25:10 +02:00
def assert_datadir_available(config_path):
path = config_path
if os.path.exists(path):
return
else:
raise FileNotFoundError(
'Electrum datadir does not exist. Was it deleted while running?' + '\n' +
'Should be at {}'.format(path))
def assert_file_in_datadir_available(path, config_path):
if os.path.exists(path):
return
else:
assert_datadir_available(config_path)
raise FileNotFoundError(
'Cannot find file but datadir is there.' + '\n' +
'Should be at {}'.format(path))
2019-02-08 12:59:06 +01:00
def standardize_path(path):
return os.path.normcase(
os.path.realpath(
os.path.abspath(
os.path.expanduser(
path
))))
2019-02-08 12:59:06 +01:00
def get_new_wallet_name(wallet_folder: str) -> str:
i = 1
while True:
filename = "wallet_%d" % i
if filename in os.listdir(wallet_folder):
i += 1
else:
break
return filename
2017-01-22 21:25:24 +03:00
def assert_bytes(*args):
"""
porting helper, assert args type
"""
try:
2017-09-04 14:43:31 +02:00
for x in args:
assert isinstance(x, (bytes, bytearray))
2017-01-22 21:25:24 +03:00
except:
print('assert bytes failed', list(map(type, args)))
raise
def assert_str(*args):
"""
porting helper, assert args type
"""
for x in args:
2017-09-04 14:43:31 +02:00
assert isinstance(x, str)
2017-01-22 21:25:24 +03:00
def to_string(x, enc) -> str:
2017-01-22 21:25:24 +03:00
if isinstance(x, (bytes, bytearray)):
return x.decode(enc)
if isinstance(x, str):
return x
else:
raise TypeError("Not a string or bytes like object")
def to_bytes(something, encoding='utf8') -> bytes:
2017-01-22 21:25:24 +03:00
"""
cast string to bytes() like object, but for python2 support it's bytearray copy
"""
2017-01-30 12:36:56 +03:00
if isinstance(something, bytes):
return something
if isinstance(something, str):
return something.encode(encoding)
elif isinstance(something, bytearray):
return bytes(something)
else:
raise TypeError("Not a string or bytes like object")
2017-01-22 21:25:24 +03:00
bfh = bytes.fromhex
2017-01-22 21:25:24 +03:00
def bh2u(x: bytes) -> str:
2017-01-22 21:25:24 +03:00
"""
str with hex representation of a bytes-like object
2017-01-22 21:25:24 +03:00
>>> x = bytes((1, 2, 10))
>>> bh2u(x)
'01020A'
2017-01-22 21:25:24 +03:00
"""
2019-02-01 18:07:28 +01:00
return x.hex()
2017-01-22 21:25:24 +03:00
2018-05-04 13:01:46 +02:00
def xor_bytes(a: bytes, b: bytes) -> bytes:
size = min(len(a), len(b))
return ((int.from_bytes(a[:size], "big") ^ int.from_bytes(b[:size], "big"))
.to_bytes(size, "big"))
def user_dir():
2020-02-25 16:28:53 +01:00
if "ELECTRUMDIR" in os.environ:
return os.environ["ELECTRUMDIR"]
elif 'ANDROID_DATA' in os.environ:
2018-11-26 17:52:30 +01:00
return android_data_dir()
2017-01-04 22:37:59 +01:00
elif os.name == 'posix':
2012-11-18 11:34:52 +01:00
return os.path.join(os.environ["HOME"], ".electrum")
elif "APPDATA" in os.environ:
2012-11-18 11:34:52 +01:00
return os.path.join(os.environ["APPDATA"], "Electrum")
elif "LOCALAPPDATA" in os.environ:
return os.path.join(os.environ["LOCALAPPDATA"], "Electrum")
else:
2013-11-09 20:21:02 -08:00
#raise Exception("No home directory found in environment variables.")
2014-04-11 20:20:52 -04:00
return
2019-02-01 19:01:21 +01:00
def resource_path(*parts):
return os.path.join(pkg_dir, *parts)
2019-02-01 19:01:21 +01:00
# absolute path to python package folder of electrum ("lib")
pkg_dir = os.path.split(os.path.realpath(__file__))[0]
2019-02-01 19:01:21 +01:00
2018-05-18 18:07:52 +02:00
def is_valid_email(s):
regexp = r"[^@]+@[^@]+\.[^@]+"
return re.match(regexp, s) is not None
2017-01-22 21:25:24 +03:00
2019-05-03 03:10:31 +02:00
def is_hash256_str(text: Any) -> bool:
if not isinstance(text, str): return False
if len(text) != 64: return False
2019-05-03 03:10:31 +02:00
return is_hex_str(text)
def is_hex_str(text: Any) -> bool:
if not isinstance(text, str): return False
try:
2021-01-27 20:04:08 +01:00
b = bytes.fromhex(text)
except:
return False
2021-01-27 20:04:08 +01:00
# forbid whitespaces in text:
if len(text) != 2 * len(b):
return False
return True
def is_integer(val: Any) -> bool:
return isinstance(val, int)
def is_non_negative_integer(val: Any) -> bool:
if is_integer(val):
return val >= 0
return False
def is_int_or_float(val: Any) -> bool:
return isinstance(val, (int, float))
def is_non_negative_int_or_float(val: Any) -> bool:
if is_int_or_float(val):
return val >= 0
return False
2019-05-26 04:10:32 +02:00
def chunks(items, size: int):
"""Break up items, an iterable, into chunks of length size."""
if size < 1:
raise ValueError(f"size must be positive, not {repr(size)}")
for i in range(0, len(items), size):
yield items[i: i + size]
2020-06-22 02:46:16 +02:00
def format_satoshis_plain(x, *, decimal_point=8) -> str:
2017-01-22 21:25:24 +03:00
"""Display a satoshi amount scaled. Always uses a '.' as a decimal
point and has no thousands separator"""
2019-11-20 19:29:29 +01:00
if x == '!':
return 'max'
2015-05-27 16:34:42 +09:00
scale_factor = pow(10, decimal_point)
return "{:.8f}".format(Decimal(x) / scale_factor).rstrip('0').rstrip('.')
2017-01-22 21:25:24 +03:00
# Check that Decimal precision is sufficient.
# We need at the very least ~20, as we deal with msat amounts, and
# log10(21_000_000 * 10**8 * 1000) ~= 18.3
# decimal.DefaultContext.prec == 28 by default, but it is mutable.
# We enforce that we have at least that available.
assert decimal.getcontext().prec >= 28, f"PyDecimal precision too low: {decimal.getcontext().prec}"
DECIMAL_POINT = localeconv()['decimal_point'] # type: str
2020-06-22 02:46:16 +02:00
def format_satoshis(
x, # in satoshis
*,
num_zeros=0,
decimal_point=8,
precision=None,
is_diff=False,
whitespaces=False,
) -> str:
2015-05-02 15:12:00 +02:00
if x is None:
return 'unknown'
if x == '!':
return 'max'
if precision is None:
precision = decimal_point
2018-10-07 17:50:52 +02:00
# format string
2018-10-13 04:22:53 +02:00
decimal_format = "." + str(precision) if precision > 0 else ""
if is_diff:
decimal_format = '+' + decimal_format
2018-10-07 17:50:52 +02:00
# initial result
scale_factor = pow(10, decimal_point)
2018-10-07 18:43:35 +02:00
if not isinstance(x, Decimal):
x = Decimal(x).quantize(Decimal('1E-8'))
result = ("{:" + decimal_format + "f}").format(x / scale_factor)
2018-10-07 17:50:52 +02:00
if "." not in result: result += "."
result = result.rstrip('0')
# extra decimal places
integer_part, fract_part = result.split(".")
if len(fract_part) < num_zeros:
fract_part += "0" * (num_zeros - len(fract_part))
2018-10-07 17:50:52 +02:00
result = integer_part + DECIMAL_POINT + fract_part
# leading/trailing whitespaces
2013-07-13 20:19:52 +02:00
if whitespaces:
result += " " * (decimal_point - len(fract_part))
2015-04-29 09:26:22 +02:00
result = " " * (15 - len(result)) + result
2017-01-22 21:25:24 +03:00
return result
2018-05-09 19:30:18 +02:00
FEERATE_PRECISION = 1 # num fractional decimal places for sat/byte fee rates
_feerate_quanta = Decimal(10) ** (-FEERATE_PRECISION)
2018-10-13 04:21:07 +02:00
def format_fee_satoshis(fee, *, num_zeros=0, precision=None):
if precision is None:
precision = FEERATE_PRECISION
num_zeros = min(num_zeros, FEERATE_PRECISION) # no more zeroes than available prec
return format_satoshis(fee, num_zeros=num_zeros, decimal_point=0, precision=precision)
2018-05-09 19:30:18 +02:00
2019-11-19 19:29:10 +01:00
def quantize_feerate(fee) -> Union[None, Decimal, int]:
2018-05-09 19:30:18 +02:00
"""Strip sat/byte fee rate of excess precision."""
if fee is None:
return None
return Decimal(fee).quantize(_feerate_quanta, rounding=decimal.ROUND_HALF_DOWN)
def timestamp_to_datetime(timestamp: Optional[int]) -> Optional[datetime]:
2018-02-24 22:37:03 +01:00
if timestamp is None:
return None
2018-02-21 11:52:40 +01:00
return datetime.fromtimestamp(timestamp)
2015-09-05 14:05:37 +09:00
def format_time(timestamp):
date = timestamp_to_datetime(timestamp)
return date.isoformat(' ')[:-3] if date else _("Unknown")
2015-04-03 14:44:03 +02:00
# Takes a timestamp and returns a string with the approximation of the age
def age(from_date, since_date = None, target_tz=None, include_seconds=False):
if from_date is None:
return "Unknown"
from_date = datetime.fromtimestamp(from_date)
if since_date is None:
since_date = datetime.now(target_tz)
td = time_difference(from_date - since_date, include_seconds)
return td + " ago" if from_date < since_date else "in " + td
def time_difference(distance_in_time, include_seconds):
#distance_in_time = since_date - from_date
distance_in_seconds = int(round(abs(distance_in_time.days * 86400 + distance_in_time.seconds)))
distance_in_minutes = int(round(distance_in_seconds/60))
if distance_in_minutes == 0:
if include_seconds:
return "%s seconds" % distance_in_seconds
else:
return "less than a minute"
elif distance_in_minutes < 45:
return "%s minutes" % distance_in_minutes
elif distance_in_minutes < 90:
return "about 1 hour"
elif distance_in_minutes < 1440:
return "about %d hours" % (round(distance_in_minutes / 60.0))
elif distance_in_minutes < 2880:
return "1 day"
elif distance_in_minutes < 43220:
return "%d days" % (round(distance_in_minutes / 1440))
elif distance_in_minutes < 86400:
return "about 1 month"
elif distance_in_minutes < 525600:
return "%d months" % (round(distance_in_minutes / 43200))
elif distance_in_minutes < 1051200:
2015-07-14 09:00:59 +09:00
return "about 1 year"
else:
return "over %d years" % (round(distance_in_minutes / 525600))
2013-03-14 16:32:05 +01:00
2017-06-30 12:11:47 +02:00
mainnet_block_explorers = {
2018-10-25 18:09:52 +03:00
'Bitupper Explorer': ('https://bitupper.com/en/explorer/bitcoin/',
{'tx': 'transactions/', 'addr': 'addresses/'}),
2018-02-01 03:28:44 +01:00
'Bitflyer.jp': ('https://chainflyer.bitflyer.jp/',
{'tx': 'Transaction/', 'addr': 'Address/'}),
'Blockchain.info': ('https://blockchain.com/btc/',
2018-02-01 03:28:44 +01:00
{'tx': 'tx/', 'addr': 'address/'}),
'blockchainbdgpzk.onion': ('https://blockchainbdgpzk.onion/',
{'tx': 'tx/', 'addr': 'address/'}),
'Blockstream.info': ('https://blockstream.info/',
{'tx': 'tx/', 'addr': 'address/'}),
2019-04-24 19:05:11 +04:00
'Bitaps.com': ('https://btc.bitaps.com/',
{'tx': '', 'addr': ''}),
'BTC.com': ('https://btc.com/',
{'tx': '', 'addr': ''}),
2018-02-01 03:28:44 +01:00
'Chain.so': ('https://www.chain.so/',
{'tx': 'tx/BTC/', 'addr': 'address/BTC/'}),
'Insight.is': ('https://insight.bitpay.com/',
{'tx': 'tx/', 'addr': 'address/'}),
'TradeBlock.com': ('https://tradeblock.com/blockchain/',
{'tx': 'tx/', 'addr': 'address/'}),
'BlockCypher.com': ('https://live.blockcypher.com/btc/',
{'tx': 'tx/', 'addr': 'address/'}),
'Blockchair.com': ('https://blockchair.com/bitcoin/',
{'tx': 'transaction/', 'addr': 'address/'}),
'blockonomics.co': ('https://www.blockonomics.co/',
{'tx': 'api/tx?txid=', 'addr': '#/search?q='}),
'mempool.space': ('https://mempool.space/',
{'tx': 'tx/', 'addr': 'address/'}),
2020-11-22 01:43:47 +01:00
'mempool.emzy.de': ('https://mempool.emzy.de/',
2021-03-22 08:25:26 +01:00
{'tx': 'tx/', 'addr': 'address/'}),
2018-07-10 21:52:06 +02:00
'OXT.me': ('https://oxt.me/',
{'tx': 'transaction/', 'addr': 'address/'}),
'smartbit.com.au': ('https://www.smartbit.com.au/',
{'tx': 'tx/', 'addr': 'address/'}),
'mynode.local': ('http://mynode.local:3002/',
{'tx': 'tx/', 'addr': 'address/'}),
2018-02-01 03:28:44 +01:00
'system default': ('blockchain:/',
{'tx': 'tx/', 'addr': 'address/'}),
}
2017-06-30 12:11:47 +02:00
testnet_block_explorers = {
2019-04-24 19:05:11 +04:00
'Bitaps.com': ('https://tbtc.bitaps.com/',
{'tx': '', 'addr': ''}),
'BlockCypher.com': ('https://live.blockcypher.com/btc-testnet/',
{'tx': 'tx/', 'addr': 'address/'}),
2020-07-05 12:59:09 +02:00
'Blockchain.info': ('https://www.blockchain.com/btc-testnet/',
{'tx': 'tx/', 'addr': 'address/'}),
'Blockstream.info': ('https://blockstream.info/testnet/',
{'tx': 'tx/', 'addr': 'address/'}),
'mempool.space': ('https://mempool.space/testnet/',
2021-03-22 08:25:26 +01:00
{'tx': 'tx/', 'addr': 'address/'}),
'smartbit.com.au': ('https://testnet.smartbit.com.au/',
{'tx': 'tx/', 'addr': 'address/'}),
2018-02-01 03:28:44 +01:00
'system default': ('blockchain://000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943/',
{'tx': 'tx/', 'addr': 'address/'}),
2017-06-30 12:11:47 +02:00
}
2021-05-02 05:54:58 +09:00
signet_block_explorers = {
'bc-2.jp': ('https://explorer.bc-2.jp/',
{'tx': 'tx/', 'addr': 'address/'}),
'mempool.space': ('https://mempool.space/signet/',
{'tx': 'tx/', 'addr': 'address/'}),
'bitcoinexplorer.org': ('https://signet.bitcoinexplorer.org/',
{'tx': 'tx/', 'addr': 'address/'}),
'wakiyamap.dev': ('https://signet-explorer.wakiyamap.dev/',
{'tx': 'tx/', 'addr': 'address/'}),
'system default': ('blockchain:/',
{'tx': 'tx/', 'addr': 'address/'}),
}
2021-01-24 06:45:48 +01:00
_block_explorer_default_api_loc = {'tx': 'tx/', 'addr': 'address/'}
2017-06-30 12:11:47 +02:00
def block_explorer_info():
2018-03-04 22:10:59 +01:00
from . import constants
2021-05-02 05:54:58 +09:00
if constants.net.NET_NAME == "testnet":
return testnet_block_explorers
elif constants.net.NET_NAME == "signet":
return signet_block_explorers
return mainnet_block_explorers
2017-06-30 12:11:47 +02:00
2021-01-24 06:45:48 +01:00
def block_explorer(config: 'SimpleConfig') -> Optional[str]:
"""Returns name of selected block explorer,
or None if a custom one (not among hardcoded ones) is configured.
"""
if config.get('block_explorer_custom') is not None:
return None
default_ = 'Blockstream.info'
be_key = config.get('block_explorer', default_)
2021-01-24 06:45:48 +01:00
be_tuple = block_explorer_info().get(be_key)
if be_tuple is None:
be_key = default_
assert isinstance(be_key, str), f"{be_key!r} should be str"
return be_key
def block_explorer_tuple(config: 'SimpleConfig') -> Optional[Tuple[str, dict]]:
2021-01-24 06:45:48 +01:00
custom_be = config.get('block_explorer_custom')
if custom_be:
if isinstance(custom_be, str):
return custom_be, _block_explorer_default_api_loc
if isinstance(custom_be, (tuple, list)) and len(custom_be) == 2:
return tuple(custom_be)
_logger.warning(f"not using 'block_explorer_custom' from config. "
f"expected a str or a pair but got {custom_be!r}")
return None
else:
# using one of the hardcoded block explorers
return block_explorer_info().get(block_explorer(config))
def block_explorer_URL(config: 'SimpleConfig', kind: str, item: str) -> Optional[str]:
be_tuple = block_explorer_tuple(config)
if not be_tuple:
return
explorer_url, explorer_dict = be_tuple
kind_str = explorer_dict.get(kind)
if kind_str is None:
return
2021-01-24 06:45:48 +01:00
if explorer_url[-1] != "/":
explorer_url += "/"
url_parts = [explorer_url, kind_str, item]
2018-02-01 03:28:44 +01:00
return ''.join(url_parts)
2013-03-14 16:32:05 +01:00
# URL decode
2014-05-07 15:26:38 +02:00
#_ud = re.compile('%([0-9a-hA-H]{2})', re.MULTILINE)
#urldecode = lambda x: _ud.sub(lambda m: chr(int(m.group(1), 16)), x)
2013-03-14 16:32:05 +01:00
2020-12-09 16:09:12 +01:00
# note: when checking against these, use .lower() to support case-insensitivity
BITCOIN_BIP21_URI_SCHEME = 'bitcoin'
LIGHTNING_URI_SCHEME = 'lightning'
2019-05-25 04:55:36 +02:00
class InvalidBitcoinURI(Exception): pass
2020-06-22 22:37:58 +02:00
# TODO rename to parse_bip21_uri or similar
2019-05-25 04:55:36 +02:00
def parse_URI(uri: str, on_pr: Callable = None, *, loop=None) -> dict:
"""Raises InvalidBitcoinURI on malformed URI."""
2017-01-22 21:25:24 +03:00
from . import bitcoin
2021-04-28 08:09:01 +02:00
from .bitcoin import COIN, TOTAL_COIN_SUPPLY_LIMIT_IN_BTC
2013-03-14 16:32:05 +01:00
2019-05-25 04:55:36 +02:00
if not isinstance(uri, str):
raise InvalidBitcoinURI(f"expected string, not {repr(uri)}")
2014-06-13 16:02:30 +02:00
if ':' not in uri:
2016-02-13 15:09:42 +01:00
if not bitcoin.is_address(uri):
2019-05-25 04:55:36 +02:00
raise InvalidBitcoinURI("Not a bitcoin address")
return {'address': uri}
2014-06-13 16:02:30 +02:00
2017-09-04 14:43:31 +02:00
u = urllib.parse.urlparse(uri)
2020-12-09 16:09:12 +01:00
if u.scheme.lower() != BITCOIN_BIP21_URI_SCHEME:
2019-05-25 04:55:36 +02:00
raise InvalidBitcoinURI("Not a bitcoin URI")
2014-05-07 15:26:38 +02:00
address = u.path
2015-02-24 18:41:29 +01:00
# python for android fails to parse query
if address.find('?') > 0:
address, query = u.path.split('?')
2017-09-04 14:43:31 +02:00
pq = urllib.parse.parse_qs(query)
2015-02-24 18:41:29 +01:00
else:
2017-09-04 14:43:31 +02:00
pq = urllib.parse.parse_qs(u.query)
2014-05-07 15:26:38 +02:00
for k, v in pq.items():
2019-05-25 04:55:36 +02:00
if len(v) != 1:
raise InvalidBitcoinURI(f'Duplicate Key: {repr(k)}')
out = {k: v[0] for k, v in pq.items()}
if address:
if not bitcoin.is_address(address):
2019-05-25 04:55:36 +02:00
raise InvalidBitcoinURI(f"Invalid bitcoin address: {address}")
out['address'] = address
if 'amount' in out:
am = out['amount']
2019-05-25 04:55:36 +02:00
try:
m = re.match(r'([0-9.]+)X([0-9])', am)
if m:
k = int(m.group(2)) - 8
2021-03-21 00:34:25 -04:00
amount = Decimal(m.group(1)) * pow(Decimal(10), k)
2019-05-25 04:55:36 +02:00
else:
amount = Decimal(am) * COIN
2021-04-28 08:09:01 +02:00
if amount > TOTAL_COIN_SUPPLY_LIMIT_IN_BTC * COIN:
raise InvalidBitcoinURI(f"amount is out-of-bounds: {amount!r} BTC")
2019-05-25 04:55:36 +02:00
out['amount'] = int(amount)
except Exception as e:
raise InvalidBitcoinURI(f"failed to parse 'amount' field: {repr(e)}") from e
if 'message' in out:
2017-09-04 14:43:31 +02:00
out['message'] = out['message']
out['memo'] = out['message']
2015-07-21 12:26:37 +02:00
if 'time' in out:
2019-05-25 04:55:36 +02:00
try:
out['time'] = int(out['time'])
except Exception as e:
raise InvalidBitcoinURI(f"failed to parse 'time' field: {repr(e)}") from e
2015-07-21 12:26:37 +02:00
if 'exp' in out:
2019-05-25 04:55:36 +02:00
try:
out['exp'] = int(out['exp'])
except Exception as e:
raise InvalidBitcoinURI(f"failed to parse 'exp' field: {repr(e)}") from e
if 'sig' in out:
2019-05-25 04:55:36 +02:00
try:
out['sig'] = bh2u(bitcoin.base_decode(out['sig'], base=58))
2019-05-25 04:55:36 +02:00
except Exception as e:
raise InvalidBitcoinURI(f"failed to parse 'sig' field: {repr(e)}") from e
2015-12-11 15:21:21 +01:00
r = out.get('r')
sig = out.get('sig')
name = out.get('name')
2017-05-19 09:10:19 +02:00
if on_pr and (r or (name and sig)):
@log_exceptions
async def get_payment_request():
2017-01-22 21:25:24 +03:00
from . import paymentrequest as pr
2015-12-11 15:21:21 +01:00
if name and sig:
s = pr.serialize_request(out).SerializeToString()
request = pr.PaymentRequest(s)
else:
request = await pr.get_payment_request(r)
2017-02-05 13:38:44 +03:00
if on_pr:
on_pr(request)
2019-05-25 04:55:36 +02:00
loop = loop or asyncio.get_event_loop()
asyncio.run_coroutine_threadsafe(get_payment_request(), loop)
2015-12-11 15:21:21 +01:00
return out
2013-03-14 16:32:05 +01:00
def create_bip21_uri(addr, amount_sat: Optional[int], message: Optional[str],
*, extra_query_params: Optional[dict] = None) -> str:
2017-01-22 21:25:24 +03:00
from . import bitcoin
2015-02-25 10:01:59 +01:00
if not bitcoin.is_address(addr):
return ""
if extra_query_params is None:
extra_query_params = {}
2015-02-25 10:01:59 +01:00
query = []
if amount_sat:
query.append('amount=%s'%format_satoshis_plain(amount_sat))
2015-02-25 10:01:59 +01:00
if message:
2017-09-04 06:13:55 -04:00
query.append('message=%s'%urllib.parse.quote(message))
for k, v in extra_query_params.items():
if not isinstance(k, str) or k != urllib.parse.quote(k):
raise Exception(f"illegal key for URI: {repr(k)}")
v = urllib.parse.quote(v)
query.append(f"{k}={v}")
2020-12-09 16:09:12 +01:00
p = urllib.parse.ParseResult(
scheme=BITCOIN_BIP21_URI_SCHEME,
netloc='',
path=addr,
params='',
query='&'.join(query),
fragment='',
)
return str(urllib.parse.urlunparse(p))
2015-02-25 10:01:59 +01:00
2019-11-20 03:21:59 +01:00
def maybe_extract_bolt11_invoice(data: str) -> Optional[str]:
data = data.strip() # whitespaces
data = data.lower()
2020-12-09 16:09:12 +01:00
if data.startswith(LIGHTNING_URI_SCHEME + ':ln'):
data = data[10:]
if data.startswith('ln'):
return data
2019-11-20 03:21:59 +01:00
return None
# Python bug (http://bugs.python.org/issue1927) causes raw_input
# to be redirected improperly between stdin/stderr on Unix systems
2017-01-22 21:25:24 +03:00
#TODO: py3
def raw_input(prompt=None):
if prompt:
sys.stdout.write(prompt)
return builtin_raw_input()
2017-01-22 21:25:24 +03:00
2017-09-04 14:43:31 +02:00
builtin_raw_input = builtins.input
builtins.input = raw_input
2014-07-26 16:24:22 +02:00
def parse_json(message):
2017-01-30 12:36:56 +03:00
# TODO: check \r\n pattern
n = message.find(b'\n')
if n==-1:
2014-07-26 16:24:22 +02:00
return None, message
try:
2017-01-30 12:36:56 +03:00
j = json.loads(message[0:n].decode('utf8'))
2014-07-26 16:24:22 +02:00
except:
j = None
return j, message[n+1:]
2014-07-27 11:33:02 +02:00
2017-03-28 15:49:50 +02:00
def setup_thread_excepthook():
"""
Workaround for `sys.excepthook` thread bug from:
http://bugs.python.org/issue1230540
Call once from the main thread before creating any threads.
"""
init_original = threading.Thread.__init__
def init(self, *args, **kwargs):
init_original(self, *args, **kwargs)
run_original = self.run
def run_with_except_hook(*args2, **kwargs2):
try:
run_original(*args2, **kwargs2)
except Exception:
sys.excepthook(*sys.exc_info())
self.run = run_with_except_hook
2018-02-07 17:51:52 +01:00
threading.Thread.__init__ = init
def send_exception_to_crash_reporter(e: BaseException):
sys.excepthook(type(e), e, e.__traceback__)
2018-02-07 17:51:52 +01:00
def versiontuple(v):
return tuple(map(int, (v.split("."))))
2018-02-15 18:07:00 +01:00
2018-02-21 18:55:37 +01:00
2020-06-05 01:28:00 +02:00
def read_json_file(path):
2018-02-15 18:07:00 +01:00
try:
with open(path, 'r', encoding='utf-8') as f:
2020-06-05 01:28:00 +02:00
data = json.loads(f.read())
2018-02-15 18:07:00 +01:00
#backwards compatibility for JSONDecodeError
except ValueError:
2019-04-26 18:52:26 +02:00
_logger.exception('')
2018-02-15 18:07:00 +01:00
raise FileImportFailed(_("Invalid JSON code."))
except BaseException as e:
2019-04-26 18:52:26 +02:00
_logger.exception('')
2018-02-21 18:55:37 +01:00
raise FileImportFailed(e)
2020-06-05 01:28:00 +02:00
return data
2018-02-21 18:55:37 +01:00
2020-06-05 01:28:00 +02:00
def write_json_file(path, data):
2018-02-21 18:55:37 +01:00
try:
2020-06-05 01:28:00 +02:00
with open(path, 'w+', encoding='utf-8') as f:
json.dump(data, f, indent=4, sort_keys=True, cls=MyEncoder)
2018-02-21 18:55:37 +01:00
except (IOError, os.error) as e:
2019-04-26 18:52:26 +02:00
_logger.exception('')
2018-02-21 18:55:37 +01:00
raise FileExportFailed(e)
def make_dir(path, allow_symlink=True):
"""Make directory if it does not yet exist."""
if not os.path.exists(path):
if not allow_symlink and os.path.islink(path):
raise Exception('Dangling link: ' + path)
os.mkdir(path)
os.chmod(path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR)
def log_exceptions(func):
"""Decorator to log AND re-raise exceptions."""
assert asyncio.iscoroutinefunction(func), 'func needs to be a coroutine'
2021-03-04 16:44:13 +01:00
@functools.wraps(func)
async def wrapper(*args, **kwargs):
self = args[0] if len(args) > 0 else None
try:
return await func(*args, **kwargs)
except asyncio.CancelledError as e:
raise
except BaseException as e:
2019-04-26 18:52:26 +02:00
mylogger = self.logger if hasattr(self, 'logger') else _logger
2018-09-16 22:17:20 +02:00
try:
2019-04-26 18:52:26 +02:00
mylogger.exception(f"Exception in {func.__name__}: {repr(e)}")
2018-09-16 22:17:20 +02:00
except BaseException as e2:
2019-04-26 18:52:26 +02:00
print(f"logging exception raised: {repr(e2)}... orig exc: {repr(e)} in {func.__name__}")
raise
return wrapper
def ignore_exceptions(func):
"""Decorator to silently swallow all exceptions."""
assert asyncio.iscoroutinefunction(func), 'func needs to be a coroutine'
2021-03-04 16:44:13 +01:00
@functools.wraps(func)
async def wrapper(*args, **kwargs):
try:
return await func(*args, **kwargs)
except asyncio.CancelledError:
# note: with python 3.8, CancelledError no longer inherits Exception, so this catch is redundant
raise
except Exception as e:
pass
return wrapper
2021-03-22 08:25:26 +01:00
def with_lock(func):
"""Decorator to enforce a lock on a function call."""
def func_wrapper(self, *args, **kwargs):
with self.lock:
return func(self, *args, **kwargs)
return func_wrapper
class TxMinedInfo(NamedTuple):
height: int # height of block that mined tx
conf: Optional[int] = None # number of confirmations, SPV verified (None means unknown)
timestamp: Optional[int] = None # timestamp of block that mined tx
txpos: Optional[int] = None # position of tx in serialized block
header_hash: Optional[str] = None # hash of block that mined tx
2018-10-03 17:13:46 +02:00
2019-02-27 21:48:33 +01:00
def make_aiohttp_session(proxy: Optional[dict], headers=None, timeout=None):
2018-10-03 17:13:46 +02:00
if headers is None:
headers = {'User-Agent': 'Electrum'}
if timeout is None:
# The default timeout is high intentionally.
# DNS on some systems can be really slow, see e.g. #5337
timeout = aiohttp.ClientTimeout(total=45)
elif isinstance(timeout, (int, float)):
timeout = aiohttp.ClientTimeout(total=timeout)
ssl_context = ssl.create_default_context(purpose=ssl.Purpose.SERVER_AUTH, cafile=ca_path)
if proxy:
connector = ProxyConnector(
proxy_type=ProxyType.SOCKS5 if proxy['mode'] == 'socks5' else ProxyType.SOCKS4,
host=proxy['host'],
port=int(proxy['port']),
username=proxy.get('user', None),
password=proxy.get('password', None),
rdns=True,
2019-03-27 18:41:42 +01:00
ssl=ssl_context,
)
else:
2019-03-27 18:41:42 +01:00
connector = aiohttp.TCPConnector(ssl=ssl_context)
return aiohttp.ClientSession(headers=headers, timeout=timeout, connector=connector)
2018-09-12 19:24:58 +02:00
class SilentTaskGroup(TaskGroup):
def spawn(self, *args, **kwargs):
# don't complain if group is already closed.
if self._closed:
raise asyncio.CancelledError()
return super().spawn(*args, **kwargs)
2018-10-12 16:09:41 +02:00
class NetworkJobOnDefaultServer(Logger, ABC):
2018-10-12 16:09:41 +02:00
"""An abstract base class for a job that runs on the main network
interface. Every time the main interface changes, the job is
restarted, and some of its internals are reset.
"""
2018-10-22 16:41:25 +02:00
def __init__(self, network: 'Network'):
2019-04-26 18:52:26 +02:00
Logger.__init__(self)
2018-10-12 16:09:41 +02:00
asyncio.set_event_loop(network.asyncio_loop)
self.network = network
2018-10-22 16:41:25 +02:00
self.interface = None # type: Interface
2018-10-12 16:09:41 +02:00
self._restart_lock = asyncio.Lock()
# Ensure fairness between NetworkJobs. e.g. if multiple wallets
# are open, a large wallet's Synchronizer should not starve the small wallets:
self._network_request_semaphore = asyncio.Semaphore(100)
2018-10-12 16:09:41 +02:00
self._reset()
# every time the main interface changes, restart:
2020-04-14 16:12:47 +02:00
register_callback(self._restart, ['default_server_changed'])
# also schedule a one-off restart now, as there might already be a main interface:
asyncio.run_coroutine_threadsafe(self._restart(), network.asyncio_loop)
2018-10-12 16:09:41 +02:00
def _reset(self):
"""Initialise fields. Called every time the underlying
server connection changes.
"""
self.taskgroup = SilentTaskGroup()
2018-10-12 16:09:41 +02:00
2018-10-22 16:41:25 +02:00
async def _start(self, interface: 'Interface'):
2018-10-12 16:09:41 +02:00
self.interface = interface
await interface.taskgroup.spawn(self._run_tasks(taskgroup=self.taskgroup))
2018-10-12 16:09:41 +02:00
@abstractmethod
async def _run_tasks(self, *, taskgroup: TaskGroup) -> None:
"""Start tasks in taskgroup. Called every time the underlying
2018-10-12 16:09:41 +02:00
server connection changes.
"""
# If self.taskgroup changed, don't start tasks. This can happen if we have
# been restarted *just now*, i.e. after the _run_tasks coroutine object was created.
if taskgroup != self.taskgroup:
raise asyncio.CancelledError()
2018-10-12 16:09:41 +02:00
async def stop(self, *, full_shutdown: bool = True):
if full_shutdown:
unregister_callback(self._restart)
await self.taskgroup.cancel_remaining()
2018-10-12 16:09:41 +02:00
@log_exceptions
2018-10-12 16:09:41 +02:00
async def _restart(self, *args):
interface = self.network.interface
if interface is None:
return # we should get called again soon
async with self._restart_lock:
await self.stop(full_shutdown=False)
2018-10-12 16:09:41 +02:00
self._reset()
await self._start(interface)
@property
def session(self):
s = self.interface.session
assert s is not None
return s
2018-11-02 20:14:59 +01:00
def create_and_start_event_loop() -> Tuple[asyncio.AbstractEventLoop,
asyncio.Future,
threading.Thread]:
def on_exception(loop, context):
"""Suppress spurious messages it appears we cannot control."""
SUPPRESS_MESSAGE_REGEX = re.compile('SSL handshake|Fatal read error on|'
'SSL error in data received')
message = context.get('message')
if message and SUPPRESS_MESSAGE_REGEX.match(message):
return
loop.default_exception_handler(context)
loop = asyncio.get_event_loop()
loop.set_exception_handler(on_exception)
# loop.set_debug(1)
stopping_fut = asyncio.Future()
loop_thread = threading.Thread(target=loop.run_until_complete,
args=(stopping_fut,),
name='EventLoop')
loop_thread.start()
loop._mythread = loop_thread
2018-11-02 20:14:59 +01:00
return loop, stopping_fut, loop_thread
class OrderedDictWithIndex(OrderedDict):
"""An OrderedDict that keeps track of the positions of keys.
Note: very inefficient to modify contents, except to add new items.
"""
2018-12-08 04:17:05 +01:00
def __init__(self):
super().__init__()
self._key_to_pos = {}
self._pos_to_key = {}
def _recalc_index(self):
self._key_to_pos = {key: pos for (pos, key) in enumerate(self.keys())}
self._pos_to_key = {pos: key for (pos, key) in enumerate(self.keys())}
def pos_from_key(self, key):
return self._key_to_pos[key]
def value_from_pos(self, pos):
key = self._pos_to_key[pos]
return self[key]
def popitem(self, *args, **kwargs):
ret = super().popitem(*args, **kwargs)
self._recalc_index()
return ret
def move_to_end(self, *args, **kwargs):
ret = super().move_to_end(*args, **kwargs)
self._recalc_index()
return ret
def clear(self):
ret = super().clear()
self._recalc_index()
return ret
def pop(self, *args, **kwargs):
ret = super().pop(*args, **kwargs)
self._recalc_index()
return ret
def update(self, *args, **kwargs):
ret = super().update(*args, **kwargs)
self._recalc_index()
return ret
def __delitem__(self, *args, **kwargs):
ret = super().__delitem__(*args, **kwargs)
self._recalc_index()
return ret
def __setitem__(self, key, *args, **kwargs):
is_new_key = key not in self
ret = super().__setitem__(key, *args, **kwargs)
if is_new_key:
pos = len(self) - 1
self._key_to_pos[key] = pos
self._pos_to_key[pos] = key
return ret
2019-02-19 11:56:46 +01:00
def multisig_type(wallet_type):
'''If wallet_type is mofn multi-sig, return [m, n],
otherwise return None.'''
if not wallet_type:
return None
match = re.match(r'(\d+)of(\d+)', wallet_type)
if match:
match = [int(x) for x in match.group(1, 2)]
return match
2018-07-26 21:08:25 +02:00
def is_ip_address(x: Union[str, bytes]) -> bool:
if isinstance(x, bytes):
x = x.decode("utf-8")
try:
ipaddress.ip_address(x)
return True
except ValueError:
return False
def is_private_netaddress(host: str) -> bool:
if str(host) in ('localhost', 'localhost.',):
return True
if host[0] == '[' and host[-1] == ']': # IPv6
host = host[1:-1]
try:
ip_addr = ipaddress.ip_address(host) # type: Union[IPv4Address, IPv6Address]
return ip_addr.is_private
except ValueError:
pass # not an IP
return False
2018-07-26 21:08:25 +02:00
def list_enabled_bits(x: int) -> Sequence[int]:
"""e.g. 77 (0b1001101) --> (0, 2, 3, 6)"""
binary = bin(x)[2:]
rev_bin = reversed(binary)
return tuple(i for i, b in enumerate(rev_bin) if b == '1')
def resolve_dns_srv(host: str):
srv_records = dns.resolver.resolve(host, 'SRV')
# priority: prefer lower
# weight: tie breaker; prefer higher
srv_records = sorted(srv_records, key=lambda x: (x.priority, -x.weight))
def dict_from_srv_record(srv):
return {
'host': str(srv.target),
'port': srv.port,
}
return [dict_from_srv_record(srv) for srv in srv_records]
def randrange(bound: int) -> int:
"""Return a random integer k such that 1 <= k < bound, uniformly
distributed across that range."""
# secrets.randbelow(bound) returns a random int: 0 <= r < bound,
# hence transformations:
return secrets.randbelow(bound - 1) + 1
2020-04-14 16:12:47 +02:00
class CallbackManager:
2021-03-11 18:50:59 +01:00
# callbacks set by the GUI or any thread
# guarantee: the callbacks will always get triggered from the asyncio thread.
2020-04-14 16:12:47 +02:00
def __init__(self):
self.callback_lock = threading.Lock()
self.callbacks = defaultdict(list) # note: needs self.callback_lock
self.asyncio_loop = None
def register_callback(self, callback, events):
with self.callback_lock:
for event in events:
self.callbacks[event].append(callback)
def unregister_callback(self, callback):
with self.callback_lock:
for callbacks in self.callbacks.values():
if callback in callbacks:
callbacks.remove(callback)
def trigger_callback(self, event, *args):
2021-03-11 18:50:59 +01:00
"""Trigger a callback with given arguments.
Can be called from any thread. The callback itself will get scheduled
on the event loop.
"""
2020-04-14 16:12:47 +02:00
if self.asyncio_loop is None:
self.asyncio_loop = asyncio.get_event_loop()
assert self.asyncio_loop.is_running(), "event loop not running"
with self.callback_lock:
callbacks = self.callbacks[event][:]
for callback in callbacks:
# FIXME: if callback throws, we will lose the traceback
if asyncio.iscoroutinefunction(callback):
asyncio.run_coroutine_threadsafe(callback(event, *args), self.asyncio_loop)
else:
self.asyncio_loop.call_soon_threadsafe(callback, event, *args)
callback_mgr = CallbackManager()
trigger_callback = callback_mgr.trigger_callback
register_callback = callback_mgr.register_callback
unregister_callback = callback_mgr.unregister_callback
_NetAddrType = TypeVar("_NetAddrType")
class NetworkRetryManager(Generic[_NetAddrType]):
"""Truncated Exponential Backoff for network connections."""
def __init__(
self, *,
max_retry_delay_normal: float,
init_retry_delay_normal: float,
max_retry_delay_urgent: float = None,
init_retry_delay_urgent: float = None,
):
self._last_tried_addr = {} # type: Dict[_NetAddrType, Tuple[float, int]] # (unix ts, num_attempts)
# note: these all use "seconds" as unit
if max_retry_delay_urgent is None:
max_retry_delay_urgent = max_retry_delay_normal
if init_retry_delay_urgent is None:
init_retry_delay_urgent = init_retry_delay_normal
self._max_retry_delay_normal = max_retry_delay_normal
self._init_retry_delay_normal = init_retry_delay_normal
self._max_retry_delay_urgent = max_retry_delay_urgent
self._init_retry_delay_urgent = init_retry_delay_urgent
def _trying_addr_now(self, addr: _NetAddrType) -> None:
last_time, num_attempts = self._last_tried_addr.get(addr, (0, 0))
# we add up to 1 second of noise to the time, so that clients are less likely
# to get synchronised and bombard the remote in connection waves:
cur_time = time.time() + random.random()
self._last_tried_addr[addr] = cur_time, num_attempts + 1
def _on_connection_successfully_established(self, addr: _NetAddrType) -> None:
self._last_tried_addr[addr] = time.time(), 0
def _can_retry_addr(self, addr: _NetAddrType, *,
now: float = None, urgent: bool = False) -> bool:
if now is None:
now = time.time()
last_time, num_attempts = self._last_tried_addr.get(addr, (0, 0))
if urgent:
max_delay = self._max_retry_delay_urgent
init_delay = self._init_retry_delay_urgent
else:
max_delay = self._max_retry_delay_normal
init_delay = self._init_retry_delay_normal
delay = self.__calc_delay(multiplier=init_delay, max_delay=max_delay, num_attempts=num_attempts)
next_time = last_time + delay
return next_time < now
@classmethod
def __calc_delay(cls, *, multiplier: float, max_delay: float,
num_attempts: int) -> float:
num_attempts = min(num_attempts, 100_000)
try:
res = multiplier * 2 ** num_attempts
except OverflowError:
return max_delay
return max(0, min(max_delay, res))
def _clear_addr_retry_times(self) -> None:
self._last_tried_addr.clear()
2020-04-15 21:41:33 +02:00
class MySocksProxy(aiorpcx.SOCKSProxy):
async def open_connection(self, host=None, port=None, **kwargs):
loop = asyncio.get_event_loop()
reader = asyncio.StreamReader(loop=loop)
protocol = asyncio.StreamReaderProtocol(reader, loop=loop)
transport, _ = await self.create_connection(
lambda: protocol, host, port, **kwargs)
writer = asyncio.StreamWriter(transport, protocol, reader, loop)
return reader, writer
@classmethod
def from_proxy_dict(cls, proxy: dict = None) -> Optional['MySocksProxy']:
if not proxy:
return None
username, pw = proxy.get('user'), proxy.get('password')
if not username or not pw:
auth = None
else:
auth = aiorpcx.socks.SOCKSUserAuth(username, pw)
addr = aiorpcx.NetAddress(proxy['host'], proxy['port'])
if proxy['mode'] == "socks4":
ret = cls(addr, aiorpcx.socks.SOCKS4a, auth)
elif proxy['mode'] == "socks5":
ret = cls(addr, aiorpcx.socks.SOCKS5, auth)
else:
raise NotImplementedError # http proxy not available with aiorpcx
return ret
class JsonRPCClient:
def __init__(self, session: aiohttp.ClientSession, url: str):
self.session = session
self.url = url
self._id = 0
async def request(self, endpoint, *args):
self._id += 1
data = ('{"jsonrpc": "2.0", "id":"%d", "method": "%s", "params": %s }'
% (self._id, endpoint, json.dumps(args)))
async with self.session.post(self.url, data=data) as resp:
if resp.status == 200:
r = await resp.json()
result = r.get('result')
error = r.get('error')
if error:
return 'Error: ' + str(error)
else:
return result
else:
text = await resp.text()
return 'Error: ' + str(text)
def add_method(self, endpoint):
async def coro(*args):
return await self.request(endpoint, *args)
setattr(self, endpoint, coro)
T = TypeVar('T')
def random_shuffled_copy(x: Iterable[T]) -> List[T]:
"""Returns a shuffled copy of the input."""
x_copy = list(x) # copy
random.shuffle(x_copy) # shuffle in-place
return x_copy
def test_read_write_permissions(path) -> None:
# note: There might already be a file at 'path'.
# Make sure we do NOT overwrite/corrupt that!
temp_path = "%s.tmptest.%s" % (path, os.getpid())
echo = "fs r/w test"
try:
# test READ permissions for actual path
if os.path.exists(path):
with open(path, "rb") as f:
f.read(1) # read 1 byte
# test R/W sanity for "similar" path
with open(temp_path, "w", encoding='utf-8') as f:
f.write(echo)
with open(temp_path, "r", encoding='utf-8') as f:
echo2 = f.read()
os.remove(temp_path)
except Exception as e:
raise IOError(e) from e
if echo != echo2:
raise IOError('echo sanity-check failed')
2021-03-10 19:01:30 +01:00
class nullcontext:
"""Context manager that does no additional processing.
This is a ~backport of contextlib.nullcontext from Python 3.10
"""
def __init__(self, enter_result=None):
self.enter_result = enter_result
def __enter__(self):
return self.enter_result
def __exit__(self, *excinfo):
pass
async def __aenter__(self):
return self.enter_result
async def __aexit__(self, *excinfo):
pass