1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-01-18 11:21:11 +00:00

apps.common.signtx: code style

This commit is contained in:
Jan Pochyla 2016-11-16 14:22:07 +01:00
parent c1aa7010ed
commit d00a6723c0

View File

@ -95,8 +95,6 @@ def request_tx_finish(tx_req: TxRequest):
yield tx_req yield tx_req
tx_req.serialized = None tx_req.serialized = None
def estimate_tx_size(inputs, outputs):
return 10 + inputs * 149 + outputs * 35
# Transaction signing # Transaction signing
# === # ===
@ -144,11 +142,10 @@ async def sign_tx(tx: SignTx, root):
raise SigningError(FailureType.Other, raise SigningError(FailureType.Other,
'Only one change output is valid') 'Only one change output is valid')
change_out = txo.amount change_out = txo.amount
else: elif txo.script_type != OutputScriptType.PAYTOOPRETURN:
if txo.script_type != OutputScriptType.PAYTOOPRETURN: if not await confirm_output(txo, coin):
if not await confirm_output(txo, coin): raise SigningError(FailureType.ActionCancelled,
raise SigningError(FailureType.ActionCancelled, 'Output cancelled')
'Output cancelled')
txo_bin.amount = txo.amount txo_bin.amount = txo.amount
txo_bin.script_pubkey = output_derive_script(txo, coin, root) txo_bin.script_pubkey = output_derive_script(txo, coin, root)
write_tx_output(h_first, txo_bin) write_tx_output(h_first, txo_bin)
@ -165,7 +162,6 @@ async def sign_tx(tx: SignTx, root):
raise SigningError(FailureType.ActionCancelled, raise SigningError(FailureType.ActionCancelled,
'Signing cancelled') 'Signing cancelled')
if not await confirm_total(total_out - change_out, fee, coin): if not await confirm_total(total_out - change_out, fee, coin):
raise SigningError(FailureType.ActionCancelled, raise SigningError(FailureType.ActionCancelled,
'Total cancelled') 'Total cancelled')
@ -310,28 +306,35 @@ def get_tx_hash(w, double: bool, reverse: bool=False) -> bytes:
return d return d
def estimate_tx_size(inputs, outputs):
return 10 + inputs * 149 + outputs * 35
# TX Outputs # TX Outputs
# === # ===
def output_derive_script(o: TxOutputType, coin: CoinType, root) -> bytes: def output_derive_script(o: TxOutputType, coin: CoinType, root) -> bytes:
if o.script_type == OutputScriptType.PAYTOADDRESS: if o.script_type == OutputScriptType.PAYTOADDRESS:
ra = output_paytoaddress_extract_raw_address(o, coin, root) ra = output_paytoaddress_extract_raw_address(o, coin, root)
ra = address_type.strip(coin.address_type, ra) ra = address_type.strip(coin.address_type, ra)
return script_paytoaddress_new(ra) return script_paytoaddress_new(ra)
elif o.script_type == OutputScriptType.PAYTOSCRIPTHASH: elif o.script_type == OutputScriptType.PAYTOSCRIPTHASH:
ra = output_paytoaddress_extract_raw_address(o, coin, root, p2sh=True) ra = output_paytoaddress_extract_raw_address(o, coin, root, p2sh=True)
ra = address_type.strip(coin.address_type_p2sh, ra) ra = address_type.strip(coin.address_type_p2sh, ra)
return script_paytoscripthash_new(ra) return script_paytoscripthash_new(ra)
elif o.script_type == OutputScriptType.PAYTOOPRETURN: elif o.script_type == OutputScriptType.PAYTOOPRETURN:
if o.amount == 0: if o.amount == 0:
return script_paytoopreturn_new(o.op_return_data) return script_paytoopreturn_new(o.op_return_data)
else: else:
raise SigningError(FailureType.SyntaxError, raise SigningError(FailureType.SyntaxError,
'OP_RETURN output with non-zero amount') 'OP_RETURN output with non-zero amount')
else: else:
raise SigningError(FailureType.SyntaxError, raise SigningError(FailureType.SyntaxError,
'Invalid output script type') 'Invalid output script type')
return
def output_paytoaddress_extract_raw_address(o: TxOutputType, coin: CoinType, root, p2sh=False) -> bytes: def output_paytoaddress_extract_raw_address(o: TxOutputType, coin: CoinType, root, p2sh=False) -> bytes:
@ -362,13 +365,15 @@ def output_is_change(o: TxOutputType):
# === # ===
def input_derive_script(i: TxInputType, pubkey: bytes, signature: bytes = None) -> bytes: def input_derive_script(i: TxInputType, pubkey: bytes, signature: bytes=None) -> bytes:
i_script_type = getattr(i, 'script_type', InputScriptType.SPENDADDRESS) i_script_type = getattr(i, 'script_type', InputScriptType.SPENDADDRESS)
if i_script_type == InputScriptType.SPENDADDRESS: if i_script_type == InputScriptType.SPENDADDRESS:
if signature is None: if signature is None:
return script_paytoaddress_new(ecdsa_hash_pubkey(pubkey)) return script_paytoaddress_new(ecdsa_hash_pubkey(pubkey))
else: else:
return script_spendaddress_new(pubkey, signature) return script_spendaddress_new(pubkey, signature)
else: else:
raise SigningError(FailureType.SyntaxError, raise SigningError(FailureType.SyntaxError,
'Unknown input script type') 'Unknown input script type')
@ -412,6 +417,7 @@ def script_paytoaddress_new(pubkeyhash: bytes) -> bytearray:
s[24] = 0xAC # OP_CHECKSIG s[24] = 0xAC # OP_CHECKSIG
return s return s
def script_paytoscripthash_new(scripthash: bytes) -> bytearray: def script_paytoscripthash_new(scripthash: bytes) -> bytearray:
s = bytearray(23) s = bytearray(23)
s[0] = 0xA9 # OP_HASH_160 s[0] = 0xA9 # OP_HASH_160
@ -420,13 +426,15 @@ def script_paytoscripthash_new(scripthash: bytes) -> bytearray:
s[22] = 0x87 # OP_EQUAL s[22] = 0x87 # OP_EQUAL
return s return s
def script_paytoopreturn_new(data: bytes) -> bytearray: def script_paytoopreturn_new(data: bytes) -> bytearray:
w = bytearray() w = bytearray()
w.append(0x6A) # OP_RETURN w.append(0x6A) # OP_RETURN
write_op_push(w, len(data)) write_op_push(w, len(data))
w.extend(data) w.extend(data)
return w return w
def script_spendaddress_new(pubkey: bytes, signature: bytes) -> bytearray: def script_spendaddress_new(pubkey: bytes, signature: bytes) -> bytearray:
w = bytearray() w = bytearray()
write_op_push(w, len(signature) + 1) write_op_push(w, len(signature) + 1)
@ -440,6 +448,7 @@ def script_spendaddress_new(pubkey: bytes, signature: bytes) -> bytearray:
# TX Serialization # TX Serialization
# === # ===
def write_tx_input(w, i: TxInputType): def write_tx_input(w, i: TxInputType):
i_sequence = getattr(i, 'sequence', 4294967295) i_sequence = getattr(i, 'sequence', 4294967295)
write_bytes_rev(w, i.prev_hash) write_bytes_rev(w, i.prev_hash)