Skip to content

Commit

Permalink
Add DidDht create, publish and resolve, add unit tests (#308)
Browse files Browse the repository at this point in the history
  • Loading branch information
KendallWeihe authored Aug 23, 2024
1 parent ae99ef6 commit d0740ae
Show file tree
Hide file tree
Showing 21 changed files with 1,358 additions and 788 deletions.
4 changes: 2 additions & 2 deletions bindings/web5_uniffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use web5_uniffi_wrapper::{
data_model::document::Document,
did::Did,
methods::{
did_dht::{did_dht_resolve, DidDht},
did_dht::{did_dht_create, did_dht_publish, did_dht_resolve, DidDhtCreateOptions},
did_jwk::{did_jwk_create, did_jwk_resolve, DidJwkCreateOptions},
did_web::{did_web_create, did_web_resolve, DidWebCreateOptions},
},
Expand All @@ -36,7 +36,7 @@ use web5::{
verification_method::VerificationMethod as VerificationMethodData,
},
did::Did as DidData,
methods::did_dht::DidDht as DidDhtData,
methods::did_dht::{DidDhtPublishOptions, DidDhtResolveOptions},
portable_did::PortableDid as PortableDidData,
resolution::{
document_metadata::DocumentMetadata as DocumentMetadataData,
Expand Down
32 changes: 18 additions & 14 deletions bindings/web5_uniffi/src/web5.udl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ namespace web5 {
ResolutionResult did_web_resolve([ByRef] string uri);

[Throws=Web5Error]
ResolutionResult did_dht_resolve([ByRef] string uri);
BearerDid did_dht_create(DidDhtCreateOptions? options);
[Throws=Web5Error]
void did_dht_publish(BearerDid bearer_did, DidDhtPublishOptions? options);
ResolutionResult did_dht_resolve([ByRef] string uri, DidDhtResolveOptions? options);
};

[Error]
Expand Down Expand Up @@ -180,21 +183,22 @@ dictionary DidWebCreateOptions {
sequence<VerificationMethodData>? verification_method;
};

dictionary DidDhtData {
DidData did;
DocumentData document;
dictionary DidDhtCreateOptions {
boolean? publish;
string? gateway_url;
KeyManager? key_manager;
sequence<ServiceData>? service;
sequence<string>? controller;
sequence<string>? also_known_as;
sequence<VerificationMethodData>? verification_method;
};

interface DidDht {
[Name=from_identity_key, Throws=Web5Error]
constructor(JwkData identity_key);
[Name=from_uri, Throws=Web5Error]
constructor([ByRef] string uri);
[Throws=Web5Error]
void publish(Signer signer);
[Throws=Web5Error]
void deactivate(Signer signer);
DidDhtData get_data();
dictionary DidDhtResolveOptions {
string? gateway_url;
};

dictionary DidDhtPublishOptions {
string? gateway_url;
};

dictionary PortableDidData {
Expand Down
54 changes: 54 additions & 0 deletions bindings/web5_uniffi_wrapper/src/dids/methods/did_dht.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use crate::{
crypto::key_manager::{KeyManager, ToInnerKeyManager},
dids::{bearer_did::BearerDid, resolution::resolution_result::ResolutionResult},
errors::Result,
};
use std::sync::Arc;
use web5::dids::{
data_model::{service::Service, verification_method::VerificationMethod},
methods::did_dht::{
DidDht as InnerDidDht, DidDhtCreateOptions as InnerDidDhtCreateOptions,
DidDhtPublishOptions, DidDhtResolveOptions,
},
};

pub fn did_dht_resolve(uri: &str, options: Option<DidDhtResolveOptions>) -> Arc<ResolutionResult> {
let resolution_result = InnerDidDht::resolve(uri, options);
Arc::new(ResolutionResult(resolution_result))
}

#[derive(Default)]
pub struct DidDhtCreateOptions {
pub publish: Option<bool>,
pub gateway_url: Option<String>,
pub key_manager: Option<Arc<dyn KeyManager>>,
pub service: Option<Vec<Service>>,
pub controller: Option<Vec<String>>,
pub also_known_as: Option<Vec<String>>,
pub verification_method: Option<Vec<VerificationMethod>>,
}

pub fn did_dht_create(options: Option<DidDhtCreateOptions>) -> Result<Arc<BearerDid>> {
let inner_options = options.map(|o| InnerDidDhtCreateOptions {
publish: o.publish,
gateway_url: o.gateway_url,
key_manager: match o.key_manager {
None => None,
Some(km) => Some(Arc::new(ToInnerKeyManager(km))),
},
service: o.service,
controller: o.controller,
also_known_as: o.also_known_as,
verification_method: o.verification_method,
});

let inner_bearer_did = InnerDidDht::create(inner_options)?;
Ok(Arc::new(BearerDid(inner_bearer_did)))
}

pub fn did_dht_publish(
bearer_did: Arc<BearerDid>,
options: Option<DidDhtPublishOptions>,
) -> Result<()> {
Ok(InnerDidDht::publish(bearer_did.0.clone(), options)?)
}
40 changes: 0 additions & 40 deletions bindings/web5_uniffi_wrapper/src/dids/methods/did_dht/mod.rs

This file was deleted.

7 changes: 0 additions & 7 deletions bindings/web5_uniffi_wrapper/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use thiserror::Error;
use web5::credentials::presentation_definition::PexError;
use web5::credentials::CredentialError;
use web5::dids::bearer_did::BearerDidError;
use web5::dids::methods::MethodError;
use web5::errors::Web5Error as InnerWeb5Error;

#[derive(Debug, Error)]
Expand Down Expand Up @@ -75,12 +74,6 @@ where
variant_name.to_string()
}

impl From<MethodError> for Web5Error {
fn from(error: MethodError) -> Self {
Web5Error::new(error)
}
}

impl From<CredentialError> for Web5Error {
fn from(error: CredentialError) -> Self {
Web5Error::new(error)
Expand Down
2 changes: 1 addition & 1 deletion bound/kt/src/main/kotlin/web5/sdk/dids/BearerDid.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class BearerDid {
val document: Document
val keyManager: KeyManager

private val rustCoreBearerDid: RustCoreBearerDid
internal val rustCoreBearerDid: RustCoreBearerDid

internal constructor(rustCoreBearerDid: RustCoreBearerDid) {
this.rustCoreBearerDid = rustCoreBearerDid
Expand Down
120 changes: 63 additions & 57 deletions bound/kt/src/main/kotlin/web5/sdk/dids/methods/dht/DidDht.kt
Original file line number Diff line number Diff line change
@@ -1,78 +1,84 @@
package web5.sdk.dids.methods.dht

import web5.sdk.crypto.keys.Jwk
import web5.sdk.crypto.signers.Signer
import web5.sdk.dids.Did
import web5.sdk.dids.Document
import web5.sdk.crypto.keys.KeyManager
import web5.sdk.crypto.keys.ToInnerKeyManager
import web5.sdk.dids.BearerDid
import web5.sdk.dids.ResolutionResult
import web5.sdk.rust.ServiceData
import web5.sdk.rust.VerificationMethodData
import web5.sdk.rust.didDhtResolve as rustCoreDidDhtResolve
import web5.sdk.rust.DidDht as RustCoreDidDht
import web5.sdk.rust.Signer as RustCoreSigner

data class DidDhtCreateOptions(
val publish: Boolean? = true,
val gatewayUrl: String? = null,
val keyManager: KeyManager? = null,
val service: List<ServiceData>? = null,
val controller: List<String>? = null,
val alsoKnownAs: List<String>? = null,
val verificationMethod: List<VerificationMethodData>? = null
)

data class DidDhtPublishOptions(
val gatewayUrl: String? = null
)

data class DidDhtResolveOptions(
val gatewayUrl: String? = null
)

/**
* A class representing a DID (Decentralized Identifier) using the DHT method.
*
* @property did The DID associated with this instance.
* @property document The DID document associated with this instance.
*/
class DidDht {
val did: Did
val document: Document

private val rustCoreDidDht: RustCoreDidDht

/**
* Constructs a DidDht instance using an identity key.
*
* @param identityKey The identity key represented as a Jwk.
*/
constructor(identityKey: Jwk) {
rustCoreDidDht = RustCoreDidDht.fromIdentityKey(identityKey.rustCoreJwkData)

this.did = Did.fromRustCoreDidData(rustCoreDidDht.getData().did)
this.document = rustCoreDidDht.getData().document
}

/**
* Constructs a DidDht instance using a DID URI.
*
* @param uri The DID URI.
*/
constructor(uri: String) {
rustCoreDidDht = RustCoreDidDht.fromUri(uri)

this.did = Did.fromRustCoreDidData(rustCoreDidDht.getData().did)
this.document = rustCoreDidDht.getData().document
}

/**
* Publishes the DID document.
*
* @param signer The signer used to sign the publish operation.
*/
fun publish(signer: Signer) {
rustCoreDidDht.publish(signer as RustCoreSigner)
}
companion object {
/**
* Create a DidDht BearerDid using available options.
*
* @param options The set of options to configure creation.
*/
fun create(options: DidDhtCreateOptions? = null): BearerDid {
val rustCoreOptions = options?.let { opts ->
web5.sdk.rust.DidDhtCreateOptions(
opts.publish,
opts.gatewayUrl,
opts.keyManager?.let { ToInnerKeyManager(it) },
opts.service,
opts.controller,
opts.alsoKnownAs,
opts.verificationMethod
)
}
val rustCoreBearerDid = web5.sdk.rust.didDhtCreate(rustCoreOptions)
return BearerDid(rustCoreBearerDid)
}

/**
* Deactivates the DID document.
*
* @param signer The signer used to sign the deactivate operation.
*/
fun deactivate(signer: Signer) {
rustCoreDidDht.deactivate(signer as RustCoreSigner)
}
/**
* Publish a DidDht BearerDid using available options.
*
* @param bearerDid The DidDht BearerDid instance to publish.
* @param options The set of options to configure publish.
*/
fun publish(bearerDid: BearerDid, options: DidDhtPublishOptions? = null) {
web5.sdk.rust.didDhtPublish(
bearerDid.rustCoreBearerDid,
web5.sdk.rust.DidDhtPublishOptions(
options?.gatewayUrl
)
)
}

companion object {
/**
* Resolves a DID URI to a DidResolutionResult.
*
* @param uri The DID URI to resolve.
* @return DidResolutionResult The result of the DID resolution.
*/
@JvmStatic
fun resolve(uri: String): ResolutionResult {
return rustCoreDidDhtResolve(uri).getData()
fun resolve(uri: String, options: DidDhtResolveOptions? = null): ResolutionResult {
val rustCoreOptions = web5.sdk.rust.DidDhtResolveOptions(
options?.gatewayUrl
)
return rustCoreDidDhtResolve(uri, rustCoreOptions).getData()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package web5.sdk.dids.methods.web

import web5.sdk.crypto.keys.KeyManager
import web5.sdk.crypto.keys.ToInnerKeyManager
import web5.sdk.crypto.keys.ToOuterKeyManager
import web5.sdk.dids.BearerDid
import web5.sdk.dids.ResolutionResult
import web5.sdk.rust.Dsa
Expand Down
Loading

0 comments on commit d0740ae

Please sign in to comment.