From 299667689b3b1f6a7de393657d8cf64449a71081 Mon Sep 17 00:00:00 2001 From: Lennart Date: Tue, 31 Oct 2023 09:07:37 +0100 Subject: [PATCH] [REF] Raise warning when trying to use mixed witness type in watch only wallets --- bitcoinlib/wallets.py | 8 +++++--- tests/test_wallets.py | 8 ++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/bitcoinlib/wallets.py b/bitcoinlib/wallets.py index 3484a897..a9f55261 100644 --- a/bitcoinlib/wallets.py +++ b/bitcoinlib/wallets.py @@ -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 @@ -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: diff --git a/tests/test_wallets.py b/tests/test_wallets.py index 9497925e..005a20ca 100644 --- a/tests/test_wallets.py +++ b/tests/test_wallets.py @@ -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')