use std::collections::BTreeMap;
use nibkeep_base::cbor::{self, FromCbor, ToCbor, Value as Cbor};
use nibkeep_base::{SpaceId, StatementId};
use nibkeep_crypto::rand_core::CryptoRng;
use nibkeep_crypto::{Envelope, Key};
use nibkeep_model::{Operation, Statement};
use crate::ChangeId;
use crate::change::{read_by_statement, write_by_statement};
use crate::keys::associated_data;
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Payload {
pub operations: Vec<Operation>,
pub sealed: BTreeMap<StatementId, Envelope>,
pub restated: BTreeMap<ChangeId, Restated>,
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Restated {
pub operations: Vec<Operation>,
pub sealed: BTreeMap<StatementId, Envelope>,
}
impl Restated {
#[must_use]
pub fn is_empty(&self) -> bool {
self.operations.is_empty() && self.sealed.is_empty()
}
}
impl Payload {
pub fn all_operations(&self) -> impl Iterator<Item = &Operation> {
self.operations.iter().chain(
self.restated
.values()
.flat_map(|restated| restated.operations.iter()),
)
}
pub fn all_sealed(&self) -> impl Iterator<Item = (&StatementId, &Envelope)> {
self.sealed.iter().chain(
self.restated
.values()
.flat_map(|restated| restated.sealed.iter()),
)
}
}
impl From<Vec<Operation>> for Payload {
fn from(operations: Vec<Operation>) -> Self {
Self {
operations,
..Self::default()
}
}
}
pub(crate) fn seal(
space: SpaceId,
statement: &Statement,
record_key: &Key,
rng: &mut impl CryptoRng,
) -> Envelope {
Envelope::seal(
record_key,
&cbor::to_vec(statement),
&associated_data(space, &statement.id),
rng,
)
}
pub(crate) fn open_sealed(
space: SpaceId,
id: StatementId,
envelope: &Envelope,
record_key: &Key,
) -> Option<Statement> {
let plaintext = envelope
.open(record_key, &associated_data(space, &id))
.ok()?;
let statement: Statement = cbor::from_slice(&plaintext).ok()?;
(statement.id == id).then_some(statement)
}
impl ToCbor for Payload {
fn to_cbor(&self) -> Cbor {
Cbor::Array(vec![
Cbor::Array(self.operations.iter().map(ToCbor::to_cbor).collect()),
write_by_statement(&self.sealed),
Cbor::Array(
self.restated
.iter()
.map(|(change, restated)| {
Cbor::Array(vec![
change.to_cbor(),
Cbor::Array(restated.operations.iter().map(ToCbor::to_cbor).collect()),
write_by_statement(&restated.sealed),
])
})
.collect(),
),
])
}
}
impl FromCbor for Payload {
fn from_cbor(value: &Cbor) -> Result<Self, cbor::Error> {
const EXPECTED: &str = "a payload: [operations, sealed, restated], with sealed statements and restated changes in ascending order of their IDs";
let error = || cbor::Error::Expected(EXPECTED);
let Cbor::Array(items) = value else {
return Err(error());
};
let [operations, sealed, restated] = items.as_slice() else {
return Err(error());
};
let Cbor::Array(restated) = restated else {
return Err(error());
};
let mut restated_by_change: BTreeMap<ChangeId, Restated> = BTreeMap::new();
for entry in restated {
let Cbor::Array(entry) = entry else {
return Err(error());
};
let [change, operations, sealed] = entry.as_slice() else {
return Err(error());
};
let change = ChangeId::from_cbor(change)?;
if restated_by_change
.last_key_value()
.is_some_and(|(&last, _)| last >= change)
{
return Err(error());
}
restated_by_change.insert(
change,
Restated {
operations: Vec::from_cbor(operations)?,
sealed: read_by_statement(sealed, EXPECTED)?,
},
);
}
Ok(Self {
operations: Vec::from_cbor(operations)?,
sealed: read_by_statement(sealed, EXPECTED)?,
restated: restated_by_change,
})
}
}