mirror of
https://github.com/basicswap/basicswap.git
synced 2025-11-05 18:38:09 +01:00
xmr: Support daemon rpc login details.
This commit is contained in:
@@ -1,3 +1,3 @@
|
||||
name = "basicswap"
|
||||
|
||||
__version__ = "0.11.49"
|
||||
__version__ = "0.11.50"
|
||||
|
||||
@@ -467,17 +467,25 @@ class BasicSwap(BaseApp):
|
||||
else:
|
||||
raise ValueError('Missing XMR wallet rpc credentials.')
|
||||
|
||||
self.coin_clients[coin]['rpcuser'] = chain_client_settings.get('rpcuser', '')
|
||||
self.coin_clients[coin]['rpcpassword'] = chain_client_settings.get('rpcpassword', '')
|
||||
|
||||
def selectXMRRemoteDaemon(self, coin):
|
||||
self.log.info('Selecting remote XMR daemon.')
|
||||
chain_client_settings = self.getChainClientSettings(coin)
|
||||
remote_daemon_urls = chain_client_settings.get('remote_daemon_urls', [])
|
||||
rpchost = self.coin_clients[coin]['rpchost']
|
||||
rpcport = self.coin_clients[coin]['rpcport']
|
||||
|
||||
coin_settings = self.coin_clients[coin]
|
||||
rpchost = coin_settings['rpchost']
|
||||
rpcport = coin_settings['rpcport']
|
||||
daemon_login = None
|
||||
if coin_settings.get('rpcuser', '') != '':
|
||||
daemon_login = (coin_settings.get('rpcuser', ''), coin_settings.get('rpcpassword', ''))
|
||||
current_daemon_url = f'{rpchost}:{rpcport}'
|
||||
if current_daemon_url in remote_daemon_urls:
|
||||
self.log.info(f'Trying last used url {rpchost}:{rpcport}.')
|
||||
try:
|
||||
rpc_cb2 = make_xmr_rpc2_func(rpcport, rpchost)
|
||||
rpc_cb2 = make_xmr_rpc2_func(rpcport, daemon_login, rpchost)
|
||||
test = rpc_cb2('get_height', timeout=20)['height']
|
||||
return True
|
||||
except Exception as e:
|
||||
@@ -487,10 +495,10 @@ class BasicSwap(BaseApp):
|
||||
self.log.info(f'Trying url {url}.')
|
||||
try:
|
||||
rpchost, rpcport = url.rsplit(':', 1)
|
||||
rpc_cb2 = make_xmr_rpc2_func(rpcport, rpchost)
|
||||
rpc_cb2 = make_xmr_rpc2_func(rpcport, daemon_login, rpchost)
|
||||
test = rpc_cb2('get_height', timeout=20)['height']
|
||||
self.coin_clients[coin]['rpchost'] = rpchost
|
||||
self.coin_clients[coin]['rpcport'] = rpcport
|
||||
coin_settings['rpchost'] = rpchost
|
||||
coin_settings['rpcport'] = rpcport
|
||||
data = {
|
||||
'rpchost': rpchost,
|
||||
'rpcport': rpcport,
|
||||
|
||||
@@ -411,6 +411,8 @@ class CoinInterface:
|
||||
str_error = str(ex).lower()
|
||||
if 'not enough unlocked money' in str_error:
|
||||
return True
|
||||
if 'No unlocked balance' in str_error:
|
||||
return True
|
||||
if 'transaction was rejected by daemon' in str_error:
|
||||
return True
|
||||
if 'invalid unlocked_balance' in str_error:
|
||||
|
||||
@@ -31,8 +31,7 @@ from basicswap.util import (
|
||||
TemporaryError)
|
||||
from basicswap.rpc_xmr import (
|
||||
make_xmr_rpc_func,
|
||||
make_xmr_rpc2_func,
|
||||
make_xmr_wallet_rpc_func)
|
||||
make_xmr_rpc2_func)
|
||||
from basicswap.util import (
|
||||
b2i, b2h)
|
||||
from basicswap.chainparams import XMR_COIN, CoinInterface, Coins
|
||||
@@ -65,9 +64,12 @@ class XMRInterface(CoinInterface):
|
||||
|
||||
def __init__(self, coin_settings, network, swap_client=None):
|
||||
super().__init__(network)
|
||||
self.rpc_cb = make_xmr_rpc_func(coin_settings['rpcport'], host=coin_settings.get('rpchost', '127.0.0.1'))
|
||||
self.rpc_cb2 = make_xmr_rpc2_func(coin_settings['rpcport'], host=coin_settings.get('rpchost', '127.0.0.1')) # non-json endpoint
|
||||
self.rpc_wallet_cb = make_xmr_wallet_rpc_func(coin_settings['walletrpcport'], coin_settings['walletrpcauth'], host=coin_settings.get('walletrpchost', '127.0.0.1'))
|
||||
daemon_login = None
|
||||
if coin_settings.get('rpcuser', '') != '':
|
||||
daemon_login = (coin_settings.get('rpcuser', ''), coin_settings.get('rpcpassword', ''))
|
||||
self.rpc_cb = make_xmr_rpc_func(coin_settings['rpcport'], daemon_login, host=coin_settings.get('rpchost', '127.0.0.1'))
|
||||
self.rpc_cb2 = make_xmr_rpc2_func(coin_settings['rpcport'], daemon_login, host=coin_settings.get('rpchost', '127.0.0.1')) # non-json endpoint
|
||||
self.rpc_wallet_cb = make_xmr_rpc_func(coin_settings['walletrpcport'], coin_settings['walletrpcauth'], host=coin_settings.get('walletrpchost', '127.0.0.1'))
|
||||
|
||||
self.blocks_confirmed = coin_settings['blocks_confirmed']
|
||||
self._restore_height = coin_settings.get('restore_height', 0)
|
||||
|
||||
@@ -159,7 +159,7 @@ class JsonrpcDigest():
|
||||
raise
|
||||
|
||||
|
||||
def callrpc_xmr(rpc_port, auth, method, params=[], rpc_host='127.0.0.1', path='json_rpc', timeout=120):
|
||||
def callrpc_xmr(rpc_port, method, params=[], rpc_host='127.0.0.1', path='json_rpc', auth=None, timeout=120):
|
||||
# auth is a tuple: (username, password)
|
||||
try:
|
||||
if rpc_host.count('://') > 0:
|
||||
@@ -168,27 +168,10 @@ def callrpc_xmr(rpc_port, auth, method, params=[], rpc_host='127.0.0.1', path='j
|
||||
url = 'http://{}:{}/{}'.format(rpc_host, rpc_port, path)
|
||||
|
||||
x = JsonrpcDigest(url)
|
||||
v = x.json_request(method, params, username=auth[0], password=auth[1], timeout=timeout)
|
||||
x.close()
|
||||
r = json.loads(v.decode('utf-8'))
|
||||
except Exception as ex:
|
||||
raise ValueError('RPC Server Error: {}'.format(str(ex)))
|
||||
|
||||
if 'error' in r and r['error'] is not None:
|
||||
raise ValueError('RPC error ' + str(r['error']))
|
||||
|
||||
return r['result']
|
||||
|
||||
|
||||
def callrpc_xmr_na(rpc_port, method, params=[], rpc_host='127.0.0.1', path='json_rpc', timeout=120):
|
||||
try:
|
||||
if rpc_host.count('://') > 0:
|
||||
url = '{}:{}/{}'.format(rpc_host, rpc_port, path)
|
||||
if auth:
|
||||
v = x.json_request(method, params, username=auth[0], password=auth[1], timeout=timeout)
|
||||
else:
|
||||
url = 'http://{}:{}/{}'.format(rpc_host, rpc_port, path)
|
||||
|
||||
x = JsonrpcDigest(url)
|
||||
v = x.json_request(method, params, timeout=timeout)
|
||||
v = x.json_request(method, params, timeout=timeout)
|
||||
x.close()
|
||||
r = json.loads(v.decode('utf-8'))
|
||||
except Exception as ex:
|
||||
@@ -200,7 +183,7 @@ def callrpc_xmr_na(rpc_port, method, params=[], rpc_host='127.0.0.1', path='json
|
||||
return r['result']
|
||||
|
||||
|
||||
def callrpc_xmr2(rpc_port, method, params=None, rpc_host='127.0.0.1', timeout=120):
|
||||
def callrpc_xmr2(rpc_port, method, params=None, auth=None, rpc_host='127.0.0.1', timeout=120):
|
||||
try:
|
||||
if rpc_host.count('://') > 0:
|
||||
url = '{}:{}/{}'.format(rpc_host, rpc_port, method)
|
||||
@@ -208,7 +191,10 @@ def callrpc_xmr2(rpc_port, method, params=None, rpc_host='127.0.0.1', timeout=12
|
||||
url = 'http://{}:{}/{}'.format(rpc_host, rpc_port, method)
|
||||
|
||||
x = JsonrpcDigest(url)
|
||||
v = x.post_request(method, params, timeout=timeout)
|
||||
if auth:
|
||||
v = x.json_request(method, params, username=auth[0], password=auth[1], timeout=timeout)
|
||||
else:
|
||||
v = x.json_request(method, params, timeout=timeout)
|
||||
x.close()
|
||||
r = json.loads(v.decode('utf-8'))
|
||||
except Exception as ex:
|
||||
@@ -217,34 +203,23 @@ def callrpc_xmr2(rpc_port, method, params=None, rpc_host='127.0.0.1', timeout=12
|
||||
return r
|
||||
|
||||
|
||||
def make_xmr_rpc_func(port, host='127.0.0.1'):
|
||||
port = port
|
||||
host = host
|
||||
|
||||
def rpc_func(method, params=None, wallet=None, timeout=120):
|
||||
nonlocal port
|
||||
nonlocal host
|
||||
return callrpc_xmr_na(port, method, params, rpc_host=host, timeout=timeout)
|
||||
return rpc_func
|
||||
|
||||
|
||||
def make_xmr_rpc2_func(port, host='127.0.0.1'):
|
||||
port = port
|
||||
host = host
|
||||
|
||||
def rpc_func(method, params=None, wallet=None, timeout=120):
|
||||
nonlocal port
|
||||
nonlocal host
|
||||
return callrpc_xmr2(port, method, params, rpc_host=host, timeout=timeout)
|
||||
return rpc_func
|
||||
|
||||
|
||||
def make_xmr_wallet_rpc_func(port, auth, host='127.0.0.1'):
|
||||
def make_xmr_rpc2_func(port, auth, host='127.0.0.1'):
|
||||
port = port
|
||||
auth = auth
|
||||
host = host
|
||||
|
||||
def rpc_func(method, params=None, wallet=None, timeout=120):
|
||||
nonlocal port, auth, host
|
||||
return callrpc_xmr(port, auth, method, params, rpc_host=host, timeout=timeout)
|
||||
return callrpc_xmr2(port, method, params, auth=auth, rpc_host=host, timeout=timeout)
|
||||
return rpc_func
|
||||
|
||||
|
||||
def make_xmr_rpc_func(port, auth, host='127.0.0.1'):
|
||||
port = port
|
||||
auth = auth
|
||||
host = host
|
||||
|
||||
def rpc_func(method, params=None, wallet=None, timeout=120):
|
||||
nonlocal port, auth, host
|
||||
return callrpc_xmr(port, method, params, rpc_host=host, auth=auth, timeout=timeout)
|
||||
return rpc_func
|
||||
|
||||
Reference in New Issue
Block a user