Skip to content

Commit

Permalink
[Misc]: Improve error handling by adding contexts (#3806)
Browse files Browse the repository at this point in the history
* [misc]: Add TWError, TWResult, TWErrorKind, and traits

* Use `TWError` in `tw_coin_entry`

* [misc]: Return SigningError, SigningResult back, make TWError<E> generic

* [misc]: Add `tw_aptos` error contexts

* [misc]: Add `tw_sui` error contexts

* [misc]: Simplify `TWError` casts, add tests

* [misc]: Add contexts in `tw_cosmos_sdk`

* [misc]: Add contexts in `tw_evm`

* [misc]: Fix compilation of `tw_ethereum`, `tw_ronin`, `tw_bitcoin`, `tw_native_evmos`, `tw_native_injective`, `tw_thorchain`

* [misc]: Add error contexts in `tw_solana`

* [misc]: Fix `tw_bitcoin` compilation errors

* [misc]: Add error contexts to `tw_greenfield`

* [misc]: Fix other compilation errors

* [CI] Trigger CI

* [codegen-v2]: Fix Rust codegen templates

* [misc]: Add `TWError` doc comments

* [CI]: Apply a workaround

* [CI]: Apply a work around actions/runner-images#9733

* [CI]: Try to disable workaround

* [CI]: Try to disable workaround

* [CI]: Fix broken dependencies cache

* [CI]: Apply the changes for linux-sampleapp-ci.yml
  • Loading branch information
satoshiotomakan authored Apr 24, 2024
1 parent bcd0422 commit 406abe4
Show file tree
Hide file tree
Showing 172 changed files with 1,687 additions and 881 deletions.
11 changes: 2 additions & 9 deletions .github/workflows/linux-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,17 @@ jobs:
runs-on: ubuntu-latest
if: github.event.pull_request.draft == false
steps:
# Work around https://github.com/actions/runner-images/issues/8659
- name: Remove GCC 13 from runner image
shell: bash
run: |
sudo rm -f /etc/apt/sources.list.d/ubuntu-toolchain-r-ubuntu-test-jammy.list
sudo apt-get update
sudo apt-get install -y --allow-downgrades libc6=2.35-0ubuntu3.7 libc6-dev=2.35-0ubuntu3.7 libstdc++6=12.3.0-1ubuntu1~22.04 libgcc-s1=12.3.0-1ubuntu1~22.04
- uses: actions/checkout@v3
- name: Install system dependencies
run: |
tools/install-sys-dependencies-linux
tools/install-sys-dependencies-linux ci
tools/install-rust-dependencies
- name: Cache internal dependencies
id: internal_cache
uses: actions/cache@v3
with:
path: build/local
key: ${{ runner.os }}-internal-${{ hashFiles('tools/install-dependencies') }}
key: ${{ runner.os }}-internal-${{ hashFiles('tools/install-sys-dependencies-linux') }}-${{ hashFiles('tools/install-dependencies') }}
- name: Install internal dependencies
run: |
tools/install-dependencies
Expand Down
11 changes: 2 additions & 9 deletions .github/workflows/linux-sampleapp-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,17 @@ jobs:
runs-on: ubuntu-latest
if: github.event.pull_request.draft == false
steps:
# Work around https://github.com/actions/runner-images/issues/8659
- name: Remove GCC 13 from runner image
shell: bash
run: |
sudo rm -f /etc/apt/sources.list.d/ubuntu-toolchain-r-ubuntu-test-jammy.list
sudo apt-get update
sudo apt-get install -y --allow-downgrades libc6=2.35-0ubuntu3.7 libc6-dev=2.35-0ubuntu3.7 libstdc++6=12.3.0-1ubuntu1~22.04 libgcc-s1=12.3.0-1ubuntu1~22.04
- uses: actions/checkout@v3
- name: Install system dependencies
run: |
tools/install-sys-dependencies-linux
tools/install-sys-dependencies-linux ci
tools/install-rust-dependencies
- name: Cache internal dependencies
id: internal_cache
uses: actions/cache@v3
with:
path: build/local
key: ${{ runner.os }}-internal-${{ hashFiles('tools/install-dependencies') }}
key: ${{ runner.os }}-internal-${{ hashFiles('tools/install-sys-dependencies-linux') }}-${{ hashFiles('tools/install-dependencies') }}
- name: Install internal dependencies
run: |
tools/install-dependencies
Expand Down
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
Loading

0 comments on commit 406abe4

Please sign in to comment.