-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
98 additions
and
201 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,109 +1,38 @@ | ||
use ref_cast::RefCast; | ||
|
||
use crate::varint::encode_varint; | ||
|
||
/// A key algorithm. | ||
pub trait KeyAlgo { | ||
type PubKey: AsRef<[u8]>; | ||
fn pub_key_size(&self) -> usize; | ||
fn multicodec_value(&self) -> u16; | ||
} | ||
|
||
/// A key algorithm that is known statically, at compile time. | ||
pub trait StaticKeyAlgo: KeyAlgo { | ||
const PUB_KEY_SIZE: usize; | ||
const MULTICODEC_VALUE: u16; | ||
const MULTICODEC_VALUE_ENCODED: &'static [u8] = | ||
encode_varint(Self::MULTICODEC_VALUE).as_slice(); | ||
type PubKeyArray: AsRef<[u8]>; | ||
} | ||
|
||
impl<T: StaticKeyAlgo> KeyAlgo for T { | ||
type PubKey = T::PubKeyArray; | ||
|
||
fn pub_key_size(&self) -> usize { | ||
Self::PUB_KEY_SIZE | ||
} | ||
|
||
fn multicodec_value(&self) -> u16 { | ||
Self::MULTICODEC_VALUE | ||
} | ||
} | ||
|
||
#[derive(RefCast)] | ||
#[repr(transparent)] | ||
pub struct PubKey<T: StaticKeyAlgo>(pub T::PubKeyArray); | ||
|
||
#[derive(Debug, Eq, PartialEq, Hash, Clone, Copy)] | ||
pub struct Ed25519; | ||
|
||
impl StaticKeyAlgo for Ed25519 { | ||
const PUB_KEY_SIZE: usize = 32; | ||
const MULTICODEC_VALUE: u16 = 0xED; | ||
type PubKeyArray = [u8; Self::PUB_KEY_SIZE]; | ||
} | ||
|
||
impl PartialEq<Ed25519> for DynKeyAlgo { | ||
fn eq(&self, _other: &Ed25519) -> bool { | ||
*self == DynKeyAlgo::Ed25519 | ||
} | ||
} | ||
|
||
#[derive(Debug, Eq, PartialEq, Hash, Clone, Copy)] | ||
pub enum DynKeyAlgo { | ||
pub enum KeyAlgo { | ||
Ed25519, | ||
} | ||
|
||
impl KeyAlgo for DynKeyAlgo { | ||
type PubKey = DynPubKey; | ||
|
||
fn pub_key_size(&self) -> usize { | ||
match self { | ||
Self::Ed25519 => Ed25519::PUB_KEY_SIZE, | ||
} | ||
} | ||
|
||
fn multicodec_value(&self) -> u16 { | ||
impl KeyAlgo { | ||
pub fn pub_key_len(&self) -> usize { | ||
match self { | ||
Self::Ed25519 => Ed25519::MULTICODEC_VALUE, | ||
Self::Ed25519 => Ed25519::PUB_KEY_LEN, | ||
} | ||
} | ||
} | ||
|
||
#[non_exhaustive] | ||
pub enum DynPubKey { | ||
Ed25519(PubKey<Ed25519>), | ||
} | ||
|
||
impl From<PubKey<Ed25519>> for DynPubKey { | ||
fn from(value: PubKey<Ed25519>) -> Self { | ||
Self::Ed25519(value) | ||
} | ||
} | ||
// ---- internal code ---- | ||
|
||
impl AsRef<[u8]> for DynPubKey { | ||
fn as_ref(&self) -> &[u8] { | ||
match self { | ||
Self::Ed25519(k) => k.0.as_ref(), | ||
} | ||
} | ||
/// A key algorithm that is known statically, at compile time. | ||
pub(crate) trait StaticKeyAlgo { | ||
const PUB_KEY_LEN: usize; | ||
const MULTICODEC_VALUE: u16; | ||
const MULTICODEC_VALUE_ENCODED: &'static [u8] = | ||
encode_varint(Self::MULTICODEC_VALUE).as_slice(); | ||
} | ||
|
||
#[non_exhaustive] | ||
pub enum DynPubKeyRef<'a> { | ||
Ed25519(&'a PubKey<Ed25519>), | ||
} | ||
#[derive(Debug, Eq, PartialEq, Hash, Clone, Copy)] | ||
pub(crate) struct Ed25519; | ||
|
||
impl<'a> From<&'a PubKey<Ed25519>> for DynPubKeyRef<'a> { | ||
fn from(value: &'a PubKey<Ed25519>) -> Self { | ||
Self::Ed25519(value) | ||
} | ||
impl StaticKeyAlgo for Ed25519 { | ||
const PUB_KEY_LEN: usize = 32; | ||
const MULTICODEC_VALUE: u16 = 0xED; | ||
} | ||
|
||
impl AsRef<[u8]> for DynPubKeyRef<'_> { | ||
fn as_ref(&self) -> &[u8] { | ||
match *self { | ||
Self::Ed25519(k) => k.0.as_ref(), | ||
} | ||
impl PartialEq<Ed25519> for KeyAlgo { | ||
fn eq(&self, _other: &Ed25519) -> bool { | ||
*self == KeyAlgo::Ed25519 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,39 @@ | ||
//! A Decentralized Identifier (aka [DID][spec]), is a globally unique | ||
//! identifier that provides a general purpose way of looking up public keys | ||
//! associated with the globally unique identifier. | ||
//! associated with the identifier. | ||
//! | ||
//! This means that unlike a UUID, someone can prove that they own a DID, and | ||
//! you can encrypt messages using DIDs! This makes DIDs strictly more useful | ||
//! than traditional UUIDs as account identifiers and are very useful for | ||
//! building federated or decentralized services. | ||
//! This means that ownership of a UUID can be proven and that support for | ||
//! common cryptography operations such as signing, verifying, and encrypting | ||
//! messages is possible. This makes DIDs strictly more useful than traditional | ||
//! UUIDs as account identifiers and are very useful for building federated or | ||
//! decentralized services. | ||
//! | ||
//! Unlike traditional centralized accounts, services that use DIDs give users | ||
//! custody over their account identity. Authentication of users can happen | ||
//! without the need for a centralized service or database. Instead, whoever | ||
//! holds the private keys associated with a DID will be able to authenticate as | ||
//! the account owner. | ||
//! Services that use DIDs give users self-custody over their account identity. | ||
//! Authentication of users can happen without the need for a centralized | ||
//! authentication service or user database. Instead, whoever holds the private | ||
//! keys associated with a DID will be able to authenticate as the account owner. | ||
//! | ||
//! This gives users the ability to maintain the same account handles/identities | ||
//! across multiple separate services (or migrate homeservers in a federated | ||
//! system) without having to create a new, different, account or identity each | ||
//! time. | ||
//! system) without having to create a different account or identity for each | ||
//! service. | ||
//! | ||
//! [spec]: https://www.w3.org/TR/did-core/ | ||
|
||
#![forbid(unsafe_code)] | ||
|
||
use std::str::FromStr; | ||
|
||
pub mod key_algos; | ||
pub(crate) mod key_algos; | ||
pub mod methods; | ||
pub mod uri; | ||
pub mod url; | ||
pub mod utf8bytes; | ||
pub mod varint; | ||
mod varint; | ||
|
||
pub use crate::key_algos::KeyAlgo; | ||
pub use crate::methods::DidDyn; | ||
pub use crate::url::DidUrl; | ||
|
||
pub trait Did: FromStr { | ||
fn uri(&self) -> self::uri::DidUri; | ||
fn url(&self) -> self::url::DidUrl; | ||
} |
Oops, something went wrong.