diff --git a/Cargo.lock b/Cargo.lock index 71794f70df..13adf6cdc4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -988,7 +988,6 @@ name = "ibc" version = "0.1.1" dependencies = [ "anomaly", - "bech32 0.8.0", "bytes", "chrono", "dyn-clonable", diff --git a/modules/Cargo.toml b/modules/Cargo.toml index fd08b5306a..17d57c80e9 100644 --- a/modules/Cargo.toml +++ b/modules/Cargo.toml @@ -35,7 +35,6 @@ prost-types = "0.7" bytes = "1.0.0" dyn-clonable = "0.9.0" regex = "1" -bech32 = "0.8.0" subtle-encoding = "0.5" sha2 = { version = "0.9.3", optional = true } diff --git a/modules/src/address.rs b/modules/src/address.rs deleted file mode 100644 index 02cf9b4908..0000000000 --- a/modules/src/address.rs +++ /dev/null @@ -1,24 +0,0 @@ -// TODO - remove when ICS message signer changes from AccountId to String - -use std::convert::TryFrom; - -use anomaly::{BoxError, Context}; -use bech32::ToBase32; -use bech32::{FromBase32, Variant}; - -use tendermint::account::Id as AccountId; - -pub fn account_to_string(addr: AccountId) -> Result { - Ok(bech32::encode("cosmos", addr.to_base32(), Variant::Bech32) - .map_err(|e| Context::new("cannot generate bech32 account", Some(e.into())))?) -} - -pub fn string_to_account(raw: String) -> Result { - let (_hrp, data, _variant) = - bech32::decode(&raw).map_err(|e| Context::new("bad signer", Some(e.into())))?; - let addr_bytes = - Vec::::from_base32(&data).map_err(|e| Context::new("bad signer", Some(e.into())))?; - - let account_id = AccountId::try_from(addr_bytes).map_err(|e| format!("bad signer: {}", e))?; - Ok(account_id) -} diff --git a/modules/src/application/ics20_fungible_token_transfer/msgs/transfer.rs b/modules/src/application/ics20_fungible_token_transfer/msgs/transfer.rs index a20dc48a90..0f5041f6a3 100644 --- a/modules/src/application/ics20_fungible_token_transfer/msgs/transfer.rs +++ b/modules/src/application/ics20_fungible_token_transfer/msgs/transfer.rs @@ -1,15 +1,14 @@ //! This is the definition of a transfer messages that an application submits to a chain. use std::convert::{TryFrom, TryInto}; -use tendermint::account::Id as AccountId; -use tendermint_proto::Protobuf; use ibc_proto::ibc::applications::transfer::v1::MsgTransfer as RawMsgTransfer; +use tendermint_proto::Protobuf; -use crate::address::{account_to_string, string_to_account}; use crate::application::ics20_fungible_token_transfer::error::{Error, Kind}; use crate::ics02_client::height::Height; use crate::ics24_host::identifier::{ChannelId, PortId}; +use crate::signer::Signer; use crate::tx_msg::Msg; pub const TYPE_URL: &str = "/ibc.applications.transfer.v1.MsgTransfer"; @@ -26,9 +25,9 @@ pub struct MsgTransfer { /// the tokens to be transferred pub token: Option, /// the sender address - pub sender: AccountId, + pub sender: Signer, /// the recipient address on the destination chain - pub receiver: AccountId, + pub receiver: Signer, /// Timeout height relative to the current block height. /// The timeout is disabled when set to 0. pub timeout_height: Height, @@ -39,6 +38,7 @@ pub struct MsgTransfer { impl Msg for MsgTransfer { type ValidationError = Error; + type Raw = RawMsgTransfer; fn route(&self) -> String { crate::keys::ROUTER_KEY.to_string() @@ -47,10 +47,6 @@ impl Msg for MsgTransfer { fn type_url(&self) -> String { TYPE_URL.to_string() } - - fn get_signers(&self) -> Vec { - vec![self.sender] - } } impl Protobuf for MsgTransfer {} @@ -63,8 +59,8 @@ impl TryFrom for MsgTransfer { source_port: raw_msg.source_port.parse().unwrap(), source_channel: raw_msg.source_channel.parse().unwrap(), token: raw_msg.token, - sender: string_to_account(raw_msg.sender).unwrap(), - receiver: string_to_account(raw_msg.receiver).unwrap(), + sender: raw_msg.sender.into(), + receiver: raw_msg.receiver.into(), timeout_height: raw_msg.timeout_height.unwrap().try_into().unwrap(), timeout_timestamp: 0, }) @@ -77,8 +73,8 @@ impl From for RawMsgTransfer { source_port: domain_msg.source_port.to_string(), source_channel: domain_msg.source_channel.to_string(), token: domain_msg.token, - sender: account_to_string(domain_msg.sender).unwrap(), - receiver: account_to_string(domain_msg.receiver).unwrap(), + sender: domain_msg.sender.to_string(), + receiver: domain_msg.receiver.to_string(), timeout_height: Some(domain_msg.timeout_height.try_into().unwrap()), timeout_timestamp: 0, } diff --git a/modules/src/ics02_client/handler/create_client.rs b/modules/src/ics02_client/handler/create_client.rs index b19446ee97..e4f1629adf 100644 --- a/modules/src/ics02_client/handler/create_client.rs +++ b/modules/src/ics02_client/handler/create_client.rs @@ -139,7 +139,7 @@ mod tests { ..height })) .into(), - signer, + signer.clone(), ) .unwrap(), MsgCreateAnyClient::new( @@ -153,7 +153,7 @@ mod tests { ..height })) .into(), - signer, + signer.clone(), ) .unwrap(), MsgCreateAnyClient::new( diff --git a/modules/src/ics02_client/handler/update_client.rs b/modules/src/ics02_client/handler/update_client.rs index 7c3cc4c1aa..66565a1976 100644 --- a/modules/src/ics02_client/handler/update_client.rs +++ b/modules/src/ics02_client/handler/update_client.rs @@ -181,7 +181,7 @@ mod tests { let msg = MsgUpdateAnyClient { client_id: cid.clone(), header: MockHeader::new(update_height).into(), - signer, + signer: signer.clone(), }; let output = dispatch(&ctx, ClientMsg::UpdateClient(msg.clone())); diff --git a/modules/src/ics02_client/msgs/create_client.rs b/modules/src/ics02_client/msgs/create_client.rs index 0ce5677fdd..197eb902c6 100644 --- a/modules/src/ics02_client/msgs/create_client.rs +++ b/modules/src/ics02_client/msgs/create_client.rs @@ -6,15 +6,14 @@ use std::convert::TryFrom; -use tendermint::account::Id as AccountId; use tendermint_proto::Protobuf; use ibc_proto::ibc::core::client::v1::MsgCreateClient as RawMsgCreateClient; -use crate::address::{account_to_string, string_to_account}; use crate::ics02_client::client_def::{AnyClientState, AnyConsensusState}; use crate::ics02_client::error; use crate::ics02_client::error::{Error, Kind}; +use crate::signer::Signer; use crate::tx_msg::Msg; pub const TYPE_URL: &str = "/ibc.core.client.v1.MsgCreateClient"; @@ -24,14 +23,14 @@ pub const TYPE_URL: &str = "/ibc.core.client.v1.MsgCreateClient"; pub struct MsgCreateAnyClient { pub client_state: AnyClientState, pub consensus_state: AnyConsensusState, - pub signer: AccountId, + pub signer: Signer, } impl MsgCreateAnyClient { pub fn new( client_state: AnyClientState, consensus_state: AnyConsensusState, - signer: AccountId, + signer: Signer, ) -> Result { if client_state.client_type() != consensus_state.client_type() { return Err(error::Kind::RawClientAndConsensusStateTypesMismatch { @@ -46,9 +45,11 @@ impl MsgCreateAnyClient { signer, }) } + pub fn client_state(&self) -> AnyClientState { self.client_state.clone() } + pub fn consensus_state(&self) -> AnyConsensusState { self.consensus_state.clone() } @@ -56,6 +57,7 @@ impl MsgCreateAnyClient { impl Msg for MsgCreateAnyClient { type ValidationError = crate::ics24_host::error::ValidationError; + type Raw = RawMsgCreateClient; fn route(&self) -> String { crate::keys::ROUTER_KEY.to_string() @@ -64,10 +66,6 @@ impl Msg for MsgCreateAnyClient { fn type_url(&self) -> String { TYPE_URL.to_string() } - - fn get_signers(&self) -> Vec { - vec![self.signer] - } } impl Protobuf for MsgCreateAnyClient {} @@ -84,14 +82,12 @@ impl TryFrom for MsgCreateAnyClient { .consensus_state .ok_or_else(|| Kind::InvalidRawConsensusState.context("missing consensus state"))?; - let signer = string_to_account(raw.signer).map_err(|e| Kind::InvalidAddress.context(e))?; - MsgCreateAnyClient::new( AnyClientState::try_from(raw_client_state) .map_err(|e| Kind::InvalidRawClientState.context(e))?, AnyConsensusState::try_from(raw_consensus_state) .map_err(|e| Kind::InvalidRawConsensusState.context(e))?, - signer, + raw.signer.into(), ) } } @@ -101,7 +97,7 @@ impl From for RawMsgCreateClient { RawMsgCreateClient { client_state: Some(ics_msg.client_state.into()), consensus_state: Some(ics_msg.consensus_state.into()), - signer: account_to_string(ics_msg.signer).unwrap(), + signer: ics_msg.signer.to_string(), } } } diff --git a/modules/src/ics02_client/msgs/update_client.rs b/modules/src/ics02_client/msgs/update_client.rs index 23aa97363b..227fb8e337 100644 --- a/modules/src/ics02_client/msgs/update_client.rs +++ b/modules/src/ics02_client/msgs/update_client.rs @@ -6,15 +6,14 @@ use std::convert::TryFrom; -use tendermint::account::Id as AccountId; use tendermint_proto::Protobuf; use ibc_proto::ibc::core::client::v1::MsgUpdateClient as RawMsgUpdateClient; -use crate::address::{account_to_string, string_to_account}; use crate::ics02_client::client_def::AnyHeader; use crate::ics02_client::error::{Error, Kind}; use crate::ics24_host::identifier::ClientId; +use crate::signer::Signer; use crate::tx_msg::Msg; pub const TYPE_URL: &str = "/ibc.core.client.v1.MsgUpdateClient"; @@ -24,11 +23,11 @@ pub const TYPE_URL: &str = "/ibc.core.client.v1.MsgUpdateClient"; pub struct MsgUpdateAnyClient { pub client_id: ClientId, pub header: AnyHeader, - pub signer: AccountId, + pub signer: Signer, } impl MsgUpdateAnyClient { - pub fn new(client_id: ClientId, header: AnyHeader, signer: AccountId) -> Self { + pub fn new(client_id: ClientId, header: AnyHeader, signer: Signer) -> Self { MsgUpdateAnyClient { client_id, header, @@ -39,6 +38,7 @@ impl MsgUpdateAnyClient { impl Msg for MsgUpdateAnyClient { type ValidationError = crate::ics24_host::error::ValidationError; + type Raw = RawMsgUpdateClient; fn route(&self) -> String { crate::keys::ROUTER_KEY.to_string() @@ -47,10 +47,6 @@ impl Msg for MsgUpdateAnyClient { fn type_url(&self) -> String { TYPE_URL.to_string() } - - fn get_signers(&self) -> Vec { - vec![self.signer] - } } impl Protobuf for MsgUpdateAnyClient {} @@ -60,12 +56,11 @@ impl TryFrom for MsgUpdateAnyClient { fn try_from(raw: RawMsgUpdateClient) -> Result { let raw_header = raw.header.ok_or(Kind::InvalidRawHeader)?; - let signer = string_to_account(raw.signer).map_err(|e| Kind::InvalidAddress.context(e))?; Ok(MsgUpdateAnyClient { client_id: raw.client_id.parse().unwrap(), header: AnyHeader::try_from(raw_header).unwrap(), - signer, + signer: raw.signer.into(), }) } } @@ -75,7 +70,7 @@ impl From for RawMsgUpdateClient { RawMsgUpdateClient { client_id: ics_msg.client_id.to_string(), header: Some(ics_msg.header.into()), - signer: account_to_string(ics_msg.signer).unwrap(), + signer: ics_msg.signer.to_string(), } } } diff --git a/modules/src/ics03_connection/msgs/conn_open_ack.rs b/modules/src/ics03_connection/msgs/conn_open_ack.rs index 670ed8dbe5..6a2c845cf1 100644 --- a/modules/src/ics03_connection/msgs/conn_open_ack.rs +++ b/modules/src/ics03_connection/msgs/conn_open_ack.rs @@ -1,17 +1,16 @@ use std::convert::{TryFrom, TryInto}; -use tendermint::account::Id as AccountId; use tendermint_proto::Protobuf; use ibc_proto::ibc::core::connection::v1::MsgConnectionOpenAck as RawMsgConnectionOpenAck; -use crate::address::{account_to_string, string_to_account}; use crate::ics02_client::client_def::AnyClientState; use crate::ics03_connection::error::{Error, Kind}; use crate::ics03_connection::version::Version; use crate::ics23_commitment::commitment::CommitmentProofBytes; use crate::ics24_host::identifier::ConnectionId; use crate::proofs::{ConsensusProof, Proofs}; +use crate::signer::Signer; use crate::tx_msg::Msg; use crate::Height; @@ -25,7 +24,7 @@ pub struct MsgConnectionOpenAck { pub client_state: Option, pub proofs: Proofs, pub version: Version, - pub signer: AccountId, + pub signer: Signer, } impl MsgConnectionOpenAck { @@ -66,6 +65,7 @@ impl MsgConnectionOpenAck { impl Msg for MsgConnectionOpenAck { type ValidationError = Error; + type Raw = RawMsgConnectionOpenAck; fn route(&self) -> String { crate::keys::ROUTER_KEY.to_string() @@ -74,10 +74,6 @@ impl Msg for MsgConnectionOpenAck { fn type_url(&self) -> String { TYPE_URL.to_string() } - - fn get_signers(&self) -> Vec { - vec![self.signer] - } } impl Protobuf for MsgConnectionOpenAck {} @@ -86,8 +82,6 @@ impl TryFrom for MsgConnectionOpenAck { type Error = anomaly::Error; fn try_from(msg: RawMsgConnectionOpenAck) -> Result { - let signer = string_to_account(msg.signer).map_err(|e| Kind::InvalidAddress.context(e))?; - let consensus_height = msg .consensus_height .ok_or(Kind::MissingConsensusHeight)? @@ -133,7 +127,7 @@ impl TryFrom for MsgConnectionOpenAck { proof_height, ) .map_err(|e| Kind::InvalidProof.context(e))?, - signer, + signer: msg.signer.into(), }) } } @@ -162,7 +156,7 @@ impl From for RawMsgConnectionOpenAck { .consensus_proof() .map_or_else(|| None, |h| Some(h.height().into())), version: Some(ics_msg.version.into()), - signer: account_to_string(ics_msg.signer).unwrap(), + signer: ics_msg.signer.to_string(), } } } diff --git a/modules/src/ics03_connection/msgs/conn_open_confirm.rs b/modules/src/ics03_connection/msgs/conn_open_confirm.rs index 5afdf89bbb..cfe48e4dec 100644 --- a/modules/src/ics03_connection/msgs/conn_open_confirm.rs +++ b/modules/src/ics03_connection/msgs/conn_open_confirm.rs @@ -1,14 +1,14 @@ use std::convert::{TryFrom, TryInto}; -use tendermint::account::Id as AccountId; use tendermint_proto::Protobuf; use ibc_proto::ibc::core::connection::v1::MsgConnectionOpenConfirm as RawMsgConnectionOpenConfirm; -use crate::address::{account_to_string, string_to_account}; use crate::ics03_connection::error::{Error, Kind}; use crate::ics24_host::identifier::ConnectionId; -use crate::{proofs::Proofs, tx_msg::Msg}; +use crate::proofs::Proofs; +use crate::signer::Signer; +use crate::tx_msg::Msg; pub const TYPE_URL: &str = "/ibc.core.connection.v1.MsgConnectionOpenConfirm"; @@ -19,7 +19,7 @@ pub const TYPE_URL: &str = "/ibc.core.connection.v1.MsgConnectionOpenConfirm"; pub struct MsgConnectionOpenConfirm { pub connection_id: ConnectionId, pub proofs: Proofs, - pub signer: AccountId, + pub signer: Signer, } impl MsgConnectionOpenConfirm { @@ -36,6 +36,7 @@ impl MsgConnectionOpenConfirm { impl Msg for MsgConnectionOpenConfirm { type ValidationError = Error; + type Raw = RawMsgConnectionOpenConfirm; fn route(&self) -> String { crate::keys::ROUTER_KEY.to_string() @@ -44,10 +45,6 @@ impl Msg for MsgConnectionOpenConfirm { fn type_url(&self) -> String { TYPE_URL.to_string() } - - fn get_signers(&self) -> Vec { - vec![self.signer] - } } impl Protobuf for MsgConnectionOpenConfirm {} @@ -56,8 +53,6 @@ impl TryFrom for MsgConnectionOpenConfirm { type Error = anomaly::Error; fn try_from(msg: RawMsgConnectionOpenConfirm) -> Result { - let signer = string_to_account(msg.signer).map_err(|e| Kind::InvalidAddress.context(e))?; - let proof_height = msg .proof_height .ok_or(Kind::MissingProofHeight)? @@ -70,7 +65,7 @@ impl TryFrom for MsgConnectionOpenConfirm { .map_err(|e| Kind::IdentifierError.context(e))?, proofs: Proofs::new(msg.proof_ack.into(), None, None, None, proof_height) .map_err(|e| Kind::InvalidProof.context(e))?, - signer, + signer: msg.signer.into(), }) } } @@ -81,7 +76,7 @@ impl From for RawMsgConnectionOpenConfirm { connection_id: ics_msg.connection_id.as_str().to_string(), proof_ack: ics_msg.proofs.object_proof().clone().into(), proof_height: Some(ics_msg.proofs.height().into()), - signer: account_to_string(ics_msg.signer).unwrap(), + signer: ics_msg.signer.to_string(), } } } diff --git a/modules/src/ics03_connection/msgs/conn_open_init.rs b/modules/src/ics03_connection/msgs/conn_open_init.rs index f1bd0f3440..ff22a260b5 100644 --- a/modules/src/ics03_connection/msgs/conn_open_init.rs +++ b/modules/src/ics03_connection/msgs/conn_open_init.rs @@ -3,13 +3,11 @@ use std::convert::{TryFrom, TryInto}; use ibc_proto::ibc::core::connection::v1::MsgConnectionOpenInit as RawMsgConnectionOpenInit; use tendermint_proto::Protobuf; -use tendermint::account::Id as AccountId; - -use crate::address::{account_to_string, string_to_account}; use crate::ics03_connection::connection::Counterparty; use crate::ics03_connection::error::{Error, Kind}; use crate::ics03_connection::version::Version; use crate::ics24_host::identifier::ClientId; +use crate::signer::Signer; use crate::tx_msg::Msg; pub const TYPE_URL: &str = "/ibc.core.connection.v1.MsgConnectionOpenInit"; @@ -23,7 +21,7 @@ pub struct MsgConnectionOpenInit { pub counterparty: Counterparty, pub version: Version, pub delay_period: u64, - pub signer: AccountId, + pub signer: Signer, } impl MsgConnectionOpenInit { @@ -40,6 +38,7 @@ impl MsgConnectionOpenInit { impl Msg for MsgConnectionOpenInit { type ValidationError = Error; + type Raw = RawMsgConnectionOpenInit; fn route(&self) -> String { crate::keys::ROUTER_KEY.to_string() @@ -48,10 +47,6 @@ impl Msg for MsgConnectionOpenInit { fn type_url(&self) -> String { TYPE_URL.to_string() } - - fn get_signers(&self) -> Vec { - vec![self.signer] - } } impl Protobuf for MsgConnectionOpenInit {} @@ -60,8 +55,6 @@ impl TryFrom for MsgConnectionOpenInit { type Error = anomaly::Error; fn try_from(msg: RawMsgConnectionOpenInit) -> Result { - let signer = string_to_account(msg.signer).map_err(|e| Kind::InvalidAddress.context(e))?; - Ok(Self { client_id: msg .client_id @@ -77,7 +70,7 @@ impl TryFrom for MsgConnectionOpenInit { .try_into() .map_err(|e| Kind::InvalidVersion.context(e))?, delay_period: msg.delay_period, - signer, + signer: msg.signer.into(), }) } } @@ -89,7 +82,7 @@ impl From for RawMsgConnectionOpenInit { counterparty: Some(ics_msg.counterparty.into()), version: Some(ics_msg.version.into()), delay_period: ics_msg.delay_period, - signer: account_to_string(ics_msg.signer).unwrap(), + signer: ics_msg.signer.to_string(), } } } diff --git a/modules/src/ics03_connection/msgs/conn_open_try.rs b/modules/src/ics03_connection/msgs/conn_open_try.rs index 936e939562..0997936442 100644 --- a/modules/src/ics03_connection/msgs/conn_open_try.rs +++ b/modules/src/ics03_connection/msgs/conn_open_try.rs @@ -1,12 +1,10 @@ use std::convert::{TryFrom, TryInto}; use std::str::FromStr; -use tendermint::account::Id as AccountId; use tendermint_proto::Protobuf; use ibc_proto::ibc::core::connection::v1::MsgConnectionOpenTry as RawMsgConnectionOpenTry; -use crate::address::{account_to_string, string_to_account}; use crate::ics02_client::client_def::AnyClientState; use crate::ics03_connection::connection::Counterparty; use crate::ics03_connection::error::{Error, Kind}; @@ -14,6 +12,7 @@ use crate::ics03_connection::version::Version; use crate::ics23_commitment::commitment::CommitmentProofBytes; use crate::ics24_host::identifier::{ClientId, ConnectionId}; use crate::proofs::{ConsensusProof, Proofs}; +use crate::signer::Signer; use crate::tx_msg::Msg; use crate::Height; @@ -30,8 +29,8 @@ pub struct MsgConnectionOpenTry { pub counterparty: Counterparty, pub counterparty_versions: Vec, pub proofs: Proofs, - pub delay_period: u64, - pub signer: AccountId, + pub delay_period: u64, // FIXME(romac): Introduce newtype for `delay_period` + pub signer: Signer, } impl MsgConnectionOpenTry { @@ -77,6 +76,7 @@ impl MsgConnectionOpenTry { impl Msg for MsgConnectionOpenTry { type ValidationError = Error; + type Raw = RawMsgConnectionOpenTry; fn route(&self) -> String { crate::keys::ROUTER_KEY.to_string() @@ -85,10 +85,6 @@ impl Msg for MsgConnectionOpenTry { fn type_url(&self) -> String { TYPE_URL.to_string() } - - fn get_signers(&self) -> Vec { - vec![self.signer] - } } impl Protobuf for MsgConnectionOpenTry {} @@ -160,7 +156,7 @@ impl TryFrom for MsgConnectionOpenTry { ) .map_err(|e| Kind::InvalidProof.context(e))?, delay_period: msg.delay_period, - signer: string_to_account(msg.signer).map_err(|e| Kind::InvalidAddress.context(e))?, + signer: msg.signer.into(), }) } } @@ -197,7 +193,7 @@ impl From for RawMsgConnectionOpenTry { .proofs .consensus_proof() .map_or_else(|| None, |h| Some(h.height().into())), - signer: account_to_string(ics_msg.signer).unwrap(), + signer: ics_msg.signer.to_string(), } } } diff --git a/modules/src/ics04_channel/msgs/acknowledgement.rs b/modules/src/ics04_channel/msgs/acknowledgement.rs index 7ff55ca996..a89003cf5d 100644 --- a/modules/src/ics04_channel/msgs/acknowledgement.rs +++ b/modules/src/ics04_channel/msgs/acknowledgement.rs @@ -1,14 +1,14 @@ use std::convert::{TryFrom, TryInto}; -use tendermint::account::Id as AccountId; use tendermint_proto::Protobuf; use ibc_proto::ibc::core::channel::v1::MsgAcknowledgement as RawMsgAcknowledgement; -use crate::address::{account_to_string, string_to_account}; use crate::ics04_channel::error::{Error, Kind}; use crate::ics04_channel::packet::Packet; -use crate::{proofs::Proofs, tx_msg::Msg}; +use crate::proofs::Proofs; +use crate::signer::Signer; +use crate::tx_msg::Msg; pub const TYPE_URL: &str = "/ibc.core.channel.v1.MsgAcknowledgement"; @@ -18,9 +18,9 @@ pub const TYPE_URL: &str = "/ibc.core.channel.v1.MsgAcknowledgement"; #[derive(Clone, Debug, PartialEq)] pub struct MsgAcknowledgement { pub packet: Packet, - acknowledgement: Vec, - proofs: Proofs, - signer: AccountId, + pub acknowledgement: Vec, // TODO(romac): Introduce a newtype for this + pub proofs: Proofs, + pub signer: Signer, } impl MsgAcknowledgement { @@ -28,7 +28,7 @@ impl MsgAcknowledgement { packet: Packet, acknowledgement: Vec, proofs: Proofs, - signer: AccountId, + signer: Signer, ) -> MsgAcknowledgement { Self { packet, @@ -41,6 +41,7 @@ impl MsgAcknowledgement { impl Msg for MsgAcknowledgement { type ValidationError = Error; + type Raw = RawMsgAcknowledgement; fn route(&self) -> String { crate::keys::ROUTER_KEY.to_string() @@ -49,10 +50,6 @@ impl Msg for MsgAcknowledgement { fn type_url(&self) -> String { TYPE_URL.to_string() } - - fn get_signers(&self) -> Vec { - vec![self.signer] - } } impl Protobuf for MsgAcknowledgement {} @@ -61,9 +58,6 @@ impl TryFrom for MsgAcknowledgement { type Error = anomaly::Error; fn try_from(raw_msg: RawMsgAcknowledgement) -> Result { - let signer = - string_to_account(raw_msg.signer).map_err(|e| Kind::InvalidSigner.context(e))?; - let proofs = Proofs::new( raw_msg.proof_acked.into(), None, @@ -84,7 +78,7 @@ impl TryFrom for MsgAcknowledgement { .try_into() .map_err(|e| Kind::InvalidPacket.context(e))?, acknowledgement: raw_msg.acknowledgement, - signer, + signer: raw_msg.signer.into(), proofs, }) } @@ -95,7 +89,7 @@ impl From for RawMsgAcknowledgement { RawMsgAcknowledgement { packet: Some(domain_msg.packet.into()), acknowledgement: domain_msg.acknowledgement, - signer: account_to_string(domain_msg.signer).unwrap(), + signer: domain_msg.signer.to_string(), proof_height: Some(domain_msg.proofs.height().into()), proof_acked: domain_msg.proofs.object_proof().clone().into(), } @@ -170,12 +164,12 @@ mod test { want_pass: false, }, Test { - name: "Missing signer".to_string(), + name: "Empty signer".to_string(), raw: RawMsgAcknowledgement { signer: "".to_string(), ..default_raw_msg.clone() }, - want_pass: false, + want_pass: true, }, Test { name: "Empty proof acked".to_string(), diff --git a/modules/src/ics04_channel/msgs/chan_close_confirm.rs b/modules/src/ics04_channel/msgs/chan_close_confirm.rs index 8418fb5d74..e4c83a61be 100644 --- a/modules/src/ics04_channel/msgs/chan_close_confirm.rs +++ b/modules/src/ics04_channel/msgs/chan_close_confirm.rs @@ -1,14 +1,14 @@ use std::convert::{TryFrom, TryInto}; -use tendermint::account::Id as AccountId; use tendermint_proto::Protobuf; use ibc_proto::ibc::core::channel::v1::MsgChannelCloseConfirm as RawMsgChannelCloseConfirm; -use crate::address::{account_to_string, string_to_account}; use crate::ics04_channel::error::{Error, Kind}; use crate::ics24_host::identifier::{ChannelId, PortId}; -use crate::{proofs::Proofs, tx_msg::Msg}; +use crate::proofs::Proofs; +use crate::signer::Signer; +use crate::tx_msg::Msg; pub const TYPE_URL: &str = "/ibc.core.channel.v1.MsgChannelCloseConfirm"; @@ -21,26 +21,19 @@ pub struct MsgChannelCloseConfirm { pub port_id: PortId, pub channel_id: ChannelId, pub proofs: Proofs, - pub signer: AccountId, + pub signer: Signer, } -impl Msg for MsgChannelCloseConfirm { - type ValidationError = Error; - - fn route(&self) -> String { - crate::keys::ROUTER_KEY.to_string() - } - - fn type_url(&self) -> String { - TYPE_URL.to_string() - } - - fn get_signers(&self) -> Vec { - vec![self.signer] +impl MsgChannelCloseConfirm { + pub fn new(port_id: PortId, channel_id: ChannelId, proofs: Proofs, signer: Signer) -> Self { + Self { + port_id, + channel_id, + proofs, + signer, + } } -} -impl MsgChannelCloseConfirm { /// Getter: borrow the `port_id` from this message. pub fn port_id(&self) -> &PortId { &self.port_id @@ -53,15 +46,25 @@ impl MsgChannelCloseConfirm { } } +impl Msg for MsgChannelCloseConfirm { + type ValidationError = Error; + type Raw = RawMsgChannelCloseConfirm; + + fn route(&self) -> String { + crate::keys::ROUTER_KEY.to_string() + } + + fn type_url(&self) -> String { + TYPE_URL.to_string() + } +} + impl Protobuf for MsgChannelCloseConfirm {} impl TryFrom for MsgChannelCloseConfirm { type Error = anomaly::Error; fn try_from(raw_msg: RawMsgChannelCloseConfirm) -> Result { - let signer = - string_to_account(raw_msg.signer).map_err(|e| Kind::InvalidSigner.context(e))?; - let proofs = Proofs::new( raw_msg.proof_init.into(), None, @@ -85,7 +88,7 @@ impl TryFrom for MsgChannelCloseConfirm { .parse() .map_err(|e| Kind::IdentifierError.context(e))?, proofs, - signer, + signer: raw_msg.signer.into(), }) } } @@ -97,7 +100,7 @@ impl From for RawMsgChannelCloseConfirm { channel_id: domain_msg.channel_id.to_string(), proof_init: domain_msg.proofs.object_proof().clone().into(), proof_height: Some(domain_msg.proofs.height().into()), - signer: account_to_string(domain_msg.signer).unwrap(), + signer: domain_msg.signer.to_string(), } } } diff --git a/modules/src/ics04_channel/msgs/chan_close_init.rs b/modules/src/ics04_channel/msgs/chan_close_init.rs index 77cc4b1e66..9d7544e5a1 100644 --- a/modules/src/ics04_channel/msgs/chan_close_init.rs +++ b/modules/src/ics04_channel/msgs/chan_close_init.rs @@ -1,13 +1,12 @@ use std::convert::TryFrom; -use tendermint::account::Id as AccountId; use tendermint_proto::Protobuf; use ibc_proto::ibc::core::channel::v1::MsgChannelCloseInit as RawMsgChannelCloseInit; -use crate::address::{account_to_string, string_to_account}; use crate::ics04_channel::error::{Error, Kind}; use crate::ics24_host::identifier::{ChannelId, PortId}; +use crate::signer::Signer; use crate::tx_msg::Msg; pub const TYPE_URL: &str = "/ibc.core.channel.v1.MsgChannelCloseInit"; @@ -19,10 +18,18 @@ pub const TYPE_URL: &str = "/ibc.core.channel.v1.MsgChannelCloseInit"; pub struct MsgChannelCloseInit { pub port_id: PortId, pub channel_id: ChannelId, - pub signer: AccountId, + pub signer: Signer, } impl MsgChannelCloseInit { + pub fn new(port_id: PortId, channel_id: ChannelId, signer: Signer) -> Self { + Self { + port_id, + channel_id, + signer, + } + } + /// Getter: borrow the `port_id` from this message. pub fn port_id(&self) -> &PortId { &self.port_id @@ -34,6 +41,7 @@ impl MsgChannelCloseInit { impl Msg for MsgChannelCloseInit { type ValidationError = Error; + type Raw = RawMsgChannelCloseInit; fn route(&self) -> String { crate::keys::ROUTER_KEY.to_string() @@ -42,10 +50,6 @@ impl Msg for MsgChannelCloseInit { fn type_url(&self) -> String { TYPE_URL.to_string() } - - fn get_signers(&self) -> Vec { - vec![self.signer] - } } impl Protobuf for MsgChannelCloseInit {} @@ -54,9 +58,6 @@ impl TryFrom for MsgChannelCloseInit { type Error = anomaly::Error; fn try_from(raw_msg: RawMsgChannelCloseInit) -> Result { - let signer = - string_to_account(raw_msg.signer).map_err(|e| Kind::InvalidSigner.context(e))?; - Ok(MsgChannelCloseInit { port_id: raw_msg .port_id @@ -66,7 +67,7 @@ impl TryFrom for MsgChannelCloseInit { .channel_id .parse() .map_err(|e| Kind::IdentifierError.context(e))?, - signer, + signer: raw_msg.signer.into(), }) } } @@ -76,7 +77,7 @@ impl From for RawMsgChannelCloseInit { RawMsgChannelCloseInit { port_id: domain_msg.port_id.to_string(), channel_id: domain_msg.channel_id.to_string(), - signer: account_to_string(domain_msg.signer).unwrap(), + signer: domain_msg.signer.to_string(), } } } diff --git a/modules/src/ics04_channel/msgs/chan_open_ack.rs b/modules/src/ics04_channel/msgs/chan_open_ack.rs index 6c1277f67d..bf3edd1969 100644 --- a/modules/src/ics04_channel/msgs/chan_open_ack.rs +++ b/modules/src/ics04_channel/msgs/chan_open_ack.rs @@ -1,11 +1,11 @@ -use crate::address::{account_to_string, string_to_account}; use crate::ics04_channel::channel::validate_version; use crate::ics04_channel::error::{Error, Kind}; use crate::ics24_host::identifier::{ChannelId, PortId}; -use crate::{proofs::Proofs, tx_msg::Msg}; +use crate::proofs::Proofs; +use crate::signer::Signer; +use crate::tx_msg::Msg; use ibc_proto::ibc::core::channel::v1::MsgChannelOpenAck as RawMsgChannelOpenAck; -use tendermint::account::Id as AccountId; use tendermint_proto::Protobuf; use std::convert::{TryFrom, TryInto}; @@ -20,12 +20,30 @@ pub struct MsgChannelOpenAck { pub port_id: PortId, pub channel_id: ChannelId, pub counterparty_channel_id: ChannelId, - pub counterparty_version: String, + pub counterparty_version: String, // FIXME(romac): Introduce newtype for versions pub proofs: Proofs, - pub signer: AccountId, + pub signer: Signer, } impl MsgChannelOpenAck { + pub fn new( + port_id: PortId, + channel_id: ChannelId, + counterparty_channel_id: ChannelId, + counterparty_version: String, + proofs: Proofs, + signer: Signer, + ) -> Self { + Self { + port_id, + channel_id, + counterparty_channel_id, + counterparty_version, + proofs, + signer, + } + } + /// Getter: borrow the `port_id` from this message. pub fn port_id(&self) -> &PortId { &self.port_id @@ -37,6 +55,7 @@ impl MsgChannelOpenAck { pub fn counterparty_channel_id(&self) -> &ChannelId { &self.counterparty_channel_id } + pub fn counterparty_version(&self) -> &String { &self.counterparty_version } @@ -48,6 +67,7 @@ impl MsgChannelOpenAck { impl Msg for MsgChannelOpenAck { type ValidationError = Error; + type Raw = RawMsgChannelOpenAck; fn route(&self) -> String { crate::keys::ROUTER_KEY.to_string() @@ -56,10 +76,6 @@ impl Msg for MsgChannelOpenAck { fn type_url(&self) -> String { TYPE_URL.to_string() } - - fn get_signers(&self) -> Vec { - vec![self.signer] - } } impl Protobuf for MsgChannelOpenAck {} @@ -68,9 +84,6 @@ impl TryFrom for MsgChannelOpenAck { type Error = anomaly::Error; fn try_from(raw_msg: RawMsgChannelOpenAck) -> Result { - let signer = - string_to_account(raw_msg.signer).map_err(|e| Kind::InvalidSigner.context(e))?; - let proofs = Proofs::new( raw_msg.proof_try.into(), None, @@ -99,7 +112,7 @@ impl TryFrom for MsgChannelOpenAck { .map_err(|e| Kind::IdentifierError.context(e))?, counterparty_version: validate_version(raw_msg.counterparty_version)?, proofs, - signer, + signer: raw_msg.signer.into(), }) } } @@ -113,7 +126,7 @@ impl From for RawMsgChannelOpenAck { counterparty_version: domain_msg.counterparty_version.to_string(), proof_try: domain_msg.proofs.object_proof().clone().into(), proof_height: Some(domain_msg.proofs.height().into()), - signer: account_to_string(domain_msg.signer).unwrap(), + signer: domain_msg.signer.to_string(), } } } diff --git a/modules/src/ics04_channel/msgs/chan_open_confirm.rs b/modules/src/ics04_channel/msgs/chan_open_confirm.rs index d011eae041..f4820cc583 100644 --- a/modules/src/ics04_channel/msgs/chan_open_confirm.rs +++ b/modules/src/ics04_channel/msgs/chan_open_confirm.rs @@ -1,11 +1,10 @@ -use crate::address::{account_to_string, string_to_account}; use crate::ics04_channel::error::{Error, Kind}; -use crate::ics23_commitment::commitment::CommitmentProofBytes; use crate::ics24_host::identifier::{ChannelId, PortId}; -use crate::{proofs::Proofs, tx_msg::Msg, Height}; +use crate::proofs::Proofs; +use crate::signer::Signer; +use crate::tx_msg::Msg; use ibc_proto::ibc::core::channel::v1::MsgChannelOpenConfirm as RawMsgChannelOpenConfirm; -use tendermint::account::Id as AccountId; use tendermint_proto::Protobuf; use std::convert::{TryFrom, TryInto}; @@ -21,32 +20,20 @@ pub struct MsgChannelOpenConfirm { pub port_id: PortId, pub channel_id: ChannelId, pub proofs: Proofs, - pub signer: AccountId, + pub signer: Signer, } impl MsgChannelOpenConfirm { - #[allow(dead_code)] - // TODO: Not in use (yet), hence private. - fn new( - port_id: String, - channel_id: String, - proof_ack: CommitmentProofBytes, - proofs_height: Height, - signer: AccountId, - ) -> Result { - Ok(Self { - port_id: port_id - .parse() - .map_err(|e| Kind::IdentifierError.context(e))?, - channel_id: channel_id - .parse() - .map_err(|e| Kind::IdentifierError.context(e))?, - proofs: Proofs::new(proof_ack, None, None, None, proofs_height) - .map_err(|e| Kind::InvalidProof.context(e))?, + pub fn new(port_id: PortId, channel_id: ChannelId, proofs: Proofs, signer: Signer) -> Self { + Self { + port_id, + channel_id, + proofs, signer, - }) + } } } + impl MsgChannelOpenConfirm { /// Getter: borrow the `port_id` from this message. pub fn port_id(&self) -> &PortId { @@ -62,6 +49,7 @@ impl MsgChannelOpenConfirm { impl Msg for MsgChannelOpenConfirm { type ValidationError = Error; + type Raw = RawMsgChannelOpenConfirm; fn route(&self) -> String { crate::keys::ROUTER_KEY.to_string() @@ -70,10 +58,6 @@ impl Msg for MsgChannelOpenConfirm { fn type_url(&self) -> String { TYPE_URL.to_string() } - - fn get_signers(&self) -> Vec { - vec![self.signer] - } } impl Protobuf for MsgChannelOpenConfirm {} @@ -82,9 +66,6 @@ impl TryFrom for MsgChannelOpenConfirm { type Error = anomaly::Error; fn try_from(raw_msg: RawMsgChannelOpenConfirm) -> Result { - let signer = - string_to_account(raw_msg.signer).map_err(|e| Kind::InvalidSigner.context(e))?; - let proofs = Proofs::new( raw_msg.proof_ack.into(), None, @@ -108,7 +89,7 @@ impl TryFrom for MsgChannelOpenConfirm { .parse() .map_err(|e| Kind::IdentifierError.context(e))?, proofs, - signer, + signer: raw_msg.signer.into(), }) } } @@ -120,7 +101,7 @@ impl From for RawMsgChannelOpenConfirm { channel_id: domain_msg.channel_id.to_string(), proof_ack: domain_msg.proofs.object_proof().clone().into(), proof_height: Some(domain_msg.proofs.height().into()), - signer: account_to_string(domain_msg.signer).unwrap(), + signer: domain_msg.signer.to_string(), } } } diff --git a/modules/src/ics04_channel/msgs/chan_open_init.rs b/modules/src/ics04_channel/msgs/chan_open_init.rs index 75f3aa27b3..c4009ea77b 100644 --- a/modules/src/ics04_channel/msgs/chan_open_init.rs +++ b/modules/src/ics04_channel/msgs/chan_open_init.rs @@ -1,11 +1,10 @@ -use crate::address::{account_to_string, string_to_account}; use crate::ics04_channel::channel::ChannelEnd; use crate::ics04_channel::error::{Error, Kind}; use crate::ics24_host::identifier::PortId; +use crate::signer::Signer; use crate::tx_msg::Msg; use ibc_proto::ibc::core::channel::v1::MsgChannelOpenInit as RawMsgChannelOpenInit; -use tendermint::account::Id as AccountId; use tendermint_proto::Protobuf; use std::convert::{TryFrom, TryInto}; @@ -19,10 +18,18 @@ pub const TYPE_URL: &str = "/ibc.core.channel.v1.MsgChannelOpenInit"; pub struct MsgChannelOpenInit { pub port_id: PortId, pub channel: ChannelEnd, - pub signer: AccountId, + pub signer: Signer, } impl MsgChannelOpenInit { + pub fn new(port_id: PortId, channel: ChannelEnd, signer: Signer) -> Self { + Self { + port_id, + channel, + signer, + } + } + /// Getter: borrow the `port_id` from this message. pub fn port_id(&self) -> &PortId { &self.port_id @@ -36,6 +43,7 @@ impl MsgChannelOpenInit { impl Msg for MsgChannelOpenInit { type ValidationError = Error; + type Raw = RawMsgChannelOpenInit; fn route(&self) -> String { crate::keys::ROUTER_KEY.to_string() @@ -44,10 +52,6 @@ impl Msg for MsgChannelOpenInit { fn type_url(&self) -> String { TYPE_URL.to_string() } - - fn get_signers(&self) -> Vec { - vec![self.signer] - } } impl Protobuf for MsgChannelOpenInit {} @@ -56,16 +60,13 @@ impl TryFrom for MsgChannelOpenInit { type Error = anomaly::Error; fn try_from(raw_msg: RawMsgChannelOpenInit) -> Result { - let signer = - string_to_account(raw_msg.signer).map_err(|e| Kind::InvalidSigner.context(e))?; - Ok(MsgChannelOpenInit { port_id: raw_msg .port_id .parse() .map_err(|e| Kind::IdentifierError.context(e))?, channel: raw_msg.channel.ok_or(Kind::MissingChannel)?.try_into()?, - signer, + signer: raw_msg.signer.into(), }) } } @@ -75,7 +76,7 @@ impl From for RawMsgChannelOpenInit { RawMsgChannelOpenInit { port_id: domain_msg.port_id.to_string(), channel: Some(domain_msg.channel.into()), - signer: account_to_string(domain_msg.signer).unwrap(), + signer: domain_msg.signer.to_string(), } } } diff --git a/modules/src/ics04_channel/msgs/chan_open_try.rs b/modules/src/ics04_channel/msgs/chan_open_try.rs index 0a4c5dba1e..3cec7334b5 100644 --- a/modules/src/ics04_channel/msgs/chan_open_try.rs +++ b/modules/src/ics04_channel/msgs/chan_open_try.rs @@ -1,15 +1,13 @@ use crate::ics04_channel::channel::{validate_version, ChannelEnd}; use crate::ics04_channel::error::{Error, Kind}; +use crate::ics24_host::error::ValidationError; use crate::ics24_host::error::ValidationKind; use crate::ics24_host::identifier::{ChannelId, PortId}; -use crate::{ - address::{account_to_string, string_to_account}, - ics24_host::error::ValidationError, -}; -use crate::{proofs::Proofs, tx_msg::Msg}; +use crate::proofs::Proofs; +use crate::signer::Signer; +use crate::tx_msg::Msg; use ibc_proto::ibc::core::channel::v1::MsgChannelOpenTry as RawMsgChannelOpenTry; -use tendermint::account::Id as AccountId; use tendermint_proto::Protobuf; use std::convert::{TryFrom, TryInto}; @@ -25,12 +23,30 @@ pub struct MsgChannelOpenTry { pub port_id: PortId, pub previous_channel_id: Option, pub channel: ChannelEnd, - pub counterparty_version: String, + pub counterparty_version: String, // TODO(romac): newtype this pub proofs: Proofs, - pub signer: AccountId, + pub signer: Signer, } impl MsgChannelOpenTry { + pub fn new( + port_id: PortId, + previous_channel_id: Option, + channel: ChannelEnd, + counterparty_version: String, + proofs: Proofs, + signer: Signer, + ) -> Self { + Self { + port_id, + previous_channel_id, + channel, + counterparty_version, + proofs, + signer, + } + } + /// Getter: borrow the `port_id` from this message. pub fn port_id(&self) -> &PortId { &self.port_id @@ -50,6 +66,7 @@ impl MsgChannelOpenTry { } impl Msg for MsgChannelOpenTry { type ValidationError = Error; + type Raw = RawMsgChannelOpenTry; fn route(&self) -> String { crate::keys::ROUTER_KEY.to_string() @@ -59,10 +76,6 @@ impl Msg for MsgChannelOpenTry { TYPE_URL.to_string() } - fn get_signers(&self) -> Vec { - vec![self.signer] - } - fn validate_basic(&self) -> Result<(), ValidationError> { match self.channel().counterparty().channel_id() { None => Err(ValidationKind::InvalidCounterpartyChannelId.into()), @@ -77,9 +90,6 @@ impl TryFrom for MsgChannelOpenTry { type Error = anomaly::Error; fn try_from(raw_msg: RawMsgChannelOpenTry) -> Result { - let signer = - string_to_account(raw_msg.signer).map_err(|e| Kind::InvalidSigner.context(e))?; - let proofs = Proofs::new( raw_msg.proof_init.into(), None, @@ -108,7 +118,7 @@ impl TryFrom for MsgChannelOpenTry { channel: raw_msg.channel.ok_or(Kind::MissingChannel)?.try_into()?, counterparty_version: validate_version(raw_msg.counterparty_version)?, proofs, - signer, + signer: raw_msg.signer.into(), }; match msg.validate_basic() { @@ -129,7 +139,7 @@ impl From for RawMsgChannelOpenTry { counterparty_version: domain_msg.counterparty_version, proof_init: domain_msg.proofs.object_proof().clone().into(), proof_height: Some(domain_msg.proofs.height().into()), - signer: account_to_string(domain_msg.signer).unwrap(), + signer: domain_msg.signer.to_string(), } } } diff --git a/modules/src/ics04_channel/msgs/recv_packet.rs b/modules/src/ics04_channel/msgs/recv_packet.rs index 982726466c..a28193f8ab 100644 --- a/modules/src/ics04_channel/msgs/recv_packet.rs +++ b/modules/src/ics04_channel/msgs/recv_packet.rs @@ -1,14 +1,14 @@ use std::convert::{TryFrom, TryInto}; -use tendermint::account::Id as AccountId; use tendermint_proto::Protobuf; use ibc_proto::ibc::core::channel::v1::MsgRecvPacket as RawMsgRecvPacket; -use crate::address::{account_to_string, string_to_account}; use crate::ics04_channel::error::{Error, Kind}; use crate::ics04_channel::packet::Packet; -use crate::{proofs::Proofs, tx_msg::Msg}; +use crate::proofs::Proofs; +use crate::signer::Signer; +use crate::tx_msg::Msg; pub const TYPE_URL: &str = "/ibc.core.channel.v1.MsgRecvPacket"; @@ -18,22 +18,23 @@ pub const TYPE_URL: &str = "/ibc.core.channel.v1.MsgRecvPacket"; #[derive(Clone, Debug, PartialEq)] pub struct MsgRecvPacket { pub packet: Packet, - proofs: Proofs, - signer: AccountId, + pub proofs: Proofs, + pub signer: Signer, } impl MsgRecvPacket { - pub fn new(packet: Packet, proofs: Proofs, signer: AccountId) -> Result { - Ok(Self { + pub fn new(packet: Packet, proofs: Proofs, signer: Signer) -> MsgRecvPacket { + Self { packet, proofs, signer, - }) + } } } impl Msg for MsgRecvPacket { type ValidationError = Error; + type Raw = RawMsgRecvPacket; fn route(&self) -> String { crate::keys::ROUTER_KEY.to_string() @@ -42,10 +43,6 @@ impl Msg for MsgRecvPacket { fn type_url(&self) -> String { TYPE_URL.to_string() } - - fn get_signers(&self) -> Vec { - vec![self.signer] - } } impl Protobuf for MsgRecvPacket {} @@ -54,9 +51,6 @@ impl TryFrom for MsgRecvPacket { type Error = anomaly::Error; fn try_from(raw_msg: RawMsgRecvPacket) -> Result { - let signer = - string_to_account(raw_msg.signer).map_err(|e| Kind::InvalidSigner.context(e))?; - let proofs = Proofs::new( raw_msg.proof_commitment.into(), None, @@ -77,7 +71,7 @@ impl TryFrom for MsgRecvPacket { .try_into() .map_err(|e| Kind::InvalidPacket.context(e))?, proofs, - signer, + signer: raw_msg.signer.into(), }) } } @@ -88,7 +82,7 @@ impl From for RawMsgRecvPacket { packet: Some(domain_msg.packet.into()), proof_commitment: domain_msg.proofs.object_proof().clone().into(), proof_height: Some(domain_msg.proofs.height().into()), - signer: account_to_string(domain_msg.signer).unwrap(), + signer: domain_msg.signer.to_string(), } } } @@ -159,12 +153,12 @@ mod test { want_pass: false, }, Test { - name: "Missing signer".to_string(), + name: "Empty signer".to_string(), raw: RawMsgRecvPacket { signer: "".to_string(), ..default_raw_msg }, - want_pass: false, + want_pass: true, }, ]; diff --git a/modules/src/ics04_channel/msgs/timeout.rs b/modules/src/ics04_channel/msgs/timeout.rs index 1e7112f5b7..75fbdfc98b 100644 --- a/modules/src/ics04_channel/msgs/timeout.rs +++ b/modules/src/ics04_channel/msgs/timeout.rs @@ -1,14 +1,14 @@ use std::convert::{TryFrom, TryInto}; -use tendermint::account::Id as AccountId; use tendermint_proto::Protobuf; use ibc_proto::ibc::core::channel::v1::MsgTimeout as RawMsgTimeout; -use crate::address::{account_to_string, string_to_account}; use crate::ics04_channel::error::{Error, Kind}; use crate::ics04_channel::packet::{Packet, Sequence}; -use crate::{proofs::Proofs, tx_msg::Msg}; +use crate::proofs::Proofs; +use crate::signer::Signer; +use crate::tx_msg::Msg; pub const TYPE_URL: &str = "/ibc.core.channel.v1.MsgTimeout"; @@ -18,9 +18,9 @@ pub const TYPE_URL: &str = "/ibc.core.channel.v1.MsgTimeout"; #[derive(Clone, Debug, PartialEq)] pub struct MsgTimeout { pub packet: Packet, - next_sequence_recv: Sequence, - proofs: Proofs, - signer: AccountId, + pub next_sequence_recv: Sequence, + pub proofs: Proofs, + pub signer: Signer, } impl MsgTimeout { @@ -28,7 +28,7 @@ impl MsgTimeout { packet: Packet, next_sequence_recv: Sequence, proofs: Proofs, - signer: AccountId, + signer: Signer, ) -> MsgTimeout { Self { packet, @@ -41,6 +41,7 @@ impl MsgTimeout { impl Msg for MsgTimeout { type ValidationError = Error; + type Raw = RawMsgTimeout; fn route(&self) -> String { crate::keys::ROUTER_KEY.to_string() @@ -49,10 +50,6 @@ impl Msg for MsgTimeout { fn type_url(&self) -> String { TYPE_URL.to_string() } - - fn get_signers(&self) -> Vec { - vec![self.signer] - } } impl Protobuf for MsgTimeout {} @@ -61,9 +58,6 @@ impl TryFrom for MsgTimeout { type Error = anomaly::Error; fn try_from(raw_msg: RawMsgTimeout) -> Result { - let signer = - string_to_account(raw_msg.signer).map_err(|e| Kind::InvalidSigner.context(e))?; - let proofs = Proofs::new( raw_msg.proof_unreceived.into(), None, @@ -86,7 +80,7 @@ impl TryFrom for MsgTimeout { .try_into() .map_err(|e| Kind::InvalidPacket.context(e))?, next_sequence_recv: Sequence::from(raw_msg.next_sequence_recv), - signer, + signer: raw_msg.signer.into(), proofs, }) } @@ -99,7 +93,7 @@ impl From for RawMsgTimeout { proof_unreceived: domain_msg.proofs.object_proof().clone().into(), proof_height: Some(domain_msg.proofs.height().into()), next_sequence_recv: domain_msg.next_sequence_recv.into(), - signer: account_to_string(domain_msg.signer).unwrap(), + signer: domain_msg.signer.to_string(), } } } @@ -180,12 +174,12 @@ mod test { want_pass: false, }, Test { - name: "Missing signer".to_string(), + name: "Empty signer".to_string(), raw: RawMsgTimeout { signer: "".to_string(), ..default_raw_msg }, - want_pass: false, + want_pass: true, }, ]; diff --git a/modules/src/ics04_channel/msgs/timeout_on_close.rs b/modules/src/ics04_channel/msgs/timeout_on_close.rs index 1a0b21a638..7a0c67ac82 100644 --- a/modules/src/ics04_channel/msgs/timeout_on_close.rs +++ b/modules/src/ics04_channel/msgs/timeout_on_close.rs @@ -1,14 +1,14 @@ use std::convert::{TryFrom, TryInto}; -use tendermint::account::Id as AccountId; use tendermint_proto::Protobuf; use ibc_proto::ibc::core::channel::v1::MsgTimeoutOnClose as RawMsgTimeoutOnClose; -use crate::address::{account_to_string, string_to_account}; use crate::ics04_channel::error::{Error, Kind}; use crate::ics04_channel::packet::{Packet, Sequence}; -use crate::{proofs::Proofs, tx_msg::Msg}; +use crate::proofs::Proofs; +use crate::signer::Signer; +use crate::tx_msg::Msg; pub const TYPE_URL: &str = "/ibc.core.channel.v1.MsgTimeoutOnClose"; @@ -18,9 +18,9 @@ pub const TYPE_URL: &str = "/ibc.core.channel.v1.MsgTimeoutOnClose"; #[derive(Clone, Debug, PartialEq)] pub struct MsgTimeoutOnClose { pub packet: Packet, - next_sequence_recv: Sequence, - proofs: Proofs, - signer: AccountId, + pub next_sequence_recv: Sequence, + pub proofs: Proofs, + pub signer: Signer, } impl MsgTimeoutOnClose { @@ -28,7 +28,7 @@ impl MsgTimeoutOnClose { packet: Packet, next_sequence_recv: Sequence, proofs: Proofs, - signer: AccountId, + signer: Signer, ) -> MsgTimeoutOnClose { Self { packet, @@ -41,6 +41,7 @@ impl MsgTimeoutOnClose { impl Msg for MsgTimeoutOnClose { type ValidationError = Error; + type Raw = RawMsgTimeoutOnClose; fn route(&self) -> String { crate::keys::ROUTER_KEY.to_string() @@ -49,10 +50,6 @@ impl Msg for MsgTimeoutOnClose { fn type_url(&self) -> String { TYPE_URL.to_string() } - - fn get_signers(&self) -> Vec { - vec![self.signer] - } } impl Protobuf for MsgTimeoutOnClose {} @@ -61,9 +58,6 @@ impl TryFrom for MsgTimeoutOnClose { type Error = anomaly::Error; fn try_from(raw_msg: RawMsgTimeoutOnClose) -> Result { - let signer = - string_to_account(raw_msg.signer).map_err(|e| Kind::InvalidSigner.context(e))?; - let proofs = Proofs::new( raw_msg.proof_unreceived.into(), None, @@ -86,7 +80,7 @@ impl TryFrom for MsgTimeoutOnClose { .try_into() .map_err(|e| Kind::InvalidPacket.context(e))?, next_sequence_recv: Sequence::from(raw_msg.next_sequence_recv), - signer, + signer: raw_msg.signer.into(), proofs, }) } @@ -104,7 +98,7 @@ impl From for RawMsgTimeoutOnClose { .map_or_else(Vec::new, |v| v.into()), proof_height: Some(domain_msg.proofs.height().into()), next_sequence_recv: domain_msg.next_sequence_recv.into(), - signer: account_to_string(domain_msg.signer).unwrap(), + signer: domain_msg.signer.to_string(), } } } diff --git a/modules/src/ics18_relayer/context.rs b/modules/src/ics18_relayer/context.rs index 39c201d7c8..c0251b58c7 100644 --- a/modules/src/ics18_relayer/context.rs +++ b/modules/src/ics18_relayer/context.rs @@ -1,11 +1,10 @@ use prost_types::Any; -use tendermint::account::Id as AccountId; -use crate::events::IbcEvent; use crate::ics02_client::client_def::{AnyClientState, AnyHeader}; use crate::ics18_relayer::error::Error; use crate::ics24_host::identifier::ClientId; use crate::Height; +use crate::{events::IbcEvent, signer::Signer}; /// Trait capturing all dependencies (i.e., the context) which algorithms in ICS18 require to /// relay packets between chains. This trait comprises the dependencies towards a single chain. @@ -28,5 +27,5 @@ pub trait Ics18Context { fn send(&mut self, msgs: Vec) -> Result, Error>; /// Temporary solution. Similar to `CosmosSDKChain::key_and_signer()` but simpler. - fn signer(&self) -> AccountId; + fn signer(&self) -> Signer; } diff --git a/modules/src/ics26_routing/handler.rs b/modules/src/ics26_routing/handler.rs index a39e5f901c..c667648313 100644 --- a/modules/src/ics26_routing/handler.rs +++ b/modules/src/ics26_routing/handler.rs @@ -258,7 +258,7 @@ mod tests { let create_client_msg = MsgCreateAnyClient::new( AnyClientState::from(MockClientState(MockHeader::new(start_client_height))), AnyConsensusState::from(MockConsensusState(MockHeader::new(start_client_height))), - default_signer, + default_signer.clone(), ) .unwrap(); @@ -344,7 +344,7 @@ mod tests { msg: Ics26Envelope::Ics2Msg(ClientMsg::UpdateClient(MsgUpdateAnyClient { client_id: client_id.clone(), header: MockHeader::new(update_client_height).into(), - signer: default_signer, + signer: default_signer.clone(), })), want_pass: true, }, diff --git a/modules/src/lib.rs b/modules/src/lib.rs index 0615bbf910..4c5072defb 100644 --- a/modules/src/lib.rs +++ b/modules/src/lib.rs @@ -23,10 +23,16 @@ //! - ICS 26: Routing //! - Applications: //! - ICS 20: Fungible Token Transfer -pub mod address; + pub mod application; pub mod events; pub mod handler; +pub mod keys; +pub mod macros; +pub mod proofs; +pub mod signer; +pub mod tx_msg; + pub mod ics02_client; pub mod ics03_connection; pub mod ics04_channel; @@ -36,10 +42,6 @@ pub mod ics18_relayer; pub mod ics23_commitment; pub mod ics24_host; pub mod ics26_routing; -pub mod keys; -pub mod macros; -pub mod proofs; -pub mod tx_msg; mod serializers; diff --git a/modules/src/mock/context.rs b/modules/src/mock/context.rs index 24eb4f05e3..60b8139a15 100644 --- a/modules/src/mock/context.rs +++ b/modules/src/mock/context.rs @@ -3,39 +3,29 @@ use std::cmp::min; use std::collections::HashMap; use std::error::Error; -use std::str::FromStr; use prost_types::Any; use sha2::Digest; -use tendermint::account::Id; +use crate::application::ics20_fungible_token_transfer::context::Ics20Context; +use crate::events::IbcEvent; +use crate::ics02_client::client_def::{AnyClientState, AnyConsensusState, AnyHeader}; +use crate::ics02_client::client_type::ClientType; use crate::ics02_client::context::{ClientKeeper, ClientReader}; -use crate::ics02_client::error::Error as Ics2Error; -use crate::{ - ics02_client::client_def::{AnyClientState, AnyConsensusState, AnyHeader}, - ics04_channel::packet::Sequence, -}; -use crate::{ - ics02_client::client_type::ClientType, ics23_commitment::commitment::CommitmentPrefix, -}; - -use crate::ics05_port::capabilities::Capability; -use crate::ics05_port::context::PortReader; - +use crate::ics02_client::error::Error as Ics02Error; use crate::ics03_connection::connection::ConnectionEnd; use crate::ics03_connection::context::{ConnectionKeeper, ConnectionReader}; use crate::ics03_connection::error::Error as Ics3Error; - -use crate::events::IbcEvent; use crate::ics04_channel::channel::ChannelEnd; use crate::ics04_channel::context::{ChannelKeeper, ChannelReader}; -use crate::ics04_channel::error::Error as Ics4Error; -use crate::ics04_channel::error::Kind as Ics4Kind; - -use crate::application::ics20_fungible_token_transfer::context::Ics20Context; +use crate::ics04_channel::error::{Error as Ics4Error, Kind as Ics4Kind}; +use crate::ics04_channel::packet::Sequence; +use crate::ics05_port::capabilities::Capability; +use crate::ics05_port::context::PortReader; use crate::ics07_tendermint::client_state::test_util::get_dummy_tendermint_client_state; use crate::ics18_relayer::context::Ics18Context; use crate::ics18_relayer::error::{Error as Ics18Error, Kind as Ics18ErrorKind}; +use crate::ics23_commitment::commitment::CommitmentPrefix; use crate::ics24_host::identifier::{ChainId, ChannelId, ClientId, ConnectionId, PortId}; use crate::ics26_routing::context::Ics26Context; use crate::ics26_routing::handler::{deliver, dispatch}; @@ -43,6 +33,7 @@ use crate::ics26_routing::msgs::Ics26Envelope; use crate::mock::client_state::{MockClientRecord, MockClientState, MockConsensusState}; use crate::mock::header::MockHeader; use crate::mock::host::{HostBlock, HostType}; +use crate::signer::Signer; use crate::Height; /// A context implementing the dependencies necessary for testing any IBC module. @@ -586,7 +577,7 @@ impl ClientKeeper for MockContext { &mut self, client_id: ClientId, client_type: ClientType, - ) -> Result<(), Ics2Error> { + ) -> Result<(), Ics02Error> { let mut client_record = self.clients.entry(client_id).or_insert(MockClientRecord { client_type, consensus_states: Default::default(), @@ -601,7 +592,7 @@ impl ClientKeeper for MockContext { &mut self, client_id: ClientId, client_state: AnyClientState, - ) -> Result<(), Ics2Error> { + ) -> Result<(), Ics02Error> { let mut client_record = self.clients.entry(client_id).or_insert(MockClientRecord { client_type: client_state.client_type(), consensus_states: Default::default(), @@ -617,7 +608,7 @@ impl ClientKeeper for MockContext { client_id: ClientId, height: Height, consensus_state: AnyConsensusState, - ) -> Result<(), Ics2Error> { + ) -> Result<(), Ics02Error> { let client_record = self.clients.entry(client_id).or_insert(MockClientRecord { client_type: ClientType::Mock, consensus_states: Default::default(), @@ -659,8 +650,8 @@ impl Ics18Context for MockContext { Ok(events) } - fn signer(&self) -> Id { - Id::from_str("0CDA3F47EF3C4906693B170EF650EB968C5F4B2C").unwrap() + fn signer(&self) -> Signer { + "0CDA3F47EF3C4906693B170EF650EB968C5F4B2C".parse().unwrap() } } diff --git a/modules/src/signer.rs b/modules/src/signer.rs new file mode 100644 index 0000000000..b4407ce187 --- /dev/null +++ b/modules/src/signer.rs @@ -0,0 +1,36 @@ +use std::{convert::Infallible, fmt::Display, str::FromStr}; + +use serde::{Deserialize, Serialize}; + +#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] +pub struct Signer(String); + +impl Signer { + pub fn new(s: impl ToString) -> Self { + Self(s.to_string()) + } + + pub fn as_str(&self) -> &str { + &self.0 + } +} + +impl Display for Signer { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", self.0) + } +} + +impl From for Signer { + fn from(s: String) -> Self { + Self(s) + } +} + +impl FromStr for Signer { + type Err = Infallible; + + fn from_str(s: &str) -> Result { + Ok(Self(s.to_string())) + } +} diff --git a/modules/src/test_utils.rs b/modules/src/test_utils.rs index 6d6d033de9..87eceb365f 100644 --- a/modules/src/test_utils.rs +++ b/modules/src/test_utils.rs @@ -1,9 +1,9 @@ #![allow(dead_code)] -use std::str::FromStr; -use tendermint::account::Id as AccountId; use tendermint::{block, consensus, evidence, public_key::Algorithm}; +use crate::signer::Signer; + // Needed in mocks. pub fn default_consensus_params() -> consensus::Params { consensus::Params { @@ -29,14 +29,10 @@ pub fn get_dummy_proof() -> Vec { .to_vec() } -fn get_dummy_raw_account_id() -> String { - "0CDA3F47EF3C4906693B170EF650EB968C5F4B2C".to_string() +pub fn get_dummy_account_id() -> Signer { + "0CDA3F47EF3C4906693B170EF650EB968C5F4B2C".parse().unwrap() } pub fn get_dummy_bech32_account() -> String { "cosmos1wxeyh7zgn4tctjzs0vtqpc6p5cxq5t2muzl7ng".to_string() } - -pub fn get_dummy_account_id() -> AccountId { - AccountId::from_str(&get_dummy_raw_account_id()).unwrap() -} diff --git a/modules/src/tx_msg.rs b/modules/src/tx_msg.rs index 14e80b501b..2f1ef10402 100644 --- a/modules/src/tx_msg.rs +++ b/modules/src/tx_msg.rs @@ -1,33 +1,30 @@ use crate::ics24_host::error::ValidationError; use prost_types::Any; -use tendermint::account::Id as AccountId; pub trait Msg: Clone { type ValidationError: std::error::Error; + type Raw: From + prost::Message; - // TODO -- clarify what is this function supposed to do & its connection to ICS26 routing mod. + // TODO: Clarify what is this function supposed to do & its connection to ICS26 routing mod. fn route(&self) -> String; - fn get_sign_bytes + prost::Message>(&self) -> Vec { - let mut buf = Vec::new(); - let raw_msg: M = self.clone().into(); - prost::Message::encode(&raw_msg, &mut buf).unwrap(); - buf - } - /// Unique type identifier for this message, to support encoding to/from `prost_types::Any`. - fn type_url(&self) -> String { - unimplemented!() - } + fn type_url(&self) -> String; - fn to_any + prost::Message>(&self) -> Any { + #[allow(clippy::wrong_self_convention)] + fn to_any(self) -> Any { Any { type_url: self.type_url(), - value: self.get_sign_bytes::(), + value: self.get_sign_bytes(), } } - fn get_signers(&self) -> Vec; + fn get_sign_bytes(self) -> Vec { + let mut buf = Vec::new(); + let raw_msg: Self::Raw = self.into(); + prost::Message::encode(&raw_msg, &mut buf).unwrap(); + buf + } fn validate_basic(&self) -> Result<(), ValidationError> { Ok(()) diff --git a/modules/tests/executor/mod.rs b/modules/tests/executor/mod.rs index c78b15aa02..f6adb840a7 100644 --- a/modules/tests/executor/mod.rs +++ b/modules/tests/executor/mod.rs @@ -1,6 +1,10 @@ pub mod modelator; pub mod step; +use std::collections::HashMap; +use std::error::Error; +use std::fmt::{Debug, Display}; + use ibc::ics02_client::client_def::{AnyClientState, AnyConsensusState, AnyHeader}; use ibc::ics02_client::client_type::ClientType; use ibc::ics02_client::error::Kind as ICS02ErrorKind; @@ -26,12 +30,10 @@ use ibc::mock::context::MockContext; use ibc::mock::header::MockHeader; use ibc::mock::host::HostType; use ibc::proofs::{ConsensusProof, Proofs}; +use ibc::signer::Signer; use ibc::Height; -use std::collections::HashMap; -use std::error::Error; -use std::fmt::{Debug, Display}; + use step::{Action, ActionOutcome, Chain, Step}; -use tendermint::account::Id as AccountId; #[derive(Debug)] pub struct IBCTestExecutor { @@ -149,13 +151,13 @@ impl IBCTestExecutor { AnyConsensusState::Mock(MockConsensusState(Self::mock_header(height))) } - pub fn signer() -> AccountId { - AccountId::new([0; 20]) + fn signer() -> Signer { + Signer::new("") } pub fn counterparty(client_id: u64, connection_id: Option) -> Counterparty { let client_id = Self::client_id(client_id); - let connection_id = connection_id.map(|connection_id| Self::connection_id(connection_id)); + let connection_id = connection_id.map(Self::connection_id); let prefix = Self::commitment_prefix(); Counterparty::new(client_id, connection_id, prefix) } diff --git a/relayer/src/chain.rs b/relayer/src/chain.rs index b65781f1b8..07afa275b8 100644 --- a/relayer/src/chain.rs +++ b/relayer/src/chain.rs @@ -2,8 +2,6 @@ use std::{sync::Arc, thread}; use crossbeam_channel as channel; use prost_types::Any; -// TODO - tendermint deps should not be here -use tendermint::account::Id as AccountId; use tendermint::block::Height; use tokio::runtime::Runtime as TokioRuntime; @@ -18,7 +16,9 @@ use ibc::ics04_channel::packet::{PacketMsgType, Sequence}; use ibc::ics23_commitment::commitment::{CommitmentPrefix, CommitmentProofBytes}; use ibc::ics24_host::identifier::{ChainId, ChannelId, ClientId, ConnectionId, PortId}; use ibc::proofs::{ConsensusProof, Proofs}; +use ibc::signer::Signer; use ibc::Height as ICSHeight; + use ibc_proto::ibc::core::channel::v1::{ PacketState, QueryChannelsRequest, QueryConnectionChannelsRequest, QueryNextSequenceReceiveRequest, QueryPacketAcknowledgementsRequest, @@ -105,7 +105,7 @@ pub trait Chain: Sized { /// Sends one or more transactions with `msgs` to chain. fn send_msgs(&mut self, proto_msgs: Vec) -> Result, Error>; - fn get_signer(&mut self) -> Result; + fn get_signer(&mut self) -> Result; fn get_key(&mut self) -> Result; diff --git a/relayer/src/chain/cosmos.rs b/relayer/src/chain/cosmos.rs index 19e1361aed..c0cf7ed383 100644 --- a/relayer/src/chain/cosmos.rs +++ b/relayer/src/chain/cosmos.rs @@ -4,10 +4,14 @@ use std::{ }; use anomaly::fail; +use bech32::{ToBase32, Variant}; use bitcoin::hashes::hex::ToHex; use crossbeam_channel as channel; use prost::Message; use prost_types::Any; +use tokio::runtime::Runtime as TokioRuntime; +use tonic::codegen::http::Uri; + use tendermint::abci::Path as TendermintABCIPath; use tendermint::account::Id as AccountId; use tendermint::block::Height; @@ -16,8 +20,6 @@ use tendermint_light_client::types::LightBlock as TMLightBlock; use tendermint_proto::Protobuf; use tendermint_rpc::query::Query; use tendermint_rpc::{endpoint::broadcast::tx_commit::Response, Client, HttpClient, Order}; -use tokio::runtime::Runtime as TokioRuntime; -use tonic::codegen::http::Uri; use ibc::downcast; use ibc::events::{from_tx_response_event, IbcEvent}; @@ -35,6 +37,7 @@ use ibc::ics24_host::identifier::{ChainId, ChannelId, ClientId, ConnectionId, Po use ibc::ics24_host::Path::ClientConsensusState as ClientConsensusPath; use ibc::ics24_host::Path::ClientState as ClientStatePath; use ibc::ics24_host::{Path, IBC_QUERY_PATH}; +use ibc::signer::Signer; use ibc::Height as ICSHeight; // Support for GRPC use ibc_proto::cosmos::auth::v1beta1::{BaseAccount, QueryAccountRequest}; @@ -363,7 +366,7 @@ impl Chain for CosmosSdkChain { } /// Get the account for the signer - fn get_signer(&mut self) -> Result { + fn get_signer(&mut self) -> Result { crate::time!("get_signer"); // Get the key from key seed file @@ -372,10 +375,8 @@ impl Chain for CosmosSdkChain { .get_key() .map_err(|e| Kind::KeyBase.context(e))?; - let signer: AccountId = - AccountId::from_str(&key.address.to_hex()).map_err(|e| Kind::KeyBase.context(e))?; - - Ok(signer) + let bech32 = encode_to_bech32(&key.address.to_hex(), &self.config.account_prefix)?; + Ok(Signer::new(bech32)) } /// Get the signing key @@ -1200,3 +1201,13 @@ pub fn tx_result_to_event( } Ok(result) } + +fn encode_to_bech32(address: &str, account_prefix: &str) -> Result { + let account = + AccountId::from_str(address).map_err(|_| Kind::InvalidKeyAddress(address.to_string()))?; + + let encoded = bech32::encode(account_prefix, account.to_base32(), Variant::Bech32) + .map_err(Kind::Bech32Encoding)?; + + Ok(encoded) +} diff --git a/relayer/src/chain/handle.rs b/relayer/src/chain/handle.rs index d982a34188..5e097532fe 100644 --- a/relayer/src/chain/handle.rs +++ b/relayer/src/chain/handle.rs @@ -4,33 +4,35 @@ use std::sync::Arc; use crossbeam_channel as channel; use dyn_clone::DynClone; use serde::{Serialize, Serializer}; -// FIXME: the handle should not depend on tendermint-specific types -use tendermint::account::Id as AccountId; -use ibc::ics04_channel::packet::{PacketMsgType, Sequence}; -use ibc::ics24_host::{identifier::ChainId, identifier::ClientId}; use ibc::{ events::IbcEvent, ics02_client::client_def::{AnyClientState, AnyConsensusState, AnyHeader}, - ics03_connection::connection::ConnectionEnd, - ics03_connection::version::Version, - ics04_channel::channel::{ChannelEnd, QueryPacketEventDataRequest}, - ics24_host::identifier::{ChannelId, ConnectionId, PortId}, + ics03_connection::{connection::ConnectionEnd, version::Version}, + ics04_channel::{ + channel::{ChannelEnd, QueryPacketEventDataRequest}, + packet::{PacketMsgType, Sequence}, + }, + ics23_commitment::commitment::CommitmentPrefix, + ics24_host::identifier::{ChainId, ChannelId, ClientId, ConnectionId, PortId}, proofs::Proofs, + signer::Signer, + Height, }; -use ibc::{ics23_commitment::commitment::CommitmentPrefix, Height}; + use ibc_proto::ibc::core::channel::v1::{ PacketState, QueryNextSequenceReceiveRequest, QueryPacketAcknowledgementsRequest, QueryPacketCommitmentsRequest, QueryUnreceivedAcksRequest, QueryUnreceivedPacketsRequest, }; + use ibc_proto::ibc::core::commitment::v1::MerkleProof; -pub use prod::ProdChainHandle; use crate::connection::ConnectionMsgType; use crate::keyring::store::KeyEntry; use crate::{error::Error, event::monitor::EventBatch}; mod prod; +pub use prod::ProdChainHandle; pub type Subscription = channel::Receiver>; @@ -64,7 +66,7 @@ pub enum ChainRequest { }, Signer { - reply_to: ReplyTo, + reply_to: ReplyTo, }, Key { @@ -210,7 +212,7 @@ pub trait ChainHandle: DynClone + Send + Sync + Debug { fn get_minimal_set(&self, from: Height, to: Height) -> Result, Error>; - fn get_signer(&self) -> Result; + fn get_signer(&self) -> Result; fn get_key(&self) -> Result; diff --git a/relayer/src/chain/handle/prod.rs b/relayer/src/chain/handle/prod.rs index 4e6bcce88a..ad943421f6 100644 --- a/relayer/src/chain/handle/prod.rs +++ b/relayer/src/chain/handle/prod.rs @@ -1,8 +1,6 @@ use std::fmt::Debug; use crossbeam_channel as channel; -// FIXME: the handle should not depend on tendermint-specific types -use tendermint::account::Id as AccountId; use ibc::ics04_channel::packet::{PacketMsgType, Sequence}; use ibc::{ @@ -16,6 +14,7 @@ use ibc::{ ics24_host::identifier::ChannelId, ics24_host::identifier::{ClientId, ConnectionId, PortId}, proofs::Proofs, + signer::Signer, Height, }; use ibc_proto::ibc::core::channel::v1::{ @@ -85,7 +84,7 @@ impl ChainHandle for ProdChainHandle { self.send(|reply_to| ChainRequest::GetMinimalSet { from, to, reply_to }) } - fn get_signer(&self) -> Result { + fn get_signer(&self) -> Result { self.send(|reply_to| ChainRequest::Signer { reply_to }) } diff --git a/relayer/src/chain/mock.rs b/relayer/src/chain/mock.rs index 425c89f05e..cf734bd252 100644 --- a/relayer/src/chain/mock.rs +++ b/relayer/src/chain/mock.rs @@ -5,7 +5,6 @@ use std::time::Duration; use crossbeam_channel as channel; use prost_types::Any; -use tendermint::account::Id; use tendermint_testgen::light_block::TMLightBlock; use tokio::runtime::Runtime; @@ -23,7 +22,7 @@ use ibc::ics23_commitment::commitment::CommitmentPrefix; use ibc::ics24_host::identifier::{ChainId, ChannelId, ClientId, ConnectionId, PortId}; use ibc::mock::context::MockContext; use ibc::mock::host::HostType; -use ibc::test_utils::get_dummy_account_id; +use ibc::signer::Signer; use ibc::Height; use ibc_proto::ibc::core::channel::v1::{ PacketState, QueryChannelsRequest, QueryConnectionChannelsRequest, @@ -42,6 +41,7 @@ use crate::error::{Error, Kind}; use crate::event::monitor::EventBatch; use crate::keyring::store::{KeyEntry, KeyRing}; use crate::light_client::{mock::LightClient as MockLightClient, LightClient}; +use ibc::test_utils::get_dummy_account_id; /// The representation of a mocked chain as the relayer sees it. /// The relayer runtime and the light client will engage with the MockChain to query/send tx; the @@ -111,7 +111,7 @@ impl Chain for MockChain { Ok(events) } - fn get_signer(&mut self) -> Result { + fn get_signer(&mut self) -> Result { Ok(get_dummy_account_id()) } diff --git a/relayer/src/chain/runtime.rs b/relayer/src/chain/runtime.rs index e68ebed523..f7e124398c 100644 --- a/relayer/src/chain/runtime.rs +++ b/relayer/src/chain/runtime.rs @@ -1,11 +1,8 @@ use std::{sync::Arc, thread}; use crossbeam_channel as channel; -// FIXME: the handle should not depend on tendermint-specific types -use tendermint::account::Id as AccountId; use tokio::runtime::Runtime as TokioRuntime; -use ibc::ics04_channel::packet::{PacketMsgType, Sequence}; use ibc::{ events::IbcEvent, ics02_client::{ @@ -13,21 +10,25 @@ use ibc::{ header::Header, state::{ClientState, ConsensusState}, }, - ics03_connection::connection::ConnectionEnd, - ics03_connection::version::Version, - ics04_channel::channel::{ChannelEnd, QueryPacketEventDataRequest}, + ics03_connection::{connection::ConnectionEnd, version::Version}, + ics04_channel::{ + channel::{ChannelEnd, QueryPacketEventDataRequest}, + packet::{PacketMsgType, Sequence}, + }, ics23_commitment::commitment::CommitmentPrefix, - ics24_host::identifier::ChannelId, - ics24_host::identifier::PortId, - ics24_host::identifier::{ClientId, ConnectionId}, + ics24_host::identifier::{ChannelId, ClientId, ConnectionId, PortId}, proofs::Proofs, + signer::Signer, Height, }; -use ibc_proto::ibc::core::channel::v1::{ - PacketState, QueryNextSequenceReceiveRequest, QueryPacketAcknowledgementsRequest, - QueryPacketCommitmentsRequest, QueryUnreceivedAcksRequest, QueryUnreceivedPacketsRequest, + +use ibc_proto::ibc::core::{ + channel::v1::{ + PacketState, QueryNextSequenceReceiveRequest, QueryPacketAcknowledgementsRequest, + QueryPacketCommitmentsRequest, QueryUnreceivedAcksRequest, QueryUnreceivedPacketsRequest, + }, + commitment::v1::MerkleProof, }; -use ibc_proto::ibc::core::commitment::v1::MerkleProof; use crate::{ config::ChainConfig, @@ -324,7 +325,7 @@ impl ChainRuntime { todo!() } - fn get_signer(&mut self, reply_to: ReplyTo) -> Result<(), Error> { + fn get_signer(&mut self, reply_to: ReplyTo) -> Result<(), Error> { let result = self.chain.get_signer(); reply_to diff --git a/relayer/src/channel.rs b/relayer/src/channel.rs index 8de58e36e0..3f9c404369 100644 --- a/relayer/src/channel.rs +++ b/relayer/src/channel.rs @@ -14,12 +14,6 @@ use ibc::ics04_channel::msgs::chan_open_try::MsgChannelOpenTry; use ibc::ics24_host::identifier::{ChainId, ChannelId, ClientId, ConnectionId, PortId}; use ibc::tx_msg::Msg; use ibc::Height; -use ibc_proto::ibc::core::channel::v1::MsgChannelCloseConfirm as RawMsgChannelCloseConfirm; -use ibc_proto::ibc::core::channel::v1::MsgChannelCloseInit as RawMsgChannelCloseInit; -use ibc_proto::ibc::core::channel::v1::MsgChannelOpenAck as RawMsgChannelOpenAck; -use ibc_proto::ibc::core::channel::v1::MsgChannelOpenConfirm as RawMsgChannelOpenConfirm; -use ibc_proto::ibc::core::channel::v1::MsgChannelOpenInit as RawMsgChannelOpenInit; -use ibc_proto::ibc::core::channel::v1::MsgChannelOpenTry as RawMsgChannelOpenTry; use crate::chain::handle::ChainHandle; use crate::connection::Connection; @@ -343,7 +337,7 @@ impl Channel { signer, }; - Ok(vec![new_msg.to_any::()]) + Ok(vec![new_msg.to_any()]) } pub fn build_chan_open_init_and_send(&self) -> Result { @@ -505,7 +499,7 @@ impl Channel { signer, }; - msgs.push(new_msg.to_any::()); + msgs.push(new_msg.to_any()); Ok(msgs) } @@ -594,7 +588,7 @@ impl Channel { signer, }; - msgs.push(new_msg.to_any::()); + msgs.push(new_msg.to_any()); Ok(msgs) } @@ -671,7 +665,7 @@ impl Channel { signer, }; - msgs.push(new_msg.to_any::()); + msgs.push(new_msg.to_any()); Ok(msgs) } @@ -724,7 +718,7 @@ impl Channel { signer, }; - Ok(vec![new_msg.to_any::()]) + Ok(vec![new_msg.to_any()]) } pub fn build_chan_close_init_and_send(&self) -> Result { @@ -802,7 +796,7 @@ impl Channel { signer, }; - msgs.push(new_msg.to_any::()); + msgs.push(new_msg.to_any()); Ok(msgs) } diff --git a/relayer/src/connection.rs b/relayer/src/connection.rs index baf227531d..43839883c7 100644 --- a/relayer/src/connection.rs +++ b/relayer/src/connection.rs @@ -2,11 +2,6 @@ use prost_types::Any; use thiserror::Error; use tracing::{error, warn}; -use ibc_proto::ibc::core::connection::v1::MsgConnectionOpenAck as RawMsgConnectionOpenAck; -use ibc_proto::ibc::core::connection::v1::MsgConnectionOpenConfirm as RawMsgConnectionOpenConfirm; -use ibc_proto::ibc::core::connection::v1::MsgConnectionOpenInit as RawMsgConnectionOpenInit; -use ibc_proto::ibc::core::connection::v1::MsgConnectionOpenTry as RawMsgConnectionOpenTry; - use ibc::events::IbcEvent; use ibc::ics02_client::height::Height; use ibc::ics03_connection::connection::{ConnectionEnd, Counterparty, State}; @@ -370,7 +365,7 @@ impl Connection { signer, }; - Ok(vec![new_msg.to_any::()]) + Ok(vec![new_msg.to_any()]) } pub fn build_conn_init_and_send(&self) -> Result { @@ -494,7 +489,7 @@ impl Connection { signer, }; - msgs.push(new_msg.to_any::()); + msgs.push(new_msg.to_any()); Ok(msgs) } @@ -590,7 +585,7 @@ impl Connection { signer, }; - msgs.push(new_msg.to_any::()); + msgs.push(new_msg.to_any()); Ok(msgs) } @@ -677,7 +672,7 @@ impl Connection { signer, }; - msgs.push(new_msg.to_any::()); + msgs.push(new_msg.to_any()); Ok(msgs) } diff --git a/relayer/src/error.rs b/relayer/src/error.rs index cbc73adb09..176fa619f6 100644 --- a/relayer/src/error.rs +++ b/relayer/src/error.rs @@ -156,6 +156,12 @@ pub enum Kind { #[error("the input header is not recognized as a header for this chain")] InvalidInputHeader, + + #[error("invalid key address: {0}")] + InvalidKeyAddress(String), + + #[error("bech32 encoding failed")] + Bech32Encoding(#[from] bech32::Error), } impl Kind { diff --git a/relayer/src/foreign_client.rs b/relayer/src/foreign_client.rs index dce1a523cc..09d7faf5cb 100644 --- a/relayer/src/foreign_client.rs +++ b/relayer/src/foreign_client.rs @@ -13,8 +13,6 @@ use ibc::ics02_client::state::ConsensusState; use ibc::ics24_host::identifier::{ChainId, ClientId}; use ibc::tx_msg::Msg; use ibc::Height; -use ibc_proto::ibc::core::client::v1::MsgCreateClient as RawMsgCreateClient; -use ibc_proto::ibc::core::client::v1::MsgUpdateClient as RawMsgUpdateClient; use crate::chain::handle::ChainHandle; @@ -162,6 +160,7 @@ impl ForeignClient { .map_err(|e| ForeignClientError::ClientCreate(format!("failed while building client consensus state from src chain ({}) with error: {}", self.src_chain.id(), e)))? .wrap_any(); + //TODO Get acct_prefix let msg = MsgCreateAnyClient::new(client_state, consensus_state, signer).map_err(|e| { ForeignClientError::ClientCreate(format!( "failed while building the create client message: {}", @@ -178,7 +177,7 @@ impl ForeignClient { let res = self .dst_chain - .send_msgs(vec![new_msg.to_any::()]) + .send_msgs(vec![new_msg.to_any()]) .map_err(|e| { ForeignClientError::ClientCreate(format!( "failed sending message to dst chain ({}) with err: {}", @@ -262,7 +261,7 @@ impl ForeignClient { signer, }; - Ok(vec![new_msg.to_any::()]) + Ok(vec![new_msg.to_any()]) } pub fn build_update_client_and_send(&self) -> Result { diff --git a/relayer/src/keyring/store.rs b/relayer/src/keyring/store.rs index 9083b962cd..6191168579 100644 --- a/relayer/src/keyring/store.rs +++ b/relayer/src/keyring/store.rs @@ -17,7 +17,6 @@ use k256::ecdsa::{signature::Signer, Signature, SigningKey}; use ripemd160::Ripemd160; use serde_json::Value; use sha2::{Digest, Sha256}; -use tendermint::account::Id as AccountId; use crate::config::ChainConfig; use crate::keyring::errors::{Error, Kind}; @@ -102,7 +101,6 @@ impl KeyRingOperations for KeyRing { let key_json: Value = serde_json::from_str(key_file_content).map_err(|e| Kind::InvalidKey.context(e))?; - let _signer: AccountId; let key: KeyEntry; let _mnemonic: String = "".to_string(); diff --git a/relayer/src/link.rs b/relayer/src/link.rs index c7e92640b9..226aff61dd 100644 --- a/relayer/src/link.rs +++ b/relayer/src/link.rs @@ -2,33 +2,31 @@ use std::time::Duration; use std::{sync::Arc, thread}; use prost_types::Any; -use tendermint::account::Id; use thiserror::Error; use tracing::{error, info}; -use ibc::ics04_channel::channel::{ChannelEnd, State}; -use ibc::ics04_channel::msgs::chan_close_confirm::MsgChannelCloseConfirm; -use ibc::ics04_channel::msgs::timeout_on_close::MsgTimeoutOnClose; use ibc::{ downcast, events::{IbcEvent, IbcEventType}, ics03_connection::connection::State as ConnectionState, - ics04_channel::channel::{Order, QueryPacketEventDataRequest, State as ChannelState}, - ics04_channel::events::{SendPacket, WriteAcknowledgement}, - ics04_channel::msgs::acknowledgement::MsgAcknowledgement, - ics04_channel::msgs::recv_packet::MsgRecvPacket, - ics04_channel::msgs::timeout::MsgTimeout, - ics04_channel::packet::{Packet, PacketMsgType, Sequence}, + ics04_channel::{ + channel::{ChannelEnd, Order, QueryPacketEventDataRequest, State as ChannelState}, + events::{SendPacket, WriteAcknowledgement}, + msgs::{ + acknowledgement::MsgAcknowledgement, chan_close_confirm::MsgChannelCloseConfirm, + recv_packet::MsgRecvPacket, timeout::MsgTimeout, timeout_on_close::MsgTimeoutOnClose, + }, + packet::{Packet, PacketMsgType, Sequence}, + }, ics24_host::identifier::{ChainId, ChannelId, ClientId, ConnectionId, PortId}, + signer::Signer, tx_msg::Msg, Height, }; + use ibc_proto::ibc::core::channel::v1::{ - MsgAcknowledgement as RawMsgAck, MsgChannelCloseConfirm as RawMsgChannelCloseConfirm, - MsgRecvPacket as RawMsgRecvPacket, MsgTimeout as RawMsgTimeout, - MsgTimeoutOnClose as RawMsgTimeoutOnClose, QueryNextSequenceReceiveRequest, - QueryPacketAcknowledgementsRequest, QueryPacketCommitmentsRequest, QueryUnreceivedAcksRequest, - QueryUnreceivedPacketsRequest, + QueryNextSequenceReceiveRequest, QueryPacketAcknowledgementsRequest, + QueryPacketCommitmentsRequest, QueryUnreceivedAcksRequest, QueryUnreceivedPacketsRequest, }; use crate::chain::handle::ChainHandle; @@ -158,7 +156,7 @@ impl RelayPath { .map_err(|e| ChannelError::QueryError(self.src_chain().id(), e))?) } - fn src_signer(&self) -> Result { + fn src_signer(&self) -> Result { self.src_chain.get_signer().map_err(|e| { LinkError::Failed(format!( "could not retrieve signer from src chain {} with error: {}", @@ -168,7 +166,7 @@ impl RelayPath { }) } - fn dst_signer(&self) -> Result { + fn dst_signer(&self) -> Result { self.dst_chain.get_signer().map_err(|e| { LinkError::Failed(format!( "could not retrieve signer from dst chain {} with error: {}", @@ -234,7 +232,9 @@ impl RelayPath { // we get a timeout packet event (this happens for both unordered and ordered channels) // Here we check it the channel is closed on src and send a channel close confirm // to the counterparty. - if self.ordered_channel() && self.src_channel()?.state_matches(&State::Closed) { + if self.ordered_channel() + && self.src_channel()?.state_matches(&ChannelState::Closed) + { info!( "{} => event {} closes the channel", self.src_chain.id(), @@ -274,7 +274,7 @@ impl RelayPath { signer: self.dst_signer()?, }; - Ok(new_msg.to_any::()) + Ok(new_msg.to_any()) } fn handle_packet_event(&mut self, event: &IbcEvent) -> Result<(), LinkError> { @@ -716,15 +716,7 @@ impl RelayPath { ) .map_err(|e| LinkError::PacketProofsConstructor(self.src_chain.id(), e))?; - let msg = MsgRecvPacket::new(packet.clone(), proofs.clone(), self.dst_signer()?).map_err( - |e| { - LinkError::Failed(format!( - "error while building the recv packet for src channel {} due to error {}", - packet.source_channel.clone(), - e - )) - }, - )?; + let msg = MsgRecvPacket::new(packet.clone(), proofs.clone(), self.dst_signer()?); info!( "built recv_packet msg {}, proofs at height {:?}", @@ -732,7 +724,7 @@ impl RelayPath { proofs.height() ); - Ok(msg.to_any::()) + Ok(msg.to_any()) } fn build_ack_from_recv_event(&self, event: &WriteAcknowledgement) -> Result { @@ -761,7 +753,7 @@ impl RelayPath { proofs.height() ); - Ok(msg.to_any::()) + Ok(msg.to_any()) } fn build_timeout_packet(&self, packet: &Packet, height: Height) -> Result { @@ -802,7 +794,7 @@ impl RelayPath { proofs.height() ); - Ok(msg.to_any::()) + Ok(msg.to_any()) } fn build_timeout_on_close_packet( @@ -834,7 +826,7 @@ impl RelayPath { proofs.height() ); - Ok(msg.to_any::()) + Ok(msg.to_any()) } fn build_recv_or_timeout_from_send_packet_event( @@ -949,7 +941,9 @@ impl Link { )) })?; - if a_channel.state_matches(&State::Closed) && b_channel.state_matches(&State::Closed) { + if a_channel.state_matches(&ChannelState::Closed) + && b_channel.state_matches(&ChannelState::Closed) + { Ok(true) } else { Ok(false) diff --git a/relayer/src/transfer.rs b/relayer/src/transfer.rs index 26d30f4404..46438c1156 100644 --- a/relayer/src/transfer.rs +++ b/relayer/src/transfer.rs @@ -1,17 +1,15 @@ -use crate::chain::{Chain, CosmosSdkChain}; use thiserror::Error; use tracing::error; -use crate::config::ChainConfig; -use crate::error::Error; +use ibc::application::ics20_fungible_token_transfer::msgs::transfer::MsgTransfer; use ibc::events::IbcEvent; +use ibc::ics24_host::identifier::ChainId; use ibc::ics24_host::identifier::{ChannelId, PortId}; use ibc::tx_msg::Msg; -use ibc::{ - application::ics20_fungible_token_transfer::msgs::transfer::MsgTransfer, - ics24_host::identifier::ChainId, -}; -use ibc_proto::ibc::applications::transfer::v1::MsgTransfer as RawMsgTransfer; + +use crate::chain::{Chain, CosmosSdkChain}; +use crate::config::ChainConfig; +use crate::error::Error; #[derive(Debug, Error)] pub enum PacketError { @@ -69,7 +67,7 @@ pub fn build_and_send_transfer_messages( timeout_timestamp: 0, }; - let raw_msg = msg.to_any::(); + let raw_msg = msg.to_any(); let msgs = vec![raw_msg; opts.number_msgs]; let events = packet_src_chain