pub struct DidKey { /* private fields */ }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
impl DidKey
Sourcepub const MAX_DID_LEN: usize
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.
Sourcepub const fn new() -> Self
pub const fn new() -> Self
The method with KeyFormat::Multikey and no key agreement derivation.
Sourcepub const fn with_format(self, format: KeyFormat) -> Self
pub const fn with_format(self, format: KeyFormat) -> Self
This method expressing keys as format.
Sourcepub const fn with_key_agreement(self, derive: bool) -> Self
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.
Sourcepub const fn key_agreement(&self) -> bool
pub const fn key_agreement(&self) -> bool
Whether an X25519 key agreement method is derived from an Ed25519 key.
Sourcepub fn did(jwk: &Jwk) -> Result<Did, DidKeyError>
pub fn did(jwk: &Jwk) -> Result<Did, DidKeyError>
The did:key of the public part of jwk.
§Errors
DidKeyError::Keywith the errors ofJwk::to_multikey: an unsupported key type or undecodable material.DidKeyError::TooLongwhen the Multikey string is so long that the DID exceedsMAX_DID_LENbytes, as a very large RSA key is; base58btc is otherwise valid DID syntax.
Sourcepub fn document(&self, did: &Did) -> Result<DidDocument, ResolutionError>
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:
MethodNotSupportedwhendidis not adid:key.InvalidDidwhen 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 iddid#multikeywould not fitDEFAULT_MAX_LENbytes.Otherwith the method’s own error names otherwise:unsupportedPublicKeyTypefor a key type this crate cannot read,invalidPublicKeyLengthwhen the raw key has the wrong length, andinvalidPublicKeywhen it is not a valid key.
A failure to decode the Multikey string has the JwkError as its source.
Trait Implementations§
impl Copy for DidKey
Source§impl DidProducer for DidKey
impl DidProducer for DidKey
Source§fn capabilities(&self) -> MethodCapabilities
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,
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§impl DidResolver for DidKey
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,
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.
impl Eq for DidKey
impl StructuralPartialEq for DidKey
Auto Trait Implementations§
impl Freeze for DidKey
impl RefUnwindSafe for DidKey
impl Send for DidKey
impl Sync for DidKey
impl Unpin for DidKey
impl UnsafeUnpin for DidKey
impl UnwindSafe for DidKey
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.