Skip to content

Commit

Permalink
fix: add dstnetwork in signature
Browse files Browse the repository at this point in the history
  • Loading branch information
sherpalden committed Dec 4, 2024
1 parent 76e8f8a commit 30b30ec
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 237 deletions.
208 changes: 2 additions & 206 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion contracts/cosmwasm-vm/cw-cluster-connection/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ hex = "0.4.3"
serde-json-wasm = {workspace=true}
sha2 = { version = "0.10.6", default-features = false }
sha3 = { version = "0.10.6", default-features = false }
k256 = "0.11.6"
k256 = "0.13.3"

[dev-dependencies]
cosmwasm = "0.7.2"
Expand Down
2 changes: 2 additions & 0 deletions contracts/cosmwasm-vm/cw-cluster-connection/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ impl<'a> ClusterConnection<'a> {
src_network: NetId,
conn_sn: u128,
msg: String,
dst_network: NetId,
signatures: Vec<Vec<u8>>,
) -> Result<Response, ContractError> {
self.ensure_relayer(deps.storage, info.sender)?;
Expand All @@ -167,6 +168,7 @@ impl<'a> ClusterConnection<'a> {
src_network: src_network.to_string(),
conn_sn: conn_sn,
data: msg_vec.clone(),
dst_network: dst_network.to_string(),
};
let signed_msg = rlp::encode(&signed_msg).to_vec();

Expand Down
15 changes: 5 additions & 10 deletions contracts/cosmwasm-vm/cw-cluster-connection/src/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,16 @@ use std::collections::HashMap;

use cosmwasm_std::{ensure_eq, Addr, BalanceResponse, BankQuery, Coin};
use cw_xcall_lib::network_address::NetId;
use k256::ecdsa::VerifyingKey;
use sha2::Digest;
use sha3::Keccak256;

use super::*;

pub fn sha256(data: &[u8]) -> Vec<u8> {
use sha2::Digest;
sha2::Sha256::digest(data).to_vec()
}

pub fn keccak256(input: &[u8]) -> [u8; 32] {
pub fn keccak256(input: &[u8]) -> Keccak256 {
use sha3::{Digest, Keccak256};
let mut hasher = Keccak256::new();
hasher.update(input);
let out: [u8; 32] = hasher.finalize().to_vec().try_into().unwrap();
out
return hasher;
}

pub fn to_truncated_le_bytes(n: u128) -> Vec<u8> {
Expand Down Expand Up @@ -114,7 +109,7 @@ impl<'a> ClusterConnection<'a> {
return Err(ContractError::InsufficientSignatures);
}

let message_hash = keccak256(&signed_msg);
let message_hash = keccak256(&signed_msg).finalize().to_vec();

let mut signers: HashMap<Vec<u8>, bool> = HashMap::new();

Expand Down
11 changes: 10 additions & 1 deletion contracts/cosmwasm-vm/cw-cluster-connection/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,17 @@ pub fn execute(
src_network,
conn_sn,
msg,
dst_network,
signatures,
} => conn.recv_message(deps, info, src_network, conn_sn, msg, signatures),
} => conn.recv_message(
deps,
info,
src_network,
conn_sn,
msg,
dst_network,
signatures,
),
}
}

Expand Down
1 change: 1 addition & 0 deletions contracts/cosmwasm-vm/cw-cluster-connection/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub enum ExecuteMsg {
src_network: NetId,
conn_sn: u128,
msg: String,
dst_network: NetId,
signatures: Vec<Vec<u8>>,
},
}
Expand Down
7 changes: 5 additions & 2 deletions contracts/cosmwasm-vm/cw-cluster-connection/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,15 @@ pub struct SignableMsg {
pub src_network: String,
pub conn_sn: u128,
pub data: Vec<u8>,
pub dst_network: String,
}
impl Encodable for SignableMsg {
fn rlp_append(&self, stream: &mut rlp::RlpStream) {
stream.begin_list(3);
stream.begin_list(4);
stream.append(&self.src_network);
stream.append(&self.conn_sn);
stream.append(&self.data);
stream.append(&self.dst_network);
}
}

Expand All @@ -65,9 +67,10 @@ pub fn test_signable_msg_rlp() {
src_network: "0x2.icon".to_string(),
conn_sn: 456456,
data: "hello".as_bytes().to_vec(),
dst_network: "archway".to_string(),
};

let signed_msg = rlp::encode(&signed_msg).to_vec();

println!("singed message: {:?}", signed_msg);
println!("signed message: {:?}", signed_msg);
}
Loading

0 comments on commit 30b30ec

Please sign in to comment.