Skip to content

Commit

Permalink
feat: convert signers with context
Browse files Browse the repository at this point in the history
  • Loading branch information
sug0 committed Jan 17, 2025
1 parent 2adeb6b commit 0c083bb
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 29 deletions.
6 changes: 5 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,11 @@ 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 account id.
fn account_id_from_signer(&self, signer: &Signer) -> Option<Self::AccountId>;

/// get_port returns the portID for the transfer module.
fn get_port(&self) -> Result<PortId, HostError>;
Expand Down
16 changes: 6 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,9 @@ 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
.account_id_from_signer(&data.sender)
.ok_or(TokenTransferError::FailedToParseAccount)?;

if is_sender_chain_source(
packet.port_id_on_a.clone(),
Expand All @@ -48,11 +46,9 @@ 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
.account_id_from_signer(&data.sender)
.ok_or(TokenTransferError::FailedToParseAccount)?;

if is_sender_chain_source(
packet.port_id_on_a.clone(),
Expand Down
14 changes: 8 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,14 @@ 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
.account_id_from_signer(&data.receiver)
.ok_or_else(|| {
(
ModuleExtras::empty(),
TokenTransferError::FailedToParseAccount,
)
})?;

let extras = if is_receiver_chain_source(
packet.port_id_on_a.clone(),
Expand Down
18 changes: 6 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,9 @@ 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
.account_id_from_signer(&msg.packet_data.sender)
.ok_or(TokenTransferError::FailedToParseAccount)?;

if is_sender_chain_source(
msg.port_id_on_a.clone(),
Expand Down Expand Up @@ -129,12 +126,9 @@ 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
.account_id_from_signer(&msg.packet_data.sender)
.ok_or(TokenTransferError::FailedToParseAccount)?;

if is_sender_chain_source(
msg.port_id_on_a.clone(),
Expand Down
4 changes: 4 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,10 @@ use super::types::DummyTransferModule;
impl TokenTransferValidationContext for DummyTransferModule {
type AccountId = Signer;

fn account_id_from_signer(&self, signer: &Signer) -> Option<Self::AccountId> {
Some(signer.clone())
}

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

0 comments on commit 0c083bb

Please sign in to comment.