Wanchain support (#149)

pull/25/head
Peter van Mourik 6 years ago committed by Pavol Rusnak
parent 5e90d5116c
commit 6a062ec5ea

@ -8,26 +8,26 @@ from . import networks
from .get_address import _ethereum_address_hex
async def require_confirm_tx(ctx, to, value, chain_id, token=None):
async def require_confirm_tx(ctx, to, value, chain_id, token=None, tx_type=None):
if to:
str_to = _ethereum_address_hex(to)
else:
str_to = 'new contract?'
content = Text('Confirm sending', ui.ICON_SEND,
ui.BOLD, format_ethereum_amount(value, token, chain_id),
ui.BOLD, format_ethereum_amount(value, token, chain_id, tx_type),
ui.NORMAL, 'to',
ui.MONO, *split_address(str_to),
icon_color=ui.GREEN)
await require_confirm(ctx, content, ButtonRequestType.SignTx) # we use SignTx, not ConfirmOutput, for compatibility with T1
async def require_confirm_fee(ctx, spending, gas_price, gas_limit, chain_id, token=None):
async def require_confirm_fee(ctx, spending, gas_price, gas_limit, chain_id, token=None, tx_type=None):
content = Text('Confirm transaction', ui.ICON_SEND,
ui.BOLD, format_ethereum_amount(spending, token, chain_id),
ui.BOLD, format_ethereum_amount(spending, token, chain_id, tx_type),
ui.NORMAL, 'Gas price:',
ui.BOLD, format_ethereum_amount(gas_price, None, chain_id),
ui.BOLD, format_ethereum_amount(gas_price, None, chain_id, tx_type),
ui.NORMAL, 'Maximum fee:',
ui.BOLD, format_ethereum_amount(gas_price * gas_limit, None, chain_id),
ui.BOLD, format_ethereum_amount(gas_price *gas_limit, None, chain_id, tx_type),
icon_color=ui.GREEN)
await require_hold_to_confirm(ctx, content, ButtonRequestType.SignTx)
@ -51,12 +51,13 @@ def split_address(address):
return chunks(address, 17)
def format_ethereum_amount(value, token, chain_id):
def format_ethereum_amount(value, token, chain_id, tx_type=None):
value = int.from_bytes(value, 'big')
if token:
suffix = token[2]
decimals = token[3]
else:
suffix = networks.suffix_by_chain_id(chain_id)
suffix = networks.suffix_by_chain_id(chain_id, tx_type)
decimals = 18
if value <= 1e9:

@ -12,5 +12,8 @@ suffixes = {
}
def suffix_by_chain_id(chain_id):
return suffixes.get(chain_id, 'UNKN')
def suffix_by_chain_id(chain_id, tx_type=None):
if (tx_type==1 or tx_type==6) and (chain_id==1 or chain_id==3):
return"WAN"
else:
return suffixes.get(chain_id, 'UNKN')

@ -31,11 +31,11 @@ async def ethereum_sign_tx(ctx, msg):
recipient = msg.data_initial_chunk[16:36]
value = int.from_bytes(msg.data_initial_chunk[36:68], 'big')
await require_confirm_tx(ctx, recipient, value, msg.chain_id, token)
await require_confirm_tx(ctx, recipient, value, msg.chain_id, token, msg.tx_type)
if token is None and msg.data_length > 0:
await require_confirm_data(ctx, msg.data_initial_chunk, data_total)
await require_confirm_fee(ctx, value, int.from_bytes(msg.gas_price, 'big'), int.from_bytes(msg.gas_limit, 'big'), msg.chain_id, token)
await require_confirm_fee(ctx, value, int.from_bytes(msg.gas_price, 'big'), int.from_bytes(msg.gas_limit, 'big'), msg.chain_id, token, msg.tx_type)
data = bytearray()
data += msg.data_initial_chunk
@ -46,6 +46,9 @@ async def ethereum_sign_tx(ctx, msg):
sha = HashWriter(sha3_256)
sha.extend(rlp.encode_length(total_length, True)) # total length
if not msg.tx_type is None:
sha.extend(rlp.encode(msg.tx_type))
for field in [msg.nonce, msg.gas_price, msg.gas_limit, msg.to, msg.value]:
sha.extend(rlp.encode(field))
@ -72,6 +75,9 @@ async def ethereum_sign_tx(ctx, msg):
def get_total_length(msg: EthereumSignTx, data_total: int) -> int:
length = 0
if not msg.tx_type is None:
length += rlp.field_length(1, [msg.tx_type])
for field in [msg.nonce, msg.gas_price, msg.gas_limit, msg.to, msg.value]:
length += rlp.field_length(len(field), field[:1])
@ -123,6 +129,9 @@ def node_derive(root, address_n: list):
def check(msg: EthereumSignTx):
if msg.tx_type not in [1, 6, None]:
raise ValueError(FailureType.DataError, 'Txtype out of bounds')
if msg.chain_id < 0 or msg.chain_id > MAX_CHAIN_ID:
raise wire.DataError('Chain id out of bounds')

Loading…
Cancel
Save