Skip to content

Commit

Permalink
Merge pull request #370 from n1rna/fix/multi-wallet-bitcoind-connection
Browse files Browse the repository at this point in the history
Fix multi wallet connection bitcoind client with optional walletname
  • Loading branch information
mccwdev authored Apr 7, 2024
2 parents 8851b53 + 2bd626f commit 2d7e2f2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
4 changes: 2 additions & 2 deletions bitcoinlib/services/bitcoind.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class BitcoindClient(BaseClient):

@staticmethod
@deprecated
def from_config(configfile=None, network='bitcoin', *args):
def from_config(configfile=None, network='bitcoin', **kwargs):
"""
Read settings from bitcoind config file
Expand Down Expand Up @@ -116,7 +116,7 @@ def from_config(configfile=None, network='bitcoin', *args):
server = _read_from_config(config, 'rpc', 'externalip', server)

url = "http://%s:%s@%s:%s" % (config.get('rpc', 'rpcuser'), config.get('rpc', 'rpcpassword'), server, port)
return BitcoindClient(network, url, *args)
return BitcoindClient(network, url, **kwargs)

def __init__(self, network='bitcoin', base_url='', denominator=100000000, *args):
"""
Expand Down
10 changes: 8 additions & 2 deletions bitcoinlib/services/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class Service(object):

def __init__(self, network=DEFAULT_NETWORK, min_providers=1, max_providers=1, providers=None,
timeout=TIMEOUT_REQUESTS, cache_uri=None, ignore_priority=False, exclude_providers=None,
max_errors=SERVICE_MAX_ERRORS, strict=True):
max_errors=SERVICE_MAX_ERRORS, strict=True, wallet_name=None):
"""
Create a service object for the specified network. By default, the object connect to 1 service provider, but you
can specify a list of providers or a minimum or maximum number of providers.
Expand Down Expand Up @@ -131,6 +131,7 @@ def __init__(self, network=DEFAULT_NETWORK, min_providers=1, max_providers=1, pr
self._blockcount = None
self.cache = None
self.cache_uri = cache_uri
self.wallet_name = wallet_name
try:
self.cache = Cache(self.network, db_uri=cache_uri)
except Exception as e:
Expand Down Expand Up @@ -166,8 +167,13 @@ def _provider_execute(self, method, *arguments):
continue
client = getattr(services, self.providers[sp]['provider'])
providerclient = getattr(client, self.providers[sp]['client_class'])

base_url = self.providers[sp]['url']
if 'bitcoind' in sp and self.wallet_name is not None:
base_url = f"{base_url}/wallet/{self.wallet_name}"

pc_instance = providerclient(
self.network, self.providers[sp]['url'], self.providers[sp]['denominator'],
self.network, base_url, self.providers[sp]['denominator'],
self.providers[sp]['api_key'], self.providers[sp]['provider_coin_id'],
self.providers[sp]['network_overrides'], self.timeout, self._blockcount, self.strict)
if not hasattr(pc_instance, method):
Expand Down
17 changes: 8 additions & 9 deletions bitcoinlib/wallets.py
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,7 @@ def send(self, offline=False):
if offline:
return None

srv = Service(network=self.network.name, providers=self.hdwallet.providers,
srv = Service(network=self.network.name, wallet_name=self.hdwallet.name, providers=self.hdwallet.providers,
cache_uri=self.hdwallet.db_cache_uri)
res = srv.sendrawtransaction(self.raw_hex())
if not res:
Expand Down Expand Up @@ -2604,7 +2604,7 @@ def balance_update_from_serviceprovider(self, account_id=None, network=None):
"""

network, account_id, acckey = self._get_account_defaults(network, account_id)
srv = Service(network=network, providers=self.providers, cache_uri=self.db_cache_uri)
srv = Service(network=network, wallet_name=self.name, providers=self.providers, cache_uri=self.db_cache_uri)
balance = srv.getbalance(self.addresslist(account_id=account_id, network=network))
if srv.results:
new_balance = {
Expand Down Expand Up @@ -2828,8 +2828,7 @@ def utxos_update(self, account_id=None, used=None, networks=None, key_id=None, d
addresslist = self.addresslist(account_id=account_id, used=used, network=network, key_id=key_id,
change=change, depth=depth)
random.shuffle(addresslist)
srv = Service(network=network, providers=self.providers, cache_uri=self.db_cache_uri)
srv = Service(network=network, providers=self.providers, cache_uri=self.db_cache_uri)
srv = Service(network=network, wallet_name=self.name, providers=self.providers, cache_uri=self.db_cache_uri)
utxos = []
for address in addresslist:
if rescan_all:
Expand Down Expand Up @@ -3030,7 +3029,7 @@ def transactions_update_confirmations(self):
:return:
"""
network = self.network.name
srv = Service(network=network, providers=self.providers, cache_uri=self.db_cache_uri)
srv = Service(network=network, wallet_name=self.name, providers=self.providers, cache_uri=self.db_cache_uri)
blockcount = srv.blockcount()
db_txs = self._session.query(DbTransaction). \
filter(DbTransaction.wallet_id == self.wallet_id,
Expand All @@ -3055,7 +3054,7 @@ def transactions_update_by_txids(self, txids):
txids = list(dict.fromkeys(txids))

txs = []
srv = Service(network=self.network.name, providers=self.providers, cache_uri=self.db_cache_uri)
srv = Service(network=self.network.name, wallet_name=self.name, providers=self.providers, cache_uri=self.db_cache_uri)
for txid in txids:
tx = srv.gettransaction(to_hexstring(txid))
if tx:
Expand Down Expand Up @@ -3111,7 +3110,7 @@ def transactions_update(self, account_id=None, used=None, network=None, key_id=N
if not key_id:
self.transactions_update_confirmations()

srv = Service(network=network, providers=self.providers, cache_uri=self.db_cache_uri)
srv = Service(network=network, wallet_name=self.name, providers=self.providers, cache_uri=self.db_cache_uri)
blockcount = srv.blockcount()
db_txs = self._session.query(DbTransaction).\
filter(DbTransaction.wallet_id == self.wallet_id,
Expand Down Expand Up @@ -3550,7 +3549,7 @@ def transaction_create(self, output_arr, input_arr=None, input_key_id=None, acco
addr = addr.key()
transaction.add_output(value, addr)

srv = Service(network=network, providers=self.providers, cache_uri=self.db_cache_uri)
srv = Service(network=network, wallet_name=self.name, providers=self.providers, cache_uri=self.db_cache_uri)
transaction.fee_per_kb = None
if isinstance(fee, int):
fee_estimate = fee
Expand Down Expand Up @@ -4036,7 +4035,7 @@ def sweep(self, to_address, account_id=None, input_key_id=None, network=None, ma
continue
input_arr.append((utxo['txid'], utxo['output_n'], utxo['key_id'], utxo['value']))
total_amount += utxo['value']
srv = Service(network=network, providers=self.providers, cache_uri=self.db_cache_uri)
srv = Service(network=network, wallet_name=self.name, providers=self.providers, cache_uri=self.db_cache_uri)

if isinstance(fee, str):
n_outputs = 1 if not isinstance(to_address, list) else len(to_address)
Expand Down

0 comments on commit 2d7e2f2

Please sign in to comment.