Skip to content
This repository has been archived by the owner on Jun 5, 2019. It is now read-only.

Commit

Permalink
trezorlib: drop @field decorator
Browse files Browse the repository at this point in the history
its function is replaced by @expect(field="name") -- it doesn't make sense
to use @field without @expect anyway
  • Loading branch information
matejcik committed Jun 25, 2018
1 parent ea4b4a5 commit beb5145
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 71 deletions.
7 changes: 2 additions & 5 deletions trezorlib/btc.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
from . import messages as proto
from .tools import expect, field, CallException, normalize_nfc, session

### Client functions ###
from .tools import expect, CallException, normalize_nfc, session


@expect(proto.PublicKey)
def get_public_node(client, n, ecdsa_curve_name=None, show_display=False, coin_name=None):
return client.call(proto.GetPublicKey(address_n=n, ecdsa_curve_name=ecdsa_curve_name, show_display=show_display, coin_name=coin_name))


@field('address')
@expect(proto.Address)
@expect(proto.Address, field="address")
def get_address(client, coin_name, n, show_display=False, multisig=None, script_type=proto.InputScriptType.SPENDADDRESS):
if multisig:
return client.call(proto.GetAddress(address_n=n, coin_name=coin_name, show_display=show_display, multisig=multisig, script_type=script_type))
Expand Down
6 changes: 2 additions & 4 deletions trezorlib/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,8 +418,7 @@ def expand_path(n):
warnings.warn('expand_path is deprecated, use tools.parse_path', DeprecationWarning, stacklevel=2)
return tools.parse_path(n)

@tools.field('message')
@tools.expect(proto.Success)
@tools.expect(proto.Success, field="message")
def ping(self, msg, button_protection=False, pin_protection=False, passphrase_protection=False):
msg = proto.Ping(message=msg,
button_protection=button_protection,
Expand Down Expand Up @@ -453,8 +452,7 @@ def _prepare_sign_tx(self, inputs, outputs):

return txes

@tools.field('message')
@tools.expect(proto.Success)
@tools.expect(proto.Success, field="message")
def clear_session(self):
return self.call(proto.ClearSession())

Expand Down
35 changes: 12 additions & 23 deletions trezorlib/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

from . import messages as proto
from . import tools
from .tools import field, expect, session
from .tools import expect, session

from .transport import enumerate_devices, get_transport

Expand All @@ -45,8 +45,7 @@ def find_by_path(cls, path):
return get_transport(path, prefix_search=False)


@field('message')
@expect(proto.Success)
@expect(proto.Success, field="message")
def apply_settings(client, label=None, language=None, use_passphrase=None, homescreen=None, passphrase_source=None, auto_lock_delay_ms=None):
settings = proto.ApplySettings()
if label is not None:
Expand All @@ -67,39 +66,34 @@ def apply_settings(client, label=None, language=None, use_passphrase=None, homes
return out


@field('message')
@expect(proto.Success)
@expect(proto.Success, field="message")
def apply_flags(client, flags):
out = client.call(proto.ApplyFlags(flags=flags))
client.init_device() # Reload Features
return out


@field('message')
@expect(proto.Success)
@expect(proto.Success, field="message")
def change_pin(client, remove=False):
ret = client.call(proto.ChangePin(remove=remove))
client.init_device() # Re-read features
return ret


@field('message')
@expect(proto.Success)
@expect(proto.Success, field="message")
def set_u2f_counter(client, u2f_counter):
ret = client.call(proto.SetU2FCounter(u2f_counter=u2f_counter))
return ret


@field('message')
@expect(proto.Success)
@expect(proto.Success, field="message")
def wipe_device(client):
ret = client.call(proto.WipeDevice())
client.init_device()
return ret


@field('message')
@expect(proto.Success)
@expect(proto.Success, field="message")
def recovery_device(client, word_count, passphrase_protection, pin_protection, label, language, type=proto.RecoveryDeviceType.ScrambledWords, expand=False, dry_run=False):
if client.features.initialized and not dry_run:
raise RuntimeError("Device is initialized already. Call wipe_device() and try again.")
Expand Down Expand Up @@ -128,8 +122,7 @@ def recovery_device(client, word_count, passphrase_protection, pin_protection, l
return res


@field('message')
@expect(proto.Success)
@expect(proto.Success, field="message")
@session
def reset_device(client, display_random, strength, passphrase_protection, pin_protection, label, language, u2f_counter=0, skip_backup=False):
if client.features.initialized:
Expand All @@ -156,15 +149,13 @@ def reset_device(client, display_random, strength, passphrase_protection, pin_pr
return ret


@field('message')
@expect(proto.Success)
@expect(proto.Success, field="message")
def backup_device(client):
ret = client.call(proto.BackupDevice())
return ret


@field('message')
@expect(proto.Success)
@expect(proto.Success, field="message")
def load_device_by_mnemonic(client, mnemonic, pin, passphrase_protection, label, language='english', skip_checksum=False, expand=False):
# Convert mnemonic to UTF8 NKFD
mnemonic = Mnemonic.normalize_string(mnemonic)
Expand Down Expand Up @@ -192,8 +183,7 @@ def load_device_by_mnemonic(client, mnemonic, pin, passphrase_protection, label,
return resp


@field('message')
@expect(proto.Success)
@expect(proto.Success, field="message")
def load_device_by_xprv(client, xprv, pin, passphrase_protection, label, language):
if client.features.initialized:
raise RuntimeError("Device is initialized already. Call wipe_device() and try again.")
Expand Down Expand Up @@ -237,8 +227,7 @@ def load_device_by_xprv(client, xprv, pin, passphrase_protection, label, languag
return resp


@field('message')
@expect(proto.Success)
@expect(proto.Success, field="message")
def self_test(client):
if client.features.bootloader_mode is False:
raise RuntimeError("Device must be in bootloader mode")
Expand Down
5 changes: 2 additions & 3 deletions trezorlib/ethereum.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from . import messages as proto
from .tools import field, expect, CallException, normalize_nfc, session
from .tools import expect, CallException, normalize_nfc, session


def int_to_big_endian(value):
Expand All @@ -9,8 +9,7 @@ def int_to_big_endian(value):
### Client functions ###


@field('address')
@expect(proto.EthereumAddress)
@expect(proto.EthereumAddress, field="address")
def get_address(client, n, show_display=False, multisig=None):
return client.call(proto.EthereumGetAddress(address_n=n, show_display=show_display))

Expand Down
5 changes: 2 additions & 3 deletions trezorlib/lisk.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import binascii

from . import messages as proto
from .tools import field, expect, CallException, normalize_nfc
from .tools import expect, CallException, normalize_nfc


@field('address')
@expect(proto.LiskAddress)
@expect(proto.LiskAddress, field="address")
def get_address(client, n, show_display=False):
return client.call(proto.LiskGetAddress(address_n=n, show_display=show_display))

Expand Down
11 changes: 4 additions & 7 deletions trezorlib/misc.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from . import messages as proto
from .tools import field, expect
from .tools import expect


@field('entropy')
@expect(proto.Entropy)
@expect(proto.Entropy, field="entropy")
def get_entropy(client, size):
return client.call(proto.GetEntropy(size=size))

Expand All @@ -18,8 +17,7 @@ def get_ecdh_session_key(client, identity, peer_public_key, ecdsa_curve_name=Non
return client.call(proto.GetECDHSessionKey(identity=identity, peer_public_key=peer_public_key, ecdsa_curve_name=ecdsa_curve_name))


@field('value')
@expect(proto.CipheredKeyValue)
@expect(proto.CipheredKeyValue, field="value")
def encrypt_keyvalue(client, n, key, value, ask_on_encrypt=True, ask_on_decrypt=True, iv=b''):
return client.call(proto.CipherKeyValue(address_n=n,
key=key,
Expand All @@ -30,8 +28,7 @@ def encrypt_keyvalue(client, n, key, value, ask_on_encrypt=True, ask_on_decrypt=
iv=iv))


@field('value')
@expect(proto.CipheredKeyValue)
@expect(proto.CipheredKeyValue, field="value")
def decrypt_keyvalue(client, n, key, value, ask_on_encrypt=True, ask_on_decrypt=True, iv=b''):
return client.call(proto.CipherKeyValue(address_n=n,
key=key,
Expand Down
5 changes: 2 additions & 3 deletions trezorlib/nem.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import binascii
import json
from . import messages as proto
from .tools import expect, field, CallException
from .tools import expect, CallException

TYPE_TRANSACTION_TRANSFER = 0x0101
TYPE_IMPORTANCE_TRANSFER = 0x0801
Expand Down Expand Up @@ -154,8 +154,7 @@ def create_sign_tx(transaction):
### Client functions ###


@field("address")
@expect(proto.NEMAddress)
@expect(proto.NEMAddress, field="address")
def get_address(client, n, network, show_display=False):
return client.call(proto.NEMGetAddress(address_n=n, network=network, show_display=show_display))

Expand Down
8 changes: 3 additions & 5 deletions trezorlib/stellar.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import xdrlib

from . import messages as proto
from .tools import field, expect, CallException
from .tools import expect, CallException

# Memo types
MEMO_TYPE_TEXT = 0
Expand Down Expand Up @@ -324,14 +324,12 @@ def _crc16_checksum(bytes):
### Client functions ###


@field('public_key')
@expect(proto.StellarPublicKey)
@expect(proto.StellarPublicKey, field="public_key")
def get_public_key(client, address_n, show_display=False):
return client.call(proto.StellarGetPublicKey(address_n=address_n, show_display=show_display))


@field('address')
@expect(proto.StellarAddress)
@expect(proto.StellarAddress, field="address")
def get_address(client, address_n, show_display=False):
return client.call(proto.StellarGetAddress(address_n=address_n, show_display=show_display))

Expand Down
26 changes: 8 additions & 18 deletions trezorlib/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def str_to_harden(x: str) -> int:
return int(x)

try:
return list(str_to_harden(x) for x in n)
return [str_to_harden(x) for x in n]
except Exception:
raise ValueError('Invalid BIP32 path', nstr)

Expand All @@ -179,35 +179,25 @@ class CallException(Exception):
pass


class field:
# Decorator extracts single value from
# protobuf object. If the field is not
# present, raises an exception.
def __init__(self, field):
self.field = field

def __call__(self, f):
@functools.wraps(f)
def wrapped_f(*args, **kwargs):
ret = f(*args, **kwargs)
return getattr(ret, self.field)
return wrapped_f


class expect:
# Decorator checks if the method
# returned one of expected protobuf messages
# or raises an exception
def __init__(self, *expected):
def __init__(self, expected, field=None):
self.expected = expected
self.field = field

def __call__(self, f):
@functools.wraps(f)
def wrapped_f(*args, **kwargs):
ret = f(*args, **kwargs)
if not isinstance(ret, self.expected):
raise RuntimeError("Got %s, expected %s" % (ret.__class__, self.expected))
return ret
if self.field is not None:
return getattr(ret, self.field)
else:
return ret

return wrapped_f


Expand Down

0 comments on commit beb5145

Please sign in to comment.