1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
use crate::{
	async_protocols::{remote::AsyncProtocolRemote, BatchKey, KeygenPartyId},
	gossip_engine::GossipEngineIface,
	utils::SendFuture,
	worker::{DKGWorker, ProtoStageType},
	Client,
};
use async_trait::async_trait;
use dkg_primitives::types::{DKGError, SSID};
use dkg_runtime_primitives::{
	crypto::{AuthorityId, Public},
	BatchId, DKGApi, MaxAuthorities, MaxProposalLength, MaxProposalsInBatch, SessionId,
	StoredUnsignedProposalBatch,
};
use mp_ecdsa::MpEcdsaDKG;
use parking_lot::RwLock;
use sc_client_api::Backend;
use sp_runtime::traits::{Block, NumberFor};
use std::{marker::PhantomData, pin::Pin, sync::Arc};
use wt_frost::WTFrostDKG;

pub mod mp_ecdsa;
pub mod wt_frost;

/// Setup parameters for the Keygen protocol
pub enum KeygenProtocolSetupParameters<B: Block> {
	MpEcdsa {
		best_authorities: Vec<(KeygenPartyId, Public)>,
		authority_public_key: Public,
		party_i: KeygenPartyId,
		session_id: SessionId,
		associated_block: NumberFor<B>,
		threshold: u16,
		stage: ProtoStageType,
		keygen_protocol_hash: [u8; 32],
	},
	WTFrost {
		authority_id: AuthorityId,
		best_authorities: Vec<(KeygenPartyId, Public)>,
		authority_public_key: Public,
		keygen_protocol_hash: [u8; 32],
		threshold: u32,
		session_id: SessionId,
		associated_block: NumberFor<B>,
		stage: ProtoStageType,
	},
}

/// Setup parameters for the Signing protocol
pub enum SigningProtocolSetupParameters<B: Block> {
	MpEcdsa {
		best_authorities: Vec<(KeygenPartyId, Public)>,
		authority_public_key: Public,
		party_i: KeygenPartyId,
		session_id: SessionId,
		threshold: u16,
		stage: ProtoStageType,
		unsigned_proposal_batch: StoredUnsignedProposalBatch<
			BatchId,
			MaxProposalLength,
			MaxProposalsInBatch,
			NumberFor<B>,
		>,
		signing_set: Vec<KeygenPartyId>,
		associated_block_id: NumberFor<B>,
		ssid: SSID,
	},
	WTFrost {
		authority_id: AuthorityId,
		unsigned_proposal_hash: [u8; 32],
		unsigned_proposal_batch: StoredUnsignedProposalBatch<
			BatchId,
			MaxProposalLength,
			MaxProposalsInBatch,
			NumberFor<B>,
		>,
		threshold: u32,
		session_id: SessionId,
		batch_key: BatchKey,
		associated_block: NumberFor<B>,
		stage: ProtoStageType,
		ssid: SSID,
	},
}

/// A type which is used directly by the Job Manager to initialize and manage the DKG protocol
pub type ProtocolInitReturn<B> =
	(AsyncProtocolRemote<NumberFor<B>>, Pin<Box<dyn SendFuture<'static, ()>>>);

#[async_trait]
/// Generalizes the DKGWorker::initialize_keygen_protocol and DKGWorker::initialize_signing_protocol
/// Also includes two functions which are used for determining whether a DKG can handle the request
/// parameters, which is used in the [`DKGModules`] implementation
pub trait DKG<B: Block>: Send + Sync {
	async fn initialize_keygen_protocol(
		&self,
		params: KeygenProtocolSetupParameters<B>,
	) -> Option<ProtocolInitReturn<B>>;
	async fn initialize_signing_protocol(
		&self,
		params: SigningProtocolSetupParameters<B>,
	) -> Result<ProtocolInitReturn<B>, DKGError>;
	fn can_handle_keygen_request(&self, params: &KeygenProtocolSetupParameters<B>) -> bool;
	fn can_handle_signing_request(&self, params: &SigningProtocolSetupParameters<B>) -> bool;
}

/// Holds a list of DKGs that can be used at runtime
pub struct DKGModules<B: Block, BE, C, GE> {
	dkgs: Arc<RwLock<Vec<Arc<dyn DKG<B>>>>>,
	_pd: PhantomData<(BE, C, GE)>,
}

impl<B, BE, C, GE> DKGModules<B, BE, C, GE>
where
	B: Block,
	BE: Backend<B> + Unpin + 'static,
	C: Client<B, BE> + 'static,
	GE: GossipEngineIface,
	C::Api: DKGApi<B, AuthorityId, NumberFor<B>, MaxProposalLength, MaxAuthorities>,
{
	/// Loads the default DKG modules internally to be available at runtime
	pub fn initialize(&self, dkg_worker: DKGWorker<B, BE, C, GE>) {
		*self.dkgs.write() = vec![
			Arc::new(MpEcdsaDKG { dkg_worker: dkg_worker.clone() }),
			Arc::new(WTFrostDKG { dkg_worker }),
		]
	}

	/// Given a set of parameters, returns the keygen protocol initializer which can handle the
	/// request
	pub fn get_keygen_protocol(
		&self,
		params: &KeygenProtocolSetupParameters<B>,
	) -> Option<Arc<dyn DKG<B>>> {
		self.dkgs
			.read()
			.iter()
			.find(|dkg| dkg.can_handle_keygen_request(params))
			.cloned()
	}

	/// Given a set of parameters, returns the signing protocol initializer which can handle the
	/// request
	pub fn get_signing_protocol(
		&self,
		params: &SigningProtocolSetupParameters<B>,
	) -> Option<Arc<dyn DKG<B>>> {
		self.dkgs
			.read()
			.iter()
			.find(|dkg| dkg.can_handle_signing_request(params))
			.cloned()
	}
}

impl<B, BE, C, GE> Default for DKGModules<B, BE, C, GE>
where
	B: Block,
	BE: Backend<B> + 'static,
	C: Client<B, BE> + 'static,
	GE: GossipEngineIface,
{
	fn default() -> Self {
		Self { dkgs: Arc::new(RwLock::new(vec![])), _pd: PhantomData }
	}
}

impl<B, BE, C, GE> Clone for DKGModules<B, BE, C, GE>
where
	B: Block,
	BE: Backend<B> + 'static,
	C: Client<B, BE> + 'static,
	GE: GossipEngineIface,
{
	fn clone(&self) -> Self {
		Self { dkgs: self.dkgs.clone(), _pd: PhantomData }
	}
}