| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- #!/usr/bin/env python3
- #
- # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
- # Copyright (C)2013-2019 The MMGen Project <mmgen@tuta.io>
- #
- # This program is free software: you can redistribute it and/or modify
- # it under the terms of the GNU General Public License as published by
- # the Free Software Foundation, either version 3 of the License, or
- # (at your option) any later version.
- #
- # This program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- #
- # You should have received a copy of the GNU General Public License
- # along with this program. If not, see <http://www.gnu.org/licenses/>.
- """
- rpc.py: Cryptocoin RPC library for the MMGen suite
- """
- import http.client,base64,json
- from mmgen.common import *
- from decimal import Decimal
- def dmsg_rpc(s):
- if g.debug_rpc: msg(s)
- class CoinDaemonRPCConnection(object):
- auth = True
- db_fs = ' host [{h}] port [{p}] user [{u}] passwd [{pw}] auth_cookie [{c}]\n'
- def __init__(self,host=None,port=None,user=None,passwd=None,auth_cookie=None):
- dmsg_rpc('=== {}.__init__() debug ==='.format(type(self).__name__))
- dmsg_rpc(self.db_fs.format(h=host,p=port,u=user,pw=passwd,c=auth_cookie))
- import socket
- try:
- socket.create_connection((host,port),timeout=3).close()
- except:
- die(1,'Unable to connect to {}:{}'.format(host,port))
- if not self.auth:
- pass
- elif user and passwd:
- self.auth_str = '{}:{}'.format(user,passwd)
- elif auth_cookie:
- self.auth_str = auth_cookie
- else:
- msg('Error: no {} RPC authentication method found'.format(g.proto.name.capitalize()))
- if passwd: die(1,"'rpcuser' entry not found in {}.conf or mmgen.cfg".format(g.proto.name))
- elif user: die(1,"'rpcpassword' entry not found in {}.conf or mmgen.cfg".format(g.proto.name))
- else:
- m1 = 'Either provide rpcuser/rpcpassword in {pn}.conf or mmgen.cfg\n'
- m2 = '(or, alternatively, copy the authentication cookie to the {pnu}\n'
- m3 = 'data dir if {pnm} and {dn} are running as different users)'
- die(1,(m1+m2+m3).format(
- pn=g.proto.name,
- pnu=g.proto.name.capitalize(),
- dn=g.proto.daemon_name,
- pnm=g.proj_name))
- self.host = host
- self.port = port
- for method in self.rpcmethods:
- exec('{c}.{m} = lambda self,*args,**kwargs: self.request("{m}",*args,**kwargs)'.format(
- c=type(self).__name__,m=method))
- # Normal mode: call with arg list unrolled, exactly as with cli
- # Batch mode: call with list of arg lists as first argument
- # kwargs are for local use and are not passed to server
- # By default, raises RPCFailure exception with an error msg on all errors and exceptions
- # on_fail is one of 'raise' (default), 'return' or 'silent'
- # With on_fail='return', returns 'rpcfail',(resp_object,(die_args))
- def request(self,cmd,*args,**kwargs):
- if g.rpc_fail_on_command == cmd:
- cmd = 'badcommand_' + cmd
- cf = { 'timeout':g.http_timeout, 'batch':False, 'on_fail':'raise' }
- if cf['on_fail'] not in ('raise','return','silent'):
- raise ValueError("request(): {}: illegal value for 'on_fail'".format(cf['on_fail']))
- for k in cf:
- if k in kwargs and kwargs[k]: cf[k] = kwargs[k]
- hc = http.client.HTTPConnection(self.host,self.port,cf['timeout'])
- if cf['batch']:
- p = [{'method':cmd,'params':r,'id':n,'jsonrpc':'2.0'} for n,r in enumerate(args[0],1)]
- else:
- p = {'method':cmd,'params':args,'id':1,'jsonrpc':'2.0'}
- def do_fail(*args):
- if cf['on_fail'] in ('return','silent'): return 'rpcfail',args
- try: s = '{}'.format(args[2])
- except: s = repr(args[2])
- raise RPCFailure(s)
- dmsg_rpc('=== request() debug ===')
- dmsg_rpc(' RPC POST data ==> {}\n'.format(p))
- ca_type = self.coin_amt_type if hasattr(self,'coin_amt_type') else str
- from mmgen.obj import HexStr
- class MyJSONEncoder(json.JSONEncoder):
- def default(self,obj):
- if isinstance(obj,g.proto.coin_amt):
- return ca_type(obj)
- elif isinstance(obj,HexStr):
- return obj
- else:
- return json.JSONEncoder.default(self,obj)
- http_hdr = { 'Content-Type': 'application/json' }
- if self.auth:
- fs = ' RPC AUTHORIZATION data ==> raw: [{}]\n{:>31}enc: [Basic {}]\n'
- as_enc = base64.b64encode(self.auth_str.encode())
- dmsg_rpc(fs.format(self.auth_str,'',as_enc))
- http_hdr.update({ 'Host':self.host, 'Authorization':'Basic {}'.format(as_enc.decode()) })
- try:
- hc.request('POST','/',json.dumps(p,cls=MyJSONEncoder),http_hdr)
- except Exception as e:
- m = '{}\nUnable to connect to {} at {}:{}'
- return do_fail(None,2,m.format(e.args[0],g.proto.daemon_name,self.host,self.port))
- try:
- r = hc.getresponse() # returns HTTPResponse instance
- except Exception:
- m = 'Unable to connect to {} at {}:{} (but port is bound?)'
- return do_fail(None,2,m.format(g.proto.daemon_name,self.host,self.port))
- dmsg_rpc(' RPC GETRESPONSE data ==> {}\n'.format(r.__dict__))
- if r.status != 200:
- if cf['on_fail'] not in ('silent','raise'):
- msg_r(yellow('{} RPC Error: '.format(g.proto.daemon_name.capitalize())))
- msg(red('{} {}'.format(r.status,r.reason)))
- e1 = r.read().decode()
- try:
- e3 = json.loads(e1)['error']
- e2 = '{} (code {})'.format(e3['message'],e3['code'])
- except:
- e2 = str(e1)
- return do_fail(r,1,e2)
- r2 = r.read().decode()
- dmsg_rpc(' RPC REPLY data ==> {}\n'.format(r2))
- if not r2:
- return do_fail(r,2,'Empty reply')
- r3 = json.loads(r2,parse_float=Decimal)
- ret = []
- for resp in r3 if cf['batch'] else [r3]:
- if 'error' in resp and resp['error'] != None:
- return do_fail(r,1,'{} returned an error: {}'.format(
- g.proto.daemon_name.capitalize(),resp['error']))
- elif 'result' not in resp:
- return do_fail(r,1, 'Missing JSON-RPC result\n' + repr(resps))
- else:
- ret.append(resp['result'])
- return ret if cf['batch'] else ret[0]
- rpcmethods = (
- 'backupwallet',
- 'createrawtransaction',
- 'decoderawtransaction',
- 'disconnectnode',
- 'estimatefee',
- 'estimatesmartfee',
- 'getaddressesbyaccount',
- 'getaddressesbylabel',
- 'getbalance',
- 'getblock',
- 'getblockchaininfo',
- 'getblockcount',
- 'getblockhash',
- 'getmempoolinfo',
- 'getmempoolentry',
- 'getnettotals',
- 'getnetworkinfo',
- 'getpeerinfo',
- 'getrawmempool',
- 'getmempoolentry',
- 'getrawtransaction',
- 'gettransaction',
- 'importaddress',
- 'listaccounts',
- 'listlabels',
- 'listunspent',
- 'setlabel',
- 'sendrawtransaction',
- 'signrawtransaction',
- 'signrawtransactionwithkey', # method new to Core v0.17.0
- 'validateaddress',
- 'walletpassphrase',
- )
- class EthereumRPCConnection(CoinDaemonRPCConnection):
- auth = False
- db_fs = ' host [{h}] port [{p}]\n'
- rpcmethods = (
- 'eth_accounts',
- 'eth_blockNumber',
- 'eth_call',
- # Returns the EIP155 chain ID used for transaction signing at the current best block.
- # Null is returned if not available.
- 'eth_chainId',
- 'eth_gasPrice',
- 'eth_getBalance',
- 'eth_getBlockByHash',
- 'eth_getBlockByNumber',
- 'eth_getCode',
- 'eth_getTransactionByHash',
- 'eth_getTransactionReceipt',
- 'eth_protocolVersion',
- 'eth_sendRawTransaction',
- 'eth_signTransaction',
- 'eth_syncing',
- 'net_listening',
- 'net_peerCount',
- 'net_version',
- 'parity_chain',
- 'parity_chainId', # superseded by eth_chainId
- 'parity_chainStatus',
- 'parity_composeTransaction',
- 'parity_gasCeilTarget',
- 'parity_gasFloorTarget',
- 'parity_localTransactions',
- 'parity_minGasPrice',
- 'parity_mode',
- 'parity_netPeers',
- 'parity_nextNonce',
- 'parity_nodeKind',
- 'parity_nodeName',
- 'parity_pendingTransactions',
- 'parity_pendingTransactionsStats',
- 'parity_versionInfo',
- )
- def rpc_error(ret):
- return type(ret) is tuple and ret and ret[0] == 'rpcfail'
- def rpc_errmsg(ret): return ret[1][2]
|