Skip to main content

derec_library/
types.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2026 DeRec Alliance. All rights reserved.
3
4//! # Cross-layer shared types
5//!
6//! Types used by both the primitives layer and the protocol layer. Anything
7//! that's "post-pairing" or orchestrator-specific lives under
8//! [`crate::protocol::types`] instead.
9//!
10//! ## Channel identifiers
11//!
12//! A [`ChannelId`] uniquely identifies the secure communication channel between
13//! an Owner and a Helper for a given pairing instance.
14//!
15//! The identifier is derived deterministically during the pairing process from
16//! the initial `ContactMessage`. Because both parties compute it from the same
17//! contact data, the resulting identifier is **symmetric** — both the Owner and
18//! the Helper obtain the same value without additional coordination.
19
20/// Identifier of the secure communication channel between an Owner and a Helper.
21///
22/// A `ChannelId` is established during the pairing flow and uniquely identifies
23/// the communication channel associated with a specific `(Owner, Helper, SecretId)`
24/// relationship.
25///
26/// In the DeRec protocol, the `ChannelId` is deterministically derived from the
27/// hash of the initial `ContactMessage`. Because both parties compute it from the
28/// same contact data, the resulting identifier is **symmetric**, meaning that the
29/// Owner and the Helper independently derive the same `ChannelId`.
30///
31/// This identifier is used internally by the library to associate protocol state
32/// and messages with the correct peer.
33#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
34#[cfg_attr(
35    any(feature = "serde", target_arch = "wasm32"),
36    derive(serde::Serialize, serde::Deserialize),
37    serde(transparent)
38)]
39pub struct ChannelId(pub u64);
40
41impl From<u64> for ChannelId {
42    fn from(value: u64) -> Self {
43        ChannelId(value)
44    }
45}
46
47impl From<ChannelId> for u64 {
48    fn from(value: ChannelId) -> Self {
49        value.0
50    }
51}
52
53impl PartialEq<u64> for ChannelId {
54    fn eq(&self, other: &u64) -> bool {
55        self.0 == *other
56    }
57}
58
59/// 32-byte symmetric key shared between an Owner and a Helper after pairing.
60///
61/// A `SharedKey` is established during the pairing flow and used to encrypt
62/// and authenticate all subsequent protocol messages on the associated channel.
63pub type SharedKey = [u8; 32];