Skip to content

Commit

Permalink
Save point before riping out pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
expede committed Feb 2, 2024
1 parent 5ec4b9f commit d8ebca2
Show file tree
Hide file tree
Showing 16 changed files with 465 additions and 182 deletions.
22 changes: 16 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ async-signature = "0.4.0"
async-trait = "0.1.73"
blst = { version = "0.3.11", optional = true, default-features = false }
cfg-if = "0.1"
cid = "0.10"
# FIXME attempt to remove
cid = "0.11"
did_url = "0.1"
downcast-rs = "1.2.0"
dyn-clone = "1.0.14"
Expand All @@ -49,7 +50,6 @@ lazy_static = "1.4.0"
libipld-core = { version = "0.16", features = ["serde-codec"] }
libipld-cbor = "0.16"
multibase = "0.9.1"
multihash = {version = "0.19", features = ["serde"]}
p256 = { version = "0.13.2", features = ["ecdsa"], optional = true, default-features = false }
p384 = { version = "0.13.0", features = ["ecdsa"], optional = true, default-features = false }
p521 = { version = "0.13.0", optional = true, default-features = false }
Expand All @@ -66,6 +66,7 @@ thiserror = "1.0"
tracing = "0.1.40"
unsigned-varint = "0.7.2"
url = { version = "2.5", features = ["serde"] }
uuid = "1.7"
web-time = "0.2.3"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
Expand All @@ -74,12 +75,13 @@ linkme = "0.3.15"
uuid = { version = "1.7", features = ["v4"] }
xid = "1.0"

# FIXME also have a wasi target
[target.'cfg(target_arch = "wasm32")'.dependencies]
console_error_panic_hook = { version = "0.1" }
getrandom = { version = "*", features = ["js"] }
getrandom = { version = "0.2", features = ["js"] }
js-sys = { version = "0.3" }
serde-wasm-bindgen = "0.6.1"
wasm-bindgen = "0.2.87"
serde-wasm-bindgen = "0.6"
wasm-bindgen = "0.2"
wasm-bindgen-futures = { version = "0.4" }
web-sys = { version = "0.3", features = ["Crypto", "CryptoKey", "CryptoKeyPair", "SubtleCrypto"] }

Expand All @@ -96,7 +98,15 @@ proptest = { version = "*", default-features = false, features = ["std"] }
wasm-bindgen-test = "0.2"

[features]
default = ["did-key", "eddsa-verifier", "es256-verifier", "es256k-verifier", "es384-verifier", "ps256-verifier", "rs256-verifier"]
default = [
"did-key",
"eddsa-verifier",
"es256-verifier",
"es256k-verifier",
"es384-verifier",
"ps256-verifier",
"rs256-verifier",
]
test_utils = ["proptest"]
did-key = []
eddsa = ["dep:ed25519", "dep:ed25519-dalek"]
Expand Down
4 changes: 2 additions & 2 deletions src/ability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ pub mod wasm;
pub mod arguments;
pub mod command;

// TODO move to crate::wasm?
// #[cfg(feature = "wasm")]
// // TODO move to crate::wasm? or hide behind feature flag?
#[cfg(target_arch = "wasm32")]
pub mod dynamic;

// FIXME macro to derive promise versions & delagted builder versions
Expand Down
8 changes: 8 additions & 0 deletions src/ability/arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ impl Arguments {
pub fn from_iter(iterable: impl IntoIterator<Item = (String, Ipld)>) -> Self {
Arguments(iterable.into_iter().collect())
}

pub fn iter(&self) -> impl Iterator<Item = (&String, &Ipld)> {
self.0.iter()
}

pub fn into_iter(self) -> impl Iterator<Item = (String, Ipld)> {
self.0.into_iter()
}
}

impl Arguments {
Expand Down
4 changes: 2 additions & 2 deletions src/ability/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ pub trait Command {
const COMMAND: &'static str;
}

// NOTE do not export
pub(crate) trait ToCommand {
// NOTE do not export // FIXME move to internal?
pub(super) trait ToCommand {
fn to_command(&self) -> String;
}

Expand Down
4 changes: 2 additions & 2 deletions src/ability/crud/destroy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ impl CheckParents for Destroy {

fn check_parents(&self, other: &Self::Parents) -> Result<(), Self::ParentError> {
match other {
Mutable::Mutate(mutate) => Ok(()),
Mutable::Any(any) => Ok(()),
Mutable::Mutate(mutate) => Ok(()), // FIXME
Mutable::Any(any) => Ok(()), // FIXME
}
}
}
195 changes: 164 additions & 31 deletions src/ability/dynamic.rs
Original file line number Diff line number Diff line change
@@ -1,67 +1,200 @@
//! This module is for dynamic abilities, especially for FFI and Wasm support

use super::{arguments::Arguments, command::ToCommand};
use crate::{delegation::Delegatable, invocation::Resolvable, promise::Promise};
use serde_derive::{Deserialize, Serialize};
use std::fmt::Debug;

// FIXME move commented-out module?
// use js_sys;
// use wasm_bindgen::prelude::*;
// type JsDynamic = Dynamic<&'a js_sys::Function>;
// type JsBuilder = Builder<&'a js_sys::Function>;
// type JsPromised = Promised<&'a js_sys::Function>;
// FIXME move these fiels to a wrapper struct in a different module
// #[serde(skip_serializing)]
// pub chain_validator: Pred,
// #[serde(skip_serializing)]
// pub shape_validator: Pred,
// #[serde(skip_serializing)]
// pub serialize_nonce: DefaultTrue,
use crate::{
delegation::Delegatable,
invocation::Resolvable,
promise::Promise,
proof::{
checkable::Checkable, parentful::Parentful, parentless::Parentless, parents::CheckParents,
same::CheckSame,
},
task::DefaultTrue,
};
use libipld_core::{error::SerdeError, ipld::Ipld, serde as ipld_serde};
use serde::{
de::DeserializeOwned, ser::SerializeMap, Deserialize, Deserializer, Serialize, Serializer,
};
use std::{collections::BTreeMap, fmt::Debug};
use wasm_bindgen::prelude::*;

// NOTE the lack of checking functions!
// This is meant to be embedded inside of structs that have e.g. FFI bindings to
// a validation function, such as a &js_sys::Function, Ruby magnus::function!, etc etc
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct Generic<Args> {
#[derive(Clone, PartialEq)]
pub struct Generic<Args, F> {
pub cmd: String,
pub args: Args,
pub is_nonce_meaningful: DefaultTrue,

pub same_validator: F,
pub parent_validator: F, // FIXME needs to be a different types, and fall back to Void
pub shape_validator: F, // FIXME needs to be a different type
}

impl<F> Debug for Generic<Arguments, F> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Generic")
.field("cmd", &self.cmd)
.field("args", &self.args)
.field("is_nonce_meaningful", &self.is_nonce_meaningful)
.finish()
}
}

pub type Dynamic<F> = Generic<Arguments, F>;
pub type Promised<F> = Generic<Promise<Arguments>, F>;

impl<Args: Serialize + Clone> Serialize for Generic<Args, ()>
where
Arguments: From<Args>,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut map = serializer.serialize_map(Some(2))?;
map.serialize_entry("cmd", &self.cmd)?;
map.serialize_entry("args", &Arguments::from(self.args.clone()))?;
map.end()
}
}

pub type Dynamic = Generic<Arguments>;
pub type Promised = Generic<Promise<Arguments>>;
impl<'de, Args: Deserialize<'de>> Deserialize<'de> for Generic<Args, ()> {
fn deserialize<D>(deserializer: D) -> Result<Generic<Args, ()>, D::Error>
where
D: Deserializer<'de>,
{
// FIXME
todo!()
// let btree = BTreeMap::deserialize(deserializer)?;
// Ok(Generic {
// cmd: btree.get("cmd")?.to_string(),
// args: btree.get("args")?.clone(),
// is_nonce_meaningful: DefaultTrue::default(),
//
// same_validator: (),
// parent_validator: (),
// shape_validator: (),
// })
}
}

impl<Args> ToCommand for Generic<Args> {
impl<Args, F> ToCommand for Generic<Args, F> {
fn to_command(&self) -> String {
self.cmd.clone()
}
}

impl Delegatable for Dynamic {
type Builder = Dynamic;
impl<F> Delegatable for Dynamic<F> {
type Builder = Dynamic<F>;
}

impl Resolvable for Dynamic {
type Promised = Dynamic;
impl<F> Resolvable for Dynamic<F> {
type Promised = Promised<F>;
}

impl From<Dynamic> for Arguments {
fn from(dynamic: Dynamic) -> Self {
dynamic.args
impl<Args: Serialize> From<Generic<Args, ()>> for Ipld {
fn from(generic: Generic<Args, ()>) -> Self {
generic.into()
}
}

impl<Args: DeserializeOwned> TryFrom<Ipld> for Generic<Args, ()> {
type Error = SerdeError;

fn try_from(ipld: Ipld) -> Result<Self, Self::Error> {
ipld_serde::from_ipld(ipld)
}
}

impl TryFrom<Promised> for Dynamic {
impl<Args: Into<Arguments>, F> From<Generic<Args, F>> for Arguments {
fn from(generic: Generic<Args, F>) -> Self {
generic.args.into()
}
}

impl<F> TryFrom<Promised<F>> for Dynamic<F> {
type Error = (); // FIXME

fn try_from(awaiting: Promised) -> Result<Self, ()> {
fn try_from(awaiting: Promised<F>) -> Result<Self, ()> {
if let Promise::Resolved(args) = &awaiting.args {
Ok(Dynamic {
cmd: awaiting.cmd,
args: args.clone(),

same_validator: awaiting.same_validator,
parent_validator: awaiting.parent_validator,
shape_validator: awaiting.shape_validator,
is_nonce_meaningful: awaiting.is_nonce_meaningful,
})
} else {
Err(())
}
}
}

impl<F> From<Dynamic<F>> for Promised<F> {
fn from(d: Dynamic<F>) -> Self {
Promised {
cmd: d.cmd,
args: Promise::Resolved(d.args),

same_validator: d.same_validator,
parent_validator: d.parent_validator,
shape_validator: d.shape_validator,
is_nonce_meaningful: d.is_nonce_meaningful,
}
}
}

impl<F> Checkable for Dynamic<F>
where
F: Fn(&String, &Arguments) -> Result<(), String>,
{
type Hierarchy = Parentless<Dynamic<F>>; // FIXME I bet we can revover parents
}

// FIXME Actually, we shoudl go back to wrapping?
// impl<F> CheckSame for Dynamic<F>
// where
// F: Fn(&String, &Arguments) -> Result<(), String>,
// {
// type Error = String;
//
// fn check_same(&self, proof: &Self) -> Result<(), Self::Error> {
// let chain_checker = &self.same_validator;
// let shape_checker = &self.same_validator;
//
// shape_checker(&proof.cmd, &proof.args)?;
// chain_checker(&proof.cmd, &proof.args)
// }
// }

// #[wasm_bindgen(module = "./ability")]
// extern "C" {
// type JsAbility;
//
// // FIXME wrap in func that checks the jsval or better: converts form Ipld
// #[wasm_bindgen(constructor)]
// fn new(cmd: String, args: BTreeMap<String, JsValue>) -> JsAbility;
//
// #[wasm_bindgen(method, getter)]
// fn command(this: &JsAbility) -> String;
//
// #[wasm_bindgen(method, getter)]
// fn arguments(this: &JsAbility) -> Arguments;
//
// #[wasm_bindgen(method, getter)]
// fn is_nonce_meaningful(this: &JsAbility) -> bool;
//
// // e.g. reject extra fields
// #[wasm_bindgen(method)]
// fn validate_shape(this: &JsAbility) -> bool;
//
// // FIXME camels to snakes
// #[wasm_bindgen(method)]
// fn check_same(this: &JsAbility, proof: &JsAbility) -> Result<(), String>;
//
// fn check_parents(th.......)
// }
1 change: 0 additions & 1 deletion src/ability/internal.rs

This file was deleted.

Loading

0 comments on commit d8ebca2

Please sign in to comment.