rpc.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2022 The MMGen Project <mmgen@tuta.io>
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. """
  19. rpc.py: Cryptocoin RPC library for the MMGen suite
  20. """
  21. import base64,json,asyncio
  22. from decimal import Decimal
  23. from .common import *
  24. from .objmethods import Hilite,InitErrors
  25. from .base_obj import AsyncInit
  26. rpc_credentials_msg = '\n'+fmt("""
  27. Error: no {proto_name} RPC authentication method found
  28. RPC credentials must be supplied using one of the following methods:
  29. A) If daemon is local and running as same user as you:
  30. - no credentials required, or matching rpcuser/rpcpassword and
  31. rpc_user/rpc_password values in {cf_name}.conf and mmgen.cfg
  32. B) If daemon is running remotely or as different user:
  33. - matching credentials in {cf_name}.conf and mmgen.cfg as described above
  34. The --rpc-user/--rpc-password options may be supplied on the MMGen command line.
  35. They override the corresponding values in mmgen.cfg. Set them to an empty string
  36. to use cookie authentication with a local server when the options are set
  37. in mmgen.cfg.
  38. For better security, rpcauth should be used in {cf_name}.conf instead of
  39. rpcuser/rpcpassword.
  40. """,strip_char='\t')
  41. def dmsg_rpc(fs,data=None,is_json=False):
  42. if g.debug_rpc:
  43. msg(
  44. fs if data == None else
  45. fs.format(pp_fmt(json.loads(data) if is_json else data))
  46. )
  47. class IPPort(str,Hilite,InitErrors):
  48. color = 'yellow'
  49. width = 0
  50. trunc_ok = False
  51. min_len = 9 # 0.0.0.0:0
  52. max_len = 21 # 255.255.255.255:65535
  53. def __new__(cls,s):
  54. if type(s) == cls:
  55. return s
  56. try:
  57. m = re.fullmatch(r'{q}\.{q}\.{q}\.{q}:(\d{{1,10}})'.format(q=r'([0-9]{1,3})'),s)
  58. assert m is not None, f'{s!r}: invalid IP:HOST specifier'
  59. for e in m.groups():
  60. if len(e) != 1 and e[0] == '0':
  61. raise ValueError(f'{e}: leading zeroes not permitted in dotted decimal element or port number')
  62. res = [int(e) for e in m.groups()]
  63. for e in res[:4]:
  64. assert e <= 255, f'{e}: dotted decimal element > 255'
  65. assert res[4] <= 65535, f'{res[4]}: port number > 65535'
  66. me = str.__new__(cls,s)
  67. me.ip = '{}.{}.{}.{}'.format(*res)
  68. me.ip_num = sum( res[i] * ( 2 ** (-(i-3)*8) ) for i in range(4) )
  69. me.port = res[4]
  70. return me
  71. except Exception as e:
  72. return cls.init_fail(e,s)
  73. class json_encoder(json.JSONEncoder):
  74. def default(self,obj):
  75. if isinstance(obj,Decimal):
  76. return str(obj)
  77. else:
  78. return json.JSONEncoder.default(self,obj)
  79. class RPCBackends:
  80. class base:
  81. def __init__(self,caller):
  82. self.host = caller.host
  83. self.port = caller.port
  84. self.proxy = caller.proxy
  85. self.url = caller.url
  86. self.timeout = caller.timeout
  87. self.http_hdrs = caller.http_hdrs
  88. self.make_host_path = caller.make_host_path
  89. class aiohttp(base):
  90. def __init__(self,caller):
  91. super().__init__(caller)
  92. self.session = g.session
  93. if caller.auth_type == 'basic':
  94. import aiohttp
  95. self.auth = aiohttp.BasicAuth(*caller.auth,encoding='UTF-8')
  96. else:
  97. self.auth = None
  98. async def run(self,payload,timeout,wallet):
  99. dmsg_rpc('\n RPC PAYLOAD data (aiohttp) ==>\n{}\n',payload)
  100. async with self.session.post(
  101. url = self.url + self.make_host_path(wallet),
  102. auth = self.auth,
  103. data = json.dumps(payload,cls=json_encoder),
  104. timeout = timeout or self.timeout,
  105. ) as res:
  106. return (await res.text(),res.status)
  107. class requests(base):
  108. def __del__(self):
  109. self.session.close()
  110. def __init__(self,caller):
  111. super().__init__(caller)
  112. import requests,urllib3
  113. urllib3.disable_warnings()
  114. self.session = requests.Session()
  115. self.session.headers = caller.http_hdrs
  116. if caller.auth_type:
  117. auth = 'HTTP' + caller.auth_type.capitalize() + 'Auth'
  118. self.session.auth = getattr(requests.auth,auth)(*caller.auth)
  119. if self.proxy:
  120. self.session.proxies.update({
  121. 'http': f'socks5h://{self.proxy}',
  122. 'https': f'socks5h://{self.proxy}'
  123. })
  124. async def run(self,payload,timeout,wallet):
  125. dmsg_rpc('\n RPC PAYLOAD data (requests) ==>\n{}\n',payload)
  126. res = self.session.post(
  127. url = self.url + self.make_host_path(wallet),
  128. data = json.dumps(payload,cls=json_encoder),
  129. timeout = timeout or self.timeout,
  130. verify = False )
  131. return (res.content,res.status_code)
  132. class httplib(base):
  133. def __del__(self):
  134. self.session.close()
  135. def __init__(self,caller):
  136. super().__init__(caller)
  137. import http.client
  138. self.session = http.client.HTTPConnection(caller.host,caller.port,caller.timeout)
  139. if caller.auth_type == 'basic':
  140. auth_str = f'{caller.auth.user}:{caller.auth.passwd}'
  141. auth_str_b64 = 'Basic ' + base64.b64encode(auth_str.encode()).decode()
  142. self.http_hdrs.update({ 'Host': self.host, 'Authorization': auth_str_b64 })
  143. dmsg_rpc(' RPC AUTHORIZATION data ==> raw: [{}]\n{:>31}enc: [{}]\n'.format(
  144. auth_str,
  145. '',
  146. auth_str_b64 ))
  147. async def run(self,payload,timeout,wallet):
  148. dmsg_rpc('\n RPC PAYLOAD data (httplib) ==>\n{}\n',payload)
  149. if timeout:
  150. import http.client
  151. s = http.client.HTTPConnection(self.host,self.port,timeout)
  152. else:
  153. s = self.session
  154. try:
  155. s.request(
  156. method = 'POST',
  157. url = self.make_host_path(wallet),
  158. body = json.dumps(payload,cls=json_encoder),
  159. headers = self.http_hdrs )
  160. r = s.getresponse() # => http.client.HTTPResponse instance
  161. except Exception as e:
  162. raise RPCFailure(str(e))
  163. if timeout:
  164. ret = ( r.read(), r.status )
  165. s.close()
  166. return ret
  167. else:
  168. return ( r.read(), r.status )
  169. class curl(base):
  170. def __init__(self,caller):
  171. def gen_opts():
  172. for k,v in caller.http_hdrs.items():
  173. for s in ('--header',f'{k}: {v}'):
  174. yield s
  175. if caller.auth_type:
  176. """
  177. Authentication with curl is insecure, as it exposes the user's credentials
  178. via the command line. Use for testing only.
  179. """
  180. for s in ('--user',f'{caller.auth.user}:{caller.auth.passwd}'):
  181. yield s
  182. if caller.auth_type == 'digest':
  183. yield '--digest'
  184. if caller.network_proto == 'https' and caller.verify_server == False:
  185. yield '--insecure'
  186. super().__init__(caller)
  187. self.exec_opts = list(gen_opts()) + ['--silent']
  188. self.arg_max = 8192 # set way below system ARG_MAX, just to be safe
  189. async def run(self,payload,timeout,wallet):
  190. data = json.dumps(payload,cls=json_encoder)
  191. if len(data) > self.arg_max:
  192. return self.httplib(payload,timeout=timeout)
  193. dmsg_rpc('\n RPC PAYLOAD data (curl) ==>\n{}\n',payload)
  194. exec_cmd = [
  195. 'curl',
  196. '--proxy', f'socks5h://{self.proxy}' if self.proxy else '',
  197. '--connect-timeout', str(timeout or self.timeout),
  198. '--write-out', '%{http_code}',
  199. '--data-binary', data
  200. ] + self.exec_opts + [self.url + self.make_host_path(wallet)]
  201. dmsg_rpc(' RPC curl exec data ==>\n{}\n',exec_cmd)
  202. from subprocess import run,PIPE
  203. res = run(exec_cmd,stdout=PIPE,check=True).stdout.decode()
  204. # res = run(exec_cmd,stdout=PIPE,check=True,text='UTF-8').stdout # Python 3.7+
  205. return (res[:-3],int(res[-3:]))
  206. from collections import namedtuple
  207. auth_data = namedtuple('rpc_auth_data',['user','passwd'])
  208. class CallSigs:
  209. class Bitcoin:
  210. class bitcoin_core:
  211. @classmethod
  212. def createwallet(cls,wallet_name,no_keys=True,blank=True,passphrase='',load_on_startup=True):
  213. """
  214. Quirk: when --datadir is specified (even if standard), wallet is created directly in
  215. datadir, otherwise in datadir/wallets
  216. """
  217. return (
  218. 'createwallet',
  219. wallet_name, # 1. wallet_name
  220. no_keys, # 2. disable_private_keys
  221. blank, # 3. blank (no keys or seed)
  222. passphrase, # 4. passphrase (empty string for non-encrypted)
  223. False, # 5. avoid_reuse (track address reuse)
  224. False, # 6. descriptors (native descriptor wallet)
  225. load_on_startup # 7. load_on_startup
  226. )
  227. class litecoin_core(bitcoin_core):
  228. @classmethod
  229. def createwallet(cls,wallet_name,no_keys=True,blank=True,passphrase='',load_on_startup=True):
  230. return (
  231. 'createwallet',
  232. wallet_name, # 1. wallet_name
  233. no_keys, # 2. disable_private_keys
  234. blank, # 3. blank (no keys or seed)
  235. )
  236. class bitcoin_cash_node(litecoin_core): pass
  237. class Ethereum: pass
  238. class RPCClient(MMGenObject):
  239. json_rpc = True
  240. auth_type = None
  241. has_auth_cookie = False
  242. network_proto = 'http'
  243. host_path = ''
  244. proxy = None
  245. def __init__(self,host,port,test_connection=True):
  246. dmsg_rpc(f'=== {type(self).__name__}.__init__() debug ===')
  247. dmsg_rpc(f' cls [{type(self).__name__}] host [{host}] port [{port}]\n')
  248. if test_connection:
  249. import socket
  250. try:
  251. socket.create_connection((host,port),timeout=1).close()
  252. except:
  253. raise SocketError(f'Unable to connect to {host}:{port}')
  254. self.http_hdrs = { 'Content-Type': 'application/json' }
  255. self.url = f'{self.network_proto}://{host}:{port}{self.host_path}'
  256. self.host = host
  257. self.port = port
  258. self.timeout = g.http_timeout
  259. self.auth = None
  260. @staticmethod
  261. def make_host_path(foo):
  262. return ''
  263. def set_backend(self,backend=None):
  264. bn = backend or opt.rpc_backend
  265. if bn == 'auto':
  266. self.backend = {'linux':RPCBackends.httplib,'win':RPCBackends.curl}[g.platform](self)
  267. else:
  268. self.backend = getattr(RPCBackends,bn)(self)
  269. def set_auth(self):
  270. """
  271. MMGen's credentials override coin daemon's
  272. """
  273. if g.rpc_user:
  274. user,passwd = (g.rpc_user,g.rpc_password)
  275. else:
  276. user,passwd = self.get_daemon_cfg_options(('rpcuser','rpcpassword')).values()
  277. if not (user and passwd):
  278. user,passwd = (self.daemon.rpc_user,self.daemon.rpc_password)
  279. if user and passwd:
  280. self.auth = auth_data(user,passwd)
  281. return
  282. if self.has_auth_cookie:
  283. cookie = self.get_daemon_auth_cookie()
  284. if cookie:
  285. self.auth = auth_data(*cookie.split(':'))
  286. return
  287. die(1,rpc_credentials_msg.format(
  288. proto_name = self.proto.name,
  289. cf_name = (self.proto.is_fork_of or self.proto.name).lower(),
  290. ))
  291. # Call family of methods - direct-to-daemon RPC call:
  292. # positional params are passed to the daemon, 'timeout' and 'wallet' kwargs to the backend
  293. async def call(self,method,*params,timeout=None,wallet=None):
  294. """
  295. default call: call with param list unrolled, exactly as with cli
  296. """
  297. return await self.process_http_resp(self.backend.run(
  298. payload = {'id': 1, 'jsonrpc': '2.0', 'method': method, 'params': params },
  299. timeout = timeout,
  300. wallet = wallet
  301. ))
  302. async def batch_call(self,method,param_list,timeout=None,wallet=None):
  303. """
  304. Make a single call with a list of tuples as first argument
  305. For RPC calls that return a list of results
  306. """
  307. return await self.process_http_resp(self.backend.run(
  308. payload = [{
  309. 'id': n,
  310. 'jsonrpc': '2.0',
  311. 'method': method,
  312. 'params': params } for n,params in enumerate(param_list,1) ],
  313. timeout = timeout,
  314. wallet = wallet
  315. ),batch=True)
  316. async def gathered_call(self,method,args_list,timeout=None,wallet=None):
  317. """
  318. Perform multiple RPC calls, returning results in a list
  319. Can be called two ways:
  320. 1) method = methodname, args_list = [args_tuple1, args_tuple2,...]
  321. 2) method = None, args_list = [(methodname1,args_tuple1), (methodname2,args_tuple2), ...]
  322. """
  323. cmd_list = args_list if method == None else tuple(zip([method] * len(args_list), args_list))
  324. cur_pos = 0
  325. chunk_size = 1024
  326. ret = []
  327. while cur_pos < len(cmd_list):
  328. tasks = [self.process_http_resp(self.backend.run(
  329. payload = {'id': n, 'jsonrpc': '2.0', 'method': method, 'params': params },
  330. timeout = timeout,
  331. wallet = wallet
  332. )) for n,(method,params) in enumerate(cmd_list[cur_pos:chunk_size+cur_pos],1)]
  333. ret.extend(await asyncio.gather(*tasks))
  334. cur_pos += chunk_size
  335. return ret
  336. # Icall family of methods - indirect RPC call using CallSigs mechanism:
  337. # - 'timeout' and 'wallet' kwargs are passed to corresponding Call method
  338. # - remaining kwargs are passed to CallSigs method
  339. # - CallSigs method returns method and positional params for Call method
  340. def icall(self,method,**kwargs):
  341. timeout = kwargs.pop('timeout',None)
  342. wallet = kwargs.pop('wallet',None)
  343. return self.call(
  344. *getattr(self.call_sigs,method)(**kwargs),
  345. timeout = timeout,
  346. wallet = wallet )
  347. async def process_http_resp(self,coro,batch=False):
  348. text,status = await coro
  349. if status == 200:
  350. dmsg_rpc(' RPC RESPONSE data ==>\n{}\n',text,is_json=True)
  351. if batch:
  352. return [r['result'] for r in json.loads(text,parse_float=Decimal)]
  353. else:
  354. try:
  355. if self.json_rpc:
  356. return json.loads(text,parse_float=Decimal)['result']
  357. else:
  358. return json.loads(text,parse_float=Decimal)
  359. except:
  360. t = json.loads(text)
  361. try:
  362. m = t['error']['message']
  363. except:
  364. try: m = t['error']
  365. except: m = t
  366. raise RPCFailure(m)
  367. else:
  368. import http
  369. m,s = ( '', http.HTTPStatus(status) )
  370. if text:
  371. try:
  372. m = json.loads(text)['error']['message']
  373. except:
  374. try: m = text.decode()
  375. except: m = text
  376. raise RPCFailure(f'{s.value} {s.name}: {m}')
  377. async def stop_daemon(self,quiet=False,silent=False):
  378. if self.daemon.state == 'ready':
  379. if not (quiet or silent):
  380. msg(f'Stopping {self.daemon.desc} on port {self.daemon.bind_port}')
  381. ret = await self.do_stop_daemon(silent=silent)
  382. if self.daemon.wait:
  383. self.daemon.wait_for_state('stopped')
  384. return ret
  385. else:
  386. if not (quiet or silent):
  387. msg(f'{self.daemon.desc} on port {self.daemon.bind_port} not running')
  388. return True
  389. async def restart_daemon(self,quiet=False,silent=False):
  390. await self.stop_daemon(quiet=quiet,silent=silent)
  391. return self.daemon.start(silent=silent)
  392. class BitcoinRPCClient(RPCClient,metaclass=AsyncInit):
  393. auth_type = 'basic'
  394. has_auth_cookie = True
  395. async def __init__(self,proto,daemon,backend):
  396. self.proto = proto
  397. self.daemon = daemon
  398. self.call_sigs = getattr(getattr(CallSigs,proto.base_proto),daemon.id,None)
  399. super().__init__(
  400. host = 'localhost' if g.test_suite else (g.rpc_host or 'localhost'),
  401. port = daemon.rpc_port )
  402. self.set_auth() # set_auth() requires cookie, so must be called after __init__() tests daemon is listening
  403. self.set_backend(backend) # backend requires self.auth
  404. self.cached = {}
  405. (
  406. self.cached['networkinfo'],
  407. self.blockcount,
  408. self.cached['blockchaininfo'],
  409. block0
  410. ) = await self.gathered_call(None, (
  411. ('getnetworkinfo',()),
  412. ('getblockcount',()),
  413. ('getblockchaininfo',()),
  414. ('getblockhash',(0,)),
  415. ))
  416. self.daemon_version = self.cached['networkinfo']['version']
  417. self.daemon_version_str = self.cached['networkinfo']['subversion']
  418. self.chain = self.cached['blockchaininfo']['chain']
  419. tip = await self.call('getblockhash',self.blockcount)
  420. self.cur_date = (await self.call('getblockheader',tip))['time']
  421. if self.chain != 'regtest':
  422. self.chain += 'net'
  423. assert self.chain in self.proto.networks
  424. async def check_chainfork_mismatch(block0):
  425. try:
  426. if block0 != self.proto.block0:
  427. raise ValueError(f'Invalid Genesis block for {self.proto.cls_name} protocol')
  428. for fork in self.proto.forks:
  429. if fork.height == None or self.blockcount < fork.height:
  430. break
  431. if fork.hash != await self.call('getblockhash',fork.height):
  432. die(3,f'Bad block hash at fork block {fork.height}. Is this the {fork.name} chain?')
  433. except Exception as e:
  434. die(2,'{!s}\n{c!r} requested, but this is not the {c} chain!'.format(e,c=self.proto.coin))
  435. if self.chain == 'mainnet': # skip this for testnet, as Genesis block may change
  436. await check_chainfork_mismatch(block0)
  437. self.caps = ('full_node',)
  438. for func,cap in (
  439. ('setlabel','label_api'),
  440. ('signrawtransactionwithkey','sign_with_key') ):
  441. if len((await self.call('help',func)).split('\n')) > 3:
  442. self.caps += (cap,)
  443. if not self.chain == 'regtest':
  444. await self.check_tracking_wallet()
  445. async def check_tracking_wallet(self,wallet_checked=[]):
  446. if not wallet_checked:
  447. wallets = await self.call('listwallets')
  448. if len(wallets) == 0:
  449. wname = self.daemon.tracking_wallet_name
  450. await self.icall('createwallet',wallet_name=wname)
  451. ymsg(f'Created {self.daemon.coind_name} wallet {wname!r}')
  452. elif len(wallets) > 1: # support only one loaded wallet for now
  453. rdie(2,f'ERROR: more than one {self.daemon.coind_name} wallet loaded: {wallets}')
  454. wallet_checked.append(True)
  455. def get_daemon_cfg_fn(self):
  456. # Use dirname() to remove 'bob' or 'alice' component
  457. return os.path.join(
  458. (os.path.dirname(g.data_dir) if self.proto.regtest else self.daemon.datadir),
  459. self.daemon.cfg_file )
  460. def get_daemon_auth_cookie_fn(self):
  461. return os.path.join(self.daemon.network_datadir,'.cookie')
  462. def get_daemon_cfg_options(self,req_keys):
  463. fn = self.get_daemon_cfg_fn()
  464. try:
  465. lines = get_lines_from_file(fn,'',silent=not opt.verbose)
  466. except:
  467. vmsg(f'Warning: {fn!r} does not exist or is unreadable')
  468. return dict((k,None) for k in req_keys)
  469. def gen():
  470. for key in req_keys:
  471. val = None
  472. for l in lines:
  473. if l.startswith(key):
  474. res = l.split('=',1)
  475. if len(res) == 2 and not ' ' in res[1].strip():
  476. val = res[1].strip()
  477. yield (key,val)
  478. return dict(gen())
  479. def get_daemon_auth_cookie(self):
  480. fn = self.get_daemon_auth_cookie_fn()
  481. return get_lines_from_file(fn,'')[0] if os.access(fn,os.R_OK) else ''
  482. @staticmethod
  483. def make_host_path(wallet):
  484. return (
  485. '/wallet/{}'.format('bob' if g.bob else 'alice') if (g.bob or g.alice) else
  486. '/wallet/{}'.format(wallet) if wallet else '/'
  487. )
  488. def info(self,info_id):
  489. def segwit_is_active():
  490. d = self.cached['blockchaininfo']
  491. if d['chain'] == 'regtest':
  492. return True
  493. try:
  494. if d['softforks']['segwit']['active'] == True:
  495. return True
  496. except:
  497. pass
  498. try:
  499. if d['bip9_softforks']['segwit']['status'] == 'active':
  500. return True
  501. except:
  502. pass
  503. if g.test_suite:
  504. return True
  505. return False
  506. return locals()[info_id]()
  507. rpcmethods = (
  508. 'backupwallet',
  509. 'createrawtransaction',
  510. 'decoderawtransaction',
  511. 'disconnectnode',
  512. 'estimatefee',
  513. 'estimatesmartfee',
  514. 'getaddressesbyaccount',
  515. 'getaddressesbylabel',
  516. 'getblock',
  517. 'getblockchaininfo',
  518. 'getblockcount',
  519. 'getblockhash',
  520. 'getblockheader',
  521. 'getblockstats', # mmgen-node-tools
  522. 'getmempoolinfo',
  523. 'getmempoolentry',
  524. 'getnettotals',
  525. 'getnetworkinfo',
  526. 'getpeerinfo',
  527. 'getrawmempool',
  528. 'getmempoolentry',
  529. 'getrawtransaction',
  530. 'gettransaction',
  531. 'importaddress',
  532. 'listaccounts',
  533. 'listlabels',
  534. 'listunspent',
  535. 'setlabel',
  536. 'sendrawtransaction',
  537. 'signrawtransaction',
  538. 'signrawtransactionwithkey', # method new to Core v0.17.0
  539. 'validateaddress',
  540. 'walletpassphrase',
  541. )
  542. class EthereumRPCClient(RPCClient,metaclass=AsyncInit):
  543. async def __init__(self,proto,daemon,backend):
  544. self.proto = proto
  545. self.daemon = daemon
  546. self.call_sigs = getattr(getattr(CallSigs,proto.base_proto),daemon.id,None)
  547. super().__init__(
  548. host = 'localhost' if g.test_suite else (g.rpc_host or 'localhost'),
  549. port = daemon.rpc_port )
  550. self.set_backend(backend)
  551. vi,bh,ci = await self.gathered_call(None, (
  552. ('web3_clientVersion',()),
  553. ('eth_getBlockByNumber',('latest',False)),
  554. ('eth_chainId',()),
  555. ))
  556. import re
  557. vip = re.match(self.daemon.version_pat,vi,re.ASCII)
  558. if not vip:
  559. ydie(1,fmt(f"""
  560. Aborting on daemon mismatch:
  561. Requested daemon: {self.daemon.id}
  562. Running daemon: {vi}
  563. """,strip_char='\t').rstrip())
  564. self.daemon_version = int('{:d}{:03d}{:03d}'.format(*[int(e) for e in vip.groups()]))
  565. self.daemon_version_str = '{}.{}.{}'.format(*vip.groups())
  566. self.daemon_version_info = vi
  567. self.blockcount = int(bh['number'],16)
  568. self.cur_date = int(bh['timestamp'],16)
  569. self.caps = ()
  570. from .obj import Int
  571. if self.daemon.id in ('parity','openethereum'):
  572. if (await self.call('parity_nodeKind'))['capability'] == 'full':
  573. self.caps += ('full_node',)
  574. self.chainID = None if ci == None else Int(ci,16) # parity/oe return chainID only for dev chain
  575. self.chain = (await self.call('parity_chain')).replace(' ','_').replace('_testnet','')
  576. elif self.daemon.id in ('geth','erigon'):
  577. if self.daemon.network == 'mainnet':
  578. daemon_warning(self.daemon.id)
  579. self.caps += ('full_node',)
  580. self.chainID = Int(ci,16)
  581. self.chain = self.proto.chain_ids[self.chainID]
  582. rpcmethods = (
  583. 'eth_blockNumber',
  584. 'eth_call',
  585. # Returns the EIP155 chain ID used for transaction signing at the current best block.
  586. # Parity: Null is returned if not available, ID not required in transactions
  587. # Erigon: always returns ID, requires ID in transactions
  588. 'eth_chainId',
  589. 'eth_gasPrice',
  590. 'eth_getBalance',
  591. 'eth_getCode',
  592. 'eth_getTransactionCount',
  593. 'eth_getTransactionReceipt',
  594. 'eth_sendRawTransaction',
  595. 'parity_chain',
  596. 'parity_nodeKind',
  597. 'parity_pendingTransactions',
  598. 'txpool_content',
  599. )
  600. class MoneroRPCClient(RPCClient):
  601. auth_type = None
  602. network_proto = 'https'
  603. host_path = '/json_rpc'
  604. verify_server = False
  605. def __init__(self,host,port,user,passwd,test_connection=True,proxy=None,daemon=None):
  606. if proxy is not None:
  607. self.proxy = IPPort(proxy)
  608. test_connection = False
  609. if host.endswith('.onion'):
  610. self.network_proto = 'http'
  611. super().__init__(host,port,test_connection)
  612. if self.auth_type:
  613. self.auth = auth_data(user,passwd)
  614. if True:
  615. self.set_backend('requests')
  616. else: # insecure, for debugging only
  617. self.set_backend('curl')
  618. self.backend.exec_opts.remove('--silent')
  619. self.backend.exec_opts.append('--verbose')
  620. self.daemon = daemon
  621. async def call(self,method,*params,**kwargs):
  622. assert params == (), f'{type(self).__name__}.call() accepts keyword arguments only'
  623. return await self.process_http_resp(self.backend.run(
  624. payload = {'id': 0, 'jsonrpc': '2.0', 'method': method, 'params': kwargs },
  625. timeout = 3600, # allow enough time to sync ≈1,000,000 blocks
  626. wallet = None
  627. ))
  628. rpcmethods = ( 'get_info', )
  629. class MoneroRPCClientRaw(MoneroRPCClient):
  630. json_rpc = False
  631. host_path = '/'
  632. async def call(self,method,*params,**kwargs):
  633. assert params == (), f'{type(self).__name__}.call() accepts keyword arguments only'
  634. return await self.process_http_resp(self.backend.run(
  635. payload = kwargs,
  636. timeout = self.timeout,
  637. wallet = method
  638. ))
  639. @staticmethod
  640. def make_host_path(arg):
  641. return arg
  642. async def do_stop_daemon(self,silent=False):
  643. return await self.call('stop_daemon')
  644. rpcmethods = ( 'get_height', 'send_raw_transaction', 'stop_daemon' )
  645. class MoneroWalletRPCClient(MoneroRPCClient):
  646. auth_type = 'digest'
  647. def __init__(self,daemon,test_connection=True):
  648. RPCClient.__init__(
  649. self,
  650. daemon.host,
  651. daemon.rpc_port,
  652. test_connection = test_connection )
  653. self.daemon = daemon
  654. self.auth = auth_data(daemon.user,daemon.passwd)
  655. self.set_backend('requests')
  656. rpcmethods = (
  657. 'get_version',
  658. 'get_height', # sync height of the open wallet
  659. 'get_balance', # account_index=0, address_indices=[]
  660. 'create_wallet', # filename, password, language="English"
  661. 'open_wallet', # filename, password
  662. 'close_wallet',
  663. # filename,password,seed (restore_height,language,seed_offset,autosave_current)
  664. 'restore_deterministic_wallet',
  665. 'refresh', # start_height
  666. )
  667. async def do_stop_daemon(self,silent=False):
  668. """
  669. NB: the 'stop_wallet' RPC call closes the open wallet before shutting down the daemon,
  670. returning an error if no wallet is open
  671. """
  672. return await self.call('stop_wallet')
  673. class daemon_warning(oneshot_warning_group):
  674. class geth:
  675. color = 'yellow'
  676. message = 'Geth has not been tested on mainnet. You may experience problems.'
  677. class erigon:
  678. color = 'red'
  679. message = 'Erigon support is EXPERIMENTAL. Use at your own risk!!!'
  680. class version:
  681. color = 'yellow'
  682. message = 'ignoring unsupported {} daemon version at user request'
  683. def handle_unsupported_daemon_version(rpc,name,warn_only):
  684. if warn_only:
  685. daemon_warning('version',div=name,fmt_args=[rpc.daemon.coind_name])
  686. else:
  687. name = rpc.daemon.coind_name
  688. rdie(1,'\n'+fmt(f"""
  689. The running {name} daemon has version {rpc.daemon_version_str}.
  690. This version of MMGen is tested only on {name} v{rpc.daemon.coind_version_str} and below.
  691. To avoid this error, downgrade your daemon to a supported version.
  692. Alternatively, you may invoke the command with the --ignore-daemon-version
  693. option, in which case you proceed at your own risk.
  694. """,indent=' '))
  695. async def rpc_init(proto,backend=None,daemon=None,ignore_daemon_version=False):
  696. if not 'rpc' in proto.mmcaps:
  697. die(1,f'Coin daemon operations not supported for {proto.name} protocol!')
  698. from .daemon import CoinDaemon
  699. rpc = await {
  700. 'Bitcoin': BitcoinRPCClient,
  701. 'Ethereum': EthereumRPCClient,
  702. }[proto.base_proto](
  703. proto = proto,
  704. daemon = daemon or CoinDaemon(proto=proto,test_suite=g.test_suite),
  705. backend = backend or opt.rpc_backend )
  706. if rpc.daemon_version > rpc.daemon.coind_version:
  707. handle_unsupported_daemon_version(
  708. rpc,
  709. proto.name,
  710. ignore_daemon_version or proto.ignore_daemon_version or g.ignore_daemon_version )
  711. if rpc.chain not in proto.chain_names:
  712. raise RPCChainMismatch('\n'+fmt(f"""
  713. Protocol: {proto.cls_name}
  714. Valid chain names: {fmt_list(proto.chain_names,fmt='bare')}
  715. RPC client chain: {rpc.chain}
  716. """,indent=' ').rstrip())
  717. return rpc