Skip to content

Commit

Permalink
feat: pass sk_str via cli opt
Browse files Browse the repository at this point in the history
  • Loading branch information
maqi committed May 15, 2024
1 parent 3898588 commit c3c68ac
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/memcheck.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 7 additions & 3 deletions sn_auditor/src/dag_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -32,6 +33,7 @@ pub struct SpendDagDb {
dag: Arc<RwLock<SpendDag>>,
forwarded_payments: Arc<RwLock<ForwardedPayments>>,
beta_participants: BTreeMap<Hash, String>,
foundation_sk: SecretKey,
}

/// Map of Discord usernames to their tracked forwarded payments
Expand All @@ -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<Self> {
pub async fn new(path: PathBuf, client: Client, foundation_sk: SecretKey) -> Result<Self> {
let dag_path = path.join(SPEND_DAG_FILENAME);
let dag = match SpendDag::load_from_file(&dag_path) {
Ok(d) => {
Expand All @@ -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<Self> {
pub fn offline(dag_path: PathBuf, foundation_sk: SecretKey) -> Result<Self> {
let path = dag_path
.parent()
.ok_or_else(|| eyre!("Failed to get parent path"))?
Expand All @@ -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,
})
}

Expand Down Expand Up @@ -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,
};
Expand Down
18 changes: 16 additions & 2 deletions sn_auditor/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PathBuf>,

/// Hex string of the Foundation SK.
#[clap(name = "sk")]
sk_str: String,
}

#[tokio::main]
Expand All @@ -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(());
Expand All @@ -98,6 +110,7 @@ async fn main() -> Result<()> {
opt.force_from_genesis,
opt.clean,
beta_participants,
sk,
)
.await?;
start_server(dag).await
Expand Down Expand Up @@ -151,6 +164,7 @@ async fn initialize_background_spend_dag_collection(
force_from_genesis: bool,
clean: bool,
beta_participants: Vec<String>,
sk: SecretKey,
) -> Result<SpendDagDb> {
println!("Initialize spend dag...");
let path = dirs_next::data_dir()
Expand All @@ -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}"))?;

Expand Down
11 changes: 9 additions & 2 deletions sn_cli/src/bin/subcommands/wallet/audit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -37,7 +38,13 @@ async fn gather_spend_dag(client: &Client, root_dir: &Path) -> Result<SpendDag>
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!(
Expand All @@ -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()?;
Expand Down
16 changes: 15 additions & 1 deletion sn_cli/src/bin/subcommands/wallet/hot_wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions sn_client/src/audit/spend_dag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, Vec<NanoTokens>> = Default::default();

let mut hash_dictionary: BTreeMap<Hash, String> = Default::default();
Expand All @@ -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 {
Expand Down
13 changes: 2 additions & 11 deletions sn_transfers/src/cashnotes/spend_reason.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -47,18 +46,10 @@ impl SpendReason {
)?))
}

pub fn get_sender_hash(&self) -> Option<Hash> {
pub fn get_sender_hash(&self, sk: &SecretKey) -> Option<Hash> {
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");
Expand Down

0 comments on commit c3c68ac

Please sign in to comment.