Skip to content
Merged
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
2 changes: 1 addition & 1 deletion secretstore/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "secretstore"
version = "1.0.0"
version = "1.1.0"
description = "Store a secret (such as a private key) in an encrypted file"
license = "MIT"
edition = "2021"
Expand Down
2 changes: 1 addition & 1 deletion secretstore/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ mod compat_backtest;
mod test_secretstore;

// re-exports
pub use crate::secretstore::{SecretStore, SecretStoreCreator};
pub use crate::secretstore::{Options, SecretStore, SecretStoreCreator};
31 changes: 25 additions & 6 deletions secretstore/src/secretstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ enum FormatVersion {
One = 1,
}

/// Various config options for usage, such as allow weak password.
#[derive(Default)]
pub struct Options {
/// If set, allow weak passowrd, skip password strength check.
allow_weak_password: bool,
}

const FORMAT_VERSION_LATEST: FormatVersion = FormatVersion::One;
const FORMAT_VERSION_OLDEST: FormatVersion = FormatVersion::One;

Expand Down Expand Up @@ -129,7 +136,7 @@ impl SecretStore {
&self,
path_for_secret_file: &str,
encryption_password: &str,
allow_weak_password: Option<bool>,
allow_weak_password: Option<Options>,
) -> Result<(), String> {
let file_exists = fs::exists(path_for_secret_file).map_err(|e| {
format!(
Expand Down Expand Up @@ -196,7 +203,7 @@ impl SecretStore {
pub fn assemble_encrypted_payload(
&self,
encryption_password: &str,
allow_weak_password: Option<bool>,
allow_weak_password: Option<Options>,
) -> Result<Vec<u8>, String> {
let mut encrypted = self.scrambled_secret_data.clone();
let _res = encrypt_scrambled_secret_data(
Expand All @@ -205,7 +212,7 @@ impl SecretStore {
self.encryption_version,
encryption_password,
&self.encryption_aux_data,
allow_weak_password,
allow_weak_password.unwrap_or_default().allow_weak_password,
)?;
assemble_payload(
self.format_version,
Expand Down Expand Up @@ -303,7 +310,7 @@ impl SecretStoreCreator {
secretstore: &SecretStore,
path_for_secret_file: &str,
encryption_password: &str,
allow_weak_password: Option<bool>,
allow_weak_password: Option<Options>,
) -> Result<(), String> {
secretstore.write_to_file(
path_for_secret_file,
Expand All @@ -322,6 +329,18 @@ impl FormatVersion {
}
}

impl Options {
pub fn new() -> Self {
Self::default()
}

/// Allow weak password, skip passowrd strength check
pub fn allow_weak_password(mut self) -> Self {
self.allow_weak_password = true;
self
}
}

fn read_payload_from_file(path_for_secret_file: &str) -> Result<Vec<u8>, String> {
let contents = fs::read(path_for_secret_file).map_err(|e| {
format!(
Expand Down Expand Up @@ -740,9 +759,9 @@ fn encrypt_scrambled_secret_data(
encryption_version: EncryptionVersion,
encryption_password: &str,
aux_data: &EncryptionAuxData,
allow_weak_password: Option<bool>,
allow_weak_password: bool,
) -> Result<(), String> {
if !(allow_weak_password.unwrap_or(false)) {
if !allow_weak_password {
let _res = SecretStore::validate_password(encryption_password)?;
}

Expand Down
8 changes: 6 additions & 2 deletions secretstore/src/test_secretstore.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{SecretStore, SecretStoreCreator};
use crate::{Options, SecretStore, SecretStoreCreator};
use hex_conservative::{DisplayHex, FromHex};
use rand::Rng;
use std::env::temp_dir;
Expand Down Expand Up @@ -278,7 +278,11 @@ fn write_to_file_weak_password() {
let temp_file = get_temp_file_name();
let password = PASSWORD1.to_owned();
let _res = store
.write_to_file(&temp_file, &password, Some(true))
.write_to_file(
&temp_file,
&password,
Some(Options::new().allow_weak_password()),
)
.unwrap();

// check the file
Expand Down
6 changes: 4 additions & 2 deletions seedstore-tool/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
[package]
name = "seedstore-tool"
version = "1.0.0"
version = "1.1.0"
description = "Store a secret (such as a private key) in an encrypted file"
license = "MIT"
edition = "2021"

[dependencies]
bip39 = "2.1.0"
seedstore = { version = "1.0.0", path = "../seedstore", features = ["toolhelper"] }
seedstore = { version = "1.1.0", path = "../seedstore", features = ["toolhelper"] }
4 changes: 2 additions & 2 deletions seedstore/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "seedstore"
version = "1.0.0"
version = "1.1.0"
description = "Store bitcoin secret material (BIP39 mnemonic entropy, or similar) in an encrypted file"
license = "MIT"
edition = "2021"
Expand All @@ -16,7 +16,7 @@ toolhelper = ["rpassword"]
bip39 = { version = "2.1.0", features = ["zeroize"] }
bitcoin = "0.32.5"
rpassword = { version = "7.4.0", optional = true }
secretstore = { version = "1.0.0", path = "../secretstore" }
secretstore = { version = "1.1.0", path = "../secretstore" }
zeroize = "1.8.1"

[dev-dependencies]
Expand Down
6 changes: 3 additions & 3 deletions seedstore/src/keystore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use bitcoin::key::Secp256k1;
use bitcoin::secp256k1::ecdsa::Signature;
use bitcoin::secp256k1::{All, Message, PublicKey, SecretKey, Signing};
use secretstore::{SecretStore, SecretStoreCreator};
use secretstore::{Options, SecretStore, SecretStoreCreator};
use zeroize::{Zeroize, ZeroizeOnDrop};

const NONSECRET_DATA_LEN: usize = 4;
Expand Down Expand Up @@ -114,7 +114,7 @@ impl KeyStore {
&self,
path_for_secret_file: &str,
encryption_password: &str,
allow_weak_password: Option<bool>,
allow_weak_password: Option<Options>,
) -> Result<(), String> {
SecretStoreCreator::write_to_file(
&self.secretstore,
Expand Down Expand Up @@ -205,7 +205,7 @@ impl KeyStoreCreator {
seedstore: &KeyStore,
path_for_secret_file: &str,
encryption_password: &str,
allow_weak_password: Option<bool>,
allow_weak_password: Option<Options>,
) -> Result<(), String> {
seedstore.write_to_file(
path_for_secret_file,
Expand Down
1 change: 1 addition & 0 deletions seedstore/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ pub use crate::keystore::{KeyStore, KeyStoreCreator};
pub use crate::seedstore::{ChildSpecifier, SeedStore, SeedStoreCreator};
#[cfg(feature = "toolhelper")]
pub use crate::tool::SeedStoreTool;
pub use secretstore::Options;
6 changes: 3 additions & 3 deletions seedstore/src/seedstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use bitcoin::key::{Keypair, Secp256k1};
use bitcoin::secp256k1::ecdsa::Signature;
use bitcoin::secp256k1::{All, Message, PublicKey, SecretKey};
use bitcoin::{Address, CompressedPublicKey, Network, NetworkKind};
use secretstore::{SecretStore, SecretStoreCreator};
use secretstore::{Options, SecretStore, SecretStoreCreator};
use std::str::FromStr;
use zeroize::{Zeroize, ZeroizeOnDrop};

Expand Down Expand Up @@ -114,7 +114,7 @@ impl SeedStore {
&self,
path_for_secret_file: &str,
encryption_password: &str,
allow_weak_password: Option<bool>,
allow_weak_password: Option<Options>,
) -> Result<(), String> {
SecretStoreCreator::write_to_file(
&self.secretstore,
Expand Down Expand Up @@ -423,7 +423,7 @@ impl SeedStoreCreator {
seedstore: &SeedStore,
path_for_secret_file: &str,
encryption_password: &str,
allow_weak_password: Option<bool>,
allow_weak_password: Option<Options>,
) -> Result<(), String> {
seedstore.write_to_file(
path_for_secret_file,
Expand Down
17 changes: 9 additions & 8 deletions seedstore/src/tool.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
///! Utility tool implementation: tool to create or check an encrypted secret seed file.
use crate::{SeedStore, SeedStoreCreator};
use crate::{Options, SeedStore, SeedStoreCreator};
use bip39::Mnemonic;
use std::io::{self, stdout, BufRead, Write};
use std::{fs, str::FromStr};
Expand Down Expand Up @@ -218,13 +218,14 @@ impl SeedStoreTool {

let xpub = self.print_info(&seedstore)?;

let _res = SeedStoreCreator::write_to_file(
&seedstore,
&self.config.filename,
&password,
Some(self.config.allow_weak_password),
)
.map_err(|e| format!("Could not write secret file, {}", e))?;
let options = if self.config.allow_weak_password {
Some(Options::new().allow_weak_password())
} else {
None
};
let _res =
SeedStoreCreator::write_to_file(&seedstore, &self.config.filename, &password, options)
.map_err(|e| format!("Could not write secret file, {}", e))?;

println!("Seed written to encrypted file: {}", self.config.filename);

Expand Down