Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions hwilib/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
getdescriptors,
prompt_pin,
toggle_passphrase,
register_descriptor,
restore_device,
send_pin,
setup_device,
Expand Down Expand Up @@ -105,6 +106,9 @@ def send_pin_handler(args: argparse.Namespace, client: HardwareWalletClient) ->
def install_udev_rules_handler(args: argparse.Namespace) -> Dict[str, bool]:
return install_udev_rules('udev', args.location)

def register_descriptor_handler(args: argparse.Namespace, client: HardwareWalletClient) -> Dict[str, str]:
return register_descriptor(client, args.name, args.descriptor)

class HWIHelpFormatter(argparse.ArgumentDefaultsHelpFormatter, argparse.RawDescriptionHelpFormatter):
pass

Expand Down Expand Up @@ -225,6 +229,11 @@ def get_parser() -> HWIArgumentParser:
sendpin_parser.add_argument('pin', help='The numeric positions of the PIN')
sendpin_parser.set_defaults(func=send_pin_handler)

register_descriptor_parser = subparsers.add_parser("registerdescriptor", help="Register a descriptor with the device")
register_descriptor_parser.add_argument("name", help="A human readable name for this descriptor. May be used by the device to identify the descriptor being used.")
register_descriptor_parser.add_argument("descriptor", help="The descriptor to register.")
register_descriptor_parser.set_defaults(func=register_descriptor_handler)

if sys.platform.startswith("linux"):
udevrules_parser = subparsers.add_parser('installudevrules', help='Install and load the udev rule files for the hardware wallet devices')
udevrules_parser.add_argument('--location', help='The path where the udev rules files will be copied', default='/etc/udev/rules.d/')
Expand Down
23 changes: 15 additions & 8 deletions hwilib/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
get_bip44_purpose,
get_bip44_chain,
H_,
HARDENED_FLAG,
is_hardened,
KeyOriginInfo,
parse_path,
Expand Down Expand Up @@ -328,18 +327,13 @@ def getdescriptor(
origin = KeyOriginInfo(master_fpr, parsed_path[:i])
path_base = origin.get_derivation_path()

path_suffix = ""
for p in parsed_path[i:]:
hardened = is_hardened(p)
p &= ~HARDENED_FLAG
path_suffix += "/{}{}".format(p, "h" if hardened else "")
path_suffix += "/*"
path_suffix = [[p] for p in parsed_path[i:]]

# Get the key at the base
if client.xpub_cache.get(path_base) is None:
client.xpub_cache[path_base] = client.get_pubkey_at_path(path_base).to_string()

pubkey = PubkeyProvider(origin, client.xpub_cache.get(path_base, ""), path_suffix)
pubkey = PubkeyProvider(origin, client.xpub_cache.get(path_base, ""), path_suffix, 0, True)
if addr_type is AddressType.LEGACY:
return PKHDescriptor(pubkey)
elif addr_type is AddressType.SH_WIT:
Expand Down Expand Up @@ -590,3 +584,16 @@ def install_udev_rules(source: str, location: str) -> Dict[str, bool]:
from .udevinstaller import UDevInstaller
return {"success": UDevInstaller.install(source, location)}
raise NotImplementedError("udev rules are not needed on your platform")


def register_descriptor(client: HardwareWalletClient, name: str, descriptor: str) -> Dict[str, str]:
"""
Register a descriptor with the device.

Returns any information that will be needed in the future to inform the
device about the descriptor to sign with. Some devices do not require any
information and will return nothing.

:return: A dictionary with the ``registration`` key containing a string
"""
return {"registration": client.register_descriptor(name, parse_descriptor(descriptor)).serialize()}
Loading
Loading