Skip to content

Commit

Permalink
set seed phrase from file, bump version number
Browse files Browse the repository at this point in the history
  • Loading branch information
Ash-L2L committed Feb 29, 2024
1 parent 87ecae8 commit 1d0e83d
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 26 deletions.
6 changes: 4 additions & 2 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion app/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "plain_bitnames_app"
version = "0.5.2"
version = "0.5.3"
edition = "2021"
authors = [ "Ash Manning <[email protected]>" ]

Expand Down
4 changes: 4 additions & 0 deletions app/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ impl App {
.enable_all()
.build()?;
let wallet = Wallet::new(&config.datadir.join("wallet.mdb"))?;
if let Some(seed_phrase_path) = &config.mnemonic_seed_phrase_path {
let mnemonic = std::fs::read_to_string(seed_phrase_path)?;
let () = wallet.set_seed_from_mnemonic(mnemonic.as_str())?;
}
let miner = Miner::new(
THIS_SIDECHAIN,
config.main_addr,
Expand Down
15 changes: 9 additions & 6 deletions app/cli.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use clap::Parser;
use std::{net::SocketAddr, path::PathBuf};

#[derive(Parser)]
#[derive(Clone, Debug, Parser)]
#[command(author, version, about, long_about = None)]
pub struct Cli {
/// data directory for storing blockchain data and wallet, defaults to ~/.local/share
Expand All @@ -16,6 +16,9 @@ pub struct Cli {
/// address to connect to mainchain node RPC server, defaults to 127.0.0.1:18443
#[arg(short, long)]
pub main_addr: Option<String>,
/// Path to a mnemonic seed phrase
#[arg(long)]
pub mnemonic_seed_phrase_path: Option<PathBuf>,
/// address to use for P2P networking, defaults to 127.0.0.1:4000
#[arg(short, long)]
pub net_addr: Option<String>,
Expand All @@ -42,14 +45,15 @@ pub struct Config {
pub main_addr: SocketAddr,
pub main_password: String,
pub main_user: String,
pub mnemonic_seed_phrase_path: Option<PathBuf>,
pub net_addr: SocketAddr,
pub rpc_addr: SocketAddr,
#[cfg(all(not(target_os = "windows"), feature = "zmq"))]
pub zmq_addr: SocketAddr,
}

impl Cli {
pub fn get_config(&self) -> anyhow::Result<Config> {
pub fn get_config(self) -> anyhow::Result<Config> {
let datadir = self
.datadir
.clone()
Expand All @@ -58,8 +62,6 @@ impl Cli {
.expect("couldn't get default datadir, specify --datadir")
})
.join("plain_bitnames");
let headless = self.headless;
let log_level = self.log_level;
const DEFAULT_MAIN_ADDR: &str = "127.0.0.1:18443";
let main_addr: SocketAddr = self
.main_addr
Expand Down Expand Up @@ -93,11 +95,12 @@ impl Cli {
.parse()?;
Ok(Config {
datadir,
headless,
log_level,
headless: self.headless,
log_level: self.log_level,
main_addr,
main_password,
main_user,
mnemonic_seed_phrase_path: self.mnemonic_seed_phrase_path,
net_addr,
rpc_addr,
#[cfg(all(not(target_os = "windows"), feature = "zmq"))]
Expand Down
6 changes: 1 addition & 5 deletions app/gui/seed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,8 @@ impl SetSeed {
.add_enabled(mnemonic.is_ok(), egui::Button::new("set"))
.clicked()
{
let mnemonic = mnemonic.expect("should never happen");
let seed = bip39::Seed::new(&mnemonic, &self.passphrase);
app.wallet
.set_seed(
seed.as_bytes().try_into().expect("seed it not 64 bytes"),
)
.set_seed_from_mnemonic(self.seed.as_str())
.expect("failed to set HD wallet seed");
}
}
Expand Down
11 changes: 1 addition & 10 deletions app/rpc_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,18 +121,9 @@ impl RpcServer for RpcServerImpl {
}

async fn set_seed_from_mnemonic(&self, mnemonic: String) -> RpcResult<()> {
let mnemonic =
bip39::Mnemonic::from_phrase(&mnemonic, bip39::Language::English)
.map_err(|err| custom_err(err.to_string()))?;
let seed = bip39::Seed::new(&mnemonic, "");
let seed_bytes: [u8; 64] = seed.as_bytes().try_into().map_err(
|err: <[u8; 64] as TryFrom<&[u8]>>::Error| {
custom_err(err.to_string())
},
)?;
self.app
.wallet
.set_seed(&seed_bytes)
.set_seed_from_mnemonic(mnemonic.as_str())
.map_err(convert_wallet_err)
}

Expand Down
4 changes: 3 additions & 1 deletion lib/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
[package]
name = "plain_bitnames"
version = "0.5.2"
version = "0.5.3"
edition = "2021"
authors = [ "Ash Manning <[email protected]>" ]

[dependencies]
addr = "0.15.6"
anyhow = { version = "1.0.72", features = ["backtrace"] }
bech32 = "0.9.1"
bincode = "1.3.3"
blake3 = "1.4.1"
Expand All @@ -31,6 +32,7 @@ serde_json = "1.0.113"
serde_with = "3.4.0"
sha256 = "1.2.2"
thiserror = "1.0.44"
tiny-bip39 = "1.0.0"
tokio = { version = "1.29.1", features = ["sync"] }
tracing = "0.1.40"
x25519-dalek = { version = "2.0.0", features = ["serde"] }
Expand Down
27 changes: 26 additions & 1 deletion lib/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ pub enum Error {
NotEnoughFunds,
#[error("utxo doesn't exist")]
NoUtxo,
#[error("failed to parse mnemonic seed phrase")]
ParseMnemonic(#[source] anyhow::Error),
#[error("seed has already been set")]
SeedAlreadyExists,
}

#[derive(Clone)]
Expand Down Expand Up @@ -138,7 +142,8 @@ impl Wallet {
Ok(address)
}

pub fn set_seed(&self, seed: &[u8; 64]) -> Result<(), Error> {
/// Overwrite the seed, or set it if it does not already exist.
pub fn overwrite_seed(&self, seed: &[u8; 64]) -> Result<(), Error> {
let mut txn = self.env.write_txn()?;
self.seed.put(&mut txn, &0, seed)?;
self.address_to_index.clear(&mut txn)?;
Expand All @@ -150,6 +155,26 @@ impl Wallet {
Ok(())
}

/// Set the seed, if it does not already exist
pub fn set_seed(&self, seed: &[u8; 64]) -> Result<(), Error> {
if self.has_seed()? {
Err(Error::SeedAlreadyExists)
} else {
self.overwrite_seed(seed)
}
}

/// Set the seed from a mnemonic seed phrase,
/// if the seed does not already exist
pub fn set_seed_from_mnemonic(&self, mnemonic: &str) -> Result<(), Error> {
let mnemonic =
bip39::Mnemonic::from_phrase(mnemonic, bip39::Language::English)
.map_err(Error::ParseMnemonic)?;
let seed = bip39::Seed::new(&mnemonic, "");
let seed_bytes: [u8; 64] = seed.as_bytes().try_into().unwrap();
self.set_seed(&seed_bytes)
}

pub fn has_seed(&self) -> Result<bool, Error> {
let txn = self.env.read_txn()?;
Ok(self.seed.get(&txn, &0)?.is_some())
Expand Down

0 comments on commit 1d0e83d

Please sign in to comment.