use std::collections::{BTreeMap, BTreeSet};
pub use nibkeep_base::ChangeId;
use nibkeep_base::cbor::{self, FromCbor, ToCbor, Value as Cbor};
use nibkeep_base::small::SmallSet;
use nibkeep_base::{Hash, SpaceId, StatementId};
use nibkeep_crypto::rand_core::CryptoRng;
use nibkeep_crypto::{Envelope, Key, Signature, SigningKey, VerifyingKey, key_hash};
use nibkeep_time::Timestamp;
use crate::keys::{associated_data, change_keys_key};
use crate::{LogError, Payload, SpaceKeys};
const SUITE: u8 = 1;
const SIGNATURE_CONTEXT: &str = "nibkeep change";
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Kind {
#[default]
Ordinary,
Governing,
}
impl ToCbor for Kind {
fn to_cbor(&self) -> Cbor {
match self {
Self::Ordinary => 0u8.to_cbor(),
Self::Governing => 1u8.to_cbor(),
}
}
}
impl FromCbor for Kind {
fn from_cbor(value: &Cbor) -> Result<Self, cbor::Error> {
match u8::from_cbor(value) {
Ok(0) => Ok(Self::Ordinary),
Ok(1) => Ok(Self::Governing),
_ => Err(cbor::Error::Expected("a kind of change: 0 or 1")),
}
}
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Purges {
pub changes: BTreeSet<ChangeId>,
pub statements: BTreeSet<StatementId>,
pub blobs: BTreeSet<Hash>,
}
impl Purges {
#[must_use]
pub fn is_empty(&self) -> bool {
self.changes.is_empty() && self.statements.is_empty() && self.blobs.is_empty()
}
}
impl ToCbor for Purges {
fn to_cbor(&self) -> Cbor {
Cbor::Array(vec![
Cbor::Array(self.changes.iter().map(ToCbor::to_cbor).collect()),
Cbor::Array(self.statements.iter().map(ToCbor::to_cbor).collect()),
Cbor::Array(self.blobs.iter().map(ToCbor::to_cbor).collect()),
])
}
}
impl FromCbor for Purges {
fn from_cbor(value: &Cbor) -> Result<Self, cbor::Error> {
const EXPECTED: &str =
"purges: [changes, statements, blobs], each in ascending order without duplicates";
let Cbor::Array(items) = value else {
return Err(cbor::Error::Expected(EXPECTED));
};
let [changes, statements, blobs] = items.as_slice() else {
return Err(cbor::Error::Expected(EXPECTED));
};
Ok(Self {
changes: ascending(changes, EXPECTED)?,
statements: ascending(statements, EXPECTED)?,
blobs: ascending(blobs, EXPECTED)?,
})
}
}
pub type Restores = Purges;
pub(crate) fn ascending<T: FromCbor + Ord>(
value: &Cbor,
expected: &'static str,
) -> Result<BTreeSet<T>, cbor::Error> {
let items: Vec<T> = Vec::from_cbor(value)?;
if items.windows(2).any(|pair| pair[0] >= pair[1]) {
return Err(cbor::Error::Expected(expected));
}
Ok(items.into_iter().collect())
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SealedPayload(Envelope);
impl SealedPayload {
#[must_use]
pub fn hash(&self) -> Hash {
Hash::of(&cbor::to_vec(self))
}
#[must_use]
pub fn byte_len(&self) -> usize {
self.0.byte_len()
}
}
impl ToCbor for SealedPayload {
fn to_cbor(&self) -> Cbor {
self.0.to_cbor()
}
}
impl FromCbor for SealedPayload {
fn from_cbor(value: &Cbor) -> Result<Self, cbor::Error> {
Envelope::from_cbor(value).map(Self)
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct WrappedChangeKey(Envelope);
impl WrappedChangeKey {
fn wrap(
space: SpaceId,
change: ChangeId,
change_key: &Key,
space_key: &Key,
rng: &mut impl CryptoRng,
) -> Self {
Self(Envelope::seal_key(
&change_keys_key(space_key),
change_key,
&associated_data(space, &change),
rng,
))
}
pub(crate) fn unwrap(&self, change: &Change, keys: &SpaceKeys) -> Option<Key> {
let space_key = keys.get(change.epoch())?;
let key = self
.0
.open_key(
&change_keys_key(space_key),
&associated_data(change.space(), &change.id()),
)
.ok()?;
(key_hash(&key) == change.header.key_hash).then_some(key)
}
}
impl ToCbor for WrappedChangeKey {
fn to_cbor(&self) -> Cbor {
self.0.to_cbor()
}
}
impl FromCbor for WrappedChangeKey {
fn from_cbor(value: &Cbor) -> Result<Self, cbor::Error> {
Envelope::from_cbor(value).map(Self)
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Parcel {
pub change: Change,
pub payload: Option<SealedPayload>,
pub keys: Vec<WrappedChangeKey>,
}
impl Parcel {
#[must_use]
pub const fn id(&self) -> ChangeId {
self.change.id()
}
}
#[derive(Clone, Debug)]
pub struct Draft {
pub parents: BTreeSet<ChangeId>,
pub timestamp: Timestamp,
pub kind: Kind,
pub purges: Purges,
pub restores: Restores,
pub record_keys: BTreeMap<StatementId, Hash>,
pub payload: Payload,
}
impl Draft {
#[must_use]
pub fn new(
parents: impl IntoIterator<Item = ChangeId>,
timestamp: Timestamp,
payload: Payload,
) -> Self {
Self {
parents: parents.into_iter().collect(),
timestamp,
kind: Kind::Ordinary,
purges: Purges::default(),
restores: Restores::default(),
record_keys: BTreeMap::new(),
payload,
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Change {
header: Header,
signature: Signature,
id: ChangeId,
}
#[derive(Clone, Debug, PartialEq, Eq)]
struct Header {
space: SpaceId,
epoch: u64,
author: VerifyingKey,
parents: SmallSet<ChangeId>,
timestamp: Timestamp,
kind: Kind,
payload_hash: Hash,
key_hash: Hash,
sealed: BTreeMap<StatementId, Hash>,
purges: Purges,
restores: Restores,
}
impl Change {
#[must_use]
pub fn create(
space: SpaceId,
keys: &SpaceKeys,
author: &SigningKey,
draft: &Draft,
rng: &mut impl CryptoRng,
) -> Parcel {
let (epoch, space_key) = keys.newest();
let change_key = Key::generate(rng);
let key_hash = key_hash(&change_key);
let payload = SealedPayload(Envelope::seal(
&change_key,
&cbor::to_vec(&draft.payload),
&associated_data(space, &key_hash),
rng,
));
let header = Header {
space,
epoch,
author: author.verifying_key(),
parents: draft.parents.iter().copied().collect(),
timestamp: draft.timestamp,
kind: draft.kind,
payload_hash: payload.hash(),
key_hash,
sealed: draft.record_keys.clone(),
purges: draft.purges.clone(),
restores: draft.restores.clone(),
};
let signature = author.sign(SIGNATURE_CONTEXT, &cbor::to_vec(&header));
let change = Self::from_parts(header, signature);
let key = WrappedChangeKey::wrap(space, change.id(), &change_key, space_key, rng);
Parcel {
change,
payload: Some(payload),
keys: vec![key],
}
}
fn from_parts(header: Header, signature: Signature) -> Self {
let mut change = Self {
header,
signature,
id: ChangeId::from_hash(Hash::of(&[])),
};
change.id = ChangeId::from_hash(Hash::of(&cbor::to_vec(&change)));
change
}
#[must_use]
pub const fn id(&self) -> ChangeId {
self.id
}
#[must_use]
pub const fn space(&self) -> SpaceId {
self.header.space
}
#[must_use]
pub const fn epoch(&self) -> u64 {
self.header.epoch
}
#[must_use]
pub const fn author(&self) -> &VerifyingKey {
&self.header.author
}
#[must_use]
pub const fn parents(&self) -> &SmallSet<ChangeId> {
&self.header.parents
}
#[must_use]
pub const fn timestamp(&self) -> Timestamp {
self.header.timestamp
}
#[must_use]
pub const fn kind(&self) -> Kind {
self.header.kind
}
#[must_use]
pub const fn payload_hash(&self) -> &Hash {
&self.header.payload_hash
}
#[must_use]
pub const fn sealed(&self) -> &BTreeMap<StatementId, Hash> {
&self.header.sealed
}
#[must_use]
pub const fn purges(&self) -> &Purges {
&self.header.purges
}
#[must_use]
pub const fn restores(&self) -> &Restores {
&self.header.restores
}
#[must_use]
pub fn purges_or_restores(&self) -> bool {
!self.header.purges.is_empty() || !self.header.restores.is_empty()
}
pub fn verify(&self) -> Result<(), LogError> {
self.header
.author
.verify(
SIGNATURE_CONTEXT,
&cbor::to_vec(&self.header),
&self.signature,
)
.map_err(|_| LogError::InvalidSignature)
}
pub fn open(
&self,
payload: &SealedPayload,
key: &WrappedChangeKey,
keys: &SpaceKeys,
) -> Result<Payload, LogError> {
if payload.hash() != self.header.payload_hash {
return Err(LogError::OtherPayload);
}
self.open_checked(payload, key, keys)
}
pub(crate) fn open_checked(
&self,
payload: &SealedPayload,
key: &WrappedChangeKey,
keys: &SpaceKeys,
) -> Result<Payload, LogError> {
if keys.get(self.header.epoch).is_none() {
return Err(LogError::UnknownEpoch(self.header.epoch));
}
let change_key = key.unwrap(self, keys).ok_or(LogError::OtherKey)?;
let plaintext = payload
.0
.open(
&change_key,
&associated_data(self.header.space, &self.header.key_hash),
)
.map_err(|_| LogError::Decryption)?;
let opened: Payload = cbor::from_slice(&plaintext).map_err(LogError::Operations)?;
let mut sealed: Vec<StatementId> = opened.all_sealed().map(|(&id, _)| id).collect();
sealed.sort_unstable();
if !sealed.iter().eq(self.header.sealed.keys()) {
return Err(LogError::Operations(cbor::Error::Expected(
"the statements that the change's header says are sealed",
)));
}
Ok(opened)
}
}
fn parents_cbor(parents: &SmallSet<ChangeId>) -> Cbor {
let mut parents: Vec<Vec<u8>> = parents
.iter()
.map(|parent| parent.hash().to_multihash())
.collect();
parents.sort();
Cbor::Array(parents.into_iter().map(Cbor::Bytes).collect())
}
impl ToCbor for Header {
fn to_cbor(&self) -> Cbor {
let mut items = vec![
self.space.to_cbor(),
self.epoch.to_cbor(),
self.author.to_cbor(),
parents_cbor(&self.parents),
self.timestamp.to_cbor(),
self.kind.to_cbor(),
self.payload_hash.to_cbor(),
self.key_hash.to_cbor(),
write_by_statement(&self.sealed),
self.purges.to_cbor(),
];
if !self.restores.is_empty() {
items.push(self.restores.to_cbor());
}
Cbor::Array(items)
}
}
impl FromCbor for Header {
fn from_cbor(value: &Cbor) -> Result<Self, cbor::Error> {
const EXPECTED: &str = "a change header: [space, epoch, author, parents, timestamp, kind, payload hash, key hash, sealed, purges], and restores if it restores anything";
let Cbor::Array(items) = value else {
return Err(cbor::Error::Expected(EXPECTED));
};
let (items, restores) = match items.as_slice() {
[header @ .., restores] if header.len() == 10 => {
let restores = Restores::from_cbor(restores)?;
if restores.is_empty() {
return Err(cbor::Error::Expected(EXPECTED));
}
(header, restores)
}
items => (items, Restores::default()),
};
let [
space,
epoch,
author,
parents,
timestamp,
kind,
payload_hash,
key_hash,
sealed,
purges,
] = items
else {
return Err(cbor::Error::Expected(EXPECTED));
};
Ok(Self {
space: SpaceId::from_cbor(space)?,
epoch: u64::from_cbor(epoch)?,
author: VerifyingKey::from_cbor(author)?,
parents: read_parents(parents)?,
timestamp: Timestamp::from_cbor(timestamp)?,
kind: Kind::from_cbor(kind)?,
payload_hash: Hash::from_cbor(payload_hash)?,
key_hash: Hash::from_cbor(key_hash)?,
sealed: read_by_statement(
sealed,
"sealed statements: [id, record key hash] in ascending order of their IDs",
)?,
purges: Purges::from_cbor(purges)?,
restores,
})
}
}
pub(crate) fn read_by_statement<T: FromCbor>(
value: &Cbor,
expected: &'static str,
) -> Result<BTreeMap<StatementId, T>, cbor::Error> {
let Cbor::Array(items) = value else {
return Err(cbor::Error::Expected(expected));
};
let mut read = BTreeMap::new();
for item in items {
let Cbor::Array(pair) = item else {
return Err(cbor::Error::Expected(expected));
};
let [statement, value] = pair.as_slice() else {
return Err(cbor::Error::Expected(expected));
};
let statement = StatementId::from_cbor(statement)?;
if read
.last_key_value()
.is_some_and(|(&last, _)| last >= statement)
{
return Err(cbor::Error::Expected(expected));
}
read.insert(statement, T::from_cbor(value)?);
}
Ok(read)
}
pub(crate) fn write_by_statement<T: ToCbor>(pairs: &BTreeMap<StatementId, T>) -> Cbor {
Cbor::Array(
pairs
.iter()
.map(|(statement, value)| Cbor::Array(vec![statement.to_cbor(), value.to_cbor()]))
.collect(),
)
}
fn read_parents(value: &Cbor) -> Result<SmallSet<ChangeId>, cbor::Error> {
const EXPECTED: &str = "parents: change IDs sorted by their bytes, without duplicates";
let Cbor::Array(items) = value else {
return Err(cbor::Error::Expected(EXPECTED));
};
let mut previous: Option<&[u8]> = None;
let mut parents = SmallSet::new();
for item in items {
let Cbor::Bytes(bytes) = item else {
return Err(cbor::Error::Expected(EXPECTED));
};
if previous.is_some_and(|previous| previous >= bytes.as_slice()) {
return Err(cbor::Error::Expected(EXPECTED));
}
previous = Some(bytes);
parents.insert(ChangeId::from_cbor(item)?);
}
Ok(parents)
}
impl ToCbor for Change {
fn to_cbor(&self) -> Cbor {
Cbor::Array(vec![
SUITE.to_cbor(),
self.header.to_cbor(),
self.signature.to_cbor(),
])
}
}
impl FromCbor for Change {
fn from_cbor(value: &Cbor) -> Result<Self, cbor::Error> {
const EXPECTED: &str = "a change: [1, header, signature]";
let Cbor::Array(items) = value else {
return Err(cbor::Error::Expected(EXPECTED));
};
let [suite, header, signature] = items.as_slice() else {
return Err(cbor::Error::Expected(EXPECTED));
};
if u8::from_cbor(suite) != Ok(SUITE) {
return Err(cbor::Error::Expected(EXPECTED));
}
Ok(Self::from_parts(
Header::from_cbor(header)?,
Signature::from_cbor(signature)?,
))
}
}