sp_mixnet/types.rs
1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! Mixnet types used by both host and runtime.
19
20use alloc::vec::Vec;
21use codec::{Decode, Encode};
22use scale_info::TypeInfo;
23
24mod app {
25 use sp_application_crypto::{app_crypto, key_types::MIXNET, sr25519};
26 app_crypto!(sr25519, MIXNET);
27}
28
29/// Authority public session key, used to verify registration signatures.
30pub type AuthorityId = app::Public;
31/// Authority signature, attached to mixnode registrations.
32pub type AuthoritySignature = app::Signature;
33
34/// Absolute session index.
35pub type SessionIndex = u32;
36
37/// Each session should progress through these phases in order.
38#[derive(Decode, Encode, TypeInfo, PartialEq, Eq)]
39pub enum SessionPhase {
40 /// Generate cover traffic to the current session's mixnode set.
41 CoverToCurrent,
42 /// Build requests using the current session's mixnode set.
43 RequestsToCurrent,
44 /// Only send cover (and forwarded) traffic to the previous session's mixnode set.
45 CoverToPrev,
46 /// Disconnect the previous session's mixnode set.
47 DisconnectFromPrev,
48}
49
50/// The index and phase of the current session.
51#[derive(Decode, Encode, TypeInfo)]
52pub struct SessionStatus {
53 /// Index of the current session.
54 pub current_index: SessionIndex,
55 /// Current session phase.
56 pub phase: SessionPhase,
57}
58
59/// Size in bytes of a [`KxPublic`].
60pub const KX_PUBLIC_SIZE: usize = 32;
61
62/// X25519 public key, used in key exchange between message senders and mixnodes. Mixnode public
63/// keys are published on-chain and change every session. Message senders generate a new key for
64/// every message they send.
65pub type KxPublic = [u8; KX_PUBLIC_SIZE];
66
67/// Ed25519 public key of a libp2p peer.
68pub type PeerId = [u8; 32];
69
70/// Information published on-chain for each mixnode every session.
71#[derive(Decode, Encode, TypeInfo)]
72pub struct Mixnode {
73 /// Key-exchange public key for the mixnode.
74 pub kx_public: KxPublic,
75 /// libp2p peer ID of the mixnode.
76 pub peer_id: PeerId,
77 /// External addresses for the mixnode, in multiaddr format, UTF-8 encoded.
78 pub external_addresses: Vec<Vec<u8>>,
79}
80
81/// Error querying the runtime for a session's mixnode set.
82#[derive(Decode, Encode, TypeInfo)]
83pub enum MixnodesErr {
84 /// Insufficient mixnodes were registered for the session.
85 InsufficientRegistrations {
86 /// The number of mixnodes that were registered for the session.
87 num: u32,
88 /// The minimum number of mixnodes that must be registered for the mixnet to operate.
89 min: u32,
90 },
91}
92
93impl core::fmt::Display for MixnodesErr {
94 fn fmt(&self, fmt: &mut core::fmt::Formatter) -> core::fmt::Result {
95 match self {
96 MixnodesErr::InsufficientRegistrations { num, min } =>
97 write!(fmt, "{num} mixnode(s) registered; {min} is the minimum"),
98 }
99 }
100}