-
Notifications
You must be signed in to change notification settings - Fork 221
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
feat(wallet): add view key commands #6426
Changes from 4 commits
419332e
f79248d
4efa79a
d61742d
f303dcc
c425049
6cda68e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ | |
|
||
use std::{fs, io, path::PathBuf, str::FromStr, sync::Arc, time::Instant}; | ||
|
||
use crossterm::terminal::{disable_raw_mode, enable_raw_mode, is_raw_mode_enabled}; | ||
use log::*; | ||
use minotari_app_utilities::{consts, identity_management::setup_node_identity}; | ||
#[cfg(feature = "ledger")] | ||
|
@@ -53,7 +54,7 @@ use tari_common::{ | |
use tari_common_types::{ | ||
key_branches::TransactionKeyManagerBranch, | ||
types::{PrivateKey, PublicKey}, | ||
wallet_types::{LedgerWallet, WalletType}, | ||
wallet_types::{LedgerWallet, ProvidedKeysWallet, WalletType}, | ||
}; | ||
use tari_comms::{ | ||
multiaddr::Multiaddr, | ||
|
@@ -77,7 +78,7 @@ use tari_key_manager::{ | |
}; | ||
use tari_p2p::{peer_seeds::SeedPeer, TransportType}; | ||
use tari_shutdown::ShutdownSignal; | ||
use tari_utilities::{hex::Hex, ByteArray, SafePassword}; | ||
use tari_utilities::{encoding::Base58, hex::Hex, ByteArray, SafePassword}; | ||
use zxcvbn::zxcvbn; | ||
|
||
use crate::{ | ||
|
@@ -98,6 +99,7 @@ pub enum WalletBoot { | |
New, | ||
Existing, | ||
Recovery, | ||
ViewAndSpendKey, | ||
} | ||
|
||
/// Get and confirm a passphrase from the user, with feedback | ||
|
@@ -758,6 +760,10 @@ fn boot(cli: &Cli, wallet_config: &WalletConfig) -> Result<WalletBoot, ExitError | |
return Ok(WalletBoot::Recovery); | ||
} | ||
|
||
if !wallet_exists && cli.view_private_key.is_some() && cli.spend_key.is_some() { | ||
return Ok(WalletBoot::ViewAndSpendKey); | ||
} | ||
|
||
if wallet_exists { | ||
// normal startup of existing wallet | ||
Ok(WalletBoot::Existing) | ||
|
@@ -780,7 +786,8 @@ fn boot(cli: &Cli, wallet_config: &WalletConfig) -> Result<WalletBoot, ExitError | |
|
||
loop { | ||
println!("1. Create a new wallet."); | ||
println!("2. Recover wallet from seed words."); | ||
println!("2. Recover wallet from seed words or hardware device."); | ||
println!("3. Create a read-only wallet using a view key."); | ||
let readline = rl.readline(">> "); | ||
match readline { | ||
Ok(line) => { | ||
|
@@ -793,6 +800,9 @@ fn boot(cli: &Cli, wallet_config: &WalletConfig) -> Result<WalletBoot, ExitError | |
// recover wallet | ||
return Ok(WalletBoot::Recovery); | ||
}, | ||
"3" => { | ||
return Ok(WalletBoot::ViewAndSpendKey); | ||
}, | ||
_ => continue, | ||
} | ||
}, | ||
|
@@ -833,6 +843,10 @@ pub(crate) fn boot_with_password( | |
debug!(target: LOG_TARGET, "Prompting for passphrase for existing wallet."); | ||
prompt_password("Enter wallet passphrase: ")? | ||
}, | ||
WalletBoot::ViewAndSpendKey => { | ||
debug!(target: LOG_TARGET, "Prompting for passphrase for view key wallet."); | ||
get_new_passphrase("Create wallet passphrase: ", "Confirm wallet passphrase: ")? | ||
}, | ||
}; | ||
|
||
Ok((boot_mode, password)) | ||
|
@@ -842,12 +856,47 @@ pub fn prompt_wallet_type( | |
boot_mode: WalletBoot, | ||
wallet_config: &WalletConfig, | ||
non_interactive: bool, | ||
view_private_key: Option<String>, | ||
spend_key: Option<String>, | ||
) -> Option<WalletType> { | ||
if non_interactive { | ||
if non_interactive && !matches!(boot_mode, WalletBoot::ViewAndSpendKey) { | ||
return Some(WalletType::default()); | ||
} | ||
|
||
match boot_mode { | ||
WalletBoot::ViewAndSpendKey => { | ||
let view_key = if let Some(vk) = view_private_key { | ||
match PrivateKey::from_base58(&vk) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in the command window you use hex, here its base58? |
||
Ok(pk) => pk, | ||
Err(_) => { | ||
println!("Invalid view key provided"); | ||
panic!("Invalid view key provided"); | ||
}, | ||
} | ||
} else { | ||
prompt_private_key("Enter view key: ").expect("View key provided was invalid") | ||
}; | ||
let spend_key = if | ||
let Some(sk) = spend_key { | ||
let spend_key = match PublicKey::from_base58(&sk) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as other command hex vs base58 |
||
Ok(pk) => pk, | ||
Err(_) => { | ||
println!("Invalid spend key provided"); | ||
panic!("Invalid spend key provided"); | ||
}, | ||
}; | ||
spend_key | ||
} else { | ||
let spend_key = prompt_public_key("Enter spend key: ").expect("Spend key provided was invalid"); | ||
spend_key | ||
}; | ||
|
||
Some(WalletType::ProvidedKeys(ProvidedKeysWallet { | ||
view_key, | ||
public_spend_key: spend_key, | ||
private_spend_key: None, | ||
})) | ||
}, | ||
WalletBoot::New | WalletBoot::Recovery => { | ||
#[cfg(not(feature = "ledger"))] | ||
return Some(WalletType::default()); | ||
|
@@ -905,6 +954,46 @@ pub fn prompt_ledger_account(boot_mode: WalletBoot) -> Option<u64> { | |
} | ||
} | ||
|
||
pub fn prompt_private_key(prompt: &str) -> Option<PrivateKey> { | ||
// see what we type, as we type it | ||
let must_re_enable_raw_mode = is_raw_mode_enabled().expect("Could not determine raw mode status"); | ||
disable_raw_mode().expect("Could not disable raw mode"); | ||
|
||
println!("{} (hex)", prompt); | ||
let mut input = "".to_string(); | ||
io::stdin().read_line(&mut input).unwrap(); | ||
let input = input.trim(); | ||
if must_re_enable_raw_mode { | ||
enable_raw_mode().expect("Could not enable raw mode"); | ||
} | ||
match PrivateKey::from_canonical_bytes(&Vec::<u8>::from_hex(input).expect("Bad hex data")) { | ||
Ok(pk) => Some(pk), | ||
Err(e) => { | ||
panic!("Bad private key: {}", e) | ||
}, | ||
} | ||
} | ||
|
||
pub fn prompt_public_key(prompt: &str) -> Option<PublicKey> { | ||
// see what we type, as we type it | ||
let must_re_enable_raw_mode = is_raw_mode_enabled().expect("Could not determine raw mode status"); | ||
disable_raw_mode().expect("Could not disable raw mode"); | ||
println!("{} (hex or base58)", prompt); | ||
let mut input = "".to_string(); | ||
io::stdin().read_line(&mut input).unwrap(); | ||
if must_re_enable_raw_mode { | ||
enable_raw_mode().expect("Could not enable raw mode"); | ||
} | ||
let input = input.trim(); | ||
match PublicKey::from_hex(input) { | ||
Ok(pk) => Some(pk), | ||
Err(_) => match PublicKey::from_base58(input) { | ||
Ok(pk) => Some(pk), | ||
Err(_) => None, | ||
}, | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use tari_utilities::SafePassword; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -353,10 +353,7 @@ where TBackend: KeyManagerBackend<PublicKey> + 'static | |
|
||
// If we're trying to access any of the private keys, just say no bueno | ||
if &TransactionKeyManagerBranch::Spend.get_branch_key() == branch { | ||
return wallet | ||
.private_spend_key | ||
.clone() | ||
.ok_or(KeyManagerServiceError::ImportedPrivateKeyInaccessible); | ||
return Ok(PrivateKey::default()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this will have a conflict anyway, make sure to choose the current code in origin. This error here is fixed. |
||
} | ||
}, | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should not use this trait:
#6447