Skip to content

Commit

Permalink
Signer parsing from context (#1393)
Browse files Browse the repository at this point in the history
* feat: convert signers with context

* chore: changelog for #1393
  • Loading branch information
sug0 authored Jan 21, 2025
1 parent 27f10ac commit a5f9fbf
Show file tree
Hide file tree
Showing 13 changed files with 49 additions and 62 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- [ibc-apps] Replace the `TryFrom<Signer>` bound on `AccountId` with new
context methods, with the aim of contextually parsing `Signer` instances.
([\#1393](https://github.com/cosmos/ibc-rs/pull/1393))
9 changes: 8 additions & 1 deletion ibc-apps/ics20-transfer/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ use ibc_core::primitives::Signer;

/// Methods required in token transfer validation, to be implemented by the host
pub trait TokenTransferValidationContext {
type AccountId: TryFrom<Signer>;
/// Native chain account id.
type AccountId;

/// Attempt to convert a [`Signer`] to a native chain sender account.
fn sender_account(&self, sender: &Signer) -> Result<Self::AccountId, HostError>;

/// Attempt to convert a [`Signer`] to a native chain receiver account.
fn receiver_account(&self, receiver: &Signer) -> Result<Self::AccountId, HostError>;

/// get_port returns the portID for the transfer module.
fn get_port(&self) -> Result<PortId, HostError>;
Expand Down
12 changes: 2 additions & 10 deletions ibc-apps/ics20-transfer/src/handler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ pub fn refund_packet_token_execute(
packet: &Packet,
data: &PacketData,
) -> Result<(), TokenTransferError> {
let sender = data
.sender
.clone()
.try_into()
.map_err(|_| TokenTransferError::FailedToParseAccount)?;
let sender = ctx_a.sender_account(&data.sender)?;

if is_sender_chain_source(
packet.port_id_on_a.clone(),
Expand All @@ -48,11 +44,7 @@ pub fn refund_packet_token_validate(
packet: &Packet,
data: &PacketData,
) -> Result<(), TokenTransferError> {
let sender = data
.sender
.clone()
.try_into()
.map_err(|_| TokenTransferError::FailedToParseAccount)?;
let sender = ctx_a.sender_account(&data.sender)?;

if is_sender_chain_source(
packet.port_id_on_a.clone(),
Expand Down
9 changes: 3 additions & 6 deletions ibc-apps/ics20-transfer/src/handler/on_recv_packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,9 @@ pub fn process_recv_packet_execute<Ctx: TokenTransferExecutionContext>(
.can_receive_coins()
.map_err(|err| (ModuleExtras::empty(), err.into()))?;

let receiver_account = data.receiver.clone().try_into().map_err(|_| {
(
ModuleExtras::empty(),
TokenTransferError::FailedToParseAccount,
)
})?;
let receiver_account = ctx_b
.receiver_account(&data.receiver)
.map_err(|err| (ModuleExtras::empty(), err.into()))?;

let extras = if is_receiver_chain_source(
packet.port_id_on_a.clone(),
Expand Down
14 changes: 2 additions & 12 deletions ibc-apps/ics20-transfer/src/handler/send_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,7 @@ where

let token = &msg.packet_data.token;

let sender: TokenCtx::AccountId = msg
.packet_data
.sender
.clone()
.try_into()
.map_err(|_| TokenTransferError::FailedToParseAccount)?;
let sender = token_ctx_a.sender_account(&msg.packet_data.sender)?;

if is_sender_chain_source(
msg.port_id_on_a.clone(),
Expand Down Expand Up @@ -129,12 +124,7 @@ where

let token = &msg.packet_data.token;

let sender = msg
.packet_data
.sender
.clone()
.try_into()
.map_err(|_| TokenTransferError::FailedToParseAccount)?;
let sender = token_ctx_a.sender_account(&msg.packet_data.sender)?;

if is_sender_chain_source(
msg.port_id_on_a.clone(),
Expand Down
2 changes: 0 additions & 2 deletions ibc-apps/ics20-transfer/types/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ pub enum TokenTransferError {
FailedToDeserializePacketData,
/// failed to deserialize acknowledgement
FailedToDeserializeAck,
/// failed to parse account
FailedToParseAccount,
}

#[cfg(feature = "std")]
Expand Down
9 changes: 8 additions & 1 deletion ibc-apps/ics721-nft-transfer/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,17 @@ pub trait NftClassContext {

/// Read-only methods required in NFT transfer validation context.
pub trait NftTransferValidationContext {
type AccountId: TryFrom<Signer> + PartialEq;
/// Native chain account id.
type AccountId: PartialEq;
type Nft: NftContext;
type NftClass: NftClassContext;

/// Attempt to convert a [`Signer`] to a native chain sender account.
fn sender_account(&self, sender: &Signer) -> Result<Self::AccountId, HostError>;

/// Attempt to convert a [`Signer`] to a native chain receiver account.
fn receiver_account(&self, receiver: &Signer) -> Result<Self::AccountId, HostError>;

/// get_port returns the portID for the transfer module.
fn get_port(&self) -> Result<PortId, HostError>;

Expand Down
12 changes: 2 additions & 10 deletions ibc-apps/ics721-nft-transfer/src/handler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ pub fn refund_packet_nft_execute(
packet: &Packet,
data: &PacketData,
) -> Result<(), NftTransferError> {
let sender = data
.sender
.clone()
.try_into()
.map_err(|_| NftTransferError::FailedToParseAccount)?;
let sender = ctx_a.sender_account(&data.sender)?;

if is_sender_chain_source(
packet.port_id_on_a.clone(),
Expand Down Expand Up @@ -58,11 +54,7 @@ pub fn refund_packet_nft_validate(
packet: &Packet,
data: &PacketData,
) -> Result<(), NftTransferError> {
let sender = data
.sender
.clone()
.try_into()
.map_err(|_| NftTransferError::FailedToParseAccount)?;
let sender = ctx_a.sender_account(&data.sender)?;

if is_sender_chain_source(
packet.port_id_on_a.clone(),
Expand Down
9 changes: 3 additions & 6 deletions ibc-apps/ics721-nft-transfer/src/handler/on_recv_packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,9 @@ where
.can_receive_nft()
.map_err(|err| (ModuleExtras::empty(), err.into()))?;

let receiver_account = data.receiver.clone().try_into().map_err(|_| {
(
ModuleExtras::empty(),
NftTransferError::FailedToParseAccount,
)
})?;
let receiver_account = ctx_b
.receiver_account(&data.receiver)
.map_err(|err| (ModuleExtras::empty(), err.into()))?;

let extras = if is_receiver_chain_source(
packet.port_id_on_a.clone(),
Expand Down
14 changes: 2 additions & 12 deletions ibc-apps/ics721-nft-transfer/src/handler/send_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,7 @@ where
let seq_send_path_on_a = SeqSendPath::new(&msg.port_id_on_a, &msg.chan_id_on_a);
let sequence = send_packet_ctx_a.get_next_sequence_send(&seq_send_path_on_a)?;

let sender: TransferCtx::AccountId = msg
.packet_data
.sender
.clone()
.try_into()
.map_err(|_| NftTransferError::FailedToParseAccount)?;
let sender: TransferCtx::AccountId = transfer_ctx.sender_account(&msg.packet_data.sender)?;

let mut packet_data = msg.packet_data;
let class_id = &packet_data.class_id;
Expand Down Expand Up @@ -159,12 +154,7 @@ where
let seq_send_path_on_a = SeqSendPath::new(&msg.port_id_on_a, &msg.chan_id_on_a);
let sequence = send_packet_ctx_a.get_next_sequence_send(&seq_send_path_on_a)?;

let sender = msg
.packet_data
.sender
.clone()
.try_into()
.map_err(|_| NftTransferError::FailedToParseAccount)?;
let sender = transfer_ctx.sender_account(&msg.packet_data.sender)?;

let mut packet_data = msg.packet_data;
let class_id = &packet_data.class_id;
Expand Down
2 changes: 0 additions & 2 deletions ibc-apps/ics721-nft-transfer/types/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ pub enum NftTransferError {
FailedToDeserializePacketData,
/// failed to deserialize acknowledgement
FailedToDeserializeAck,
/// failed to parse account ID
FailedToParseAccount,
/// invalid channel state: cannot be closed
InvalidClosedChannel,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ impl NftTransferValidationContext for DummyNftTransferModule {
type Nft = DummyNft;
type NftClass = DummyNftClass;

fn sender_account(&self, sender: &Signer) -> Result<Self::AccountId, HostError> {
Ok(sender.clone())
}

fn receiver_account(&self, receiver: &Signer) -> Result<Self::AccountId, HostError> {
Ok(receiver.clone())
}

fn get_port(&self) -> Result<PortId, HostError> {
Ok(PortId::transfer())
}
Expand Down
8 changes: 8 additions & 0 deletions ibc-testkit/src/testapp/ibc/applications/transfer/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ use super::types::DummyTransferModule;
impl TokenTransferValidationContext for DummyTransferModule {
type AccountId = Signer;

fn sender_account(&self, sender: &Signer) -> Result<Self::AccountId, HostError> {
Ok(sender.clone())
}

fn receiver_account(&self, receiver: &Signer) -> Result<Self::AccountId, HostError> {
Ok(receiver.clone())
}

fn get_port(&self) -> Result<PortId, HostError> {
Ok(PortId::transfer())
}
Expand Down

0 comments on commit a5f9fbf

Please sign in to comment.