use alloy_primitives::U256;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct TaskId {
pub service_id: u64,
pub call_id: u64,
}
impl TaskId {
pub fn new(service_id: u64, call_id: u64) -> Self {
Self {
service_id,
call_id,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ThresholdConfig {
Count { required_signers: u32 },
StakeWeighted {
threshold_bps: u32,
operator_stakes: Vec<OperatorStake>,
},
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OperatorStake {
pub operator_index: u32,
pub stake: u64,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SubmitSignatureRequest {
pub service_id: u64,
pub call_id: u64,
pub operator_index: u32,
#[serde(with = "hex_bytes")]
pub output: Vec<u8>,
#[serde(with = "hex_bytes")]
pub signature: Vec<u8>,
#[serde(with = "hex_bytes")]
pub public_key: Vec<u8>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SubmitSignatureResponse {
pub accepted: bool,
pub signatures_collected: usize,
pub threshold_required: usize,
pub threshold_met: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub error: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GetStatusRequest {
pub service_id: u64,
pub call_id: u64,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GetStatusResponse {
pub exists: bool,
pub signatures_collected: usize,
pub threshold_required: usize,
pub threshold_met: bool,
pub signer_bitmap: U256,
#[serde(skip_serializing_if = "Option::is_none")]
pub signed_stake_bps: Option<u32>,
pub submitted: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub is_expired: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub time_remaining_secs: Option<u64>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InitTaskRequest {
pub service_id: u64,
pub call_id: u64,
pub operator_count: u32,
pub threshold: ThresholdConfig,
#[serde(with = "hex_bytes")]
pub output: Vec<u8>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InitTaskResponse {
pub success: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub error: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AggregatedResultResponse {
pub service_id: u64,
pub call_id: u64,
#[serde(with = "hex_bytes")]
pub output: Vec<u8>,
pub signer_bitmap: U256,
pub non_signer_indices: Vec<u32>,
#[serde(with = "hex_bytes")]
pub aggregated_signature: Vec<u8>,
#[serde(with = "hex_bytes")]
pub aggregated_pubkey: Vec<u8>,
}
mod hex_bytes {
use serde::{Deserialize, Deserializer, Serializer};
pub fn serialize<S>(bytes: &[u8], serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&format!("0x{}", hex::encode(bytes)))
}
pub fn deserialize<'de, D>(deserializer: D) -> Result<Vec<u8>, D::Error>
where
D: Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
let s = s.strip_prefix("0x").unwrap_or(&s);
hex::decode(s).map_err(serde::de::Error::custom)
}
}