rpc.py 24 KB

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