Skip to content

Commit

Permalink
autonomi_cli: add create evm wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
ermineJose committed Oct 16, 2024
1 parent a592c08 commit 89ed7f6
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 0 deletions.
1 change: 1 addition & 0 deletions autonomi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
extern crate tracing;

pub mod client;
pub mod wallet;
#[cfg(feature = "data")]
mod self_encryption;

Expand Down
9 changes: 9 additions & 0 deletions autonomi/src/wallet.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use sn_evm::wallet::{get_random_private_key,create_a_evm_wallet,create_file_with_keys};


pub fn create_evm_wallet() {
let wallet_private_key = get_random_private_key();
let wallet_public_key = create_a_evm_wallet(&wallet_private_key);
let file_path = create_file_with_keys(wallet_private_key, wallet_public_key);
println!("A file is created with the path: {}", file_path);
}
20 changes: 20 additions & 0 deletions autonomi_cli/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
mod file;
mod register;
mod vault;
mod wallet;

use clap::Subcommand;
use color_eyre::Result;
Expand All @@ -34,6 +35,12 @@ pub enum SubCmd {
#[command(subcommand)]
command: VaultCmd,
},

/// Operations related to wallet management.
Wallet {
#[command(subcommand)]
command: WalletCmd,
}
}

#[derive(Subcommand, Debug)]
Expand Down Expand Up @@ -129,6 +136,15 @@ pub enum VaultCmd {
Sync,
}

#[derive(Subcommand, Debug)]
pub enum WalletCmd {
/// Create a wallet
Create,

/// Check the balance of the wallet
Balance,
}

pub async fn handle_subcommand(opt: Opt) -> Result<()> {
let peers = crate::access::network::get_peers(opt.peers);
let cmd = opt.command;
Expand Down Expand Up @@ -163,5 +179,9 @@ pub async fn handle_subcommand(opt: Opt) -> Result<()> {
VaultCmd::Create => vault::create(peers.await?),
VaultCmd::Sync => vault::sync(peers.await?),
},
SubCmd::Wallet { command } => match command {
WalletCmd::Create => wallet::create(peers.await?),
WalletCmd::Balance => wallet::balance(peers.await?),
}
}
}
24 changes: 24 additions & 0 deletions autonomi_cli/src/commands/wallet.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2024 MaidSafe.net limited.
//
// This SAFE Network Software is licensed to you under The General Public License (GPL), version 3.
// Unless required by applicable law or agreed to in writing, the SAFE Network Software distributed
// under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// 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 autonomi::Multiaddr;
use autonomi::wallet::*;
use color_eyre::{
eyre::{bail, Context},
Result,
};


pub fn create(peers: Vec<Multiaddr>) -> Result<()> {
create_evm_wallet();
Ok(())
}

pub fn balance(peers: Vec<Multiaddr>) -> Result<()> {
Ok(())
}
5 changes: 5 additions & 0 deletions evmlib/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,11 @@ pub fn wallet_address(wallet: &EthereumWallet) -> Address {
<EthereumWallet as NetworkWallet<Ethereum>>::default_signer_address(wallet)
}

pub fn get_random_private_key_for_wallet() -> String {
let signer: PrivateKeySigner = LocalSigner::random();
signer.to_bytes().to_string()
}

/// Returns the raw balance of payment tokens for this wallet.
pub async fn balance_of_tokens(
account: Address,
Expand Down
1 change: 1 addition & 0 deletions sn_evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub use evmlib::Network as EvmNetwork;
mod amount;
mod data_payments;
mod error;
pub mod wallet;

pub use data_payments::{PaymentQuote, ProofOfPayment, QuotingMetrics};

Expand Down
24 changes: 24 additions & 0 deletions sn_evm/src/wallet.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use evmlib::{wallet::{get_random_private_key_for_wallet, Wallet}, Network};
use evmlib::utils::get_evm_network_from_env;
use std::fs::File;
use std::io::Write;
use std::path::Path;

pub fn get_random_private_key() -> String {
get_random_private_key_for_wallet()
}

pub fn create_a_evm_wallet(private_key: &String) -> String {
let network = get_evm_network_from_env()
.expect("Failed to get EVM network from environment variables");
let wallet = Wallet::new_from_private_key(network, &private_key)
.expect("Could not init deployer wallet");
hex::encode(wallet.address())
}

pub fn create_file_with_keys(private_key: String, public_key: String) -> String {
let mut file = File::create(public_key.clone()).expect("could not create file");
file.write_all(private_key.as_bytes()).expect("Not able to write into file");
let file_path = Path::new(&public_key).canonicalize().expect("Not able to find the absolute path for the file");
file_path.to_string_lossy().to_string()
}

0 comments on commit 89ed7f6

Please sign in to comment.