From 380e02e4c30738437af7877e48dd5161c5d71c14 Mon Sep 17 00:00:00 2001 From: Zshan0 Date: Mon, 23 May 2022 03:18:11 +0530 Subject: [PATCH] Hex validation for `PrivateNoiseKey`. --- src/installer/step/mod.rs | 20 ++++++++++++++++++-- src/installer/view.rs | 4 +++- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/installer/step/mod.rs b/src/installer/step/mod.rs index 82ef5e08..eea3d24f 100644 --- a/src/installer/step/mod.rs +++ b/src/installer/step/mod.rs @@ -148,11 +148,27 @@ impl Step for DefinePrivateNoiseKey { fn update(&mut self, message: Message) { if let Message::PrivateNoiseKey(msg) = message { self.key.value = msg; - self.key.valid = self.key.value.as_bytes().len() == 64; + + self.key.valid = true; + if let Ok(bytes) = Vec::from_hex(&self.key.value) { + if bytes.len() != 32 { + self.key.valid = false; + } + } else { + self.key.valid = false; + } } } fn apply(&mut self, ctx: &mut Context, _config: &mut config::Config) -> bool { - self.key.valid = self.key.value.as_bytes().len() == 64; + self.key.valid = true; + if let Ok(bytes) = Vec::from_hex(&self.key.value) { + if bytes.len() != 32 { + self.key.valid = false; + } + } else { + self.key.valid = false; + } + ctx.private_noise_key = self.key.value.clone(); self.key.valid } diff --git a/src/installer/view.rs b/src/installer/view.rs index 85539bc8..15949609 100644 --- a/src/installer/view.rs +++ b/src/installer/view.rs @@ -233,7 +233,9 @@ impl DefinePrivateNoiseKey { .push( Column::new().spacing(10).push( form::Form::new(&mut self.key_input, "", key, Message::PrivateNoiseKey) - .warning("Key must be a 64 characters hex encoded string") + .warning( + "Please enter a 32 bytes noise private key that is hex encoded", + ) .size(20) .padding(10) .render(),