1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-17 11:58:13 +00:00

nem: wire.ProcessError instead of ValueError

This commit is contained in:
Tomas Susanka 2018-06-13 13:47:22 +02:00
parent 9910fef423
commit 5910ebf279

View File

@ -1,5 +1,7 @@
from trezor.crypto import nem from trezor.crypto import nem
from trezor.wire import ProcessError
from trezor.messages import NEMModificationType from trezor.messages import NEMModificationType
from trezor.messages import NEMSupplyChangeType
from trezor.messages.NEMSignTx import (NEMAggregateModification, from trezor.messages.NEMSignTx import (NEMAggregateModification,
NEMImportanceTransfer, NEMImportanceTransfer,
NEMMosaicCreation, NEMMosaicCreation,
@ -15,7 +17,7 @@ from .helpers import (NEM_MAX_DIVISIBILITY, NEM_MAX_ENCRYPTED_PAYLOAD_SIZE,
def validate(msg: NEMSignTx): def validate(msg: NEMSignTx):
if msg.transaction is None: if msg.transaction is None:
raise ValueError('No common provided') raise ProcessError('No common provided')
_validate_single_tx(msg) _validate_single_tx(msg)
_validate_common(msg.transaction) _validate_common(msg.transaction)
@ -24,7 +26,7 @@ def validate(msg: NEMSignTx):
_validate_common(msg.multisig, True) _validate_common(msg.multisig, True)
_validate_multisig(msg.multisig, msg.transaction.network) _validate_multisig(msg.multisig, msg.transaction.network)
if not msg.multisig and msg.cosigning: if not msg.multisig and msg.cosigning:
raise ValueError('No multisig transaction to cosign') raise ProcessError('No multisig transaction to cosign')
if msg.transfer: if msg.transfer:
_validate_transfer(msg.transfer, msg.transaction.network) _validate_transfer(msg.transfer, msg.transaction.network)
@ -44,7 +46,7 @@ def validate_network(network: int) -> int:
if network is None: if network is None:
return NEM_NETWORK_MAINNET return NEM_NETWORK_MAINNET
if network not in (NEM_NETWORK_MAINNET, NEM_NETWORK_TESTNET, NEM_NETWORK_MIJIN): if network not in (NEM_NETWORK_MAINNET, NEM_NETWORK_TESTNET, NEM_NETWORK_MIJIN):
raise ValueError('Invalid NEM network') raise ProcessError('Invalid NEM network')
return network return network
@ -58,9 +60,9 @@ def _validate_single_tx(msg: NEMSignTx):
bool(msg.aggregate_modification) + \ bool(msg.aggregate_modification) + \
bool(msg.importance_transfer) bool(msg.importance_transfer)
if tx_count == 0: if tx_count == 0:
raise ValueError('No transaction provided') raise ProcessError('No transaction provided')
if tx_count > 1: if tx_count > 1:
raise ValueError('More than one transaction provided') raise ProcessError('More than one transaction provided')
def _validate_common(common: NEMTransactionCommon, inner: bool = False): def _validate_common(common: NEMTransactionCommon, inner: bool = False):
@ -75,16 +77,16 @@ def _validate_common(common: NEMTransactionCommon, inner: bool = False):
err = 'deadline' err = 'deadline'
if not inner and common.signer: if not inner and common.signer:
raise ValueError('Signer not allowed in outer transaction') raise ProcessError('Signer not allowed in outer transaction')
if inner and common.signer is None: if inner and common.signer is None:
err = 'signer' err = 'signer'
if err: if err:
if inner: if inner:
raise ValueError('No %s provided in inner transaction' % err) raise ProcessError('No %s provided in inner transaction' % err)
else: else:
raise ValueError('No %s provided' % err) raise ProcessError('No %s provided' % err)
if common.signer is not None: if common.signer is not None:
_validate_public_key(common.signer, 'Invalid signer public key in inner transaction') _validate_public_key(common.signer, 'Invalid signer public key in inner transaction')
@ -92,20 +94,20 @@ def _validate_common(common: NEMTransactionCommon, inner: bool = False):
def _validate_public_key(public_key: bytes, err_msg: str): def _validate_public_key(public_key: bytes, err_msg: str):
if not public_key: if not public_key:
raise ValueError('%s (none provided)' % err_msg) raise ProcessError('%s (none provided)' % err_msg)
if len(public_key) != NEM_PUBLIC_KEY_SIZE: if len(public_key) != NEM_PUBLIC_KEY_SIZE:
raise ValueError('%s (invalid length)' % err_msg) raise ProcessError('%s (invalid length)' % err_msg)
def _validate_importance_transfer(importance_transfer: NEMImportanceTransfer): def _validate_importance_transfer(importance_transfer: NEMImportanceTransfer):
if importance_transfer.mode is None: if importance_transfer.mode is None:
raise ValueError('No mode provided') raise ProcessError('No mode provided')
_validate_public_key(importance_transfer.public_key, 'Invalid remote account public key provided') _validate_public_key(importance_transfer.public_key, 'Invalid remote account public key provided')
def _validate_multisig(multisig: NEMTransactionCommon, network: int): def _validate_multisig(multisig: NEMTransactionCommon, network: int):
if multisig.network != network: if multisig.network != network:
raise ValueError('Inner transaction network is different') raise ProcessError('Inner transaction network is different')
_validate_public_key(multisig.signer, 'Invalid multisig signer public key provided') _validate_public_key(multisig.signer, 'Invalid multisig signer public key provided')
@ -114,124 +116,128 @@ def _validate_aggregate_modification(
creation: bool = False): creation: bool = False):
if creation and not aggregate_modification.modifications: if creation and not aggregate_modification.modifications:
raise ValueError('No modifications provided') raise ProcessError('No modifications provided')
for m in aggregate_modification.modifications: for m in aggregate_modification.modifications:
if not m.type: if not m.type:
raise ValueError('No modification type provided') raise ProcessError('No modification type provided')
if m.type not in ( if m.type not in (
NEMModificationType.CosignatoryModification_Add, NEMModificationType.CosignatoryModification_Add,
NEMModificationType.CosignatoryModification_Delete NEMModificationType.CosignatoryModification_Delete
): ):
raise ValueError('Unknown aggregate modification') raise ProcessError('Unknown aggregate modification')
if creation and m.type == NEMModificationType.CosignatoryModification_Delete: if creation and m.type == NEMModificationType.CosignatoryModification_Delete:
raise ValueError('Cannot remove cosignatory when converting account') raise ProcessError('Cannot remove cosignatory when converting account')
_validate_public_key(m.public_key, 'Invalid cosignatory public key provided') _validate_public_key(m.public_key, 'Invalid cosignatory public key provided')
def _validate_supply_change(supply_change: NEMMosaicSupplyChange): def _validate_supply_change(supply_change: NEMMosaicSupplyChange):
if supply_change.namespace is None: if supply_change.namespace is None:
raise ValueError('No namespace provided') raise ProcessError('No namespace provided')
if supply_change.mosaic is None: if supply_change.mosaic is None:
raise ValueError('No mosaic provided') raise ProcessError('No mosaic provided')
if supply_change.type is None: if supply_change.type is None:
raise ValueError('No type provided') raise ProcessError('No type provided')
elif supply_change.type not in [NEMSupplyChangeType.SupplyChange_Decrease, NEMSupplyChangeType.SupplyChange_Increase]:
raise ProcessError('Invalid supply change type')
if supply_change.delta is None: if supply_change.delta is None:
raise ValueError('No delta provided') raise ProcessError('No delta provided')
def _validate_mosaic_creation(mosaic_creation: NEMMosaicCreation, network: int): def _validate_mosaic_creation(mosaic_creation: NEMMosaicCreation, network: int):
if mosaic_creation.definition is None: if mosaic_creation.definition is None:
raise ValueError('No mosaic definition provided') raise ProcessError('No mosaic definition provided')
if mosaic_creation.sink is None: if mosaic_creation.sink is None:
raise ValueError('No creation sink provided') raise ProcessError('No creation sink provided')
if mosaic_creation.fee is None: if mosaic_creation.fee is None:
raise ValueError('No creation sink fee provided') raise ProcessError('No creation sink fee provided')
if not nem.validate_address(mosaic_creation.sink, network): if not nem.validate_address(mosaic_creation.sink, network):
raise ValueError('Invalid creation sink address') raise ProcessError('Invalid creation sink address')
if mosaic_creation.definition.name is not None: if mosaic_creation.definition.name is not None:
raise ValueError('Name not allowed in mosaic creation transactions') raise ProcessError('Name not allowed in mosaic creation transactions')
if mosaic_creation.definition.ticker is not None: if mosaic_creation.definition.ticker is not None:
raise ValueError('Ticker not allowed in mosaic creation transactions') raise ProcessError('Ticker not allowed in mosaic creation transactions')
if mosaic_creation.definition.networks: if mosaic_creation.definition.networks:
raise ValueError('Networks not allowed in mosaic creation transactions') raise ProcessError('Networks not allowed in mosaic creation transactions')
if mosaic_creation.definition.namespace is None: if mosaic_creation.definition.namespace is None:
raise ValueError('No mosaic namespace provided') raise ProcessError('No mosaic namespace provided')
if mosaic_creation.definition.mosaic is None: if mosaic_creation.definition.mosaic is None:
raise ValueError('No mosaic name provided') raise ProcessError('No mosaic name provided')
if mosaic_creation.definition.supply is not None and mosaic_creation.definition.divisibility is None: if mosaic_creation.definition.supply is not None and mosaic_creation.definition.divisibility is None:
raise ValueError('Definition divisibility needs to be provided when supply is') raise ProcessError('Definition divisibility needs to be provided when supply is')
if mosaic_creation.definition.supply is None and mosaic_creation.definition.divisibility is not None: if mosaic_creation.definition.supply is None and mosaic_creation.definition.divisibility is not None:
raise ValueError('Definition supply needs to be provided when divisibility is') raise ProcessError('Definition supply needs to be provided when divisibility is')
if mosaic_creation.definition.levy is not None: if mosaic_creation.definition.levy is not None:
if mosaic_creation.definition.fee is None: if mosaic_creation.definition.fee is None:
raise ValueError('No levy fee provided') raise ProcessError('No levy fee provided')
if mosaic_creation.definition.levy_address is None: if mosaic_creation.definition.levy_address is None:
raise ValueError('No levy address provided') raise ProcessError('No levy address provided')
if mosaic_creation.definition.levy_namespace is None: if mosaic_creation.definition.levy_namespace is None:
raise ValueError('No levy namespace provided') raise ProcessError('No levy namespace provided')
if mosaic_creation.definition.levy_mosaic is None: if mosaic_creation.definition.levy_mosaic is None:
raise ValueError('No levy mosaic name provided') raise ProcessError('No levy mosaic name provided')
if mosaic_creation.definition.divisibility is None: if mosaic_creation.definition.divisibility is None:
raise ValueError('No divisibility provided') raise ProcessError('No divisibility provided')
if mosaic_creation.definition.supply is None: if mosaic_creation.definition.supply is None:
raise ValueError('No supply provided') raise ProcessError('No supply provided')
if mosaic_creation.definition.mutable_supply is None: if mosaic_creation.definition.mutable_supply is None:
raise ValueError('No supply mutability provided') raise ProcessError('No supply mutability provided')
if mosaic_creation.definition.transferable is None: if mosaic_creation.definition.transferable is None:
raise ValueError('No mosaic transferability provided') raise ProcessError('No mosaic transferability provided')
if mosaic_creation.definition.description is None: if mosaic_creation.definition.description is None:
raise ValueError('No description provided') raise ProcessError('No description provided')
if mosaic_creation.definition.divisibility > NEM_MAX_DIVISIBILITY: if mosaic_creation.definition.divisibility > NEM_MAX_DIVISIBILITY:
raise ValueError('Invalid divisibility provided') raise ProcessError('Invalid divisibility provided')
if mosaic_creation.definition.supply > NEM_MAX_SUPPLY: if mosaic_creation.definition.supply > NEM_MAX_SUPPLY:
raise ValueError('Invalid supply provided') raise ProcessError('Invalid supply provided')
if not nem.validate_address(mosaic_creation.definition.levy_address, network): if not nem.validate_address(mosaic_creation.definition.levy_address, network):
raise ValueError('Invalid levy address') raise ProcessError('Invalid levy address')
def _validate_provision_namespace(provision_namespace: NEMProvisionNamespace, network: int): def _validate_provision_namespace(provision_namespace: NEMProvisionNamespace, network: int):
if provision_namespace.namespace is None: if provision_namespace.namespace is None:
raise ValueError('No namespace provided') raise ProcessError('No namespace provided')
if provision_namespace.sink is None: if provision_namespace.sink is None:
raise ValueError('No rental sink provided') raise ProcessError('No rental sink provided')
if provision_namespace.fee is None: if provision_namespace.fee is None:
raise ValueError('No rental sink fee provided') raise ProcessError('No rental sink fee provided')
if not nem.validate_address(provision_namespace.sink, network): if not nem.validate_address(provision_namespace.sink, network):
raise ValueError('Invalid rental sink address') raise ProcessError('Invalid rental sink address')
def _validate_transfer(transfer: NEMTransfer, network: int): def _validate_transfer(transfer: NEMTransfer, network: int):
if transfer.recipient is None: if transfer.recipient is None:
raise ValueError('No recipient provided') raise ProcessError('No recipient provided')
if transfer.amount is None: if transfer.amount is None:
raise ValueError('No amount provided') raise ProcessError('No amount provided')
if transfer.public_key is not None: if transfer.public_key is not None:
_validate_public_key(transfer.public_key, 'Invalid recipient public key') _validate_public_key(transfer.public_key, 'Invalid recipient public key')
if transfer.payload is None:
raise ProcessError('Public key provided but no payload to encrypt')
if transfer.payload: if transfer.payload:
if len(transfer.payload) > NEM_MAX_PLAIN_PAYLOAD_SIZE: if len(transfer.payload) > NEM_MAX_PLAIN_PAYLOAD_SIZE:
raise ValueError('Payload too large') raise ProcessError('Payload too large')
if transfer.public_key and len(transfer.payload) > NEM_MAX_ENCRYPTED_PAYLOAD_SIZE: if transfer.public_key and len(transfer.payload) > NEM_MAX_ENCRYPTED_PAYLOAD_SIZE:
raise ValueError('Payload too large') raise ProcessError('Payload too large')
if not nem.validate_address(transfer.recipient, network): if not nem.validate_address(transfer.recipient, network):
raise ValueError('Invalid recipient address') raise ProcessError('Invalid recipient address')
for m in transfer.mosaics: for m in transfer.mosaics:
if m.namespace is None: if m.namespace is None:
raise ValueError('No mosaic namespace provided') raise ProcessError('No mosaic namespace provided')
if m.mosaic is None: if m.mosaic is None:
raise ValueError('No mosaic name provided') raise ProcessError('No mosaic name provided')
if m.quantity is None: if m.quantity is None:
raise ValueError('No mosaic quantity provided') raise ProcessError('No mosaic quantity provided')