Skip to content

Commit

Permalink
chore: alow setting SU_WALLET directly
Browse files Browse the repository at this point in the history
  • Loading branch information
dtfiedler committed Apr 30, 2024
1 parent cb65448 commit 6ad5971
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 43 deletions.
4 changes: 3 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:-}
Expand Down
5 changes: 3 additions & 2 deletions servers/su/src/domain/clients/gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
47 changes: 13 additions & 34 deletions servers/su/src/domain/clients/wallet.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
use std::fs::File;
use std::io::Read;

use base64_url;
use jsonwebkey::JsonWebKey;
use rsa::{pkcs8::DecodePrivateKey, PublicKeyParts, RsaPrivateKey};
Expand All @@ -14,41 +11,23 @@ pub struct FileWallet;
impl Wallet for FileWallet {
fn wallet_json(&self) -> Result<String, String> {
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<String, String> {
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()));
}
}
31 changes: 27 additions & 4 deletions servers/su/src/domain/config.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
use std::env;

use std::fs;
use dotenv::dotenv;

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<String>,
su_wallet: Option<String>,
}

impl AoConfig {
Expand All @@ -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::<serde_json::Value>(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<String> {
self.su_wallet_path.clone()
}
fn su_wallet(&self) -> Option<String> {
self.su_wallet.clone()
}
fn upload_node_url(&self) -> String {
self.upload_node_url.clone()
}
Expand Down
3 changes: 2 additions & 1 deletion servers/su/src/domain/core/dal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ pub trait ScheduleProvider {
}

pub trait Config: Send + Sync {
fn su_wallet_path(&self) -> String;
fn su_wallet_path(&self) -> Option<String>;
fn su_wallet(&self) -> Option<String>;
fn upload_node_url(&self) -> String;
fn gateway_url(&self) -> String;
fn mode(&self) -> String;
Expand Down
2 changes: 1 addition & 1 deletion servers/su/src/domain/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub async fn init_deps(mode: Option<String>) -> Arc<Deps> {
);

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);

Expand Down

0 comments on commit 6ad5971

Please sign in to comment.