From 89ed7f66293c2788bbe07f5dc2c057751dc0dddb Mon Sep 17 00:00:00 2001 From: Ermine Jose Date: Wed, 16 Oct 2024 16:23:13 +0530 Subject: [PATCH] autonomi_cli: add create evm wallet --- autonomi/src/lib.rs | 1 + autonomi/src/wallet.rs | 9 +++++++++ autonomi_cli/src/commands.rs | 20 ++++++++++++++++++++ autonomi_cli/src/commands/wallet.rs | 24 ++++++++++++++++++++++++ evmlib/src/wallet.rs | 5 +++++ sn_evm/src/lib.rs | 1 + sn_evm/src/wallet.rs | 24 ++++++++++++++++++++++++ 7 files changed, 84 insertions(+) create mode 100644 autonomi/src/wallet.rs create mode 100644 autonomi_cli/src/commands/wallet.rs create mode 100644 sn_evm/src/wallet.rs diff --git a/autonomi/src/lib.rs b/autonomi/src/lib.rs index 98921768ce..b5fc42b7cd 100644 --- a/autonomi/src/lib.rs +++ b/autonomi/src/lib.rs @@ -33,6 +33,7 @@ extern crate tracing; pub mod client; +pub mod wallet; #[cfg(feature = "data")] mod self_encryption; diff --git a/autonomi/src/wallet.rs b/autonomi/src/wallet.rs new file mode 100644 index 0000000000..c132da0d7f --- /dev/null +++ b/autonomi/src/wallet.rs @@ -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); +} \ No newline at end of file diff --git a/autonomi_cli/src/commands.rs b/autonomi_cli/src/commands.rs index bb718df43a..0142b3a3b9 100644 --- a/autonomi_cli/src/commands.rs +++ b/autonomi_cli/src/commands.rs @@ -9,6 +9,7 @@ mod file; mod register; mod vault; +mod wallet; use clap::Subcommand; use color_eyre::Result; @@ -34,6 +35,12 @@ pub enum SubCmd { #[command(subcommand)] command: VaultCmd, }, + + /// Operations related to wallet management. + Wallet { + #[command(subcommand)] + command: WalletCmd, + } } #[derive(Subcommand, Debug)] @@ -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; @@ -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?), + } } } diff --git a/autonomi_cli/src/commands/wallet.rs b/autonomi_cli/src/commands/wallet.rs new file mode 100644 index 0000000000..564399ed3b --- /dev/null +++ b/autonomi_cli/src/commands/wallet.rs @@ -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) -> Result<()> { + create_evm_wallet(); + Ok(()) +} + +pub fn balance(peers: Vec) -> Result<()> { + Ok(()) +} \ No newline at end of file diff --git a/evmlib/src/wallet.rs b/evmlib/src/wallet.rs index e758e58eee..7cf236cc8f 100644 --- a/evmlib/src/wallet.rs +++ b/evmlib/src/wallet.rs @@ -174,6 +174,11 @@ pub fn wallet_address(wallet: &EthereumWallet) -> Address { >::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, diff --git a/sn_evm/src/lib.rs b/sn_evm/src/lib.rs index f4c70e04a6..b376b1f93e 100644 --- a/sn_evm/src/lib.rs +++ b/sn_evm/src/lib.rs @@ -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}; diff --git a/sn_evm/src/wallet.rs b/sn_evm/src/wallet.rs new file mode 100644 index 0000000000..cb2d5d9831 --- /dev/null +++ b/sn_evm/src/wallet.rs @@ -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() +} \ No newline at end of file