diff --git a/frame/cosmos/x/auth/src/basic.rs b/frame/cosmos/x/auth/src/basic.rs index ef85ce5..92bb8e4 100644 --- a/frame/cosmos/x/auth/src/basic.rs +++ b/frame/cosmos/x/auth/src/basic.rs @@ -21,9 +21,7 @@ use cosmos_sdk_proto::cosmos::tx::v1beta1::Tx; use pallet_cosmos_types::handler::AnteDecorator; use sp_runtime::{ traits::Get, - transaction_validity::{ - InvalidTransaction, TransactionValidity, TransactionValidityError, ValidTransaction, - }, + transaction_validity::{InvalidTransaction, TransactionValidity, ValidTransaction}, SaturatedConversion, }; @@ -35,15 +33,12 @@ where { fn ante_handle(tx: &Tx, _simulate: bool) -> TransactionValidity { if tx.signatures.is_empty() { - return Err(TransactionValidityError::Invalid(InvalidTransaction::BadProof)); + return Err(InvalidTransaction::BadProof.into()); } - let auth_info = tx - .auth_info - .as_ref() - .ok_or(TransactionValidityError::Invalid(InvalidTransaction::BadSigner))?; + let auth_info = tx.auth_info.as_ref().ok_or(InvalidTransaction::BadSigner)?; if auth_info.signer_infos.len() != tx.signatures.len() { - return Err(TransactionValidityError::Invalid(InvalidTransaction::BadSigner)); + return Err(InvalidTransaction::BadSigner.into()); } Ok(ValidTransaction::default()) @@ -57,16 +52,13 @@ where T: frame_system::Config, { fn ante_handle(tx: &Tx, _simulate: bool) -> TransactionValidity { - let body = tx - .body - .as_ref() - .ok_or(TransactionValidityError::Invalid(InvalidTransaction::Call))?; + let body = tx.body.as_ref().ok_or(InvalidTransaction::Call)?; if body.timeout_height > 0 && frame_system::Pallet::::block_number().saturated_into::() > body.timeout_height { - return Err(TransactionValidityError::Invalid(InvalidTransaction::Stale)); + return Err(InvalidTransaction::Stale.into()); } Ok(ValidTransaction::default()) @@ -80,13 +72,10 @@ where T: pallet_cosmos::Config, { fn ante_handle(tx: &Tx, _simulate: bool) -> TransactionValidity { - let body = tx - .body - .as_ref() - .ok_or(TransactionValidityError::Invalid(InvalidTransaction::Call))?; + let body = tx.body.as_ref().ok_or(InvalidTransaction::Call)?; if body.memo.len().saturated_into::() > T::MaxMemoCharacters::get() { - return Err(TransactionValidityError::Invalid(InvalidTransaction::Call)); + return Err(InvalidTransaction::Call.into()); } Ok(ValidTransaction::default()) diff --git a/frame/cosmos/x/auth/src/fee.rs b/frame/cosmos/x/auth/src/fee.rs index 0eca367..a4a8bcd 100644 --- a/frame/cosmos/x/auth/src/fee.rs +++ b/frame/cosmos/x/auth/src/fee.rs @@ -40,7 +40,7 @@ use pallet_cosmos_x_auth_signing::sign_verifiable_tx::traits::SigVerifiableTx; use sp_core::{Get, H160}; use sp_runtime::{ traits::{Convert, Zero}, - transaction_validity::{TransactionValidity, TransactionValidityError, ValidTransaction}, + transaction_validity::{TransactionValidity, ValidTransaction}, SaturatedConversion, }; @@ -55,13 +55,13 @@ where .auth_info .as_ref() .and_then(|auth_info| auth_info.fee.as_ref()) - .ok_or(TransactionValidityError::Invalid(InvalidTransaction::Call))?; + .ok_or(InvalidTransaction::Call)?; if !simulate && !frame_system::Pallet::::block_number().is_zero() && fee.gas_limit.is_zero() { - return Err(TransactionValidityError::Invalid(InvalidTransaction::Call)); + return Err(InvalidTransaction::Call.into()); } // TODO: Implements txFeeChecker @@ -77,24 +77,23 @@ where T: pallet_cosmos::Config, { fn check_deduct_fee(tx: &Tx) -> TransactionValidity { - let fee_payer = T::SigVerifiableTx::fee_payer(tx) - .map_err(|_| TransactionValidityError::Invalid(InvalidTransaction::Call))?; + let fee_payer = T::SigVerifiableTx::fee_payer(tx).map_err(|_| InvalidTransaction::Call)?; let fee = tx .auth_info .as_ref() .and_then(|auth_info| auth_info.fee.as_ref()) - .ok_or(TransactionValidityError::Invalid(InvalidTransaction::Call))?; + .ok_or(InvalidTransaction::Call)?; // TODO: Fee granter not supported if !fee.granter.is_empty() { - return Err(TransactionValidityError::Invalid(InvalidTransaction::Call)); + return Err(InvalidTransaction::Call.into()); } - let (_hrp, address_raw) = acc_address_from_bech32(&fee_payer) - .map_err(|_| TransactionValidityError::Invalid(InvalidTransaction::BadSigner))?; + let (_hrp, address_raw) = + acc_address_from_bech32(&fee_payer).map_err(|_| InvalidTransaction::BadSigner)?; if address_raw.len() != 20 { - return Err(TransactionValidityError::Invalid(InvalidTransaction::BadSigner)); + return Err(InvalidTransaction::BadSigner.into()); } let deduct_fees_from = T::AddressMapping::into_account_id(H160::from_slice(&address_raw)); @@ -121,10 +120,7 @@ where fn deduct_fees(acc: &T::AccountId, fee: &Fee) -> TransactionValidity { for amt in fee.amount.iter() { - let amount = amt - .amount - .parse::() - .map_err(|_| TransactionValidityError::Invalid(InvalidTransaction::Call))?; + let amount = amt.amount.parse::().map_err(|_| InvalidTransaction::Call)?; if amt.denom == T::NativeDenom::get() { let _imbalance = T::NativeAsset::withdraw( @@ -133,12 +129,12 @@ where WithdrawReasons::TRANSACTION_PAYMENT, ExistenceRequirement::KeepAlive, ) - .map_err(|_| TransactionValidityError::Invalid(InvalidTransaction::Payment))?; + .map_err(|_| InvalidTransaction::Payment)?; // TODO: Resolve imbalance } else { let asset_id = T::AssetToDenom::convert(amt.denom.clone()) - .map_err(|_| TransactionValidityError::Invalid(InvalidTransaction::Call))?; + .map_err(|_| InvalidTransaction::Call)?; let _imbalance = T::Assets::withdraw( asset_id, @@ -148,7 +144,7 @@ where Preservation::Preserve, Fortitude::Polite, ) - .map_err(|_| TransactionValidityError::Invalid(InvalidTransaction::Payment))?; + .map_err(|_| InvalidTransaction::Payment)?; // TODO: Resolve imbalance } diff --git a/frame/cosmos/x/auth/src/msg.rs b/frame/cosmos/x/auth/src/msg.rs index 9a0682d..00ef3be 100644 --- a/frame/cosmos/x/auth/src/msg.rs +++ b/frame/cosmos/x/auth/src/msg.rs @@ -19,9 +19,7 @@ use cosmos_sdk_proto::cosmos::tx::v1beta1::Tx; use frame_support::traits::Contains; use pallet_cosmos_types::handler::AnteDecorator; -use sp_runtime::transaction_validity::{ - InvalidTransaction, TransactionValidity, TransactionValidityError, ValidTransaction, -}; +use sp_runtime::transaction_validity::{InvalidTransaction, TransactionValidity, ValidTransaction}; pub struct KnownMsgDecorator(core::marker::PhantomData); @@ -30,14 +28,11 @@ where T: pallet_cosmos::Config, { fn ante_handle(tx: &Tx, _simulate: bool) -> TransactionValidity { - let body = tx - .body - .as_ref() - .ok_or(TransactionValidityError::Invalid(InvalidTransaction::Call))?; + let body = tx.body.as_ref().ok_or(InvalidTransaction::Call)?; for msg in body.messages.iter() { if !T::MsgFilter::contains(msg) { - return Err(TransactionValidityError::Invalid(InvalidTransaction::Call)); + return Err(InvalidTransaction::Call.into()); } } diff --git a/frame/cosmos/x/auth/src/sigverify.rs b/frame/cosmos/x/auth/src/sigverify.rs index 9ed181a..90cc842 100644 --- a/frame/cosmos/x/auth/src/sigverify.rs +++ b/frame/cosmos/x/auth/src/sigverify.rs @@ -50,51 +50,41 @@ where { fn ante_handle(tx: &Tx, _simulate: bool) -> TransactionValidity { let signatures = &tx.signatures; - let signers = T::SigVerifiableTx::get_signers(tx) - .map_err(|_| TransactionValidityError::Invalid(InvalidTransaction::BadSigner))?; + let signers = + T::SigVerifiableTx::get_signers(tx).map_err(|_| InvalidTransaction::BadSigner)?; - let auth_info = tx - .auth_info - .as_ref() - .ok_or(TransactionValidityError::Invalid(InvalidTransaction::BadSigner))?; + let auth_info = tx.auth_info.as_ref().ok_or(InvalidTransaction::BadSigner)?; if signatures.len() != signers.len() { - return Err(TransactionValidityError::Invalid(InvalidTransaction::BadSigner)); + return Err(InvalidTransaction::BadSigner.into()); } if signatures.len() != auth_info.signer_infos.len() { - return Err(TransactionValidityError::Invalid(InvalidTransaction::BadSigner)); + return Err(InvalidTransaction::BadSigner.into()); } for (i, sig) in signatures.iter().enumerate() { - let signer = signers - .get(i) - .ok_or(TransactionValidityError::Invalid(InvalidTransaction::BadSigner))?; + let signer = signers.get(i).ok_or(InvalidTransaction::BadSigner)?; - let signer_info = auth_info - .signer_infos - .get(i) - .ok_or(TransactionValidityError::Invalid(InvalidTransaction::BadSigner))?; + let signer_info = auth_info.signer_infos.get(i).ok_or(InvalidTransaction::BadSigner)?; - let (_hrp, signer_addr_raw) = acc_address_from_bech32(signer) - .map_err(|_| TransactionValidityError::Invalid(InvalidTransaction::BadSigner))?; + let (_hrp, signer_addr_raw) = + acc_address_from_bech32(signer).map_err(|_| InvalidTransaction::BadSigner)?; if signer_addr_raw.len() != 20 { - return Err(TransactionValidityError::Invalid(InvalidTransaction::BadSigner)); + return Err(InvalidTransaction::BadSigner.into()); } let who = T::AddressMapping::into_account_id(H160::from_slice(&signer_addr_raw)); let sequence = frame_system::Pallet::::account_nonce(&who).saturated_into(); if signer_info.sequence > sequence { - return Err(TransactionValidityError::Invalid(InvalidTransaction::Future)); + return Err(InvalidTransaction::Future.into()); } else if signer_info.sequence < sequence { - return Err(TransactionValidityError::Invalid(InvalidTransaction::Stale)); + return Err(InvalidTransaction::Stale.into()); } - let public_key = signer_info - .public_key - .as_ref() - .ok_or(TransactionValidityError::Invalid(InvalidTransaction::BadSigner))?; + let public_key = + signer_info.public_key.as_ref().ok_or(InvalidTransaction::BadSigner)?; let chain_id = T::ChainId::get().to_string(); let signer_data = SignerData { address: signer.clone(), @@ -104,10 +94,7 @@ where pub_key: public_key.clone(), }; - let sign_mode = signer_info - .mode_info - .as_ref() - .ok_or(TransactionValidityError::Invalid(InvalidTransaction::BadSigner))?; + let sign_mode = signer_info.mode_info.as_ref().ok_or(InvalidTransaction::BadSigner)?; Self::verify_signature(public_key, &signer_data, sign_mode, sig, tx)?; } @@ -132,7 +119,7 @@ where secp256k1::PubKey => { let public_key = secp256k1::PubKey::decode(&mut &*public_key.value).map_err(|_| { - TransactionValidityError::Invalid(InvalidTransaction::BadSigner) + InvalidTransaction::BadSigner })?; let mut hasher = ripemd::Ripemd160::new(); hasher.update(sha2_256(&public_key.key)); @@ -140,28 +127,28 @@ where let (_hrp, signer_addr_raw) = acc_address_from_bech32(&signer_data.address).map_err(|_| { - TransactionValidityError::Invalid(InvalidTransaction::BadSigner) + InvalidTransaction::BadSigner })?; if signer_addr_raw.len() != 20 { - return Err(TransactionValidityError::Invalid(InvalidTransaction::BadSigner)); + return Err(InvalidTransaction::BadSigner.into()); } if H160::from_slice(&signer_addr_raw) != address { - return Err(TransactionValidityError::Invalid(InvalidTransaction::BadSigner)); + return Err(InvalidTransaction::BadSigner.into()); } let sign_bytes = T::SignModeHandler::get_sign_bytes(sign_mode, signer_data, tx) - .map_err(|_| TransactionValidityError::Invalid(InvalidTransaction::Call))?; + .map_err(|_| InvalidTransaction::Call)?; if !secp256k1_ecdsa_verify(signature, &sha2_256(&sign_bytes), &public_key.key) { - return Err(TransactionValidityError::Invalid(InvalidTransaction::BadProof)); + return Err(InvalidTransaction::BadProof.into()); } Ok(()) } }, - Err(TransactionValidityError::Invalid(InvalidTransaction::BadSigner)) + Err(InvalidTransaction::BadSigner.into()) ) } } @@ -175,18 +162,13 @@ where fn ante_handle(tx: &Tx, _simulate: bool) -> TransactionValidity { let mut sig_count = 0u64; - let auth_info = tx - .auth_info - .as_ref() - .ok_or(TransactionValidityError::Invalid(InvalidTransaction::BadSigner))?; + let auth_info = tx.auth_info.as_ref().ok_or(InvalidTransaction::BadSigner)?; for SignerInfo { public_key, .. } in auth_info.signer_infos.iter() { - let public_key = public_key - .as_ref() - .ok_or(TransactionValidityError::Invalid(InvalidTransaction::BadSigner))?; + let public_key = public_key.as_ref().ok_or(InvalidTransaction::BadSigner)?; sig_count = sig_count.saturating_add(Self::count_sub_keys(public_key)?); if sig_count > T::TxSigLimit::get() { - return Err(TransactionValidityError::Invalid(InvalidTransaction::BadProof)); + return Err(InvalidTransaction::BadProof.into()); } } @@ -198,7 +180,7 @@ impl ValidateSigCountDecorator { fn count_sub_keys(pubkey: &Any) -> Result { // TODO: Support legacy multi signatures. if LegacyAminoPubKey::decode(&mut &*pubkey.value).is_ok() { - Err(TransactionValidityError::Invalid(InvalidTransaction::BadProof)) + Err(InvalidTransaction::BadProof.into()) } else { Ok(1) } @@ -212,13 +194,12 @@ where T: frame_system::Config + pallet_cosmos::Config, { fn ante_handle(tx: &Tx, _simulate: bool) -> TransactionValidity { - let signers = T::SigVerifiableTx::get_signers(tx) - .map_err(|_| TransactionValidityError::Invalid(InvalidTransaction::Call))?; + let signers = T::SigVerifiableTx::get_signers(tx).map_err(|_| InvalidTransaction::Call)?; for signer in signers.iter() { - let (_hrp, address_raw) = acc_address_from_bech32(signer) - .map_err(|_| TransactionValidityError::Invalid(InvalidTransaction::BadSigner))?; + let (_hrp, address_raw) = + acc_address_from_bech32(signer).map_err(|_| InvalidTransaction::BadSigner)?; if address_raw.len() != 20 { - return Err(TransactionValidityError::Invalid(InvalidTransaction::BadSigner)); + return Err(InvalidTransaction::BadSigner.into()); } let account = T::AddressMapping::into_account_id(H160::from_slice(&address_raw)); diff --git a/template/runtime/src/lib.rs b/template/runtime/src/lib.rs index aa74d06..672b80a 100644 --- a/template/runtime/src/lib.rs +++ b/template/runtime/src/lib.rs @@ -540,9 +540,7 @@ impl fp_self_contained::SelfContainedCall for RuntimeCall { RuntimeCall::Cosmos(call) => { if let pallet_cosmos::Call::transact { tx_bytes } = call { if Runtime::migrate_cosm_account(tx_bytes).is_err() { - return Some(Err(TransactionValidityError::Invalid( - InvalidTransaction::BadSigner, - ))); + return Some(Err(InvalidTransaction::BadSigner.into())); } } @@ -562,9 +560,7 @@ impl fp_self_contained::SelfContainedCall for RuntimeCall { RuntimeCall::Cosmos(call) => { if let pallet_cosmos::Call::transact { tx_bytes } = call { if Runtime::migrate_cosm_account(tx_bytes).is_err() { - return Some(Err(TransactionValidityError::Invalid( - InvalidTransaction::BadSigner, - ))); + return Some(Err(InvalidTransaction::BadSigner.into())); } } @@ -594,51 +590,38 @@ impl Runtime { use fungible::{Inspect, Mutate}; use pallet_cosmos_x_auth_signing::sign_verifiable_tx::traits::SigVerifiableTx; - let tx = Tx::decode(&mut &*tx_bytes) - .map_err(|_| TransactionValidityError::Invalid(InvalidTransaction::Call))?; + let tx = Tx::decode(&mut &*tx_bytes).map_err(|_| InvalidTransaction::Call)?; let signers = ::SigVerifiableTx::get_signers(&tx) - .map_err(|_| TransactionValidityError::Invalid(InvalidTransaction::Call))?; + .map_err(|_| InvalidTransaction::Call)?; - let signer_infos = tx - .auth_info - .ok_or(TransactionValidityError::Invalid(InvalidTransaction::Call))? - .signer_infos; + let signer_infos = tx.auth_info.ok_or(InvalidTransaction::Call)?.signer_infos; for (i, signer_info) in signer_infos.iter().enumerate() { if signer_info.sequence == 0 { - let signer = signers - .get(i) - .ok_or(TransactionValidityError::Invalid(InvalidTransaction::BadSigner))?; + let signer = signers.get(i).ok_or(InvalidTransaction::BadSigner)?; - let (_hrp, address_raw) = acc_address_from_bech32(signer).map_err(|_| { - TransactionValidityError::Invalid(InvalidTransaction::BadSigner) - })?; + let (_hrp, address_raw) = + acc_address_from_bech32(signer).map_err(|_| InvalidTransaction::BadSigner)?; if address_raw.len() != 20 { - return Err(TransactionValidityError::Invalid(InvalidTransaction::BadSigner)); + return Err(InvalidTransaction::BadSigner.into()); } let interim_account = ::AddressMapping::into_account_id( H160::from_slice(&address_raw), ); - let public_key = signer_info - .public_key - .as_ref() - .ok_or(TransactionValidityError::Invalid(InvalidTransaction::Call))?; + let public_key = signer_info.public_key.as_ref().ok_or(InvalidTransaction::Call)?; let who = any_match!( public_key, { secp256k1::PubKey => { - let public_key = secp256k1::PubKey::decode(&mut &*public_key.value) - .map_err(|_| { - TransactionValidityError::Invalid(InvalidTransaction::Call) - })?; + let public_key = secp256k1::PubKey::decode(&mut &*public_key.value).map_err(|_| InvalidTransaction::Call)?; let mut pk = [0u8; 33]; pk.copy_from_slice(&public_key.key); Ok(CosmosSigner(Public(pk))) } }, - Err(TransactionValidityError::Invalid(InvalidTransaction::Call)) + Err(InvalidTransaction::Call) )?; let balance = pallet_balances::Pallet::::reducible_balance( @@ -652,12 +635,12 @@ impl Runtime { balance, Preservation::Expendable, ) - .map_err(|_| TransactionValidityError::Invalid(InvalidTransaction::Call))?; + .map_err(|_| InvalidTransaction::Call)?; // TODO: Add asset transfer for migration pallet_cosmos_accounts::Pallet::::connect_account(&who) - .map_err(|_| TransactionValidityError::Invalid(InvalidTransaction::Call))?; + .map_err(|_| InvalidTransaction::Call)?; } }