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

feat(lc/starknet): verify signed commitment proofs from starknet #242

Draft
wants to merge 15 commits into
base: main
Choose a base branch
from
Draft
3 changes: 2 additions & 1 deletion light-client/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 light-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ ibc-client-starknet = { path = "./ibc-client-starknet" }
ibc-client-starknet-cw = { path = "./ibc-client-starknet-cw" }
ibc-client-starknet-types = { path = "./ibc-client-starknet-types" }

ibc-client-cw = { git = "https://github.com/informalsystems/cosmwasm-ibc.git", branch = "luca_joss/add-cw-client-extension-trait" }
ibc-client-cw = { git = "https://github.com/informalsystems/cosmwasm-ibc.git", branch = "starknet/demo2" }

ibc = { git = "https://github.com/cosmos/ibc-rs", branch = "main" }
ibc-core = { git = "https://github.com/cosmos/ibc-rs", branch = "main" }
Expand Down
14 changes: 14 additions & 0 deletions light-client/ibc-client-starknet/src/client_state/cw.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use ibc_client_cw::api::CwClientStateExecution;
use ibc_client_cw::context::client_ctx::CwClientExecution;

use super::ClientState;
use crate::ConsensusState;

impl<'a, E> CwClientStateExecution<'a, E> for ClientState
where
E: CwClientExecution<'a, ClientStateMut = ClientState, ConsensusStateRef = ConsensusState>,
{
fn public_key(&self) -> Option<Vec<u8>> {
Some(self.0.pub_key.clone())
}
}
10 changes: 4 additions & 6 deletions light-client/ibc-client-starknet/src/client_state/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,11 @@ where

let raw_header = signed_header.header;

let header_digest = ctx.generate_sha256_digest(&raw_header);
let header_digest = ctx.checksum(&raw_header);

let deps = ctx
.cosmwasm_execute_context()
.ok_or_else(|| ClientError::ClientSpecific {
description: "missing Deps from context".to_owned(),
})?;
let deps = ctx.deps_mut().ok_or_else(|| ClientError::ClientSpecific {
description: "missing Deps from context".to_owned(),
})?;

match deps.api.secp256k1_verify(
header_digest.as_slice(),
Expand Down
1 change: 1 addition & 0 deletions light-client/ibc-client-starknet/src/client_state/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod common;
pub mod cw;
pub mod execution;
pub mod validation;

Expand Down
2 changes: 1 addition & 1 deletion nix/ibc-starknet-cw.nix
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ let
outputHashes = {
"hermes-cosmos-encoding-components-0.1.0" = "sha256-uxXpzxVc89DkAC4VqC/Au3cpBzUbxiSS2KiFKZ+rqdg=";
"cgp-0.3.1" = "sha256-AOQ+WVQWPlF2ZfYYc5Eq3t7XAljd5P2qExWLYZWNnd8=";
"ibc-client-cw-0.56.0" = "sha256-EkLxuJfr3vf0busmSZD7DwOS9GfgfhT+sdopi1nNiCs=";
"ibc-client-cw-0.56.0" = "sha256-DA3AB8ejUrx4ksBtN/vaOznjpKE0+0F6vGA7JmWyHWA=";
"ibc-0.56.0" = "sha256-7DPIqu/zs0szjmtJTfXI2eQ0HEkRyvGjArcMZsFWMT4=";
};
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use core::marker::PhantomData;
use cgp::prelude::*;
use hermes_cairo_encoding_components::strategy::ViaCairo;
use hermes_cairo_encoding_components::types::as_felt::AsFelt;
use hermes_chain_components::traits::queries::chain_status::CanQueryChainHeight;
use hermes_chain_components::traits::queries::chain_status::CanQueryChainStatus;
use hermes_chain_components::traits::queries::connection_end::{
ConnectionEndQuerier, ConnectionEndWithProofsQuerier,
};
Expand All @@ -24,6 +24,7 @@ use crate::traits::types::blob::HasBlobType;
use crate::traits::types::method::HasSelectorType;
use crate::types::commitment_proof::StarknetCommitmentProof;
use crate::types::connection_id::{ConnectionEnd, ConnectionId};
use crate::types::status::StarknetChainStatus;

pub struct QueryConnectionEndFromStarknet;

Expand Down Expand Up @@ -68,7 +69,7 @@ impl<Chain, Counterparty, Encoding> ConnectionEndWithProofsQuerier<Chain, Counte
for QueryConnectionEndFromStarknet
where
Chain: HasHeightType<Height = u64>
+ CanQueryChainHeight
+ CanQueryChainStatus<ChainStatus = StarknetChainStatus>
+ HasCommitmentProofType<CommitmentProof = StarknetCommitmentProof>
+ HasConnectionIdType<Counterparty, ConnectionId = ConnectionId>
+ HasConnectionEndType<Counterparty, ConnectionEnd = ConnectionEnd>
Expand Down Expand Up @@ -99,10 +100,13 @@ where
.call_contract(&contract_address, &selector!("connection_end"), &calldata)
.await?;

// hack(rano): passing block hash to message builder
let chain_status = chain.query_chain_status().await?;

// TODO(rano): how to get the proof?
let dummy_proof = StarknetCommitmentProof {
proof_height: chain.query_chain_height().await?,
proof_bytes: vec![0x1],
proof_height: chain_status.height,
proof_bytes: chain_status.block_hash.to_bytes_be().to_vec(),
};

Ok((
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ use hermes_chain_components::traits::message_builders::connection_handshake::{
ConnectionOpenAckMessageBuilder, ConnectionOpenConfirmMessageBuilder,
ConnectionOpenInitMessageBuilder, ConnectionOpenTryMessageBuilder,
};
use hermes_chain_components::traits::queries::chain_status::CanQueryChainStatus;
use hermes_chain_components::traits::queries::consensus_state::CanQueryConsensusStateWithLatestHeight;
use hermes_chain_components::traits::types::client_state::HasClientStateType;
use hermes_chain_components::traits::types::connection::{
HasConnectionEndType, HasConnectionOpenAckPayloadType, HasConnectionOpenConfirmPayloadType,
HasConnectionOpenInitPayloadType, HasConnectionOpenTryPayloadType,
HasInitConnectionOptionsType,
};
use hermes_chain_components::traits::types::consensus_state::HasConsensusStateType;
use hermes_chain_components::traits::types::height::HasHeightType;
use hermes_chain_components::traits::types::ibc::{HasClientIdType, HasConnectionIdType};
use hermes_chain_components::traits::types::message::HasMessageType;
Expand All @@ -22,23 +25,31 @@ use hermes_chain_components::types::payloads::connection::{
};
use hermes_cosmos_chain_components::traits::message::{CosmosMessage, ToCosmosMessage};
use hermes_cosmos_chain_components::types::connection::CosmosInitConnectionOptions;
use hermes_cosmos_chain_components::types::key_types::secp256k1::Secp256k1KeyPair;
use hermes_cosmos_chain_components::types::messages::connection::open_ack::CosmosConnectionOpenAckMessage;
use hermes_cosmos_chain_components::types::messages::connection::open_confirm::CosmosConnectionOpenConfirmMessage;
use hermes_cosmos_chain_components::types::messages::connection::open_init::CosmosConnectionOpenInitMessage;
use hermes_cosmos_chain_components::types::messages::connection::open_try::CosmosConnectionOpenTryMessage;
use hermes_cosmos_chain_components::types::status::ChainStatus;
use hermes_relayer_components::transaction::traits::default_signer::HasDefaultSigner;
use ibc::core::client::types::error::ClientError;
use ibc::core::client::types::Height as CosmosHeight;
use ibc::core::connection::types::version::Version as CosmosConnectionVersion;
use ibc::core::connection::types::ConnectionEnd;
use ibc::core::host::types::identifiers::{
ClientId as CosmosClientId, ConnectionId as CosmosConnectionId,
};
use ibc::core::host::types::path::{ConnectionPath, Path};
use ibc::primitives::proto::Any as IbcProtoAny;
use ibc_proto::Protobuf;
use prost_types::Any;

use crate::types::client_id::ClientId as StarknetClientId;
use crate::types::commitment_proof::StarknetCommitmentProof;
use crate::types::connection_id::ConnectionId as StarknetConnectionId;
use crate::types::consensus_state::WasmStarknetConsensusState;
use crate::types::cosmos::client_state::CometClientState;
use crate::types::membership_proof_signer::MembershipVerifierContainer;
pub struct BuildStarknetToCosmosConnectionHandshake;

impl<Chain, Counterparty> ConnectionOpenInitMessageBuilder<Chain, Counterparty>
Expand Down Expand Up @@ -83,19 +94,25 @@ where
+ HasHeightType<Height = CosmosHeight>
+ HasClientIdType<Counterparty, ClientId = CosmosClientId>
+ CanRaiseAsyncError<ClientError>
+ CanQueryChainStatus<ChainStatus = ChainStatus>
+ CanQueryConsensusStateWithLatestHeight<Counterparty>
+ HasDefaultSigner<Signer = Secp256k1KeyPair>
+ CanRaiseAsyncError<String>
+ CanRaiseAsyncError<&'static str>
+ HasClientStateType<Counterparty, ClientState = CometClientState>,
Counterparty: HasConnectionOpenTryPayloadType<
Chain,
ConnectionOpenTryPayload = ConnectionOpenTryPayload<Counterparty, Chain>,
> + HasClientIdType<Chain, ClientId = StarknetClientId>
+ HasConnectionIdType<Chain, ConnectionId = StarknetConnectionId>
+ HasHeightType<Height = u64>
+ HasConnectionEndType<Chain>
+ HasConsensusStateType<Chain, ConsensusState = WasmStarknetConsensusState>
+ HasConnectionEndType<Chain, ConnectionEnd = ConnectionEnd>
+ HasCommitmentPrefixType<CommitmentPrefix = Vec<u8>>
+ HasCommitmentProofType<CommitmentProof = StarknetCommitmentProof>,
{
async fn build_connection_open_try_message(
_chain: &Chain,
chain: &Chain,
client_id: &CosmosClientId,
counterparty_client_id: &StarknetClientId,
counterparty_connection_id: &StarknetConnectionId,
Expand All @@ -109,10 +126,25 @@ where
// TODO(rano): delay period
let delay_period = Duration::from_secs(0);

// TODO(rano): apparently update height is set to zero
let update_height =
CosmosHeight::new(0, core::cmp::max(1, counterparty_payload.update_height))
.map_err(Chain::raise_error)?;
CosmosHeight::new(0, counterparty_payload.update_height).map_err(Chain::raise_error)?;

let proof_init = {
let data = MembershipVerifierContainer {
// FIXME(hack) we are passing consensus root as proof
state_root: counterparty_payload.proof_init.proof_bytes,
prefix: counterparty_payload.commitment_prefix.clone(),
path: Path::Connection(ConnectionPath::new(counterparty_connection_id))
.to_string()
.into(),
value: Some(counterparty_payload.connection_end.encode_vec()),
};

chain
.get_default_signer()
.sign(&data.canonical_bytes())
.map_err(Chain::raise_error)?
};

let message = CosmosConnectionOpenTryMessage {
client_id: client_id.to_string(),
Expand All @@ -126,7 +158,7 @@ where
},
delay_period,
update_height,
proof_init: counterparty_payload.proof_init.proof_bytes,
proof_init,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I successfully managed to sign the proof_init

// TODO(rano): counterparty_payload has empty proofs?
// proof_client: counterparty_payload.proof_client.proof_bytes,
proof_client: vec![0x1],
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like these two are skipped. Probably ibc-go doesn't use them after cosmos/ibc-go#7129

Expand Down Expand Up @@ -214,7 +246,8 @@ where
let message = CosmosConnectionOpenConfirmMessage {
connection_id: connection_id.to_string(),
update_height,
proof_ack: counterparty_payload.proof_ack.proof_bytes,
// FIXME(rano): generate the signature
proof_ack: dbg!(counterparty_payload.proof_ack.proof_bytes),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, the signature verification fails here. But I don't know how to create the signature at this point without access to ConnectionEnd and ConnectionId at starknet.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can query the stored counteryparty connection end (starknet) at cosmos, cosmos's own connection id to recreate the ConnectionEnd.

};

Ok(message.to_cosmos_message())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#[derive(Debug)]
pub struct MembershipVerifierContainer {
pub state_root: Vec<u8>,
pub prefix: Vec<u8>,
pub path: Vec<u8>,
pub value: Option<Vec<u8>>,
}

impl MembershipVerifierContainer {
pub fn canonical_bytes(self) -> Vec<u8> {
let mut bytes = Vec::new();
let encode_bytes = |mut data: Vec<u8>, output: &mut Vec<u8>| {
let len = data.len() as u64;
output.extend(len.to_be_bytes());
output.append(&mut data);
};
encode_bytes(self.state_root, &mut bytes);
encode_bytes(self.prefix, &mut bytes);
encode_bytes(self.path, &mut bytes);
if let Some(v) = self.value {
encode_bytes(v, &mut bytes);
}
bytes
}
}
1 change: 1 addition & 0 deletions relayer/crates/starknet-chain-components/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub mod consensus_state;
pub mod cosmos;
pub mod event;
pub mod events;
pub mod membership_proof_signer;
pub mod message_response;
pub mod message_responses;
pub mod messages;
Expand Down
Loading