Skip to content

Commit

Permalink
Change KeyMeta to KeyPattern
Browse files Browse the repository at this point in the history
Add DIDMethods::generate to accept method name + pattern and pass on the
pattern to the appropriate DID method
  • Loading branch information
clehner committed Mar 19, 2021
1 parent c54e372 commit 6918513
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 51 deletions.
6 changes: 3 additions & 3 deletions did-ethr/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,9 @@ impl DIDMethod for DIDEthr {
fn generate(&self, source: &Source) -> Option<String> {
let jwk = match source {
Source::Key(jwk) => jwk,
Source::KeyMeta(jwk, meta) => {
if !meta.is_empty() {
// No metadata properties supported
Source::KeyAndPattern(jwk, pattern) => {
if !pattern.is_empty() {
// TODO: support pattern
return None;
}
jwk
Expand Down
6 changes: 3 additions & 3 deletions did-key/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,9 @@ impl DIDMethod for DIDKey {
fn generate(&self, source: &Source) -> Option<String> {
let jwk = match source {
Source::Key(jwk) => jwk,
Source::KeyMeta(jwk, meta) => {
if !meta.is_empty() {
// No metadata properties supported
Source::KeyAndPattern(jwk, pattern) => {
if !pattern.is_empty() {
// TODO: support pattern
return None;
}
jwk
Expand Down
46 changes: 10 additions & 36 deletions did-pkh/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ use ssi::did::{
DEFAULT_CONTEXT, DIDURL,
};
use ssi::did_resolve::{
DIDResolver, DocumentMetadata, Metadata, ResolutionInputMetadata, ResolutionMetadata,
ERROR_INVALID_DID, TYPE_DID_LD_JSON,
DIDResolver, DocumentMetadata, ResolutionInputMetadata, ResolutionMetadata, ERROR_INVALID_DID,
TYPE_DID_LD_JSON,
};

pub const PUBLIC_KEY_HASH_NAME: &'static str = "publicKeyHashName";

/// did:pkh DID Method
pub struct DIDPKH;

Expand Down Expand Up @@ -161,27 +159,10 @@ impl DIDMethod for DIDPKH {
}

fn generate(&self, source: &Source) -> Option<String> {
let (key, meta) = match source {
Source::KeyMeta(key, meta) => (key, meta),
let (key, pkh_name) = match source {
Source::KeyAndPattern(key, pattern) => (key, pattern),
_ => return None,
};
let mut pkh_name = None;
for (name, value) in *meta {
match &name[..] {
PUBLIC_KEY_HASH_NAME => match value {
Metadata::String(string) => pkh_name = Some(string),
_ => return None,
},
_ => {
// Other metadata properties not supported
return None;
}
}
}
let pkh_name = match pkh_name {
Some(pkh_name) => pkh_name,
None => return None,
};
let addr = match match &pkh_name[..] {
"tz" => ssi::blakesig::hash_public_key(key).ok(),
"eth" => ssi::keccak_hash::hash_public_key(key).ok(),
Expand All @@ -207,16 +188,12 @@ mod tests {
use ssi::ldp::ProofSuite;
use ssi::one_or_many::OneOrMany;
use ssi::vc::Proof;
use std::collections::HashMap;

fn test_generate(jwk_value: Value, type_: &str, did_expected: &str) {
let jwk: JWK = from_value(jwk_value).unwrap();
let mut meta = HashMap::new();
meta.insert(
PUBLIC_KEY_HASH_NAME.to_string(),
Metadata::String(type_.to_string()),
);
let did = DIDPKH.generate(&Source::KeyMeta(&jwk, &meta)).unwrap();
let did = DIDPKH
.generate(&Source::KeyAndPattern(&jwk, type_))
.unwrap();
assert_eq!(did, did_expected);
}

Expand Down Expand Up @@ -337,12 +314,9 @@ mod tests {
proof_suite: &dyn ProofSuite,
) {
use ssi::vc::{Credential, Issuer, LinkedDataProofOptions, URI};
let mut meta = HashMap::new();
meta.insert(
PUBLIC_KEY_HASH_NAME.to_string(),
Metadata::String(type_.to_string()),
);
let did = DIDPKH.generate(&Source::KeyMeta(&key, &meta)).unwrap();
let did = DIDPKH
.generate(&Source::KeyAndPattern(&key, type_))
.unwrap();
eprintln!("did: {}", did);
let mut vc: Credential = from_value(json!({
"@context": "https://www.w3.org/2018/credentials/v1",
Expand Down
6 changes: 3 additions & 3 deletions did-sol/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ impl DIDMethod for DIDSol {
fn generate(&self, source: &Source) -> Option<String> {
let jwk = match source {
Source::Key(jwk) => jwk,
Source::KeyMeta(jwk, meta) => {
if !meta.is_empty() {
// No metadata properties supported
Source::KeyAndPattern(jwk, pattern) => {
if !pattern.is_empty() {
// TODO: support pattern
return None;
}
jwk
Expand Down
6 changes: 3 additions & 3 deletions did-tezos/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,9 +265,9 @@ impl DIDMethod for DIDTz {
fn generate(&self, source: &Source) -> Option<String> {
let jwk = match source {
Source::Key(jwk) => jwk,
Source::KeyMeta(jwk, meta) => {
if !meta.is_empty() {
// No metadata properties supported
Source::KeyAndPattern(jwk, pattern) => {
if !pattern.is_empty() {
// TODO: support pattern
return None;
}
jwk
Expand Down
30 changes: 27 additions & 3 deletions src/did.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::str::FromStr;

use crate::did_resolve::{
Content, ContentMetadata, DIDResolver, DereferencingInputMetadata, DereferencingMetadata,
DocumentMetadata, Metadata, ResolutionInputMetadata, ResolutionMetadata, ERROR_INVALID_DID,
DocumentMetadata, ResolutionInputMetadata, ResolutionMetadata, ERROR_INVALID_DID,
ERROR_METHOD_NOT_SUPPORTED, TYPE_DID_LD_JSON,
};
use crate::error::Error;
Expand Down Expand Up @@ -208,8 +208,8 @@ pub enum Resource {
pub enum Source<'a> {
/// Public key
Key(&'a JWK),
/// Public key and additional parameters
KeyMeta(&'a JWK, &'a HashMap<String, Metadata>),
/// Public key and additional pattern
KeyAndPattern(&'a JWK, &'a str),
}

#[derive(Debug, Serialize, Deserialize, Clone, Default)]
Expand Down Expand Up @@ -295,6 +295,30 @@ impl<'a> DIDMethods<'a> {
};
Ok(method)
}

/// Generate a DID given some input
pub fn generate(&self, source: &Source) -> Option<String> {
let (jwk, pattern) = match source {
Source::Key(_) => {
// Need name/pattern to select DID method
return None;
}
Source::KeyAndPattern(jwk, pattern) => (jwk, pattern),
};
let mut parts = pattern.splitn(2, ':');
let method_name = parts.next().unwrap();
let method = match self.methods.get(method_name) {
Some(method) => method,
None => return None,
};
if let Some(method_pattern) = parts.next() {
let source = Source::KeyAndPattern(jwk, method_pattern);
method.generate(&source)
} else {
let source = Source::Key(jwk);
method.generate(&source)
}
}
}

#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
Expand Down

0 comments on commit 6918513

Please sign in to comment.