Skip to content

Commit

Permalink
* Introduce RPC and new protocol crate
Browse files Browse the repository at this point in the history
  • Loading branch information
ryan-s-roberts committed Nov 7, 2023
1 parent f0c4df1 commit d1182ff
Show file tree
Hide file tree
Showing 28 changed files with 2,736 additions and 982 deletions.
2,229 changes: 1,747 additions & 482 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ members = [
"crates/pallet-chronicle",
"node/runtime-chronicle",
"node/node-chronicle",
"crates/runtime-api-chronicle",
"crates/rpc-chronicle",
"crates/protocol-substrate-chronicle",
]

[workspace.dependencies]
Expand Down
6 changes: 3 additions & 3 deletions crates/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ k256 = { version="0.11.3",default-features=false,features=["ecdsa","pkcs8"] }
lazy_static = { version="1.4" }
locspan = { version="0.7", optional=true}
mime = { version="0.3", optional=true }
scale-info = {version="2.10.0", default-features=false, features=["derive"]}
parity-scale-codec = {version = "3.6.5", default-features=false,features=["derive","max-encoded-len"]}
scale-info = {version="2.10.0", default-features=false, features=["derive"], optional=true}
parity-scale-codec = {version = "3.6.5", default-features=false,features=["derive","max-encoded-len"], optional=true}
newtype-derive-2018 = {workspace=true}
macro-attr-2018 = {workspace=true}
serde = { version="1.0", default-features=false }
Expand Down Expand Up @@ -65,7 +65,7 @@ tempfile = { workspace = true }
std = ["k256/std","tracing/std","serde/std","serde_json/std","hex/std","futures/std","iri-string/std","chrono/std","anyhow/std"]
default = ["std","graphql-bindings","diesel-bindings","json-ld"]
# Enable parity support, annoyingly lazy_static has a non standard way of enabling non_std
parity = ["lazy_static/spin_no_std"]
parity-encoding = ["lazy_static/spin_no_std","dep:parity-scale-codec","dep:scale-info"]
# At this point, LD should be a seperate crate
json-ld = ["dep:json-ld","dep:json-syntax","dep:rdf-types","dep:hashbrown","dep:mime","dep:locspan","dep:iref"]
graphql-bindings = ["async-graphql"]
Expand Down
57 changes: 33 additions & 24 deletions crates/common/src/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ use parity_scale_codec::{alloc::collections::BTreeMap, alloc::string::String};
#[cfg(not(feature = "std"))]
use scale_info::{prelude::borrow::ToOwned, prelude::string::ToString, prelude::*};

use parity_scale_codec::{Decode, Encode};
use scale_info::{build::Fields, Path, Type, TypeInfo};
use serde_json::Value;

use crate::prov::DomaintypeId;
Expand All @@ -27,27 +25,17 @@ impl core::fmt::Display for SerdeWrapper {
}
}

impl Encode for SerdeWrapper {
#[cfg(feature = "parity-encoding")]
impl parity_scale_codec::Encode for SerdeWrapper {
fn encode_to<T: parity_scale_codec::Output + ?Sized>(&self, dest: &mut T) {
let json_string =
serde_json::to_string(&self.0).expect("Failed to serialize Value to JSON string");
json_string.encode_to(dest);
}
}

impl From<Value> for SerdeWrapper {
fn from(value: Value) -> Self {
SerdeWrapper(value)
}
}

impl From<SerdeWrapper> for Value {
fn from(wrapper: SerdeWrapper) -> Self {
wrapper.0
}
}

impl Decode for SerdeWrapper {
#[cfg(feature = "parity-encoding")]
impl parity_scale_codec::Decode for SerdeWrapper {
fn decode<I: parity_scale_codec::Input>(
input: &mut I,
) -> Result<Self, parity_scale_codec::Error> {
Expand All @@ -59,16 +47,35 @@ impl Decode for SerdeWrapper {
}
}

impl TypeInfo for SerdeWrapper {
#[cfg(feature = "parity-encoding")]
impl scale_info::TypeInfo for SerdeWrapper {
type Identity = Self;
fn type_info() -> Type {
Type::builder()
.path(Path::new("SerdeWrapper", module_path!()))
.composite(Fields::unnamed().field(|f| f.ty::<String>().type_name("Json")))
fn type_info() -> scale_info::Type {
scale_info::Type::builder()
.path(scale_info::Path::new("SerdeWrapper", module_path!()))
.composite(
scale_info::build::Fields::unnamed().field(|f| f.ty::<String>().type_name("Json")),
)
}
}

#[derive(Debug, Clone, Encode, Decode, TypeInfo, Serialize, Deserialize, PartialEq, Eq)]
impl From<Value> for SerdeWrapper {
fn from(value: Value) -> Self {
SerdeWrapper(value)
}
}

impl From<SerdeWrapper> for Value {
fn from(wrapper: SerdeWrapper) -> Self {
wrapper.0
}
}

#[cfg_attr(
feature = "parity-encoding",
derive(scale_info::TypeInfo, parity_scale_codec::Encode, parity_scale_codec::Decode)
)]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct Attribute {
pub typ: String,
pub value: SerdeWrapper,
Expand Down Expand Up @@ -98,9 +105,11 @@ impl Attribute {
}
}

#[derive(
Debug, Clone, Serialize, Deserialize, Encode, Decode, TypeInfo, PartialEq, Eq, Default,
#[cfg_attr(
feature = "parity-encoding",
derive(scale_info::TypeInfo, parity_scale_codec::Encode, parity_scale_codec::Decode)
)]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
pub struct Attributes {
pub typ: Option<DomaintypeId>,
pub attributes: BTreeMap<String, Attribute>,
Expand Down
17 changes: 8 additions & 9 deletions crates/common/src/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ use tracing::warn;
#[cfg(not(feature = "std"))]
use parity_scale_codec::{
alloc::collections::BTreeMap, alloc::collections::BTreeSet, alloc::string::String,
alloc::vec::Vec,
alloc::vec::Vec, Decode, Encode,
};
#[cfg(not(feature = "std"))]
use scale_info::{prelude::borrow::ToOwned, prelude::string::ToString, prelude::*};
use scale_info::{prelude::borrow::ToOwned, prelude::string::ToString, prelude::*, TypeInfo};
#[cfg(feature = "std")]
use std::collections::BTreeMap;

Expand Down Expand Up @@ -192,22 +192,21 @@ impl OpaData {
))
}
}

/// Signed user identity containing the serialized identity, signature, and
/// verifying key. Implements `TryFrom` to deserialize to the user identity object
#[cfg_attr(
feature = "parity-encoding",
derive(scale_info::TypeInfo, parity_scale_codec::Encode, parity_scale_codec::Decode)
)]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SignedIdentity {
pub identity: String,
pub signature: Option<Vec<u8>>,
pub verifying_key: Option<VerifyingKey>,
pub verifying_key: Option<Vec<u8>>,
}

impl SignedIdentity {
fn new(
id: &AuthId,
signature: Vec<u8>,
verifying_key: VerifyingKey,
) -> Result<Self, IdentityError> {
fn new(id: &AuthId, signature: Vec<u8>, verifying_key: Vec<u8>) -> Result<Self, IdentityError> {
Ok(Self {
identity: serde_json::to_string(&id)?,
signature: Some(signature),
Expand Down
39 changes: 27 additions & 12 deletions crates/common/src/ledger.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use scale_info::TypeInfo;
use tracing::instrument;

use crate::{
Expand All @@ -19,7 +18,8 @@ use core::str::FromStr;
#[cfg(not(feature = "std"))]
use parity_scale_codec::{
alloc::boxed::Box, alloc::collections::btree_map::Entry, alloc::collections::BTreeMap,
alloc::collections::BTreeSet, alloc::string::String, alloc::sync::Arc, alloc::vec::Vec,
alloc::collections::BTreeSet, alloc::string::String, alloc::sync::Arc, alloc::vec::Vec, Decode,
Encode,
};
#[cfg(not(feature = "std"))]
use scale_info::prelude::*;
Expand Down Expand Up @@ -81,6 +81,26 @@ impl core::fmt::Display for SubmissionError {
}
}

#[cfg_attr(
feature = "parity-encoding",
derive(scale_info::TypeInfo, parity_scale_codec::Encode, parity_scale_codec::Decode)
)]
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct OperationSubmission {
pub identity: SignedIdentity,
pub operations: Vec<ChronicleOperation>,
}

impl OperationSubmission {
pub fn new(identity: SignedIdentity, operations: Vec<ChronicleOperation>) -> Self {
OperationSubmission { identity, operations }
}

pub fn new_anonymous(operations: Vec<ChronicleOperation>) -> Self {
Self::new(SignedIdentity::new_no_identity(), operations)
}
}

pub type SubmitResult = Result<ChronicleTransactionId, SubmissionError>;

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -138,23 +158,18 @@ impl SubmissionStage {
}
}

#[derive(
parity_scale_codec::Encode,
parity_scale_codec::Decode,
TypeInfo,
PartialEq,
Eq,
PartialOrd,
Ord,
Debug,
Clone,
#[derive(PartialEq, Eq, PartialOrd, Ord, Debug, Clone)]
#[cfg_attr(
feature = "parity-encoding",
derive(scale_info::TypeInfo, parity_scale_codec::Encode, parity_scale_codec::Decode)
)]
pub struct LedgerAddress {
// Namespaces do not have a namespace
namespace: Option<NamespaceId>,
resource: ChronicleIri,
}

#[cfg(feature = "parity-encoding")]
impl parity_scale_codec::MaxEncodedLen for LedgerAddress {
fn max_encoded_len() -> usize {
2048usize
Expand Down
Loading

0 comments on commit d1182ff

Please sign in to comment.