Skip to content

Commit

Permalink
[ADD] Get used witness types in wallet and use in scan method
Browse files Browse the repository at this point in the history
  • Loading branch information
mccwdev committed Nov 4, 2023
1 parent 2996676 commit 28a5e5c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
29 changes: 27 additions & 2 deletions bitcoinlib/wallets.py
Original file line number Diff line number Diff line change
Expand Up @@ -1869,8 +1869,11 @@ def scan(self, scan_gap_limit=5, account_id=None, change=None, rescan_used=False
keys_to_scan = [self.key(k.id) for k in self.keys_addresses()[counter:counter+scan_gap_limit]]
counter += scan_gap_limit
else:
keys_to_scan = self.get_keys(account_id, self.witness_type, network,
number_of_keys=scan_gap_limit, change=chg)
keys_to_scan = []
for witness_type in self.witness_types(network=network):
keys_to_scan += self.get_keys(account_id, witness_type, network,
number_of_keys=scan_gap_limit, change=chg)

n_highest_updated = 0
for key in keys_to_scan:
if key.key_id in keys_ignore:
Expand Down Expand Up @@ -2547,6 +2550,28 @@ def accounts(self, network=DEFAULT_NETWORK):
accounts = [self.default_account_id]
return list(dict.fromkeys(accounts))

def witness_types(self, account_id=None, network=None):
"""
Get witness types in use by this wallet. For example 'legacy', 'segwit', 'p2sh-segwit'
:param account_id: Account ID. Leave empty for default account
:type account_id: int
:param network: Network name filter. Default filter is DEFAULT_NETWORK
:type network: str
:return list of str:
"""

# network, account_id, _ = self._get_account_defaults(network, account_id)
qr = self._session.query(DbKey.witness_type).filter_by(wallet_id=self.wallet_id)
if network is not None:
qr = qr.filter(DbKey.network_name == network)
if account_id is not None:
qr = qr.filter(DbKey.account_id == account_id)
qr = qr.group_by(DbKey.witness_type).all()
return [x[0] for x in qr]


def networks(self, as_dict=False):
"""
Get list of networks used by this wallet
Expand Down
11 changes: 11 additions & 0 deletions tests/test_wallets.py
Original file line number Diff line number Diff line change
Expand Up @@ -2753,3 +2753,14 @@ def test_wallet_mixed_witness_no_private_key(self):
self.assertEqual(address, w.get_key().address)
self.assertRaisesRegexp(WalletError, "This wallet has no private key, cannot use multiple witness types",
w.get_key, witness_type='legacy')

def test_wallet_mixed_witness_type_create(self):
w = Wallet.create('test_wallet_mixed_witness_type_create', network='testnet', db_uri=self.DATABASE_URI)
w.get_key(witness_type='legacy')
w.new_account('test-account', 101, witness_type='p2sh-segwit')
w.get_key(account_id=101)
kltc = w.get_key(network='litecoin')
self.assertEqual(kltc.network, 'litecoin')
self.assertListEqual(sorted(w.witness_types()), ['legacy', 'p2sh-segwit', 'segwit'])
self.assertListEqual(sorted(w.witness_types(account_id=101)), ['p2sh-segwit', 'segwit'])
self.assertListEqual(w.witness_types(network='litecoin'), ['segwit'])

0 comments on commit 28a5e5c

Please sign in to comment.