Skip to content

Commit

Permalink
lli - Solution First Pass
Browse files Browse the repository at this point in the history
Signed-off-by: lli <lli@anonyome.com>
  • Loading branch information
lukewli-anonyome committed Jan 3, 2024
1 parent 40e9a87 commit a80a396
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 23 deletions.
58 changes: 35 additions & 23 deletions did_core/did_methods/did_resolver_sov/src/resolution/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,31 +68,41 @@ pub(super) async fn ledger_response_to_ddo<E: Default>(
) -> Result<DidResolutionOutput<E>, DidSovError> {
let (service_id, ddo_id) = prepare_ids(did)?;

let service_data = get_data_from_response(resp)?;
let endpoint: EndpointDidSov = serde_json::from_value(service_data["endpoint"].clone())?;
let service_data = get_data_from_response(resp);

let txn_time = get_txn_time_from_response(resp)?;
let datetime = unix_to_datetime(txn_time);
let txn_time = get_txn_time_from_response(resp);

let service = {
// Initialize DID document builder
let mut ddo_builder = DidDocument::builder(ddo_id);

// Process endpoint data if available
if let Ok(service_data) = service_data {
let endpoint: EndpointDidSov = serde_json::from_value(service_data["endpoint"].clone())?;
let service_types: Vec<String> = endpoint
.types
.into_iter()
.filter(|t| *t != DidSovServiceType::Unknown)
.map(|t| t.to_string())
.collect();
let mut builder = Service::builder(
service_id,
endpoint.endpoint.as_str().try_into()?,
Default::default(),
);
for service_type in service_types {
builder = builder.add_service_type(service_type)?;
}
builder.build()
};

// TODO: Use multibase instead of base58
let service = {
let mut builder = Service::builder(
service_id,
endpoint.endpoint.as_str().try_into()?,
Default::default(),
);
for service_type in service_types {
builder = builder.add_service_type(service_type)?;
}

builder.build()
};

// Add service to DID document
ddo_builder = ddo_builder.add_service(service);
}

// Continue building DID document
let verification_method = VerificationMethod::builder(
did.to_string().try_into()?,
did.to_string().try_into()?,
Expand All @@ -101,16 +111,18 @@ pub(super) async fn ledger_response_to_ddo<E: Default>(
.add_public_key_base58(verkey)
.build();

let ddo = DidDocument::builder(ddo_id)
.add_service(service)
.add_verification_method(verification_method)
.build();
ddo_builder = ddo_builder.add_verification_method(verification_method);

let ddo = ddo_builder.build();

let ddo_metadata = {
let mut metadata_builder = DidDocumentMetadata::builder().deactivated(false);
if let Some(datetime) = datetime {
metadata_builder = metadata_builder.updated(datetime);
};
if let Ok(txn_time) = txn_time {
let datetime = unix_to_datetime(txn_time);
if let Some(datetime) = datetime {
metadata_builder = metadata_builder.updated(datetime);
};
}
metadata_builder.build()
};

Expand Down
42 changes: 42 additions & 0 deletions did_core/did_methods/did_resolver_sov/tests/resolution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,45 @@ async fn test_error_handling_during_resolution() {

assert!(result.is_err());
}

#[tokio::test]
async fn write_new_nym_and_get_did_doc() {
let profile = build_setup_profile().await;
let (new_nym, verkey) = profile
.wallet
.create_and_store_my_did(None, None)
.await
.unwrap();

profile
.ledger_write
.publish_nym(
&profile.wallet,
&profile.institution_did,
&new_nym,
Some(&verkey),
None,
None,
)
.await
.unwrap();

// NEED TO WRITE ENDPOINT FOR IT TO RESOLVE
// write_test_endpoint(&profile.wallet, &profile.ledger_write, &new_nym).await;

let resolver = DidSovResolver::new(profile.ledger_read);
let did = format!("did:sov:{}", new_nym);

let did_doc = resolver
.resolve(
&Did::parse(did.clone()).unwrap(),
&DidResolutionOptions::default(),
)
.await
.unwrap();

println!(
"{}",
serde_json::to_string_pretty(&did_doc.did_document()).unwrap()
);
}

0 comments on commit a80a396

Please sign in to comment.