mirror of
https://github.com/trezor/trezor-firmware.git
synced 2024-12-22 22:38:08 +00:00
core/bitcoin: Move script types from helpers to common.
This commit is contained in:
parent
3b6c1e5e6b
commit
a901573ea2
@ -3,14 +3,48 @@ from micropython import const
|
||||
from trezor import wire
|
||||
from trezor.crypto import bech32, bip32, der
|
||||
from trezor.crypto.curve import secp256k1
|
||||
from trezor.messages import InputScriptType, OutputScriptType
|
||||
from trezor.utils import ensure
|
||||
|
||||
if False:
|
||||
from apps.common.coininfo import CoinInfo
|
||||
from typing import Dict
|
||||
from trezor.messages.TxInputType import EnumTypeInputScriptType
|
||||
from trezor.messages.TxOutputType import EnumTypeOutputScriptType
|
||||
|
||||
# supported witness version for bech32 addresses
|
||||
_BECH32_WITVER = const(0x00)
|
||||
|
||||
MULTISIG_INPUT_SCRIPT_TYPES = (
|
||||
InputScriptType.SPENDMULTISIG,
|
||||
InputScriptType.SPENDP2SHWITNESS,
|
||||
InputScriptType.SPENDWITNESS,
|
||||
)
|
||||
MULTISIG_OUTPUT_SCRIPT_TYPES = (
|
||||
OutputScriptType.PAYTOMULTISIG,
|
||||
OutputScriptType.PAYTOP2SHWITNESS,
|
||||
OutputScriptType.PAYTOWITNESS,
|
||||
)
|
||||
|
||||
CHANGE_OUTPUT_TO_INPUT_SCRIPT_TYPES = {
|
||||
OutputScriptType.PAYTOADDRESS: InputScriptType.SPENDADDRESS,
|
||||
OutputScriptType.PAYTOMULTISIG: InputScriptType.SPENDMULTISIG,
|
||||
OutputScriptType.PAYTOP2SHWITNESS: InputScriptType.SPENDP2SHWITNESS,
|
||||
OutputScriptType.PAYTOWITNESS: InputScriptType.SPENDWITNESS,
|
||||
} # type: Dict[EnumTypeOutputScriptType, EnumTypeInputScriptType]
|
||||
INTERNAL_INPUT_SCRIPT_TYPES = tuple(CHANGE_OUTPUT_TO_INPUT_SCRIPT_TYPES.values())
|
||||
CHANGE_OUTPUT_SCRIPT_TYPES = tuple(CHANGE_OUTPUT_TO_INPUT_SCRIPT_TYPES.keys())
|
||||
|
||||
SEGWIT_INPUT_SCRIPT_TYPES = (
|
||||
InputScriptType.SPENDP2SHWITNESS,
|
||||
InputScriptType.SPENDWITNESS,
|
||||
)
|
||||
|
||||
NONSEGWIT_INPUT_SCRIPT_TYPES = (
|
||||
InputScriptType.SPENDADDRESS,
|
||||
InputScriptType.SPENDMULTISIG,
|
||||
)
|
||||
|
||||
|
||||
def ecdsa_sign(node: bip32.HDNode, digest: bytes) -> bytes:
|
||||
sig = secp256k1.sign(node.private_key(), digest)
|
||||
@ -29,7 +63,7 @@ def ecdsa_hash_pubkey(pubkey: bytes, coin: CoinInfo) -> bytes:
|
||||
return coin.script_hash(pubkey)
|
||||
|
||||
|
||||
def encode_bech32_address(prefix: str, script: bytes) -> bytes:
|
||||
def encode_bech32_address(prefix: str, script: bytes) -> str:
|
||||
address = bech32.encode(prefix, _BECH32_WITVER, script)
|
||||
if address is None:
|
||||
raise wire.ProcessError("Invalid address")
|
||||
|
@ -17,7 +17,7 @@ from trezor.utils import HashWriter, ensure
|
||||
from apps.common import coininfo, seed
|
||||
from apps.common.writers import write_bitcoin_varint
|
||||
|
||||
from .. import addresses, multisig, scripts, writers
|
||||
from .. import addresses, common, multisig, scripts, writers
|
||||
from ..common import ecdsa_hash_pubkey, ecdsa_sign
|
||||
from . import helpers, progress, tx_weight
|
||||
from .matchcheck import MultisigFingerprintChecker, WalletPathChecker
|
||||
@ -177,7 +177,7 @@ class Bitcoin:
|
||||
if not addresses.validate_full_path(txi.address_n, self.coin, txi.script_type):
|
||||
await helpers.confirm_foreign_address(txi.address_n)
|
||||
|
||||
if txi.script_type not in helpers.INTERNAL_INPUT_SCRIPT_TYPES:
|
||||
if txi.script_type not in common.INTERNAL_INPUT_SCRIPT_TYPES:
|
||||
raise wire.DataError("Wrong input script type")
|
||||
|
||||
prev_amount, script_pubkey = await self.get_prevtx_output(
|
||||
@ -436,7 +436,7 @@ class Bitcoin:
|
||||
if txo.address_n:
|
||||
# change output
|
||||
try:
|
||||
input_script_type = helpers.CHANGE_OUTPUT_TO_INPUT_SCRIPT_TYPES[
|
||||
input_script_type = common.CHANGE_OUTPUT_TO_INPUT_SCRIPT_TYPES[
|
||||
txo.script_type
|
||||
]
|
||||
except KeyError:
|
||||
@ -449,7 +449,7 @@ class Bitcoin:
|
||||
return scripts.output_derive_script(txo.address, self.coin)
|
||||
|
||||
def output_is_change(self, txo: TxOutputType) -> bool:
|
||||
if txo.script_type not in helpers.CHANGE_OUTPUT_SCRIPT_TYPES:
|
||||
if txo.script_type not in common.CHANGE_OUTPUT_SCRIPT_TYPES:
|
||||
return False
|
||||
if txo.multisig and not self.multisig_fingerprint.output_matches(txo):
|
||||
return False
|
||||
@ -536,8 +536,8 @@ class Bitcoin:
|
||||
|
||||
|
||||
def input_is_segwit(txi: TxInputType) -> bool:
|
||||
return txi.script_type in helpers.SEGWIT_INPUT_SCRIPT_TYPES
|
||||
return txi.script_type in common.SEGWIT_INPUT_SCRIPT_TYPES
|
||||
|
||||
|
||||
def input_is_nonsegwit(txi: TxInputType) -> bool:
|
||||
return txi.script_type in helpers.NONSEGWIT_INPUT_SCRIPT_TYPES
|
||||
return txi.script_type in common.NONSEGWIT_INPUT_SCRIPT_TYPES
|
||||
|
@ -18,42 +18,12 @@ from trezor.messages.TxRequest import TxRequest
|
||||
|
||||
from apps.common.coininfo import CoinInfo
|
||||
|
||||
from .. import common
|
||||
from ..writers import TX_HASH_SIZE
|
||||
|
||||
if False:
|
||||
from typing import Any, Awaitable, Dict
|
||||
from trezor.messages.TxInputType import EnumTypeInputScriptType
|
||||
from trezor.messages.TxOutputType import EnumTypeOutputScriptType
|
||||
from typing import Any, Awaitable
|
||||
|
||||
MULTISIG_INPUT_SCRIPT_TYPES = (
|
||||
InputScriptType.SPENDMULTISIG,
|
||||
InputScriptType.SPENDP2SHWITNESS,
|
||||
InputScriptType.SPENDWITNESS,
|
||||
)
|
||||
MULTISIG_OUTPUT_SCRIPT_TYPES = (
|
||||
OutputScriptType.PAYTOMULTISIG,
|
||||
OutputScriptType.PAYTOP2SHWITNESS,
|
||||
OutputScriptType.PAYTOWITNESS,
|
||||
)
|
||||
|
||||
CHANGE_OUTPUT_TO_INPUT_SCRIPT_TYPES = {
|
||||
OutputScriptType.PAYTOADDRESS: InputScriptType.SPENDADDRESS,
|
||||
OutputScriptType.PAYTOMULTISIG: InputScriptType.SPENDMULTISIG,
|
||||
OutputScriptType.PAYTOP2SHWITNESS: InputScriptType.SPENDP2SHWITNESS,
|
||||
OutputScriptType.PAYTOWITNESS: InputScriptType.SPENDWITNESS,
|
||||
} # type: Dict[EnumTypeOutputScriptType, EnumTypeInputScriptType]
|
||||
INTERNAL_INPUT_SCRIPT_TYPES = tuple(CHANGE_OUTPUT_TO_INPUT_SCRIPT_TYPES.values())
|
||||
CHANGE_OUTPUT_SCRIPT_TYPES = tuple(CHANGE_OUTPUT_TO_INPUT_SCRIPT_TYPES.keys())
|
||||
|
||||
SEGWIT_INPUT_SCRIPT_TYPES = (
|
||||
InputScriptType.SPENDP2SHWITNESS,
|
||||
InputScriptType.SPENDWITNESS,
|
||||
)
|
||||
|
||||
NONSEGWIT_INPUT_SCRIPT_TYPES = (
|
||||
InputScriptType.SPENDADDRESS,
|
||||
InputScriptType.SPENDMULTISIG,
|
||||
)
|
||||
|
||||
# Machine instructions
|
||||
# ===
|
||||
@ -244,13 +214,13 @@ def sanitize_tx_input(tx: TransactionType, coin: CoinInfo) -> TxInputType:
|
||||
raise wire.DataError("Missing prev_index field.")
|
||||
if txi.prev_hash is None or len(txi.prev_hash) != TX_HASH_SIZE:
|
||||
raise wire.DataError("Provided prev_hash is invalid.")
|
||||
if txi.multisig and txi.script_type not in MULTISIG_INPUT_SCRIPT_TYPES:
|
||||
if txi.multisig and txi.script_type not in common.MULTISIG_INPUT_SCRIPT_TYPES:
|
||||
raise wire.DataError("Multisig field provided but not expected.")
|
||||
if txi.address_n and txi.script_type not in INTERNAL_INPUT_SCRIPT_TYPES:
|
||||
if txi.address_n and txi.script_type not in common.INTERNAL_INPUT_SCRIPT_TYPES:
|
||||
raise wire.DataError("Input's address_n provided but not expected.")
|
||||
if not coin.decred and txi.decred_tree is not None:
|
||||
raise wire.DataError("Decred details provided but Decred coin not specified.")
|
||||
if txi.script_type in SEGWIT_INPUT_SCRIPT_TYPES:
|
||||
if txi.script_type in common.SEGWIT_INPUT_SCRIPT_TYPES:
|
||||
if not coin.segwit:
|
||||
raise wire.DataError("Segwit not enabled on this coin")
|
||||
return txi
|
||||
@ -258,9 +228,9 @@ def sanitize_tx_input(tx: TransactionType, coin: CoinInfo) -> TxInputType:
|
||||
|
||||
def sanitize_tx_output(tx: TransactionType, coin: CoinInfo) -> TxOutputType:
|
||||
txo = tx.outputs[0]
|
||||
if txo.multisig and txo.script_type not in MULTISIG_OUTPUT_SCRIPT_TYPES:
|
||||
if txo.multisig and txo.script_type not in common.MULTISIG_OUTPUT_SCRIPT_TYPES:
|
||||
raise wire.DataError("Multisig field provided but not expected.")
|
||||
if txo.address_n and txo.script_type not in CHANGE_OUTPUT_SCRIPT_TYPES:
|
||||
if txo.address_n and txo.script_type not in common.CHANGE_OUTPUT_SCRIPT_TYPES:
|
||||
raise wire.DataError("Output's address_n provided but not expected.")
|
||||
if txo.amount is None:
|
||||
raise wire.DataError("Missing amount field.")
|
||||
|
Loading…
Reference in New Issue
Block a user