diff --git a/tester/Cargo.toml b/tester/Cargo.toml index ac6ef90..9400bd2 100644 --- a/tester/Cargo.toml +++ b/tester/Cargo.toml @@ -33,10 +33,6 @@ sphinx-crypter = { git = "https://github.com/stakwork/sphinx-rs.git" } name = "config" path = "src/config.rs" -[[bin]] -name = "wifi" -path = "src/wifi.rs" - [[bin]] name = "config-server" path = "src/server.rs" diff --git a/tester/src/config.rs b/tester/src/config.rs index 2c977c5..6eb6a01 100644 --- a/tester/src/config.rs +++ b/tester/src/config.rs @@ -4,11 +4,14 @@ use serde::{Deserialize, Serialize}; use sphinx_crypter::chacha::{encrypt, MSG_LEN, NONCE_LEN}; use sphinx_crypter::ecdh::{derive_shared_secret_from_slice, PUBLIC_KEY_LEN}; use sphinx_crypter::secp256k1::Secp256k1; +use sphinx_signer::sphinx_glyph::control::Config; use std::convert::TryInto; use std::env; use std::time::Duration; const URL: &str = "http://192.168.71.1"; +// Set to true to only reset wifi credentials, and not transmit the seed again +const WIFI_RESET: bool = false; #[derive(Clone, Debug, Serialize, Deserialize)] pub struct EcdhBody { @@ -59,32 +62,42 @@ async fn main() -> anyhow::Result<()> { .build() .expect("couldnt build reqwest client"); - let res = client - .get(format!("{}/{}", url, "ecdh")) - .header("Content-Type", "application/json") - .send() - .await?; - let their_ecdh: EcdhBody = res.json().await?; - let their_pk = hex::decode(their_ecdh.pubkey)?; + let conf_string = if !WIFI_RESET { + let res = client + .get(format!("{}/{}", url, "ecdh")) + .header("Content-Type", "application/json") + .send() + .await?; + let their_ecdh: EcdhBody = res.json().await?; + let their_pk = hex::decode(their_ecdh.pubkey)?; - let their_pk_bytes: [u8; PUBLIC_KEY_LEN] = their_pk[..PUBLIC_KEY_LEN].try_into()?; - let shared_secret = derive_shared_secret_from_slice(their_pk_bytes, sk1.secret_bytes())?; + let their_pk_bytes: [u8; PUBLIC_KEY_LEN] = their_pk[..PUBLIC_KEY_LEN].try_into()?; + let shared_secret = derive_shared_secret_from_slice(their_pk_bytes, sk1.secret_bytes())?; - let mut nonce_end = [0; NONCE_LEN]; - OsRng.fill_bytes(&mut nonce_end); - let cipher = encrypt(seed, shared_secret, nonce_end)?; + let mut nonce_end = [0; NONCE_LEN]; + OsRng.fill_bytes(&mut nonce_end); + let cipher = encrypt(seed, shared_secret, nonce_end)?; - let cipher_seed = hex::encode(cipher); - let config = ConfigBody { - seed: cipher_seed, - ssid, - pass, - broker, - network, - pubkey: hex::encode(pk1.serialize()), + let cipher_seed = hex::encode(cipher); + let config = ConfigBody { + seed: cipher_seed, + ssid, + pass, + broker, + network, + pubkey: hex::encode(pk1.serialize()), + }; + serde_json::to_string(&config)? + } else { + let config = Config { + ssid, + pass, + broker, + network, + }; + serde_json::to_string(&config)? }; - let conf_string = serde_json::to_string(&config)?; let conf_encoded = urlencoding::encode(&conf_string).to_owned(); let res2 = client diff --git a/tester/src/wifi.rs b/tester/src/wifi.rs deleted file mode 100644 index 843b100..0000000 --- a/tester/src/wifi.rs +++ /dev/null @@ -1,61 +0,0 @@ -use dotenv::dotenv; -use serde::{Deserialize, Serialize}; -use std::env; -use std::time::Duration; - -use sphinx_signer::sphinx_glyph::control::Config; - -const URL: &str = "http://192.168.71.1"; - -#[derive(Clone, Debug, Serialize, Deserialize)] -pub struct ConfigResponse { - pub success: bool, -} - -#[tokio::main] -async fn main() -> anyhow::Result<()> { - dotenv().ok(); - - let url: String = env::var("URL").unwrap_or(URL.to_string()); - - let ssid: String = env::var("SSID").expect("no ssid"); - let pass: String = env::var("PASS").expect("no pass"); - let broker: String = env::var("BROKER").expect("no broker"); - let network: String = env::var("NETWORK").unwrap_or("regtest".to_string()); - if !(network == "bitcoin" - || network == "mainnet" - || network == "testnet" - || network == "signet" - || network == "regtest") - { - panic!("invalid network string"); - } - println!("network {:?}", network); - - let client = reqwest::Client::builder() - .timeout(Duration::from_secs(10)) - .build() - .expect("couldnt build reqwest client"); - - let config = Config { - ssid, - pass, - broker, - network, - }; - - let conf_string = serde_json::to_string(&config)?; - let conf_encoded = urlencoding::encode(&conf_string).to_owned(); - - let res = client - .post(format!("{}/{}={}", url, "config?config", conf_encoded)) - .send() - .await?; - let conf_res: ConfigResponse = res.json().await?; - - if conf_res.success { - println!("SUCCESS!") - } - - Ok(()) -}