Skip to content

Commit

Permalink
Merge pull request #625 from axone-protocol/feat/dataverse-vc-headers
Browse files Browse the repository at this point in the history
feat(dataverse)!: rework and enhance vc stored context
  • Loading branch information
amimart authored Aug 23, 2024
2 parents 63b8573 + eb52ace commit 3f79933
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 85 deletions.
27 changes: 16 additions & 11 deletions contracts/axone-dataverse/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,14 @@ pub fn instantiate(
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn execute(
deps: DepsMut<'_>,
_env: Env,
env: Env,
info: MessageInfo,
msg: ExecuteMsg,
) -> Result<Response, ContractError> {
nonpayable(&info)?;
match msg {
ExecuteMsg::SubmitClaims { claims, format: _ } => {
execute::submit_claims(deps, info, claims)
execute::submit_claims(deps, env, info, claims)
}
_ => Err(StdError::generic_err("Not implemented").into()),
}
Expand All @@ -92,6 +92,7 @@ pub mod execute {

pub fn submit_claims(
deps: DepsMut<'_>,
env: Env,
info: MessageInfo,
claims: Binary,
) -> Result<Response, ContractError> {
Expand All @@ -102,7 +103,7 @@ pub mod execute {
let vc = VerifiableCredential::try_from(&vc_dataset)?;
vc.verify(&deps)?;

let credential = DataverseCredential::try_from((info.sender, &vc))?;
let credential = DataverseCredential::try_from((env, info, &vc))?;
let registrar = ClaimRegistrar::try_new(deps.storage)?;

Ok(Response::default()
Expand Down Expand Up @@ -368,16 +369,20 @@ mod tests {
]
);

let expected_data = "<http://example.edu/credentials/3732> <dataverse:credential#submitterAddress> \"axone1072nc6egexqr2v6vpp7yxwm68plvqnkf5uemr0\" .
<http://example.edu/credentials/3732> <dataverse:credential#issuer> <did:key:z6MkpwdnLPAm4apwcrRYQ6fZ3rAcqjLZR4AMk14vimfnozqY> .
<http://example.edu/credentials/3732> <dataverse:credential#type> <https://example.org/examples#UniversityDegreeCredential> .
<http://example.edu/credentials/3732> <dataverse:credential#validFrom> \"2024-02-16T00:00:00Z\"^^<http://www.w3.org/2001/XMLSchema#dateTime> .
<http://example.edu/credentials/3732> <dataverse:credential#subject> <did:key:zDnaeUm3QkcyZWZTPttxB711jgqRDhkwvhF485SFw1bDZ9AQw> .
let expected_data = r#"<http://example.edu/credentials/3732> <dataverse:credential:header#height> "12345" .
<http://example.edu/credentials/3732> <dataverse:credential:header#timestamp> "1571797419" .
<http://example.edu/credentials/3732> <dataverse:credential:header#sender> "axone1072nc6egexqr2v6vpp7yxwm68plvqnkf5uemr0" .
<http://example.edu/credentials/3732> <dataverse:credential:body#issuer> <did:key:z6MkpwdnLPAm4apwcrRYQ6fZ3rAcqjLZR4AMk14vimfnozqY> .
<http://example.edu/credentials/3732> <dataverse:credential:body#type> <https://example.org/examples#UniversityDegreeCredential> .
<http://example.edu/credentials/3732> <dataverse:credential:body#validFrom> "2024-02-16T00:00:00Z"^^<http://www.w3.org/2001/XMLSchema#dateTime> .
<http://example.edu/credentials/3732> <dataverse:credential:body#subject> <did:key:zDnaeUm3QkcyZWZTPttxB711jgqRDhkwvhF485SFw1bDZ9AQw> .
<http://example.edu/credentials/3732> <dataverse:credential:header#tx_index> "3" .
_:c0 <https://example.org/examples#degree> _:b0 .
_:b0 <http://schema.org/name> \"Bachelor of Science and Arts\"^^<http://www.w3.org/1999/02/22-rdf-syntax-ns#HTML> .
_:b0 <http://schema.org/name> "Bachelor of Science and Arts"^^<http://www.w3.org/1999/02/22-rdf-syntax-ns#HTML> .
_:b0 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <https://example.org/examples#BachelorDegree> .
<http://example.edu/credentials/3732> <dataverse:credential#claim> _:c0 .
<http://example.edu/credentials/3732> <dataverse:credential#validUntil> \"2026-02-16T00:00:00Z\"^^<http://www.w3.org/2001/XMLSchema#dateTime> .\n";
<http://example.edu/credentials/3732> <dataverse:credential:body#claim> _:c0 .
<http://example.edu/credentials/3732> <dataverse:credential:body#validUntil> "2026-02-16T00:00:00Z"^^<http://www.w3.org/2001/XMLSchema#dateTime> .
"#;

match resp.messages[0].msg.clone() {
CosmosMsg::Wasm(WasmMsg::Execute {
Expand Down
33 changes: 22 additions & 11 deletions contracts/axone-dataverse/src/registrar/credential.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
use crate::credential::rdf_marker::IRI_VC_TYPE;
use crate::credential::vc::{Claim, VerifiableCredential};
use crate::ContractError;
use cosmwasm_std::Addr;
use cosmwasm_std::{Addr, Env, MessageInfo};
use itertools::Itertools;

#[derive(Debug, PartialEq)]
pub struct DataverseCredential<'a> {
pub submitter_addr: Addr,
pub height: String,
pub timestamp: String,
pub tx_index: Option<String>,
pub sender: Addr,
pub id: &'a str,
pub issuer: &'a str,
pub r#type: &'a str,
Expand Down Expand Up @@ -38,14 +41,17 @@ impl<'a> DataverseCredential<'a> {
}
}

impl<'a> TryFrom<(Addr, &'a VerifiableCredential<'a>)> for DataverseCredential<'a> {
impl<'a> TryFrom<(Env, MessageInfo, &'a VerifiableCredential<'a>)> for DataverseCredential<'a> {
type Error = ContractError;

fn try_from(
(submitter_addr, vc): (Addr, &'a VerifiableCredential<'a>),
(env, info, vc): (Env, MessageInfo, &'a VerifiableCredential<'a>),
) -> Result<Self, Self::Error> {
Ok(DataverseCredential {
submitter_addr,
height: env.block.height.to_string(),
timestamp: env.block.time.seconds().to_string(),
tx_index: env.transaction.map(|tx| tx.index.to_string()),
sender: info.sender,
id: vc.id,
issuer: vc.issuer,
r#type: DataverseCredential::extract_vc_type(vc)?,
Expand All @@ -61,21 +67,25 @@ mod test {
use super::*;
use crate::testutil::testutil;
use axone_rdf::dataset::Dataset;
use cosmwasm_std::testing::message_info;
use rio_api::model::{Literal, NamedNode, Quad};
use testing::addr::{addr, SENDER};
use testing::mock::mock_env_addr;

#[test]
fn proper_from_verifiable() {
let owned_quads = testutil::read_test_quads("vc-valid.nq");
let dataset = Dataset::from(owned_quads.as_slice());
let vc = VerifiableCredential::try_from(&dataset).unwrap();
let dc_res = DataverseCredential::try_from((
Addr::unchecked("axone1072nc6egexqr2v6vpp7yxwm68plvqnkf5uemr0"),
&vc,
));
let dc_res =
DataverseCredential::try_from((mock_env_addr(), message_info(&addr(SENDER), &[]), &vc));

assert!(dc_res.is_ok());
assert_eq!(dc_res.unwrap(), DataverseCredential {
submitter_addr: Addr::unchecked("axone1072nc6egexqr2v6vpp7yxwm68plvqnkf5uemr0"),
height: "12345".to_string(),
timestamp: "1571797419".to_string(),
tx_index: Some("3".to_string()),
sender: addr(SENDER),
id: "https://w3id.org/axone/ontology/vnext/schema/credential/digital-service/description/72cab400-5bd6-4eb4-8605-a5ee8c1a45c9",
issuer: "did:key:zQ3shs7auhJSmVJpiUbQWco6bxxEhSqWnVEPvaBHBRvBKw6Q3",
r#type: "https://w3id.org/axone/ontology/vnext/schema/credential/digital-service/description/DigitalServiceDescriptionCredential",
Expand Down Expand Up @@ -120,7 +130,8 @@ mod test {
let dataset = Dataset::from(owned_quads.as_slice());
let vc = VerifiableCredential::try_from(&dataset).unwrap();
let dc_res = DataverseCredential::try_from((
Addr::unchecked("axone1072nc6egexqr2v6vpp7yxwm68plvqnkf5uemr0"),
mock_env_addr(),
message_info(&addr(SENDER), &[]),
&vc,
));

Expand Down
Loading

0 comments on commit 3f79933

Please sign in to comment.