Skip to content

Commit 340f3a2

Browse files
committed
feat(p2p/webrtc): check chain id
1 parent 4a00fca commit 340f3a2

File tree

4 files changed

+33
-1
lines changed

4 files changed

+33
-1
lines changed

core/src/chain_id.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@ use multihash::{Blake2b256, Hasher};
55
use time::macros::format_description;
66
use time::OffsetDateTime;
77

8-
use std::fmt::{self, Debug, Display, Formatter};
8+
use std::{
9+
fmt::{self, Debug, Display, Formatter},
10+
io::{Read, Write},
11+
};
912

13+
use binprot::{BinProtRead, BinProtWrite};
1014
use serde::{Deserialize, Deserializer, Serialize, Serializer};
1115

1216
#[derive(Clone, PartialEq, Eq)]
@@ -95,6 +99,23 @@ impl ChainId {
9599
}
96100
}
97101

102+
impl BinProtWrite for ChainId {
103+
fn binprot_write<W: Write>(&self, w: &mut W) -> std::io::Result<()> {
104+
w.write_all(&self.0)
105+
}
106+
}
107+
108+
impl BinProtRead for ChainId {
109+
fn binprot_read<R: Read + ?Sized>(r: &mut R) -> Result<Self, binprot::Error>
110+
where
111+
Self: Sized,
112+
{
113+
let mut bytes = [0; 32];
114+
r.read_exact(&mut bytes)?;
115+
Ok(Self(bytes))
116+
}
117+
}
118+
98119
impl Serialize for ChainId {
99120
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
100121
serializer.serialize_str(&self.to_hex())

p2p/src/connection/incoming/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ impl P2pState {
3333
peer_id: PeerId,
3434
offer: &webrtc::Offer,
3535
) -> Result<(), RejectionReason> {
36+
if self.chain_id != offer.chain_id {
37+
return Err(RejectionReason::ChainIdMismatch);
38+
}
39+
3640
if peer_id != offer.identity_pub_key.peer_id() {
3741
return Err(RejectionReason::PeerIdAndPublicKeyMismatch);
3842
}

p2p/src/connection/outgoing/p2p_connection_outgoing_reducer.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ impl P2pConnectionOutgoingState {
152152
Ok(())
153153
}
154154
P2pConnectionOutgoingAction::OfferSdpCreateSuccess { sdp, peer_id } => {
155+
let chain_id = p2p_state.chain_id.clone();
155156
let state = p2p_state
156157
.outgoing_peer_connection_mut(&peer_id)
157158
.ok_or("Missing peer connection for `P2pConnectionOutgoingAction::OfferSdpCreateSuccess`")?;
@@ -170,6 +171,7 @@ impl P2pConnectionOutgoingState {
170171

171172
let offer = Box::new(crate::webrtc::Offer {
172173
sdp,
174+
chain_id,
173175
identity_pub_key: p2p_state.config.identity_pub_key.clone(),
174176
target_peer_id: peer_id,
175177
// TODO(vlad9486): put real address

p2p/src/webrtc/signal.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use binprot_derive::{BinProtRead, BinProtWrite};
22
use derive_more::From;
3+
use openmina_core::ChainId;
34
use serde::{Deserialize, Serialize};
45

56
use crate::identity::{EncryptableType, PeerId, PublicKey};
@@ -9,6 +10,7 @@ use super::{ConnectionAuth, Host};
910
#[derive(Serialize, Deserialize, Eq, PartialEq, Debug, Clone)]
1011
pub struct Offer {
1112
pub sdp: String,
13+
pub chain_id: ChainId,
1214
/// Offerer's identity public key.
1315
pub identity_pub_key: PublicKey,
1416
/// Peer id that the offerer wants to connect to.
@@ -37,6 +39,8 @@ pub enum Signal {
3739

3840
#[derive(Serialize, Deserialize, Eq, PartialEq, Debug, Clone, Copy, thiserror::Error)]
3941
pub enum RejectionReason {
42+
#[error("peer is on a different chain")]
43+
ChainIdMismatch,
4044
#[error("peer_id does not match peer's public key")]
4145
PeerIdAndPublicKeyMismatch,
4246
#[error("target peer_id is not local node's peer_id")]
@@ -83,6 +87,7 @@ impl Answer {
8387
impl RejectionReason {
8488
pub fn is_bad(&self) -> bool {
8589
match self {
90+
Self::ChainIdMismatch => false,
8691
Self::PeerIdAndPublicKeyMismatch => true,
8792
Self::TargetPeerIdNotMe => true,
8893
Self::PeerCapacityFull => false,

0 commit comments

Comments
 (0)