use std::{
borrow::Borrow,
cmp::{Ord, PartialOrd},
fmt::{Debug, Display},
hash::Hash,
str::FromStr,
};
use curve25519_dalek::edwards::CompressedEdwardsY;
pub use ed25519_dalek::Signature;
use ed25519_dalek::{SignatureError, SigningKey, VerifyingKey};
use rand_core::CryptoRngCore;
use serde::{Deserialize, Serialize};
#[derive(Clone, Copy, PartialEq, Eq)]
#[repr(transparent)]
pub struct PublicKey(CompressedEdwardsY);
impl Borrow<[u8; 32]> for PublicKey {
fn borrow(&self) -> &[u8; 32] {
self.as_bytes()
}
}
impl PartialOrd for PublicKey {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl Ord for PublicKey {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.0.as_bytes().cmp(other.0.as_bytes())
}
}
pub type NodeId = PublicKey;
impl Hash for PublicKey {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.0.hash(state);
}
}
impl Serialize for PublicKey {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
if serializer.is_human_readable() {
serializer.serialize_str(&self.to_string())
} else {
self.0.as_bytes().serialize(serializer)
}
}
}
impl<'de> Deserialize<'de> for PublicKey {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
if deserializer.is_human_readable() {
let s = String::deserialize(deserializer)?;
Self::from_str(&s).map_err(serde::de::Error::custom)
} else {
let data: [u8; 32] = serde::Deserialize::deserialize(deserializer)?;
Self::try_from(data.as_ref()).map_err(serde::de::Error::custom)
}
}
}
impl PublicKey {
pub fn as_bytes(&self) -> &[u8; 32] {
self.0.as_bytes()
}
pub fn public(&self) -> VerifyingKey {
VerifyingKey::from_bytes(self.0.as_bytes()).expect("already verified")
}
pub fn from_bytes(bytes: &[u8; 32]) -> Result<Self, SignatureError> {
let key = VerifyingKey::from_bytes(bytes)?;
let y = CompressedEdwardsY(key.to_bytes());
Ok(Self(y))
}
pub fn verify(&self, message: &[u8], signature: &Signature) -> Result<(), SignatureError> {
self.public().verify_strict(message, signature)
}
pub fn fmt_short(&self) -> String {
data_encoding::HEXLOWER.encode(&self.as_bytes()[..5])
}
pub const LENGTH: usize = ed25519_dalek::PUBLIC_KEY_LENGTH;
}
impl TryFrom<&[u8]> for PublicKey {
type Error = SignatureError;
#[inline]
fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
let vk = VerifyingKey::try_from(bytes)?;
Ok(Self(CompressedEdwardsY(vk.to_bytes())))
}
}
impl TryFrom<&[u8; 32]> for PublicKey {
type Error = SignatureError;
#[inline]
fn try_from(bytes: &[u8; 32]) -> Result<Self, Self::Error> {
Self::from_bytes(bytes)
}
}
impl AsRef<[u8]> for PublicKey {
fn as_ref(&self) -> &[u8] {
self.as_bytes()
}
}
impl From<VerifyingKey> for PublicKey {
fn from(verifying_key: VerifyingKey) -> Self {
let key = verifying_key.to_bytes();
PublicKey(CompressedEdwardsY(key))
}
}
impl Debug for PublicKey {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"PublicKey({})",
data_encoding::HEXLOWER.encode(self.as_bytes())
)
}
}
impl Display for PublicKey {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", data_encoding::HEXLOWER.encode(self.as_bytes()))
}
}
#[derive(thiserror::Error, Debug)]
pub enum KeyParsingError {
#[error("decoding: {0}")]
Decode(#[from] data_encoding::DecodeError),
#[error("key: {0}")]
Key(#[from] ed25519_dalek::SignatureError),
#[error("invalid length")]
DecodeInvalidLength,
}
impl FromStr for PublicKey {
type Err = KeyParsingError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let bytes = decode_base32_hex(s)?;
Ok(Self::from_bytes(&bytes)?)
}
}
#[derive(Clone)]
pub struct SecretKey {
secret: SigningKey,
}
impl Debug for SecretKey {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "SecretKey(..)")
}
}
impl Display for SecretKey {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}",
data_encoding::HEXLOWER.encode(self.secret.as_bytes())
)
}
}
impl FromStr for SecretKey {
type Err = KeyParsingError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let bytes = decode_base32_hex(s)?;
Ok(SecretKey::from(bytes))
}
}
impl Serialize for SecretKey {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
self.secret.serialize(serializer)
}
}
impl<'de> Deserialize<'de> for SecretKey {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let secret = SigningKey::deserialize(deserializer)?;
Ok(secret.into())
}
}
impl SecretKey {
pub fn public(&self) -> PublicKey {
self.secret.verifying_key().into()
}
pub fn generate<R: CryptoRngCore>(mut csprng: R) -> Self {
let secret = SigningKey::generate(&mut csprng);
Self { secret }
}
pub fn sign(&self, msg: &[u8]) -> Signature {
use ed25519_dalek::Signer;
self.secret.sign(msg)
}
pub fn to_bytes(&self) -> [u8; 32] {
self.secret.to_bytes()
}
pub fn from_bytes(bytes: &[u8; 32]) -> Self {
let secret = SigningKey::from_bytes(bytes);
secret.into()
}
pub fn secret(&self) -> &SigningKey {
&self.secret
}
}
impl From<SigningKey> for SecretKey {
fn from(secret: SigningKey) -> Self {
SecretKey { secret }
}
}
impl From<[u8; 32]> for SecretKey {
fn from(value: [u8; 32]) -> Self {
Self::from_bytes(&value)
}
}
impl TryFrom<&[u8]> for SecretKey {
type Error = SignatureError;
#[inline]
fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
let secret = SigningKey::try_from(bytes)?;
Ok(secret.into())
}
}
fn decode_base32_hex(s: &str) -> Result<[u8; 32], KeyParsingError> {
let mut bytes = [0u8; 32];
let res = if s.len() == PublicKey::LENGTH * 2 {
data_encoding::HEXLOWER.decode_mut(s.as_bytes(), &mut bytes)
} else {
let input = s.to_ascii_uppercase();
let input = input.as_bytes();
if data_encoding::BASE32_NOPAD.decode_len(input.len())? != bytes.len() {
return Err(KeyParsingError::DecodeInvalidLength);
}
data_encoding::BASE32_NOPAD.decode_mut(input, &mut bytes)
};
match res {
Ok(len) => {
if len != PublicKey::LENGTH {
return Err(KeyParsingError::DecodeInvalidLength);
}
}
Err(partial) => return Err(partial.error.into()),
}
Ok(bytes)
}
#[cfg(test)]
mod tests {
use data_encoding::HEXLOWER;
use super::*;
#[test]
fn test_public_key_postcard() {
let public_key =
PublicKey::from_str("ae58ff8833241ac82d6ff7611046ed67b5072d142c588d0063e942d9a75502b6")
.unwrap();
let bytes = postcard::to_stdvec(&public_key).unwrap();
let expected = HEXLOWER
.decode(b"ae58ff8833241ac82d6ff7611046ed67b5072d142c588d0063e942d9a75502b6")
.unwrap();
assert_eq!(bytes, expected);
}
#[test]
fn public_key_postcard() {
let key = PublicKey::from_bytes(&[0; 32]).unwrap();
let bytes = postcard::to_stdvec(&key).unwrap();
let key2: PublicKey = postcard::from_bytes(&bytes).unwrap();
assert_eq!(key, key2);
}
#[test]
fn public_key_json() {
let key = PublicKey::from_bytes(&[0; 32]).unwrap();
let bytes = serde_json::to_string(&key).unwrap();
let key2: PublicKey = serde_json::from_str(&bytes).unwrap();
assert_eq!(key, key2);
}
#[test]
fn test_display_from_str() {
let key = SecretKey::generate(&mut rand::thread_rng());
assert_eq!(
SecretKey::from_str(&key.to_string()).unwrap().to_bytes(),
key.to_bytes()
);
assert_eq!(
PublicKey::from_str(&key.public().to_string()).unwrap(),
key.public()
);
}
#[test]
fn test_regression_parse_node_id_panic() {
let not_a_node_id = "foobarbaz";
assert!(PublicKey::from_str(not_a_node_id).is_err());
}
}