From 66b8bbb1a5aabd076f71c57856066768ec10e409 Mon Sep 17 00:00:00 2001 From: Tomas Susanka Date: Tue, 10 Jul 2018 16:30:36 +0200 Subject: [PATCH] ripple: style fixes --- src/apps/ripple/__init__.py | 8 +-- src/apps/ripple/get_address.py | 6 +- src/apps/ripple/layout.py | 18 +++--- src/apps/ripple/serialize.py | 113 ++++++++++++--------------------- src/apps/ripple/sign_tx.py | 25 +++++--- 5 files changed, 74 insertions(+), 96 deletions(-) diff --git a/src/apps/ripple/__init__.py b/src/apps/ripple/__init__.py index cd31ff63cb..1ae476315b 100644 --- a/src/apps/ripple/__init__.py +++ b/src/apps/ripple/__init__.py @@ -1,16 +1,16 @@ -from trezor.wire import register, protobuf_workflow -from trezor.messages.MessageType import RippleGetAddress -from trezor.messages.MessageType import RippleSignTx -from .get_address import get_address +from trezor.messages.MessageType import RippleGetAddress, RippleSignTx +from trezor.wire import protobuf_workflow, register def dispatch_RippleGetAddress(*args, **kwargs): from .get_address import get_address + return get_address(*args, **kwargs) def dispatch_RippleSignTx(*args, **kwargs): from .sign_tx import sign_tx + return sign_tx(*args, **kwargs) diff --git a/src/apps/ripple/get_address.py b/src/apps/ripple/get_address.py index 8f66f8c9db..86d251044a 100644 --- a/src/apps/ripple/get_address.py +++ b/src/apps/ripple/get_address.py @@ -1,9 +1,11 @@ -from apps.common import seed -from apps.common.display_address import show_address, show_qr from trezor.messages.RippleAddress import RippleAddress from trezor.messages.RippleGetAddress import RippleGetAddress + from . import helpers +from apps.common import seed +from apps.common.display_address import show_address, show_qr + async def get_address(ctx, msg: RippleGetAddress): node = await seed.derive_node(ctx, msg.address_n) diff --git a/src/apps/ripple/layout.py b/src/apps/ripple/layout.py index f31917f160..1c997caf24 100644 --- a/src/apps/ripple/layout.py +++ b/src/apps/ripple/layout.py @@ -1,23 +1,25 @@ -from apps.common.confirm import require_confirm, require_hold_to_confirm -from apps.common.display_address import split_address from trezor import ui from trezor.messages import ButtonRequestType from trezor.ui.text import Text from trezor.utils import format_amount + from . import helpers +from apps.common.confirm import require_confirm, require_hold_to_confirm +from apps.common.display_address import split_address + async def require_confirm_fee(ctx, fee): - text = Text('Confirm fee', ui.ICON_SEND, icon_color=ui.GREEN) - text.normal('Transaction fee:') - text.bold(format_amount(fee, helpers.DIVISIBILITY) + ' XRP') + text = Text("Confirm fee", ui.ICON_SEND, icon_color=ui.GREEN) + text.normal("Transaction fee:") + text.bold(format_amount(fee, helpers.DIVISIBILITY) + " XRP") await require_confirm(ctx, text, ButtonRequestType.ConfirmOutput) async def require_confirm_tx(ctx, to, value): - text = Text('Confirm sending', ui.ICON_SEND, icon_color=ui.GREEN) - text.bold(format_amount(value, helpers.DIVISIBILITY) + ' XRP') - text.normal('to') + text = Text("Confirm sending", ui.ICON_SEND, icon_color=ui.GREEN) + text.bold(format_amount(value, helpers.DIVISIBILITY) + " XRP") + text.normal("to") text.mono(*split_address(to)) return await require_hold_to_confirm(ctx, text, ButtonRequestType.SignTx) diff --git a/src/apps/ripple/serialize.py b/src/apps/ripple/serialize.py index 2d6582e9ac..74d9d7b21f 100644 --- a/src/apps/ripple/serialize.py +++ b/src/apps/ripple/serialize.py @@ -9,6 +9,7 @@ # transaction type and the fields that are required for it. # from trezor.messages.RippleSignTx import RippleSignTx + from . import helpers FIELD_TYPE_INT16 = 1 @@ -18,66 +19,34 @@ FIELD_TYPE_VL = 7 FIELD_TYPE_ACCOUNT = 8 FIELDS_MAP = { - 'account': { - 'type': FIELD_TYPE_ACCOUNT, - 'key': 1, - }, - 'amount': { - 'type': FIELD_TYPE_AMOUNT, - 'key': 1, - }, - 'destination': { - 'type': FIELD_TYPE_ACCOUNT, - 'key': 3, - }, - 'fee': { - 'type': FIELD_TYPE_AMOUNT, - 'key': 8, - }, - 'sequence': { - 'type': FIELD_TYPE_INT32, - 'key': 4, - }, - 'type': { - 'type': FIELD_TYPE_INT16, - 'key': 2, - }, - 'signingPubKey': { - 'type': FIELD_TYPE_VL, - 'key': 3, - }, - 'flags': { - 'type': FIELD_TYPE_INT32, - 'key': 2, - }, - 'txnSignature': { - 'type': FIELD_TYPE_VL, - 'key': 4, - }, - 'lastLedgerSequence': { - 'type': FIELD_TYPE_INT32, - 'key': 27, - }, + "account": {"type": FIELD_TYPE_ACCOUNT, "key": 1}, + "amount": {"type": FIELD_TYPE_AMOUNT, "key": 1}, + "destination": {"type": FIELD_TYPE_ACCOUNT, "key": 3}, + "fee": {"type": FIELD_TYPE_AMOUNT, "key": 8}, + "sequence": {"type": FIELD_TYPE_INT32, "key": 4}, + "type": {"type": FIELD_TYPE_INT16, "key": 2}, + "signingPubKey": {"type": FIELD_TYPE_VL, "key": 3}, + "flags": {"type": FIELD_TYPE_INT32, "key": 2}, + "txnSignature": {"type": FIELD_TYPE_VL, "key": 4}, + "lastLedgerSequence": {"type": FIELD_TYPE_INT32, "key": 27}, } -TRANSACTION_TYPES = { - 'Payment': 0, -} +TRANSACTION_TYPES = {"Payment": 0} def serialize(msg: RippleSignTx, source_address: str, pubkey=None, signature=None): w = bytearray() # must be sorted numerically first by type and then by name - write(w, FIELDS_MAP['type'], TRANSACTION_TYPES['Payment']) - write(w, FIELDS_MAP['flags'], msg.flags) - write(w, FIELDS_MAP['sequence'], msg.sequence) - write(w, FIELDS_MAP['lastLedgerSequence'], msg.last_ledger_sequence) - write(w, FIELDS_MAP['amount'], msg.payment.amount) - write(w, FIELDS_MAP['fee'], msg.fee) - write(w, FIELDS_MAP['signingPubKey'], pubkey) - write(w, FIELDS_MAP['txnSignature'], signature) - write(w, FIELDS_MAP['account'], source_address) - write(w, FIELDS_MAP['destination'], msg.payment.destination) + write(w, FIELDS_MAP["type"], TRANSACTION_TYPES["Payment"]) + write(w, FIELDS_MAP["flags"], msg.flags) + write(w, FIELDS_MAP["sequence"], msg.sequence) + write(w, FIELDS_MAP["lastLedgerSequence"], msg.last_ledger_sequence) + write(w, FIELDS_MAP["amount"], msg.payment.amount) + write(w, FIELDS_MAP["fee"], msg.fee) + write(w, FIELDS_MAP["signingPubKey"], pubkey) + write(w, FIELDS_MAP["txnSignature"], signature) + write(w, FIELDS_MAP["account"], source_address) + write(w, FIELDS_MAP["destination"], msg.payment.destination) return w @@ -85,36 +54,36 @@ def write(w: bytearray, field: dict, value): if value is None: return write_type(w, field) - if field['type'] == FIELD_TYPE_INT16: - w.extend(value.to_bytes(2, 'big')) - elif field['type'] == FIELD_TYPE_INT32: - w.extend(value.to_bytes(4, 'big')) - elif field['type'] == FIELD_TYPE_AMOUNT: + if field["type"] == FIELD_TYPE_INT16: + w.extend(value.to_bytes(2, "big")) + elif field["type"] == FIELD_TYPE_INT32: + w.extend(value.to_bytes(4, "big")) + elif field["type"] == FIELD_TYPE_AMOUNT: w.extend(serialize_amount(value)) - elif field['type'] == FIELD_TYPE_ACCOUNT: + elif field["type"] == FIELD_TYPE_ACCOUNT: write_bytes(w, helpers.decode_address(value)) - elif field['type'] == FIELD_TYPE_VL: + elif field["type"] == FIELD_TYPE_VL: write_bytes(w, value) else: - raise ValueError('Unknown field type') + raise ValueError("Unknown field type") def write_type(w: bytearray, field: dict): - if field['key'] <= 0xf: - w.append((field['type'] << 4) | field['key']) + if field["key"] <= 0xf: + w.append((field["type"] << 4) | field["key"]) else: # this concerns two-bytes fields such as lastLedgerSequence - w.append(field['type'] << 4) - w.append(field['key']) + w.append(field["type"] << 4) + w.append(field["key"]) def serialize_amount(value: int) -> bytearray: if value < 0 or isinstance(value, float): - raise ValueError('Only positive integers are supported') + raise ValueError("Only positive integers are supported") if value > 100000000000: # max allowed value - raise ValueError('Value is larger than 100000000000') + raise ValueError("Value is larger than 100000000000") - b = bytearray(value.to_bytes(8, 'big')) + b = bytearray(value.to_bytes(8, "big")) # Clear first bit to indicate XRP b[0] &= 0x7f # Set second bit to indicate positive number @@ -145,12 +114,8 @@ def serialize_varint(w, val): b.extend([193 + rshift(val, 8), val & 0xff]) elif val <= 918744: val -= 12481 - b.extend([ - 241 + rshift(val, 16), - rshift(val, 8) & 0xff, - val & 0xff - ]) + b.extend([241 + rshift(val, 16), rshift(val, 8) & 0xff, val & 0xff]) else: - raise ValueError('Variable integer overflow.') + raise ValueError("Variable integer overflow.") w.extend(b) diff --git a/src/apps/ripple/sign_tx.py b/src/apps/ripple/sign_tx.py index dfc2bbdebb..6c623b1445 100644 --- a/src/apps/ripple/sign_tx.py +++ b/src/apps/ripple/sign_tx.py @@ -1,13 +1,14 @@ -from apps.common import seed from trezor.crypto import der from trezor.crypto.curve import secp256k1 from trezor.crypto.hashlib import sha512 -from trezor.messages.RippleSignTx import RippleSignTx from trezor.messages.RippleSignedTx import RippleSignedTx +from trezor.messages.RippleSignTx import RippleSignTx from trezor.wire import ProcessError + +from . import helpers, layout from .serialize import serialize -from . import helpers -from . import layout + +from apps.common import seed async def sign_tx(ctx, msg: RippleSignTx): @@ -30,12 +31,12 @@ async def sign_tx(ctx, msg: RippleSignTx): def check_fee(fee: int): if fee < helpers.MIN_FEE or fee > helpers.MAX_FEE: - raise ProcessError('Fee must be in the range of 10 to 10,000 drops') + raise ProcessError("Fee must be in the range of 10 to 10,000 drops") def get_network_prefix(): """Network prefix is prepended before the transaction and public key is included""" - return helpers.HASH_TX_SIGN.to_bytes(4, 'big') + return helpers.HASH_TX_SIGN.to_bytes(4, "big") def first_half_of_sha512(b): @@ -64,5 +65,13 @@ def set_canonical_flag(msg: RippleSignTx): def validate(msg: RippleSignTx): - if None in (msg.fee, msg.sequence, msg.payment, msg.payment.amount, msg.payment.destination): - raise ProcessError("Some of the required fields are missing (fee, sequence, payment.amount, payment.destination)") + if None in ( + msg.fee, + msg.sequence, + msg.payment, + msg.payment.amount, + msg.payment.destination, + ): + raise ProcessError( + "Some of the required fields are missing (fee, sequence, payment.amount, payment.destination)" + )