From c3c68acfd2353dbc7708ed82eebf139d9130da8a Mon Sep 17 00:00:00 2001 From: qima Date: Wed, 15 May 2024 18:53:22 +0800 Subject: [PATCH] feat: pass sk_str via cli opt --- .github/workflows/memcheck.yml | 2 +- sn_auditor/src/dag_db.rs | 10 +++++++--- sn_auditor/src/main.rs | 18 ++++++++++++++++-- sn_cli/src/bin/subcommands/wallet/audit.rs | 11 +++++++++-- .../src/bin/subcommands/wallet/hot_wallet.rs | 16 +++++++++++++++- sn_client/src/audit/spend_dag.rs | 5 +++-- sn_transfers/src/cashnotes/spend_reason.rs | 13 ++----------- 7 files changed, 53 insertions(+), 22 deletions(-) diff --git a/.github/workflows/memcheck.yml b/.github/workflows/memcheck.yml index 1df58b0c61..c33448a689 100644 --- a/.github/workflows/memcheck.yml +++ b/.github/workflows/memcheck.yml @@ -282,7 +282,7 @@ jobs: - name: Audit from genesis to collect entire spend DAG and dump to a dot file run: | - ./target/release/safe --log-output-dest=data-dir wallet audit --dot > spend_dag_and_statistics.txt + ./target/release/safe --log-output-dest=data-dir wallet audit -- --sk 1f85ace7ff4567cb8bcd9365b374843039d1c2e935a14e0517123bbf51a39c09 --dot > spend_dag_and_statistics.txt echo "==============================================================================" cat spend_dag_and_statistics.txt env: diff --git a/sn_auditor/src/dag_db.rs b/sn_auditor/src/dag_db.rs index 627d22471b..732e4083e5 100644 --- a/sn_auditor/src/dag_db.rs +++ b/sn_auditor/src/dag_db.rs @@ -6,6 +6,7 @@ // KIND, either express or implied. Please review the Licences for the specific language governing // permissions and limitations relating to use of the SAFE Network Software. +use bls::SecretKey; use color_eyre::eyre::{eyre, Result}; use graphviz_rust::{cmd::Format, exec, parse, printer::PrinterContext}; use serde::{Deserialize, Serialize}; @@ -32,6 +33,7 @@ pub struct SpendDagDb { dag: Arc>, forwarded_payments: Arc>, beta_participants: BTreeMap, + foundation_sk: SecretKey, } /// Map of Discord usernames to their tracked forwarded payments @@ -49,7 +51,7 @@ impl SpendDagDb { /// Create a new SpendDagDb /// If a local spend DAG file is found, it will be loaded /// Else a new DAG will be created containing only Genesis - pub async fn new(path: PathBuf, client: Client) -> Result { + pub async fn new(path: PathBuf, client: Client, foundation_sk: SecretKey) -> Result { let dag_path = path.join(SPEND_DAG_FILENAME); let dag = match SpendDag::load_from_file(&dag_path) { Ok(d) => { @@ -68,11 +70,12 @@ impl SpendDagDb { dag: Arc::new(RwLock::new(dag)), forwarded_payments: Arc::new(RwLock::new(BTreeMap::new())), beta_participants: BTreeMap::new(), + foundation_sk, }) } /// Create a new SpendDagDb from a local file and no network connection - pub fn offline(dag_path: PathBuf) -> Result { + pub fn offline(dag_path: PathBuf, foundation_sk: SecretKey) -> Result { let path = dag_path .parent() .ok_or_else(|| eyre!("Failed to get parent path"))? @@ -84,6 +87,7 @@ impl SpendDagDb { dag: Arc::new(RwLock::new(dag)), forwarded_payments: Arc::new(RwLock::new(BTreeMap::new())), beta_participants: BTreeMap::new(), + foundation_sk, }) } @@ -262,7 +266,7 @@ impl SpendDagDb { // find spends with payments let mut payments: ForwardedPayments = BTreeMap::new(); for spend in all_spends { - let user_name_hash = match spend.reason().get_sender_hash() { + let user_name_hash = match spend.reason().get_sender_hash(&self.foundation_sk) { Some(n) => n, None => continue, }; diff --git a/sn_auditor/src/main.rs b/sn_auditor/src/main.rs index 076d5c375c..a9b0fb4660 100644 --- a/sn_auditor/src/main.rs +++ b/sn_auditor/src/main.rs @@ -64,6 +64,10 @@ struct Opt { /// Provide a JSON file with a list of Discord usernames as argument #[clap(short, long, value_name = "discord_names_file")] beta_participants: Option, + + /// Hex string of the Foundation SK. + #[clap(name = "sk")] + sk_str: String, } #[tokio::main] @@ -85,8 +89,16 @@ async fn main() -> Result<()> { Vec::new() }; + let sk = match SecretKey::from_hex(&opt.sk_str) { + Ok(sk) => sk, + Err(err) => panic!( + "Cann't parse Foundation SK from input string: {} {err:?}", + opt.sk_str + ), + }; + if let Some(dag_to_view) = opt.offline_viewer { - let dag = SpendDagDb::offline(dag_to_view)?; + let dag = SpendDagDb::offline(dag_to_view, sk)?; dag.dump_dag_svg()?; start_server(dag).await?; return Ok(()); @@ -98,6 +110,7 @@ async fn main() -> Result<()> { opt.force_from_genesis, opt.clean, beta_participants, + sk, ) .await?; start_server(dag).await @@ -151,6 +164,7 @@ async fn initialize_background_spend_dag_collection( force_from_genesis: bool, clean: bool, beta_participants: Vec, + sk: SecretKey, ) -> Result { println!("Initialize spend dag..."); let path = dirs_next::data_dir() @@ -166,7 +180,7 @@ async fn initialize_background_spend_dag_collection( } // initialize the DAG - let dag = dag_db::SpendDagDb::new(path.clone(), client.clone()) + let dag = dag_db::SpendDagDb::new(path.clone(), client.clone(), sk) .await .map_err(|e| eyre!("Could not create SpendDag Db: {e}"))?; diff --git a/sn_cli/src/bin/subcommands/wallet/audit.rs b/sn_cli/src/bin/subcommands/wallet/audit.rs index 0e900f36b3..2b275c1f0a 100644 --- a/sn_cli/src/bin/subcommands/wallet/audit.rs +++ b/sn_cli/src/bin/subcommands/wallet/audit.rs @@ -8,6 +8,7 @@ use std::path::Path; +use bls::SecretKey; use color_eyre::Result; use sn_client::acc_packet::load_account_wallet_or_create_with_mnemonic; use sn_client::transfers::{CashNoteRedemption, SpendAddress, Transfer, GENESIS_CASHNOTE}; @@ -37,7 +38,13 @@ async fn gather_spend_dag(client: &Client, root_dir: &Path) -> Result Ok(dag) } -pub async fn audit(client: &Client, to_dot: bool, royalties: bool, root_dir: &Path) -> Result<()> { +pub async fn audit( + client: &Client, + to_dot: bool, + royalties: bool, + root_dir: &Path, + sk: &SecretKey, +) -> Result<()> { if to_dot { let dag = gather_spend_dag(client, root_dir).await?; println!( @@ -47,7 +54,7 @@ pub async fn audit(client: &Client, to_dot: bool, royalties: bool, root_dir: &Pa println!( "======================= payment forward statistics ==========================" ); - println!("{}", dag.dump_payment_forward_statistics()); + println!("{}", dag.dump_payment_forward_statistics(sk)); } else if royalties { let dag = gather_spend_dag(client, root_dir).await?; let royalties = dag.all_royalties()?; diff --git a/sn_cli/src/bin/subcommands/wallet/hot_wallet.rs b/sn_cli/src/bin/subcommands/wallet/hot_wallet.rs index cd0c8a12e4..45c727f726 100644 --- a/sn_cli/src/bin/subcommands/wallet/hot_wallet.rs +++ b/sn_cli/src/bin/subcommands/wallet/hot_wallet.rs @@ -114,6 +114,9 @@ pub enum WalletCmds { /// Note that this might take a very long time /// Analogous to verifying the entire blockchain in Bitcoin Audit { + /// Hex string of the Foundation SK. + #[clap(name = "sk")] + sk: String, /// EXPERIMENTAL Dump Audit DAG in dot format on stdout #[clap(long, default_value = "false")] dot: bool, @@ -205,7 +208,18 @@ pub(crate) async fn wallet_cmds( maid_address, signature, } => get_faucet(root_dir, client, url.clone(), maid_address, signature).await, - WalletCmds::Audit { dot, royalties } => audit(client, dot, royalties, root_dir).await, + WalletCmds::Audit { dot, royalties, sk } => { + let sk_key = match SecretKey::from_hex(&sk) { + Ok(sk_key) => sk_key, + Err(err) => { + return Err(eyre!( + "Cann't parse Foundation SK from input string: {sk} {err:?}" + )) + } + }; + + audit(client, dot, royalties, root_dir, &sk_key).await + } WalletCmds::Verify { spend_address, genesis, diff --git a/sn_client/src/audit/spend_dag.rs b/sn_client/src/audit/spend_dag.rs index 7690ffc08e..e6def55aa5 100644 --- a/sn_client/src/audit/spend_dag.rs +++ b/sn_client/src/audit/spend_dag.rs @@ -6,6 +6,7 @@ // KIND, either express or implied. Please review the Licences for the specific language governing // permissions and limitations relating to use of the SAFE Network Software. +use bls::SecretKey; use petgraph::dot::Dot; use petgraph::graph::{DiGraph, NodeIndex}; use petgraph::visit::EdgeRef; @@ -280,7 +281,7 @@ impl SpendDag { format!("{:?}", Dot::with_config(&self.dag, &[])) } - pub fn dump_payment_forward_statistics(&self) -> String { + pub fn dump_payment_forward_statistics(&self, sk: &SecretKey) -> String { let mut statistics: BTreeMap> = Default::default(); let mut hash_dictionary: BTreeMap = Default::default(); @@ -300,7 +301,7 @@ impl SpendDag { for spend_dag_entry in self.spends.values() { if let DagEntry::Spend(signed_spend, _) = spend_dag_entry { - if let Some(sender_hash) = signed_spend.spend.reason.get_sender_hash() { + if let Some(sender_hash) = signed_spend.spend.reason.get_sender_hash(sk) { let sender = if let Some(readable_sender) = hash_dictionary.get(&sender_hash) { readable_sender.clone() } else { diff --git a/sn_transfers/src/cashnotes/spend_reason.rs b/sn_transfers/src/cashnotes/spend_reason.rs index 2136bdb934..2d6a179874 100644 --- a/sn_transfers/src/cashnotes/spend_reason.rs +++ b/sn_transfers/src/cashnotes/spend_reason.rs @@ -6,7 +6,6 @@ // KIND, either express or implied. Please review the Licences for the specific language governing // permissions and limitations relating to use of the SAFE Network Software. -use crate::GENESIS_CASHNOTE_SK; use bls::{Ciphertext, PublicKey, SecretKey}; use serde::{Deserialize, Serialize}; use xor_name::XorName; @@ -47,18 +46,10 @@ impl SpendReason { )?)) } - pub fn get_sender_hash(&self) -> Option { + pub fn get_sender_hash(&self, sk: &SecretKey) -> Option { match self { Self::BetaRewardTracking(cypher) => { - let sk = match SecretKey::from_hex(GENESIS_CASHNOTE_SK) { - Ok(sk) => sk, - Err(err) => { - error!("Failed to get GENESIS sk {err:?}"); - return None; - } - }; - - if let Ok(hash) = cypher.decrypt_to_username_hash(&sk) { + if let Ok(hash) = cypher.decrypt_to_username_hash(sk) { Some(hash) } else { error!("Failed to decrypt BetaRewardTracking");