Skip to content
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

Add resolution for did:jwk #245

Merged
merged 6 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion bindings/web5_uniffi/src/web5.udl
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
namespace web5 {
JwkData ed25519_generator_generate();

[Throws=RustCoreError]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did this mean to get removed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, because we're moving to all resolve() functions returning a ResolutionResult wherein the error case is represented via the ResolutionMetadataError

ResolutionResult did_jwk_resolve([ByRef] string uri);
[Async, Throws=RustCoreError]
ResolutionResult did_web_resolve([ByRef] string uri);
Expand Down Expand Up @@ -36,6 +35,7 @@ interface InMemoryKeyManager {
Signer get_signer(JwkData public_jwk);
[Throws=RustCoreError]
JwkData import_private_jwk(JwkData private_key);
KeyManager get_as_key_manager();
};

enum Dsa {
Expand Down Expand Up @@ -275,6 +275,7 @@ dictionary FilterData {

interface PresentationDefinition {
constructor(PresentationDefinitionData data);
PresentationDefinitionData get_data();
[Throws=RustCoreError]
sequence<string> select_credentials([ByRef] sequence<string> vc_jwts);
};
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@ impl PresentationDefinition {
.select_credentials(vc_jwts)
.map_err(|e| Arc::new(e.into()))
}

pub fn get_data(&self) -> InnerPresentationDefinition {
self.0.clone()
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use super::key_manager::KeyManager;
use crate::{
dsa::{OuterSigner, Signer},
errors::Result,
Expand All @@ -11,8 +12,7 @@ use web5::apid::crypto::{
},
};

use super::key_manager::KeyManager;

#[derive(Clone)]
pub struct InMemoryKeyManager(pub InnerInMemoryKeyManager);

impl InMemoryKeyManager {
Expand All @@ -25,6 +25,10 @@ impl InMemoryKeyManager {
.import_private_jwk(private_key)
.map_err(|e| Arc::new(e.into()))
}

pub fn get_as_key_manager(&self) -> Arc<dyn KeyManager> {
Arc::new(self.clone())
}
}

impl KeyManager for InMemoryKeyManager {
Expand Down
6 changes: 3 additions & 3 deletions bindings/web5_uniffi_wrapper/src/dids/methods/did_jwk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use web5::apid::{crypto::jwk::Jwk, dids::methods::did_jwk::DidJwk as InnerDidJwk

pub struct DidJwk(pub InnerDidJwk);

pub fn did_jwk_resolve(uri: &str) -> Result<Arc<ResolutionResult>> {
let resolution_result = InnerDidJwk::resolve(uri).map_err(|e| Arc::new(e.into()))?;
Ok(Arc::new(ResolutionResult(resolution_result)))
pub fn did_jwk_resolve(uri: &str) -> Arc<ResolutionResult> {
let resolution_result = InnerDidJwk::resolve(uri);
Arc::new(ResolutionResult(resolution_result))
}

impl DidJwk {
Expand Down
12 changes: 6 additions & 6 deletions crates/web5/src/apid/credentials/presentation_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type Result<T> = std::result::Result<T, PexError>;

/// Represents a DIF Presentation Definition defined [here](https://identity.foundation/presentation-exchange/#presentation-definition).
/// Presentation Definitions are objects that articulate what proofs a Verifier requires.
#[derive(Debug, Serialize, Deserialize, Clone)]
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct PresentationDefinition {
pub id: String,
#[serde(skip_serializing_if = "Option::is_none")]
Expand All @@ -34,7 +34,7 @@ pub struct PresentationDefinition {

/// Represents a DIF Input Descriptor defined [here](https://identity.foundation/presentation-exchange/#input-descriptor).
/// Input Descriptors are used to describe the information a Verifier requires of a Holder.
#[derive(Debug, Serialize, Deserialize, Clone)]
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct InputDescriptor {
pub id: String,
#[serde(skip_serializing_if = "Option::is_none")]
Expand All @@ -45,14 +45,14 @@ pub struct InputDescriptor {
}

/// Contains the requirements for a given Input Descriptor.
#[derive(Debug, Serialize, Deserialize, Clone)]
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct Constraints {
#[serde(skip_serializing_if = "Vec::is_empty")]
pub fields: Vec<Field>,
}

/// Contains the requirements for a given field within a proof.
#[derive(Debug, Serialize, Deserialize, Clone)]
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct Field {
#[serde(skip_serializing_if = "Option::is_none")]
pub id: Option<String>,
Expand All @@ -71,14 +71,14 @@ pub struct Field {
}

/// Type alias for the possible values of the predicate field.
#[derive(Debug, Serialize, Deserialize, Clone)]
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub enum Optionality {
Required,
Preferred,
}

/// A JSON Schema that is applied against the value of a field.
#[derive(Debug, Serialize, Deserialize, Clone)]
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct Filter {
#[serde(rename = "type", skip_serializing_if = "Option::is_none")]
pub r#type: Option<String>,
Expand Down
2 changes: 2 additions & 0 deletions crates/web5/src/apid/crypto/jwk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ pub struct Jwk {
pub alg: String,
pub kty: String,
pub crv: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub d: Option<String>,
pub x: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub y: Option<String>,
}

Expand Down
16 changes: 16 additions & 0 deletions crates/web5/src/apid/dids/bearer_did.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,19 @@ impl BearerDid {
Ok(self.key_manager.get_signer(public_jwk)?)
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::apid::crypto::key_managers::in_memory_key_manager::InMemoryKeyManager;

#[test]
fn can_instantiate_did_jwk() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we'll come back through and use test vectors once we have them

let did_uri = "did:jwk:eyJrdHkiOiJPS1AiLCJ1c2UiOiJzaWciLCJjcnYiOiJFZDI1NTE5Iiwia2lkIjoiVnRTSFhQbEtEdzFFRW9PajVYTjNYV2hqU1BZVk52WC1lNHZqUk8weVlKQSIsIngiOiJpejcwc3ZTTHhOWmhzRHhlSlFfam5PVmJYM0tGTmtjQmNNaldqWm1YRXNBIiwiYWxnIjoiRWREU0EifQ";
let key_manager = InMemoryKeyManager::new();

let bearer_did = BearerDid::new(did_uri, Arc::new(key_manager)).unwrap();

assert_eq!(did_uri, bearer_did.document.id);
}
}
50 changes: 39 additions & 11 deletions crates/web5/src/apid/dids/methods/did_jwk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use crate::apid::{
data_model::{document::Document, verification_method::VerificationMethod},
did::Did,
resolution::{
resolution_metadata::ResolutionMetadataError, resolution_result::ResolutionResult,
resolution_metadata::{ResolutionMetadata, ResolutionMetadataError},
resolution_result::ResolutionResult,
},
},
};
Expand Down Expand Up @@ -49,7 +50,7 @@ impl DidJwk {
}

pub fn from_uri(uri: &str) -> Result<Self> {
let resolution_result = DidJwk::resolve(uri)?;
let resolution_result = DidJwk::resolve(uri);

match resolution_result.document {
None => Err(match resolution_result.resolution_metadata.error {
Expand All @@ -63,16 +64,43 @@ impl DidJwk {
}
}

pub fn resolve(uri: &str) -> Result<ResolutionResult> {
let identifier = Did::new(uri)?;
let decoded_jwk = general_purpose::URL_SAFE_NO_PAD.decode(identifier.id)?;
let public_jwk = serde_json::from_slice(&decoded_jwk)?;
pub fn resolve(uri: &str) -> ResolutionResult {
let result: Result<ResolutionResult> = (|| {
let identifier = Did::new(uri)?;
let decoded_jwk = general_purpose::URL_SAFE_NO_PAD.decode(identifier.id)?;
let public_jwk = serde_json::from_slice::<Jwk>(&decoded_jwk)?;

let did_jwk = DidJwk::from_public_jwk(public_jwk)?;
let kid = format!("{}#0", identifier.uri);
let document = Document {
id: identifier.uri.clone(),
verification_method: vec![VerificationMethod {
id: kid.clone(),
r#type: "JsonWebKey".to_string(),
controller: identifier.uri.clone(),
public_key_jwk: public_jwk,
}],
assertion_method: Some(vec![kid.clone()]),
authentication: Some(vec![kid.clone()]),
capability_invocation: Some(vec![kid.clone()]),
capability_delegation: Some(vec![kid.clone()]),
key_agreement: Some(vec![kid.clone()]),
..Default::default()
};

Ok(ResolutionResult {
document: Some(did_jwk.document),
..Default::default()
})
Ok(ResolutionResult {
document: Some(document),
..Default::default()
})
})();

match result {
Ok(resolution_result) => resolution_result,
Err(_) => ResolutionResult {
resolution_metadata: ResolutionMetadata {
error: Some(ResolutionMetadataError::InternalError),
},
..Default::default()
},
}
}
}
32 changes: 28 additions & 4 deletions crates/web5/src/apid/dids/resolution/resolution_result.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use super::{document_metadata::DocumentMetadata, resolution_metadata::ResolutionMetadata};
use crate::apid::dids::data_model::document::Document;
use crate::apid::dids::{
data_model::document::Document, did::Did, methods::did_jwk::DidJwk,
resolution::resolution_metadata::ResolutionMetadataError,
};
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Default, Deserialize, Serialize)]
Expand All @@ -11,9 +14,30 @@ pub struct ResolutionResult {

impl ResolutionResult {
pub fn new(uri: &str) -> Self {
println!("ResolutionResult::new() called with {}", uri);
Self {
..Default::default()
let did = Did::new(uri).unwrap(); // 🚧
KendallWeihe marked this conversation as resolved.
Show resolved Hide resolved

match did.method.as_str() {
"jwk" => DidJwk::resolve(uri),
_ => ResolutionResult {
resolution_metadata: ResolutionMetadata {
error: Some(ResolutionMetadataError::MethodNotSupported),
},
..Default::default()
},
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn can_resolve_did_jwk() {
let did_uri = "did:jwk:eyJrdHkiOiJPS1AiLCJ1c2UiOiJzaWciLCJjcnYiOiJFZDI1NTE5Iiwia2lkIjoiVnRTSFhQbEtEdzFFRW9PajVYTjNYV2hqU1BZVk52WC1lNHZqUk8weVlKQSIsIngiOiJpejcwc3ZTTHhOWmhzRHhlSlFfam5PVmJYM0tGTmtjQmNNaldqWm1YRXNBIiwiYWxnIjoiRWREU0EifQ";
let resolution_result = ResolutionResult::new(did_uri);

assert_eq!(None, resolution_result.resolution_metadata.error);
assert_eq!(resolution_result.document.unwrap().id, did_uri.to_string());
}
}
Loading