2016-02-23 11:36:42 +01:00
|
|
|
# Electrum - Lightweight Bitcoin Client
|
|
|
|
|
# Copyright (c) 2015 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.
|
2015-07-02 12:44:53 +02:00
|
|
|
import re
|
2023-11-01 17:35:45 +00:00
|
|
|
from typing import Optional, Tuple, Dict, Any, TYPE_CHECKING
|
2022-05-30 20:49:08 +02:00
|
|
|
|
2015-07-02 12:44:53 +02:00
|
|
|
import dns
|
2022-04-29 16:56:19 +02:00
|
|
|
import threading
|
2018-03-06 16:07:33 +01:00
|
|
|
from dns.exception import DNSException
|
2015-07-02 12:44:53 +02:00
|
|
|
|
2017-01-22 21:25:24 +03:00
|
|
|
from . import bitcoin
|
|
|
|
|
from . import dnssec
|
2025-05-02 16:12:55 +02:00
|
|
|
from .util import read_json_file, write_json_file, to_string, is_valid_email
|
2023-07-10 17:50:53 +00:00
|
|
|
from .logging import Logger, get_logger
|
2022-04-29 16:56:19 +02:00
|
|
|
from .util import trigger_callback
|
2015-07-08 19:20:54 +02:00
|
|
|
|
2023-11-01 17:35:45 +00:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
from .wallet_db import WalletDB
|
|
|
|
|
from .simple_config import SimpleConfig
|
|
|
|
|
|
2023-06-01 14:51:38 +02:00
|
|
|
|
2023-07-10 17:50:53 +00:00
|
|
|
_logger = get_logger(__name__)
|
|
|
|
|
|
|
|
|
|
|
2023-06-01 14:51:38 +02:00
|
|
|
class AliasNotFoundException(Exception):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
2019-04-26 18:52:26 +02:00
|
|
|
class Contacts(dict, Logger):
|
2015-07-02 12:44:53 +02:00
|
|
|
|
2023-11-01 17:35:45 +00:00
|
|
|
def __init__(self, db: 'WalletDB'):
|
2019-04-26 18:52:26 +02:00
|
|
|
Logger.__init__(self)
|
2020-02-05 15:13:37 +01:00
|
|
|
self.db = db
|
|
|
|
|
d = self.db.get('contacts', {})
|
2017-03-29 10:29:02 +02:00
|
|
|
try:
|
|
|
|
|
self.update(d)
|
2023-04-23 01:33:12 +00:00
|
|
|
except Exception:
|
2017-03-29 10:29:02 +02:00
|
|
|
return
|
2016-05-28 16:56:18 +02:00
|
|
|
# backward compatibility
|
|
|
|
|
for k, v in self.items():
|
|
|
|
|
_type, n = v
|
|
|
|
|
if _type == 'address' and bitcoin.is_address(n):
|
|
|
|
|
self.pop(k)
|
|
|
|
|
self[n] = ('address', k)
|
|
|
|
|
|
2017-03-06 17:12:27 +01:00
|
|
|
def save(self):
|
2020-02-05 15:13:37 +01:00
|
|
|
self.db.put('contacts', dict(self))
|
2017-03-06 17:12:27 +01:00
|
|
|
|
|
|
|
|
def import_file(self, path):
|
2020-06-05 01:28:00 +02:00
|
|
|
data = read_json_file(path)
|
|
|
|
|
data = self._validate(data)
|
2018-02-15 18:07:00 +01:00
|
|
|
self.update(data)
|
2017-03-06 17:12:27 +01:00
|
|
|
self.save()
|
|
|
|
|
|
2020-06-05 01:28:00 +02:00
|
|
|
def export_file(self, path):
|
|
|
|
|
write_json_file(path, self)
|
2018-02-15 18:07:00 +01:00
|
|
|
|
2017-03-06 17:12:27 +01:00
|
|
|
def __setitem__(self, key, value):
|
|
|
|
|
dict.__setitem__(self, key, value)
|
|
|
|
|
self.save()
|
|
|
|
|
|
|
|
|
|
def pop(self, key):
|
|
|
|
|
if key in self.keys():
|
2018-11-27 21:32:55 +01:00
|
|
|
res = dict.pop(self, key)
|
2017-03-06 17:12:27 +01:00
|
|
|
self.save()
|
2018-11-27 21:32:55 +01:00
|
|
|
return res
|
2025-05-02 16:12:55 +02:00
|
|
|
return None
|
2015-07-02 12:44:53 +02:00
|
|
|
|
|
|
|
|
def resolve(self, k):
|
|
|
|
|
if bitcoin.is_address(k):
|
|
|
|
|
return {
|
|
|
|
|
'address': k,
|
|
|
|
|
'type': 'address'
|
|
|
|
|
}
|
2025-05-02 16:12:55 +02:00
|
|
|
for address, (_type, label) in self.items():
|
|
|
|
|
if k.casefold() != label.casefold():
|
|
|
|
|
continue
|
|
|
|
|
if _type in ('address', 'lnaddress'):
|
2015-07-02 12:44:53 +02:00
|
|
|
return {
|
2025-05-02 16:12:55 +02:00
|
|
|
'address': address,
|
2015-07-02 12:44:53 +02:00
|
|
|
'type': 'contact'
|
|
|
|
|
}
|
2023-07-10 17:50:53 +00:00
|
|
|
if openalias := self.resolve_openalias(k):
|
|
|
|
|
return openalias
|
|
|
|
|
raise AliasNotFoundException("Invalid Bitcoin address or alias", k)
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def resolve_openalias(cls, url: str) -> Dict[str, Any]:
|
|
|
|
|
out = cls._resolve_openalias(url)
|
2015-07-02 12:44:53 +02:00
|
|
|
if out:
|
2015-07-08 19:20:54 +02:00
|
|
|
address, name, validated = out
|
2015-07-02 12:44:53 +02:00
|
|
|
return {
|
|
|
|
|
'address': address,
|
|
|
|
|
'name': name,
|
|
|
|
|
'type': 'openalias',
|
|
|
|
|
'validated': validated
|
|
|
|
|
}
|
2023-07-10 17:50:53 +00:00
|
|
|
return {}
|
2015-07-02 12:44:53 +02:00
|
|
|
|
2023-07-07 22:54:46 +02:00
|
|
|
def by_name(self, name):
|
|
|
|
|
for k in self.keys():
|
|
|
|
|
_type, addr = self[k]
|
|
|
|
|
if addr.casefold() == name.casefold():
|
|
|
|
|
return {
|
|
|
|
|
'name': addr,
|
|
|
|
|
'type': _type,
|
|
|
|
|
'address': k
|
|
|
|
|
}
|
|
|
|
|
return None
|
|
|
|
|
|
2023-11-01 17:35:45 +00:00
|
|
|
def fetch_openalias(self, config: 'SimpleConfig'):
|
2022-04-29 16:56:19 +02:00
|
|
|
self.alias_info = None
|
2023-05-24 17:41:44 +00:00
|
|
|
alias = config.OPENALIAS_ID
|
2022-04-29 16:56:19 +02:00
|
|
|
if alias:
|
|
|
|
|
alias = str(alias)
|
|
|
|
|
def f():
|
2023-07-10 17:50:53 +00:00
|
|
|
self.alias_info = self._resolve_openalias(alias)
|
2022-04-29 16:56:19 +02:00
|
|
|
trigger_callback('alias_received')
|
|
|
|
|
t = threading.Thread(target=f)
|
2022-05-23 17:52:39 +02:00
|
|
|
t.daemon = True
|
2022-04-29 16:56:19 +02:00
|
|
|
t.start()
|
|
|
|
|
|
2023-07-10 17:50:53 +00:00
|
|
|
@classmethod
|
|
|
|
|
def _resolve_openalias(cls, url: str) -> Optional[Tuple[str, str, bool]]:
|
2015-07-08 19:20:54 +02:00
|
|
|
# support email-style addresses, per the OA standard
|
|
|
|
|
url = url.replace('@', '.')
|
2018-03-06 16:07:33 +01:00
|
|
|
try:
|
|
|
|
|
records, validated = dnssec.query(url, dns.rdatatype.TXT)
|
|
|
|
|
except DNSException as e:
|
2023-07-10 17:50:53 +00:00
|
|
|
_logger.info(f'Error resolving openalias: {repr(e)}')
|
2018-03-06 16:07:33 +01:00
|
|
|
return None
|
2015-07-02 12:44:53 +02:00
|
|
|
prefix = 'btc'
|
2015-07-08 19:20:54 +02:00
|
|
|
for record in records:
|
2024-10-08 11:57:23 +02:00
|
|
|
if record.rdtype != dns.rdatatype.TXT:
|
|
|
|
|
continue
|
2018-03-06 16:08:49 +01:00
|
|
|
string = to_string(record.strings[0], 'utf8')
|
2015-07-08 19:20:54 +02:00
|
|
|
if string.startswith('oa1:' + prefix):
|
2023-07-10 17:50:53 +00:00
|
|
|
address = cls.find_regex(string, r'recipient_address=([A-Za-z0-9]+)')
|
|
|
|
|
name = cls.find_regex(string, r'recipient_name=([^;]+)')
|
2015-07-08 19:20:54 +02:00
|
|
|
if not name:
|
|
|
|
|
name = address
|
|
|
|
|
if not address:
|
|
|
|
|
continue
|
|
|
|
|
return address, name, validated
|
2025-05-02 16:12:55 +02:00
|
|
|
return None
|
|
|
|
|
return None
|
2015-07-02 12:44:53 +02:00
|
|
|
|
2023-07-10 17:50:53 +00:00
|
|
|
@staticmethod
|
|
|
|
|
def find_regex(haystack, needle):
|
2015-07-02 12:44:53 +02:00
|
|
|
regex = re.compile(needle)
|
|
|
|
|
try:
|
|
|
|
|
return regex.search(haystack).groups()[0]
|
|
|
|
|
except AttributeError:
|
|
|
|
|
return None
|
2022-10-31 16:13:22 +00:00
|
|
|
|
2018-02-21 18:55:37 +01:00
|
|
|
def _validate(self, data):
|
|
|
|
|
for k, v in list(data.items()):
|
2017-08-01 19:56:46 +07:00
|
|
|
if k == 'contacts':
|
|
|
|
|
return self._validate(v)
|
2025-05-02 16:12:55 +02:00
|
|
|
if not (bitcoin.is_address(k) or is_valid_email(k)):
|
2017-08-01 19:56:46 +07:00
|
|
|
data.pop(k)
|
|
|
|
|
else:
|
2018-02-21 18:55:37 +01:00
|
|
|
_type, _ = v
|
2025-05-02 16:12:55 +02:00
|
|
|
if _type not in ('address', 'lnaddress'):
|
2017-08-01 19:56:46 +07:00
|
|
|
data.pop(k)
|
|
|
|
|
return data
|
2015-07-02 12:44:53 +02:00
|
|
|
|