def __init__(self, seed_phrase, passphrase, derivation, network, wordlist): self.seed_phrase = seed_phrase self.passphrase = passphrase self.derivation = derivation self.wordlist = wordlist self.parts = [] self.part_num_sent = 0 self.sent_complete = False if self.wordlist == None: raise Exception('Wordlist Required') version = bip32.detect_version(self.derivation, default="xpub", network=NETWORKS[network]) print(self.derivation) self.seed = Seed(mnemonic=self.seed_phrase, passphrase=self.passphrase, wordlist=self.wordlist) self.root = bip32.HDKey.from_seed(self.seed.seed, version=NETWORKS[network]["xprv"]) self.fingerprint = self.root.child(0).fingerprint self.xprv = self.root.derive(self.derivation) self.xpub = self.xprv.to_public() self.xpub_base58 = self.xpub.to_string(version=version) self.network = network self.xpubstring = "[%s%s]%s" % (hexlify( self.fingerprint).decode('utf-8'), self.derivation[1:], self.xpub_base58) if isinstance(self, XPubQR): self.__createParts()
def extract_xpub( self, derivation=None, device_type=None, path=None, fingerprint=None, passphrase="", chain="", ): with self._get_client( device_type=device_type, fingerprint=fingerprint, path=path, passphrase=passphrase, chain=chain, ) as client: # Client will be configured for testnet if our Specter instance is # currently connected to testnet. This will prevent us from # getting mainnet xpubs unless we set is_testnet here: if not chain: try: client.is_testnet = derivation.split("/")[2].startswith( "1") except: client.is_testnet = False else: client.is_testnet = is_testnet(chain) network = networks.NETWORKS["test" if client. is_testnet else "main"] master_fpr = client.get_master_fingerprint_hex() try: xpub = client.get_pubkey_at_path(derivation)["xpub"] slip132_prefix = bip32.detect_version(derivation, default="xpub", network=network) xpub = convert_xpub_prefix(xpub, slip132_prefix) return "[{}/{}]{}\n".format(master_fpr, derivation.split("m/")[1], xpub) except Exception: logger.warn( "Failed to import Nested Segwit singlesig mainnet key.")
def extract_xpub( self, derivation=None, device_type=None, path=None, fingerprint=None, passphrase="", chain="", ): with self._get_client( device_type=device_type, fingerprint=fingerprint, path=path, passphrase=passphrase, chain=chain, ) as client: # Client will be configured for testnet if our Specter instance is # currently connected to testnet. This will prevent us from # getting mainnet xpubs unless we set is_testnet here: try: client.chain = (Chain.TEST if derivation.split("/")[2].startswith("1") else Chain.MAIN) except: client.chain = Chain.MAIN network = networks.get_network("main" if client.chain == Chain.MAIN else "test") master_fpr = client.get_master_fingerprint().hex() try: xpub = client.get_pubkey_at_path(derivation).to_string() slip132_prefix = bip32.detect_version(derivation, default="xpub", network=network) xpub = convert_xpub_prefix(xpub, slip132_prefix) return "[{}/{}]{}\n".format(master_fpr, derivation.split("m/")[1], xpub) except Exception as e: logger.warning( f"Failed to import Nested Segwit singlesig mainnet key. Error: {e}" )
def add_hot_wallet_keys( self, mnemonic, passphrase, paths, file_password, wallet_manager, testnet, keys_range=[0, 1000], keys_purposes=[], ): if type(keys_range[0]) == str: keys_range[0] = int(keys_range[0]) if type(keys_range[1]) == str: keys_range[1] = int(keys_range[1]) seed = bip39.mnemonic_to_seed(mnemonic, passphrase) root = bip32.HDKey.from_seed(seed) network = networks.NETWORKS["test" if testnet else "main"] root.version = network["xprv"] xprv = root.to_base58() # Load the wallet if not loaded self._load_wallet(wallet_manager) rpc = wallet_manager.rpc.wallet( os.path.join(wallet_manager.rpc_path + "_hotstorage", self.alias)) if file_password: rpc.walletpassphrase(file_password, 60) rpc.importmulti( [{ "desc": AddChecksum("sh(wpkh({}{}/0/*))".format( xprv, path.rstrip("/").replace("m", ""))), "range": keys_range, "timestamp": "now", } for path in paths] + [{ "desc": AddChecksum("sh(wpkh({}{}/1/*))".format( xprv, path.rstrip("/").replace("m", ""))), "range": keys_range, "timestamp": "now", } for path in paths], {"rescan": False}, ) xpubs = [root.derive(path).to_public().to_base58() for path in paths] # root fingerprint is fingerprint field of the first child master_fpr = root.child(0).fingerprint.hex() keys = [] for i in range(len(paths)): try: path = paths[i] xpub = xpubs[i] # detect slip132 version for xpubs slip132_prefix = bip32.detect_version(path, default="xpub", network=network) xpub = "[{}{}]{}\n".format( master_fpr, path.replace("m", ""), convert_xpub_prefix(xpub, slip132_prefix), ) keys.append( Key.parse_xpub( xpub, keys_purposes[i] if len(keys_purposes) > i else "")) except Exception: # TODO: This should never occur, but just in case, # we must make sure to catch it properly so it # doesn't crash the app no matter what. raise Exception("Failed to parse this xpub:\n" + "\n".join(xpub)) self.add_keys(keys)