polytone_evm/handshake/
mod.rs

1use cosmwasm_std::{
2    to_json_binary, Ibc3ChannelOpenResponse, IbcChannelOpenMsg, IbcChannelOpenResponse, IbcOrder,
3};
4
5use error::HandshakeError;
6
7pub const POLYTONE_VERSION: &str = "polytone-1";
8
9/// The version returned by the note module during the first step of
10/// the handshake.
11pub fn note_version() -> String {
12    format!("{POLYTONE_VERSION}-note")
13}
14
15/// The version returned by the voice module during the first step of
16/// the handshake.
17pub fn voice_version() -> String {
18    format!("{POLYTONE_VERSION}-voice")
19}
20
21/// Performs the open step of the IBC handshake for Polytone modules.
22///
23/// # Arguments
24///
25/// - `version` the version to return, one of `note_version()`, or
26///   `voice_version()`.
27/// - `extensions` the Polytone extensions supported by the caller.
28///   Extensions are explained in detail in the polytone spec.
29/// - `msg` the message received to open the channel.
30/// - `counterparty_version` the expected version of the counterparty
31///   for example, for a note connecting to a voice, this should be
32///   `voice_version()`.
33fn open(
34    msg: &IbcChannelOpenMsg,
35    extensions: &[&str],
36    version: String,
37    counterparty_version: String,
38) -> Result<IbcChannelOpenResponse, HandshakeError> {
39    match msg {
40        IbcChannelOpenMsg::OpenInit { channel } => {
41            if channel.version != POLYTONE_VERSION {
42                Err(HandshakeError::ProtocolMismatch {
43                    actual: channel.version.clone(),
44                    expected: POLYTONE_VERSION.to_string(),
45                })
46            } else if channel.order != IbcOrder::Unordered {
47                Err(HandshakeError::ExpectUnordered)
48            } else {
49                Ok(Some(Ibc3ChannelOpenResponse { version }))
50            }
51        }
52        IbcChannelOpenMsg::OpenTry {
53            channel,
54            counterparty_version: cv,
55        } => {
56            if *cv != counterparty_version {
57                Err(HandshakeError::WrongCounterparty)
58            } else if channel.order != IbcOrder::Unordered {
59                Err(HandshakeError::ExpectUnordered)
60            } else {
61                Ok(Some(Ibc3ChannelOpenResponse {
62                    version: to_json_binary(extensions).unwrap().to_base64(),
63                }))
64            }
65        }
66    }
67}
68
69pub mod error;
70pub mod note;
71pub mod voice;
72
73#[cfg(test)]
74mod tests;