Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/update grc20 specs #4

Merged
merged 4 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.lock

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

9 changes: 5 additions & 4 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ use futures::{stream, StreamExt, TryStreamExt};
use ipfs::IpfsClient;
use kg_core::ids;
use kg_core::pb::grc20;
use kg_core::system_ids::ROOT_SPACE_ID;
use kg_node::kg;
use kg_node::kg::entity::{Entity, EntityNode};
use kg_node::kg::{
self,
entity::{Entity, EntityNode},
};
use kg_node::ops::conversions;
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::util::SubscriberInitExt;
Expand Down Expand Up @@ -128,7 +129,7 @@ enum Command {
ipfs_hash: String,

/// Space ID (defaults to root space)
#[arg(default_value = ROOT_SPACE_ID)]
// #[arg(default_value = ROOT_SPACE_ID)]
space_id: String,
},

Expand Down
3 changes: 2 additions & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ chrono = "0.4.38"
md-5 = "0.10.6"
neo4rs = "0.8.0"
prost = "0.13.3"
rand = "0.8.5"
serde = { version = "1.0.215", features = ["derive"] }
thiserror = "2.0.3"
tracing = "0.1.40"
uuid = "1.11.0"
uuid = { version = "1.11.0", features = ["v4"] }
web3-utils = { version = "0.1.0", path = "../web3-utils" }
71 changes: 48 additions & 23 deletions core/proto/grc20.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,28 @@ syntax = "proto3";

package grc20;

message IpfsMetadata {
// We version the data structured used to represent proposal metadata. Each
// proposal type has their own metadata and versioning that we can change
// independently of other proposal types.
string version = 1;
ActionType type = 2;
string id = 3;
string name = 4;
}

message Edit {
ActionType type = 1;
string version = 2;
string version = 1;
ActionType type = 2;
string id = 3;
string name = 4;
repeated Op ops = 5;
repeated string authors = 6;
}

message ImportEdit {
ActionType type = 1;
string version = 2;
string version = 1;
ActionType type = 2;
string id = 3;
string name = 4;
repeated Op ops = 5;
Expand Down Expand Up @@ -42,44 +52,59 @@ message Value {
}

enum OpType {
DEFAULT_OP_TYPE = 0;
NONE = 0;
SET_TRIPLE = 1;
DELETE_TRIPLE = 2;
}

enum ValueType {
DEFAULT_VALUE_TYPE = 0;
UNKNOWN = 0;
TEXT = 1;
NUMBER = 2;
ENTITY = 3;
URI = 4;
CHECKBOX = 5;
TIME = 6;
GEO_LOCATION = 7;
CHECKBOX = 3;
URL = 4;
TIME = 5;
POINT = 6;
}

message Membership {
ActionType type = 1;
string name = 2;
string version = 3;
string id = 4;
string user = 5;
}

message Subspace {
ActionType type = 1;
string name = 2;
string version = 3;
string id = 4;
string subspace = 5;
}

enum ActionType {
DEFAULT_ACTION_TYPE = 0;
EMPTY = 0;
ADD_EDIT = 1;
IMPORT_SPACE = 2;
ADD_SUBSPACE = 3;
REMOVE_SUBSPACE = 4;
ADD_EDITOR = 5;
REMOVE_EDITOR = 6;
ADD_MEMBER = 7;
REMOVE_MEMBER = 8;
ADD_SUBSPACE = 2;
REMOVE_SUBSPACE = 3;
IMPORT_SPACE = 4;
ARCHIVE_SPACE = 5;
ADD_EDITOR = 6;
REMOVE_EDITOR = 7;
ADD_MEMBER = 8;
REMOVE_MEMBER = 9;
}

message Import {
ActionType type = 1;
string version = 2;
string version = 1;
ActionType type = 2;
string previousNetwork = 3;
string previousContractAddress = 4;
repeated string edits = 5;
// repeated Edit edits = 3;
}

message Options {
string format = 1;
string crop = 2;
}
}
6 changes: 3 additions & 3 deletions core/proto/ipfs.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ message IpfsMetadata {
// We version the data structured used to represent proposal metadata. Each
// proposal type has their own metadata and versioning that we can change
// independently of other proposal types.
ActionType type = 1;
string version = 2;
string version = 1;
ActionType type = 2;
string id = 3;
string name = 4;
}
Expand Down Expand Up @@ -107,4 +107,4 @@ message Import {
message Options {
string format = 1;
string crop = 2;
}
}
119 changes: 119 additions & 0 deletions core/src/blocks.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
use crate::{ids::create_geo_id, pb::grc20, relation::create_relationship, system_ids};

pub struct DataBlock;

impl DataBlock {
pub fn create_triples(
from_id: &str,
source_type: DataBlockType,
position: Option<&str>,
name: Option<&str>,
) -> impl Iterator<Item = grc20::Triple> {
let new_block_id = create_geo_id();

std::iter::empty()
// Create relation: NewBlock > TYPES > DataBlock
.chain(create_relationship(
&new_block_id,
system_ids::DATA_BLOCK,
system_ids::TYPES,
None,
))
// Create relation: NewBlock > DATA_SOURCE_TYPE_RELATION_TYPE > SourceType
.chain(create_relationship(
&new_block_id,
source_type.to_id(),
system_ids::DATA_SOURCE_TYPE_RELATION_TYPE,
None,
))
// Create relation: Entity > BLOCKS > NewBlock
.chain(create_relationship(
from_id,
&new_block_id,
system_ids::BLOCKS,
position,
))
// Set attribute: NewBlock.Name
.chain(name.map(|name| grc20::Triple {
entity: new_block_id,
attribute: system_ids::NAME.to_string(),
value: Some(grc20::Value {
r#type: grc20::ValueType::Text.into(),
value: name.to_string(),
}),
}))
}
}

pub enum DataBlockType {
Collection,
Geo,
Query,
}

impl DataBlockType {
pub fn to_id(&self) -> &str {
match self {
DataBlockType::Collection => system_ids::COLLECTION_DATA_SOURCE,
DataBlockType::Geo => system_ids::ALL_OF_GEO_DATA_SOURCE,
DataBlockType::Query => system_ids::QUERY_DATA_SOURCE,
}
}
}

pub struct TextBlock;

impl TextBlock {
pub fn create_triples(
from_id: &str,
text: &str,
position: Option<&str>,
) -> impl Iterator<Item = grc20::Triple> {
let new_block_id = create_geo_id();

std::iter::empty()
// Create relation: NewBlock > TYPES > TextBlock
.chain(create_relationship(
&new_block_id,
system_ids::TEXT_BLOCK,
system_ids::TYPES,
None,
))
// Create relation: Entity > BLOCKS > NewBlock
.chain(create_relationship(
from_id,
&new_block_id,
system_ids::BLOCKS,
position,
))
// Set attribute: NewBlock.MarkdownContent
.chain(std::iter::once(grc20::Triple {
entity: new_block_id,
attribute: system_ids::MARKDOWN_CONTENT.to_string(),
value: Some(grc20::Value {
r#type: grc20::ValueType::Text.into(),
value: text.to_string(),
}),
}))
}
}

// pub struct ImageBlock;

// impl ImageBlock {
// pub fn new(from_id: &str, url: &str, position: Option<&str>) -> impl Iterator<Item = grc20::Triple> {
// let new_block_id = create_geo_id();

// std::iter::empty()
// // Create relation: NewBlock > TYPES > ImageBlock
// .chain(create_relationship(&new_block_id, system_ids::IMAGE_BLOCK, system_ids::TYPES, None))
// // Create relation: Entity > BLOCKS > NewBlock
// .chain(create_relationship(from_id, &new_block_id, system_ids::BLOCKS, position))
// // Set attribute: NewBlock.Url
// .chain(std::iter::once(grc20::Triple {
// entity: new_block_id,
// attribute: system_ids::URL.to_string(),
// value: Some(grc20::Value { r#type: grc20::ValueType::Text.into(), value: url.to_string() }),
// }))
// }
// }
27 changes: 27 additions & 0 deletions core/src/conversion.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use crate::{ids::Grc20Id, pb::grc20};

/// A trait for converting a type to a sequence of triples.
pub trait ToTriples {
fn to_triples(&self) -> impl Iterator<Item = grc20::Triple>;
}

/// A trait for creating a type from a sequence of triples.
pub trait FromTriples: Sized {
type Error;

fn from_triples(
id: Grc20Id,
triples: impl IntoIterator<Item = grc20::Triple>,
) -> Result<Self, Self::Error>;
}

pub trait ToOps {
fn to_ops(&self) -> impl Iterator<Item = grc20::Op>;
}

pub trait FromOps: Sized {
type Error;

fn from_ops(id: Grc20Id, ops: impl IntoIterator<Item = grc20::Op>)
-> Result<Self, Self::Error>;
}
45 changes: 45 additions & 0 deletions core/src/graph_uri.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use std::fmt::Display;

use crate::ids::Grc20Id;

pub struct GraphUri {
pub id: String,
}

impl Display for GraphUri {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "graph://{}", self.id)
}
}

#[derive(Debug, thiserror::Error)]
#[error("Invalid graph uri: {0}")]
pub struct InvalidGraphUri(String);

impl GraphUri {
pub fn from_id_str(id: &str) -> Self {
Self { id: id.to_string() }
}

pub fn from_id(id: Grc20Id) -> Self {
Self { id: id.to_string() }
}

pub fn to_id(&self) -> Grc20Id {
Grc20Id(self.id.clone())
}

pub fn from_uri(uri: &str) -> Result<Self, InvalidGraphUri> {
if !uri.starts_with("graph://") {
Err(InvalidGraphUri(uri.to_string()))
} else {
Ok(Self {
id: uri.replace("graph://", ""),
})
}
}

pub fn is_valid(uri: &str) -> bool {
uri.starts_with("graph://")
}
}
Loading
Loading