Skip to content

Mnemonic wallet recovery #97

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions client/src/bin/space-cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ extern crate core;

use std::{
fs, io,
io::{Cursor, IsTerminal},
io::{Cursor, IsTerminal, Write},
path::PathBuf,
};

Expand Down Expand Up @@ -78,6 +78,9 @@ enum Commands {
/// Generate a new wallet
#[command(name = "createwallet")]
CreateWallet,
/// Restore wallet from mnemonic phrase
#[command(name = "restorewallet")]
RestoreWallet,
/// Load a wallet
#[command(name = "loadwallet")]
LoadWallet,
Expand Down Expand Up @@ -582,7 +585,17 @@ async fn handle_commands(cli: &SpaceCli, command: Commands) -> Result<(), Client
print_list_wallets(result, cli.format);
}
Commands::CreateWallet => {
cli.client.wallet_create(&cli.wallet).await?;
let response = cli.client.wallet_create(&cli.wallet).await?;
println!("⚠️ Write down your recovery phrase NOW!");
println!("This is the ONLY time it will be shown:");
println!("{}", &response);
}
Commands::RestoreWallet => {
print!("Enter mnemonic phrase: ");
io::stdout().flush().unwrap();
let mut mnemonic = String::new();
io::stdin().read_line(&mut mnemonic).unwrap();
cli.client.wallet_restore(&cli.wallet, mnemonic).await?;
}
Commands::LoadWallet => {
cli.client.wallet_load(&cli.wallet).await?;
Expand Down
31 changes: 25 additions & 6 deletions client/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,10 @@ pub trait Rpc {
async fn wallet_export(&self, name: &str) -> Result<WalletExport, ErrorObjectOwned>;

#[method(name = "walletcreate")]
async fn wallet_create(&self, name: &str) -> Result<(), ErrorObjectOwned>;
async fn wallet_create(&self, name: &str) -> Result<String, ErrorObjectOwned>;

#[method(name = "walletrestore")]
async fn wallet_restore(&self, name: &str, mnemonic: String) -> Result<(), ErrorObjectOwned>;

#[method(name = "walletsendrequest")]
async fn wallet_send_request(
Expand Down Expand Up @@ -491,11 +494,18 @@ impl WalletManager {
Ok(export)
}

pub async fn create_wallet(&self, client: &reqwest::Client, name: &str) -> anyhow::Result<()> {
pub async fn create_wallet(&self, client: &reqwest::Client, name: &str) -> anyhow::Result<String> {
let mnemonic: GeneratedKey<_, Tap> =
Mnemonic::generate((WordCount::Words12, Language::English))
.map_err(|_| anyhow!("Mnemonic generation error"))?;

let start_block = self.get_wallet_start_block(client).await?;
self.setup_new_wallet(name.to_string(), mnemonic.to_string(), start_block)?;
self.load_wallet(name).await?;
Ok(mnemonic.to_string())
}

pub async fn restore_wallet(&self, client: &reqwest::Client, name: &str, mnemonic: &str) -> anyhow::Result<()> {
let start_block = self.get_wallet_start_block(client).await?;
self.setup_new_wallet(name.to_string(), mnemonic.to_string(), start_block)?;
self.load_wallet(name).await?;
Expand All @@ -513,7 +523,7 @@ impl WalletManager {
return Err(anyhow!(format!("Wallet `{}` already exists", name)));
}

let export = self.wallet_from_mnemonic(name.clone(), mnemonic.to_string(), start_block)?;
let export = self.wallet_from_mnemonic(name.clone(), mnemonic, start_block)?;
fs::create_dir_all(&wallet_path)?;
let wallet_export_path = wallet_path.join("wallet.json");
let mut file = fs::File::create(wallet_export_path)?;
Expand All @@ -528,7 +538,7 @@ impl WalletManager {
start_block: BlockId,
) -> anyhow::Result<WalletExport> {
let (network, _) = self.fallback_network();
let xpriv = Self::descriptor_from_mnemonic(network, &mnemonic.to_string())?;
let xpriv = Self::descriptor_from_mnemonic(network, &mnemonic)?;

let (external, internal) = Self::default_descriptors(xpriv);
let tmp = bdk::Wallet::create(external, internal)
Expand Down Expand Up @@ -653,7 +663,7 @@ impl WalletManager {
}

fn descriptor_from_mnemonic(network: Network, m: &str) -> anyhow::Result<Xpriv> {
let mnemonic = Mnemonic::parse(m).unwrap();
let mnemonic = Mnemonic::parse(m)?;
let xkey: ExtendedKey = mnemonic.clone().into_extended_key()?;
Ok(xkey.into_xprv(network).expect("xpriv"))
}
Expand Down Expand Up @@ -908,7 +918,7 @@ impl RpcServer for RpcServerImpl {
})
}

async fn wallet_create(&self, name: &str) -> Result<(), ErrorObjectOwned> {
async fn wallet_create(&self, name: &str) -> Result<String, ErrorObjectOwned> {
self.wallet_manager
.create_wallet(&self.client, name)
.await
Expand All @@ -917,6 +927,15 @@ impl RpcServer for RpcServerImpl {
})
}

async fn wallet_restore(&self, name: &str, mnemonic: String) -> Result<(), ErrorObjectOwned> {
self.wallet_manager
.restore_wallet(&self.client, name, &mnemonic)
.await
.map_err(|error| {
ErrorObjectOwned::owned(RPC_WALLET_NOT_LOADED, error.to_string(), None::<String>)
})
}

async fn wallet_send_request(
&self,
wallet: &str,
Expand Down
Loading