From 627e752174f568813460598e84d5fa6795e30fce Mon Sep 17 00:00:00 2001 From: benk10 Date: Thu, 27 May 2021 16:53:51 +0300 Subject: [PATCH 1/2] Properly manage addresses caching using Core native descriptor wallets --- src/cryptoadvance/specter/wallet.py | 75 ++++++++++++++--------------- 1 file changed, 36 insertions(+), 39 deletions(-) diff --git a/src/cryptoadvance/specter/wallet.py b/src/cryptoadvance/specter/wallet.py index 6862fb2a7f..6f5b15a609 100644 --- a/src/cryptoadvance/specter/wallet.py +++ b/src/cryptoadvance/specter/wallet.py @@ -27,8 +27,6 @@ class Wallet: - # if the wallet is old we import 300 addresses - IMPORT_KEYPOOL = 300 # a gap of 20 addresses is what many wallets do (not used with descriptor wallets) GAP_LIMIT = 20 # minimal fee rate is slightly above 1 sat/vbyte @@ -1153,8 +1151,6 @@ def keypoolrefill(self, start, end=None, change=False): # Descriptor wallets were introduced in v0.21.0, but upgraded nodes may # still have legacy wallets. Use getwalletinfo to check the wallet type. # The "keypool" for descriptor wallets is automatically refilled - if self.use_descriptors and start > 0: - return if end is None: # end is ignored for descriptor wallets @@ -1185,43 +1181,44 @@ def keypoolrefill(self, start, end=None, change=False): ] self._addresses.add(addresses, check_rpc=False) - if not self.is_multisig: - if self.use_descriptors: - r = self.rpc.importdescriptors(args) - else: - r = self.rpc.importmulti(args, {"rescan": False}) - # bip67 requires sorted public keys for multisig addresses - else: - if self.use_descriptors: - self.rpc.importdescriptors(args) + if not self.use_descriptors or start > 0: + if not self.is_multisig: + if self.use_descriptors: + r = self.rpc.importdescriptors(args) + else: + r = self.rpc.importmulti(args, {"rescan": False}) + # bip67 requires sorted public keys for multisig addresses else: - # try if sortedmulti is supported - r = self.rpc.importmulti(args, {"rescan": False}) - # doesn't raise, but instead returns "success": False - if not r[0]["success"]: - # first import normal multi - # remove checksum - desc = desc.split("#")[0] - # switch to multi - desc = desc.replace("sortedmulti", "multi") - # add checksum - desc = AddChecksum(desc) - # update descriptor - args[0]["desc"] = desc + if self.use_descriptors: + self.rpc.importdescriptors(args) + else: + # try if sortedmulti is supported r = self.rpc.importmulti(args, {"rescan": False}) - # make a batch of single addresses to import - arg = args[0] - # remove range key - arg.pop("range") - batch = [] - for i in range(start, end): - sorted_desc = sort_descriptor(desc, index=i) - # create fresh object - obj = {} - obj.update(arg) - obj.update({"desc": sorted_desc}) - batch.append(obj) - r = self.rpc.importmulti(batch, {"rescan": False}) + # doesn't raise, but instead returns "success": False + if not r[0]["success"]: + # first import normal multi + # remove checksum + desc = desc.split("#")[0] + # switch to multi + desc = desc.replace("sortedmulti", "multi") + # add checksum + desc = AddChecksum(desc) + # update descriptor + args[0]["desc"] = desc + r = self.rpc.importmulti(args, {"rescan": False}) + # make a batch of single addresses to import + arg = args[0] + # remove range key + arg.pop("range") + batch = [] + for i in range(start, end): + sorted_desc = sort_descriptor(desc, index=i) + # create fresh object + obj = {} + obj.update(arg) + obj.update({"desc": sorted_desc}) + batch.append(obj) + r = self.rpc.importmulti(batch, {"rescan": False}) if change: self.change_keypool = end else: From 36b8a6ec80115dd2082ed75ccd8fd478782405b0 Mon Sep 17 00:00:00 2001 From: benk10 Date: Thu, 27 May 2021 17:30:22 +0300 Subject: [PATCH 2/2] Move comment --- src/cryptoadvance/specter/wallet.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/cryptoadvance/specter/wallet.py b/src/cryptoadvance/specter/wallet.py index 6f5b15a609..8877bd27af 100644 --- a/src/cryptoadvance/specter/wallet.py +++ b/src/cryptoadvance/specter/wallet.py @@ -1148,10 +1148,6 @@ def get_balance(self): return self.balance def keypoolrefill(self, start, end=None, change=False): - # Descriptor wallets were introduced in v0.21.0, but upgraded nodes may - # still have legacy wallets. Use getwalletinfo to check the wallet type. - # The "keypool" for descriptor wallets is automatically refilled - if end is None: # end is ignored for descriptor wallets end = start + self.GAP_LIMIT @@ -1181,6 +1177,9 @@ def keypoolrefill(self, start, end=None, change=False): ] self._addresses.add(addresses, check_rpc=False) + # Descriptor wallets were introduced in v0.21.0, but upgraded nodes may + # still have legacy wallets. Use getwalletinfo to check the wallet type. + # The "keypool" for descriptor wallets is automatically refilled if not self.use_descriptors or start > 0: if not self.is_multisig: if self.use_descriptors: