mod key_generation; mod key_resharing; mod sign; use std::{collections::HashMap, sync::Arc}; use borsh::{BorshDeserialize, BorshSerialize}; use mpc_primitives::domain::DomainId; use near_mpc_contract_interface::types::KeyEventId; use threshold_signatures::confidential_key_derivation::{ ElementG1, KeygenOutput, SigningShare, VerifyingKey, }; use threshold_signatures::ReconstructionThreshold; use mpc_node_config::ConfigFile; use crate::{ config::{MpcConfig, ParticipantsConfig}, network::{MeshNetworkClient, NetworkTaskChannel}, primitives::MpcTaskId, providers::SignatureProvider, storage::CKDRequestStorage, types::{CKDId, SignatureId}, }; #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, BorshSerialize, BorshDeserialize)] pub enum CKDTaskId { KeyGeneration { key_event: KeyEventId }, KeyResharing { key_event: KeyEventId }, Ckd { id: CKDId }, } impl From for MpcTaskId { fn from(value: CKDTaskId) -> Self { MpcTaskId::CKDTaskId(value) } } #[derive(Clone)] pub struct CKDProvider { config: Arc, mpc_config: Arc, client: Arc, ckd_request_store: Arc, keyshares: HashMap, } impl CKDProvider { pub fn new( config: Arc, mpc_config: Arc, client: Arc, ckd_request_store: Arc, keyshares: HashMap, ) -> Self { Self { config, mpc_config, client, ckd_request_store, keyshares, } } } impl SignatureProvider for CKDProvider { type PublicKey = VerifyingKey; type SecretShare = SigningShare; type KeygenOutput = KeygenOutput; type Signature = (ElementG1, ElementG1); type TaskId = CKDTaskId; async fn make_signature( &self, id: SignatureId, ) -> anyhow::Result<(Self::Signature, Self::PublicKey)> { self.make_ckd_leader(id).await } async fn run_key_generation_client( threshold: ReconstructionThreshold, channel: NetworkTaskChannel, ) -> anyhow::Result { Self::run_key_generation_client_internal(threshold, channel).await } async fn run_key_resharing_client( new_threshold: ReconstructionThreshold, key_share: Option, public_key: VerifyingKey, old_participants: &ParticipantsConfig, channel: NetworkTaskChannel, ) -> anyhow::Result { Self::run_key_resharing_client_internal( new_threshold, key_share, public_key, old_participants, channel, ) .await } async fn process_channel(&self, channel: NetworkTaskChannel) -> anyhow::Result<()> { match channel.task_id() { MpcTaskId::CKDTaskId(task) => match task { CKDTaskId::KeyGeneration { .. } => { anyhow::bail!("Key generation rejected in normal node operation"); } CKDTaskId::KeyResharing { .. } => { anyhow::bail!("Key resharing rejected in normal node operation"); } CKDTaskId::Ckd { id } => { self.make_ckd_follower(channel, id).await?; } }, _ => anyhow::bail!( "ckd task handler: received unexpected task id: {:?}", channel.task_id() ), } Ok(()) } async fn spawn_background_tasks(self: Arc) -> anyhow::Result<()> { Ok(()) } }