Skip to content

Commit eb52ace

Browse files
committedAug 23, 2024
feat(dataverse)!: rework and enhance vc stored context
1 parent a21a75c commit eb52ace

File tree

4 files changed

+139
-85
lines changed

4 files changed

+139
-85
lines changed
 

‎contracts/axone-dataverse/src/contract.rs

+16-11
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,14 @@ pub fn instantiate(
6868
#[cfg_attr(not(feature = "library"), entry_point)]
6969
pub fn execute(
7070
deps: DepsMut<'_>,
71-
_env: Env,
71+
env: Env,
7272
info: MessageInfo,
7373
msg: ExecuteMsg,
7474
) -> Result<Response, ContractError> {
7575
nonpayable(&info)?;
7676
match msg {
7777
ExecuteMsg::SubmitClaims { claims, format: _ } => {
78-
execute::submit_claims(deps, info, claims)
78+
execute::submit_claims(deps, env, info, claims)
7979
}
8080
_ => Err(StdError::generic_err("Not implemented").into()),
8181
}
@@ -92,6 +92,7 @@ pub mod execute {
9292

9393
pub fn submit_claims(
9494
deps: DepsMut<'_>,
95+
env: Env,
9596
info: MessageInfo,
9697
claims: Binary,
9798
) -> Result<Response, ContractError> {
@@ -102,7 +103,7 @@ pub mod execute {
102103
let vc = VerifiableCredential::try_from(&vc_dataset)?;
103104
vc.verify(&deps)?;
104105

105-
let credential = DataverseCredential::try_from((info.sender, &vc))?;
106+
let credential = DataverseCredential::try_from((env, info, &vc))?;
106107
let registrar = ClaimRegistrar::try_new(deps.storage)?;
107108

108109
Ok(Response::default()
@@ -368,16 +369,20 @@ mod tests {
368369
]
369370
);
370371

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

382387
match resp.messages[0].msg.clone() {
383388
CosmosMsg::Wasm(WasmMsg::Execute {

‎contracts/axone-dataverse/src/registrar/credential.rs

+22-11
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
use crate::credential::rdf_marker::IRI_VC_TYPE;
22
use crate::credential::vc::{Claim, VerifiableCredential};
33
use crate::ContractError;
4-
use cosmwasm_std::Addr;
4+
use cosmwasm_std::{Addr, Env, MessageInfo};
55
use itertools::Itertools;
66

77
#[derive(Debug, PartialEq)]
88
pub struct DataverseCredential<'a> {
9-
pub submitter_addr: Addr,
9+
pub height: String,
10+
pub timestamp: String,
11+
pub tx_index: Option<String>,
12+
pub sender: Addr,
1013
pub id: &'a str,
1114
pub issuer: &'a str,
1215
pub r#type: &'a str,
@@ -38,14 +41,17 @@ impl<'a> DataverseCredential<'a> {
3841
}
3942
}
4043

41-
impl<'a> TryFrom<(Addr, &'a VerifiableCredential<'a>)> for DataverseCredential<'a> {
44+
impl<'a> TryFrom<(Env, MessageInfo, &'a VerifiableCredential<'a>)> for DataverseCredential<'a> {
4245
type Error = ContractError;
4346

4447
fn try_from(
45-
(submitter_addr, vc): (Addr, &'a VerifiableCredential<'a>),
48+
(env, info, vc): (Env, MessageInfo, &'a VerifiableCredential<'a>),
4649
) -> Result<Self, Self::Error> {
4750
Ok(DataverseCredential {
48-
submitter_addr,
51+
height: env.block.height.to_string(),
52+
timestamp: env.block.time.seconds().to_string(),
53+
tx_index: env.transaction.map(|tx| tx.index.to_string()),
54+
sender: info.sender,
4955
id: vc.id,
5056
issuer: vc.issuer,
5157
r#type: DataverseCredential::extract_vc_type(vc)?,
@@ -61,21 +67,25 @@ mod test {
6167
use super::*;
6268
use crate::testutil::testutil;
6369
use axone_rdf::dataset::Dataset;
70+
use cosmwasm_std::testing::message_info;
6471
use rio_api::model::{Literal, NamedNode, Quad};
72+
use testing::addr::{addr, SENDER};
73+
use testing::mock::mock_env_addr;
6574

6675
#[test]
6776
fn proper_from_verifiable() {
6877
let owned_quads = testutil::read_test_quads("vc-valid.nq");
6978
let dataset = Dataset::from(owned_quads.as_slice());
7079
let vc = VerifiableCredential::try_from(&dataset).unwrap();
71-
let dc_res = DataverseCredential::try_from((
72-
Addr::unchecked("axone1072nc6egexqr2v6vpp7yxwm68plvqnkf5uemr0"),
73-
&vc,
74-
));
80+
let dc_res =
81+
DataverseCredential::try_from((mock_env_addr(), message_info(&addr(SENDER), &[]), &vc));
7582

7683
assert!(dc_res.is_ok());
7784
assert_eq!(dc_res.unwrap(), DataverseCredential {
78-
submitter_addr: Addr::unchecked("axone1072nc6egexqr2v6vpp7yxwm68plvqnkf5uemr0"),
85+
height: "12345".to_string(),
86+
timestamp: "1571797419".to_string(),
87+
tx_index: Some("3".to_string()),
88+
sender: addr(SENDER),
7989
id: "https://w3id.org/axone/ontology/vnext/schema/credential/digital-service/description/72cab400-5bd6-4eb4-8605-a5ee8c1a45c9",
8090
issuer: "did:key:zQ3shs7auhJSmVJpiUbQWco6bxxEhSqWnVEPvaBHBRvBKw6Q3",
8191
r#type: "https://w3id.org/axone/ontology/vnext/schema/credential/digital-service/description/DigitalServiceDescriptionCredential",
@@ -120,7 +130,8 @@ mod test {
120130
let dataset = Dataset::from(owned_quads.as_slice());
121131
let vc = VerifiableCredential::try_from(&dataset).unwrap();
122132
let dc_res = DataverseCredential::try_from((
123-
Addr::unchecked("axone1072nc6egexqr2v6vpp7yxwm68plvqnkf5uemr0"),
133+
mock_env_addr(),
134+
message_info(&addr(SENDER), &[]),
124135
&vc,
125136
));
126137

0 commit comments

Comments
 (0)