generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add DidDht create, publish and resolve, add unit tests (#308)
- Loading branch information
1 parent
ae99ef6
commit d0740ae
Showing
21 changed files
with
1,358 additions
and
788 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
40
bindings/web5_uniffi_wrapper/src/dids/methods/did_dht/mod.rs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 63 additions & 57 deletions
120
bound/kt/src/main/kotlin/web5/sdk/dids/methods/dht/DidDht.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.