use identity::Ed25519Keypair;
use p2panda_core::cbor::{decode_cbor, encode_cbor};
use p2panda_core::operation::validate_operation;
use p2panda_core::{Body, Hash, Header, Operation, SigningKey};
use serde::{Deserialize, Serialize};
use super::MootDelegationEvent;
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct MootDelegationExt {
pub moot_id: [u8; 32],
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, thiserror::Error)]
pub enum MootDelegationWireError {
#[error("delegation operation has no body")]
MissingBody,
#[error("delegation operation body is malformed")]
Malformed,
}
pub fn to_operation(
keypair: &Ed25519Keypair,
moot_id: [u8; 32],
event: &MootDelegationEvent,
seq_num: u32,
backlink: Option<[u8; 32]>,
) -> Operation<MootDelegationExt> {
to_operation_seed(keypair.to_seed(), moot_id, event, seq_num, backlink)
}
pub fn to_operation_seed(
signing_seed: [u8; 32],
moot_id: [u8; 32],
event: &MootDelegationEvent,
seq_num: u32,
backlink: Option<[u8; 32]>,
) -> Operation<MootDelegationExt> {
let signing_key = SigningKey::from_bytes(&signing_seed);
let bytes = encode_cbor(event).expect("delegation statements always CBOR-encode");
let body = Body::from_bytes(&bytes);
let header = Header::builder()
.body(&bytes)
.seq_num(seq_num)
.backlink(backlink.map(Hash::from))
.build(&signing_key, MootDelegationExt { moot_id });
let hash = header.hash();
Operation {
hash,
header,
body: Some(body),
}
}
pub fn from_operation(
operation: &Operation<MootDelegationExt>,
) -> Result<MootDelegationEvent, MootDelegationWireError> {
let body = operation
.body
.as_ref()
.ok_or(MootDelegationWireError::MissingBody)?;
decode_cbor(body.to_bytes().as_slice()).map_err(|_| MootDelegationWireError::Malformed)
}
pub fn verify(operation: &Operation<MootDelegationExt>) -> bool {
validate_operation(operation).is_ok() && operation.hash == operation.header.hash()
}
#[cfg(test)]
mod tests {
use std::collections::BTreeSet;
use identity::delegation::{
CapabilityScope, DelegationCertificate, DelegationParent, SignedDelegationCertificate,
delegation_signing_salt,
};
use identity::{IdentityProvider, InMemoryProvider};
use super::*;
#[test]
fn event_round_trips_with_matching_inner_and_outer_signers() {
let provider = InMemoryProvider::from_seed([1; 32]);
let subject = InMemoryProvider::from_seed([2; 32]);
let scope = CapabilityScope {
domain: "moot".into(),
resource: vec![9; 32],
path_prefix: "moot/fauna".into(),
actions: BTreeSet::from(["act".into()]),
};
let signed = SignedDelegationCertificate::issue(
&provider,
DelegationCertificate::new(
DelegationParent::Root([7; 32]),
provider.master_public_key().to_bytes(),
subject.master_public_key().to_bytes(),
scope.clone(),
1,
2,
Some(100),
0,
[3; 32],
),
)
.unwrap();
let event = MootDelegationEvent::Issued(signed);
let keypair = provider
.derive_keypair(&delegation_signing_salt(&scope))
.unwrap();
let operation = to_operation(&keypair, [9; 32], &event, 0, None);
assert!(verify(&operation));
assert_eq!(from_operation(&operation).unwrap(), event);
assert_eq!(event.signer(), Some(keypair.public_key().to_bytes()));
}
}