1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-17 11:58:13 +00:00

feat(python): Support UnlockPath in trezorlib.

This commit is contained in:
Andrew Kozlik 2022-06-03 11:35:09 +02:00 committed by Andrew Kozlik
parent 77be3653b4
commit a394ac5bb4
3 changed files with 53 additions and 3 deletions

View File

@ -0,0 +1 @@
Add UnlockPath message.

View File

@ -114,7 +114,21 @@ def get_public_node(
coin_name: Optional[str] = None,
script_type: messages.InputScriptType = messages.InputScriptType.SPENDADDRESS,
ignore_xpub_magic: bool = False,
preauthorized: bool = False,
unlock_path: Optional[List[int]] = None,
unlock_path_mac: Optional[bytes] = None,
) -> "MessageType":
if unlock_path:
res = client.call(
messages.UnlockPath(address_n=unlock_path, mac=unlock_path_mac)
)
if not isinstance(res, messages.UnlockedPathRequest):
raise exceptions.TrezorException("Unexpected message")
elif preauthorized:
res = client.call(messages.DoPreauthorized())
if not isinstance(res, messages.PreauthorizedRequest):
raise exceptions.TrezorException("Unexpected message")
return client.call(
messages.GetPublicKey(
address_n=n,
@ -141,7 +155,21 @@ def get_authenticated_address(
multisig: Optional[messages.MultisigRedeemScriptType] = None,
script_type: messages.InputScriptType = messages.InputScriptType.SPENDADDRESS,
ignore_xpub_magic: bool = False,
preauthorized: bool = False,
unlock_path: Optional[List[int]] = None,
unlock_path_mac: Optional[bytes] = None,
) -> "MessageType":
if unlock_path:
res = client.call(
messages.UnlockPath(address_n=unlock_path, mac=unlock_path_mac)
)
if not isinstance(res, messages.UnlockedPathRequest):
raise exceptions.TrezorException("Unexpected message")
elif preauthorized:
res = client.call(messages.DoPreauthorized())
if not isinstance(res, messages.PreauthorizedRequest):
raise exceptions.TrezorException("Unexpected message")
return client.call(
messages.GetAddress(
address_n=n,
@ -257,6 +285,8 @@ def sign_tx(
prev_txes: Optional["TxCacheType"] = None,
payment_reqs: Sequence[messages.TxAckPaymentRequest] = (),
preauthorized: bool = False,
unlock_path: Optional[List[int]] = None,
unlock_path_mac: Optional[bytes] = None,
**kwargs: Any,
) -> Tuple[Sequence[Optional[bytes]], bytes]:
"""Sign a Bitcoin-like transaction.
@ -294,7 +324,13 @@ def sign_tx(
if hasattr(signtx, name):
setattr(signtx, name, value)
if preauthorized:
if unlock_path:
res = client.call(
messages.UnlockPath(address_n=unlock_path, mac=unlock_path_mac)
)
if not isinstance(res, messages.UnlockedPathRequest):
raise exceptions.TrezorException("Unexpected message")
elif preauthorized:
res = client.call(messages.DoPreauthorized())
if not isinstance(res, messages.PreauthorizedRequest):
raise exceptions.TrezorException("Unexpected message")

View File

@ -19,8 +19,8 @@ import time
from typing import TYPE_CHECKING, Callable, Optional
from . import messages
from .exceptions import Cancelled
from .tools import expect, session
from .exceptions import Cancelled, TrezorException
from .tools import Address, expect, session
if TYPE_CHECKING:
from .client import TrezorClient
@ -220,6 +220,19 @@ def cancel_authorization(client: "TrezorClient") -> "MessageType":
return client.call(messages.CancelAuthorization())
@expect(messages.UnlockedPathRequest, field="mac", ret_type=bytes)
def unlock_path(client: "TrezorClient", n: "Address") -> "MessageType":
resp = client.call(messages.UnlockPath(address_n=n))
# Cancel the UnlockPath workflow now that we have the authentication code.
try:
client.call(messages.Cancel())
except Cancelled:
return resp
else:
raise TrezorException("Unexpected response in UnlockPath flow")
@session
@expect(messages.Success, field="message", ret_type=str)
def reboot_to_bootloader(client: "TrezorClient") -> "MessageType":