Skip to content

Commit

Permalink
feat: adds a create combo key cli command for wallet (#4751)
Browse files Browse the repository at this point in the history
Description
---
Adds a `create_combo_key` method to wallet. The main goal is to allow the user to create a new pair of (secret key, public key), by passing in a given string value.

Motivation and Context
---
Wallet needs a terminal command to create a new public key and secret key from the key manager service.
This key needs to be created based on a provided string seed and send back to the user. 

The key manager service derives a key based on the string and the master seed of the wallet. We need to add this seed string as a branch on the local instance of the key manager and then we can track if the user asks for the key twice.

The current command is needed in order to as a firs step to execute an m-of-n multi-party script payment protocol. See [here](https://demo.hedgedoc.org/rgLUGP_YRiGKjKkgehiG4w#Step-2) and [here](https://demo.hedgedoc.org/0Lb9sKovSb-ct3g3E4A-9A?view) for more details.

How Has This Been Tested?
---
Unit tests
  • Loading branch information
jorgeantonio21 authored Oct 3, 2022
1 parent 88b75dc commit c9019a8
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 4 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions applications/tari_console_wallet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ tari_app_grpc = { path = "../tari_app_grpc" }
tari_shutdown = { path = "../../infrastructure/shutdown" }
tari_key_manager = { path = "../../base_layer/key_manager" }
tari_utilities = { git = "https://github.com/tari-project/tari_utilities.git", tag = "v0.4.5" }
zeroize = "1.5"

# Uncomment for tokio tracing via tokio-console (needs "tracing" featurs)
#console-subscriber = "0.1.3"
Expand Down
17 changes: 16 additions & 1 deletion applications/tari_console_wallet/src/automation/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ use tari_utilities::{hex::Hex, ByteArray};
use tari_wallet::{
connectivity_service::WalletConnectivityInterface,
error::WalletError,
key_manager_service::NextKeyResult,
key_manager_service::{KeyManagerInterface, NextKeyResult},
output_manager_service::{handle::OutputManagerHandle, UtxoSelectionCriteria},
transaction_service::handle::{TransactionEvent, TransactionServiceHandle},
TransactionStage,
Expand All @@ -68,6 +68,7 @@ use tokio::{
sync::{broadcast, mpsc},
time::{sleep, timeout},
};
use zeroize::Zeroizing;

use super::error::CommandError;
use crate::{
Expand All @@ -84,6 +85,7 @@ pub enum WalletCommand {
GetBalance,
SendTari,
SendOneSided,
CreateKeyPair,
MakeItRain,
CoinSplit,
DiscoverPeer,
Expand Down Expand Up @@ -583,6 +585,7 @@ pub async fn command_runner(

let mut transaction_service = wallet.transaction_service.clone();
let mut output_service = wallet.output_manager_service.clone();
let key_manager_service = wallet.key_manager_service.clone();
let dht_service = wallet.dht_service.discovery_service_requester().clone();
let connectivity_requester = wallet.comms.connectivity();
let mut online = false;
Expand Down Expand Up @@ -637,6 +640,18 @@ pub async fn command_runner(
Err(e) => eprintln!("BurnTari error! {}", e),
}
},
CreateKeyPair(args) => match key_manager_service.create_key_pair(args.key_branch).await {
Ok((sk, pk)) => {
println!(
"New key pair:
1. secret key: {},
2. public key: {}",
*Zeroizing::new(sk.to_hex()),
pk.to_hex()
)
},
Err(e) => eprintln!("CreateKeyPair error! {}", e),
},
SendTari(args) => {
match send_tari(
transaction_service.clone(),
Expand Down
6 changes: 6 additions & 0 deletions applications/tari_console_wallet/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ pub enum CliCommands {
GetBalance,
SendTari(SendTariArgs),
BurnTari(BurnTariArgs),
CreateKeyPair(CreateKeyPairArgs),
SendOneSided(SendTariArgs),
SendOneSidedToStealthAddress(SendTariArgs),
MakeItRain(MakeItRainArgs),
Expand Down Expand Up @@ -155,6 +156,11 @@ pub struct BurnTariArgs {
pub message: String,
}

#[derive(Debug, Args, Clone)]
pub struct CreateKeyPairArgs {
pub key_branch: String,
}

#[derive(Debug, Args, Clone)]
pub struct MakeItRainArgs {
pub destination: UniPublicKey,
Expand Down
15 changes: 14 additions & 1 deletion applications/tari_console_wallet/src/wallet_modes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,8 @@ mod test {
burn-tari --message Ups_these_funds_will_be_burned! 100T
create-key-pair pie
coin-split --message Make_many_dust_UTXOs! --fee-per-gram 2 0.001T 499
make-it-rain --duration 100 --transactions-per-second 10 --start-amount 0.009200T --increase-amount 0T \
Expand All @@ -444,6 +446,7 @@ mod test {
let mut get_balance = false;
let mut send_tari = false;
let mut burn_tari = false;
let mut create_key_pair = false;
let mut make_it_rain = false;
let mut coin_split = false;
let mut discover_peer = false;
Expand All @@ -453,6 +456,7 @@ mod test {
CliCommands::GetBalance => get_balance = true,
CliCommands::SendTari(_) => send_tari = true,
CliCommands::BurnTari(_) => burn_tari = true,
CliCommands::CreateKeyPair(_) => create_key_pair = true,
CliCommands::SendOneSided(_) => {},
CliCommands::SendOneSidedToStealthAddress(_) => {},
CliCommands::MakeItRain(_) => make_it_rain = true,
Expand All @@ -472,6 +476,15 @@ mod test {
CliCommands::HashGrpcPassword(_) => {},
}
}
assert!(get_balance && send_tari && burn_tari && make_it_rain && coin_split && discover_peer && whois);
assert!(
get_balance &&
send_tari &&
burn_tari &&
create_key_pair &&
make_it_rain &&
coin_split &&
discover_peer &&
whois
);
}
}
20 changes: 19 additions & 1 deletion base_layer/wallet/src/key_manager_service/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
use std::sync::Arc;

use chacha20poly1305::XChaCha20Poly1305;
use tari_common_types::types::PrivateKey;
use tari_common_types::types::{PrivateKey, PublicKey};
use tari_key_manager::cipher_seed::CipherSeed;
use tokio::sync::RwLock;

Expand Down Expand Up @@ -73,6 +73,24 @@ where TBackend: KeyManagerBackend + 'static
(*self.key_manager_inner).write().await.apply_encryption(cipher)
}

async fn create_key_pair<T: Into<String> + Send>(
&self,
branch: T,
) -> Result<(PrivateKey, PublicKey), KeyManagerServiceError> {
let branch: String = branch.into();

self.key_manager_inner
.write()
.await
.add_key_manager_branch(branch.clone())?;

let next_key = self.get_next_key(branch).await?;
let sk = next_key.key.clone();
let pk = next_key.to_public_key();

Ok((sk, pk))
}

async fn remove_encryption(&self) -> Result<(), KeyManagerServiceError> {
(*self.key_manager_inner).write().await.remove_encryption()
}
Expand Down
6 changes: 6 additions & 0 deletions base_layer/wallet/src/key_manager_service/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ pub trait KeyManagerInterface: Clone + Send + Sync + 'static {
index: u64,
) -> Result<PrivateKey, KeyManagerServiceError>;

/// Gets new key combo pair out of a key seed
async fn create_key_pair<T: Into<String> + Send>(
&self,
branch: T,
) -> Result<(PrivateKey, PublicKey), KeyManagerServiceError>;

/// Searches the branch to find the index used to generated the key, O(N) where N = index used.
async fn find_key_index<T: Into<String> + Send>(
&self,
Expand Down
9 changes: 8 additions & 1 deletion base_layer/wallet/src/key_manager_service/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

use chacha20poly1305::XChaCha20Poly1305;
use log::*;
use tari_common_types::types::PrivateKey;
use tari_common_types::types::{PrivateKey, PublicKey};
use tari_key_manager::{cipher_seed::CipherSeed, key_manager::KeyManager};
use tokio::sync::RwLock;

Expand Down Expand Up @@ -162,6 +162,13 @@ impl KeyManagerInterface for KeyManagerMock {
unimplemented!("Not supported");
}

async fn create_key_pair<T: Into<String> + Send>(
&self,
_branch: T,
) -> Result<(PrivateKey, PublicKey), KeyManagerServiceError> {
unimplemented!("Not supported");
}

async fn find_key_index<T: Into<String> + Send>(
&self,
branch: T,
Expand Down

0 comments on commit c9019a8

Please sign in to comment.