diff --git a/rust/chains/tw_greenfield/src/address.rs b/rust/chains/tw_greenfield/src/address.rs index d0d4d794448..94b9565b17a 100644 --- a/rust/chains/tw_greenfield/src/address.rs +++ b/rust/chains/tw_greenfield/src/address.rs @@ -6,7 +6,7 @@ use serde::Serialize; use std::fmt; use std::str::FromStr; use tw_coin_entry::coin_entry::CoinAddress; -use tw_coin_entry::error::AddressError; +use tw_coin_entry::error::prelude::*; use tw_cosmos_sdk::address::CosmosAddress; use tw_evm::address::Address as EthereumAddress; use tw_keypair::ecdsa::secp256k1; diff --git a/rust/chains/tw_greenfield/src/compiler.rs b/rust/chains/tw_greenfield/src/compiler.rs index a37ac39a7f2..e9db4725199 100644 --- a/rust/chains/tw_greenfield/src/compiler.rs +++ b/rust/chains/tw_greenfield/src/compiler.rs @@ -11,7 +11,7 @@ use std::borrow::Cow; use tw_coin_entry::coin_context::CoinContext; use tw_coin_entry::coin_entry::{PublicKeyBytes, SignatureBytes}; use tw_coin_entry::common::compile_input::SingleSignaturePubkey; -use tw_coin_entry::error::{SigningError, SigningErrorType, SigningResult}; +use tw_coin_entry::error::prelude::*; use tw_coin_entry::signing_output_error; use tw_cosmos_sdk::modules::broadcast_msg::{BroadcastMode, BroadcastMsg}; use tw_cosmos_sdk::modules::serializer::json_serializer::JsonSerializer; @@ -72,8 +72,12 @@ impl GreenfieldCompiler { } = SingleSignaturePubkey::from_sign_pubkey_list(signatures, public_keys)?; let public_key_params = None; - let public_key = GreenfieldPublicKey::from_bytes(coin, &public_key, public_key_params)?; - let signature = GreenfieldSignature::try_from(raw_signature.as_slice())?; + let public_key = GreenfieldPublicKey::from_bytes(coin, &public_key, public_key_params) + .into_tw() + .context("Invalid provided public key")?; + let signature = GreenfieldSignature::try_from(raw_signature.as_slice()) + .into_tw() + .context("Invalid provided signature")?; let signature_bytes = signature.to_vec(); // Set the public key. It will be used to construct a signer info. @@ -91,7 +95,8 @@ impl GreenfieldCompiler { signature_bytes.clone(), ); let signature_json = serde_json::to_string(&[signature_json]) - .map_err(|_| SigningError(SigningErrorType::Error_internal))?; + .tw_err(|_| SigningErrorType::Error_internal) + .context("Error serializing signatures as JSON")?; Ok(Proto::SigningOutput { signature: Cow::from(signature_bytes), diff --git a/rust/chains/tw_greenfield/src/entry.rs b/rust/chains/tw_greenfield/src/entry.rs index 52ea07203f4..01ee90e50ae 100644 --- a/rust/chains/tw_greenfield/src/entry.rs +++ b/rust/chains/tw_greenfield/src/entry.rs @@ -9,7 +9,7 @@ use std::str::FromStr; use tw_coin_entry::coin_context::CoinContext; use tw_coin_entry::coin_entry::{CoinEntry, PublicKeyBytes, SignatureBytes}; use tw_coin_entry::derivation::Derivation; -use tw_coin_entry::error::{AddressError, AddressResult}; +use tw_coin_entry::error::prelude::*; use tw_coin_entry::modules::json_signer::NoJsonSigner; use tw_coin_entry::modules::message_signer::NoMessageSigner; use tw_coin_entry::modules::plan_builder::NoPlanBuilder; diff --git a/rust/chains/tw_greenfield/src/modules/eip712_signer.rs b/rust/chains/tw_greenfield/src/modules/eip712_signer.rs index 7b9fcb34f05..c5ead1f19de 100644 --- a/rust/chains/tw_greenfield/src/modules/eip712_signer.rs +++ b/rust/chains/tw_greenfield/src/modules/eip712_signer.rs @@ -7,10 +7,10 @@ use crate::eip712_types::{ }; use crate::transaction::GreenfieldUnsignedTransaction; use std::collections::BTreeMap; -use tw_coin_entry::error::{SigningError, SigningErrorType, SigningResult}; +use tw_coin_entry::error::prelude::*; use tw_evm::message::eip712::eip712_message::Eip712Message; use tw_evm::message::eip712::message_types::MessageTypesBuilder; -use tw_evm::message::EthMessage; +use tw_evm::message::{to_signing, EthMessage}; use tw_hash::H256; use tw_number::U256; @@ -65,15 +65,17 @@ impl Eip712Signer { let msg_to_sign = Eip712Message { types: types_builder.build(), domain: serde_json::to_value(domain) - .map_err(|_| SigningError(SigningErrorType::Error_internal))?, + .tw_err(|_| SigningErrorType::Error_internal) + .context("Error serializing EIP712Domain as JSON")?, primary_type: Eip712Transaction::TYPE_NAME.to_string(), message: serde_json::to_value(tx_to_sign) - .map_err(|_| SigningError(SigningErrorType::Error_internal))?, + .tw_err(|_| SigningErrorType::Error_internal) + .context("Error serializing EIP712 message payload as JSON")?, }; - let tx_hash = msg_to_sign.hash()?; - let eip712_tx = serde_json::to_string(&msg_to_sign) - .map_err(|_| SigningError(SigningErrorType::Error_internal))?; + let tx_hash = msg_to_sign.hash().map_err(to_signing)?; + let eip712_tx = + serde_json::to_string(&msg_to_sign).tw_err(|_| SigningErrorType::Error_internal)?; Ok(Eip712TxPreimage { eip712_tx, tx_hash }) } diff --git a/rust/chains/tw_greenfield/src/modules/tx_builder.rs b/rust/chains/tw_greenfield/src/modules/tx_builder.rs index 2e0951c8cce..6a12bb2aa6e 100644 --- a/rust/chains/tw_greenfield/src/modules/tx_builder.rs +++ b/rust/chains/tw_greenfield/src/modules/tx_builder.rs @@ -11,7 +11,7 @@ use crate::transaction::{ }; use std::str::FromStr; use tw_coin_entry::coin_context::CoinContext; -use tw_coin_entry::error::{SigningError, SigningErrorType, SigningResult}; +use tw_coin_entry::error::prelude::*; use tw_cosmos_sdk::public_key::CosmosPublicKey; use tw_cosmos_sdk::transaction::{Coin, SignerInfo}; use tw_misc::traits::OptionalEmpty; @@ -36,11 +36,13 @@ impl TxBuilder { let fee = input .fee .as_ref() - .ok_or(SigningError(SigningErrorType::Error_wrong_fee))?; + .or_tw_err(SigningErrorType::Error_wrong_fee) + .context("No 'fee' specified")?; let fee = Self::fee_from_proto(fee, &signer)?; let eth_chain_id = U256::from_str(&input.eth_chain_id) - .map_err(|_| SigningError(SigningErrorType::Error_invalid_params))?; + .tw_err(|_| SigningErrorType::Error_invalid_params) + .context("Invalid ETH chain ID")?; Ok(GreenfieldUnsignedTransaction { signer, @@ -89,7 +91,9 @@ impl TxBuilder { } fn coin_from_proto(input: &Proto::Amount<'_>) -> SigningResult { - let amount = U256::from_str(&input.amount)?; + let amount = U256::from_str(&input.amount) + .into_tw() + .context("Invalid amount: expected uint256 decimal-string")?; Ok(Coin { amount, denom: input.denom.to_string(), @@ -98,7 +102,8 @@ impl TxBuilder { fn tx_body_from_proto(input: &Proto::SigningInput<'_>) -> SigningResult { if input.messages.is_empty() { - return Err(SigningError(SigningErrorType::Error_invalid_params)); + return SigningError::err(SigningErrorType::Error_invalid_params) + .context("No transaction messages provided"); } let messages = input @@ -122,7 +127,8 @@ impl TxBuilder { MessageEnum::bridge_transfer_out(ref transfer_out) => { Self::bridge_transfer_out_from_proto(transfer_out) }, - MessageEnum::None => Err(SigningError(SigningErrorType::Error_invalid_params)), + MessageEnum::None => SigningError::err(SigningErrorType::Error_invalid_params) + .context("No message type provided"), } } @@ -139,8 +145,12 @@ impl TxBuilder { .collect::>()?; let msg = SendMessage { custom_type_prefix: send.type_prefix.to_string().empty_or_some(), - from_address: GreenfieldAddress::from_str(&send.from_address)?, - to_address: GreenfieldAddress::from_str(&send.to_address)?, + from_address: GreenfieldAddress::from_str(&send.from_address) + .into_tw() + .context("Invalid sender address")?, + to_address: GreenfieldAddress::from_str(&send.to_address) + .into_tw() + .context("Invalid recipient address")?, amount: amounts, }; Ok(Box::new(GreenfieldSendMessage(msg))) @@ -154,13 +164,18 @@ impl TxBuilder { let amount = transfer_out .amount .as_ref() - .ok_or(SigningError(SigningErrorType::Error_wrong_fee))?; + .or_tw_err(SigningErrorType::Error_invalid_params) + .context("No transfer amount specified")?; let msg = GreenfieldTransferOut { custom_type_prefix: transfer_out.type_prefix.to_string().empty_or_some(), amount: Self::coin_from_proto(amount)?, - from: GreenfieldAddress::from_str(&transfer_out.from_address)?, - to: GreenfieldAddress::from_str(&transfer_out.to_address)?, + from: GreenfieldAddress::from_str(&transfer_out.from_address) + .into_tw() + .context("Invalid sender address")?, + to: GreenfieldAddress::from_str(&transfer_out.to_address) + .into_tw() + .context("Invalid recipient address")?, }; Ok(Box::new(msg)) } diff --git a/rust/chains/tw_greenfield/src/signer.rs b/rust/chains/tw_greenfield/src/signer.rs index 6b0d2c88e28..afde1ed2747 100644 --- a/rust/chains/tw_greenfield/src/signer.rs +++ b/rust/chains/tw_greenfield/src/signer.rs @@ -7,7 +7,7 @@ use crate::modules::eip712_signer::{Eip712Signer, Eip712TxPreimage}; use crate::modules::tx_builder::TxBuilder; use std::borrow::Cow; use tw_coin_entry::coin_context::CoinContext; -use tw_coin_entry::error::SigningResult; +use tw_coin_entry::error::prelude::*; use tw_coin_entry::signing_output_error; use tw_keypair::ecdsa::secp256k1; use tw_keypair::traits::{KeyPairTrait, SigningKeyTrait}; diff --git a/rust/chains/tw_greenfield/src/transaction/message/send_order.rs b/rust/chains/tw_greenfield/src/transaction/message/send_order.rs index 8d9ebc62ccf..7733629ba9d 100644 --- a/rust/chains/tw_greenfield/src/transaction/message/send_order.rs +++ b/rust/chains/tw_greenfield/src/transaction/message/send_order.rs @@ -5,7 +5,7 @@ use crate::address::GreenfieldAddress; use crate::transaction::message::type_msg_amount::TypeMsgAmount; use crate::transaction::message::GreenfieldMessage; -use tw_coin_entry::error::SigningResult; +use tw_coin_entry::error::prelude::*; use tw_cosmos_sdk::proto::cosmos as CosmosProto; use tw_cosmos_sdk::transaction::message::cosmos_bank_message::SendMessage; use tw_cosmos_sdk::transaction::message::{ diff --git a/rust/chains/tw_greenfield/src/transaction/message/transfer_out.rs b/rust/chains/tw_greenfield/src/transaction/message/transfer_out.rs index f626db7d5a7..00a01116db5 100644 --- a/rust/chains/tw_greenfield/src/transaction/message/transfer_out.rs +++ b/rust/chains/tw_greenfield/src/transaction/message/transfer_out.rs @@ -6,7 +6,7 @@ use crate::address::GreenfieldAddress; use crate::transaction::message::type_msg_amount::TypeMsgAmount; use crate::transaction::message::GreenfieldMessage; use serde::Serialize; -use tw_coin_entry::error::SigningResult; +use tw_coin_entry::error::prelude::*; use tw_cosmos_sdk::modules::serializer::protobuf_serializer::build_coin; use tw_cosmos_sdk::proto::greenfield as GreenfieldProto; use tw_cosmos_sdk::transaction::message::{