2018-06-21 14:28:34 +00:00
# This file is part of the Trezor project.
#
2021-11-26 14:50:43 +00:00
# Copyright (C) 2012-2022 SatoshiLabs and contributors
2018-06-21 14:28:34 +00:00
#
# This library is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License version 3
# as published by the Free Software Foundation.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the License along with this library.
# If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
2021-11-26 14:50:43 +00:00
2021-07-29 07:19:05 +00:00
from decimal import Decimal
feat(python): add full type information
WIP - typing the trezorctl apps
typing functions trezorlib/cli
addressing most of mypy issue for trezorlib apps and _internal folder
fixing broken device tests by changing asserts in debuglink.py
addressing most of mypy issues in trezorlib/cli folder
adding types to some untyped functions, mypy section in setup.cfg
typing what can be typed, some mypy fixes, resolving circular import issues
importing type objects in "if TYPE_CHECKING:" branch
fixing CI by removing assert in emulator, better ignore comments
CI assert fix, style fixes, new config options
fixup! CI assert fix, style fixes, new config options
type fixes after rebasing on master
fixing python3.6 and 3.7 unittests by importing Literal from typing_extensions
couple mypy and style fixes
fixes and improvements from code review
silencing all but one mypy issues
trial of typing the tools.expect function
fixup! trial of typing the tools.expect function
@expect and @session decorators correctly type-checked
Optional args in CLI where relevant, not using general list/tuple/dict where possible
python/Makefile commands, adding them into CI, ignoring last mypy issue
documenting overload for expect decorator, two mypy fixes coming from that
black style fix
improved typing of decorators, pyright config file
addressing or ignoring pyright errors, replacing mypy in CI by pyright
fixing incomplete assert causing device tests to fail
pyright issue that showed in CI but not locally, printing pyright version in CI
fixup! pyright issue that showed in CI but not locally, printing pyright version in CI
unifying type:ignore statements for pyright usage
resolving PIL.Image issues, pyrightconfig not excluding anything
replacing couple asserts with TypeGuard on safe_issubclass
better error handling of usb1 import for webusb
better error handling of hid import
small typing details found out by strict pyright mode
improvements from code review
chore(python): changing List to Sequence for protobuf messages
small code changes to reflect the protobuf change to Sequence
importing TypedDict from typing_extensions to support 3.6 and 3.7
simplify _format_access_list function
fixup! simplify _format_access_list function
typing tools folder
typing helper-scripts folder
some click typing
enforcing all functions to have typed arguments
reverting the changed argument name in tools
replacing TransportType with Transport
making PinMatrixRequest.type protobuf attribute required
reverting the protobuf change, making argument into get_pin Optional
small fixes in asserts
solving the session decorator type issues
fixup! solving the session decorator type issues
improvements from code review
fixing new pyright errors introduced after version increase
changing -> Iterable to -> Sequence in enumerate_devices, change in wait_for_devices
style change in debuglink.py
chore(python): adding type annotation to Sequences in messages.py
better "self and cls" types on Transport
fixup! better "self and cls" types on Transport
fixing some easy things from strict pyright run
2021-11-03 22:12:53 +00:00
from typing import TYPE_CHECKING , List , Tuple , Union
2018-04-04 01:50:22 +00:00
2020-03-24 13:18:08 +00:00
from . import exceptions , messages
from . tools import expect
2018-04-19 21:10:27 +00:00
feat(python): add full type information
WIP - typing the trezorctl apps
typing functions trezorlib/cli
addressing most of mypy issue for trezorlib apps and _internal folder
fixing broken device tests by changing asserts in debuglink.py
addressing most of mypy issues in trezorlib/cli folder
adding types to some untyped functions, mypy section in setup.cfg
typing what can be typed, some mypy fixes, resolving circular import issues
importing type objects in "if TYPE_CHECKING:" branch
fixing CI by removing assert in emulator, better ignore comments
CI assert fix, style fixes, new config options
fixup! CI assert fix, style fixes, new config options
type fixes after rebasing on master
fixing python3.6 and 3.7 unittests by importing Literal from typing_extensions
couple mypy and style fixes
fixes and improvements from code review
silencing all but one mypy issues
trial of typing the tools.expect function
fixup! trial of typing the tools.expect function
@expect and @session decorators correctly type-checked
Optional args in CLI where relevant, not using general list/tuple/dict where possible
python/Makefile commands, adding them into CI, ignoring last mypy issue
documenting overload for expect decorator, two mypy fixes coming from that
black style fix
improved typing of decorators, pyright config file
addressing or ignoring pyright errors, replacing mypy in CI by pyright
fixing incomplete assert causing device tests to fail
pyright issue that showed in CI but not locally, printing pyright version in CI
fixup! pyright issue that showed in CI but not locally, printing pyright version in CI
unifying type:ignore statements for pyright usage
resolving PIL.Image issues, pyrightconfig not excluding anything
replacing couple asserts with TypeGuard on safe_issubclass
better error handling of usb1 import for webusb
better error handling of hid import
small typing details found out by strict pyright mode
improvements from code review
chore(python): changing List to Sequence for protobuf messages
small code changes to reflect the protobuf change to Sequence
importing TypedDict from typing_extensions to support 3.6 and 3.7
simplify _format_access_list function
fixup! simplify _format_access_list function
typing tools folder
typing helper-scripts folder
some click typing
enforcing all functions to have typed arguments
reverting the changed argument name in tools
replacing TransportType with Transport
making PinMatrixRequest.type protobuf attribute required
reverting the protobuf change, making argument into get_pin Optional
small fixes in asserts
solving the session decorator type issues
fixup! solving the session decorator type issues
improvements from code review
fixing new pyright errors introduced after version increase
changing -> Iterable to -> Sequence in enumerate_devices, change in wait_for_devices
style change in debuglink.py
chore(python): adding type annotation to Sequences in messages.py
better "self and cls" types on Transport
fixup! better "self and cls" types on Transport
fixing some easy things from strict pyright run
2021-11-03 22:12:53 +00:00
if TYPE_CHECKING :
from . protobuf import MessageType
from . client import TrezorClient
from . tools import Address
StellarMessageType = Union [
messages . StellarAccountMergeOp ,
messages . StellarAllowTrustOp ,
messages . StellarBumpSequenceOp ,
messages . StellarChangeTrustOp ,
messages . StellarCreateAccountOp ,
messages . StellarCreatePassiveSellOfferOp ,
messages . StellarManageDataOp ,
messages . StellarManageBuyOfferOp ,
messages . StellarManageSellOfferOp ,
messages . StellarPathPaymentStrictReceiveOp ,
messages . StellarPathPaymentStrictSendOp ,
messages . StellarPaymentOp ,
messages . StellarSetOptionsOp ,
]
2021-07-29 07:19:05 +00:00
try :
from stellar_sdk import (
AccountMerge ,
AllowTrust ,
Asset ,
BumpSequence ,
ChangeTrust ,
CreateAccount ,
CreatePassiveSellOffer ,
HashMemo ,
IdMemo ,
ManageData ,
ManageSellOffer ,
2021-08-09 11:54:30 +00:00
NoneMemo ,
2021-07-29 07:19:05 +00:00
Operation ,
PathPaymentStrictReceive ,
2021-10-07 14:06:08 +00:00
PathPaymentStrictSend ,
2021-07-29 07:19:05 +00:00
Payment ,
ReturnHashMemo ,
SetOptions ,
TextMemo ,
TransactionEnvelope ,
TrustLineEntryFlag ,
Price ,
Network ,
2021-10-07 03:48:45 +00:00
ManageBuyOffer ,
2021-10-08 00:06:16 +00:00
MuxedAccount ,
2021-07-29 07:19:05 +00:00
)
from stellar_sdk . xdr . signer_key_type import SignerKeyType
HAVE_STELLAR_SDK = True
DEFAULT_NETWORK_PASSPHRASE = Network . PUBLIC_NETWORK_PASSPHRASE
except ImportError :
HAVE_STELLAR_SDK = False
DEFAULT_NETWORK_PASSPHRASE = " Public Global Stellar Network ; September 2015 "
2018-05-28 13:44:43 +00:00
DEFAULT_BIP32_PATH = " m/44h/148h/0h "
2018-04-04 01:50:22 +00:00
feat(python): add full type information
WIP - typing the trezorctl apps
typing functions trezorlib/cli
addressing most of mypy issue for trezorlib apps and _internal folder
fixing broken device tests by changing asserts in debuglink.py
addressing most of mypy issues in trezorlib/cli folder
adding types to some untyped functions, mypy section in setup.cfg
typing what can be typed, some mypy fixes, resolving circular import issues
importing type objects in "if TYPE_CHECKING:" branch
fixing CI by removing assert in emulator, better ignore comments
CI assert fix, style fixes, new config options
fixup! CI assert fix, style fixes, new config options
type fixes after rebasing on master
fixing python3.6 and 3.7 unittests by importing Literal from typing_extensions
couple mypy and style fixes
fixes and improvements from code review
silencing all but one mypy issues
trial of typing the tools.expect function
fixup! trial of typing the tools.expect function
@expect and @session decorators correctly type-checked
Optional args in CLI where relevant, not using general list/tuple/dict where possible
python/Makefile commands, adding them into CI, ignoring last mypy issue
documenting overload for expect decorator, two mypy fixes coming from that
black style fix
improved typing of decorators, pyright config file
addressing or ignoring pyright errors, replacing mypy in CI by pyright
fixing incomplete assert causing device tests to fail
pyright issue that showed in CI but not locally, printing pyright version in CI
fixup! pyright issue that showed in CI but not locally, printing pyright version in CI
unifying type:ignore statements for pyright usage
resolving PIL.Image issues, pyrightconfig not excluding anything
replacing couple asserts with TypeGuard on safe_issubclass
better error handling of usb1 import for webusb
better error handling of hid import
small typing details found out by strict pyright mode
improvements from code review
chore(python): changing List to Sequence for protobuf messages
small code changes to reflect the protobuf change to Sequence
importing TypedDict from typing_extensions to support 3.6 and 3.7
simplify _format_access_list function
fixup! simplify _format_access_list function
typing tools folder
typing helper-scripts folder
some click typing
enforcing all functions to have typed arguments
reverting the changed argument name in tools
replacing TransportType with Transport
making PinMatrixRequest.type protobuf attribute required
reverting the protobuf change, making argument into get_pin Optional
small fixes in asserts
solving the session decorator type issues
fixup! solving the session decorator type issues
improvements from code review
fixing new pyright errors introduced after version increase
changing -> Iterable to -> Sequence in enumerate_devices, change in wait_for_devices
style change in debuglink.py
chore(python): adding type annotation to Sequences in messages.py
better "self and cls" types on Transport
fixup! better "self and cls" types on Transport
fixing some easy things from strict pyright run
2021-11-03 22:12:53 +00:00
def from_envelope (
envelope : " TransactionEnvelope " ,
) - > Tuple [ messages . StellarSignTx , List [ " StellarMessageType " ] ] :
2021-07-29 07:19:05 +00:00
""" Parses transaction envelope into a map with the following keys:
2020-09-25 11:59:16 +00:00
tx - a StellarSignTx describing the transaction header
operations - an array of protobuf message objects for each operation
2018-04-04 01:50:22 +00:00
"""
2021-07-29 07:19:05 +00:00
if not HAVE_STELLAR_SDK :
raise RuntimeError ( " Stellar SDK not available " )
2018-04-04 01:50:22 +00:00
2021-08-09 11:54:30 +00:00
parsed_tx = envelope . transaction
if parsed_tx . time_bounds is None :
raise ValueError ( " Timebounds are mandatory " )
2021-07-29 07:19:05 +00:00
2021-08-09 11:54:30 +00:00
memo_type = messages . StellarMemoType . NONE
memo_text = None
memo_id = None
memo_hash = None
if isinstance ( parsed_tx . memo , NoneMemo ) :
pass
elif isinstance ( parsed_tx . memo , TextMemo ) :
2021-07-29 07:19:05 +00:00
# memo_text is specified as UTF-8 string, but returned as bytes from the XDR parser
2021-08-09 11:54:30 +00:00
memo_type = messages . StellarMemoType . TEXT
memo_text = parsed_tx . memo . memo_text . decode ( " utf-8 " )
elif isinstance ( parsed_tx . memo , IdMemo ) :
memo_type = messages . StellarMemoType . ID
memo_id = parsed_tx . memo . memo_id
elif isinstance ( parsed_tx . memo , HashMemo ) :
memo_type = messages . StellarMemoType . HASH
memo_hash = parsed_tx . memo . memo_hash
elif isinstance ( parsed_tx . memo , ReturnHashMemo ) :
memo_type = messages . StellarMemoType . RETURN
memo_hash = parsed_tx . memo . memo_return
2021-07-29 07:19:05 +00:00
else :
2021-08-09 11:54:30 +00:00
raise ValueError ( " Unsupported memo type " )
2021-10-08 00:06:16 +00:00
_raise_if_account_muxed_id_exists ( parsed_tx . source )
2021-08-09 11:54:30 +00:00
tx = messages . StellarSignTx (
source_account = parsed_tx . source . account_id ,
fee = parsed_tx . fee ,
sequence_number = parsed_tx . sequence ,
timebounds_start = parsed_tx . time_bounds . min_time ,
timebounds_end = parsed_tx . time_bounds . max_time ,
memo_type = memo_type ,
memo_text = memo_text ,
memo_id = memo_id ,
memo_hash = memo_hash ,
num_operations = len ( parsed_tx . operations ) ,
network_passphrase = envelope . network_passphrase ,
)
2021-07-29 07:19:05 +00:00
operations = [ _read_operation ( op ) for op in parsed_tx . operations ]
2018-04-20 16:34:26 +00:00
return tx , operations
2018-04-04 01:50:22 +00:00
2018-04-28 18:59:08 +00:00
feat(python): add full type information
WIP - typing the trezorctl apps
typing functions trezorlib/cli
addressing most of mypy issue for trezorlib apps and _internal folder
fixing broken device tests by changing asserts in debuglink.py
addressing most of mypy issues in trezorlib/cli folder
adding types to some untyped functions, mypy section in setup.cfg
typing what can be typed, some mypy fixes, resolving circular import issues
importing type objects in "if TYPE_CHECKING:" branch
fixing CI by removing assert in emulator, better ignore comments
CI assert fix, style fixes, new config options
fixup! CI assert fix, style fixes, new config options
type fixes after rebasing on master
fixing python3.6 and 3.7 unittests by importing Literal from typing_extensions
couple mypy and style fixes
fixes and improvements from code review
silencing all but one mypy issues
trial of typing the tools.expect function
fixup! trial of typing the tools.expect function
@expect and @session decorators correctly type-checked
Optional args in CLI where relevant, not using general list/tuple/dict where possible
python/Makefile commands, adding them into CI, ignoring last mypy issue
documenting overload for expect decorator, two mypy fixes coming from that
black style fix
improved typing of decorators, pyright config file
addressing or ignoring pyright errors, replacing mypy in CI by pyright
fixing incomplete assert causing device tests to fail
pyright issue that showed in CI but not locally, printing pyright version in CI
fixup! pyright issue that showed in CI but not locally, printing pyright version in CI
unifying type:ignore statements for pyright usage
resolving PIL.Image issues, pyrightconfig not excluding anything
replacing couple asserts with TypeGuard on safe_issubclass
better error handling of usb1 import for webusb
better error handling of hid import
small typing details found out by strict pyright mode
improvements from code review
chore(python): changing List to Sequence for protobuf messages
small code changes to reflect the protobuf change to Sequence
importing TypedDict from typing_extensions to support 3.6 and 3.7
simplify _format_access_list function
fixup! simplify _format_access_list function
typing tools folder
typing helper-scripts folder
some click typing
enforcing all functions to have typed arguments
reverting the changed argument name in tools
replacing TransportType with Transport
making PinMatrixRequest.type protobuf attribute required
reverting the protobuf change, making argument into get_pin Optional
small fixes in asserts
solving the session decorator type issues
fixup! solving the session decorator type issues
improvements from code review
fixing new pyright errors introduced after version increase
changing -> Iterable to -> Sequence in enumerate_devices, change in wait_for_devices
style change in debuglink.py
chore(python): adding type annotation to Sequences in messages.py
better "self and cls" types on Transport
fixup! better "self and cls" types on Transport
fixing some easy things from strict pyright run
2021-11-03 22:12:53 +00:00
def _read_operation ( op : " Operation " ) - > " StellarMessageType " :
2021-07-29 07:19:05 +00:00
# TODO: Let's add muxed account support later.
if op . source :
2021-10-08 00:06:16 +00:00
_raise_if_account_muxed_id_exists ( op . source )
2021-07-29 07:19:05 +00:00
source_account = op . source . account_id
else :
source_account = None
if isinstance ( op , CreateAccount ) :
2018-06-28 02:26:21 +00:00
return messages . StellarCreateAccountOp (
2018-04-19 21:10:27 +00:00
source_account = source_account ,
2021-07-29 07:19:05 +00:00
new_account = op . destination ,
starting_balance = _read_amount ( op . starting_balance ) ,
2018-04-19 21:10:27 +00:00
)
2021-07-29 07:19:05 +00:00
if isinstance ( op , Payment ) :
2021-10-08 00:06:16 +00:00
_raise_if_account_muxed_id_exists ( op . destination )
2018-06-28 02:26:21 +00:00
return messages . StellarPaymentOp (
2018-04-19 21:10:27 +00:00
source_account = source_account ,
2021-07-29 07:19:05 +00:00
destination_account = op . destination . account_id ,
asset = _read_asset ( op . asset ) ,
amount = _read_amount ( op . amount ) ,
2018-04-19 21:10:27 +00:00
)
2021-07-29 07:19:05 +00:00
if isinstance ( op , PathPaymentStrictReceive ) :
2021-10-08 00:06:16 +00:00
_raise_if_account_muxed_id_exists ( op . destination )
feat(python): add full type information
WIP - typing the trezorctl apps
typing functions trezorlib/cli
addressing most of mypy issue for trezorlib apps and _internal folder
fixing broken device tests by changing asserts in debuglink.py
addressing most of mypy issues in trezorlib/cli folder
adding types to some untyped functions, mypy section in setup.cfg
typing what can be typed, some mypy fixes, resolving circular import issues
importing type objects in "if TYPE_CHECKING:" branch
fixing CI by removing assert in emulator, better ignore comments
CI assert fix, style fixes, new config options
fixup! CI assert fix, style fixes, new config options
type fixes after rebasing on master
fixing python3.6 and 3.7 unittests by importing Literal from typing_extensions
couple mypy and style fixes
fixes and improvements from code review
silencing all but one mypy issues
trial of typing the tools.expect function
fixup! trial of typing the tools.expect function
@expect and @session decorators correctly type-checked
Optional args in CLI where relevant, not using general list/tuple/dict where possible
python/Makefile commands, adding them into CI, ignoring last mypy issue
documenting overload for expect decorator, two mypy fixes coming from that
black style fix
improved typing of decorators, pyright config file
addressing or ignoring pyright errors, replacing mypy in CI by pyright
fixing incomplete assert causing device tests to fail
pyright issue that showed in CI but not locally, printing pyright version in CI
fixup! pyright issue that showed in CI but not locally, printing pyright version in CI
unifying type:ignore statements for pyright usage
resolving PIL.Image issues, pyrightconfig not excluding anything
replacing couple asserts with TypeGuard on safe_issubclass
better error handling of usb1 import for webusb
better error handling of hid import
small typing details found out by strict pyright mode
improvements from code review
chore(python): changing List to Sequence for protobuf messages
small code changes to reflect the protobuf change to Sequence
importing TypedDict from typing_extensions to support 3.6 and 3.7
simplify _format_access_list function
fixup! simplify _format_access_list function
typing tools folder
typing helper-scripts folder
some click typing
enforcing all functions to have typed arguments
reverting the changed argument name in tools
replacing TransportType with Transport
making PinMatrixRequest.type protobuf attribute required
reverting the protobuf change, making argument into get_pin Optional
small fixes in asserts
solving the session decorator type issues
fixup! solving the session decorator type issues
improvements from code review
fixing new pyright errors introduced after version increase
changing -> Iterable to -> Sequence in enumerate_devices, change in wait_for_devices
style change in debuglink.py
chore(python): adding type annotation to Sequences in messages.py
better "self and cls" types on Transport
fixup! better "self and cls" types on Transport
fixing some easy things from strict pyright run
2021-11-03 22:12:53 +00:00
return messages . StellarPathPaymentStrictReceiveOp (
2018-04-19 21:10:27 +00:00
source_account = source_account ,
2021-07-29 07:19:05 +00:00
send_asset = _read_asset ( op . send_asset ) ,
send_max = _read_amount ( op . send_max ) ,
destination_account = op . destination . account_id ,
destination_asset = _read_asset ( op . dest_asset ) ,
destination_amount = _read_amount ( op . dest_amount ) ,
paths = [ _read_asset ( asset ) for asset in op . path ] ,
2018-04-19 21:10:27 +00:00
)
2021-07-29 07:19:05 +00:00
if isinstance ( op , ManageSellOffer ) :
price = _read_price ( op . price )
2021-10-07 01:58:52 +00:00
return messages . StellarManageSellOfferOp (
2018-04-19 21:10:27 +00:00
source_account = source_account ,
2021-07-29 07:19:05 +00:00
selling_asset = _read_asset ( op . selling ) ,
buying_asset = _read_asset ( op . buying ) ,
amount = _read_amount ( op . amount ) ,
price_n = price . n ,
price_d = price . d ,
offer_id = op . offer_id ,
2018-04-19 21:10:27 +00:00
)
2021-07-29 07:19:05 +00:00
if isinstance ( op , CreatePassiveSellOffer ) :
price = _read_price ( op . price )
2021-10-07 23:33:09 +00:00
return messages . StellarCreatePassiveSellOfferOp (
2018-04-19 21:10:27 +00:00
source_account = source_account ,
2021-07-29 07:19:05 +00:00
selling_asset = _read_asset ( op . selling ) ,
buying_asset = _read_asset ( op . buying ) ,
amount = _read_amount ( op . amount ) ,
price_n = price . n ,
price_d = price . d ,
2018-04-19 21:10:27 +00:00
)
2021-07-29 07:19:05 +00:00
if isinstance ( op , SetOptions ) :
operation = messages . StellarSetOptionsOp (
source_account = source_account ,
inflation_destination_account = op . inflation_dest ,
clear_flags = op . clear_flags ,
set_flags = op . set_flags ,
master_weight = op . master_weight ,
low_threshold = op . low_threshold ,
medium_threshold = op . med_threshold ,
high_threshold = op . high_threshold ,
home_domain = op . home_domain ,
)
if op . signer :
signer_type = op . signer . signer_key . signer_key . type
if signer_type == SignerKeyType . SIGNER_KEY_TYPE_ED25519 :
signer_key = op . signer . signer_key . signer_key . ed25519 . uint256
elif signer_type == SignerKeyType . SIGNER_KEY_TYPE_HASH_X :
signer_key = op . signer . signer_key . signer_key . hash_x . uint256
elif signer_type == SignerKeyType . SIGNER_KEY_TYPE_PRE_AUTH_TX :
signer_key = op . signer . signer_key . signer_key . pre_auth_tx . uint256
else :
raise ValueError ( " Unsupported signer key type " )
2021-09-08 12:50:29 +00:00
operation . signer_type = messages . StellarSignerType ( signer_type . value )
2021-07-29 07:19:05 +00:00
operation . signer_key = signer_key
operation . signer_weight = op . signer . weight
return operation
if isinstance ( op , ChangeTrust ) :
2018-06-28 02:26:21 +00:00
return messages . StellarChangeTrustOp (
2018-04-19 21:10:27 +00:00
source_account = source_account ,
2021-07-29 07:19:05 +00:00
asset = _read_asset ( op . asset ) ,
limit = _read_amount ( op . limit ) ,
2018-04-19 21:10:27 +00:00
)
2021-07-29 07:19:05 +00:00
if isinstance ( op , AllowTrust ) :
2021-08-10 00:44:20 +00:00
if op . authorize not in (
TrustLineEntryFlag . UNAUTHORIZED_FLAG ,
TrustLineEntryFlag . AUTHORIZED_FLAG ,
) :
raise ValueError ( " Unsupported trust line flag " )
2021-07-29 07:19:05 +00:00
asset_type = (
2021-09-08 12:50:29 +00:00
messages . StellarAssetType . ALPHANUM4
if len ( op . asset_code ) < = 4
else messages . StellarAssetType . ALPHANUM12
2021-07-29 07:19:05 +00:00
)
return messages . StellarAllowTrustOp (
2018-04-19 21:10:27 +00:00
source_account = source_account ,
2021-07-29 07:19:05 +00:00
trusted_account = op . trustor ,
asset_type = asset_type ,
asset_code = op . asset_code ,
2021-09-08 09:18:35 +00:00
is_authorized = bool ( op . authorize . value ) ,
2018-04-19 21:10:27 +00:00
)
2021-07-29 07:19:05 +00:00
if isinstance ( op , AccountMerge ) :
2021-10-08 00:06:16 +00:00
_raise_if_account_muxed_id_exists ( op . destination )
2018-06-28 02:26:21 +00:00
return messages . StellarAccountMergeOp (
2018-04-19 21:10:27 +00:00
source_account = source_account ,
2021-07-29 07:19:05 +00:00
destination_account = op . destination . account_id ,
2018-04-19 21:10:27 +00:00
)
# Inflation is not implemented since anyone can submit this operation to the network
2021-07-29 07:19:05 +00:00
if isinstance ( op , ManageData ) :
return messages . StellarManageDataOp (
source_account = source_account ,
key = op . data_name ,
value = op . data_value ,
2018-04-19 21:10:27 +00:00
)
2021-07-29 07:19:05 +00:00
if isinstance ( op , BumpSequence ) :
2018-06-28 02:26:21 +00:00
return messages . StellarBumpSequenceOp (
2021-07-29 07:19:05 +00:00
source_account = source_account , bump_to = op . bump_to
2018-04-19 21:10:27 +00:00
)
2021-10-07 03:48:45 +00:00
if isinstance ( op , ManageBuyOffer ) :
price = _read_price ( op . price )
return messages . StellarManageBuyOfferOp (
source_account = source_account ,
selling_asset = _read_asset ( op . selling ) ,
buying_asset = _read_asset ( op . buying ) ,
amount = _read_amount ( op . amount ) ,
price_n = price . n ,
price_d = price . d ,
offer_id = op . offer_id ,
)
2021-10-07 14:06:08 +00:00
if isinstance ( op , PathPaymentStrictSend ) :
2021-10-08 00:06:16 +00:00
_raise_if_account_muxed_id_exists ( op . destination )
feat(python): add full type information
WIP - typing the trezorctl apps
typing functions trezorlib/cli
addressing most of mypy issue for trezorlib apps and _internal folder
fixing broken device tests by changing asserts in debuglink.py
addressing most of mypy issues in trezorlib/cli folder
adding types to some untyped functions, mypy section in setup.cfg
typing what can be typed, some mypy fixes, resolving circular import issues
importing type objects in "if TYPE_CHECKING:" branch
fixing CI by removing assert in emulator, better ignore comments
CI assert fix, style fixes, new config options
fixup! CI assert fix, style fixes, new config options
type fixes after rebasing on master
fixing python3.6 and 3.7 unittests by importing Literal from typing_extensions
couple mypy and style fixes
fixes and improvements from code review
silencing all but one mypy issues
trial of typing the tools.expect function
fixup! trial of typing the tools.expect function
@expect and @session decorators correctly type-checked
Optional args in CLI where relevant, not using general list/tuple/dict where possible
python/Makefile commands, adding them into CI, ignoring last mypy issue
documenting overload for expect decorator, two mypy fixes coming from that
black style fix
improved typing of decorators, pyright config file
addressing or ignoring pyright errors, replacing mypy in CI by pyright
fixing incomplete assert causing device tests to fail
pyright issue that showed in CI but not locally, printing pyright version in CI
fixup! pyright issue that showed in CI but not locally, printing pyright version in CI
unifying type:ignore statements for pyright usage
resolving PIL.Image issues, pyrightconfig not excluding anything
replacing couple asserts with TypeGuard on safe_issubclass
better error handling of usb1 import for webusb
better error handling of hid import
small typing details found out by strict pyright mode
improvements from code review
chore(python): changing List to Sequence for protobuf messages
small code changes to reflect the protobuf change to Sequence
importing TypedDict from typing_extensions to support 3.6 and 3.7
simplify _format_access_list function
fixup! simplify _format_access_list function
typing tools folder
typing helper-scripts folder
some click typing
enforcing all functions to have typed arguments
reverting the changed argument name in tools
replacing TransportType with Transport
making PinMatrixRequest.type protobuf attribute required
reverting the protobuf change, making argument into get_pin Optional
small fixes in asserts
solving the session decorator type issues
fixup! solving the session decorator type issues
improvements from code review
fixing new pyright errors introduced after version increase
changing -> Iterable to -> Sequence in enumerate_devices, change in wait_for_devices
style change in debuglink.py
chore(python): adding type annotation to Sequences in messages.py
better "self and cls" types on Transport
fixup! better "self and cls" types on Transport
fixing some easy things from strict pyright run
2021-11-03 22:12:53 +00:00
return messages . StellarPathPaymentStrictSendOp (
2021-10-07 14:06:08 +00:00
source_account = source_account ,
send_asset = _read_asset ( op . send_asset ) ,
send_amount = _read_amount ( op . send_amount ) ,
destination_account = op . destination . account_id ,
destination_asset = _read_asset ( op . dest_asset ) ,
destination_min = _read_amount ( op . dest_min ) ,
paths = [ _read_asset ( asset ) for asset in op . path ] ,
)
2021-07-29 07:19:05 +00:00
raise ValueError ( f " Unknown operation type: { op . __class__ . __name__ } " )
2018-04-04 01:50:22 +00:00
2021-10-08 00:06:16 +00:00
def _raise_if_account_muxed_id_exists ( account : " MuxedAccount " ) :
# Currently Trezor firmware does not support MuxedAccount,
# so we throw an exception here.
if account . account_muxed_id is not None :
raise ValueError ( " MuxedAccount is not supported " )
2021-07-29 07:19:05 +00:00
def _read_amount ( amount : str ) - > int :
return Operation . to_xdr_amount ( amount )
2018-04-04 01:50:22 +00:00
2018-04-28 18:59:08 +00:00
2021-07-29 07:19:05 +00:00
def _read_price ( price : Union [ " Price " , str , Decimal ] ) - > " Price " :
2021-10-08 00:39:00 +00:00
# In the coming stellar-sdk 6.x, the type of price must be Price,
2021-07-29 07:19:05 +00:00
# at that time we can remove this function
if isinstance ( price , Price ) :
return price
return Price . from_raw_price ( price )
2018-04-04 01:50:22 +00:00
2021-09-08 12:50:29 +00:00
def _read_asset ( asset : " Asset " ) - > messages . StellarAsset :
2021-07-29 07:19:05 +00:00
""" Reads a stellar Asset from unpacker """
if asset . is_native ( ) :
2021-09-08 12:50:29 +00:00
return messages . StellarAsset ( type = messages . StellarAssetType . NATIVE )
2021-07-29 07:19:05 +00:00
if asset . guess_asset_type ( ) == " credit_alphanum4 " :
2021-09-08 12:50:29 +00:00
return messages . StellarAsset (
type = messages . StellarAssetType . ALPHANUM4 ,
code = asset . code ,
issuer = asset . issuer ,
2021-07-29 07:19:05 +00:00
)
if asset . guess_asset_type ( ) == " credit_alphanum12 " :
2021-09-08 12:50:29 +00:00
return messages . StellarAsset (
type = messages . StellarAssetType . ALPHANUM12 ,
code = asset . code ,
issuer = asset . issuer ,
2021-07-29 07:19:05 +00:00
)
raise ValueError ( " Unsupported asset type " )
2018-06-13 17:04:18 +00:00
2018-08-10 14:05:14 +00:00
# ====== Client functions ====== #
2018-06-13 17:04:18 +00:00
feat(python): add full type information
WIP - typing the trezorctl apps
typing functions trezorlib/cli
addressing most of mypy issue for trezorlib apps and _internal folder
fixing broken device tests by changing asserts in debuglink.py
addressing most of mypy issues in trezorlib/cli folder
adding types to some untyped functions, mypy section in setup.cfg
typing what can be typed, some mypy fixes, resolving circular import issues
importing type objects in "if TYPE_CHECKING:" branch
fixing CI by removing assert in emulator, better ignore comments
CI assert fix, style fixes, new config options
fixup! CI assert fix, style fixes, new config options
type fixes after rebasing on master
fixing python3.6 and 3.7 unittests by importing Literal from typing_extensions
couple mypy and style fixes
fixes and improvements from code review
silencing all but one mypy issues
trial of typing the tools.expect function
fixup! trial of typing the tools.expect function
@expect and @session decorators correctly type-checked
Optional args in CLI where relevant, not using general list/tuple/dict where possible
python/Makefile commands, adding them into CI, ignoring last mypy issue
documenting overload for expect decorator, two mypy fixes coming from that
black style fix
improved typing of decorators, pyright config file
addressing or ignoring pyright errors, replacing mypy in CI by pyright
fixing incomplete assert causing device tests to fail
pyright issue that showed in CI but not locally, printing pyright version in CI
fixup! pyright issue that showed in CI but not locally, printing pyright version in CI
unifying type:ignore statements for pyright usage
resolving PIL.Image issues, pyrightconfig not excluding anything
replacing couple asserts with TypeGuard on safe_issubclass
better error handling of usb1 import for webusb
better error handling of hid import
small typing details found out by strict pyright mode
improvements from code review
chore(python): changing List to Sequence for protobuf messages
small code changes to reflect the protobuf change to Sequence
importing TypedDict from typing_extensions to support 3.6 and 3.7
simplify _format_access_list function
fixup! simplify _format_access_list function
typing tools folder
typing helper-scripts folder
some click typing
enforcing all functions to have typed arguments
reverting the changed argument name in tools
replacing TransportType with Transport
making PinMatrixRequest.type protobuf attribute required
reverting the protobuf change, making argument into get_pin Optional
small fixes in asserts
solving the session decorator type issues
fixup! solving the session decorator type issues
improvements from code review
fixing new pyright errors introduced after version increase
changing -> Iterable to -> Sequence in enumerate_devices, change in wait_for_devices
style change in debuglink.py
chore(python): adding type annotation to Sequences in messages.py
better "self and cls" types on Transport
fixup! better "self and cls" types on Transport
fixing some easy things from strict pyright run
2021-11-03 22:12:53 +00:00
@expect ( messages . StellarAddress , field = " address " , ret_type = str )
def get_address (
client : " TrezorClient " , address_n : " Address " , show_display : bool = False
) - > " MessageType " :
2018-08-13 16:21:24 +00:00
return client . call (
messages . StellarGetAddress ( address_n = address_n , show_display = show_display )
)
2018-06-13 17:04:18 +00:00
2018-08-13 16:21:24 +00:00
def sign_tx (
feat(python): add full type information
WIP - typing the trezorctl apps
typing functions trezorlib/cli
addressing most of mypy issue for trezorlib apps and _internal folder
fixing broken device tests by changing asserts in debuglink.py
addressing most of mypy issues in trezorlib/cli folder
adding types to some untyped functions, mypy section in setup.cfg
typing what can be typed, some mypy fixes, resolving circular import issues
importing type objects in "if TYPE_CHECKING:" branch
fixing CI by removing assert in emulator, better ignore comments
CI assert fix, style fixes, new config options
fixup! CI assert fix, style fixes, new config options
type fixes after rebasing on master
fixing python3.6 and 3.7 unittests by importing Literal from typing_extensions
couple mypy and style fixes
fixes and improvements from code review
silencing all but one mypy issues
trial of typing the tools.expect function
fixup! trial of typing the tools.expect function
@expect and @session decorators correctly type-checked
Optional args in CLI where relevant, not using general list/tuple/dict where possible
python/Makefile commands, adding them into CI, ignoring last mypy issue
documenting overload for expect decorator, two mypy fixes coming from that
black style fix
improved typing of decorators, pyright config file
addressing or ignoring pyright errors, replacing mypy in CI by pyright
fixing incomplete assert causing device tests to fail
pyright issue that showed in CI but not locally, printing pyright version in CI
fixup! pyright issue that showed in CI but not locally, printing pyright version in CI
unifying type:ignore statements for pyright usage
resolving PIL.Image issues, pyrightconfig not excluding anything
replacing couple asserts with TypeGuard on safe_issubclass
better error handling of usb1 import for webusb
better error handling of hid import
small typing details found out by strict pyright mode
improvements from code review
chore(python): changing List to Sequence for protobuf messages
small code changes to reflect the protobuf change to Sequence
importing TypedDict from typing_extensions to support 3.6 and 3.7
simplify _format_access_list function
fixup! simplify _format_access_list function
typing tools folder
typing helper-scripts folder
some click typing
enforcing all functions to have typed arguments
reverting the changed argument name in tools
replacing TransportType with Transport
making PinMatrixRequest.type protobuf attribute required
reverting the protobuf change, making argument into get_pin Optional
small fixes in asserts
solving the session decorator type issues
fixup! solving the session decorator type issues
improvements from code review
fixing new pyright errors introduced after version increase
changing -> Iterable to -> Sequence in enumerate_devices, change in wait_for_devices
style change in debuglink.py
chore(python): adding type annotation to Sequences in messages.py
better "self and cls" types on Transport
fixup! better "self and cls" types on Transport
fixing some easy things from strict pyright run
2021-11-03 22:12:53 +00:00
client : " TrezorClient " ,
tx : messages . StellarSignTx ,
operations : List [ " StellarMessageType " ] ,
address_n : " Address " ,
network_passphrase : str = DEFAULT_NETWORK_PASSPHRASE ,
) - > messages . StellarSignedTx :
2018-06-13 17:04:18 +00:00
tx . network_passphrase = network_passphrase
tx . address_n = address_n
tx . num_operations = len ( operations )
# Signing loop works as follows:
#
# 1. Start with tx (header information for the transaction) and operations (an array of operation protobuf messagess)
# 2. Send the tx header to the device
# 3. Receive a StellarTxOpRequest message
# 4. Send operations one by one until all operations have been sent. If there are more operations to sign, the device will send a StellarTxOpRequest message
# 5. The final message received will be StellarSignedTx which is returned from this method
resp = client . call ( tx )
try :
while isinstance ( resp , messages . StellarTxOpRequest ) :
resp = client . call ( operations . pop ( 0 ) )
except IndexError :
# pop from empty list
2020-03-24 13:18:08 +00:00
raise exceptions . TrezorException (
" Reached end of operations without a signature. "
2018-08-13 16:21:24 +00:00
) from None
2018-06-13 17:04:18 +00:00
if not isinstance ( resp , messages . StellarSignedTx ) :
2020-03-24 13:18:08 +00:00
raise exceptions . TrezorException (
2021-09-27 10:13:51 +00:00
f " Unexpected message: { resp . __class__ . __name__ } "
2020-03-24 13:18:08 +00:00
)
2018-06-13 17:04:18 +00:00
if operations :
2020-03-24 13:18:08 +00:00
raise exceptions . TrezorException (
" Received a signature before processing all operations. "
2018-08-13 16:21:24 +00:00
)
2018-06-13 17:04:18 +00:00
return resp