generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create and resolve did:key #25
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
use crate::did::Did; | ||
use crate::method::{DidMethod, DidMethodError, DidResolutionResult}; | ||
use async_trait::async_trait; | ||
use crypto::key::{Key, KeyType}; | ||
use crypto::key_manager::KeyManager; | ||
use did_method_key::DIDKey as SpruceDidKeyMethod; | ||
use ssi_dids::did_resolve::{DIDResolver, ResolutionInputMetadata}; | ||
use ssi_dids::{DIDMethod, Source}; | ||
use std::sync::Arc; | ||
|
||
/// Concrete implementation for a did:key DID | ||
pub struct DidKey { | ||
uri: String, | ||
key_manager: Arc<dyn KeyManager>, | ||
} | ||
|
||
pub struct DidKeyCreateOptions { | ||
pub key_type: KeyType, | ||
} | ||
|
||
impl Did for DidKey { | ||
fn uri(&self) -> &str { | ||
&self.uri | ||
} | ||
|
||
fn key_manager(&self) -> &Arc<dyn KeyManager> { | ||
&self.key_manager | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl DidMethod<DidKey, DidKeyCreateOptions> for DidKey { | ||
const NAME: &'static str = "key"; | ||
|
||
fn create( | ||
key_manager: Arc<dyn KeyManager>, | ||
options: DidKeyCreateOptions, | ||
) -> Result<DidKey, DidMethodError> { | ||
let key_alias = key_manager.generate_private_key(options.key_type)?; | ||
let public_key = | ||
key_manager | ||
.get_public_key(&key_alias)? | ||
.ok_or(DidMethodError::DidCreationFailure( | ||
"PublicKey not found".to_string(), | ||
))?; | ||
|
||
let uri = SpruceDidKeyMethod | ||
.generate(&Source::Key(public_key.jwk())) | ||
.ok_or(DidMethodError::DidCreationFailure( | ||
"Failed to generate did:key".to_string(), | ||
))?; | ||
|
||
Ok(DidKey { uri, key_manager }) | ||
} | ||
|
||
async fn resolve_uri(did_uri: &str) -> DidResolutionResult { | ||
let input_metadata = ResolutionInputMetadata::default(); | ||
let (did_resolution_metadata, did_document, did_document_metadata) = | ||
SpruceDidKeyMethod.resolve(did_uri, &input_metadata).await; | ||
|
||
DidResolutionResult { | ||
did_resolution_metadata, | ||
did_document, | ||
did_document_metadata, | ||
..Default::default() | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crypto::key_manager::local_key_manager::LocalKeyManager; | ||
use ssi_dids::did_resolve::ERROR_INVALID_DID; | ||
|
||
fn create_did_key(key_type: KeyType) -> DidKey { | ||
let key_manager = Arc::new(LocalKeyManager::new_in_memory()); | ||
|
||
DidKey::create(key_manager, DidKeyCreateOptions { key_type }) | ||
.expect("DidKey creation failed") | ||
} | ||
|
||
#[test] | ||
fn create_produces_correct_uri() { | ||
let did = create_did_key(KeyType::Ed25519); | ||
assert!(did.uri.starts_with("did:key:")); | ||
} | ||
|
||
#[test] | ||
fn create_each_key_type() { | ||
create_did_key(KeyType::Ed25519); | ||
create_did_key(KeyType::Secp256k1); | ||
create_did_key(KeyType::Secp256r1); | ||
} | ||
|
||
#[tokio::test] | ||
async fn instance_resolve() { | ||
let did = create_did_key(KeyType::Ed25519); | ||
let result = did.resolve().await; | ||
assert!(result.did_resolution_metadata.error.is_none()); | ||
|
||
let did_document = result.did_document.unwrap(); | ||
assert_eq!(did_document.id, did.uri); | ||
} | ||
|
||
#[tokio::test] | ||
async fn resolve_uri_success() { | ||
let did = create_did_key(KeyType::Ed25519); | ||
let result = DidKey::resolve_uri(&did.uri).await; | ||
assert!(result.did_resolution_metadata.error.is_none()); | ||
|
||
let did_document = result.did_document.expect("did_document not found"); | ||
KendallWeihe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
assert_eq!(did_document.id, did.uri); | ||
} | ||
|
||
#[tokio::test] | ||
async fn resolve_uri_failure() { | ||
let result = DidKey::resolve_uri("did:key:does-not-exist").await; | ||
assert_eq!( | ||
result.did_resolution_metadata.error, | ||
Some(ERROR_INVALID_DID.to_string()) | ||
); | ||
} | ||
} |
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,4 +1,5 @@ | ||
pub mod jwk; | ||
pub mod key; | ||
pub mod web; | ||
|
||
use crate::did::Did; | ||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it make sense to add documentation to public facing functions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't public.
DidMethod
is public, and it is documented here: https://github.com/TBD54566975/web5-rs/blob/main/crates/dids/src/method/mod.rs#L21-L42There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The implementation of
DidMethod::create
is being done viaDidKey::create
. Since the former is public, I thought that meant this method is public as well....Anyways, I don't think it's important. My main concern is that there is no place in documentation that describes to consumers of the crate how to use
DidKeyCreateOptions
when creating a did of type key. While this is obvious forDidKey
, it will become not so obvious forDidDht
in the future.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can address this when we implement
DidDht