Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setup is now in lib and not main #29

Merged
merged 1 commit into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion seedelf-cli/src/commands/balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use reqwest::Error;
use seedelf_cli::display;
use seedelf_cli::koios::UtxoResponse;
use seedelf_cli::utxos;
use crate::setup;
use seedelf_cli::setup;

pub async fn run(network_flag: bool) -> Result<(), Error> {
display::preprod_text(network_flag);
Expand Down
2 changes: 1 addition & 1 deletion seedelf-cli/src/commands/seedelf_all.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use blstrs::Scalar;
use reqwest::Error;
use seedelf_cli::display;
use crate::setup;
use seedelf_cli::setup;

pub async fn run(network_flag: bool) -> Result<(), Error> {
display::preprod_text(network_flag);
Expand Down
2 changes: 1 addition & 1 deletion seedelf-cli/src/commands/seedelf_new.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::setup;
use seedelf_cli::setup;
use blstrs::Scalar;
use clap::Args;
use hex;
Expand Down
2 changes: 1 addition & 1 deletion seedelf-cli/src/commands/seedelf_remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use seedelf_cli::register::Register;
use seedelf_cli::schnorr::create_proof;
use seedelf_cli::transaction;
use seedelf_cli::utxos;
use crate::setup;
use seedelf_cli::setup;

/// Struct to hold command-specific arguments
#[derive(Args)]
Expand Down
2 changes: 1 addition & 1 deletion seedelf-cli/src/commands/sweep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use seedelf_cli::register::Register;
use seedelf_cli::schnorr::create_proof;
use seedelf_cli::transaction;
use seedelf_cli::utxos;
use crate::setup;
use seedelf_cli::setup;

/// Struct to hold command-specific arguments
#[derive(Args)]
Expand Down
2 changes: 1 addition & 1 deletion seedelf-cli/src/commands/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use seedelf_cli::register::Register;
use seedelf_cli::schnorr::create_proof;
use seedelf_cli::transaction;
use seedelf_cli::utxos;
use crate::setup;
use seedelf_cli::setup;

/// Struct to hold command-specific arguments
#[derive(Args)]
Expand Down
2 changes: 1 addition & 1 deletion seedelf-cli/src/commands/wallet_info.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use blstrs::Scalar;
use seedelf_cli::register::Register;
use crate::setup;
use seedelf_cli::setup;

pub fn run() {
println!("\nSeedelf Wallet Information");
Expand Down
1 change: 1 addition & 0 deletions seedelf-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub mod hashing;
pub mod koios;
pub mod register;
pub mod schnorr;
pub mod setup;
pub mod transaction;
pub mod utxos;
pub mod web_server;
2 changes: 1 addition & 1 deletion seedelf-cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use clap::{Parser, Subcommand};
mod commands;
mod setup;
use seedelf_cli::setup;

#[derive(Parser)]
#[command(name = "seedelf-cli")]
Expand Down
25 changes: 13 additions & 12 deletions seedelf-cli/src/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use serde::{Deserialize, Serialize};
use std::fs;
use std::io::{self, Write};
use std::path::{Path, PathBuf};
use seedelf_cli::schnorr::random_scalar;
use crate::schnorr::random_scalar;


/// Data structure for storing wallet information
#[derive(Serialize, Deserialize)]
Expand All @@ -15,15 +16,15 @@ struct Wallet {
/// Check if `.seedelf` exists, create it if it doesn't, and handle file logic
pub fn check_and_prepare_seedelf() {
// this may only work on ubuntu
let seedelf_path = Path::new("/home").join(whoami::username()).join(".seedelf");
let seedelf_path: PathBuf = Path::new("/home").join(whoami::username()).join(".seedelf");

// Check if `.seedelf` exists
if !seedelf_path.exists() {
fs::create_dir_all(&seedelf_path).expect("Failed to create .seedelf directory");
}

// Check if there are any files in `.seedelf`
let contents = fs::read_dir(&seedelf_path)
let contents: Vec<fs::DirEntry> = fs::read_dir(&seedelf_path)
.expect("Failed to read .seedelf directory")
.filter_map(|entry| entry.ok())
.collect::<Vec<_>>();
Expand Down Expand Up @@ -56,15 +57,15 @@ fn prompt_wallet_name() -> String {
/// Create a wallet file and save a random private key
fn create_wallet(wallet_path: &PathBuf) {
// Generate a random private key
let sk = random_scalar(); // Requires `Field` trait in scope
let private_key_bytes = sk.to_repr(); // Use `to_repr()` to get canonical bytes
let private_key_hex = hex::encode(private_key_bytes);
let sk: Scalar = random_scalar(); // Requires `Field` trait in scope
let private_key_bytes: [u8; 32] = sk.to_repr(); // Use `to_repr()` to get canonical bytes
let private_key_hex: String = hex::encode(private_key_bytes);

// Serialize the wallet
let wallet = Wallet {
let wallet: Wallet = Wallet {
private_key: private_key_hex,
};
let wallet_data = serde_json::to_string_pretty(&wallet).expect("Failed to serialize wallet");
let wallet_data: String = serde_json::to_string_pretty(&wallet).expect("Failed to serialize wallet");

// Save to file
fs::write(wallet_path, wallet_data).expect("Failed to write wallet file");
Expand All @@ -75,10 +76,10 @@ fn create_wallet(wallet_path: &PathBuf) {
/// Load the wallet file and deserialize the private key into a Scalar
pub fn load_wallet() -> Scalar {
// Default `.seedelf` directory path
let seedelf_path = Path::new("/home").join(whoami::username()).join(".seedelf");
let seedelf_path: PathBuf = Path::new("/home").join(whoami::username()).join(".seedelf");

// Get the list of files in `.seedelf`
let contents = fs::read_dir(&seedelf_path)
let contents: Vec<fs::DirEntry> = fs::read_dir(&seedelf_path)
.expect("Failed to read .seedelf directory")
.filter_map(|entry| entry.ok())
.collect::<Vec<_>>();
Expand All @@ -88,8 +89,8 @@ pub fn load_wallet() -> Scalar {
}

// Use the first file in the directory to build the wallet path
let first_file = &contents[0];
let wallet_path = first_file.path();
let first_file: &fs::DirEntry = &contents[0];
let wallet_path: PathBuf = first_file.path();

// Read the wallet file
let wallet_data: String = fs::read_to_string(&wallet_path).expect("Failed to read wallet file");
Expand Down
Loading