Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Represent signer as a String and use account prefix from the config #711

Merged
merged 14 commits into from
Mar 11, 2021
Merged
11 changes: 8 additions & 3 deletions modules/src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@ use anomaly::{BoxError, Context};
use bech32::ToBase32;
use bech32::{FromBase32, Variant};

use std::str::FromStr;
use tendermint::account::Id as AccountId;

pub fn account_to_string(addr: AccountId) -> Result<String, BoxError> {
Ok(bech32::encode("cosmos", addr.to_base32(), Variant::Bech32)
.map_err(|e| Context::new("cannot generate bech32 account", Some(e.into())))?)
pub fn encode_to_bech32(addr: String, hrp: String) -> Result<String, BoxError> {
let account =
AccountId::from_str(addr.as_str()).map_err(|e| Context::new("bad address", Some(e)))?;
Ok(
bech32::encode(hrp.as_str(), account.to_base32(), Variant::Bech32)
.map_err(|e| Context::new("cannot generate bech32 account", Some(e.into())))?,
)
romac marked this conversation as resolved.
Show resolved Hide resolved
}

pub fn string_to_account(raw: String) -> Result<AccountId, BoxError> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
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 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};
Expand All @@ -25,9 +23,9 @@ pub struct MsgTransfer {
/// the tokens to be transferred
pub token: Option<ibc_proto::cosmos::base::v1beta1::Coin>,
/// the sender address
pub sender: AccountId,
pub sender: String,
/// the recipient address on the destination chain
pub receiver: AccountId,
pub receiver: String,
/// Timeout height relative to the current block height.
/// The timeout is disabled when set to 0.
pub timeout_height: Height,
Expand All @@ -46,10 +44,6 @@ impl Msg for MsgTransfer {
fn type_url(&self) -> String {
TYPE_URL.to_string()
}

fn get_signers(&self) -> Vec<AccountId> {
vec![self.sender]
}
}

impl Protobuf<RawMsgTransfer> for MsgTransfer {}
Expand All @@ -62,8 +56,8 @@ impl TryFrom<RawMsgTransfer> 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,
receiver: raw_msg.receiver,
timeout_height: raw_msg.timeout_height.unwrap().try_into().unwrap(),
timeout_timestamp: 0,
})
Expand All @@ -76,8 +70,8 @@ impl From<MsgTransfer> 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,
receiver: domain_msg.receiver,
timeout_height: Some(domain_msg.timeout_height.try_into().unwrap()),
timeout_timestamp: 0,
}
Expand Down
4 changes: 2 additions & 2 deletions modules/src/ics02_client/handler/create_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ mod tests {
..height
}))
.into(),
signer,
signer.clone(),
)
.unwrap(),
MsgCreateAnyClient::new(
Expand All @@ -154,7 +154,7 @@ mod tests {
..height
}))
.into(),
signer,
signer.clone(),
)
.unwrap(),
MsgCreateAnyClient::new(
Expand Down
2 changes: 1 addition & 1 deletion modules/src/ics02_client/handler/update_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ mod tests {
let msg = MsgUpdateAnyClient {
client_id: cid.clone(),
header: MockHeader(update_height).into(),
signer,
signer: signer.clone(),
};

let output = dispatch(&ctx, ClientMsg::UpdateClient(msg.clone()));
Expand Down
17 changes: 5 additions & 12 deletions modules/src/ics02_client/msgs/create_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@

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};
Expand All @@ -24,14 +22,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: String,
}

impl MsgCreateAnyClient {
pub fn new(
client_state: AnyClientState,
consensus_state: AnyConsensusState,
signer: AccountId,
signer: String,
) -> Result<Self, Error> {
if client_state.client_type() != consensus_state.client_type() {
return Err(error::Kind::RawClientAndConsensusStateTypesMismatch {
Expand All @@ -46,6 +44,7 @@ impl MsgCreateAnyClient {
signer,
})
}

pub fn client_state(&self) -> AnyClientState {
self.client_state.clone()
}
Expand All @@ -64,10 +63,6 @@ impl Msg for MsgCreateAnyClient {
fn type_url(&self) -> String {
TYPE_URL.to_string()
}

fn get_signers(&self) -> Vec<AccountId> {
vec![self.signer]
}
}

impl Protobuf<RawMsgCreateClient> for MsgCreateAnyClient {}
Expand All @@ -84,14 +79,12 @@ impl TryFrom<RawMsgCreateClient> 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,
)
}
}
Expand All @@ -101,7 +94,7 @@ impl From<MsgCreateAnyClient> 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,
}
}
}
Expand Down
15 changes: 4 additions & 11 deletions modules/src/ics02_client/msgs/update_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@

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;
Expand All @@ -24,11 +22,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: String,
}

impl MsgUpdateAnyClient {
pub fn new(client_id: ClientId, header: AnyHeader, signer: AccountId) -> Self {
pub fn new(client_id: ClientId, header: AnyHeader, signer: String) -> Self {
MsgUpdateAnyClient {
client_id,
header,
Expand All @@ -47,10 +45,6 @@ impl Msg for MsgUpdateAnyClient {
fn type_url(&self) -> String {
TYPE_URL.to_string()
}

fn get_signers(&self) -> Vec<AccountId> {
vec![self.signer]
}
}

impl Protobuf<RawMsgUpdateClient> for MsgUpdateAnyClient {}
Expand All @@ -60,12 +54,11 @@ impl TryFrom<RawMsgUpdateClient> for MsgUpdateAnyClient {

fn try_from(raw: RawMsgUpdateClient) -> Result<Self, Self::Error> {
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,
})
}
}
Expand All @@ -75,7 +68,7 @@ impl From<MsgUpdateAnyClient> 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,
}
}
}
Expand Down
14 changes: 3 additions & 11 deletions modules/src/ics03_connection/msgs/conn_open_ack.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
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;
Expand All @@ -25,7 +23,7 @@ pub struct MsgConnectionOpenAck {
pub client_state: Option<AnyClientState>,
pub proofs: Proofs,
pub version: Version,
pub signer: AccountId,
pub signer: String,
}

impl MsgConnectionOpenAck {
Expand Down Expand Up @@ -74,10 +72,6 @@ impl Msg for MsgConnectionOpenAck {
fn type_url(&self) -> String {
TYPE_URL.to_string()
}

fn get_signers(&self) -> Vec<AccountId> {
vec![self.signer]
}
}

impl Protobuf<RawMsgConnectionOpenAck> for MsgConnectionOpenAck {}
Expand All @@ -86,8 +80,6 @@ impl TryFrom<RawMsgConnectionOpenAck> for MsgConnectionOpenAck {
type Error = anomaly::Error<Kind>;

fn try_from(msg: RawMsgConnectionOpenAck) -> Result<Self, Self::Error> {
let signer = string_to_account(msg.signer).map_err(|e| Kind::InvalidAddress.context(e))?;

let consensus_height = msg
.consensus_height
.ok_or(Kind::MissingConsensusHeight)?
Expand Down Expand Up @@ -133,7 +125,7 @@ impl TryFrom<RawMsgConnectionOpenAck> for MsgConnectionOpenAck {
proof_height,
)
.map_err(|e| Kind::InvalidProof.context(e))?,
signer,
signer: msg.signer,
})
}
}
Expand Down Expand Up @@ -162,7 +154,7 @@ impl From<MsgConnectionOpenAck> 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,
}
}
}
Expand Down
14 changes: 3 additions & 11 deletions modules/src/ics03_connection/msgs/conn_open_confirm.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
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};
Expand All @@ -19,7 +17,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: String,
}

impl MsgConnectionOpenConfirm {
Expand All @@ -44,10 +42,6 @@ impl Msg for MsgConnectionOpenConfirm {
fn type_url(&self) -> String {
TYPE_URL.to_string()
}

fn get_signers(&self) -> Vec<AccountId> {
vec![self.signer]
}
}

impl Protobuf<RawMsgConnectionOpenConfirm> for MsgConnectionOpenConfirm {}
Expand All @@ -56,8 +50,6 @@ impl TryFrom<RawMsgConnectionOpenConfirm> for MsgConnectionOpenConfirm {
type Error = anomaly::Error<Kind>;

fn try_from(msg: RawMsgConnectionOpenConfirm) -> Result<Self, Self::Error> {
let signer = string_to_account(msg.signer).map_err(|e| Kind::InvalidAddress.context(e))?;

let proof_height = msg
.proof_height
.ok_or(Kind::MissingProofHeight)?
Expand All @@ -70,7 +62,7 @@ impl TryFrom<RawMsgConnectionOpenConfirm> 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,
})
}
}
Expand All @@ -81,7 +73,7 @@ impl From<MsgConnectionOpenConfirm> 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,
}
}
}
Expand Down
Loading