From 6ad59710909f478952a555557579bc0da7e7d05c Mon Sep 17 00:00:00 2001 From: dtfiedler Date: Tue, 30 Apr 2024 17:42:56 -0600 Subject: [PATCH] chore: alow setting SU_WALLET directly --- docker-compose.yml | 4 +- servers/su/src/domain/clients/gateway.rs | 5 ++- servers/su/src/domain/clients/wallet.rs | 47 +++++++----------------- servers/su/src/domain/config.rs | 31 ++++++++++++++-- servers/su/src/domain/core/dal.rs | 3 +- servers/su/src/domain/mod.rs | 2 +- 6 files changed, 49 insertions(+), 43 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index da50273ea..3c10f7699 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -40,13 +40,15 @@ services: su: image: ghcr.io/ar-io/ao-su:latest + build: + context: servers/su ports: - 4003:9000 volumes: - ./wallets/ao-wallet.json:/usr/app/ao-wallet.json environment: - DATABASE_URL=${DATABASE_URL:-postgresql://su_user:su_pass@su-database/su_db} - - GATEWAY_URL=${GATEWAY_URL:-http://arlocal} + - GATEWAY_URL=http://arlocal - UPLOAD_NODE_URL=${UPLOAD_NODE_URL:-https://turbo.ardrive.dev} - MODE=${MODE:-su} - SCHEDULER_LIST_PATH=${SCHEDULER_LIST_PATH:-} diff --git a/servers/su/src/domain/clients/gateway.rs b/servers/su/src/domain/clients/gateway.rs index d52182912..3c638c7d3 100644 --- a/servers/su/src/domain/clients/gateway.rs +++ b/servers/su/src/domain/clients/gateway.rs @@ -74,8 +74,9 @@ impl ArweaveGateway { Err(_) if attempt < 4 => { // Log the failed attempt and wait before retrying println!( - "Attempt {}: Failed to fetch network info, retrying...", - attempt + 1 + "Attempt {}: Failed to fetch network info from {}, retrying...", + attempt + 1, + gateway_url ); sleep(Duration::from_secs(1)).await; } diff --git a/servers/su/src/domain/clients/wallet.rs b/servers/su/src/domain/clients/wallet.rs index 7d84a6008..759963fb9 100644 --- a/servers/su/src/domain/clients/wallet.rs +++ b/servers/su/src/domain/clients/wallet.rs @@ -1,6 +1,3 @@ -use std::fs::File; -use std::io::Read; - use base64_url; use jsonwebkey::JsonWebKey; use rsa::{pkcs8::DecodePrivateKey, PublicKeyParts, RsaPrivateKey}; @@ -14,41 +11,23 @@ pub struct FileWallet; impl Wallet for FileWallet { fn wallet_json(&self) -> Result { let config = AoConfig::new(Some("su".to_string())).expect("Failed to read configuration"); - let file_path = config.su_wallet_path; - let mut file = match File::open(&file_path) { - Ok(f) => f, - Err(_) => return Err("failed to read wallet file".to_string()), - }; - let mut key_json = String::new(); - if let Ok(_) = file.read_to_string(&mut key_json) { - return Ok(key_json); - } else { - return Err("Failed to read wallet from file system".to_string()); - } + let wallet = config.su_wallet(); + return Ok(wallet); } fn wallet_address(&self) -> Result { let config = AoConfig::new(Some("su".to_string())).expect("Failed to read configuration"); - let file_path = config.su_wallet_path; - let mut file = match File::open(&file_path) { - Ok(f) => f, - Err(_) => return Err("failed to read wallet file".to_string()), + let wallet = config.su_wallet(); + let jwk: JsonWebKey = match serde_json::from_str(&wallet) { + Ok(s) => s, + Err(_) => return Err("failed to parse the wallet file".to_string()), }; - let mut key_json = String::new(); - if let Ok(_) = file.read_to_string(&mut key_json) { - let jwk: JsonWebKey = match serde_json::from_str(&key_json) { - Ok(s) => s, - Err(_) => return Err("failed to parse the wallet file".to_string()), - }; - let pem = jwk.key.to_pem(); - let priv_key = RsaPrivateKey::from_pkcs8_pem(&pem).unwrap(); - let modulus = priv_key.to_public_key().n().to_bytes_be(); - let keypair_modulus = modulus.to_vec(); - let mut context = sha2::Sha256::new(); - context.update(&keypair_modulus); - return Ok(base64_url::encode(&context.finalize().to_vec())); - } else { - return Err("Failed to read wallet from file system".to_string()); - } + let pem = jwk.key.to_pem(); + let priv_key = RsaPrivateKey::from_pkcs8_pem(&pem).unwrap(); + let modulus = priv_key.to_public_key().n().to_bytes_be(); + let keypair_modulus = modulus.to_vec(); + let mut context = sha2::Sha256::new(); + context.update(&keypair_modulus); + return Ok(base64_url::encode(&context.finalize().to_vec())); } } diff --git a/servers/su/src/domain/config.rs b/servers/su/src/domain/config.rs index b386ce8f4..cd7e554d6 100644 --- a/servers/su/src/domain/config.rs +++ b/servers/su/src/domain/config.rs @@ -1,5 +1,5 @@ use std::env; - +use std::fs; use dotenv::dotenv; use crate::domain::Config; @@ -7,11 +7,12 @@ use crate::domain::Config; #[derive(Debug)] pub struct AoConfig { pub database_url: String, - pub su_wallet_path: String, pub gateway_url: String, pub upload_node_url: String, pub mode: String, pub scheduler_list_path: String, + su_wallet_path: Option, + su_wallet: Option, } impl AoConfig { @@ -23,19 +24,41 @@ impl AoConfig { }; Ok(AoConfig { database_url: env::var("DATABASE_URL")?, - su_wallet_path: env::var("SU_WALLET_PATH")?, + su_wallet_path: env::var("SU_WALLET_PATH").ok(), + su_wallet: env::var("SU_WALLET").ok(), gateway_url: env::var("GATEWAY_URL")?, upload_node_url: env::var("UPLOAD_NODE_URL")?, mode: mode_out, scheduler_list_path: env::var("SCHEDULER_LIST_PATH")?, }) } + + pub fn su_wallet(&self) -> String { + if let Some(wallet) = self.su_wallet.as_ref() { + // If su_wallet is present, parse it and return as JSON string + match serde_json::from_str::(wallet) { + Ok(parsed) => parsed.to_string(), + Err(err) => panic!("Failed to parse SU_WALLET JSON: {}", err), + } + } else if let Some(wallet_path) = self.su_wallet_path.as_ref() { + // If su_wallet is not present but su_wallet_path is, read from the file + match fs::read_to_string(wallet_path) { + Ok(content) => content.trim().to_string(), + Err(_) => panic!("Failed to read SU_WALLET_PATH from file system"), + } + } else { + panic!("Neither SU_WALLET nor SU_WALLET_PATH is set. Please set one."); + } + } } impl Config for AoConfig { - fn su_wallet_path(&self) -> String { + fn su_wallet_path(&self) -> Option { self.su_wallet_path.clone() } + fn su_wallet(&self) -> Option { + self.su_wallet.clone() + } fn upload_node_url(&self) -> String { self.upload_node_url.clone() } diff --git a/servers/su/src/domain/core/dal.rs b/servers/su/src/domain/core/dal.rs index cf1090aae..0a1d612d1 100644 --- a/servers/su/src/domain/core/dal.rs +++ b/servers/su/src/domain/core/dal.rs @@ -52,7 +52,8 @@ pub trait ScheduleProvider { } pub trait Config: Send + Sync { - fn su_wallet_path(&self) -> String; + fn su_wallet_path(&self) -> Option; + fn su_wallet(&self) -> Option; fn upload_node_url(&self) -> String; fn gateway_url(&self) -> String; fn mode(&self) -> String; diff --git a/servers/su/src/domain/mod.rs b/servers/su/src/domain/mod.rs index a9bf32561..af4e336a1 100644 --- a/servers/su/src/domain/mod.rs +++ b/servers/su/src/domain/mod.rs @@ -42,7 +42,7 @@ pub async fn init_deps(mode: Option) -> Arc { ); let signer = - Arc::new(ArweaveSigner::new(&config.su_wallet_path).expect("Invalid su wallet path")); + Arc::new(ArweaveSigner::new(&config.su_wallet()).expect("Invalid su wallet provided")); let wallet = Arc::new(FileWallet);