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

cairo: generalize ibc packet data #185

Open
rnbguy opened this issue Jan 10, 2025 · 0 comments
Open

cairo: generalize ibc packet data #185

rnbguy opened this issue Jan 10, 2025 · 0 comments

Comments

@rnbguy
Copy link
Member

rnbguy commented Jan 10, 2025

Cairo core contract takes application specific PacketData cairo serialization. So, when we are relaying packets from cosmos to starknet, we can't just pass the packet data.

The current hack to assume all the raw packet data is from ics20, so we json-deserialize to ibc ics20 packet data format and do necessary domain type conversion and then cairo-serialize. Then we put the serialized Vec<Felt> to cairo contract message for packet handlers.

Ideally, a relayer should not assume any application structure and relay any packet without knowing the details of the applications.

let ibc_ics20_packet_data: IbcIcs20PacketData = serde_json::from_slice(&packet.data).unwrap();
// convert to cairo packet message
// TODO(rano): can't iter. need fix at ibc-rs side
// for now, using json hack
let trace_path_json =
serde_json::to_string(&ibc_ics20_packet_data.token.denom.trace_path).unwrap();
#[derive(serde::Deserialize)]
struct DummyTracePath {
pub port_id: String,
pub channel_id: String,
}
let trace_path: Vec<DummyTracePath> = serde_json::from_str(&trace_path_json).unwrap();
let denom = PrefixedDenom {
trace_path: trace_path
.into_iter()
.map(
|DummyTracePath {
port_id,
channel_id,
}| TracePrefix {
port_id,
channel_id,
},
)
.collect(),
base: Denom::Hosted(
ibc_ics20_packet_data
.token
.denom
.base_denom
.as_str()
.to_string(),
),
};
let amount = {
let bytes = ibc_ics20_packet_data.token.amount.as_ref().0;
crypto_bigint::U256::from(bytes).into()
};
let sender_string = ibc_ics20_packet_data.sender.as_ref().to_string();
let receiver_string = ibc_ics20_packet_data.receiver.as_ref().to_string();
// TODO(rano): the following is a hack
// do we really need Participant variants?
let sender = sender_string
.parse()
.map(Participant::Native)
.unwrap_or_else(|_| Participant::External(sender_string));
let receiver = receiver_string
.parse()
.map(Participant::Native)
.unwrap_or_else(|_| Participant::External(receiver_string));
match (&sender, &receiver) {
(Participant::Native(_), Participant::Native(_)) => {
panic!("Native to Native transfer is not supported")
}
(Participant::External(_), Participant::External(_)) => {
panic!("External to External transfer is not supported")
}
_ => {}
}
let memo = ibc_ics20_packet_data.memo.as_ref().to_string();
let cairo_ics20_packet_data = CairoTransferPacketData {
denom,
amount,
sender,
receiver,
memo,
};
// serialize to vec<felt>
let data_felt = encoding.encode(&cairo_ics20_packet_data).unwrap();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant