Skip to content

Commit

Permalink
Add Source::KeyAndPattern and DIDMethods::generate
Browse files Browse the repository at this point in the history
Allow passing an additional string to a DID method in the generate
function.

Implement generate on DIDMethods to allow generating a DID from a set of
DID methods given a DID method name and optional additional string,
separated by a colon (':').

The additional string may be a prefix or pattern for the DID's
method-specific ID, according to the DID method.
  • Loading branch information
clehner committed Apr 1, 2021
1 parent b61ea69 commit 76cdb3d
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Support HTTP(S) requests in WASM and on Android.
- Support relative DID URLs in DID documents.
- Support [publicKeyBase58][] for Ed25519.
- Added `DIDMethods::generate` function.

### Changed
- Make `ResolutionResult` struct public.
Expand All @@ -56,6 +57,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Make `jws::sign_bytes` return bytes instead of string.
- Allow multiple proofs and multiple verification methods in a DID document
- Bundle `json-ld` crate, for `crates.io` release.
- Added `Source::KeyAndPattern` enum variant.

### Fixed
- Fix `tz1` hashing.
Expand Down
7 changes: 7 additions & 0 deletions did-ethr/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,13 @@ impl DIDMethod for DIDEthr {
fn generate(&self, source: &Source) -> Option<String> {
let jwk = match source {
Source::Key(jwk) => jwk,
Source::KeyAndPattern(jwk, pattern) => {
if !pattern.is_empty() {
// TODO: support pattern
return None;
}
jwk
}
_ => return None,
};
let hash = match ssi::keccak_hash::hash_public_key(jwk) {
Expand Down
7 changes: 7 additions & 0 deletions did-key/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,13 @@ impl DIDMethod for DIDKey {
fn generate(&self, source: &Source) -> Option<String> {
let jwk = match source {
Source::Key(jwk) => jwk,
Source::KeyAndPattern(jwk, pattern) => {
if !pattern.is_empty() {
// pattern not supported
return None;
}
jwk
}
_ => return None,
};
let did = match jwk.params {
Expand Down
7 changes: 7 additions & 0 deletions did-sol/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,13 @@ impl DIDMethod for DIDSol {
fn generate(&self, source: &Source) -> Option<String> {
let jwk = match source {
Source::Key(jwk) => jwk,
Source::KeyAndPattern(jwk, pattern) => {
if !pattern.is_empty() {
// pattern not supported
return None;
}
jwk
}
_ => return None,
};
let did = match jwk.params {
Expand Down
7 changes: 7 additions & 0 deletions did-tezos/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,13 @@ impl DIDMethod for DIDTz {
fn generate(&self, source: &Source) -> Option<String> {
let jwk = match source {
Source::Key(jwk) => jwk,
Source::KeyAndPattern(jwk, pattern) => {
if !pattern.is_empty() {
// TODO: support pattern
return None;
}
jwk
}
_ => return None,
};
let hash = match hash_public_key(jwk) {
Expand Down
27 changes: 27 additions & 0 deletions src/did.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,10 @@ pub enum Resource {
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum Source<'a> {
/// Public key
Key(&'a JWK),
/// Public key and additional pattern
KeyAndPattern(&'a JWK, &'a str),
}

#[derive(Debug, Serialize, Deserialize, Clone, Default)]
Expand Down Expand Up @@ -292,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 76cdb3d

Please sign in to comment.