Module mydata_did.v1_0.utils.verification_method
DID Document Public Key classes.
Copyright 2017-2019 Government of Canada Public Services and Procurement Canada - buyandsell.gc.ca
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Expand source code
"""
DID Document Public Key classes.
Copyright 2017-2019 Government of Canada
Public Services and Procurement Canada - buyandsell.gc.ca
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
from collections import namedtuple
from enum import Enum
from .util import canon_did, canon_ref, derive_did_type, resource
LinkedDataKeySpec = namedtuple("LinkedDataKeySpec", "ver_type authn_type specifier")
class PublicKeyType(Enum):
"""Class encapsulating public key types."""
ED25519_SIG_2018 = LinkedDataKeySpec(
"Ed25519VerificationKey2018",
"Ed25519SignatureAuthentication2018",
"publicKeyBase58",
)
@staticmethod
def get(val: str) -> "PublicKeyType":
"""
Find enum instance corresponding to input value (RsaVerificationKey2018 etc).
Args:
val: input value marking public key type
Returns: the public key type
"""
for pktype in PublicKeyType:
if val in (pktype.ver_type, pktype.authn_type):
return pktype
return None
@property
def ver_type(self) -> str:
"""Accessor for the verification type identifier."""
return self.value.ver_type
@property
def authn_type(self) -> str:
"""Accessor for the authentication type identifier."""
return self.value.authn_type
@property
def specifier(self) -> str:
"""Accessor for the value specifier."""
return self.value.specifier
def specification(self, val: str) -> str:
"""
Return specifier and input value for use in public key specification.
Args:
val: value of public key
Returns: dict mapping applicable specifier to input value
"""
return {self.specifier: val}
class PublicKey:
"""
Public key specification to embed in DID document.
Retains DIDs as raw values (orientated toward indy-facing operations),
everything else as URIs (oriented toward W3C-facing operations).
"""
def __init__(
self,
did: str,
ident: str,
value: str,
pk_type: PublicKeyType = None,
controller: str = None,
authn: bool = False,
) -> None:
"""
Retain key specification particulars.
Args:
did: DID of DID document embedding public key
ident: identifier for public key
value: key content, encoded as key specification requires
pk_type: public key type (enum), default ED25519_SIG_2018
controller: controller DID (default DID of DID document)
authn: whether key as has DID authentication privilege (default False)
Raises:
ValueError: on any bad input DID.
"""
self._did = canon_did(did)
self._id = canon_ref(self._did, ident)
self._did_type = derive_did_type(resource(self._id))
self._value = value
self._type = pk_type or PublicKeyType.ED25519_SIG_2018
self._controller = canon_did(controller) if controller else self._did
self._authn = authn
@property
def did(self) -> str:
"""Accessor for the DID."""
return self._did
@property
def id(self) -> str:
"""Accessor for the public key identifier."""
return self._id
@property
def type(self) -> PublicKeyType:
"""Accessor for the public key type."""
return self._type
@property
def value(self) -> str:
"""Accessor for the public key value."""
return self._value
@property
def controller(self) -> str:
"""Accessor for the controller DID."""
return self._controller
@property
def authn(self) -> bool:
"""Accessor for the authentication marker.
Returns: whether public key is marked as having DID authentication privilege
"""
return self._authn
@authn.setter
def authn(self, value: bool) -> None:
"""Setter for the authentication marker.
Args:
value: authentication marker
"""
self._authn = value
def to_dict(self) -> dict:
"""Return dict representation of public key to embed in DID document."""
return {
"id": self.id,
"type": str(self.type.ver_type),
"controller": canon_ref(self.did, self.controller, did_type=self._did_type),
**self.type.specification(self.value),
}
def __repr__(self) -> str:
"""Return string representation of the public key instance."""
return "PublicKey({}, {}, {}, {}, {}, {})".format(
self.did, self.id, self.value, self.type, self.controller, self.authn
)
Classes
class LinkedDataKeySpec (ver_type, authn_type, specifier)
-
LinkedDataKeySpec(ver_type, authn_type, specifier)
Ancestors
- builtins.tuple
Instance variables
var authn_type
-
Alias for field number 1
var specifier
-
Alias for field number 2
var ver_type
-
Alias for field number 0
class PublicKey (did: str, ident: str, value: str, pk_type: PublicKeyType = None, controller: str = None, authn: bool = False)
-
Public key specification to embed in DID document.
Retains DIDs as raw values (orientated toward indy-facing operations), everything else as URIs (oriented toward W3C-facing operations).
Retain key specification particulars.
Args
did
- DID of DID document embedding public key
ident
- identifier for public key
value
- key content, encoded as key specification requires
pk_type
- public key type (enum), default ED25519_SIG_2018
controller
- controller DID (default DID of DID document)
authn
- whether key as has DID authentication privilege (default False)
Raises
ValueError
- on any bad input DID.
Expand source code
class PublicKey: """ Public key specification to embed in DID document. Retains DIDs as raw values (orientated toward indy-facing operations), everything else as URIs (oriented toward W3C-facing operations). """ def __init__( self, did: str, ident: str, value: str, pk_type: PublicKeyType = None, controller: str = None, authn: bool = False, ) -> None: """ Retain key specification particulars. Args: did: DID of DID document embedding public key ident: identifier for public key value: key content, encoded as key specification requires pk_type: public key type (enum), default ED25519_SIG_2018 controller: controller DID (default DID of DID document) authn: whether key as has DID authentication privilege (default False) Raises: ValueError: on any bad input DID. """ self._did = canon_did(did) self._id = canon_ref(self._did, ident) self._did_type = derive_did_type(resource(self._id)) self._value = value self._type = pk_type or PublicKeyType.ED25519_SIG_2018 self._controller = canon_did(controller) if controller else self._did self._authn = authn @property def did(self) -> str: """Accessor for the DID.""" return self._did @property def id(self) -> str: """Accessor for the public key identifier.""" return self._id @property def type(self) -> PublicKeyType: """Accessor for the public key type.""" return self._type @property def value(self) -> str: """Accessor for the public key value.""" return self._value @property def controller(self) -> str: """Accessor for the controller DID.""" return self._controller @property def authn(self) -> bool: """Accessor for the authentication marker. Returns: whether public key is marked as having DID authentication privilege """ return self._authn @authn.setter def authn(self, value: bool) -> None: """Setter for the authentication marker. Args: value: authentication marker """ self._authn = value def to_dict(self) -> dict: """Return dict representation of public key to embed in DID document.""" return { "id": self.id, "type": str(self.type.ver_type), "controller": canon_ref(self.did, self.controller, did_type=self._did_type), **self.type.specification(self.value), } def __repr__(self) -> str: """Return string representation of the public key instance.""" return "PublicKey({}, {}, {}, {}, {}, {})".format( self.did, self.id, self.value, self.type, self.controller, self.authn )
Instance variables
var authn : bool
-
Accessor for the authentication marker.
Returns: whether public key is marked as having DID authentication privilege
Expand source code
@property def authn(self) -> bool: """Accessor for the authentication marker. Returns: whether public key is marked as having DID authentication privilege """ return self._authn
var controller : str
-
Accessor for the controller DID.
Expand source code
@property def controller(self) -> str: """Accessor for the controller DID.""" return self._controller
var did : str
-
Accessor for the DID.
Expand source code
@property def did(self) -> str: """Accessor for the DID.""" return self._did
var id : str
-
Accessor for the public key identifier.
Expand source code
@property def id(self) -> str: """Accessor for the public key identifier.""" return self._id
var type : PublicKeyType
-
Accessor for the public key type.
Expand source code
@property def type(self) -> PublicKeyType: """Accessor for the public key type.""" return self._type
var value : str
-
Accessor for the public key value.
Expand source code
@property def value(self) -> str: """Accessor for the public key value.""" return self._value
Methods
def to_dict(self) ‑> dict
-
Return dict representation of public key to embed in DID document.
Expand source code
def to_dict(self) -> dict: """Return dict representation of public key to embed in DID document.""" return { "id": self.id, "type": str(self.type.ver_type), "controller": canon_ref(self.did, self.controller, did_type=self._did_type), **self.type.specification(self.value), }
class PublicKeyType (value, names=None, *, module=None, qualname=None, type=None, start=1)
-
Class encapsulating public key types.
Expand source code
class PublicKeyType(Enum): """Class encapsulating public key types.""" ED25519_SIG_2018 = LinkedDataKeySpec( "Ed25519VerificationKey2018", "Ed25519SignatureAuthentication2018", "publicKeyBase58", ) @staticmethod def get(val: str) -> "PublicKeyType": """ Find enum instance corresponding to input value (RsaVerificationKey2018 etc). Args: val: input value marking public key type Returns: the public key type """ for pktype in PublicKeyType: if val in (pktype.ver_type, pktype.authn_type): return pktype return None @property def ver_type(self) -> str: """Accessor for the verification type identifier.""" return self.value.ver_type @property def authn_type(self) -> str: """Accessor for the authentication type identifier.""" return self.value.authn_type @property def specifier(self) -> str: """Accessor for the value specifier.""" return self.value.specifier def specification(self, val: str) -> str: """ Return specifier and input value for use in public key specification. Args: val: value of public key Returns: dict mapping applicable specifier to input value """ return {self.specifier: val}
Ancestors
- enum.Enum
Class variables
var ED25519_SIG_2018
Static methods
def get(val: str) ‑> PublicKeyType
-
Find enum instance corresponding to input value (RsaVerificationKey2018 etc).
Args
val
- input value marking public key type
Returns: the public key type
Expand source code
@staticmethod def get(val: str) -> "PublicKeyType": """ Find enum instance corresponding to input value (RsaVerificationKey2018 etc). Args: val: input value marking public key type Returns: the public key type """ for pktype in PublicKeyType: if val in (pktype.ver_type, pktype.authn_type): return pktype return None
Instance variables
var authn_type : str
-
Accessor for the authentication type identifier.
Expand source code
@property def authn_type(self) -> str: """Accessor for the authentication type identifier.""" return self.value.authn_type
var specifier : str
-
Accessor for the value specifier.
Expand source code
@property def specifier(self) -> str: """Accessor for the value specifier.""" return self.value.specifier
var ver_type : str
-
Accessor for the verification type identifier.
Expand source code
@property def ver_type(self) -> str: """Accessor for the verification type identifier.""" return self.value.ver_type
Methods
def specification(self, val: str) ‑> str
-
Return specifier and input value for use in public key specification.
Args
val
- value of public key
Returns: dict mapping applicable specifier to input value
Expand source code
def specification(self, val: str) -> str: """ Return specifier and input value for use in public key specification. Args: val: value of public key Returns: dict mapping applicable specifier to input value """ return {self.specifier: val}