-
Notifications
You must be signed in to change notification settings - Fork 247
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #210 from subspace/farmer-identity-management
Farmer identity management
- Loading branch information
Showing
8 changed files
with
274 additions
and
49 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
crates/subspace-farmer/src/bin/subspace-farmer/commands.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,41 @@ | ||
mod farm; | ||
mod identity; | ||
|
||
pub(crate) use farm::farm; | ||
pub(crate) use identity::identity; | ||
use log::info; | ||
use std::path::Path; | ||
use std::{fs, io}; | ||
|
||
/// Helper function for ignoring the error that given file/directory does not exist. | ||
fn try_remove<P: AsRef<Path>>( | ||
path: P, | ||
remove: impl FnOnce(P) -> std::io::Result<()>, | ||
) -> io::Result<()> { | ||
if path.as_ref().exists() { | ||
remove(path)?; | ||
} | ||
Ok(()) | ||
} | ||
|
||
pub(crate) fn erase_plot<P: AsRef<Path>>(path: P) -> io::Result<()> { | ||
info!("Erasing the plot"); | ||
try_remove(path.as_ref().join("plot.bin"), fs::remove_file)?; | ||
info!("Erasing plot metadata"); | ||
try_remove(path.as_ref().join("plot-metadata"), fs::remove_dir_all)?; | ||
info!("Erasing plot commitments"); | ||
try_remove(path.as_ref().join("commitments"), fs::remove_dir_all)?; | ||
info!("Erasing object mappings"); | ||
try_remove(path.as_ref().join("object-mappings"), fs::remove_dir_all)?; | ||
|
||
Ok(()) | ||
} | ||
|
||
pub(crate) fn wipe<P: AsRef<Path>>(path: P) -> io::Result<()> { | ||
erase_plot(path.as_ref())?; | ||
|
||
info!("Erasing identity"); | ||
try_remove(path.as_ref().join("identity.bin"), fs::remove_file)?; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
crates/subspace-farmer/src/bin/subspace-farmer/commands/identity.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
use crate::{utils, IdentityCommand}; | ||
use anyhow::bail; | ||
use bip39::{Language, Mnemonic}; | ||
use sp_core::crypto::{Ss58AddressFormatRegistry, Ss58Codec, UncheckedFrom}; | ||
use sp_core::sr25519::Public; | ||
use std::path::Path; | ||
use subspace_farmer::Identity; | ||
use zeroize::Zeroize; | ||
|
||
pub(crate) fn identity(identity_command: IdentityCommand) -> anyhow::Result<()> { | ||
match identity_command { | ||
IdentityCommand::View { | ||
address, | ||
public_key, | ||
mnemonic, | ||
custom_path, | ||
} => { | ||
let path = utils::get_path(custom_path); | ||
view(&path, address, public_key, mnemonic) | ||
} | ||
IdentityCommand::ImportFromMnemonic { | ||
mut phrase, | ||
custom_path, | ||
} => { | ||
let path = utils::get_path(custom_path); | ||
if Identity::open(&path)?.is_some() { | ||
bail!("Identity already exists, can't import!"); | ||
} | ||
|
||
let result = Identity::import_from_mnemonic(path, &phrase).map(|_identity| ()); | ||
phrase.zeroize(); | ||
result | ||
} | ||
} | ||
} | ||
|
||
fn view<P: AsRef<Path>>( | ||
path: P, | ||
address: bool, | ||
public_key: bool, | ||
mnemonic: bool, | ||
) -> anyhow::Result<()> { | ||
let identity = match Identity::open(&path)? { | ||
Some(identity) => identity, | ||
None => { | ||
bail!("Identity doesn't exist"); | ||
} | ||
}; | ||
|
||
let public = Public::unchecked_from(identity.public_key().to_bytes()); | ||
|
||
if (false, false, false) == (address, public_key, mnemonic) || address { | ||
eprint!("Address (SS58 format):\n "); | ||
|
||
println!( | ||
"{}", | ||
public.to_ss58check_with_version( | ||
Ss58AddressFormatRegistry::SubspaceTestnetAccount.into() | ||
) | ||
); | ||
} | ||
|
||
if public_key { | ||
eprint!("Public key (hex format):\n "); | ||
|
||
println!("0x{}", hex::encode(public)); | ||
} | ||
|
||
if mnemonic { | ||
eprint!("Mnemonic (NOTE: never share this with anyone!):\n "); | ||
|
||
println!( | ||
"{}", | ||
Mnemonic::from_entropy(identity.entropy(), Language::English).unwrap() | ||
); | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.