From 02b194bde085d6eb606a69ca4ef1afc2d0c9e8b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bogdan-=C8=98tefan=20Neac=C5=9Fu?= Date: Thu, 4 Jul 2024 10:40:53 +0000 Subject: [PATCH] Function to create AuthReq --- Cargo.lock | 1 + common/authenticator-requests/Cargo.toml | 1 + .../authenticator-requests/src/v1/request.rs | 20 +++++++++++++++++++ 3 files changed, 22 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 1fc2048511..2c913a0efa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3992,6 +3992,7 @@ dependencies = [ "bincode", "nym-sphinx", "nym-wireguard-types", + "rand 0.8.5", "serde", ] diff --git a/common/authenticator-requests/Cargo.toml b/common/authenticator-requests/Cargo.toml index 8e79945837..03fbf4f34f 100644 --- a/common/authenticator-requests/Cargo.toml +++ b/common/authenticator-requests/Cargo.toml @@ -10,6 +10,7 @@ license.workspace = true [dependencies] bincode = { workspace = true } +rand = "0.8.5" serde = { workspace = true, features = ["derive"] } nym-sphinx = { path = "../nymsphinx" } diff --git a/common/authenticator-requests/src/v1/request.rs b/common/authenticator-requests/src/v1/request.rs index 8e97fde189..bc50b87465 100644 --- a/common/authenticator-requests/src/v1/request.rs +++ b/common/authenticator-requests/src/v1/request.rs @@ -7,6 +7,14 @@ use serde::{Deserialize, Serialize}; use crate::make_bincode_serializer; +use super::VERSION; + +fn generate_random() -> u64 { + use rand::RngCore; + let mut rng = rand::rngs::OsRng; + rng.next_u64() +} + #[derive(Clone, Debug, Serialize, Deserialize)] pub struct AuthenticatorRequest { pub version: u8, @@ -21,6 +29,18 @@ impl AuthenticatorRequest { use bincode::Options; make_bincode_serializer().deserialize(&message.message) } + + pub fn new_initial_request(init_message: InitMessage, reply_to: Recipient) -> (Self, u64) { + let request_id = generate_random(); + ( + Self { + version: VERSION, + data: AuthenticatorRequestData::Initial(init_message), + reply_to, + }, + request_id, + ) + } } #[derive(Clone, Debug, Serialize, Deserialize)]