Skip to content

Commit

Permalink
Add trusted relayer verification on Cosmos side (#203)
Browse files Browse the repository at this point in the history
* Add SignedStarknetHeader containing a header and signature

* Fix cgp version mismatch in light-client crate

* Fix update_state light client execution
  • Loading branch information
ljoss17 authored Jan 17, 2025
1 parent d75dfdb commit 55c5f4e
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 55 deletions.
67 changes: 29 additions & 38 deletions light-client/Cargo.lock

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

17 changes: 15 additions & 2 deletions light-client/ibc-client-starknet-types/src/encoding/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ use ibc_core::primitives::Timestamp;

use crate::encoding::impls::client_state::EncodeStarknetClientState;
use crate::encoding::impls::consensus_state::EncodeStarknetConsensusState;
use crate::encoding::impls::header::EncodeStarknetHeader;
use crate::header::{StarknetHeader, STARKNET_HEADER_TYPE_URL};
use crate::encoding::impls::header::{EncodeSignedStarknetHeader, EncodeStarknetHeader};
use crate::header::{SignedStarknetHeader, StarknetHeader, STARKNET_HEADER_TYPE_URL};
use crate::{
StarknetClientState, StarknetConsensusState, STARKNET_CLIENT_STATE_TYPE_URL,
STARKNET_CONSENSUS_STATE_TYPE_URL,
Expand Down Expand Up @@ -71,12 +71,14 @@ delegate_components! {
(ViaProtobuf, StarknetClientState),
(ViaProtobuf, StarknetConsensusState),
(ViaProtobuf, StarknetHeader),
(ViaProtobuf, SignedStarknetHeader),
]: EncodeProtoWithMutBuffer,

[
(ViaAny, StarknetClientState),
(ViaAny, StarknetConsensusState),
(ViaAny, StarknetHeader),
(ViaAny, SignedStarknetHeader),
]: EncodeViaAny<ViaProtobuf>,
}
}
Expand All @@ -98,6 +100,9 @@ delegate_components! {

(ViaProtobuf, StarknetHeader):
EncodeStarknetHeader,

(ViaProtobuf, SignedStarknetHeader):
EncodeSignedStarknetHeader,
}
}

Expand All @@ -107,12 +112,14 @@ delegate_components! {
(StarknetClientState, Any),
(StarknetConsensusState, Any),
(StarknetHeader, Any),
(SignedStarknetHeader, Any),
]: EncodeAsAnyProtobuf<ViaProtobuf, UseContext>,

[
(Any, StarknetClientState),
(Any, StarknetConsensusState),
(Any, StarknetHeader),
(Any, SignedStarknetHeader),
]: DecodeAsAnyProtobuf<ViaProtobuf, UseContext>,
}
}
Expand All @@ -134,3 +141,9 @@ impl_type_url!(
StarknetHeader,
STARKNET_HEADER_TYPE_URL,
);

impl_type_url!(
StarknetLightClientTypeUrlSchemas,
SignedStarknetHeader,
STARKNET_HEADER_TYPE_URL,
);
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ use hermes_encoding_components::impls::encode_mut::combine::CombineEncoders;
use hermes_encoding_components::impls::encode_mut::field::EncodeField;
use hermes_encoding_components::impls::encode_mut::from::DecodeFrom;
use hermes_encoding_components::traits::transform::Transformer;
use hermes_protobuf_encoding_components::impls::encode_mut::proto_field::bytes::EncodeByteField;
use hermes_protobuf_encoding_components::impls::encode_mut::proto_field::decode_required::DecodeRequiredProtoField;
use hermes_protobuf_encoding_components::impls::encode_mut::proto_field::encode::EncodeLengthDelimitedProtoField;
use ibc_core::client::types::Height;

use crate::header::StarknetHeader;
use crate::header::{SignedStarknetHeader, StarknetHeader};
use crate::StarknetConsensusState;

pub struct EncodeStarknetHeader;
Expand Down Expand Up @@ -49,3 +50,38 @@ impl Transformer for EncodeStarknetHeader {
}
}
}

pub struct EncodeSignedStarknetHeader;

delegate_components! {
EncodeSignedStarknetHeader {
MutEncoderComponent:
CombineEncoders<Product![
EncodeField<
symbol!("header"),
EncodeLengthDelimitedProtoField<1, UseContext>,
>,
EncodeField<
symbol!("signature"),
EncodeByteField<2>,
>,
]>,
MutDecoderComponent: DecodeFrom<
Self,
CombineEncoders<Product![
DecodeRequiredProtoField<1, UseContext>,
EncodeByteField<2>,
]>
>,
}
}

impl Transformer for EncodeSignedStarknetHeader {
type From = Product![StarknetHeader, Vec<u8>];

type To = SignedStarknetHeader;

fn transform(product![header, signature]: Self::From) -> Self::To {
SignedStarknetHeader { header, signature }
}
}
6 changes: 6 additions & 0 deletions light-client/ibc-client-starknet-types/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,9 @@ pub struct StarknetHeader {
pub height: Height,
pub consensus_state: StarknetConsensusState,
}

#[derive(Debug, Clone, HasField)]
pub struct SignedStarknetHeader {
pub header: StarknetHeader,
pub signature: Vec<u8>,
}
13 changes: 8 additions & 5 deletions light-client/ibc-client-starknet/src/client_state/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use cgp::core::component::UseContext;
use hermes_cosmos_encoding_components::impls::any::ConvertIbcAny;
use hermes_encoding_components::impls::convert::ConvertVia;
use hermes_encoding_components::traits::convert::Converter;
use ibc_client_starknet_types::header::StarknetHeader;
use ibc_client_starknet_types::header::SignedStarknetHeader;
use ibc_client_starknet_types::StarknetClientState as ClientStateType;
use ibc_core::client::context::client_state::ClientStateExecution;
use ibc_core::client::context::prelude::{ClientStateCommon, ConsensusState};
Expand Down Expand Up @@ -50,10 +50,13 @@ where
client_id: &ClientId,
header: Any,
) -> Result<Vec<Height>, ClientError> {
let header: StarknetHeader = <ConvertVia<ProstAny, ConvertIbcAny, UseContext>>::convert(
&StarknetLightClientEncoding,
&header,
)?;
let signed_header: SignedStarknetHeader =
<ConvertVia<ProstAny, ConvertIbcAny, UseContext>>::convert(
&StarknetLightClientEncoding,
&header,
)?;

let header = signed_header.header;

let latest_height = header.height;

Expand Down
9 changes: 7 additions & 2 deletions light-client/ibc-client-starknet/src/encoding/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use core::str::Utf8Error;

use cgp::core::error::{ErrorRaiser, ProvideErrorType};
use cgp::prelude::*;
use hermes_encoding_components::traits::convert::CanConvertBothWays;
use hermes_encoding_components::traits::convert::{CanConvert, CanConvertBothWays};
use hermes_encoding_components::traits::encode_and_decode::CanEncodeAndDecode;
use hermes_encoding_components::traits::encode_and_decode_mut::CanEncodeAndDecodeMut;
use hermes_protobuf_encoding_components::impls::any::TypeUrlMismatchError;
Expand All @@ -16,7 +16,7 @@ use hermes_protobuf_encoding_components::impls::encode_mut::proto_field::decode_
use hermes_protobuf_encoding_components::types::any::Any;
use hermes_protobuf_encoding_components::types::strategy::{ViaAny, ViaProtobuf};
use ibc_client_starknet_types::encoding::components::*;
use ibc_client_starknet_types::header::StarknetHeader;
use ibc_client_starknet_types::header::{SignedStarknetHeader, StarknetHeader};
use ibc_client_starknet_types::{StarknetClientState, StarknetConsensusState};
use ibc_core::client::types::error::ClientError;
use ibc_core::client::types::Height;
Expand Down Expand Up @@ -192,12 +192,17 @@ pub trait CanUseStarknetLightClientEncoding:
+ CanEncodeAndDecode<ViaProtobuf, StarknetClientState>
+ CanEncodeAndDecode<ViaProtobuf, StarknetConsensusState>
+ CanEncodeAndDecode<ViaProtobuf, StarknetHeader>
+ CanEncodeAndDecode<ViaProtobuf, SignedStarknetHeader>
+ CanEncodeAndDecode<ViaAny, StarknetClientState>
+ CanEncodeAndDecode<ViaAny, StarknetConsensusState>
+ CanEncodeAndDecode<ViaAny, StarknetHeader>
+ CanEncodeAndDecode<ViaAny, SignedStarknetHeader>
+ CanConvertBothWays<Any, StarknetClientState>
+ CanConvertBothWays<Any, StarknetConsensusState>
+ CanConvertBothWays<Any, StarknetHeader>
+ CanConvertBothWays<Any, SignedStarknetHeader>
+ CanConvert<StarknetHeader, Any>
+ CanConvert<SignedStarknetHeader, Any>
+ CanEncodeAndDecodeMut<ViaProtobuf, Timestamp>
+ CanEncodeAndDecodeMut<ViaProtobuf, CommitmentRoot>
{
Expand Down
Loading

0 comments on commit 55c5f4e

Please sign in to comment.