diff --git a/client/rpc/src/cosm.rs b/client/rpc/src/cosm.rs index d8df0a5c..f42d5f98 100644 --- a/client/rpc/src/cosm.rs +++ b/client/rpc/src/cosm.rs @@ -76,10 +76,10 @@ where .runtime_api() .convert_tx(block_hash, tx_bytes.to_vec(), chain_id.as_bytes().to_vec()) .map_err(|_| internal_err("cannot access runtime api"))?; - let tx_hash = sha2_256(&tx_bytes); + let tx_hash = H256(sha2_256(&tx_bytes)); self.pool .submit_one(block_hash, TransactionSource::Local, extrinsic) - .map_ok(move |_| H256(tx_hash)) + .map_ok(move |_| tx_hash) .map_err(|e| internal_err(e.to_string())) .await } diff --git a/template/runtime/src/compat/cosm.rs b/template/runtime/src/compat/cosm.rs index 724c1541..41cbcd72 100644 --- a/template/runtime/src/compat/cosm.rs +++ b/template/runtime/src/compat/cosm.rs @@ -25,7 +25,7 @@ use sp_core::{ecdsa, Hasher, H160, H256}; use sp_std::marker::PhantomData; /// Hashed address mapping. -pub struct HashedAddressMapping(PhantomData, PhantomData); +pub struct HashedAddressMapping(PhantomData<(T, H)>); impl AddressMapping for HashedAddressMapping where diff --git a/template/runtime/src/lib.rs b/template/runtime/src/lib.rs index dbecbf9d..35f1764c 100644 --- a/template/runtime/src/lib.rs +++ b/template/runtime/src/lib.rs @@ -41,6 +41,7 @@ use frame_support::{ IdentityFee, Weight, }, }; +use hp_cosmos::{PublicKey, SignerPublicKey}; use hp_crypto::EcdsaExt; use pallet_cosmos::handler::cosm::MsgHandler; use pallet_grandpa::{ @@ -58,7 +59,6 @@ use sp_runtime::{ }, transaction_validity::{ InvalidTransaction, TransactionSource, TransactionValidity, TransactionValidityError, - UnknownTransaction, }, ApplyExtrinsicResult, ExtrinsicInclusionMode, Perbill, }; @@ -393,38 +393,22 @@ impl fp_self_contained::SelfContainedCall for RuntimeCall { fn check_self_contained(&self) -> Option> { match self { - RuntimeCall::Cosmos(call) => { - if let pallet_cosmos::Call::transact { tx_bytes, chain_id } = call { - let tx = hp_io::decode_tx::decode(tx_bytes, chain_id).unwrap(); - - let check = || { - if let Some(hp_cosmos::SignerPublicKey::Single( - hp_cosmos::PublicKey::Secp256k1(pk), - )) = tx.auth_info.signer_infos[0].public_key - { - if hp_io::crypto::secp256k1_ecdsa_verify( - &tx.signatures[0], - tx.hash.as_bytes(), - &pk, - ) { - Ok(Self::SignedInfo::from(pk)) - } else { - Err(InvalidTransaction::Custom( - hp_cosmos::error::TransactionValidationError::InvalidSignature - as u8, - ))? - } + RuntimeCall::Cosmos(call) => match call.check_self_contained()? { + Ok(_) => + if let pallet_cosmos::Call::transact { tx_bytes, chain_id } = call { + let tx = hp_io::decode_tx::decode(tx_bytes, chain_id)?; + let public_key = tx.auth_info.signer_infos.first()?.clone().public_key?; + if let SignerPublicKey::Single(PublicKey::Secp256k1(pk)) = public_key { + Some(Ok(Self::SignedInfo::from(pk))) } else { - Err(InvalidTransaction::Custom( - hp_cosmos::error::TransactionValidationError::UnsupportedSignerType - as u8, - ))? + Some(Err(TransactionValidityError::Invalid( + InvalidTransaction::BadProof, + ))) } - }; - Some(check()) - } else { - None - } + } else { + None + }, + Err(e) => Some(Err(e)), }, _ => None, } @@ -437,21 +421,8 @@ impl fp_self_contained::SelfContainedCall for RuntimeCall { len: usize, ) -> Option { match self { - RuntimeCall::Cosmos(call) => { - if let pallet_cosmos::Call::transact { tx_bytes, chain_id } = &call { - let tx = hp_io::decode_tx::decode(tx_bytes, chain_id).unwrap(); - - if tx.auth_info.signer_infos[0].sequence == 0 && - Runtime::migrate_cosm_account(&info.to_cosm_address().unwrap(), info) - .is_err() - { - return Some(Err(TransactionValidityError::Unknown( - UnknownTransaction::CannotLookup, - ))); - } - } - call.validate_self_contained(&info.to_cosm_address().unwrap(), dispatch_info, len) - }, + RuntimeCall::Cosmos(call) => + call.validate_self_contained(&info.to_cosm_address().unwrap(), dispatch_info, len), _ => None, } } @@ -463,24 +434,11 @@ impl fp_self_contained::SelfContainedCall for RuntimeCall { len: usize, ) -> Option> { match self { - RuntimeCall::Cosmos(call) => { - if let pallet_cosmos::Call::transact { tx_bytes, chain_id } = &call { - let tx = hp_io::decode_tx::decode(tx_bytes, chain_id).unwrap(); - if tx.auth_info.signer_infos[0].sequence == 0 && - Runtime::migrate_cosm_account(&info.to_cosm_address().unwrap(), info) - .is_err() - { - return Some(Err(TransactionValidityError::Unknown( - UnknownTransaction::CannotLookup, - ))); - } - } - call.pre_dispatch_self_contained( - &info.to_cosm_address().unwrap(), - dispatch_info, - len, - ) - }, + RuntimeCall::Cosmos(call) => call.pre_dispatch_self_contained( + &info.to_cosm_address().unwrap(), + dispatch_info, + len, + ), _ => None, } }