node: Implement binary encoding for messages

Signed-off-by: Alexis Sellier <self@cloudhead.io>
This commit is contained in:
Alexis Sellier
2022-09-13 10:16:05 +02:00
parent c7f78eafb6
commit b3d3077ccc
11 changed files with 638 additions and 88 deletions
+26 -3
View File
@@ -74,13 +74,13 @@ impl std::hash::Hash for PublicKey {
impl fmt::Display for PublicKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.encode())
write!(f, "{}", self.to_human())
}
}
impl From<PublicKey> for String {
fn from(other: PublicKey) -> Self {
other.encode()
other.to_human()
}
}
@@ -96,8 +96,16 @@ impl From<ed25519::VerificationKey> for PublicKey {
}
}
impl TryFrom<[u8; 32]> for PublicKey {
type Error = ed25519::Error;
fn try_from(other: [u8; 32]) -> Result<Self, Self::Error> {
Ok(Self(ed25519::VerificationKey::try_from(other)?))
}
}
impl PublicKey {
pub fn encode(&self) -> String {
pub fn to_human(&self) -> String {
multibase::encode(multibase::Base::Base58Btc, &self.0)
}
}
@@ -131,3 +139,18 @@ impl Deref for PublicKey {
&self.0
}
}
#[cfg(test)]
mod test {
use crate::crypto::PublicKey;
use quickcheck_macros::quickcheck;
use std::str::FromStr;
#[quickcheck]
fn prop_encode_decode(input: PublicKey) {
let encoded = input.to_string();
let decoded = PublicKey::from_str(&encoded).unwrap();
assert_eq!(input, decoded);
}
}