Skip to content

Commit

Permalink
simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
TheButlah committed May 20, 2024
1 parent 33badb1 commit f58e99d
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 201 deletions.
21 changes: 0 additions & 21 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion apps/legacy_web/backend/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ async fn delete_avatar(

async fn ensure_user_in_db(db: &mut Database, username: &str) {
db.transaction(|data| {
if data.users.get(username).is_none() {
if data.users.contains_key(username) {
println!("creating user: {}", username);
data.users
.insert(username.to_owned(), User::new(username.to_owned()));
Expand Down
2 changes: 1 addition & 1 deletion crates/did-chain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use did_simple::{methods::key::DidKey, methods::DidDyn};
/// This is like an account UUID, it provides a unique identifier for the
/// account. Changing it is impossible.
#[derive(Debug)]
pub struct DidRoot(DidKey);
pub struct DidRoot(pub DidKey);

#[derive(Debug)]
pub struct DidChain {
Expand Down
1 change: 0 additions & 1 deletion crates/did-simple/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ publish = false
thiserror = "1.0.60"
bytes = "1.6.0"
bs58 = "0.5.1"
ref-cast = "1.0.23"

[dev-dependencies]
eyre = "0.6.12"
Expand Down
109 changes: 19 additions & 90 deletions crates/did-simple/src/key_algos.rs
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
}
}
36 changes: 20 additions & 16 deletions crates/did-simple/src/lib.rs
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;
}
Loading

0 comments on commit f58e99d

Please sign in to comment.