Skip to content

Commit

Permalink
feat: add payload option to return signed payload without signature…
Browse files Browse the repository at this point in the history
… information
  • Loading branch information
dav1do committed Jun 27, 2024
1 parent 673719f commit 3b1718d
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 6 deletions.
2 changes: 1 addition & 1 deletion api-server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ To see how to make this your own, look here:
[README]((https://openapi-generator.tech))

- API version: 0.25.0
- Build date: 2024-06-26T12:41:21.980959-06:00[America/Denver]
- Build date: 2024-06-27T10:50:10.100652-06:00[America/Denver]



Expand Down
4 changes: 3 additions & 1 deletion api-server/api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -325,14 +325,16 @@ paths:
- description: |
Whether to include the event data (carfile) in the response. In the future, only the payload or other options may be supported:
* `none` - Empty, only the event ID is returned
* `full` - The entire envelope carfile (including the envelope and payload)
* `payload` - Only the signed event payload data as a carfile. Unsigned and time events are unchanged.
* `full` - The entire envelope carfile (including the envelope, capability and payload when applicable)
explode: true
in: query
name: includeData
required: false
schema:
enum:
- none
- payload
- full
type: string
style: form
Expand Down
2 changes: 1 addition & 1 deletion api-server/docs/default_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------
**resume_at** | **String**| token that designates the point to resume from, that is find keys added after this point |
**limit** | **i32**| The maximum number of events to return, default is 100. The max with data is 10000. |
**include_data** | **String**| Whether to include the event data (carfile) in the response. In the future, only the payload or other options may be supported: * `none` - Empty, only the event ID is returned * `full` - The entire envelope carfile (including the envelope and payload) |
**include_data** | **String**| Whether to include the event data (carfile) in the response. In the future, only the payload or other options may be supported: * `none` - Empty, only the event ID is returned * `payload` - Only the signed event payload data as a carfile. Unsigned and time events are unchanged. * `full` - The entire envelope carfile (including the envelope, capability and payload when applicable) |

### Return type

Expand Down
5 changes: 3 additions & 2 deletions api/ceramic.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -302,12 +302,13 @@ paths:
required: false
schema:
type: string
enum: [none, full]
enum: [none, payload, full]
description: >
Whether to include the event data (carfile) in the response.
In the future, only the payload or other options may be supported:
* `none` - Empty, only the event ID is returned
* `full` - The entire envelope carfile (including the envelope and payload)
* `payload` - Only the signed event payload data as a carfile. Unsigned and time events are unchanged.
* `full` - The entire envelope carfile (including the envelope, capability and payload when applicable)
responses:
"200":
description: success
Expand Down
4 changes: 3 additions & 1 deletion api/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ impl EventDataResult {
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum IncludeEventData {
None,
Payload,
Full,
}

Expand All @@ -216,6 +217,7 @@ impl TryFrom<String> for IncludeEventData {
fn try_from(value: String) -> Result<Self, Self::Error> {
match value.as_str() {
"none" => Ok(Self::None),
"payload" => Ok(Self::Payload),
"full" => Ok(Self::Full),
_ => Err(format!("Invalid value: {}.", value)),
}
Expand Down Expand Up @@ -441,7 +443,7 @@ where
Err(e) => {
return Ok(FeedEventsGetResponse::BadRequest(
models::BadRequestResponse::new(format!(
"{} must be one of 'none' or 'full'",
"{} must be one of 'none', 'payload', or 'full'",
e
)),
))
Expand Down
12 changes: 12 additions & 0 deletions event/src/unvalidated/signed/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,18 @@ impl<D: serde::Serialize> Event<D> {
Ok(car)
}

/// Encodes the signed event payload into a CAR file without the envelope or capability.
pub async fn encode_payload_car(&self) -> anyhow::Result<Vec<u8>> {
let payload_bytes = self.encode_payload()?;
let mut car = Vec::new();
let roots: Vec<Cid> = vec![self.payload_cid];
let mut writer = CarWriter::new(CarHeader::V1(roots.into()), &mut car);
writer.write(self.payload_cid, payload_bytes).await?;
writer.finish().await?;

Ok(car)
}

/// Accessor for the envelope and payload.
pub fn into_parts(self) -> (Envelope, Payload<D>) {
(self.envelope, self.payload)
Expand Down
17 changes: 17 additions & 0 deletions service/src/event/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,23 @@ impl ceramic_api::EventStore for CeramicEventService {
.collect();
(hw, res)
}
ceramic_api::IncludeEventData::Payload => {
let (hw, data) =
CeramicOneEvent::new_events_since_value_with_data(&self.pool, highwater, limit)
.await?;
let mut res = Vec::with_capacity(data.len());
for (cid, event) in data {
let encoded = match event {
ceramic_event::unvalidated::Event::Time(t) => t.encode_car().await?,
ceramic_event::unvalidated::Event::Signed(s) => {
s.encode_payload_car().await?
}
ceramic_event::unvalidated::Event::Unsigned(u) => u.encode_car().await?,
};
res.push(ceramic_api::EventDataResult::new(cid, Some(encoded)));
}
(hw, res)
}
ceramic_api::IncludeEventData::Full => {
let (hw, data) =
CeramicOneEvent::new_events_since_value_with_data(&self.pool, highwater, limit)
Expand Down

0 comments on commit 3b1718d

Please sign in to comment.