Skip to content

Commit

Permalink
feat: Update event types and builder APIs to prevent building events …
Browse files Browse the repository at this point in the history
…that dont align with the protocol spec (#357)

* feat: Enforce no unexpected fields in events

* chore: pass StreamID to Header factory

* chore: StreamId.to_vec cannot fail

* chore: Add comment clarifying that SignedEvent is only for the rust js-ceramic client

* Revert "chore: Add comment clarifying that SignedEvent is only for the rust js-ceramic client"

This reverts commit cb53168.

* chore: Rename signed::Payload to signed::Envelope for clarity

* wip

* WIP builder API

* feat: event builder passes tests, type state pattern

* chore: make model required in init event header

* add data event builder

* chore: rename Event to RawEvent

* Add function to sign events

* fix it

* make private

* add car file encoding

* cache cids

* wip: remove jws and related types

* comments and small cleanups

* remove deny_unknown_fields directive (for now)

* comment out unused constants

* fix clippy

* fix test

* rm unused deps

* rm unusued value type

---------

Co-authored-by: Nathaniel Cook <nvcook42@gmail.com>
  • Loading branch information
stbrody and nathanielc authored May 17, 2024
1 parent f433c64 commit 50cc8ba
Show file tree
Hide file tree
Showing 24 changed files with 561 additions and 1,065 deletions.
6 changes: 4 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ async-trait = "0.1"
asynchronous-codec = "0.7"
axum = "0.6"
backoff = "0.4"
base64 = "0.20.0"
base64 = "0.21"
bincode = "1.3.3"
bs58 = "0.4"
bytecheck = "0.6.7"
Expand Down
20 changes: 10 additions & 10 deletions api/src/server/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::HashMap;

use anyhow::{anyhow, bail, Context, Result};
use ceramic_core::{Cid, EventId, Network};
use ceramic_event::{unvalidated, unvalidated::CeramicExt};
use ceramic_event::unvalidated;
use ipld_core::ipld::Ipld;
use iroh_car::CarReader;
use tokio::io::AsyncRead;
Expand Down Expand Up @@ -31,15 +31,15 @@ where
car_blocks.insert(cid, bytes);
}
let event_bytes = get_block(&event_cid, &car_blocks, store).await?;
let event: unvalidated::Event<Ipld> =
let event: unvalidated::RawEvent<Ipld> =
serde_ipld_dagcbor::from_slice(&event_bytes).context("decoding event")?;

let (init_id, init_payload) = match event {
unvalidated::Event::Time(event) => (
unvalidated::RawEvent::Time(event) => (
event.id(),
get_init_event_payload(&event.id(), &car_blocks, store).await?,
),
unvalidated::Event::Signed(event) => {
unvalidated::RawEvent::Signed(event) => {
let link = event
.link()
.ok_or_else(|| anyhow!("event should have a link"))?;
Expand All @@ -56,7 +56,7 @@ where
get_init_event_payload(&init_id, &car_blocks, store).await?,
)
}
unvalidated::Event::Unsigned(event) => (event_cid, event),
unvalidated::RawEvent::Unsigned(event) => (event_cid, event),
};

let controller = init_payload
Expand All @@ -67,7 +67,7 @@ where
Ok(EventId::new(
&network,
init_payload.header().sep(),
init_payload.model()?.as_slice(),
init_payload.header().model(),
controller,
&init_id,
&event_cid,
Expand All @@ -80,10 +80,10 @@ async fn get_init_event_payload(
store: &impl AccessModelStore,
) -> Result<unvalidated::init::Payload<Ipld>> {
let init_bytes = get_block(init_id, car_blocks, store).await?;
let init_event: unvalidated::Event<Ipld> =
let init_event: unvalidated::RawEvent<Ipld> =
serde_ipld_dagcbor::from_slice(&init_bytes).context("decoding init event")?;
match init_event {
unvalidated::Event::Signed(event) => {
unvalidated::RawEvent::Signed(event) => {
let link = event
.link()
.ok_or_else(|| anyhow!("init event should have a link"))?;
Expand All @@ -97,8 +97,8 @@ async fn get_init_event_payload(
bail!("init event payload is not well formed")
}
}
unvalidated::Event::Unsigned(event) => Ok(event),
unvalidated::Event::Time(_) => {
unvalidated::RawEvent::Unsigned(event) => Ok(event),
unvalidated::RawEvent::Time(_) => {
bail!("init event payload can't be a time event")
}
}
Expand Down
5 changes: 1 addition & 4 deletions api/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,7 @@ pub const SIGNED_INIT_EVENT: &str = "
wVnRoYml5U2NtZnU4bjVWN2JvWGd4eW81cTNTWlJSI3o2TWt0QnluQVBMckV5ZVM3cFZ0aGJpeVNjbWZ
1OG41Vjdib1hneHlvNXEzU1pSUiJ9aXNpZ25hdHVyZVhAJAOOXHx9PyttNHjTzwe04TbsskxzfwNK5_X
9e0rXiEOnVDq-Eeqe6KhuqnkSo06nS80UILPCAb4OyOZ6z2MnDA";
pub const SIGNED_INIT_EVENT_PAYLOAD: &str = "
uomRkYXRhoWVzdGVwaBkBTWZoZWFkZXKkY3NlcGVtb2RlbGVtb2RlbFgozgECAYUBEiCg6DKjNeOL00C
0Og8Cmb88UVoPwz3kzXkQpD6Lkx6NWGZ1bmlxdWVMRKbxOrJBC7tqhWjea2NvbnRyb2xsZXJzgXg4ZGl
kOmtleTp6Nk1rdEJ5bkFQTHJFeWVTN3BWdGhiaXlTY21mdThuNVY3Ym9YZ3h5bzVxM1NaUlI";
pub const SIGNED_INIT_EVENT_PAYLOAD: &str = "uomRkYXRhoWVzdGVwaBkBTWZoZWFkZXKkY3NlcGVtb2RlbGVtb2RlbFgozgECAYUBEiCg6DKjNeOL00C0Og8Cmb88UVoPwz3kzXkQpD6Lkx6NWGZ1bmlxdWVMRKbxOrJBC7tqhWjea2NvbnRyb2xsZXJzgXg4ZGlkOmtleTp6Nk1rdEJ5bkFQTHJFeWVTN3BWdGhiaXlTY21mdThuNVY3Ym9YZ3h5bzVxM1NaUlI";

pub const UNSIGNED_INIT_EVENT_CID: &str =
"bafyreiakimdaub7m6inx2nljypdhvhu5vozjhylqukif4hjxt65qnkv6my";
Expand Down
1 change: 0 additions & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ publish = false

[dependencies]
anyhow.workspace = true
async-trait.workspace = true
base64 = "0.21.2"
cid.workspace = true
did-method-key = "0.2.1"
Expand Down
172 changes: 0 additions & 172 deletions core/src/jws.rs

This file was deleted.

6 changes: 1 addition & 5 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,16 @@ mod bytes;
pub mod event_id;
pub mod interest;
mod jwk;
mod jws;
mod network;
mod range;
mod signer;
mod stream_id;

pub use bytes::Bytes;
pub use event_id::EventId;
pub use interest::{Interest, PeerId};
pub use jwk::Jwk;
pub use jws::{Jws, JwsSignature};
pub use network::Network;
pub use range::RangeOpen;
pub use signer::{JwkSigner, Signer};
pub use stream_id::{StreamId, StreamIdType};

pub use cid::Cid;
Expand Down Expand Up @@ -49,7 +45,7 @@ macro_rules! impl_multi_base {
type Error = anyhow::Error;

fn try_from(v: &StreamId) -> Result<Self, Self::Error> {
let v = v.to_vec()?;
let v = v.to_vec();
Ok(Self::from(v))
}
}
Expand Down
62 changes: 0 additions & 62 deletions core/src/signer.rs

This file was deleted.

Loading

0 comments on commit 50cc8ba

Please sign in to comment.