Skip to content

Commit

Permalink
feat(eth): Implement ERC721, ERC1155, ContractGeneric, Batch transact…
Browse files Browse the repository at this point in the history
…ions
  • Loading branch information
satoshiotomakan committed Jul 11, 2023
1 parent d284429 commit a28d838
Show file tree
Hide file tree
Showing 16 changed files with 1,397 additions and 60 deletions.
63 changes: 32 additions & 31 deletions rust/tw_coin_entry/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,61 +20,62 @@ pub enum AddressError {
}

pub type SigningResult<T> = Result<T, SigningError>;
pub type SigningErrorType = Proto::SigningError;

/// The wrapper over the `Common::Proto::SigningError` error for convenient use.
pub struct SigningError(pub Proto::SigningError);
/// The wrapper over the `Common::SigningErrorType` error for convenient use.
pub struct SigningError(pub SigningErrorType);

impl From<NumberError> for SigningError {
fn from(_err: NumberError) -> Self {
SigningError(Proto::SigningError::Error_invalid_params)
SigningError(SigningErrorType::Error_invalid_params)
}
}

impl From<AddressError> for SigningError {
fn from(_err: AddressError) -> Self {
SigningError(Proto::SigningError::Error_invalid_params)
SigningError(SigningErrorType::Error_invalid_params)
}
}

impl From<KeyPairError> for SigningError {
fn from(err: KeyPairError) -> Self {
match err {
KeyPairError::InvalidSecretKey => {
SigningError(Proto::SigningError::Error_invalid_private_key)
SigningError(SigningErrorType::Error_invalid_private_key)
},
KeyPairError::SigningError => SigningError(Proto::SigningError::Error_signing),
_ => SigningError(Proto::SigningError::Error_internal),
KeyPairError::SigningError => SigningError(SigningErrorType::Error_signing),
_ => SigningError(SigningErrorType::Error_internal),
}
}
}

impl fmt::Display for SigningError {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let str = match self.0 {
Proto::SigningError::OK => "",
Proto::SigningError::Error_general => "Unknown error",
Proto::SigningError::Error_internal => "Internal error",
Proto::SigningError::Error_low_balance => "Low balance: the sender balance is not enough to cover the send and other auxiliary amount such as fee, deposit, or minimal balance",
Proto::SigningError::Error_zero_amount_requested => "Requested amount is zero, send of 0 makes no sense",
Proto::SigningError::Error_missing_private_key => "One required key is missing (too few or wrong keys are provided)",
Proto::SigningError::Error_invalid_private_key => "A private key provided is invalid (e.g. wrong size, usually should be 32 bytes)",
Proto::SigningError::Error_invalid_address => "A provided address (e.g. destination address) is invalid",
Proto::SigningError::Error_invalid_utxo => "A provided input UTXO is invalid",
Proto::SigningError::Error_invalid_utxo_amount => "The amount of an input UTXO is invalid",
Proto::SigningError::Error_wrong_fee => "Wrong fee is given, probably it is too low to cover minimal fee for the transaction",
Proto::SigningError::Error_signing => "General signing error",
Proto::SigningError::Error_tx_too_big => "Resulting transaction is too large",
Proto::SigningError::Error_missing_input_utxos => "No input UTXOs provided",
Proto::SigningError::Error_not_enough_utxos => "Not enough non-dust input UTXOs to cover requested amount (dust UTXOs are filtered out)",
Proto::SigningError::Error_script_redeem => "Missing required redeem script",
Proto::SigningError::Error_script_output => "Invalid required output script",
Proto::SigningError::Error_script_witness_program => "Unrecognized witness program",
Proto::SigningError::Error_invalid_memo => "Invalid memo",
Proto::SigningError::Error_input_parse => "Some input field cannot be parsed",
Proto::SigningError::Error_no_support_n2n => "Multi-input and multi-output transaction not supported",
Proto::SigningError::Error_signatures_count => "Incorrect count of signatures passed to compile",
Proto::SigningError::Error_invalid_params => "Incorrect input parameter",
Proto::SigningError::Error_invalid_requested_token_amount => "Invalid input token amount",
SigningErrorType::OK => "",
SigningErrorType::Error_general => "Unknown error",
SigningErrorType::Error_internal => "Internal error",
SigningErrorType::Error_low_balance => "Low balance: the sender balance is not enough to cover the send and other auxiliary amount such as fee, deposit, or minimal balance",
SigningErrorType::Error_zero_amount_requested => "Requested amount is zero, send of 0 makes no sense",
SigningErrorType::Error_missing_private_key => "One required key is missing (too few or wrong keys are provided)",
SigningErrorType::Error_invalid_private_key => "A private key provided is invalid (e.g. wrong size, usually should be 32 bytes)",
SigningErrorType::Error_invalid_address => "A provided address (e.g. destination address) is invalid",
SigningErrorType::Error_invalid_utxo => "A provided input UTXO is invalid",
SigningErrorType::Error_invalid_utxo_amount => "The amount of an input UTXO is invalid",
SigningErrorType::Error_wrong_fee => "Wrong fee is given, probably it is too low to cover minimal fee for the transaction",
SigningErrorType::Error_signing => "General signing error",
SigningErrorType::Error_tx_too_big => "Resulting transaction is too large",
SigningErrorType::Error_missing_input_utxos => "No input UTXOs provided",
SigningErrorType::Error_not_enough_utxos => "Not enough non-dust input UTXOs to cover requested amount (dust UTXOs are filtered out)",
SigningErrorType::Error_script_redeem => "Missing required redeem script",
SigningErrorType::Error_script_output => "Invalid required output script",
SigningErrorType::Error_script_witness_program => "Unrecognized witness program",
SigningErrorType::Error_invalid_memo => "Invalid memo",
SigningErrorType::Error_input_parse => "Some input field cannot be parsed",
SigningErrorType::Error_no_support_n2n => "Multi-input and multi-output transaction not supported",
SigningErrorType::Error_signatures_count => "Incorrect count of signatures passed to compile",
SigningErrorType::Error_invalid_params => "Incorrect input parameter",
SigningErrorType::Error_invalid_requested_token_amount => "Invalid input token amount",
};
write!(f, "{str}")
}
Expand Down
2 changes: 1 addition & 1 deletion rust/tw_coin_entry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ mod prefix;
pub use coin_context::CoinContext;
pub use coin_entry::{CoinEntry, ProtoError, ProtoResult};
pub use derivation::Derivation;
pub use error::{AddressError, AddressResult, SigningError, SigningResult};
pub use error::{AddressError, AddressResult, SigningError, SigningErrorType, SigningResult};
pub use prefix::{NoPrefix, Prefix};
8 changes: 8 additions & 0 deletions rust/tw_ethereum/src/abi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
// file LICENSE at the root of the source code distribution tree.

use crate::address::Address;
use tw_coin_entry::{SigningError, SigningErrorType};
use tw_number::U256;

pub mod prebuild;

pub type AbiResult<T> = Result<T, AbiError>;

#[derive(Debug)]
pub enum AbiError {
InvalidParams,
}
Expand All @@ -21,6 +23,12 @@ impl From<ethabi::Error> for AbiError {
}
}

impl From<AbiError> for SigningError {
fn from(_: AbiError) -> Self {
SigningError(SigningErrorType::Error_internal)
}
}

/// TODO remove this when Ethereum ABI is designed manually.
pub fn convert_u256(num: U256) -> ethabi::Uint {
let bytes = num.to_big_endian().take();
Expand Down
Loading

0 comments on commit a28d838

Please sign in to comment.