Add Decred rpc

This commit is contained in:
tecnovert
2024-04-18 22:15:35 +02:00
parent 942b436974
commit 9160bfe452
14 changed files with 141 additions and 72 deletions

View File

@@ -654,6 +654,9 @@ class BasicSwap(BaseApp):
interface = LTCInterface(self.coin_clients[coin], self.chain, self)
self.coin_clients[coin]['interface_mweb'] = LTCInterfaceMWEB(self.coin_clients[coin], self.chain, self)
return interface
elif coin == Coins.DCR:
from .interface.dcr import DCRInterface
return DCRInterface(self.coin_clients[coin], self.chain, self)
elif coin == Coins.NMC:
from .interface.nmc import NMCInterface
return NMCInterface(self.coin_clients[coin], self.chain, self)

View File

@@ -0,0 +1,4 @@
from .dcr import DCRInterface
__all__ = ['DCRInterface',]

View File

@@ -15,6 +15,7 @@ from basicswap.util.crypto import (
blake256,
ripemd160,
)
from basicswap.interface.dcr.rpc import make_rpc_func
class DCRInterface(Secp256k1Interface):
@@ -41,6 +42,10 @@ class DCRInterface(Secp256k1Interface):
def __init__(self, coin_settings, network, swap_client=None):
super().__init__(network)
self._rpc_host = coin_settings.get('rpchost', '127.0.0.1')
self._rpcport = coin_settings['rpcport']
self._rpcauth = coin_settings['rpcauth']
self.rpc = make_rpc_func(self._rpcport, self._rpcauth, host=self._rpc_host)
def pkh(self, pubkey: bytes) -> bytes:
return ripemd160(blake256(pubkey))
@@ -61,3 +66,9 @@ class DCRInterface(Secp256k1Interface):
if blake256(blake256(prefixed_data))[:4] != checksum:
raise ValueError('Checksum mismatch')
return prefixed_data
def testDaemonRPC(self, with_wallet=True) -> None:
if with_wallet:
self.rpc_wallet('getwalletinfo')
else:
self.rpc('getblockchaininfo')

View File

@@ -0,0 +1,39 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2024 tecnovert
# Distributed under the MIT software license, see the accompanying
# file LICENSE or http://www.opensource.org/licenses/mit-license.php.
import json
import traceback
from basicswap.rpc import Jsonrpc
def callrpc(rpc_port, auth, method, params=[], host='127.0.0.1'):
try:
url = 'http://{}@{}:{}/'.format(auth, host, rpc_port)
x = Jsonrpc(url)
x.__handler = None
v = x.json_request(method, params)
x.close()
print('[rm] v', v)
r = json.loads(v.decode('utf-8'))
except Exception as ex:
traceback.print_exc()
raise ValueError('RPC server error ' + str(ex) + ', method: ' + method)
if 'error' in r and r['error'] is not None:
raise ValueError('RPC error ' + str(r['error']))
return r['result']
def make_rpc_func(port, auth, host='127.0.0.1'):
port = port
auth = auth
host = host
def rpc_func(method, params=None):
nonlocal port, auth, host
return callrpc(port, auth, method, params, host)
return rpc_func

View File

@@ -1,15 +1,13 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2020-2023 tecnovert
# Copyright (c) 2020-2024 tecnovert
# Distributed under the MIT software license, see the accompanying
# file LICENSE or http://www.opensource.org/licenses/mit-license.php.
import os
import time
import json
import shlex
import urllib
import logging
import traceback
import subprocess
from xmlrpc.client import (
@@ -20,18 +18,6 @@ from xmlrpc.client import (
from .util import jsonDecimal
def waitForRPC(rpc_func, expect_wallet=True, max_tries=7):
for i in range(max_tries + 1):
try:
rpc_func('getwalletinfo' if expect_wallet else 'getblockchaininfo')
return
except Exception as ex:
if i < max_tries:
logging.warning('Can\'t connect to RPC: %s. Retrying in %d second/s.', str(ex), (i + 1))
time.sleep(i + 1)
raise ValueError('waitForRPC failed')
class Jsonrpc():
# __getattr__ complicates extending ServerProxy
def __init__(self, uri, transport=None, encoding=None, verbose=False,