1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-17 21:22:10 +00:00

refactor(core): convert apps.binance to layouts

This commit is contained in:
Martin Milata 2021-03-11 12:46:11 +01:00 committed by matejcik
parent ddac852acf
commit 3ffbae5d17
4 changed files with 64 additions and 53 deletions

View File

@ -1,4 +1,3 @@
from trezor import ui
from trezor.enums import BinanceOrderSide, ButtonRequestType
from trezor.messages import (
BinanceCancelMsg,
@ -7,77 +6,67 @@ from trezor.messages import (
BinanceTransferMsg,
)
from trezor.strings import format_amount
from trezor.ui.components.tt.scroll import Paginated
from trezor.ui.components.tt.text import Text
from apps.common.confirm import require_hold_to_confirm
from apps.common.layout import split_address
from trezor.ui.layouts import confirm_properties, confirm_transfer_binance
from . import helpers
async def require_confirm_transfer(ctx, msg: BinanceTransferMsg):
items = []
def make_input_output_pages(msg: BinanceInputOutput, direction):
pages = []
for coin in msg.coins:
coin_page = Text("Confirm " + direction, ui.ICON_SEND, icon_color=ui.GREEN)
coin_page.bold(
format_amount(coin.amount, helpers.DECIMALS) + " " + coin.denom
items.append(
(
direction,
format_amount(coin.amount, helpers.DECIMALS) + " " + coin.denom,
msg.address,
)
)
coin_page.normal("to")
coin_page.mono(*split_address(msg.address))
pages.append(coin_page)
return pages
pages = []
for txinput in msg.inputs:
pages.extend(make_input_output_pages(txinput, "input"))
make_input_output_pages(txinput, "Confirm input")
for txoutput in msg.outputs:
pages.extend(make_input_output_pages(txoutput, "output"))
make_input_output_pages(txoutput, "Confirm output")
return await require_hold_to_confirm(
ctx, Paginated(pages), ButtonRequestType.ConfirmOutput
)
await confirm_transfer_binance(ctx, items)
async def require_confirm_cancel(ctx, msg: BinanceCancelMsg):
page1 = Text("Confirm cancel 1/2", ui.ICON_SEND, icon_color=ui.GREEN)
page1.normal("Sender address:")
page1.bold(msg.sender)
page1.normal("Pair:")
page1.bold(msg.symbol)
page2 = Text("Confirm cancel 2/2", ui.ICON_SEND, icon_color=ui.GREEN)
page2.normal("Order ID:")
page2.bold(msg.refid)
return await require_hold_to_confirm(
ctx, Paginated([page1, page2]), ButtonRequestType.SignTx
await confirm_properties(
ctx,
"confirm_cancel",
title="Confirm cancel",
props=[
("Sender address:", msg.sender),
("Pair:", msg.symbol),
("Order ID:", msg.refid),
],
hold=True,
br_code=ButtonRequestType.SignTx,
)
async def require_confirm_order(ctx, msg: BinanceOrderMsg):
page1 = Text("Confirm order 1/3", ui.ICON_SEND, icon_color=ui.GREEN)
page1.normal("Sender address:")
page1.bold(msg.sender)
page2 = Text("Confirm order 2/3", ui.ICON_SEND, icon_color=ui.GREEN)
page2.normal("Pair:")
page2.bold(msg.symbol)
page2.normal("Side:")
if msg.side == BinanceOrderSide.BUY:
page2.bold("Buy")
side = "Buy"
elif msg.side == BinanceOrderSide.SELL:
page2.bold("Sell")
side = "Sell"
else:
side = "?"
page3 = Text("Confirm order 3/3", ui.ICON_SEND, icon_color=ui.GREEN)
page3.normal("Quantity:")
page3.bold(format_amount(msg.quantity, helpers.DECIMALS))
page3.normal("Price:")
page3.bold(format_amount(msg.price, helpers.DECIMALS))
return await require_hold_to_confirm(
ctx, Paginated([page1, page2, page3]), ButtonRequestType.SignTx
await confirm_properties(
ctx,
"confirm_order",
title="Confirm order",
props=[
("Sender address:", msg.sender),
("Pair:", msg.symbol),
("Side:", side),
("Quantity:", format_amount(msg.quantity, helpers.DECIMALS)),
("Price:", format_amount(msg.price, helpers.DECIMALS)),
],
hold=True,
br_code=ButtonRequestType.SignTx,
)

View File

@ -67,6 +67,7 @@ __all__ = (
"confirm_coinjoin",
"confirm_timebounds_stellar",
"confirm_proposals_tezos",
"confirm_transfer_binance",
)
@ -864,3 +865,24 @@ async def confirm_proposals_tezos(
await raise_if_cancelled(
interact(ctx, paginated, "confirm_proposals", ButtonRequestType.SignTx)
)
# TODO cleanup @ redesign
async def confirm_transfer_binance(
ctx: wire.GenericContext, inputs_outputs: Sequence[Tuple[str, str, str]]
) -> None:
pages: list[ui.Component] = []
for title, amount, address in inputs_outputs:
coin_page = Text(title, ui.ICON_SEND, icon_color=ui.GREEN, new_lines=False)
coin_page.bold(amount)
coin_page.normal("\nto\n")
coin_page.mono(*_split_address(address))
pages.append(coin_page)
pages[-1] = HoldToConfirm(pages[-1])
await raise_if_cancelled(
interact(
ctx, Paginated(pages), "confirm_transfer", ButtonRequestType.ConfirmOutput
)
)

View File

@ -35,5 +35,5 @@ BINANCE_ADDRESS_TEST_VECTORS = [
def test_binance_get_address(client, path, expected_address):
# data from https://github.com/binance-chain/javascript-sdk/blob/master/__tests__/crypto.test.js#L50
address = get_address(client, parse_path(path))
address = get_address(client, parse_path(path), show_display=True)
assert address == expected_address

View File

@ -29,7 +29,7 @@ BINANCE_PATH = parse_path("m/44h/714h/0h/0/0")
mnemonic="offer caution gift cross surge pretty orange during eye soldier popular holiday mention east eight office fashion ill parrot vault rent devote earth cousin"
)
def test_binance_get_public_key(client):
sig = binance.get_public_key(client, BINANCE_PATH)
sig = binance.get_public_key(client, BINANCE_PATH, show_display=True)
assert (
sig.hex()
== "029729a52e4e3c2b4a4e52aa74033eedaf8ba1df5ab6d1f518fd69e67bbd309b0e"