Skip to content

Commit

Permalink
[misc]: Add error contexts to tw_greenfield
Browse files Browse the repository at this point in the history
  • Loading branch information
satoshiotomakan committed Apr 22, 2024
1 parent 3ed3211 commit b0a200a
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 27 deletions.
2 changes: 1 addition & 1 deletion rust/chains/tw_greenfield/src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
13 changes: 9 additions & 4 deletions rust/chains/tw_greenfield/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand All @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion rust/chains/tw_greenfield/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: 9 additions & 7 deletions rust/chains/tw_greenfield/src/modules/eip712_signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 })
}
Expand Down
37 changes: 26 additions & 11 deletions rust/chains/tw_greenfield/src/modules/tx_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -89,7 +91,9 @@ impl TxBuilder {
}

fn coin_from_proto(input: &Proto::Amount<'_>) -> SigningResult<Coin> {
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(),
Expand All @@ -98,7 +102,8 @@ impl TxBuilder {

fn tx_body_from_proto(input: &Proto::SigningInput<'_>) -> SigningResult<GreenfieldTxBody> {
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
Expand All @@ -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"),
}
}

Expand All @@ -139,8 +145,12 @@ impl TxBuilder {
.collect::<SigningResult<_>>()?;
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)))
Expand All @@ -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))
}
Expand Down
2 changes: 1 addition & 1 deletion rust/chains/tw_greenfield/src/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down

0 comments on commit b0a200a

Please sign in to comment.