def sign_message(self, message, keypath): self._check_unlocked() path = tools.parse_path(keypath) result = btc.sign_message(self.client, self.coin_name, path, message) return { "signature": base64.b64encode(result.signature).decode("utf-8") }
def display_address(self, keypath, p2sh_p2wpkh, bech32, redeem_script=None): self._check_unlocked() # redeem_script means p2sh/multisig if redeem_script: # Get multisig object required by Trezor's get_address multisig = parse_multisig(bytes.fromhex(redeem_script)) if not multisig[0]: raise BadArgumentError( "The redeem script provided is not a multisig. Only multisig scripts can be displayed." ) multisig = multisig[1] else: multisig = None # Script type if p2sh_p2wpkh: script_type = proto.InputScriptType.SPENDP2SHWITNESS elif bech32: script_type = proto.InputScriptType.SPENDWITNESS elif redeem_script: script_type = proto.InputScriptType.SPENDMULTISIG else: script_type = proto.InputScriptType.SPENDADDRESS # convert device fingerprint to 'm' if exists in path keypath = keypath.replace(self.get_master_fingerprint_hex(), "m") for path in keypath.split(","): if len(path.split("/")[0]) == 8: path = path.split("/", 1)[1] expanded_path = tools.parse_path(path) try: address = btc.get_address( self.client, self.coin_name, expanded_path, show_display=True, script_type=script_type, multisig=multisig, ) return {"address": address} except: pass raise BadArgumentError("No path supplied matched device keys")
def get_pubkey_at_path(self, path): self._check_unlocked() try: expanded_path = tools.parse_path(path) except ValueError as e: raise BadArgumentError(str(e)) output = btc.get_public_node(self.client, expanded_path, coin_name=self.coin_name) if self.is_testnet: result = {'xpub': xpub_main_2_test(output.xpub)} else: result = {'xpub': output.xpub} if self.expert: xpub_obj = ExtendedKey() xpub_obj.deserialize(output.xpub) result.update(xpub_obj.get_printable_dict()) return result
def display_address(self, keypath, p2sh_p2wpkh, bech32, redeem_script=None, descriptor=None): self._check_unlocked() # descriptor means multisig with xpubs if descriptor: pubkeys = [] xpub = ExtendedKey() for i in range(0, descriptor.multisig_N): xpub.deserialize(descriptor.base_key[i]) hd_node = proto.HDNodeType( depth=xpub.depth, fingerprint=int.from_bytes(xpub.parent_fingerprint, "big"), child_num=xpub.child_num, chain_code=xpub.chaincode, public_key=xpub.pubkey, ) pubkeys.append( proto.HDNodePathType( node=hd_node, address_n=tools.parse_path("m" + descriptor.path_suffix[i]), )) multisig = proto.MultisigRedeemScriptType( m=int(descriptor.multisig_M), signatures=[b""] * int(descriptor.multisig_N), pubkeys=pubkeys, ) # redeem_script means p2sh/multisig elif redeem_script: # Get multisig object required by Trezor's get_address multisig = parse_multisig(bytes.fromhex(redeem_script)) if not multisig[0]: raise BadArgumentError( "The redeem script provided is not a multisig. Only multisig scripts can be displayed." ) multisig = multisig[1] else: multisig = None # Script type if p2sh_p2wpkh: script_type = proto.InputScriptType.SPENDP2SHWITNESS elif bech32: script_type = proto.InputScriptType.SPENDWITNESS elif redeem_script: script_type = proto.InputScriptType.SPENDMULTISIG else: script_type = proto.InputScriptType.SPENDADDRESS # convert device fingerprint to 'm' if exists in path keypath = keypath.replace(self.get_master_fingerprint_hex(), "m") for path in keypath.split(","): if len(path.split("/")[0]) == 8: path = path.split("/", 1)[1] expanded_path = tools.parse_path(path) try: address = btc.get_address( self.client, self.coin_name, expanded_path, show_display=True, script_type=script_type, multisig=multisig, ) return {"address": address} except: pass raise BadArgumentError("No path supplied matched device keys")