From fdaae6cb263ac96097148e6b686812ae4593eb9d Mon Sep 17 00:00:00 2001 From: Brooklyn Zelenka Date: Wed, 31 Jan 2024 14:46:49 -0800 Subject: [PATCH] I think mainly done moving modules around --- src/ability.rs | 1 + src/ability/arguments.rs | 16 ++++ src/ability/dynamic.rs | 4 +- src/ability/msg/send.rs | 4 +- src/ability/ucan.rs | 82 +++++++++++++++++++ src/condition.rs | 1 - src/condition/common.rs | 1 - src/{delegation.rs => delegate.rs} | 13 +-- src/{delegation => delegate}/condition.rs | 0 .../condition/contains_all.rs | 0 .../condition/contains_any.rs | 0 .../condition/contains_key.rs | 0 .../condition/excludes_all.rs | 0 .../condition/excludes_key.rs | 0 .../condition/matches_regex.rs | 0 .../condition/max_length.rs | 0 .../condition/max_number.rs | 0 .../condition/min_length.rs | 0 .../condition/min_number.rs | 0 .../condition/traits.rs | 0 src/{delegation => delegate}/delegatable.rs | 0 src/{delegation => delegate}/payload.rs | 9 +- src/{delegation => delegate}/traits.rs | 0 src/delegation/delegate.rs | 19 ----- src/invocation/serializer.rs | 1 - src/{invocation.rs => invoke.rs} | 7 +- src/{invocation => invoke}/payload.rs | 0 src/{invocation => invoke}/resolvable.rs | 0 src/invoke/serializer.rs | 0 src/ipld.rs | 10 +-- src/lib.rs | 6 +- src/promise.rs | 2 + src/receipt.rs | 51 ------------ src/respond.rs | 9 ++ src/{receipt => respond}/payload.rs | 12 +-- .../runnable.rs => respond/responds.rs} | 2 +- src/signature.rs | 56 ++++++++++++- 37 files changed, 200 insertions(+), 106 deletions(-) create mode 100644 src/ability/ucan.rs delete mode 100644 src/condition.rs delete mode 100644 src/condition/common.rs rename src/{delegation.rs => delegate.rs} (73%) rename src/{delegation => delegate}/condition.rs (100%) rename src/{delegation => delegate}/condition/contains_all.rs (100%) rename src/{delegation => delegate}/condition/contains_any.rs (100%) rename src/{delegation => delegate}/condition/contains_key.rs (100%) rename src/{delegation => delegate}/condition/excludes_all.rs (100%) rename src/{delegation => delegate}/condition/excludes_key.rs (100%) rename src/{delegation => delegate}/condition/matches_regex.rs (100%) rename src/{delegation => delegate}/condition/max_length.rs (100%) rename src/{delegation => delegate}/condition/max_number.rs (100%) rename src/{delegation => delegate}/condition/min_length.rs (100%) rename src/{delegation => delegate}/condition/min_number.rs (100%) rename src/{delegation => delegate}/condition/traits.rs (100%) rename src/{delegation => delegate}/delegatable.rs (100%) rename src/{delegation => delegate}/payload.rs (97%) rename src/{delegation => delegate}/traits.rs (100%) delete mode 100644 src/delegation/delegate.rs delete mode 100644 src/invocation/serializer.rs rename src/{invocation.rs => invoke.rs} (54%) rename src/{invocation => invoke}/payload.rs (100%) rename src/{invocation => invoke}/resolvable.rs (100%) create mode 100644 src/invoke/serializer.rs delete mode 100644 src/receipt.rs create mode 100644 src/respond.rs rename src/{receipt => respond}/payload.rs (79%) rename src/{receipt/runnable.rs => respond/responds.rs} (94%) diff --git a/src/ability.rs b/src/ability.rs index 788870b2..48abc10d 100644 --- a/src/ability.rs +++ b/src/ability.rs @@ -1,6 +1,7 @@ // FIXME feature flag each? pub mod crud; pub mod msg; +pub mod ucan; pub mod wasm; pub mod arguments; diff --git a/src/ability/arguments.rs b/src/ability/arguments.rs index 67b86d54..67d6baf6 100644 --- a/src/ability/arguments.rs +++ b/src/ability/arguments.rs @@ -11,6 +11,22 @@ impl Arguments { } } +impl Arguments { + pub fn insert(&mut self, key: String, value: Ipld) -> Option { + self.0.insert(key, value) + } + + pub fn get(&self, key: &str) -> Option<&Ipld> { + self.0.get(key) + } +} + +impl Default for Arguments { + fn default() -> Self { + Arguments(BTreeMap::new()) + } +} + impl TryFrom for Arguments { type Error = SerdeError; diff --git a/src/ability/dynamic.rs b/src/ability/dynamic.rs index cb4e5129..197cf8f2 100644 --- a/src/ability/dynamic.rs +++ b/src/ability/dynamic.rs @@ -1,9 +1,7 @@ //! This module is for dynamic abilities, especially for FFI and Wasm support use super::{arguments::Arguments, command::ToCommand}; -use crate::{ - delegation::delegatable::Delegatable, invocation::resolvable::Resolvable, promise::Promise, -}; +use crate::{delegate::Delegatable, invoke::Resolvable, promise::Promise}; use serde_derive::{Deserialize, Serialize}; use std::fmt::Debug; diff --git a/src/ability/msg/send.rs b/src/ability/msg/send.rs index 2cd3293d..f0cc21d3 100644 --- a/src/ability/msg/send.rs +++ b/src/ability/msg/send.rs @@ -1,7 +1,7 @@ use crate::{ ability::{arguments::Arguments, command::Command}, - delegation::delegatable::Delegatable, - invocation::resolvable::Resolvable, + delegate::Delegatable, + invoke::Resolvable, promise::Promise, proof::{checkable::Checkable, parentful::Parentful, parents::CheckParents, same::CheckSame}, }; diff --git a/src/ability/ucan.rs b/src/ability/ucan.rs new file mode 100644 index 00000000..ba8b19b6 --- /dev/null +++ b/src/ability/ucan.rs @@ -0,0 +1,82 @@ +use super::arguments::Arguments; +use crate::{ability::command::Command, delegate::Delegatable, promise::Promise}; +use libipld_core::ipld::Ipld; +use serde::{Deserialize, Serialize}; +use std::fmt::Debug; + +// NOTE This one is primarily for enabling delegated recipets + +// FIXME +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +struct Generic { + pub cmd: String, + pub args: Args, // FIXME Does this have specific fields? +} + +pub type Resolved = Generic; +pub type Builder = Generic>; +pub type Promised = Generic>; + +impl Command for Generic { + const COMMAND: &'static str = "ucan/proxy"; +} + +impl Delegatable for Resolved { + type Builder = Builder; +} + +impl From for Builder { + fn from(resolved: Resolved) -> Builder { + Builder { + cmd: resolved.cmd, + args: Some(resolved.args), + } + } +} + +impl TryFrom for Resolved { + type Error = (); // FIXME + + fn try_from(b: Builder) -> Result { + Ok(Resolved { + cmd: b.cmd, + args: b.args.ok_or(())?, + }) + } +} + +impl From for Arguments { + fn from(b: Builder) -> Arguments { + let mut args = b.args.unwrap_or_default(); + args.insert("cmd".into(), Ipld::String(b.cmd)); + args + } +} + +// // FIXME hmmm +// #[derive(Debug, Clone, PartialEq)] +// pub struct ProxyExecuteBuilder { +// pub command: Option, +// pub args: BTreeMap, +// } +// +// +// impl From for ProxyExecuteBuilder { +// fn from(proxy: ProxyExecute) -> Self { +// ProxyExecuteBuilder { +// command: Some(ProxyExecute::COMMAND.into()), +// args: proxy.args.clone(), +// } +// } +// } +// +// impl TryFrom for ProxyExecute { +// type Error = (); // FIXME +// +// fn try_from(ProxyExecuteBuilder { command, args }: ProxyExecuteBuilder) -> Result { +// match command { +// None => Err(()), +// Some(command) => Ok(Self { command, args }), +// } +// } +// } diff --git a/src/condition.rs b/src/condition.rs deleted file mode 100644 index 8b137891..00000000 --- a/src/condition.rs +++ /dev/null @@ -1 +0,0 @@ - diff --git a/src/condition/common.rs b/src/condition/common.rs deleted file mode 100644 index 8b137891..00000000 --- a/src/condition/common.rs +++ /dev/null @@ -1 +0,0 @@ - diff --git a/src/delegation.rs b/src/delegate.rs similarity index 73% rename from src/delegation.rs rename to src/delegate.rs index 431531d1..86ebbedd 100644 --- a/src/delegation.rs +++ b/src/delegate.rs @@ -1,14 +1,15 @@ // FIXME rename delegate? -pub mod condition; -pub mod delegatable; -pub mod delegate; -pub mod payload; +mod condition; +mod delegatable; +mod payload; -use crate::signature; -pub use delegate::Delegate; +pub use condition::traits::Condition; +pub use delegatable::Delegatable; pub use payload::Payload; +use crate::signature; + /// A [`Delegation`] is a signed delegation [`Payload`] /// /// A [`Payload`] on its own is not a valid [`Delegation`], as it must be signed by the issuer. diff --git a/src/delegation/condition.rs b/src/delegate/condition.rs similarity index 100% rename from src/delegation/condition.rs rename to src/delegate/condition.rs diff --git a/src/delegation/condition/contains_all.rs b/src/delegate/condition/contains_all.rs similarity index 100% rename from src/delegation/condition/contains_all.rs rename to src/delegate/condition/contains_all.rs diff --git a/src/delegation/condition/contains_any.rs b/src/delegate/condition/contains_any.rs similarity index 100% rename from src/delegation/condition/contains_any.rs rename to src/delegate/condition/contains_any.rs diff --git a/src/delegation/condition/contains_key.rs b/src/delegate/condition/contains_key.rs similarity index 100% rename from src/delegation/condition/contains_key.rs rename to src/delegate/condition/contains_key.rs diff --git a/src/delegation/condition/excludes_all.rs b/src/delegate/condition/excludes_all.rs similarity index 100% rename from src/delegation/condition/excludes_all.rs rename to src/delegate/condition/excludes_all.rs diff --git a/src/delegation/condition/excludes_key.rs b/src/delegate/condition/excludes_key.rs similarity index 100% rename from src/delegation/condition/excludes_key.rs rename to src/delegate/condition/excludes_key.rs diff --git a/src/delegation/condition/matches_regex.rs b/src/delegate/condition/matches_regex.rs similarity index 100% rename from src/delegation/condition/matches_regex.rs rename to src/delegate/condition/matches_regex.rs diff --git a/src/delegation/condition/max_length.rs b/src/delegate/condition/max_length.rs similarity index 100% rename from src/delegation/condition/max_length.rs rename to src/delegate/condition/max_length.rs diff --git a/src/delegation/condition/max_number.rs b/src/delegate/condition/max_number.rs similarity index 100% rename from src/delegation/condition/max_number.rs rename to src/delegate/condition/max_number.rs diff --git a/src/delegation/condition/min_length.rs b/src/delegate/condition/min_length.rs similarity index 100% rename from src/delegation/condition/min_length.rs rename to src/delegate/condition/min_length.rs diff --git a/src/delegation/condition/min_number.rs b/src/delegate/condition/min_number.rs similarity index 100% rename from src/delegation/condition/min_number.rs rename to src/delegate/condition/min_number.rs diff --git a/src/delegation/condition/traits.rs b/src/delegate/condition/traits.rs similarity index 100% rename from src/delegation/condition/traits.rs rename to src/delegate/condition/traits.rs diff --git a/src/delegation/delegatable.rs b/src/delegate/delegatable.rs similarity index 100% rename from src/delegation/delegatable.rs rename to src/delegate/delegatable.rs diff --git a/src/delegation/payload.rs b/src/delegate/payload.rs similarity index 97% rename from src/delegation/payload.rs rename to src/delegate/payload.rs index 527246f8..f6997ec6 100644 --- a/src/delegation/payload.rs +++ b/src/delegate/payload.rs @@ -3,7 +3,8 @@ use crate::{ ability::{arguments::Arguments, command::Command, dynamic}, capsule::Capsule, did::Did, - invocation::{payload as invocation, resolvable::Resolvable}, + invoke, + invoke::Resolvable, nonce::Nonce, proof::{ checkable::Checkable, @@ -89,14 +90,14 @@ impl From> for Ipld { impl<'a, T: Delegatable + Resolvable + Checkable + Clone, C: Condition> Payload { pub fn check( - invoked: &'a invocation::Payload, // FIXME promisory version + invoked: &'a invoke::Payload, // FIXME promisory version proofs: Vec>, now: SystemTime, ) -> Result<(), ()> where - invocation::Payload: Clone, + invoke::Payload: Clone, U::Builder: Clone + Into, - T::Hierarchy: From>, + T::Hierarchy: From>, { let start: Acc<'a, T> = Acc { issuer: &invoked.issuer, diff --git a/src/delegation/traits.rs b/src/delegate/traits.rs similarity index 100% rename from src/delegation/traits.rs rename to src/delegate/traits.rs diff --git a/src/delegation/delegate.rs b/src/delegation/delegate.rs deleted file mode 100644 index 0046484a..00000000 --- a/src/delegation/delegate.rs +++ /dev/null @@ -1,19 +0,0 @@ -use libipld_core::{ipld::Ipld, serde as ipld_serde}; -use serde_derive::{Deserialize, Serialize}; -use std::fmt::Debug; - -#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] -#[serde(untagged)] -pub enum Delegate { - #[serde(rename = "ucan/*")] - Any, - Specific(T), -} - -impl + serde::de::DeserializeOwned> TryFrom for Delegate { - type Error = (); // FIXME - - fn try_from(ipld: Ipld) -> Result { - ipld_serde::from_ipld(ipld).map_err(|_| ()) - } -} diff --git a/src/invocation/serializer.rs b/src/invocation/serializer.rs deleted file mode 100644 index 8b137891..00000000 --- a/src/invocation/serializer.rs +++ /dev/null @@ -1 +0,0 @@ - diff --git a/src/invocation.rs b/src/invoke.rs similarity index 54% rename from src/invocation.rs rename to src/invoke.rs index bb914f29..83575db1 100644 --- a/src/invocation.rs +++ b/src/invoke.rs @@ -1,7 +1,10 @@ -pub mod payload; -pub mod resolvable; +mod payload; +mod resolvable; mod serializer; +pub use payload::Payload; +pub use resolvable::Resolvable; + use crate::signature; pub type Invocation = signature::Envelope>; diff --git a/src/invocation/payload.rs b/src/invoke/payload.rs similarity index 100% rename from src/invocation/payload.rs rename to src/invoke/payload.rs diff --git a/src/invocation/resolvable.rs b/src/invoke/resolvable.rs similarity index 100% rename from src/invocation/resolvable.rs rename to src/invoke/resolvable.rs diff --git a/src/invoke/serializer.rs b/src/invoke/serializer.rs new file mode 100644 index 00000000..e69de29b diff --git a/src/ipld.rs b/src/ipld.rs index 64a8ea5a..a820acaf 100644 --- a/src/ipld.rs +++ b/src/ipld.rs @@ -3,23 +3,23 @@ use libipld_core::ipld::Ipld; #[cfg(target_arch = "wasm32")] use wasm_bindgen::JsValue; -pub struct WrappedIpld(pub Ipld); +pub struct Newtype(pub Ipld); -impl From for WrappedIpld { +impl From for Newtype { fn from(ipld: Ipld) -> Self { Self(ipld) } } -impl From for Ipld { - fn from(wrapped: WrappedIpld) -> Self { +impl From for Ipld { + fn from(wrapped: Newtype) -> Self { wrapped.0 } } // TODO testme #[cfg(target_arch = "wasm32")] -impl From for JsValue { +impl From for JsValue { fn from(wrapped: WrappedIpld) -> Self { match wrapped.0 { Ipld::Null => JsValue::Null, diff --git a/src/lib.rs b/src/lib.rs index 2eee9e05..2c922a09 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -114,14 +114,14 @@ use std::fmt::Debug; pub mod ability; pub mod capsule; -pub mod delegation; -pub mod invocation; +pub mod delegate; +pub mod invoke; pub mod ipld; pub mod nonce; pub mod number; pub mod promise; pub mod proof; -pub mod receipt; +pub mod respond; pub mod signature; pub mod task; diff --git a/src/promise.rs b/src/promise.rs index 54465191..f3189f1d 100644 --- a/src/promise.rs +++ b/src/promise.rs @@ -4,6 +4,8 @@ use libipld_core::{ipld::Ipld, serde as ipld_serde}; use serde_derive::{Deserialize, Serialize}; use std::fmt::Debug; +// FIXME move under invoke? + #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] #[serde(untagged)] pub enum Promise { diff --git a/src/receipt.rs b/src/receipt.rs deleted file mode 100644 index 5e70bd54..00000000 --- a/src/receipt.rs +++ /dev/null @@ -1,51 +0,0 @@ -use crate::signature; -use libipld_core::ipld::Ipld; -use payload::Payload; -use std::{collections::BTreeMap, fmt::Debug}; - -pub mod payload; -pub mod runnable; - -pub type Receipt = signature::Envelope>; - -// FIXME -#[derive(Debug, Clone, PartialEq)] -pub struct ProxyExecute { - pub cmd: String, - pub args: BTreeMap, -} - -// impl Delegatable for ProxyExecute { -// type Builder = ProxyExecuteBuilder; -// } -// -// // FIXME hmmm -// #[derive(Debug, Clone, PartialEq)] -// pub struct ProxyExecuteBuilder { -// pub command: Option, -// pub args: BTreeMap, -// } -// -// impl Command for ProxyExecute { -// const COMMAND: &'static str = "ucan/proxy"; -// } -// -// impl From for ProxyExecuteBuilder { -// fn from(proxy: ProxyExecute) -> Self { -// ProxyExecuteBuilder { -// command: Some(ProxyExecute::COMMAND.into()), -// args: proxy.args.clone(), -// } -// } -// } -// -// impl TryFrom for ProxyExecute { -// type Error = (); // FIXME -// -// fn try_from(ProxyExecuteBuilder { command, args }: ProxyExecuteBuilder) -> Result { -// match command { -// None => Err(()), -// Some(command) => Ok(Self { command, args }), -// } -// } -// } diff --git a/src/respond.rs b/src/respond.rs new file mode 100644 index 00000000..2cd81284 --- /dev/null +++ b/src/respond.rs @@ -0,0 +1,9 @@ +mod payload; +mod responds; + +pub use payload::Payload; +pub use responds::Responds; + +use crate::signature; + +pub type Receipt = signature::Envelope>; diff --git a/src/receipt/payload.rs b/src/respond/payload.rs similarity index 79% rename from src/receipt/payload.rs rename to src/respond/payload.rs index 25a7033b..b6374f26 100644 --- a/src/receipt/payload.rs +++ b/src/respond/payload.rs @@ -1,11 +1,11 @@ -use super::runnable::Runnable; +use super::responds::Responds; use crate::{capsule::Capsule, did::Did, nonce::Nonce, time::Timestamp}; use libipld_core::{cid::Cid, ipld::Ipld, serde as ipld_serde}; -use serde::{de::DeserializeOwned, Deserialize, Serialize, Serializer}; +use serde::{de::DeserializeOwned, Deserialize, Serialize}; use std::{collections::BTreeMap, fmt::Debug}; #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] -pub struct Payload +pub struct Payload where T::Success: Serialize + DeserializeOwned, { @@ -22,14 +22,14 @@ where pub issued_at: Option, } -impl Capsule for Payload +impl Capsule for Payload where for<'de> T::Success: Serialize + Deserialize<'de>, { const TAG: &'static str = "ucan/r/1.0.0-rc.1"; // FIXME extract out version } -impl TryFrom for Payload +impl TryFrom for Payload where for<'de> T::Success: Serialize + Deserialize<'de>, { @@ -40,7 +40,7 @@ where } } -impl From> for Ipld +impl From> for Ipld where for<'de> T::Success: Serialize + Deserialize<'de>, { diff --git a/src/receipt/runnable.rs b/src/respond/responds.rs similarity index 94% rename from src/receipt/runnable.rs rename to src/respond/responds.rs index 1fa9f00a..4bd6dfee 100644 --- a/src/receipt/runnable.rs +++ b/src/respond/responds.rs @@ -1,6 +1,6 @@ use crate::{did::Did, nonce::Nonce, task, task::Task}; -pub trait Runnable { +pub trait Responds { type Success; fn to_task(&self, subject: Did, nonce: Nonce) -> Task; diff --git a/src/signature.rs b/src/signature.rs index 12ceda4d..392a9f9a 100644 --- a/src/signature.rs +++ b/src/signature.rs @@ -1,5 +1,6 @@ use crate::capsule::Capsule; use libipld_core::ipld::Ipld; +use serde::{Deserialize, Serialize}; use std::collections::BTreeMap; #[derive(Debug, Clone, PartialEq)] @@ -9,7 +10,8 @@ pub struct Envelope { } // FIXME consider kicking Batch down the road for spec reasons? -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +#[serde(untagged)] pub enum Signature { One(Vec), Batch { @@ -18,6 +20,58 @@ pub enum Signature { }, } +// #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +// #[serde(transparent)] +// pub struct StaticVec { +// pub slice: Box<[T]>, +// } +// +// impl From> for StaticVec { +// fn from(vec: Vec) -> Self { +// Self { +// slice: vec.into_boxed_slice(), +// } +// } +// } +// +// impl From> for Vec { +// fn from(vec: StaticVec) -> Vec { +// vec.slice.into() +// } +// } +// +// #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +// #[serde(transparent)] +// pub struct StaticString { +// string: Box, +// } +// +// impl From for StaticString { +// fn from(string: String) -> Self { +// Self { +// string: string.into_boxed_str(), +// } +// } +// } +// +// impl<'a> From<&'a str> for StaticString { +// fn from(s: &'a str) -> Self { +// Self { string: s.into() } +// } +// } +// +// impl<'a> From<&'a StaticString> for &'a str { +// fn from(s: &'a StaticString) -> &'a str { +// &s.string +// } +// } +// +// impl From for String { +// fn from(s: StaticString) -> String { +// s.string.into() +// } +// } + impl From<&Signature> for Ipld { fn from(sig: &Signature) -> Self { match sig {