👕 refactor (ptth_relay): use the new blake3 deserializer

main
_ 2020-12-12 15:50:38 +00:00
parent 9bc4b57058
commit b43106393a
2 changed files with 16 additions and 9 deletions

View File

@ -23,6 +23,16 @@ use crate::errors::ConfigError;
pub struct BlakeHashWrapper (blake3::Hash); pub struct BlakeHashWrapper (blake3::Hash);
impl BlakeHashWrapper {
pub fn from_key (bytes: &[u8]) -> Self {
Self (blake3::hash (bytes))
}
pub fn encode_base64 (&self) -> String {
base64::encode (self.as_bytes ())
}
}
impl Deref for BlakeHashWrapper { impl Deref for BlakeHashWrapper {
type Target = blake3::Hash; type Target = blake3::Hash;
@ -69,7 +79,7 @@ pub mod file {
#[derive (Deserialize)] #[derive (Deserialize)]
pub struct Server { pub struct Server {
pub name: String, pub name: String,
pub tripcode: String, pub tripcode: BlakeHashWrapper,
pub display_name: Option <String>, pub display_name: Option <String>,
} }
@ -110,13 +120,8 @@ impl TryFrom <file::Server> for Server {
type Error = ConfigError; type Error = ConfigError;
fn try_from (f: file::Server) -> Result <Self, Self::Error> { fn try_from (f: file::Server) -> Result <Self, Self::Error> {
let bytes: Vec <u8> = base64::decode (f.tripcode)?;
let bytes: [u8; 32] = (&bytes [..]).try_into ().map_err (|_| ConfigError::TripcodeBadLength)?;
let tripcode = blake3::Hash::from (bytes);
Ok (Self { Ok (Self {
tripcode, tripcode: *f.tripcode,
display_name: f.display_name, display_name: f.display_name,
}) })
} }

View File

@ -18,6 +18,8 @@ fn end_to_end () {
use reqwest::Client; use reqwest::Client;
use tracing::{debug, info}; use tracing::{debug, info};
use ptth_relay::config::BlakeHashWrapper;
// Prefer this form for tests, since all tests share one process // Prefer this form for tests, since all tests share one process
// and we don't care if another test already installed a subscriber. // and we don't care if another test already installed a subscriber.
@ -28,8 +30,8 @@ fn end_to_end () {
rt.block_on (async { rt.block_on (async {
let server_name = "aliens_wildland"; let server_name = "aliens_wildland";
let api_key = "AnacondaHardcoverGrannyUnlatchLankinessMutate"; let api_key = "AnacondaHardcoverGrannyUnlatchLankinessMutate";
let tripcode = base64::encode (blake3::hash (api_key.as_bytes ()).as_bytes ()); let tripcode = BlakeHashWrapper::from_key (api_key.as_bytes ());
debug! ("Relay is expecting tripcode {}", tripcode); debug! ("Relay is expecting tripcode {}", tripcode.encode_base64 ());
let config_file = ptth_relay::config::file::Config { let config_file = ptth_relay::config::file::Config {
port: None, port: None,
servers: vec! [ servers: vec! [