Skip to content

Commit ec2f965

Browse files
fix: hex string encoding (#314)
* fix: hex string encoding * fix: mark commitments * chore: cargo fmt * feat: add hex string support * chore: fmt * fix: unit test * chore: refactor signer --------- Co-authored-by: sabinchitrakar <immortal.infidel@gmail.com>
1 parent a5fcb0c commit ec2f965

File tree

14 files changed

+184
-102
lines changed

14 files changed

+184
-102
lines changed

contracts/cosmwasm-vm/cw-common/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ cosmwasm-std = { version = "1.2.1", features = ["iterator", "ibc3"] }
1414
ibc = { version = "0.32.0", default-features = false, features = ["parity-scale-codec", "mocks-no-std", "serde", "borsh"]}
1515
serde = { version = "1.0.154", default-features = false, features = ["derive"] }
1616
cw-storage-plus = "1.0.1"
17+
serde_json = "*"
18+
hex-buffer-serde = "0.4.0"
19+
schemars = "0.8.12"
1720
hex = { version = "0.4.3", default-features = false}
1821
common = { path="../../../libraries/rust/common" }
1922
ibc-proto = { version = "0.26.0", default-features = false, features = ["parity-scale-codec", "borsh"] }

contracts/cosmwasm-vm/cw-common/src/client_msg.rs

+2
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ pub enum ExecuteMsg {
182182
message_info: MessageInfo,
183183
endpoint: IbcEndpoint,
184184
verify_channel_state: VerifyChannelState,
185+
// add all props that we need on response
185186
},
186187
Misbehaviour {
187188
client_id: String,
@@ -191,6 +192,7 @@ pub enum ExecuteMsg {
191192
expected_response: OpenConfirmResponse,
192193
client_id: String,
193194
verify_connection_state: VerifyConnectionState,
195+
// add all props that we need on response
194196
},
195197
TimeoutOnCLose {
196198
client_id: String,

contracts/cosmwasm-vm/cw-common/src/core_msg.rs

+22-20
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use cosmwasm_schema::QueryResponses;
22
use cosmwasm_std::Addr;
33

4+
use crate::hex_string::HexString;
5+
46
use super::*;
57

68
#[cw_serde]
@@ -14,14 +16,14 @@ pub enum ExecuteMsg {
1416
client_address: Addr,
1517
},
1618
CreateClient {
17-
client_state: Vec<u8>,
18-
consensus_state: Vec<u8>,
19-
signer: Vec<u8>,
19+
client_state: HexString,
20+
consensus_state: HexString,
21+
signer: HexString,
2022
},
2123
UpdateClient {
2224
client_id: String,
23-
header: Vec<u8>,
24-
signer: Vec<u8>,
25+
header: HexString,
26+
signer: HexString,
2527
},
2628
// Not included in this version of ibc core
2729
UpgradeClient {},
@@ -32,82 +34,82 @@ pub enum ExecuteMsg {
3234
ConnectionOpenInit {
3335
//raw message bytes:
3436
//ibc_proto::ibc::core::connection::v1::MsgConnectionOpenInit,
35-
msg: Vec<u8>,
37+
msg: HexString,
3638
},
3739
ConnectionOpenTry {
3840
//raw message bytes:
3941
//ibc_proto::ibc::core::connection::v1::MsgConnectionOpenTry
40-
msg: Vec<u8>,
42+
msg: HexString,
4143
},
4244
ConnectionOpenAck {
4345
//raw message bytes:
4446
//ibc_proto::ibc::core::connection::v1::MsgConnectionOpenAck
45-
msg: Vec<u8>,
47+
msg: HexString,
4648
},
4749
ConnectionOpenConfirm {
4850
//raw message bytes:
4951
//ibc_proto::ibc::core::connection::v1::MsgConnectionOpenConfirm
50-
msg: Vec<u8>,
52+
msg: HexString,
5153
},
5254

5355
// Channel Messages
5456
ChannelOpenInit {
5557
// raw message bytes:
5658
//ibc_proto::ibc::core::channel::v1::MsgChannelOpenInit
57-
msg: Vec<u8>,
59+
msg: HexString,
5860
},
5961
ChannelOpenTry {
6062
//raw message bytes:
6163
//ibc_proto::ibc::core::channel::v1::MsgChannelOpenTry
62-
msg: Vec<u8>,
64+
msg: HexString,
6365
},
6466
ChannelOpenAck {
6567
//raw message bytes:
6668
//ibc_proto::ibc::core::channel::v1::MsgChannelOpenAck
67-
msg: Vec<u8>,
69+
msg: HexString,
6870
},
6971
ChannelOpenConfirm {
7072
//raw message bytes:
7173
//ibc_proto::ibc::core::channel::v1::MsgChannelOpenConfirm
72-
msg: Vec<u8>,
74+
msg: HexString,
7375
},
7476
ChannelCloseInit {
7577
port_id_on_a: String,
7678
chan_id_on_a: String,
77-
signer: Vec<u8>,
79+
signer: HexString,
7880
},
7981
ChannelCloseConfirm {
8082
//raw message bytes:
8183
//ibc_proto::ibc::core::channel::v1::MsgChannelCloseConfirm
82-
msg: Vec<u8>,
84+
msg: HexString,
8385
},
8486

8587
// Packet Messages
8688
SendPacket {
8789
//raw message bytes:
8890
//ibc_proto::ibc::core::channel::v1::Packet
89-
packet: Vec<u8>,
91+
packet: HexString,
9092
},
9193
ReceivePacket {
9294
//raw message bytes:
9395
//ibc_proto::ibc::core::channel::v1::MsgRecvPacket
94-
msg: Vec<u8>,
96+
msg: HexString,
9597
},
9698
AcknowledgementPacket {
9799
//raw message bytes:
98100
//ibc_proto::ibc::core::channel::v1::MsgAcknowledgement
99-
msg: Vec<u8>,
101+
msg: HexString,
100102
},
101103
RequestTimeout {},
102104
Timeout {
103105
//raw message bytes:
104106
//ibc_proto::ibc::core::channel::v1::MsgTimeout
105-
msg: Vec<u8>,
107+
msg: HexString,
106108
},
107109
TimeoutOnClose {
108110
//raw message bytes:
109111
//ibc_proto::ibc::core::channel::v1::MsgTimeoutOnClose
110-
msg: Vec<u8>,
112+
msg: HexString,
111113
},
112114

113115
// Storage Messages
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
use std::borrow::Cow;
2+
3+
use cosmwasm_schema::cw_serde;
4+
use hex_buffer_serde::Hex;
5+
use schemars::JsonSchema;
6+
7+
#[derive(JsonSchema)]
8+
struct FromHexString(());
9+
10+
impl Hex<Vec<u8>> for FromHexString {
11+
type Error = hex::FromHexError;
12+
13+
fn create_bytes(value: &Vec<u8>) -> Cow<'_, [u8]> {
14+
Cow::Borrowed(&value[..])
15+
}
16+
17+
fn from_bytes(bytes: &[u8]) -> Result<Vec<u8>, Self::Error> {
18+
Ok(bytes.to_vec())
19+
}
20+
}
21+
22+
#[cw_serde]
23+
pub struct HexString(String);
24+
25+
impl HexString {
26+
pub fn to_bytes(&self) -> Result<Vec<u8>, hex::FromHexError> {
27+
let str = self.0.replace("0x", "");
28+
hex::decode(str)
29+
}
30+
31+
pub fn from_bytes(bytes: &[u8]) -> HexString {
32+
HexString(hex::encode(bytes))
33+
}
34+
}
35+
36+
#[cw_serde]
37+
pub struct TestHex {
38+
#[serde(with = "FromHexString")]
39+
pub bytes: Vec<u8>,
40+
}
41+
42+
#[cfg(test)]
43+
mod tests {
44+
use super::TestHex;
45+
46+
#[test]
47+
fn test_hex_serialize_deserialize() {
48+
let test = TestHex {
49+
bytes: hex::decode("deadbeef").unwrap(),
50+
};
51+
let serialized = serde_json::to_value(&test).unwrap();
52+
assert_eq!("{\"bytes\":\"deadbeef\"}", serialized.to_string());
53+
let deserialized = serde_json::from_str::<TestHex>("{\"bytes\":\"deadbeef\"}").unwrap();
54+
assert_eq!(test, deserialized);
55+
}
56+
}

contracts/cosmwasm-vm/cw-common/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ pub mod client_msg;
22
pub mod client_response;
33
pub mod commitment;
44
pub mod errors;
5+
pub mod hex_string;
56
pub mod types;
67
pub mod xcall_msg;
78
use cosmwasm_std::IbcPacket;
@@ -44,6 +45,7 @@ use crate::types::{ClientId, ClientType};
4445
use common::rlp::Encodable;
4546
use common::rlp::{self, Decodable};
4647
use cosmwasm_schema::cw_serde;
48+
use cosmwasm_schema::serde::{Deserialize, Serialize};
4749
use cosmwasm_std::Binary;
4850
use cosmwasm_std::IbcEndpoint;
4951
use cosmwasm_std::{
@@ -59,7 +61,6 @@ use ibc::{
5961
signer::Signer,
6062
};
6163
pub use ibc_proto::ibc::core::channel::v1::Packet as RawPacket;
62-
use serde::{Deserialize, Serialize};
6364
pub mod core_msg;
6465
use core_msg::*;
6566
pub use ibc::core::ics04_channel::packet::Packet;

contracts/cosmwasm-vm/cw-common/src/types.rs

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::borrow::Cow;
2+
13
use super::*;
24
use cosmwasm_std::{Addr, Coin};
35
#[cw_serde]
@@ -6,7 +8,9 @@ pub struct VerifyChannelState {
68
pub counterparty_prefix: Vec<u8>,
79
pub proof: Vec<u8>,
810
pub root: Vec<u8>,
11+
// commitment key
912
pub counterparty_chan_end_path: Vec<u8>,
13+
// commitment bytes
1014
pub expected_counterparty_channel_end: Vec<u8>,
1115
}
1216

@@ -16,7 +20,9 @@ pub struct VerifyPacketData {
1620
pub prefix: Vec<u8>,
1721
pub proof: Vec<u8>,
1822
pub root: Vec<u8>,
23+
// commitment key
1924
pub commitment_path: Vec<u8>,
25+
// commitment bytes
2026
pub commitment: Vec<u8>,
2127
}
2228
#[derive(Debug, Serialize, Deserialize)]
@@ -49,7 +55,9 @@ pub struct VerifyPacketAcknowledgement {
4955
pub prefix: Vec<u8>,
5056
pub proof: Vec<u8>,
5157
pub root: Vec<u8>,
58+
// commitment key
5259
pub ack_path: Vec<u8>,
60+
// commitment byte
5361
pub ack: Vec<u8>,
5462
}
5563

0 commit comments

Comments
 (0)