diff --git a/src/protocol/initialize.rs b/src/protocol/initialize.rs index 4ee6b2b..5053570 100644 --- a/src/protocol/initialize.rs +++ b/src/protocol/initialize.rs @@ -4,12 +4,41 @@ use super::*; impl super::Client { pub async fn initialize(mut socket: WebSocket) -> anyhow::Result { - if let Some(ServerMessage::Initialize()) = Client::read_socket(&mut socket).await? {} + let Some(ServerMethod::Initialize { + public_key, + timestamp, + signature, + }) = Client::read_socket(&mut socket).await? + else { + Client::send_socket( + &mut socket, + ClientMethod::Error { + error: Cow::Borrowed( + "Failed to initialize, unexpected method, expected: initialize", + ), + }, + ) + .await?; + + return Err(anyhow::anyhow!( + "Failed to initialize: Client sent the wrong method" + )); + }; + + Client::send_socket( + &mut socket, + ClientMethod::Initialized { + public_key, + timestamp, + signature, + }, + ) + .await?; Ok(Self { socket, meta: super::ClientMeta {}, - pub_key: String::new(), + public_key: String::new(), }) } } diff --git a/src/protocol/mod.rs b/src/protocol/mod.rs index 43b30f3..47e4e90 100644 --- a/src/protocol/mod.rs +++ b/src/protocol/mod.rs @@ -1,3 +1,5 @@ +use std::borrow::Cow; + use axum::extract::ws::{Message, Utf8Bytes, WebSocket}; use serde::{Deserialize, Serialize}; @@ -6,24 +8,41 @@ pub mod initialize; pub struct Client { pub socket: WebSocket, pub meta: ClientMeta, - pub pub_key: String, + pub public_key: String, } #[derive(Debug, Clone, Serialize, Deserialize)] pub struct ClientMeta {} #[derive(Debug, Clone, Serialize, Deserialize)] -pub enum ClientMessage<'a> { - Error(&'a str), +#[serde(tag = "method")] +pub enum ClientMethod { + Initialized { + public_key: String, + timestamp: u64, + signature: String, + }, + + #[serde(rename = "error")] + Error { error: Cow<'static, str> }, } #[derive(Debug, Clone, Serialize, Deserialize)] -pub enum ServerMessage { - Initialize(), +#[serde(tag = "method")] +pub enum ServerMethod { + Initialize { + public_key: String, + timestamp: u64, + signature: String, + }, + + Error { + error: String, + }, } impl Client { - pub async fn read_socket(socket: &mut WebSocket) -> anyhow::Result> { + pub async fn read_socket(socket: &mut WebSocket) -> anyhow::Result> { match socket.recv().await.transpose()? { Some(Message::Text(text)) => { if let Ok(msg) = serde_json::from_str(&text.to_string()) { @@ -31,7 +50,9 @@ impl Client { } else { Client::send_socket( socket, - ClientMessage::Error("Unable to parse message: {text}"), + ClientMethod::Error { + error: Cow::Borrowed("Unable to parse message: {text}"), + }, ) .await?; @@ -51,10 +72,7 @@ impl Client { } } - pub async fn send_socket<'a>( - socket: &mut WebSocket, - message: ClientMessage<'a>, - ) -> anyhow::Result<()> { + pub async fn send_socket(socket: &mut WebSocket, message: ClientMethod) -> anyhow::Result<()> { socket .send(Message::Text(Utf8Bytes::from(serde_json::to_string( &message, @@ -64,11 +82,11 @@ impl Client { Ok(()) } - pub async fn read(&mut self) -> anyhow::Result> { + pub async fn read(&mut self) -> anyhow::Result> { Self::read_socket(&mut self.socket).await } - pub async fn send<'a>(&mut self, message: ClientMessage<'a>) -> anyhow::Result<()> { + pub async fn send(&mut self, message: ClientMethod) -> anyhow::Result<()> { Self::send_socket(&mut self.socket, message).await } }