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 Source::KeyAndPattern and DIDMethods::generate #133

Merged
merged 1 commit into from
Apr 1, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
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