diff --git a/rust/chains/tw_binance/src/address.rs b/rust/chains/tw_binance/src/address.rs index 3c718a97bd8..45b00e26bb4 100644 --- a/rust/chains/tw_binance/src/address.rs +++ b/rust/chains/tw_binance/src/address.rs @@ -9,7 +9,7 @@ use tw_bech32_address::bech32_prefix::Bech32Prefix; use tw_bech32_address::Bech32Address; use tw_coin_entry::coin_context::CoinContext; use tw_coin_entry::coin_entry::CoinAddress; -use tw_coin_entry::error::{AddressError, AddressResult}; +use tw_coin_entry::error::prelude::*; use tw_cosmos_sdk::address::CosmosAddress; use tw_keypair::tw::PublicKey; use tw_memory::Data; diff --git a/rust/chains/tw_binance/src/compiler.rs b/rust/chains/tw_binance/src/compiler.rs index 754cdd854ed..400c37e36ec 100644 --- a/rust/chains/tw_binance/src/compiler.rs +++ b/rust/chains/tw_binance/src/compiler.rs @@ -11,7 +11,7 @@ use crate::transaction::SignerInfo; 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::serializer::json_serializer::JsonSerializer; use tw_cosmos_sdk::public_key::secp256k1::Secp256PublicKey; @@ -89,8 +89,8 @@ impl BinanceCompiler { let encoded_tx = BinanceAminoSerializer::serialize_signed_tx(&signed_tx)?; - let signature_json = serde_json::to_string(&signature_json) - .map_err(|_| SigningError(SigningErrorType::Error_internal))?; + let signature_json = + serde_json::to_string(&signature_json).tw_err(|_| SigningErrorType::Error_internal)?; Ok(Proto::SigningOutput { encoded: encoded_tx.into(), signature: signature_bytes.into(), diff --git a/rust/chains/tw_binance/src/entry.rs b/rust/chains/tw_binance/src/entry.rs index f7798bd0f39..cd552881c9d 100644 --- a/rust/chains/tw_binance/src/entry.rs +++ b/rust/chains/tw_binance/src/entry.rs @@ -11,7 +11,7 @@ use tw_bech32_address::bech32_prefix::Bech32Prefix; 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::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_binance/src/modules/preimager.rs b/rust/chains/tw_binance/src/modules/preimager.rs index ba881f8beae..45e2dacdf53 100644 --- a/rust/chains/tw_binance/src/modules/preimager.rs +++ b/rust/chains/tw_binance/src/modules/preimager.rs @@ -3,7 +3,7 @@ // Copyright © 2017 Trust Wallet. use crate::transaction::UnsignedTransaction; -use tw_coin_entry::error::{SigningError, SigningErrorType, SigningResult}; +use tw_coin_entry::error::prelude::*; use tw_hash::{sha2, H256}; pub struct JsonTxPreimage { @@ -15,8 +15,8 @@ pub struct JsonPreimager; impl JsonPreimager { pub fn preimage_hash(unsigned: &UnsignedTransaction) -> SigningResult { - let encoded_tx = serde_json::to_string(unsigned) - .map_err(|_| SigningError(SigningErrorType::Error_internal))?; + let encoded_tx = + serde_json::to_string(unsigned).tw_err(|_| SigningErrorType::Error_internal)?; let tx_hash = sha2::sha256(encoded_tx.as_bytes()); let tx_hash = H256::try_from(tx_hash.as_slice()).expect("sha256 must return 32 bytes"); Ok(JsonTxPreimage { diff --git a/rust/chains/tw_binance/src/modules/serializer.rs b/rust/chains/tw_binance/src/modules/serializer.rs index cb1ea5ff4d6..88ec0ad22f3 100644 --- a/rust/chains/tw_binance/src/modules/serializer.rs +++ b/rust/chains/tw_binance/src/modules/serializer.rs @@ -5,7 +5,7 @@ use crate::amino::AminoEncoder; use crate::transaction::SignedTransaction; use std::borrow::Cow; -use tw_coin_entry::error::{SigningError, SigningErrorType, SigningResult}; +use tw_coin_entry::error::prelude::*; use tw_cosmos_sdk::public_key::CosmosPublicKey; use tw_memory::Data; use tw_misc::traits::ToBytesVec; @@ -58,6 +58,6 @@ impl BinanceAminoSerializer { sequence: signed.unsigned.sequence, }; // There is no need to use Amino encoding here as the prefix is empty. - serialize(&sign_msg).map_err(|_| SigningError(SigningErrorType::Error_internal)) + serialize(&sign_msg).tw_err(|_| SigningErrorType::Error_internal) } } diff --git a/rust/chains/tw_binance/src/modules/tx_builder.rs b/rust/chains/tw_binance/src/modules/tx_builder.rs index 4b6fb2ebbea..ce1340d63c0 100644 --- a/rust/chains/tw_binance/src/modules/tx_builder.rs +++ b/rust/chains/tw_binance/src/modules/tx_builder.rs @@ -6,7 +6,7 @@ use crate::transaction::message::{BinanceMessageEnum, TWBinanceProto}; use crate::transaction::UnsignedTransaction; use std::borrow::Cow; use tw_coin_entry::coin_context::CoinContext; -use tw_coin_entry::error::{SigningError, SigningErrorType, SigningResult}; +use tw_coin_entry::error::prelude::*; use tw_proto::Binance::Proto; pub struct TxBuilder; @@ -32,7 +32,8 @@ impl TxBuilder { unsigned: &UnsignedTransaction, ) -> SigningResult> { if unsigned.msgs.len() != 1 { - return Err(SigningError(SigningErrorType::Error_invalid_params)); + return SigningError::err(SigningErrorType::Error_invalid_params) + .context("Expected exactly one Transaction Message"); } let msg = unsigned .msgs diff --git a/rust/chains/tw_binance/src/modules/wallet_connect/connector.rs b/rust/chains/tw_binance/src/modules/wallet_connect/connector.rs index a1e60b7da16..e66b4688a2f 100644 --- a/rust/chains/tw_binance/src/modules/wallet_connect/connector.rs +++ b/rust/chains/tw_binance/src/modules/wallet_connect/connector.rs @@ -5,7 +5,7 @@ use crate::modules::tx_builder::TxBuilder; use crate::modules::wallet_connect::types::SignAminoRequest; use tw_coin_entry::coin_context::CoinContext; -use tw_coin_entry::error::{SigningError, SigningErrorType, SigningResult}; +use tw_coin_entry::error::prelude::*; use tw_coin_entry::modules::wallet_connector::WalletConnector; use tw_coin_entry::signing_output_error; use tw_proto::WalletConnect::Proto::{ @@ -32,7 +32,8 @@ impl BinanceWalletConnector { ) -> SigningResult> { match request.method { WCProto::Method::CosmosSignAmino => Self::parse_sign_amino_request(coin, request), - _ => Err(SigningError(SigningErrorType::Error_not_supported)), + _ => SigningError::err(SigningErrorType::Error_not_supported) + .context("Unknown WalletConnect method"), } } @@ -41,7 +42,8 @@ impl BinanceWalletConnector { request: WCProto::ParseRequestInput<'_>, ) -> SigningResult> { let amino_req: SignAminoRequest = serde_json::from_str(&request.payload) - .map_err(|_| SigningError(SigningErrorType::Error_input_parse))?; + .tw_err(|_| SigningErrorType::Error_input_parse) + .context("Error deserializing WalletConnect signAmino request as JSON")?; // Parse a `SigningInput` from the given `signDoc`. let signing_input = TxBuilder::unsigned_tx_to_proto(&amino_req.sign_doc)?; diff --git a/rust/chains/tw_binance/src/signer.rs b/rust/chains/tw_binance/src/signer.rs index b17553e067e..86cb5da5b4b 100644 --- a/rust/chains/tw_binance/src/signer.rs +++ b/rust/chains/tw_binance/src/signer.rs @@ -9,7 +9,7 @@ use crate::modules::tx_builder::TxBuilder; use crate::signature::BinanceSignature; use crate::transaction::SignerInfo; use tw_coin_entry::coin_context::CoinContext; -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::serializer::json_serializer::JsonSerializer; use tw_cosmos_sdk::public_key::secp256k1::Secp256PublicKey; @@ -54,8 +54,8 @@ impl BinanceSigner { }); let encoded_tx = BinanceAminoSerializer::serialize_signed_tx(&signed_tx)?; - let signature_json = serde_json::to_string(&signature_json) - .map_err(|_| SigningError(SigningErrorType::Error_internal))?; + let signature_json = + serde_json::to_string(&signature_json).tw_err(|_| SigningErrorType::Error_internal)?; Ok(Proto::SigningOutput { encoded: encoded_tx.into(), signature: signature_bytes.into(), diff --git a/rust/chains/tw_binance/src/transaction/message/htlt_order.rs b/rust/chains/tw_binance/src/transaction/message/htlt_order.rs index e55e3e4b3e5..902ae019e82 100644 --- a/rust/chains/tw_binance/src/transaction/message/htlt_order.rs +++ b/rust/chains/tw_binance/src/transaction/message/htlt_order.rs @@ -8,7 +8,7 @@ use crate::transaction::message::{BinanceMessage, TWBinanceProto, Token}; use serde::{Deserialize, Serialize}; use tw_coin_entry::coin_context::CoinContext; use tw_coin_entry::coin_entry::CoinAddress; -use tw_coin_entry::error::SigningResult; +use tw_coin_entry::error::prelude::*; use tw_encoding::hex::as_hex; use tw_memory::Data; use tw_proto::Binance::Proto; diff --git a/rust/chains/tw_binance/src/transaction/message/mod.rs b/rust/chains/tw_binance/src/transaction/message/mod.rs index 5974ba50400..0879bcf5b3e 100644 --- a/rust/chains/tw_binance/src/transaction/message/mod.rs +++ b/rust/chains/tw_binance/src/transaction/message/mod.rs @@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize, Serializer}; use tw_coin_entry::coin_context::CoinContext; -use tw_coin_entry::error::{SigningError, SigningErrorType, SigningResult}; +use tw_coin_entry::error::prelude::*; use tw_memory::Data; use tw_proto::Binance::Proto::{self, mod_SigningInput::OneOforder_oneof as BinanceMessageProto}; @@ -138,7 +138,7 @@ impl TWBinanceProto for BinanceMessageEnum { side_chain_delegate::StakeMigrationOrder::from_tw_proto(coin, order) .map(BinanceMessageEnum::StakeMigrationOrder) }, - BinanceMessageProto::None => Err(SigningError(SigningErrorType::Error_invalid_params)), + BinanceMessageProto::None => SigningError::err(SigningErrorType::Error_invalid_params), } } diff --git a/rust/chains/tw_binance/src/transaction/message/send_order.rs b/rust/chains/tw_binance/src/transaction/message/send_order.rs index ef1b5107134..6ffd7d69be2 100644 --- a/rust/chains/tw_binance/src/transaction/message/send_order.rs +++ b/rust/chains/tw_binance/src/transaction/message/send_order.rs @@ -8,7 +8,7 @@ use crate::transaction::message::{BinanceMessage, TWBinanceProto, Token}; use serde::{Deserialize, Serialize}; use tw_coin_entry::coin_context::CoinContext; use tw_coin_entry::coin_entry::CoinAddress; -use tw_coin_entry::error::SigningResult; +use tw_coin_entry::error::prelude::*; use tw_memory::Data; use tw_proto::Binance::Proto; diff --git a/rust/chains/tw_binance/src/transaction/message/side_chain_delegate.rs b/rust/chains/tw_binance/src/transaction/message/side_chain_delegate.rs index 690d78def18..4684056cede 100644 --- a/rust/chains/tw_binance/src/transaction/message/side_chain_delegate.rs +++ b/rust/chains/tw_binance/src/transaction/message/side_chain_delegate.rs @@ -8,7 +8,7 @@ use crate::transaction::message::{BinanceMessage, TWBinanceProto, Token}; use serde::{Deserialize, Serialize}; use tw_coin_entry::coin_context::CoinContext; use tw_coin_entry::coin_entry::CoinAddress; -use tw_coin_entry::error::{SigningError, SigningErrorType, SigningResult}; +use tw_coin_entry::error::prelude::*; use tw_evm::address::Address as EthereumAddress; use tw_memory::Data; use tw_misc::serde::Typed; @@ -52,7 +52,7 @@ impl TWBinanceProto for SideDelegateOrder { let delegation = msg .delegation .as_ref() - .ok_or(SigningError(SigningErrorType::Error_invalid_params))?; + .or_tw_err(SigningErrorType::Error_invalid_params)?; let value = SideDelegateOrderValue { delegator_addr, @@ -118,7 +118,7 @@ impl TWBinanceProto for SideRedelegateOrder { let amount = msg .amount .as_ref() - .ok_or(SigningError(SigningErrorType::Error_invalid_params))?; + .or_tw_err(SigningErrorType::Error_invalid_params)?; let value = SideRedelegateOrderValue { delegator_addr, @@ -182,7 +182,7 @@ impl TWBinanceProto for SideUndelegateOrder { let amount = msg .amount .as_ref() - .ok_or(SigningError(SigningErrorType::Error_invalid_params))?; + .or_tw_err(SigningErrorType::Error_invalid_params)?; let value = SideUndelegateOrderValue { delegator_addr, @@ -248,7 +248,7 @@ impl TWBinanceProto for StakeMigrationOrder { let amount = msg .amount .as_ref() - .ok_or(SigningError(SigningErrorType::Error_invalid_params))?; + .or_tw_err(SigningErrorType::Error_invalid_params)?; let value = StakeMigrationOrderValue { amount: Token::from_tw_proto(amount), diff --git a/rust/chains/tw_binance/src/transaction/message/time_lock_order.rs b/rust/chains/tw_binance/src/transaction/message/time_lock_order.rs index 2647d6553cb..9e2e4e21e8c 100644 --- a/rust/chains/tw_binance/src/transaction/message/time_lock_order.rs +++ b/rust/chains/tw_binance/src/transaction/message/time_lock_order.rs @@ -8,7 +8,7 @@ use crate::transaction::message::{BinanceMessage, TWBinanceProto, Token}; use serde::{Deserialize, Serialize}; use tw_coin_entry::coin_context::CoinContext; use tw_coin_entry::coin_entry::CoinAddress; -use tw_coin_entry::error::SigningResult; +use tw_coin_entry::error::prelude::*; use tw_memory::Data; use tw_proto::Binance::Proto; diff --git a/rust/chains/tw_binance/src/transaction/message/token_order.rs b/rust/chains/tw_binance/src/transaction/message/token_order.rs index ecff5236773..ec88ba88e26 100644 --- a/rust/chains/tw_binance/src/transaction/message/token_order.rs +++ b/rust/chains/tw_binance/src/transaction/message/token_order.rs @@ -8,7 +8,7 @@ use crate::transaction::message::{BinanceMessage, TWBinanceProto}; use serde::{Deserialize, Serialize}; use tw_coin_entry::coin_context::CoinContext; use tw_coin_entry::coin_entry::CoinAddress; -use tw_coin_entry::error::SigningResult; +use tw_coin_entry::error::prelude::*; use tw_memory::Data; use tw_proto::Binance::Proto; diff --git a/rust/chains/tw_binance/src/transaction/message/trade_order.rs b/rust/chains/tw_binance/src/transaction/message/trade_order.rs index 5aced178ea6..0add4dcd87b 100644 --- a/rust/chains/tw_binance/src/transaction/message/trade_order.rs +++ b/rust/chains/tw_binance/src/transaction/message/trade_order.rs @@ -8,7 +8,7 @@ use crate::transaction::message::{BinanceMessage, TWBinanceProto}; use serde::{Deserialize, Serialize}; use tw_coin_entry::coin_context::CoinContext; use tw_coin_entry::coin_entry::CoinAddress; -use tw_coin_entry::error::{SigningError, SigningErrorType, SigningResult}; +use tw_coin_entry::error::prelude::*; use tw_memory::Data; use tw_proto::Binance::Proto; @@ -61,7 +61,7 @@ impl TWBinanceProto for NewTradeOrder { fn from_tw_proto(coin: &dyn CoinContext, msg: &Self::Proto<'_>) -> SigningResult { let order_type = OrderType::from_repr(msg.ordertype) - .ok_or(SigningError(SigningErrorType::Error_invalid_params))?; + .or_tw_err(SigningErrorType::Error_invalid_params)?; let sender = BinanceAddress::from_key_hash_with_coin(coin, msg.sender.to_vec())?; Ok(NewTradeOrder { diff --git a/rust/chains/tw_binance/src/transaction/message/tranfer_out_order.rs b/rust/chains/tw_binance/src/transaction/message/tranfer_out_order.rs index ae8aabff1ca..30dfef73f56 100644 --- a/rust/chains/tw_binance/src/transaction/message/tranfer_out_order.rs +++ b/rust/chains/tw_binance/src/transaction/message/tranfer_out_order.rs @@ -8,7 +8,7 @@ use crate::transaction::message::{BinanceMessage, TWBinanceProto, Token}; use serde::{Deserialize, Serialize}; use tw_coin_entry::coin_context::CoinContext; use tw_coin_entry::coin_entry::CoinAddress; -use tw_coin_entry::error::{SigningError, SigningErrorType, SigningResult}; +use tw_coin_entry::error::prelude::*; use tw_evm::address::Address as EthereumAddress; use tw_hash::H160; use tw_memory::Data; @@ -41,14 +41,14 @@ impl TWBinanceProto for TransferOutOrder { fn from_tw_proto(coin: &dyn CoinContext, msg: &Self::Proto<'_>) -> SigningResult { let from = BinanceAddress::from_key_hash_with_coin(coin, msg.from.to_vec())?; - let to_bytes = H160::try_from(msg.to.as_ref()) - .map_err(|_| SigningError(SigningErrorType::Error_invalid_address))?; + let to_bytes = + H160::try_from(msg.to.as_ref()).tw_err(|_| SigningErrorType::Error_invalid_address)?; let to = EthereumAddress::from_bytes(to_bytes); let amount_proto = msg .amount .as_ref() - .ok_or(SigningError(SigningErrorType::Error_invalid_params))?; + .or_tw_err(SigningErrorType::Error_invalid_params)?; Ok(TransferOutOrder { from,