-
Notifications
You must be signed in to change notification settings - Fork 577
Expand file tree
/
Copy pathnamecoin.py
More file actions
374 lines (316 loc) · 12.1 KB
/
namecoin.py
File metadata and controls
374 lines (316 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
"""
Namecoin queries
"""
# pylint: disable=too-many-branches,protected-access
import base64
import json
import os
import socket
import sys
from six.moves import http_client as httplib
import defaults
from addresses import decodeAddress
from bmconfigparser import config
from debug import logger
from tr import _translate # translate
configSection = "bitmessagesettings"
class RPCError(Exception):
"""Error thrown when the RPC call returns an error."""
error = None
def __init__(self, data):
super(RPCError, self).__init__()
self.error = data
def __str__(self):
return "{0}: {1}".format(type(self).__name__, self.error)
class namecoinConnection(object):
"""This class handles the Namecoin identity integration."""
user = None
password = None
host = None
port = None
nmctype = None
bufsize = 4096
queryid = 1
con = None
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).
"""
if options is None:
self.nmctype = config.get(
configSection, "namecoinrpctype")
self.host = config.get(
configSection, "namecoinrpchost")
self.port = int(config.get(
configSection, "namecoinrpcport"))
self.user = config.get(
configSection, "namecoinrpcuser")
self.password = config.get(
configSection, "namecoinrpcpassword")
else:
self.nmctype = options["type"]
self.host = options["host"]
self.port = int(options["port"])
self.user = options["user"]
self.password = options["password"]
assert self.nmctype == "namecoind" or self.nmctype == "nmcontrol"
if self.nmctype == "namecoind":
self.con = httplib.HTTPConnection(self.host, self.port, timeout=3)
def query(self, identity):
"""
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.
"""
slashPos = identity.find("/")
if slashPos < 0:
display_name = identity
identity = "id/" + identity
else:
display_name = identity.split("/")[1]
try:
if self.nmctype == "namecoind":
res = self.callRPC("name_show", [identity])
res = res["value"]
elif self.nmctype == "nmcontrol":
res = self.callRPC("data", ["getValue", identity])
res = res["reply"]
if not res:
return (_translate(
"MainWindow", "The name %1 was not found."
).arg(identity.decode("utf-8", "ignore")), None)
else:
assert False
except RPCError as exc:
logger.exception("Namecoin query RPC exception")
if isinstance(exc.error, dict):
errmsg = exc.error["message"]
else:
errmsg = exc.error
return (_translate(
"MainWindow", "The namecoin query failed (%1)"
).arg(errmsg.decode("utf-8", "ignore")), None)
except AssertionError:
return (_translate(
"MainWindow", "Unknown namecoin interface type: %1"
).arg(self.nmctype.decode("utf-8", "ignore")), None)
except Exception:
logger.exception("Namecoin query exception")
return (_translate(
"MainWindow", "The namecoin query failed."), None)
try:
res = json.loads(res)
except ValueError:
pass
else:
try:
display_name = res["name"]
except KeyError:
pass
res = res.get("bitmessage")
valid = decodeAddress(res)[0] == "success"
return (
None, "%s <%s>" % (display_name, res)
) if valid else (
_translate(
"MainWindow",
"The name %1 has no associated Bitmessage address."
).arg(identity.decode("utf-8", "ignore")), None)
def test(self):
"""
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.
"""
try:
if self.nmctype == "namecoind":
try:
vers = self.callRPC("getinfo", [])["version"]
except RPCError:
vers = self.callRPC("getnetworkinfo", [])["version"]
v3 = vers % 100
vers = vers / 100
v2 = vers % 100
vers = vers / 100
v1 = vers
if v3 == 0:
versStr = "0.%d.%d" % (v1, v2)
else:
versStr = "0.%d.%d.%d" % (v1, v2, v3)
message = (
"success",
_translate(
"MainWindow",
"Success! Namecoind version %1 running.").arg(
versStr.decode("utf-8", "ignore")))
elif self.nmctype == "nmcontrol":
res = self.callRPC("data", ["status"])
prefix = "Plugin data running"
if ("reply" in res) and res["reply"][:len(prefix)] == prefix:
return (
"success",
_translate(
"MainWindow",
"Success! NMControll is up and running."
)
)
logger.error("Unexpected nmcontrol reply: %s", res)
message = (
"failed",
_translate(
"MainWindow",
"Couldn\'t understand NMControl."
)
)
else:
sys.exit("Unsupported Namecoin type")
return message
except Exception:
logger.info("Namecoin connection test failure")
return (
"failed",
_translate(
"MainWindow", "The connection to namecoin failed.")
)
def callRPC(self, method, params):
"""Helper routine that actually performs an JSON RPC call."""
data = {"method": method, "params": params, "id": self.queryid}
if self.nmctype == "namecoind":
resp = self.queryHTTP(json.dumps(data))
elif self.nmctype == "nmcontrol":
resp = self.queryServer(json.dumps(data))
else:
assert False
val = json.loads(resp)
if val["id"] != self.queryid:
raise Exception("ID mismatch in JSON RPC answer.")
if self.nmctype == "namecoind":
self.queryid = self.queryid + 1
error = val["error"]
if error is None:
return val["result"]
if isinstance(error, bool):
raise RPCError(val["result"])
raise RPCError(error)
def queryHTTP(self, data):
"""Query the server via HTTP."""
result = None
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)
self.con.putheader(
"Authorization", "Basic %s" % base64.b64encode(authstr))
self.con.endheaders()
self.con.send(data)
except: # noqa:E722
logger.info("HTTP connection error")
return None
try:
resp = self.con.getresponse()
result = resp.read()
if resp.status != 200:
raise Exception(
"Namecoin returned status"
" %i: %s" % (resp.status, resp.reason))
except: # noqa:E722
logger.info("HTTP receive error")
return None
return result
def queryServer(self, data):
"""Helper routine sending data to the RPC "
"server and returning the result."""
try:
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)
result = ""
while True:
tmp = s.recv(self.bufsize)
if not tmp:
break
result += tmp
s.close()
return result
except socket.error as exc:
raise Exception("Socket error in RPC connection: %s" % exc)
def lookupNamecoinFolder():
"""
Look up the namecoin data folder.
.. todo:: Check whether this works on other platforms as well!
"""
app = "namecoin"
from os import environ, path
if sys.platform == "darwin":
if "HOME" in environ:
dataFolder = path.join(os.environ["HOME"],
"Library/Application Support/", app) + "/"
else:
sys.exit(
"Could not find home folder, please report this message"
" and your OS X version to the BitMessage Github."
)
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
def ensureNamecoinOptions():
"""
Ensure all namecoin options are set, by setting those to default values
that aren't there.
"""
if not config.has_option(configSection, "namecoinrpctype"):
config.set(configSection, "namecoinrpctype", "namecoind")
if not config.has_option(configSection, "namecoinrpchost"):
config.set(configSection, "namecoinrpchost", "localhost")
hasUser = config.has_option(configSection, "namecoinrpcuser")
hasPass = config.has_option(configSection, "namecoinrpcpassword")
hasPort = config.has_option(configSection, "namecoinrpcport")
# Try to read user/password from .namecoin configuration file.
defaultUser = ""
defaultPass = ""
nmcFolder = lookupNamecoinFolder()
nmcConfig = nmcFolder + "namecoin.conf"
try:
nmc = open(nmcConfig, "r")
while True:
line = nmc.readline()
if line == "":
break
parts = line.split("=")
if len(parts) == 2:
key = parts[0]
val = parts[1].rstrip()
if key == "rpcuser" and not hasUser:
defaultUser = val
if key == "rpcpassword" and not hasPass:
defaultPass = val
if key == "rpcport":
defaults.namecoinDefaultRpcPort = val
nmc.close()
except IOError:
logger.warning(
"%s unreadable or missing, Namecoin support deactivated",
nmcConfig)
except Exception:
logger.warning("Error processing namecoin.conf", exc_info=True)
# If still nothing found, set empty at least.
if not hasUser:
config.set(configSection, "namecoinrpcuser", defaultUser)
if not hasPass:
config.set(configSection, "namecoinrpcpassword", defaultPass)
# Set default port now, possibly to found value.
if not hasPort:
config.set(configSection, "namecoinrpcport", defaults.namecoinDefaultRpcPort)