From cd3e1e3574d5f0977a566b5b5d02640310f7a57b Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Fri, 21 Feb 2020 13:53:21 +0000 Subject: [PATCH] MoneroWalletRPCConnection: use requests library --- mmgen/rpc.py | 38 +++++++++++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/mmgen/rpc.py b/mmgen/rpc.py index 06a7ca06..a38096ca 100755 --- a/mmgen/rpc.py +++ b/mmgen/rpc.py @@ -74,6 +74,8 @@ class RPCConnection(MMGenObject): self.host = host self.port = port + self.user = user + self.passwd = passwd for method in self.rpcmethods: exec('{c}.{m} = lambda self,*args,**kwargs: self.request("{m}",*args,**kwargs)'.format( @@ -296,18 +298,44 @@ class EthereumRPCConnection(RPCConnection): ) class MoneroWalletRPCConnection(RPCConnection): + rpcmethods = ( 'get_version', 'get_height', # sync height of the open wallet - 'get_balance', # { "account_index":0,"address_indices":[0,1] } - 'create_wallet', # { "filename":"name","password":"passw0rd","language":"English" } - 'open_wallet', # { "filename":"name","password":"passw0rd" } + 'get_balance', # account_index=0, address_indices=[] + 'create_wallet', # filename, password, language="English" + 'open_wallet', # filename, password 'close_wallet', 'restore_deterministic_wallet', # name,password,seed (restore_height,language,seed_offset,autosave_current) - 'refresh', # {"start_height":100000} + 'refresh', # start_height ) def request(self,cmd,*args,**kwargs): + if args != (): + m = '{}.request() accepts only keyword args\nCmd: {!r}' + raise ValueError(m.format(type(self).__name__,cmd)) + import requests + import urllib3 + urllib3.disable_warnings() + ret = requests.post( + url = 'https://{}:{}/json_rpc'.format(self.host,self.port), + json = { + 'jsonrpc': '2.0', + 'id': '0', + 'method': cmd, + 'params': kwargs, + }, + auth = requests.auth.HTTPDigestAuth(self.user,self.passwd), + headers = self.http_hdrs, + verify = False ) + + res = json.loads(ret._content) + if 'error' in res: + raise RPCFailure(repr(res['error'])) + return(res['result']) + + def request_curltest(self,cmd,*args,**kwargs): + "insecure, for testing only" from subprocess import run,PIPE data = { 'jsonrpc': '2.0', @@ -316,7 +344,7 @@ class MoneroWalletRPCConnection(RPCConnection): 'params': kwargs, } exec_cmd = [ - 'curl', '--proxy', '', '--silent','--insecure', '--request', 'POST', + 'curl', '--proxy', '', '--verbose','--insecure', '--request', 'POST', '--digest', '--user', '{}:{}'.format(g.monero_wallet_rpc_user,g.monero_wallet_rpc_password), '--header', 'Content-Type: application/json', '--data', json.dumps(data),