#[cfg(any(feature = "ffi", target_arch = "wasm32"))]
pub(crate) mod wire;
#[cfg(test)]
mod tests;
use std::collections::HashMap;
use std::fmt;
use std::fmt::Debug;
use std::fmt::Formatter;
use crate::protocol::types::ChannelShare;
use crate::protocol::types::Secret;
use crate::{
primitives::discovery::response::SecretVersionEntry,
protocol::types::{Target, UserSecret},
types::{ChannelId, SharedKey},
};
use derec_proto::{
ContactMessage, GetSecretIdsVersionsRequestMessage, GetShareRequestMessage, PairRequestMessage,
PrePairRequestMessage, SenderKind, StoreShareRequestMessage, TransportProtocol,
UnpairRequestMessage, UpdateChannelInfoRequestMessage, VerifyShareRequestMessage,
};
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct RecoveryHelper {
pub channel_id: ChannelId,
pub communication_info: HashMap<String, String>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct RecoveryCandidate {
pub secret: Secret,
pub supported_by: Vec<RecoveryHelper>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum IgnoreReason {
PendingVerification,
Expired,
TimestampMismatch,
StaleResponse,
Superseded,
ProtocolVersionTooOld,
ProtocolVersionMismatch,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum VerifyFailureReason {
NoSharedKey,
NoProof,
DispatchFailed,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum NotRestoredReason {
NoTransports,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum CorruptionReason {
Malformed,
InvalidProof,
Forged,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum PendingActionKind {
Pairing,
PrePair,
StoreShare,
VerifyShare,
Discovery,
GetShare,
Unpair,
UpdateChannelInfo,
Failed,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct AutoAcceptPolicy {
pub pairing: bool,
pub pre_pair: bool,
pub store_share: bool,
pub verify_share: bool,
pub discovery: bool,
pub get_share: bool,
pub unpair: bool,
pub update_channel_info: bool,
}
impl AutoAcceptPolicy {
pub fn all() -> Self {
Self {
pairing: true,
pre_pair: true,
store_share: true,
verify_share: true,
discovery: true,
get_share: true,
unpair: true,
update_channel_info: true,
}
}
pub fn allows(&self, action: &PendingAction) -> bool {
match action.kind() {
PendingActionKind::Pairing => self.pairing,
PendingActionKind::PrePair => self.pre_pair,
PendingActionKind::StoreShare => self.store_share,
PendingActionKind::VerifyShare => self.verify_share,
PendingActionKind::Discovery => self.discovery,
PendingActionKind::GetShare => self.get_share,
PendingActionKind::Unpair => self.unpair,
PendingActionKind::UpdateChannelInfo => self.update_channel_info,
PendingActionKind::Failed => false,
}
}
}
#[derive(Clone)]
pub enum PendingAction {
Pairing {
channel_id: ChannelId,
request: PairRequestMessage,
kind: SenderKind,
peer_communication_info: HashMap<String, String>,
trace_id: u64,
},
PrePair {
channel_id: ChannelId,
request: PrePairRequestMessage,
trace_id: u64,
},
StoreShare {
channel_id: ChannelId,
request: StoreShareRequestMessage,
shared_key: SharedKey,
trace_id: u64,
},
VerifyShare {
channel_id: ChannelId,
request: VerifyShareRequestMessage,
shared_key: SharedKey,
trace_id: u64,
},
Discovery {
channel_id: ChannelId,
request: GetSecretIdsVersionsRequestMessage,
shared_key: SharedKey,
trace_id: u64,
},
GetShare {
channel_id: ChannelId,
request: GetShareRequestMessage,
shared_key: SharedKey,
trace_id: u64,
},
Unpair {
channel_id: ChannelId,
request: UnpairRequestMessage,
shared_key: SharedKey,
trace_id: u64,
},
UpdateChannelInfo {
channel_id: ChannelId,
request: UpdateChannelInfoRequestMessage,
shared_key: SharedKey,
trace_id: u64,
},
RespondToFailure {
channel_id: ChannelId,
trace_id: u64,
reply: ReplyTarget,
request: FailedRequest,
},
}
#[derive(Clone, Debug, PartialEq)]
pub struct ReplyTarget {
pub channel_id: ChannelId,
pub endpoints: Vec<TransportProtocol>,
}
#[derive(Clone, PartialEq)]
pub enum FailedRequest {
StoreShare(StoreShareRequestMessage),
VerifyShare(VerifyShareRequestMessage),
GetSecretIdsVersions(GetSecretIdsVersionsRequestMessage),
GetShare(GetShareRequestMessage),
Unpair(UnpairRequestMessage),
UpdateChannelInfo(UpdateChannelInfoRequestMessage),
Undecodable,
}
impl Debug for FailedRequest {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.write_str(match self {
FailedRequest::StoreShare(_) => "StoreShare",
FailedRequest::VerifyShare(_) => "VerifyShare",
FailedRequest::GetSecretIdsVersions(_) => "GetSecretIdsVersions",
FailedRequest::GetShare(_) => "GetShare",
FailedRequest::Unpair(_) => "Unpair",
FailedRequest::UpdateChannelInfo(_) => "UpdateChannelInfo",
FailedRequest::Undecodable => "Undecodable",
})
}
}
impl Debug for PendingAction {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let channel_id = match self {
PendingAction::Pairing { channel_id, .. }
| PendingAction::PrePair { channel_id, .. }
| PendingAction::StoreShare { channel_id, .. }
| PendingAction::VerifyShare { channel_id, .. }
| PendingAction::Discovery { channel_id, .. }
| PendingAction::GetShare { channel_id, .. }
| PendingAction::Unpair { channel_id, .. }
| PendingAction::UpdateChannelInfo { channel_id, .. }
| PendingAction::RespondToFailure { channel_id, .. } => channel_id,
};
f.debug_struct("PendingAction")
.field("kind", &self.kind())
.field("channel_id", &channel_id.0)
.finish()
}
}
impl PendingAction {
pub fn kind(&self) -> PendingActionKind {
match self {
PendingAction::Pairing { .. } => PendingActionKind::Pairing,
PendingAction::PrePair { .. } => PendingActionKind::PrePair,
PendingAction::StoreShare { .. } => PendingActionKind::StoreShare,
PendingAction::VerifyShare { .. } => PendingActionKind::VerifyShare,
PendingAction::Discovery { .. } => PendingActionKind::Discovery,
PendingAction::GetShare { .. } => PendingActionKind::GetShare,
PendingAction::Unpair { .. } => PendingActionKind::Unpair,
PendingAction::UpdateChannelInfo { .. } => PendingActionKind::UpdateChannelInfo,
PendingAction::RespondToFailure { .. } => PendingActionKind::Failed,
}
}
pub fn trace_id(&self) -> u64 {
match self {
PendingAction::Pairing { trace_id, .. }
| PendingAction::PrePair { trace_id, .. }
| PendingAction::StoreShare { trace_id, .. }
| PendingAction::VerifyShare { trace_id, .. }
| PendingAction::Discovery { trace_id, .. }
| PendingAction::GetShare { trace_id, .. }
| PendingAction::Unpair { trace_id, .. }
| PendingAction::UpdateChannelInfo { trace_id, .. }
| PendingAction::RespondToFailure { trace_id, .. } => *trace_id,
}
}
}
pub enum DeRecFlow {
Pairing {
kind: SenderKind,
contact: ContactMessage,
peer_communication_info: HashMap<String, String>,
},
Discovery {
target: Target,
},
ProtectSecret {
secrets: Vec<UserSecret>,
description: Option<String>,
weights: HashMap<ChannelId, u32>,
threshold: Option<u32>,
},
VerifyShares {
secret_id: u64,
version: u32,
target: Target,
},
RecoverSecret {
secret_id: u64,
version: u32,
},
ReplicaDiscovery,
UnpairReplica {
replica_id: u64,
memo: Option<String>,
},
Unpair {
channel_id: ChannelId,
memo: Option<String>,
},
UpdateChannelInfo {
target: Target,
communication_info: Option<HashMap<String, String>>,
own_transports: Vec<TransportProtocol>,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum UnpairAck {
#[default]
Required,
NotRequired,
}
#[non_exhaustive]
#[derive(Debug)]
#[allow(clippy::large_enum_variant)]
pub enum DeRecEvent {
PairingCompleted {
channel_id: ChannelId,
pairing_channel_id: ChannelId,
kind: SenderKind,
peer_communication_info: HashMap<String, String>,
},
ReplicaPaired {
channel_id: ChannelId,
peer_replica_id: u64,
},
ShareStored { channel_id: ChannelId, version: u32 },
ShareRefused {
channel_id: ChannelId,
version: u32,
status: i32,
memo: String,
},
ReplicaSecretReceived {
channel_id: ChannelId,
from_replica_id: u64,
author_replica_id: Option<u64>,
secret_id: u64,
version: u32,
secret: Secret,
shares: Vec<ChannelShare>,
},
ReplicaSecretInstalled {
channel_id: ChannelId,
from_replica_id: u64,
author_replica_id: Option<u64>,
secret_id: u64,
version: u32,
secret: Secret,
shares: Vec<ChannelShare>,
},
ReplicaVersionConflict {
channel_id: ChannelId,
from_replica_id: u64,
secret_id: u64,
version: u32,
held_author_replica_id: Option<u64>,
incoming_author_replica_id: Option<u64>,
secret: Secret,
},
ReplicaSecretRefused {
channel_id: ChannelId,
from_replica_id: u64,
secret_id: u64,
version: u32,
status: i32,
memo: String,
},
ReplicaSecretAcked {
channel_id: ChannelId,
from_replica_id: u64,
secret_id: u64,
version: u32,
status: i32,
memo: String,
},
ReplicaSyncRejected {
replica_id: u64,
secret_id: u64,
version: u32,
status: i32,
memo: String,
},
ReplicaSyncFailed {
replica_id: u64,
version: u32,
reason: String,
},
ReplicaRemoved {
replica_id: u64,
},
ReplicaSourceChanged {
replica_id: u64,
},
SelfRemovedFromGroup {
version: u32,
},
ReplicaDiscoveryComplete {
local_version: u32,
group_version: u32,
fetched_from: Option<u64>,
},
ReplicaSyncComplete {
version: u32,
synced: Vec<u64>,
behind: Vec<u64>,
},
ShareConfirmed { channel_id: ChannelId, version: u32 },
ShareRejected {
channel_id: ChannelId,
version: u32,
status: i32,
memo: String,
},
SharingComplete {
version: u32,
confirmed_count: usize,
failed_count: usize,
threshold_met: bool,
},
ShareVerified {
channel_id: ChannelId,
version: u32,
subshares: Option<u32>,
threshold: Option<u32>,
},
ShareVerifyInvalid { channel_id: ChannelId, version: u32 },
ShareVerifyRejected {
channel_id: ChannelId,
version: u32,
status: i32,
memo: String,
},
SecretsDiscovered {
channel_id: ChannelId,
secrets: Vec<SecretVersionEntry>,
},
RecoveryShareReceived {
channel_id: ChannelId,
shares_received: usize,
},
RecoveryShareRefused {
channel_id: ChannelId,
version: u32,
status: i32,
memo: String,
},
RecoveryShareCorrupted {
channel_id: ChannelId,
version: u32,
reason: CorruptionReason,
},
SecretRecovered { secret: Secret },
RecoveryConflict {
version: u32,
candidates: Vec<RecoveryCandidate>,
disputed_by: Vec<RecoveryHelper>,
},
RecoveryDisputed {
version: u32,
secret: Secret,
supported_by: Vec<RecoveryHelper>,
disputed_by: Vec<RecoveryHelper>,
},
RecoveryIncomplete {
secret_id: u64,
version: u32,
shares_received: usize,
},
ActionRequired {
channel_id: ChannelId,
action: PendingAction,
},
AutoAccepted {
channel_id: ChannelId,
action_kind: PendingActionKind,
},
RequestFailed {
channel_id: ChannelId,
action_kind: PendingActionKind,
responded: bool,
status: i32,
memo: String,
error_code: String,
message: String,
action: Option<PendingAction>,
},
ErrorResponseReceived {
channel_id: ChannelId,
status: i32,
memo: String,
},
Unpaired { channel_id: ChannelId },
UnpairRejected {
channel_id: ChannelId,
status: i32,
memo: String,
},
PrePairRejected {
channel_id: ChannelId,
status: i32,
memo: String,
},
PairingRefused {
channel_id: ChannelId,
status: i32,
memo: String,
error_code: String,
message: String,
},
ChannelInfoUpdated { channel_id: ChannelId },
ChannelInfoUpdateRejected {
channel_id: ChannelId,
status: i32,
memo: String,
},
NoOp,
MessageIgnored {
channel_id: ChannelId,
reason: IgnoreReason,
trace_id: u64,
},
PeerNotRestored {
channel_id: ChannelId,
replica_id: Option<u64>,
reason: NotRestoredReason,
},
PairingStarted {
channel_id: ChannelId,
trace_id: u64,
kind: SenderKind,
},
DiscoveryStarted {
channel_id: ChannelId,
trace_id: u64,
},
DiscoveryFailed {
channel_id: ChannelId,
error: String,
},
ProtectSecretStarted {
channel_id: ChannelId,
version: u32,
trace_id: u64,
weight: u32,
},
ProtectSecretFailed {
channel_id: ChannelId,
version: u32,
error: String,
weight: u32,
},
VerifySharesStarted {
channel_id: ChannelId,
version: u32,
trace_id: u64,
},
VerifySharesFailed {
channel_id: ChannelId,
version: u32,
reason: VerifyFailureReason,
error: String,
},
RecoverSecretStarted {
channel_id: ChannelId,
version: u32,
trace_id: u64,
},
RecoverSecretFailed {
channel_id: ChannelId,
version: u32,
error: String,
},
UnpairFailed {
channel_id: ChannelId,
error: String,
},
UnpairStarted {
channel_id: ChannelId,
trace_id: u64,
},
UpdateChannelInfoStarted {
channel_id: ChannelId,
trace_id: u64,
},
UpdateChannelInfoFailed {
channel_id: ChannelId,
error: String,
},
}