Skip to content

Commit

Permalink
[REF] Raise warning when trying to use mixed witness type in watch on…
Browse files Browse the repository at this point in the history
…ly wallets
  • Loading branch information
mccwdev committed Oct 31, 2023
1 parent abda2a4 commit 2996676
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
8 changes: 5 additions & 3 deletions bitcoinlib/wallets.py
Original file line number Diff line number Diff line change
Expand Up @@ -2028,7 +2028,7 @@ def new_account(self, name='', account_id=None, witness_type=None, network=None)
raise WalletError("A master private key of depth 0 is needed to create new accounts (depth: %d)" %
self.main_key.depth)
if "account'" not in self.key_path:
raise WalletError("Accounts are not supported for this wallet. Account not found in key path %s" %
raise WalletError("Accounts are not supported for this wallet. Account level not found in key path %s" %
self.key_path)
if network is None:
network = self.network.name
Expand Down Expand Up @@ -2147,9 +2147,11 @@ def key_for_path(self, path, level_offset=None, name=None, account_id=None, cosi
level_offset_key = level_offset
if level_offset and self.main_key and level_offset > 0:
level_offset_key = level_offset - self.main_key.depth

key_path = self.key_path
witness_type = witness_type if witness_type else self.witness_type
if ((not self.main_key or not self.main_key.is_private or self.main_key.depth != 0) and
self.witness_type != witness_type):
raise WalletError("This wallet has no private key, cannot use multiple witness types")
key_path = self.key_path
purpose = self.purpose
encoding = self.encoding
if witness_type != self.witness_type:
Expand Down
8 changes: 8 additions & 0 deletions tests/test_wallets.py
Original file line number Diff line number Diff line change
Expand Up @@ -2745,3 +2745,11 @@ def test_wallet_mixed_witness_types_send(self):
t.send()
self.assertIsNone(t.error)

def test_wallet_mixed_witness_no_private_key(self):
pub_master = ('zpub6qwhKTArtsgtCpVweSyJdVqmXTkmH3HXE2sc7RdhF5drnmcW2HXuFBqRPzVxhQkdaER3bSZeJbAbYxNGeShwUu'
'T49JfJqZLHNAsEUHD76AR')
address = 'bc1qgf8fzfj65lcr5vae0sh77akurh4zc9s9m4uspm'
w = Wallet.create('wallet_mix_no_private', keys=pub_master, db_uri=self.DATABASE_URI)
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')

0 comments on commit 2996676

Please sign in to comment.