Browse Source

Generate seed automatically if no seed is present

Smooth out user experience by automatically generating a new seed if there
is none present (and by writing that fact in the log file).
shutdown-taker-if-no-maker-present
Mariusz Klochowicz 3 years ago
parent
commit
78945d5545
No known key found for this signature in database GPG Key ID: 470C865699C8D4D
  1. 2
      .github/workflows/ci.yml
  2. 6
      daemon/src/maker.rs
  3. 27
      daemon/src/seed.rs
  4. 6
      daemon/src/taker.rs

2
.github/workflows/ci.yml

@ -108,7 +108,7 @@ jobs:
sleep 10s # Wait for maker to start\
# The maker-id is generated from the makers seed found in daemon/util/testnet_seeds/maker_seed
target/debug/taker --data-dir=/tmp/taker --generate-seed --maker-id 10d4ba2ac3f7a22da4009d813ff1bc3f404dfe2cc93a32bedf1512aa9951c95e testnet &
target/debug/taker --data-dir=/tmp/taker --maker-id 10d4ba2ac3f7a22da4009d813ff1bc3f404dfe2cc93a32bedf1512aa9951c95e testnet &
sleep 10s # Wait for taker to start
curl --fail http://localhost:8000/api/alive

6
daemon/src/maker.rs

@ -44,10 +44,6 @@ struct Opts {
#[clap(long)]
data_dir: Option<PathBuf>,
/// Generate a seed file within the data directory.
#[clap(long)]
generate_seed: bool,
/// If enabled logs will be in json format
#[clap(short, long)]
json: bool,
@ -160,7 +156,7 @@ async fn main() -> Result<()> {
tokio::fs::create_dir_all(&data_dir).await?;
}
let seed = Seed::initialize(&data_dir.join("maker_seed"), opts.generate_seed).await?;
let seed = Seed::initialize(&data_dir.join("maker_seed")).await?;
let bitcoin_network = opts.network.bitcoin_network();
let ext_priv_key = seed.derive_extended_priv_key(bitcoin_network)?;

27
daemon/src/seed.rs

@ -1,4 +1,4 @@
use anyhow::{anyhow, bail, Result};
use anyhow::{anyhow, Result};
use bdk::bitcoin::util::bip32::ExtendedPrivKey;
use bdk::bitcoin::Network;
use hkdf::Hkdf;
@ -11,23 +11,16 @@ pub struct Seed([u8; 256]);
impl Seed {
/// Initialize a [`Seed`] from a path.
///
/// Fails if the file does not exist or it exists but would be overwritten.
pub async fn initialize(seed_file: &Path, generate: bool) -> Result<Seed> {
let exists = seed_file.exists();
let seed = match (exists, generate) {
(true, false) => Seed::read_from(seed_file).await?,
(false, true) => {
let seed = Seed::default();
seed.write_to(seed_file).await?;
seed
}
(true, true) => bail!("Refusing to overwrite seed at {}", seed_file.display()),
(false, false) => bail!("Seed file at {} does not exist", seed_file.display()),
/// Generates new seed if there was no seed found in the given path
pub async fn initialize(seed_file: &Path) -> Result<Seed> {
let seed = if !seed_file.exists() {
tracing::info!("No seed found. Generating new seed");
let seed = Seed::default();
seed.write_to(seed_file).await?;
seed
} else {
Seed::read_from(seed_file).await?
};
Ok(seed)
}

6
daemon/src/taker.rs

@ -43,10 +43,6 @@ struct Opts {
#[clap(long)]
data_dir: Option<PathBuf>,
/// Generate a seed file within the data directory.
#[clap(long)]
generate_seed: bool,
/// If enabled logs will be in json format
#[clap(short, long)]
json: bool,
@ -159,7 +155,7 @@ async fn main() -> Result<()> {
tokio::fs::create_dir_all(&data_dir).await?;
}
let seed = Seed::initialize(&data_dir.join("taker_seed"), opts.generate_seed).await?;
let seed = Seed::initialize(&data_dir.join("taker_seed")).await?;
let bitcoin_network = opts.network.bitcoin_network();
let ext_priv_key = seed.derive_extended_priv_key(bitcoin_network)?;

Loading…
Cancel
Save