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

Merged
merged 33 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
a5f39fd
update ibc-client-cw patch branch
rnbguy Jan 28, 2025
01290a9
fix cw context methods
rnbguy Jan 28, 2025
e3080cf
impl cw client state execution for starknet
rnbguy Jan 28, 2025
de53ed2
add membership proof signer
rnbguy Jan 31, 2025
07e36a1
sign connection open try message
rnbguy Jan 31, 2025
59ec68f
ignore client and consensus proof
rnbguy Jan 31, 2025
d71ba5d
open ack proof
rnbguy Jan 31, 2025
776aa81
todo comment
rnbguy Jan 31, 2025
311be49
generate membership proof message at starknet payload
rnbguy Jan 31, 2025
4c465ac
membership proof msg for channel end
rnbguy Jan 31, 2025
cbaf6be
membership proof msg for packet commitment
rnbguy Jan 31, 2025
02d4d6b
membership proof msg for packet ack
rnbguy Jan 31, 2025
7722d4d
membership proof msg for packet receipt
rnbguy Jan 31, 2025
cb4fa8d
rename field in StarknetCommitmentProof
rnbguy Feb 1, 2025
c14166f
rm redundant todos
rnbguy Feb 1, 2025
426a861
sign self client and consensus state proofs
rnbguy Feb 3, 2025
fd25041
use chain_status height as proof height
rnbguy Feb 3, 2025
043f2a1
add tiny-bip39 dep
rnbguy Feb 4, 2025
e233c3e
proof_signer components
rnbguy Feb 4, 2025
56b8427
add proof_signer field to StarknetChain
rnbguy Feb 4, 2025
0ad7614
generate secp256k1 keypair from felt signing key
rnbguy Feb 4, 2025
da71d3c
sign with starknet proof signer
rnbguy Feb 4, 2025
c6ba01e
Merge branch 'main' into rano/starknet-to-cosmos-commitment-proofs
rnbguy Feb 4, 2025
becea65
nits
rnbguy Feb 4, 2025
7644902
rm redundant endpoint
rnbguy Feb 4, 2025
46931e2
use packet_receipt endpoint
rnbguy Feb 4, 2025
e1e9c15
ibc-go compatible packet commitment
rnbguy Feb 4, 2025
aa5bc01
test consistent packet commitment in cairo and rust
rnbguy Feb 4, 2025
8f5ce48
happy clippy
rnbguy Feb 4, 2025
1db25f1
rm test steps with dummy proofs
rnbguy Feb 4, 2025
205756e
cargo fmt
rnbguy Feb 4, 2025
b84c5c4
cargo clippy
rnbguy Feb 4, 2025
b8f240b
update expected commitment hash after fix
rnbguy Feb 4, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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