Skip to content

Commit

Permalink
feat(dataverse): implements claims registering in cognitarium
Browse files Browse the repository at this point in the history
  • Loading branch information
amimart committed Feb 13, 2024
1 parent d4da31d commit 385426e
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
3 changes: 3 additions & 0 deletions contracts/okp4-dataverse/src/registrar/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod credential;
mod rdf;
pub mod registry;
65 changes: 65 additions & 0 deletions contracts/okp4-dataverse/src/registrar/registry.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use crate::registrar::credential::DataverseCredential;
use crate::registrar::rdf::serialize;
use crate::state::DATAVERSE;
use crate::ContractError;
use cosmwasm_std::{DepsMut, StdResult, Storage, WasmMsg};
use okp4_cognitarium::msg::{
DataFormat, Node, SelectItem, SelectQuery, SimpleWhereCondition, TriplePattern, VarOrNode,
VarOrNodeOrLiteral, WhereCondition, IRI,
};
use okp4_cognitarium_client::CognitariumClient;

/// ClaimRegistrar is the entity responsible to manage claims (i.e. submission and revocation) into
/// the Dataverse, ensuring that any pre-condition criteria to an action is met, and any attached
/// logic is properly executed.
pub struct ClaimRegistrar {
triplestore: CognitariumClient,
}

impl ClaimRegistrar {
const RDF_DATA_FORMAT: DataFormat = DataFormat::NTriples;

pub fn try_new(storage: &dyn Storage) -> StdResult<Self> {
let dataverse = DATAVERSE.load(storage)?;
Ok(Self {
triplestore: CognitariumClient::new(dataverse.triplestore_address),
})
}

pub fn submit_claim(
&self,
deps: &DepsMut<'_>,
credential: &DataverseCredential<'_>,
) -> Result<WasmMsg, ContractError> {
let resp = self.triplestore.select(
deps.querier,
SelectQuery {
prefixes: vec![],
limit: Some(1u32),
select: vec![SelectItem::Variable("p".to_string())],
r#where: vec![WhereCondition::Simple(SimpleWhereCondition::TriplePattern(
TriplePattern {
subject: VarOrNode::Node(Node::NamedNode(IRI::Full(
credential.id.to_string(),
))),
predicate: VarOrNode::Variable("p".to_string()),
object: VarOrNodeOrLiteral::Variable("o".to_string()),
},
))],
},
)?;

if !resp.results.bindings.is_empty() {
Err(ContractError::CredentialAlreadyExists(
credential.id.to_string(),
))?;
}

self.triplestore
.insert_data(
Some(Self::RDF_DATA_FORMAT),
serialize(credential, (&Self::RDF_DATA_FORMAT).into())?,
)
.map_err(ContractError::from)
}
}

0 comments on commit 385426e

Please sign in to comment.