aboutsummaryrefslogtreecommitdiffstats
path: root/src/federation
diff options
context:
space:
mode:
Diffstat (limited to 'src/federation')
-rw-r--r--src/federation/event.rs79
-rw-r--r--src/federation/harness.rs56
2 files changed, 87 insertions, 48 deletions
diff --git a/src/federation/event.rs b/src/federation/event.rs
index fc519d0..00c7018 100644
--- a/src/federation/event.rs
+++ b/src/federation/event.rs
@@ -13,6 +13,85 @@ pub(crate) const LARGEST: usize = 256 * 1024;
/// Dependencies an event may name.
pub(crate) const DEPENDENCIES: usize = 64;
+/// An event `party` signs with `signer`, its key spelled `key`, on `deps`, doing `does`
+/// (a field's name, and what it holds): its bytes, as JCS.
+pub(crate) fn signed(
+ signer: &ed25519_dalek::SigningKey,
+ party: &str,
+ key: &str,
+ deps: &[Id],
+ does: (&str, Json),
+) -> Vec<u8> {
+ let mut members = vec![
+ ("deps".to_owned(), ids(deps)),
+ ("party".to_owned(), Json::Str(party.to_owned())),
+ ("key".to_owned(), Json::Str(key.to_owned())),
+ (does.0.to_owned(), does.1),
+ ];
+ let unsigned = Json::object(members.clone()).expect("names once each");
+ members.push((
+ "sig".to_owned(),
+ Json::Str(keys::sign(signer, &unsigned.bytes())),
+ ));
+ Json::object(members).expect("names once each").bytes()
+}
+
+/// What writing `value` at `path` does.
+pub(crate) fn write(path: &str, value: Json) -> (&'static str, Json) {
+ let does = Json::object(vec![
+ ("path".to_owned(), Json::Str(path.to_owned())),
+ ("value".to_owned(), value),
+ ]);
+ ("write", does.expect("names once each"))
+}
+
+/// What writing a party's key set does.
+pub(crate) fn keys(keys: &[String]) -> (&'static str, Json) {
+ ("keys", texts(keys))
+}
+
+/// What writing a party's node list does.
+pub(crate) fn nodes(nodes: &[String]) -> (&'static str, Json) {
+ ("nodes", texts(nodes))
+}
+
+/// What proposing `version` does.
+pub(crate) fn proposal(version: &Json) -> (&'static str, Json) {
+ ("proposal", version.clone())
+}
+
+/// What voting `yes` on `proposal`, on the version `on`, does.
+pub(crate) fn vote(on: Id, proposal: Id, yes: bool) -> (&'static str, Json) {
+ let does = Json::object(vec![
+ ("on".to_owned(), Json::Str(on.to_string())),
+ ("proposal".to_owned(), Json::Str(proposal.to_string())),
+ ("yes".to_owned(), Json::Bool(yes)),
+ ]);
+ ("vote", does.expect("names once each"))
+}
+
+/// A version event, which nobody signs: its bytes.
+pub(crate) fn version_event(deps: &[Id], version: Id) -> Vec<u8> {
+ Json::object(vec![
+ ("deps".to_owned(), ids(deps)),
+ ("version".to_owned(), Json::Str(version.to_string())),
+ ])
+ .expect("names once each")
+ .bytes()
+}
+
+/// IDs as an event names them: ascending, each once.
+fn ids(ids: &[Id]) -> Json {
+ let mut ids = ids.to_vec();
+ ids.sort();
+ ids.dedup();
+ Json::Array(ids.iter().map(|id| Json::Str(id.to_string())).collect())
+}
+
+fn texts(texts: &[String]) -> Json {
+ Json::Array(texts.iter().map(|text| Json::Str(text.clone())).collect())
+}
+
/// An event, read from its bytes.
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) enum Event {
diff --git a/src/federation/harness.rs b/src/federation/harness.rs
index 6d1b214..80328dd 100644
--- a/src/federation/harness.rs
+++ b/src/federation/harness.rs
@@ -3,8 +3,8 @@
use ed25519_dalek::SigningKey;
+use super::event;
use super::jcs::Json;
-use super::keys;
use super::store::Id;
use crate::access::sshsig::PublicKey;
@@ -78,18 +78,7 @@ impl Party {
deps: &[Id],
does: (&str, Json),
) -> Vec<u8> {
- let unsigned = object(vec![
- ("deps", ids(deps)),
- ("party", str(&self.name)),
- ("key", str(key)),
- (does.0, does.1),
- ]);
- let sig = keys::sign(signer, &unsigned.bytes());
- let Json::Object(mut members) = unsigned else {
- unreachable!("an object")
- };
- members.push(("sig".to_owned(), Json::Str(sig)));
- Json::object(members).expect("names once each").bytes()
+ event::signed(signer, &self.name, key, deps, does)
}
pub(crate) fn sign(&self, deps: &[Id], does: (&str, Json)) -> Vec<u8> {
@@ -97,58 +86,29 @@ impl Party {
}
pub(crate) fn write(&self, deps: &[Id], path: &str, value: Json) -> Vec<u8> {
- self.sign(
- deps,
- ("write", object(vec![("path", str(path)), ("value", value)])),
- )
+ self.sign(deps, event::write(path, value))
}
pub(crate) fn keys(&self, deps: &[Id], keys: &[String]) -> Vec<u8> {
- self.sign(
- deps,
- (
- "keys",
- Json::Array(keys.iter().map(|key| str(key)).collect()),
- ),
- )
+ self.sign(deps, event::keys(keys))
}
pub(crate) fn nodes(&self, deps: &[Id], nodes: &[String]) -> Vec<u8> {
- self.sign(
- deps,
- (
- "nodes",
- Json::Array(nodes.iter().map(|id| str(id)).collect()),
- ),
- )
+ self.sign(deps, event::nodes(nodes))
}
pub(crate) fn propose(&self, deps: &[Id], version: &Json) -> Vec<u8> {
- self.sign(deps, ("proposal", version.clone()))
+ self.sign(deps, event::proposal(version))
}
pub(crate) fn vote(&self, deps: &[Id], on: Id, proposal: Id, yes: bool) -> Vec<u8> {
- self.sign(
- deps,
- (
- "vote",
- object(vec![
- ("on", str(&on.to_string())),
- ("proposal", str(&proposal.to_string())),
- ("yes", Json::Bool(yes)),
- ]),
- ),
- )
+ self.sign(deps, event::vote(on, proposal, yes))
}
}
/// A version event, which nobody signs.
pub(crate) fn version_event(deps: &[Id], version: Id) -> Vec<u8> {
- object(vec![
- ("deps", ids(deps)),
- ("version", str(&version.to_string())),
- ])
- .bytes()
+ event::version_event(deps, version)
}
/// What a version gives a party, as a test spells it out.