Skip to main content

DidKey

Struct DidKey 

Source
pub struct DidKey { /* private fields */ }
Available on crate feature did-key and (crate features did-jwk or did-key or did-web) only.
Expand description

The did:key method: a DidResolver that expands the DID and a DidProducer that encodes one public key.

The resolver’s options are those the specification defines: the KeyFormat (publicKeyFormat) and whether to derive an X25519 key agreement method from an Ed25519 key (enableEncryptionKeyDerivation). Both are set here as defaults and may be overridden per resolution through ResolutionOptions::properties under those names, with a string and a boolean respectively.

§Examples

use identity_kit::did::Did;
use identity_kit::jose::Jwk;
use identity_kit::methods::DidKey;
use identity_kit::methods::key::KeyFormat;

let jwk: Jwk = serde_json::from_str(
    r#"{"kty":"OKP","crv":"Ed25519","x":"Lm_M42cB3HkUiODQsXRcweM6TByfzEHGO9ND274JcOY"}"#,
)?;
let did = DidKey::did(&jwk)?;
assert_eq!(did.as_str(), "did:key:z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK");

let document = DidKey::new().with_format(KeyFormat::JsonWebKey).document(&did)?;
let method = &document.verification_method[0];
assert_eq!(method.id.fragment(), Some("z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK"));
assert_eq!(method.public_key_jwk(), Some(&jwk));

Implementations§

Source§

impl DidKey

Source

pub const METHOD: &'static str = "key"

The method name, key.

Source

pub const MAX_DID_LEN: usize

The longest did:key this method can produce: the verification method id did#multikey repeats the Multikey string, and must fit DEFAULT_MAX_LEN bytes.

Source

pub const CONTEXT: [&'static str; 1]

The @context of every did:key document.

Source

pub const fn new() -> Self

The method with KeyFormat::Multikey and no key agreement derivation.

Source

pub const fn with_format(self, format: KeyFormat) -> Self

This method expressing keys as format.

Source

pub const fn with_key_agreement(self, derive: bool) -> Self

This method deriving an X25519 key agreement method from an Ed25519 key when derive is set (enableEncryptionKeyDerivation). The derived method is embedded under keyAgreement, as in the specification’s examples.

Source

pub const fn format(&self) -> KeyFormat

The key format.

Source

pub const fn key_agreement(&self) -> bool

Whether an X25519 key agreement method is derived from an Ed25519 key.

Source

pub fn did(jwk: &Jwk) -> Result<Did, DidKeyError>

The did:key of the public part of jwk.

§Errors
Source

pub fn document(&self, did: &Did) -> Result<DidDocument, ResolutionError>

The DID document did expands to with this method’s options (the method’s Read operation).

§Errors

A ResolutionError whose kind is:

  • MethodNotSupported when did is not a did:key.
  • InvalidDid when the method-specific id is not a Multikey string (optionally preceded by a positive integer version) with a known multicodec header, or the DID is so long that the verification method id did#multikey would not fit DEFAULT_MAX_LEN bytes.
  • Other with the method’s own error names otherwise: unsupportedPublicKeyType for a key type this crate cannot read, invalidPublicKeyLength when the raw key has the wrong length, and invalidPublicKey when it is not a valid key.

A failure to decode the Multikey string has the JwkError as its source.

Trait Implementations§

Source§

impl Clone for DidKey

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for DidKey

Source§

impl Debug for DidKey

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for DidKey

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl DidProducer for DidKey

Source§

fn capabilities(&self) -> MethodCapabilities

Embedded, one key, never updated; the fragment is the Multikey string of the key.

Source§

fn produce<'life0, 'life1, 'async_trait>( &'life0 self, keys: &'life1 [Jwk], __arg2: (), ) -> Pin<Box<dyn Future<Output = Result<DidDocument, ProduceError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

The document of DidKey::did for the one key in keys, with this method’s options.

Source§

type Params = ()

Method-specific parameters.
Source§

fn method(&self) -> &'static str

The method name: web for did:web.
Source§

impl DidResolver for DidKey

Source§

fn resolve<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, did: &'life1 Did, options: &'life2 ResolutionOptions, ) -> Pin<Box<dyn Future<Output = Result<ResolutionResult, ResolutionError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Expands did with DidKey::document, with the publicKeyFormat and enableEncryptionKeyDerivation options of options overriding this method’s defaults.

Source§

fn method(&self) -> &'static str

The method name this resolver handles: web for did:web.
Source§

impl Eq for DidKey

Source§

impl Hash for DidKey

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for DidKey

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for DidKey

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more