Skip to content

Commit

Permalink
resolver system tests are working
Browse files Browse the repository at this point in the history
Signed-off-by: George Mulhearn <[email protected]>
  • Loading branch information
gmulhearn-anonyome committed Nov 26, 2024
1 parent 96d74e9 commit a98803a
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 7 deletions.
2 changes: 1 addition & 1 deletion did_core/did_methods/did_cheqd/src/resolution/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::{
const MAINNET_NAMESPACE: &str = "mainnet";
const MAINNET_DEFAULT_GRPC: &str = "https://grpc.cheqd.net:443";
const TESTNET_NAMESPACE: &str = "testnet";
const TESTNET_DEFAULT_GRPC: &str = "https://rpc.cheqd.network:443";
const TESTNET_DEFAULT_GRPC: &str = "https://grpc.cheqd.network:443";

pub struct DidCheqdResolverConfiguration {
networks: Vec<NetworkConfiguration>,
Expand Down
34 changes: 32 additions & 2 deletions did_core/did_methods/did_cheqd/src/resolution/transformer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::str::FromStr;
use did_resolver::{
did_doc::schema::{
did_doc::DidDocument,
service::Service,
types::uri::Uri,
utils::OneOrList,
verification_method::{PublicKeyField, VerificationMethod, VerificationMethodType},
Expand All @@ -13,7 +14,10 @@ use serde_json::json;

use crate::{
error::DidCheqdError,
proto::cheqd::did::v2::{DidDoc as CheqdDidDoc, VerificationMethod as CheqdVerificationMethod},
proto::cheqd::did::v2::{
DidDoc as CheqdDidDoc, Service as CheqdService,
VerificationMethod as CheqdVerificationMethod,
},
};

impl TryFrom<CheqdDidDoc> for DidDocument {
Expand All @@ -28,7 +32,9 @@ impl TryFrom<CheqdDidDoc> for DidDocument {
.into_iter()
.map(Did::parse)
.collect::<Result<_, _>>()?;
doc.set_controller(OneOrList::from(controller));
if controller.len() > 0 {
doc.set_controller(OneOrList::from(controller));
}

for vm in value.verification_method {
let vm = VerificationMethod::try_from(vm)?;
Expand All @@ -52,6 +58,11 @@ impl TryFrom<CheqdDidDoc> for DidDocument {
doc.add_key_agreement_ref(vm_id.parse()?);
}

for svc in value.service {
let svc = Service::try_from(svc)?;
doc.add_service(svc);
}

let aka: Vec<_> = value
.also_known_as
.iter()
Expand All @@ -72,6 +83,7 @@ impl TryFrom<CheqdVerificationMethod> for VerificationMethod {

let vm_key_encoded = value.verification_material;

// TODO - lots of todo!()s
let pk = match vm_type {
VerificationMethodType::Ed25519VerificationKey2020 => PublicKeyField::Multibase {
public_key_multibase: vm_key_encoded,
Expand Down Expand Up @@ -122,3 +134,21 @@ impl TryFrom<CheqdVerificationMethod> for VerificationMethod {
Ok(vm)
}
}

impl TryFrom<CheqdService> for Service {
type Error = DidCheqdError;

fn try_from(value: CheqdService) -> Result<Self, Self::Error> {
// TODO #1301 - fix mapping: https://github.com/hyperledger/aries-vcx/issues/1301
let endpoint = value.service_endpoint.into_iter().next().unwrap(); // TODO

let svc = Service::new(
Uri::from_str(&value.id)?,
endpoint.parse().unwrap(), // TODO
serde_json::from_value(json!(value.service_type))?,
Default::default(),
);

Ok(svc)
}
}
80 changes: 76 additions & 4 deletions did_core/did_methods/did_cheqd/tests/resolution.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,85 @@
use did_cheqd::resolution::resolver::{DidCheqdResolver, DidCheqdResolverConfiguration};
use serde_json::json;

#[tokio::test]
async fn test_resolve_known_mainnet_vector() {
// sample from https://dev.uniresolver.io/
let did = "did:cheqd:mainnet:Ps1ysXP2Ae6GBfxNhNQNKN".parse().unwrap();
// NOTE: modifications from uni-resolver:
// *remove contexts,
// make serviceEndpoints into single item (not array)
let expected_doc = json!({
"@context": [],
"id": "did:cheqd:mainnet:Ps1ysXP2Ae6GBfxNhNQNKN",
"verificationMethod": [
{
"id": "did:cheqd:mainnet:Ps1ysXP2Ae6GBfxNhNQNKN#key1",
"type": "Ed25519VerificationKey2020",
"controller": "did:cheqd:mainnet:Ps1ysXP2Ae6GBfxNhNQNKN",
"publicKeyMultibase": "z6Mkta7joRuvDh7UnoESdgpr9dDUMh5LvdoECDi3WGrJoscA"
}
],
"authentication": [
"did:cheqd:mainnet:Ps1ysXP2Ae6GBfxNhNQNKN#key1"
],
"service": [
{
"id": "did:cheqd:mainnet:Ps1ysXP2Ae6GBfxNhNQNKN#website",
"type": "LinkedDomains",
"serviceEndpoint": "https://www.cheqd.io/"
},
{
"id": "did:cheqd:mainnet:Ps1ysXP2Ae6GBfxNhNQNKN#non-fungible-image",
"type": "LinkedDomains",
"serviceEndpoint": "https://gateway.ipfs.io/ipfs/bafybeihetj2ng3d74k7t754atv2s5dk76pcqtvxls6dntef3xa6rax25xe"
},
{
"id": "did:cheqd:mainnet:Ps1ysXP2Ae6GBfxNhNQNKN#twitter",
"type": "LinkedDomains",
"serviceEndpoint": "https://twitter.com/cheqd_io"
},
{
"id": "did:cheqd:mainnet:Ps1ysXP2Ae6GBfxNhNQNKN#linkedin",
"type": "LinkedDomains",
"serviceEndpoint": "https://www.linkedin.com/company/cheqd-identity/"
}
]
});

let resolver = DidCheqdResolver::new(DidCheqdResolverConfiguration::default());
let doc = resolver.resolve_did(&did).await.unwrap();
assert_eq!(serde_json::to_value(doc.clone()).unwrap(), expected_doc);
assert_eq!(doc, serde_json::from_value(expected_doc).unwrap());
}

#[tokio::test]
async fn test_resolve_known_testnet_vector() {
// let did = "did:cheqd:testnet:BttdoaxtC5JkYJoLeGV8ny".parse().unwrap();
let did = "did:cheqd:mainnet:e536be60-880a-4d10-bd95-e84d13d7db6d"
// sample from https://dev.uniresolver.io/
let did = "did:cheqd:testnet:55dbc8bf-fba3-4117-855c-1e0dc1d3bb47"
.parse()
.unwrap();
dbg!(&did);
// NOTE: modifications from uni-resolver:
// * remove contexts,
// * made controller a single item
let expected_doc = json!({
"@context": [],
"id": "did:cheqd:testnet:55dbc8bf-fba3-4117-855c-1e0dc1d3bb47",
"controller": "did:cheqd:testnet:55dbc8bf-fba3-4117-855c-1e0dc1d3bb47",
"verificationMethod": [
{
"id": "did:cheqd:testnet:55dbc8bf-fba3-4117-855c-1e0dc1d3bb47#key-1",
"type": "Ed25519VerificationKey2020",
"controller": "did:cheqd:testnet:55dbc8bf-fba3-4117-855c-1e0dc1d3bb47",
"publicKeyMultibase": "z6MkkVbyHJLLjdjU5B62DaJ4mkdMdUkttf9UqySSkA9bVTeZ"
}
],
"authentication": [
"did:cheqd:testnet:55dbc8bf-fba3-4117-855c-1e0dc1d3bb47#key-1"
]
});

let resolver = DidCheqdResolver::new(DidCheqdResolverConfiguration::default());
let doc = resolver.resolve_did(&did).await.unwrap();
dbg!(doc);
assert_eq!(serde_json::to_value(doc.clone()).unwrap(), expected_doc);
assert_eq!(doc, serde_json::from_value(expected_doc).unwrap());
}

0 comments on commit a98803a

Please sign in to comment.