Skip to main content

soil_cli/params/
mixnet_params.rs

1// This file is part of Soil.
2
3// Copyright (C) Soil contributors.
4// Copyright (C) Parity Technologies (UK) Ltd.
5// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
6
7use clap::Args;
8use std::str::FromStr;
9use subsoil::core::H256;
10
11fn parse_kx_secret(s: &str) -> Result<soil_network::mixnet::KxSecret, String> {
12	H256::from_str(s).map(H256::to_fixed_bytes).map_err(|err| err.to_string())
13}
14
15/// Parameters used to create the mixnet configuration.
16#[derive(Debug, Clone, Args)]
17pub struct MixnetParams {
18	/// Enable the mixnet service.
19	///
20	/// This will make the mixnet RPC methods available. If the node is running as a validator, it
21	/// will also attempt to register and operate as a mixnode.
22	#[arg(long)]
23	pub mixnet: bool,
24
25	/// The mixnet key-exchange secret to use in session 0.
26	///
27	/// Should be 64 hex characters, giving a 32-byte secret.
28	///
29	/// WARNING: Secrets provided as command-line arguments are easily exposed. Use of this option
30	/// should be limited to development and testing.
31	#[arg(long, value_name = "SECRET", value_parser = parse_kx_secret)]
32	pub mixnet_session_0_kx_secret: Option<soil_network::mixnet::KxSecret>,
33}
34
35impl MixnetParams {
36	/// Returns the mixnet configuration, or `None` if the mixnet is disabled.
37	pub fn config(&self, is_authority: bool) -> Option<soil_network::mixnet::Config> {
38		self.mixnet.then(|| {
39			let mut config = soil_network::mixnet::Config {
40				core: soil_network::mixnet::CoreConfig {
41					session_0_kx_secret: self.mixnet_session_0_kx_secret,
42					..Default::default()
43				},
44				..Default::default()
45			};
46			if !is_authority {
47				// Only authorities can be mixnodes; don't attempt to register
48				config.substrate.register = false;
49				// Only mixnodes need to allow connections from non-mixnodes
50				config.substrate.num_gateway_slots = 0;
51			}
52			config
53		})
54	}
55}