Skip to content

Commit

Permalink
Rm setup only option, tidy
Browse files Browse the repository at this point in the history
  • Loading branch information
ameba23 committed Dec 16, 2024
1 parent 87c6afd commit e71cc72
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 89 deletions.
62 changes: 1 addition & 61 deletions crates/threshold-signature-server/src/helpers/launch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ use entropy_kvdb::{
};
use entropy_shared::NETWORK_PARENT_KEY;
use serde::Deserialize;
use serde_json::json;
use subxt::ext::sp_core::{
crypto::{AccountId32, Ss58Codec},
sr25519, Pair,
};

pub const DEFAULT_MNEMONIC: &str =
"alarm mutual concert decrease hurry invest culture survey diagram crash snap click";
Expand All @@ -51,16 +46,7 @@ pub const LATEST_BLOCK_NUMBER_PROACTIVE_REFRESH: &str = "LATEST_BLOCK_NUMBER_PRO
#[cfg(any(test, feature = "test_helpers"))]
pub const DEFAULT_ENDPOINT: &str = "ws://localhost:9944";

pub const FORBIDDEN_KEYS: [&str; 4] = [
FORBIDDEN_KEY_MNEMONIC,
FORBIDDEN_KEY_SHARED_SECRET,
FORBIDDEN_KEY_DIFFIE_HELLMAN_PUBLIC,
NETWORK_PARENT_KEY,
];

pub const FORBIDDEN_KEY_MNEMONIC: &str = "MNEMONIC";
pub const FORBIDDEN_KEY_SHARED_SECRET: &str = "SHARED_SECRET";
pub const FORBIDDEN_KEY_DIFFIE_HELLMAN_PUBLIC: &str = "DH_PUBLIC";
pub const FORBIDDEN_KEYS: [&str; 1] = [NETWORK_PARENT_KEY];

// Deafult name for TSS server
// Will set mnemonic and db path
Expand Down Expand Up @@ -198,23 +184,6 @@ pub struct StartupArgs {
/// The path to a password file
#[arg(short = 'f', long = "password-file")]
pub password_file: Option<PathBuf>,

/// Set up the key-value store (KVDB), or ensure one already exists, print setup information to
/// stdout, then exit. Supply the `--password-file` option for fully non-interactive operation.
///
/// Returns the AccountID and Diffie-Hellman Public Keys associated with this server.
#[arg(long = "setup-only")]
pub setup_only: bool,
}

pub async fn has_mnemonic(kv: &KvManager) -> bool {
let exists = kv.kv().exists(FORBIDDEN_KEY_MNEMONIC).await.expect("issue querying DB");

if exists {
tracing::debug!("Existing mnemonic found in keystore.");
}

exists
}

pub fn development_mnemonic(validator_name: &Option<ValidatorName>) -> bip39::Mnemonic {
Expand All @@ -234,16 +203,6 @@ pub fn development_mnemonic(validator_name: &Option<ValidatorName>) -> bip39::Mn
.expect("Unable to parse given mnemonic.")
}

pub async fn threshold_account_id(kv: &KvManager) -> String {
let mnemonic = kv.kv().get(FORBIDDEN_KEY_MNEMONIC).await.expect("Issue getting mnemonic");
let pair = <sr25519::Pair as Pair>::from_phrase(
&String::from_utf8(mnemonic).expect("Issue converting mnemonic to string"),
None,
)
.expect("Issue converting mnemonic to pair");
AccountId32::new(pair.0.public().into()).to_ss58check()
}

pub async fn setup_latest_block_number(kv: &KvManager) -> Result<(), KvError> {
let exists_result_new_user =
kv.kv().exists(LATEST_BLOCK_NUMBER_NEW_USER).await.expect("issue querying DB");
Expand Down Expand Up @@ -300,25 +259,6 @@ pub async fn setup_latest_block_number(kv: &KvManager) -> Result<(), KvError> {
Ok(())
}

pub async fn setup_only(kv: &KvManager) {
let mnemonic = kv.kv().get(FORBIDDEN_KEYS[0]).await.expect("Issue getting mnemonic");
let pair = <sr25519::Pair as Pair>::from_phrase(
&String::from_utf8(mnemonic).expect("Issue converting mnemonic to string"),
None,
)
.expect("Issue converting mnemonic to pair");
let account_id = AccountId32::new(pair.0.public().into()).to_ss58check();

let dh_public_key = kv.kv().get(FORBIDDEN_KEYS[2]).await.expect("Issue getting dh public key");
let dh_public_key = format!("{dh_public_key:?}").replace('"', "");
let output = json!({
"account_id": account_id,
"dh_public_key": dh_public_key,
});

println!("{}", output);
}

pub async fn check_node_prerequisites(url: &str, account_id: &str) {
use crate::chain_api::{get_api, get_rpc};

Expand Down
6 changes: 5 additions & 1 deletion crates/threshold-signature-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ use axum::{
};
use entropy_kvdb::kv_manager::KvManager;
use rand_core::OsRng;
use sp_core::{sr25519, Pair};
use sp_core::{crypto::AccountId32, sr25519, Pair};
use subxt::{tx::PairSigner, utils::AccountId32 as SubxtAccountId32};
use tower_http::{
cors::{Any, CorsLayer},
Expand Down Expand Up @@ -239,6 +239,10 @@ impl AppState {
PairSigner::<EntropyConfig, sr25519::Pair>::new(self.signer.clone())
}

pub fn account_id(&self) -> AccountId32 {
AccountId32::new(self.signer.public().0)
}

pub fn subxt_account_id(&self) -> SubxtAccountId32 {
SubxtAccountId32(self.signer.public().0)
}
Expand Down
43 changes: 16 additions & 27 deletions crates/threshold-signature-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@
use std::{net::SocketAddr, str::FromStr};

use clap::Parser;
use sp_core::crypto::Ss58Codec;

use entropy_tss::{
app,
launch::{
load_kv_store, setup_latest_block_number, setup_only, Configuration, StartupArgs,
ValidatorName,
},
launch::{load_kv_store, setup_latest_block_number, Configuration, StartupArgs, ValidatorName},
AppState,
};

Expand All @@ -31,19 +29,15 @@ async fn main() {
let args = StartupArgs::parse();
args.logger.setup().await;

if !args.setup_only {
tracing::info!("Starting Threshold Signature Sever");
tracing::info!("Starting server on: `{}`", &args.threshold_url);
}
tracing::info!("Starting Threshold Signature Sever");
tracing::info!("Starting server on: `{}`", &args.threshold_url);

if args.logger.loki {
tracing::info!("Sending logs to Loki server at `{}`", &args.logger.loki_endpoint);
}

let configuration = Configuration::new(args.chain_endpoint);
if !args.setup_only {
tracing::info!("Connecting to Substrate node at: `{}`", &configuration.endpoint);
}
tracing::info!("Connecting to Substrate node at: `{}`", &configuration.endpoint);

let mut validator_name = None;
if args.alice {
Expand Down Expand Up @@ -71,21 +65,16 @@ async fn main() {
// Below deals with syncing the kvdb
let addr = SocketAddr::from_str(&args.threshold_url).expect("failed to parse threshold url.");

if args.setup_only {
setup_only(&kv_store).await;
} else {
let account_id = entropy_tss::launch::threshold_account_id(&kv_store).await;
entropy_tss::launch::check_node_prerequisites(
&app_state.configuration.endpoint,
&account_id,
)
.await;
entropy_tss::launch::check_node_prerequisites(
&app_state.configuration.endpoint,
&app_state.account_id().to_ss58check(),
)
.await;

let listener = tokio::net::TcpListener::bind(&addr)
.await
.expect("Unable to bind to given server address.");
axum::serve(listener, app(app_state).into_make_service())
.await
.expect("failed to launch axum server.");
}
let listener = tokio::net::TcpListener::bind(&addr)
.await
.expect("Unable to bind to given server address.");
axum::serve(listener, app(app_state).into_make_service())
.await
.expect("failed to launch axum server.");
}

0 comments on commit e71cc72

Please sign in to comment.