Skip to content

Commit

Permalink
Merge again: Fix multi wallet connection bitcoind client with optiona…
Browse files Browse the repository at this point in the history
…l walletname
  • Loading branch information
n1rna authored and Cryp Toon committed Apr 8, 2024
1 parent 2917687 commit 07b8464
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 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
21 changes: 13 additions & 8 deletions bitcoinlib/wallets.py
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,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 @@ -2801,7 +2801,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 @@ -3025,7 +3025,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, 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 @@ -3226,7 +3226,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()
self.session.query(DbTransaction).\
filter(DbTransaction.wallet_id == self.wallet_id,
Expand All @@ -3249,7 +3249,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 @@ -3304,7 +3304,7 @@ def transactions_update(self, account_id=None, used=None, network=None, key_id=N
# Update number of confirmations and status for already known transactions
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)

# Get transactions for wallet's addresses
txs = []
Expand Down Expand Up @@ -3545,6 +3545,11 @@ def transaction_spent(self, txid, output_n):
if qr:
return qr.transaction.txid.hex()


def update_transactions_from_block(block, network=None):
pass


def _objects_by_key_id(self, key_id):
key = self.session.query(DbKey).filter_by(id=key_id).scalar()
if not key:
Expand Down Expand Up @@ -3737,7 +3742,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)

if not locktime and self.anti_fee_sniping:
srv = Service(network=network, providers=self.providers, cache_uri=self.db_cache_uri)
Expand Down Expand Up @@ -4244,7 +4249,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 07b8464

Please sign in to comment.