2018-05-28 15:35:30 +02:00
|
|
|
"""
|
2019-10-10 15:38:13 +02:00
|
|
|
Namecoin queries
|
|
|
|
"""
|
2020-01-15 11:47:26 +01:00
|
|
|
# pylint: disable=too-many-branches,protected-access
|
2013-07-05 19:08:39 +02:00
|
|
|
|
|
|
|
import base64
|
2016-08-17 17:26:00 +02:00
|
|
|
import httplib
|
2013-07-05 18:14:47 +02:00
|
|
|
import json
|
2018-05-28 15:35:30 +02:00
|
|
|
import os
|
2013-07-05 19:08:39 +02:00
|
|
|
import socket
|
2013-07-05 20:08:19 +02:00
|
|
|
import sys
|
|
|
|
|
2017-02-08 13:41:56 +01:00
|
|
|
import defaults
|
2020-01-24 15:03:13 +01:00
|
|
|
from addresses import decodeAddress
|
2018-05-28 15:35:30 +02:00
|
|
|
from bmconfigparser import BMConfigParser
|
2020-01-24 15:03:13 +01:00
|
|
|
from debug import logger
|
2021-09-02 17:40:57 +02:00
|
|
|
from tr import _translate # translate
|
2016-08-17 17:26:00 +02:00
|
|
|
|
2013-07-05 20:08:19 +02:00
|
|
|
configSection = "bitmessagesettings"
|
2013-07-05 17:29:49 +02:00
|
|
|
|
2018-05-28 15:35:30 +02:00
|
|
|
|
|
|
|
class RPCError(Exception):
|
|
|
|
"""Error thrown when the RPC call returns an error."""
|
|
|
|
|
2013-07-05 19:08:39 +02:00
|
|
|
error = None
|
2013-07-05 17:29:49 +02:00
|
|
|
|
2018-05-28 15:35:30 +02:00
|
|
|
def __init__(self, data):
|
|
|
|
super(RPCError, self).__init__()
|
2013-07-05 19:08:39 +02:00
|
|
|
self.error = data
|
2018-05-28 15:35:30 +02:00
|
|
|
|
2016-08-17 17:26:00 +02:00
|
|
|
def __str__(self):
|
2021-09-07 18:47:49 +02:00
|
|
|
return "{0}: {1}".format(type(self).__name__, self.error)
|
2013-07-05 17:29:49 +02:00
|
|
|
|
2018-05-28 15:35:30 +02:00
|
|
|
|
|
|
|
class namecoinConnection(object):
|
|
|
|
"""This class handles the Namecoin identity integration."""
|
|
|
|
|
2013-07-05 19:08:39 +02:00
|
|
|
user = None
|
|
|
|
password = None
|
|
|
|
host = None
|
|
|
|
port = None
|
2013-07-07 20:04:57 +02:00
|
|
|
nmctype = None
|
2013-07-05 19:08:39 +02:00
|
|
|
bufsize = 4096
|
|
|
|
queryid = 1
|
2016-08-17 17:26:00 +02:00
|
|
|
con = None
|
2013-07-05 19:08:39 +02:00
|
|
|
|
2018-05-28 15:35:30 +02:00
|
|
|
def __init__(self, options=None):
|
|
|
|
"""
|
|
|
|
Initialise. If options are given, take the connection settings from
|
|
|
|
them instead of loading from the configs. This can be used to test
|
|
|
|
currently entered connection settings in the config dialog without
|
|
|
|
actually changing the values (yet).
|
|
|
|
"""
|
2013-07-07 18:41:13 +02:00
|
|
|
if options is None:
|
2021-09-07 18:47:49 +02:00
|
|
|
self.nmctype = BMConfigParser().get(
|
|
|
|
configSection, "namecoinrpctype")
|
|
|
|
self.host = BMConfigParser().get(
|
|
|
|
configSection, "namecoinrpchost")
|
|
|
|
self.port = int(BMConfigParser().get(
|
|
|
|
configSection, "namecoinrpcport"))
|
|
|
|
self.user = BMConfigParser().get(
|
|
|
|
configSection, "namecoinrpcuser")
|
|
|
|
self.password = BMConfigParser().get(
|
|
|
|
configSection, "namecoinrpcpassword")
|
2013-07-07 18:41:13 +02:00
|
|
|
else:
|
2018-05-28 15:35:30 +02:00
|
|
|
self.nmctype = options["type"]
|
|
|
|
self.host = options["host"]
|
|
|
|
self.port = int(options["port"])
|
|
|
|
self.user = options["user"]
|
|
|
|
self.password = options["password"]
|
2013-07-05 17:29:49 +02:00
|
|
|
|
2013-07-07 20:04:57 +02:00
|
|
|
assert self.nmctype == "namecoind" or self.nmctype == "nmcontrol"
|
2016-08-17 17:26:00 +02:00
|
|
|
if self.nmctype == "namecoind":
|
2018-05-28 15:35:30 +02:00
|
|
|
self.con = httplib.HTTPConnection(self.host, self.port, timeout=3)
|
|
|
|
|
2021-08-27 09:45:09 +02:00
|
|
|
def query(self, identity):
|
2018-05-28 15:35:30 +02:00
|
|
|
"""
|
|
|
|
Query for the bitmessage address corresponding to the given identity
|
|
|
|
string. If it doesn't contain a slash, id/ is prepended. We return
|
|
|
|
the result as (Error, Address) pair, where the Error is an error
|
|
|
|
message to display or None in case of success.
|
|
|
|
"""
|
2021-08-27 09:45:09 +02:00
|
|
|
slashPos = identity.find("/")
|
2013-07-05 18:14:47 +02:00
|
|
|
if slashPos < 0:
|
2021-08-27 09:45:09 +02:00
|
|
|
display_name = identity
|
|
|
|
identity = "id/" + identity
|
2018-11-07 12:56:06 +01:00
|
|
|
else:
|
2021-08-27 09:45:09 +02:00
|
|
|
display_name = identity.split("/")[1]
|
2013-07-05 18:14:47 +02:00
|
|
|
|
|
|
|
try:
|
2013-07-07 20:04:57 +02:00
|
|
|
if self.nmctype == "namecoind":
|
2021-08-27 09:45:09 +02:00
|
|
|
res = self.callRPC("name_show", [identity])
|
2013-07-07 20:04:57 +02:00
|
|
|
res = res["value"]
|
|
|
|
elif self.nmctype == "nmcontrol":
|
2021-08-27 09:45:09 +02:00
|
|
|
res = self.callRPC("data", ["getValue", identity])
|
2013-07-07 20:04:57 +02:00
|
|
|
res = res["reply"]
|
2018-05-28 15:35:30 +02:00
|
|
|
if not res:
|
2021-08-27 09:45:09 +02:00
|
|
|
return (_translate(
|
2021-09-07 18:47:49 +02:00
|
|
|
"MainWindow", "The name %1 was not found."
|
|
|
|
).arg(identity.decode("utf-8", "ignore")), None)
|
2013-07-07 20:04:57 +02:00
|
|
|
else:
|
|
|
|
assert False
|
2013-07-05 19:08:39 +02:00
|
|
|
except RPCError as exc:
|
2016-08-17 17:26:00 +02:00
|
|
|
logger.exception("Namecoin query RPC exception")
|
|
|
|
if isinstance(exc.error, dict):
|
|
|
|
errmsg = exc.error["message"]
|
2013-07-05 18:14:47 +02:00
|
|
|
else:
|
2016-08-17 17:26:00 +02:00
|
|
|
errmsg = exc.error
|
2021-08-27 09:45:09 +02:00
|
|
|
return (_translate(
|
2021-09-07 18:47:49 +02:00
|
|
|
"MainWindow", "The namecoin query failed (%1)"
|
|
|
|
).arg(errmsg.decode("utf-8", "ignore")), None)
|
2018-11-07 12:56:06 +01:00
|
|
|
except AssertionError:
|
2021-08-27 09:45:09 +02:00
|
|
|
return (_translate(
|
2021-09-07 18:47:49 +02:00
|
|
|
"MainWindow", "Unknown namecoin interface type: %1"
|
|
|
|
).arg(self.nmctype.decode("utf-8", "ignore")), None)
|
2018-05-28 15:35:30 +02:00
|
|
|
except Exception:
|
2016-08-17 17:26:00 +02:00
|
|
|
logger.exception("Namecoin query exception")
|
2021-08-27 09:45:09 +02:00
|
|
|
return (_translate(
|
2021-09-07 18:47:49 +02:00
|
|
|
"MainWindow", "The namecoin query failed."), None)
|
2013-07-05 18:14:47 +02:00
|
|
|
|
|
|
|
try:
|
2018-11-07 12:56:06 +01:00
|
|
|
res = json.loads(res)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
display_name = res["name"]
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
res = res.get("bitmessage")
|
|
|
|
|
2021-09-07 18:47:49 +02:00
|
|
|
valid = decodeAddress(res)[0] == "success"
|
2018-05-28 15:35:30 +02:00
|
|
|
return (
|
2018-11-07 12:56:06 +01:00
|
|
|
None, "%s <%s>" % (display_name, res)
|
|
|
|
) if valid else (
|
2021-08-27 09:45:09 +02:00
|
|
|
_translate(
|
2018-05-28 15:35:30 +02:00
|
|
|
"MainWindow",
|
2021-09-07 18:47:49 +02:00
|
|
|
"The name %1 has no associated Bitmessage address."
|
|
|
|
).arg(identity.decode("utf-8", "ignore")), None)
|
2013-07-05 19:08:39 +02:00
|
|
|
|
2018-02-12 23:50:47 +01:00
|
|
|
def test(self):
|
2018-05-28 15:35:30 +02:00
|
|
|
"""
|
|
|
|
Test the connection settings. This routine tries to query a "getinfo"
|
|
|
|
command, and builds either an error message or a success message with
|
|
|
|
some info from it.
|
|
|
|
"""
|
2013-07-07 18:41:13 +02:00
|
|
|
try:
|
2013-07-07 20:04:57 +02:00
|
|
|
if self.nmctype == "namecoind":
|
2018-02-12 23:50:47 +01:00
|
|
|
try:
|
|
|
|
vers = self.callRPC("getinfo", [])["version"]
|
|
|
|
except RPCError:
|
|
|
|
vers = self.callRPC("getnetworkinfo", [])["version"]
|
|
|
|
|
2013-07-07 20:04:57 +02:00
|
|
|
v3 = vers % 100
|
|
|
|
vers = vers / 100
|
|
|
|
v2 = vers % 100
|
|
|
|
vers = vers / 100
|
|
|
|
v1 = vers
|
|
|
|
if v3 == 0:
|
2018-05-28 15:35:30 +02:00
|
|
|
versStr = "0.%d.%d" % (v1, v2)
|
2013-07-07 20:04:57 +02:00
|
|
|
else:
|
2018-05-28 15:35:30 +02:00
|
|
|
versStr = "0.%d.%d.%d" % (v1, v2, v3)
|
|
|
|
message = (
|
2021-09-07 18:47:49 +02:00
|
|
|
"success",
|
2021-08-27 09:45:09 +02:00
|
|
|
_translate(
|
2018-05-28 15:35:30 +02:00
|
|
|
"MainWindow",
|
2021-09-07 18:47:49 +02:00
|
|
|
"Success! Namecoind version %1 running.").arg(
|
|
|
|
versStr.decode("utf-8", "ignore")))
|
2013-07-07 20:04:57 +02:00
|
|
|
|
|
|
|
elif self.nmctype == "nmcontrol":
|
2018-05-28 15:35:30 +02:00
|
|
|
res = self.callRPC("data", ["status"])
|
2013-07-07 20:04:57 +02:00
|
|
|
prefix = "Plugin data running"
|
|
|
|
if ("reply" in res) and res["reply"][:len(prefix)] == prefix:
|
2021-09-07 18:47:49 +02:00
|
|
|
return (
|
|
|
|
"success",
|
|
|
|
_translate(
|
|
|
|
"MainWindow",
|
|
|
|
"Success! NMControll is up and running."
|
|
|
|
)
|
|
|
|
)
|
2013-07-07 20:04:57 +02:00
|
|
|
|
2016-08-17 17:26:00 +02:00
|
|
|
logger.error("Unexpected nmcontrol reply: %s", res)
|
2021-09-07 18:47:49 +02:00
|
|
|
message = (
|
|
|
|
"failed",
|
|
|
|
_translate(
|
|
|
|
"MainWindow",
|
|
|
|
"Couldn\'t understand NMControl."
|
|
|
|
)
|
|
|
|
)
|
2013-07-07 18:41:13 +02:00
|
|
|
|
2013-07-07 20:04:57 +02:00
|
|
|
else:
|
2021-09-07 18:47:49 +02:00
|
|
|
sys.exit("Unsupported Namecoin type")
|
2018-05-28 15:35:30 +02:00
|
|
|
|
|
|
|
return message
|
2013-07-07 18:41:13 +02:00
|
|
|
|
2016-08-17 17:26:00 +02:00
|
|
|
except Exception:
|
2016-08-29 06:13:08 +02:00
|
|
|
logger.info("Namecoin connection test failure")
|
2018-02-13 10:53:43 +01:00
|
|
|
return (
|
2021-09-07 18:47:49 +02:00
|
|
|
"failed",
|
2021-08-27 09:45:09 +02:00
|
|
|
_translate(
|
2018-02-13 10:53:43 +01:00
|
|
|
"MainWindow", "The connection to namecoin failed.")
|
|
|
|
)
|
2013-07-07 18:41:13 +02:00
|
|
|
|
2018-05-28 15:35:30 +02:00
|
|
|
def callRPC(self, method, params):
|
|
|
|
"""Helper routine that actually performs an JSON RPC call."""
|
|
|
|
|
2013-07-05 19:08:39 +02:00
|
|
|
data = {"method": method, "params": params, "id": self.queryid}
|
2013-07-07 20:04:57 +02:00
|
|
|
if self.nmctype == "namecoind":
|
2018-05-28 15:35:30 +02:00
|
|
|
resp = self.queryHTTP(json.dumps(data))
|
2013-07-07 20:04:57 +02:00
|
|
|
elif self.nmctype == "nmcontrol":
|
2018-05-28 15:35:30 +02:00
|
|
|
resp = self.queryServer(json.dumps(data))
|
2013-07-07 20:04:57 +02:00
|
|
|
else:
|
2018-05-28 15:35:30 +02:00
|
|
|
assert False
|
|
|
|
val = json.loads(resp)
|
2013-07-05 19:08:39 +02:00
|
|
|
|
|
|
|
if val["id"] != self.queryid:
|
2018-05-28 15:35:30 +02:00
|
|
|
raise Exception("ID mismatch in JSON RPC answer.")
|
|
|
|
|
2016-08-17 17:26:00 +02:00
|
|
|
if self.nmctype == "namecoind":
|
|
|
|
self.queryid = self.queryid + 1
|
2013-07-05 19:08:39 +02:00
|
|
|
|
2016-08-17 17:26:00 +02:00
|
|
|
error = val["error"]
|
|
|
|
if error is None:
|
|
|
|
return val["result"]
|
2013-07-05 19:08:39 +02:00
|
|
|
|
2016-08-17 17:26:00 +02:00
|
|
|
if isinstance(error, bool):
|
2018-05-28 15:35:30 +02:00
|
|
|
raise RPCError(val["result"])
|
|
|
|
raise RPCError(error)
|
|
|
|
|
|
|
|
def queryHTTP(self, data):
|
|
|
|
"""Query the server via HTTP."""
|
2013-07-05 19:08:39 +02:00
|
|
|
|
|
|
|
result = None
|
2016-08-17 17:26:00 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
self.con.putrequest("POST", "/")
|
|
|
|
self.con.putheader("Connection", "Keep-Alive")
|
|
|
|
self.con.putheader("User-Agent", "bitmessage")
|
|
|
|
self.con.putheader("Host", self.host)
|
|
|
|
self.con.putheader("Content-Type", "application/json")
|
|
|
|
self.con.putheader("Content-Length", str(len(data)))
|
|
|
|
self.con.putheader("Accept", "application/json")
|
|
|
|
authstr = "%s:%s" % (self.user, self.password)
|
2021-09-07 18:47:49 +02:00
|
|
|
self.con.putheader(
|
|
|
|
"Authorization", "Basic %s" % base64.b64encode(authstr))
|
2016-08-17 17:26:00 +02:00
|
|
|
self.con.endheaders()
|
|
|
|
self.con.send(data)
|
|
|
|
try:
|
|
|
|
resp = self.con.getresponse()
|
|
|
|
result = resp.read()
|
|
|
|
if resp.status != 200:
|
2021-09-07 18:47:49 +02:00
|
|
|
raise Exception(
|
|
|
|
"Namecoin returned status"
|
|
|
|
" %i: %s" % (resp.status, resp.reason))
|
2021-08-25 16:38:46 +02:00
|
|
|
except: # noqa:E722
|
2016-08-29 06:13:08 +02:00
|
|
|
logger.info("HTTP receive error")
|
2021-08-25 16:38:46 +02:00
|
|
|
except: # noqa:E722
|
2016-08-29 06:13:08 +02:00
|
|
|
logger.info("HTTP connection error")
|
2013-07-05 19:08:39 +02:00
|
|
|
|
|
|
|
return result
|
|
|
|
|
2018-05-28 15:35:30 +02:00
|
|
|
def queryServer(self, data):
|
2021-09-07 18:47:49 +02:00
|
|
|
"""Helper routine sending data to the RPC "
|
|
|
|
"server and returning the result."""
|
2018-05-28 15:35:30 +02:00
|
|
|
|
2013-07-05 19:08:39 +02:00
|
|
|
try:
|
2018-05-28 15:35:30 +02:00
|
|
|
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
|
|
|
s.settimeout(3)
|
|
|
|
s.connect((self.host, self.port))
|
|
|
|
s.sendall(data)
|
2013-07-05 19:08:39 +02:00
|
|
|
result = ""
|
|
|
|
|
|
|
|
while True:
|
2018-05-28 15:35:30 +02:00
|
|
|
tmp = s.recv(self.bufsize)
|
2013-07-05 19:08:39 +02:00
|
|
|
if not tmp:
|
2018-05-28 15:35:30 +02:00
|
|
|
break
|
2013-07-05 19:08:39 +02:00
|
|
|
result += tmp
|
|
|
|
|
2018-05-28 15:35:30 +02:00
|
|
|
s.close()
|
2013-07-05 19:08:39 +02:00
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
except socket.error as exc:
|
2019-10-12 16:12:19 +02:00
|
|
|
raise Exception("Socket error in RPC connection: %s" % exc)
|
2018-05-28 15:35:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
def lookupNamecoinFolder():
|
|
|
|
"""
|
|
|
|
Look up the namecoin data folder.
|
|
|
|
|
|
|
|
.. todo:: Check whether this works on other platforms as well!
|
|
|
|
"""
|
2013-07-05 20:08:19 +02:00
|
|
|
|
|
|
|
app = "namecoin"
|
|
|
|
from os import path, environ
|
|
|
|
if sys.platform == "darwin":
|
|
|
|
if "HOME" in environ:
|
2018-05-28 15:35:30 +02:00
|
|
|
dataFolder = path.join(os.environ["HOME"],
|
2021-09-07 18:47:49 +02:00
|
|
|
"Library/Application Support/", app) + "/"
|
2013-07-05 20:08:19 +02:00
|
|
|
else:
|
2021-09-07 18:47:49 +02:00
|
|
|
sys.exit(
|
2018-05-28 15:35:30 +02:00
|
|
|
"Could not find home folder, please report this message"
|
|
|
|
" and your OS X version to the BitMessage Github."
|
|
|
|
)
|
2013-07-05 20:08:19 +02:00
|
|
|
|
|
|
|
elif "win32" in sys.platform or "win64" in sys.platform:
|
|
|
|
dataFolder = path.join(environ["APPDATA"], app) + "\\"
|
|
|
|
else:
|
|
|
|
dataFolder = path.join(environ["HOME"], ".%s" % app) + "/"
|
|
|
|
|
|
|
|
return dataFolder
|
|
|
|
|
|
|
|
|
2018-05-28 15:35:30 +02:00
|
|
|
def ensureNamecoinOptions():
|
|
|
|
"""
|
|
|
|
Ensure all namecoin options are set, by setting those to default values
|
|
|
|
that aren't there.
|
|
|
|
"""
|
|
|
|
|
|
|
|
if not BMConfigParser().has_option(configSection, "namecoinrpctype"):
|
|
|
|
BMConfigParser().set(configSection, "namecoinrpctype", "namecoind")
|
|
|
|
if not BMConfigParser().has_option(configSection, "namecoinrpchost"):
|
|
|
|
BMConfigParser().set(configSection, "namecoinrpchost", "localhost")
|
|
|
|
|
|
|
|
hasUser = BMConfigParser().has_option(configSection, "namecoinrpcuser")
|
|
|
|
hasPass = BMConfigParser().has_option(configSection, "namecoinrpcpassword")
|
|
|
|
hasPort = BMConfigParser().has_option(configSection, "namecoinrpcport")
|
2013-07-05 20:08:19 +02:00
|
|
|
|
|
|
|
# Try to read user/password from .namecoin configuration file.
|
2013-07-18 07:09:49 +02:00
|
|
|
defaultUser = ""
|
|
|
|
defaultPass = ""
|
2018-05-28 15:35:30 +02:00
|
|
|
nmcFolder = lookupNamecoinFolder()
|
2016-08-31 10:24:28 +02:00
|
|
|
nmcConfig = nmcFolder + "namecoin.conf"
|
2013-07-17 18:33:26 +02:00
|
|
|
try:
|
2018-05-28 15:35:30 +02:00
|
|
|
nmc = open(nmcConfig, "r")
|
2013-07-17 18:33:26 +02:00
|
|
|
|
|
|
|
while True:
|
2018-05-28 15:35:30 +02:00
|
|
|
line = nmc.readline()
|
2013-07-17 18:33:26 +02:00
|
|
|
if line == "":
|
|
|
|
break
|
2018-05-28 15:35:30 +02:00
|
|
|
parts = line.split("=")
|
|
|
|
if len(parts) == 2:
|
2013-07-17 18:33:26 +02:00
|
|
|
key = parts[0]
|
2018-05-28 15:35:30 +02:00
|
|
|
val = parts[1].rstrip()
|
2013-07-17 18:33:26 +02:00
|
|
|
|
|
|
|
if key == "rpcuser" and not hasUser:
|
2013-07-18 07:09:49 +02:00
|
|
|
defaultUser = val
|
2013-07-17 18:33:26 +02:00
|
|
|
if key == "rpcpassword" and not hasPass:
|
2013-07-18 07:09:49 +02:00
|
|
|
defaultPass = val
|
2013-07-17 18:33:26 +02:00
|
|
|
if key == "rpcport":
|
2017-02-08 13:41:56 +01:00
|
|
|
defaults.namecoinDefaultRpcPort = val
|
2018-05-28 15:35:30 +02:00
|
|
|
|
|
|
|
nmc.close()
|
2016-08-31 10:24:28 +02:00
|
|
|
except IOError:
|
2021-09-07 18:47:49 +02:00
|
|
|
logger.warning(
|
|
|
|
"%s unreadable or missing, Namecoin support deactivated",
|
|
|
|
nmcConfig)
|
2018-05-28 15:35:30 +02:00
|
|
|
except Exception:
|
2016-08-17 17:26:00 +02:00
|
|
|
logger.warning("Error processing namecoin.conf", exc_info=True)
|
2013-07-18 07:09:49 +02:00
|
|
|
|
|
|
|
# If still nothing found, set empty at least.
|
2018-05-28 15:35:30 +02:00
|
|
|
if not hasUser:
|
|
|
|
BMConfigParser().set(configSection, "namecoinrpcuser", defaultUser)
|
|
|
|
if not hasPass:
|
|
|
|
BMConfigParser().set(configSection, "namecoinrpcpassword", defaultPass)
|
2013-07-17 18:40:02 +02:00
|
|
|
|
|
|
|
# Set default port now, possibly to found value.
|
2018-05-28 15:35:30 +02:00
|
|
|
if not hasPort:
|
|
|
|
BMConfigParser().set(configSection, "namecoinrpcport",
|
|
|
|
defaults.namecoinDefaultRpcPort)
|