Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Misc]: Improve error handling by adding contexts #3806

Merged
merged 24 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
16a1001
[misc]: Add TWError, TWResult, TWErrorKind, and traits
satoshiotomakan Apr 12, 2024
cb83c2d
[misc]: Return SigningError, SigningResult back, make TWError<E> generic
satoshiotomakan Apr 15, 2024
d659c69
[misc]: Add `tw_aptos` error contexts
satoshiotomakan Apr 15, 2024
1725817
[misc]: Add `tw_sui` error contexts
satoshiotomakan Apr 15, 2024
18fe063
[misc]: Simplify `TWError` casts, add tests
satoshiotomakan Apr 15, 2024
b8a61cf
[misc]: Add contexts in `tw_cosmos_sdk`
satoshiotomakan Apr 17, 2024
647c05e
[misc]: Add contexts in `tw_evm`
satoshiotomakan Apr 18, 2024
78fe3e8
[misc]: Fix compilation of `tw_ethereum`, `tw_ronin`, `tw_bitcoin`, `…
satoshiotomakan Apr 19, 2024
5bc7603
[misc]: Add error contexts in `tw_solana`
satoshiotomakan Apr 22, 2024
3ed3211
[misc]: Fix `tw_bitcoin` compilation errors
satoshiotomakan Apr 22, 2024
b0a200a
[misc]: Add error contexts to `tw_greenfield`
satoshiotomakan Apr 22, 2024
d7150cc
[misc]: Fix other compilation errors
satoshiotomakan Apr 22, 2024
d34e26a
Merge branch 'master' into s/rust-tw-error
satoshiotomakan Apr 22, 2024
8b03cab
[CI] Trigger CI
satoshiotomakan Apr 22, 2024
6e2ed5c
[codegen-v2]: Fix Rust codegen templates
satoshiotomakan Apr 22, 2024
43d5e3a
[misc]: Add `TWError` doc comments
satoshiotomakan Apr 22, 2024
24c7e62
Merge branch 'master' into s/rust-tw-error
satoshiotomakan Apr 24, 2024
a432121
Merge branch 'master' into s/rust-tw-error
satoshiotomakan Apr 24, 2024
bc93082
[CI]: Apply a workaround
satoshiotomakan Apr 24, 2024
83c77be
[CI]: Apply a work around https://github.com/actions/runner-images/is…
satoshiotomakan Apr 24, 2024
ca447f4
[CI]: Try to disable workaround
satoshiotomakan Apr 24, 2024
5592585
[CI]: Try to disable workaround
satoshiotomakan Apr 24, 2024
408c917
[CI]: Fix broken dependencies cache
satoshiotomakan Apr 24, 2024
3e38e65
[CI]: Apply the changes for linux-sampleapp-ci.yml
satoshiotomakan Apr 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
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_memory::Data;

pub struct {BLOCKCHAIN}Address {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use tw_coin_entry::coin_context::CoinContext;
use tw_coin_entry::coin_entry::{PublicKeyBytes, SignatureBytes};
use tw_coin_entry::error::SigningResult;
use tw_coin_entry::error::prelude::*;
use tw_coin_entry::signing_output_error;
use tw_proto::{BLOCKCHAIN}::Proto;
use tw_proto::TxCompiler::Proto as CompilerProto;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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::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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Copyright © 2017 Trust Wallet.

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_proto::{BLOCKCHAIN}::Proto;

Expand Down
2 changes: 1 addition & 1 deletion rust/chains/tw_aptos/src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use move_core_types::account_address::{AccountAddress, AccountAddressParseError}
use std::fmt::{Display, Formatter};
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_hash::sha3::sha3_256;
use tw_keypair::ed25519;
use tw_memory::Data;
Expand Down
2 changes: 1 addition & 1 deletion rust/chains/tw_aptos/src/aptos_move_packages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use move_core_types::account_address::AccountAddress;
use move_core_types::ident_str;
use move_core_types::language_storage::{ModuleId, TypeTag};
use serde_json::json;
use tw_coin_entry::error::SigningResult;
use tw_coin_entry::error::prelude::*;
use tw_encoding::bcs;

pub fn aptos_account_transfer(
Expand Down
Empty file.
10 changes: 6 additions & 4 deletions rust/chains/tw_aptos/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::address::Address;
use crate::transaction_builder;
use std::str::FromStr;
use tw_coin_entry::coin_entry::{PublicKeyBytes, SignatureBytes};
use tw_coin_entry::error::{SigningError, SigningErrorType, SigningResult};
use tw_coin_entry::error::prelude::*;
use tw_coin_entry::signing_output_error;
use tw_proto::Aptos::Proto;
use tw_proto::TxCompiler::Proto as CompilerProto;
Expand All @@ -22,7 +22,9 @@ impl Compiler {
input: Proto::SigningInput<'_>,
) -> SigningResult<CompilerProto::PreSigningOutput<'static>> {
let builder = transaction_builder::TransactionFactory::new_from_protobuf(input.clone())?;
let sender = Address::from_str(&input.sender)?;
let sender = Address::from_str(&input.sender)
.into_tw()
.context("Invalid sender address")?;
let signed_tx = builder
.sender(sender.inner())
.sequence_number(input.sequence_number as u64)
Expand Down Expand Up @@ -53,10 +55,10 @@ impl Compiler {
let sender = Address::from_str(&input.sender)?;
let signature = signatures
.first()
.ok_or(SigningError(SigningErrorType::Error_signatures_count))?;
.or_tw_err(SigningErrorType::Error_signatures_count)?;
let public_key = public_keys
.first()
.ok_or(SigningError(SigningErrorType::Error_signatures_count))?;
.or_tw_err(SigningErrorType::Error_signatures_count)?;

let signed_tx = builder
.sender(sender.inner())
Expand Down
2 changes: 1 addition & 1 deletion rust/chains/tw_aptos/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
16 changes: 11 additions & 5 deletions rust/chains/tw_aptos/src/liquid_staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::transaction_payload::{EntryFunction, TransactionPayload};
use move_core_types::{account_address::AccountAddress, ident_str, language_storage::ModuleId};
use serde_json::json;
use std::str::FromStr;
use tw_coin_entry::error::{SigningError, SigningErrorType, SigningResult};
use tw_coin_entry::error::prelude::*;
use tw_encoding::bcs;
use tw_proto::{
Aptos::Proto::mod_LiquidStaking::OneOfliquid_stake_transaction_payload,
Expand Down Expand Up @@ -91,7 +91,9 @@ impl TryFrom<LiquidStaking<'_>> for LiquidStakingOperation {
OneOfliquid_stake_transaction_payload::stake(stake_msg) => {
let smart_contract_address =
AccountAddress::from_str(&value.smart_contract_address)
.map_err(from_account_error)?;
.map_err(from_account_error)
.into_tw()
.context("Invalid Smart Contract address")?;
Ok(LiquidStakingOperation::Stake(Stake {
amount: stake_msg.amount,
smart_contract_address,
Expand All @@ -100,7 +102,9 @@ impl TryFrom<LiquidStaking<'_>> for LiquidStakingOperation {
OneOfliquid_stake_transaction_payload::unstake(unstake_msg) => {
let smart_contract_address =
AccountAddress::from_str(&value.smart_contract_address)
.map_err(from_account_error)?;
.map_err(from_account_error)
.into_tw()
.context("Invalid Smart Contract address")?;
Ok(LiquidStakingOperation::Unstake(Unstake {
amount: unstake_msg.amount,
smart_contract_address,
Expand All @@ -109,14 +113,16 @@ impl TryFrom<LiquidStaking<'_>> for LiquidStakingOperation {
OneOfliquid_stake_transaction_payload::claim(claim) => {
let smart_contract_address =
AccountAddress::from_str(&value.smart_contract_address)
.map_err(from_account_error)?;
.map_err(from_account_error)
.into_tw()
.context("Invalid Smart Contract address")?;
Ok(LiquidStakingOperation::Claim(Claim {
idx: claim.idx,
smart_contract_address,
}))
},
OneOfliquid_stake_transaction_payload::None => {
Err(SigningError(SigningErrorType::Error_invalid_params))
SigningError::err(SigningErrorType::Error_invalid_params)
},
}
}
Expand Down
25 changes: 19 additions & 6 deletions rust/chains/tw_aptos/src/nft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use crate::address::from_account_error;
use move_core_types::account_address::AccountAddress;
use std::str::FromStr;
use tw_coin_entry::error::{SigningError, SigningErrorType, SigningResult};
use tw_coin_entry::error::prelude::*;
use tw_proto::Aptos::Proto::mod_NftMessage::OneOfnft_transaction_payload;
use tw_proto::Aptos::Proto::{CancelOfferNftMessage, ClaimNftMessage, NftMessage, OfferNftMessage};

Expand Down Expand Up @@ -47,7 +47,8 @@ impl TryFrom<NftMessage<'_>> for NftOperation {
Ok(NftOperation::Claim(Claim::try_from(msg)?))
},
OneOfnft_transaction_payload::None => {
Err(SigningError(SigningErrorType::Error_invalid_params))
SigningError::err(SigningErrorType::Error_invalid_params)
.context("No transaction payload provided")
},
}
}
Expand Down Expand Up @@ -106,8 +107,14 @@ impl TryFrom<CancelOfferNftMessage<'_>> for Offer {

fn try_from(value: CancelOfferNftMessage) -> SigningResult<Self> {
Ok(Offer {
receiver: AccountAddress::from_str(&value.receiver).map_err(from_account_error)?,
creator: AccountAddress::from_str(&value.creator).map_err(from_account_error)?,
receiver: AccountAddress::from_str(&value.receiver)
.map_err(from_account_error)
.into_tw()
.context("Invalid receiver address")?,
creator: AccountAddress::from_str(&value.creator)
.map_err(from_account_error)
.into_tw()
.context("Invalid creator address")?,
collection: value.collectionName.as_bytes().to_vec(),
name: value.name.as_bytes().to_vec(),
property_version: value.property_version,
Expand Down Expand Up @@ -137,8 +144,14 @@ impl TryFrom<ClaimNftMessage<'_>> for Claim {

fn try_from(value: ClaimNftMessage) -> SigningResult<Self> {
Ok(Claim {
sender: AccountAddress::from_str(&value.sender).map_err(from_account_error)?,
creator: AccountAddress::from_str(&value.creator).map_err(from_account_error)?,
sender: AccountAddress::from_str(&value.sender)
.map_err(from_account_error)
.into_tw()
.context("Invalid sender address")?,
creator: AccountAddress::from_str(&value.creator)
.map_err(from_account_error)
.into_tw()
.context("Invalid creator address")?,
collection: value.collectionName.as_bytes().to_vec(),
name: value.name.as_bytes().to_vec(),
property_version: value.property_version,
Expand Down
6 changes: 4 additions & 2 deletions rust/chains/tw_aptos/src/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use crate::address::Address;
use crate::transaction_builder;
use std::str::FromStr;
use tw_coin_entry::error::SigningResult;
use tw_coin_entry::error::prelude::*;
use tw_coin_entry::signing_output_error;
use tw_keypair::ed25519;
use tw_proto::Aptos::Proto;
Expand All @@ -24,7 +24,9 @@ impl Signer {
) -> SigningResult<Proto::SigningOutput<'static>> {
let key_pair = ed25519::sha512::KeyPair::try_from(input.private_key.as_ref())?;
let builder = transaction_builder::TransactionFactory::new_from_protobuf(input.clone())?;
let sender = Address::from_str(&input.sender)?;
let sender = Address::from_str(&input.sender)
.into_tw()
.context("Invalid sender address")?;
let signed_tx = builder
.sender(sender.inner())
.sequence_number(input.sequence_number as u64)
Expand Down
7 changes: 5 additions & 2 deletions rust/chains/tw_aptos/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use move_core_types::account_address::AccountAddress;
use serde::Serialize;
use serde_json::{json, Value};
use std::borrow::Cow;
use tw_coin_entry::error::SigningResult;
use tw_coin_entry::error::prelude::*;
use tw_encoding::hex::encode;
use tw_encoding::{bcs, EncodingResult};
use tw_keypair::ed25519::sha512::KeyPair;
Expand Down Expand Up @@ -128,7 +128,10 @@ impl RawTransaction {
}

fn msg_to_sign(&self) -> SigningResult<Data> {
let serialized = self.serialize()?;
let serialized = self
.serialize()
.into_tw()
.context("Error serializing RawTransaction")?;
let mut preimage = tw_hash::sha3::sha3_256(APTOS_SALT);
preimage.extend_from_slice(serialized.as_slice());
Ok(preimage)
Expand Down
41 changes: 29 additions & 12 deletions rust/chains/tw_aptos/src/transaction_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use move_core_types::account_address::AccountAddress;
use move_core_types::language_storage::TypeTag;
use serde_json::Value;
use std::str::FromStr;
use tw_coin_entry::error::{SigningError, SigningErrorType, SigningResult};
use tw_coin_entry::error::prelude::*;
use tw_proto::Aptos::Proto::mod_SigningInput::OneOftransaction_payload;
use tw_proto::Aptos::Proto::SigningInput;

Expand Down Expand Up @@ -49,10 +49,12 @@ impl TransactionBuilder {
pub fn build(self) -> SigningResult<RawTransaction> {
let sender = self
.sender
.ok_or(SigningError(SigningErrorType::Error_invalid_params))?;
.or_tw_err(SigningErrorType::Error_invalid_params)
.context("Invalid sender address")?;
let sequence_number = self
.sequence_number
.ok_or(SigningError(SigningErrorType::Error_invalid_params))?;
.or_tw_err(SigningErrorType::Error_invalid_params)
.context("Invalid sequence number")?;
Ok(RawTransaction::new(
sender,
sequence_number,
Expand Down Expand Up @@ -91,22 +93,31 @@ impl TransactionFactory {
match input.transaction_payload {
OneOftransaction_payload::transfer(transfer) => factory
.implicitly_create_user_account_and_transfer(
AccountAddress::from_str(&transfer.to).map_err(from_account_error)?,
AccountAddress::from_str(&transfer.to)
.map_err(from_account_error)
.into_tw()
.context("Invalid destination address")?,
transfer.amount,
),
OneOftransaction_payload::token_transfer(token_transfer) => {
let func = token_transfer
.function
.ok_or(SigningError(SigningErrorType::Error_invalid_params))?;
.or_tw_err(SigningErrorType::Error_invalid_params)
.context("'TokenTransferMessage::function' is not set")?;
factory.coins_transfer(
AccountAddress::from_str(&token_transfer.to).map_err(from_account_error)?,
AccountAddress::from_str(&token_transfer.to)
.map_err(from_account_error)
.into_tw()
.context("Invalid destination address")?,
token_transfer.amount,
convert_proto_struct_tag_to_type_tag(func)?,
)
},
OneOftransaction_payload::create_account(create_account) => {
let address = AccountAddress::from_str(&create_account.auth_key)
.map_err(from_account_error)?;
.map_err(from_account_error)
.into_tw()
.context("Invalid 'auth_key' address")?;
factory.create_user_account(address)
},
OneOftransaction_payload::nft_message(nft_message) => {
Expand All @@ -115,7 +126,8 @@ impl TransactionFactory {
OneOftransaction_payload::register_token(register_token) => {
let function = register_token
.function
.ok_or(SigningError(SigningErrorType::Error_invalid_params))?;
.or_tw_err(SigningErrorType::Error_invalid_params)
.context("'ManagedTokensRegisterMessage::function' is not set")?;
Ok(factory.register_token(convert_proto_struct_tag_to_type_tag(function)?))
},
OneOftransaction_payload::liquid_staking_message(msg) => {
Expand All @@ -124,22 +136,27 @@ impl TransactionFactory {
OneOftransaction_payload::token_transfer_coins(token_transfer_coins) => {
let func = token_transfer_coins
.function
.ok_or(SigningError(SigningErrorType::Error_invalid_params))?;
.or_tw_err(SigningErrorType::Error_invalid_params)
.context("'TokenTransferCoinsMessage::function' is not set")?;
factory.implicitly_create_user_and_coins_transfer(
AccountAddress::from_str(&token_transfer_coins.to)
.map_err(from_account_error)?,
.map_err(from_account_error)
.into_tw()
.context("Invalid destination address")?,
token_transfer_coins.amount,
convert_proto_struct_tag_to_type_tag(func)?,
)
},
OneOftransaction_payload::None => {
let is_blind_sign = !input.any_encoded.is_empty();
let v = serde_json::from_str::<Value>(&input.any_encoded)?;
let v = serde_json::from_str::<Value>(&input.any_encoded)
.into_tw()
.context("Error decoding 'SigningInput::any_encoded' as JSON")?;
if is_blind_sign {
let entry_function = EntryFunction::try_from(v)?;
Ok(factory.payload(TransactionPayload::EntryFunction(entry_function)))
} else {
Err(SigningError(SigningErrorType::Error_input_parse))
SigningError::err(SigningErrorType::Error_input_parse)
}
},
}
Expand Down
9 changes: 5 additions & 4 deletions rust/chains/tw_aptos/src/transaction_payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use std::default::Default;
use std::str::FromStr;
use tw_coin_entry::error::{SigningError, SigningErrorType, SigningResult};
use tw_coin_entry::error::prelude::*;
use tw_encoding::{bcs, EncodingError, EncodingResult};
use tw_memory::Data;
use tw_proto::Aptos;
Expand All @@ -36,8 +36,9 @@ impl From<EncodingError> for EntryFunctionError {
}

impl From<EntryFunctionError> for SigningError {
fn from(_: EntryFunctionError) -> Self {
SigningError(SigningErrorType::Error_invalid_params)
fn from(e: EntryFunctionError) -> Self {
SigningError::new(SigningErrorType::Error_invalid_params)
.context(format!("Error decoding EntryFunction: {e:?}"))
}
}

Expand Down Expand Up @@ -122,7 +123,7 @@ pub fn convert_proto_struct_tag_to_type_tag(
"{}::{}::{}",
struct_tag.account_address, struct_tag.module, struct_tag.name
))
.map_err(|_| SigningError(SigningErrorType::Error_invalid_params))
.tw_err(|_| SigningErrorType::Error_invalid_params)
}

pub fn convert_type_tag_to_struct_tag(type_tag: TypeTag) -> Aptos::Proto::StructTag<'static> {
Expand Down
2 changes: 1 addition & 1 deletion rust/chains/tw_aptos/tests/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use tw_aptos::liquid_staking::{LiquidStakingOperation, Stake, Unstake};
use tw_aptos::nft::{Claim, NftOperation, Offer};
use tw_aptos::signer::Signer;
use tw_aptos::transaction_payload::convert_type_tag_to_struct_tag;
use tw_coin_entry::error::SigningErrorType;
use tw_coin_entry::error::prelude::*;
use tw_encoding::hex;
use tw_proto::Aptos::Proto;
use tw_proto::Aptos::Proto::{SigningInput, SigningOutput};
Expand Down
2 changes: 1 addition & 1 deletion rust/chains/tw_binance/src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading
Loading