Skip to content

Commit cf296c6

Browse files
committed
WIP: needs refactoring
1 parent 12b3d53 commit cf296c6

File tree

6 files changed

+258
-20
lines changed

6 files changed

+258
-20
lines changed

Cargo.lock

Lines changed: 102 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/src/config.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,21 @@ pub struct Settings {
6161
pub admin: Admin,
6262
}
6363

64+
/// Returns the default derivation path per config version.
65+
pub const fn default_derivation_path(config: ConfigDiscriminants) -> u32 {
66+
match config {
67+
// TODO: what should these be?
68+
ConfigDiscriminants::V1 => 2,
69+
ConfigDiscriminants::V2 => 2,
70+
ConfigDiscriminants::V3 => 2,
71+
}
72+
}
73+
74+
#[allow(clippy::large_enum_variant)]
6475
#[cfg_attr(test, derive(Clone, PartialEq))]
6576
#[derive(Debug, Deserialize, Serialize, EnumDiscriminants)]
6677
#[strum_discriminants(
67-
derive(VariantNames, EnumString, strum::Display),
78+
derive(VariantNames, EnumString, strum::Display, Default),
6879
strum(ascii_case_insensitive)
6980
)]
7081
pub enum Config {
@@ -86,6 +97,7 @@ pub enum Config {
8697
settings: Settings,
8798
},
8899
#[serde(rename = "v3")]
100+
#[strum_discriminants(default)]
89101
V3 {
90102
/// This is the Device Seed Bundle as a base64 string which is compatible with lair-keystore >=v0.0.8
91103
/// And is encoded with a password that will be needed to be used to decrypt it

core/src/utils.rs

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
11
use ed25519_dalek::SigningKey;
2-
use failure::bail;
2+
use failure::{bail, ResultExt};
33
use log::debug;
44

55
use crate::{
6-
config::Seed,
6+
config::{self, ConfigDiscriminants, Seed},
77
types::{SeedExplorerError, SeedExplorerResult},
88
};
99
use hc_seed_bundle::{LockedSeedCipher, UnlockedSeedBundle};
1010

11-
// TODO: what should this be?
12-
pub const DEFAULT_DERIVATION_PATH_V2: u32 = 3;
13-
14-
// TODO: what should this be?
15-
pub const DEFAULT_DERIVATION_PATH_V3: u32 = 3;
16-
1711
pub fn get_seed_from_bundle(device_bundle: &UnlockedSeedBundle) -> Result<Seed, failure::Error> {
1812
let mut seed = Seed::default();
1913

@@ -53,7 +47,9 @@ pub async fn generate_device_bundle(
5347
.await
5448
.unwrap();
5549

56-
let derivation_path = maybe_derivation_path.unwrap_or(DEFAULT_DERIVATION_PATH_V3);
50+
let derivation_path = maybe_derivation_path.unwrap_or(config::default_derivation_path(
51+
ConfigDiscriminants::default(),
52+
));
5753

5854
let device_bundle = master.derive(derivation_path).await.unwrap();
5955

@@ -69,21 +65,25 @@ pub async fn generate_device_bundle(
6965
}
7066

7167
/// Unlock the given device bundle with the given password.
72-
pub async fn get_seed_from_locked_device_bundle(
68+
async fn _get_seed_from_locked_device_bundle(
7369
locked_device_bundle: &[u8],
7470
passphrase: &str,
7571
) -> Result<Seed, failure::Error> {
7672
let passphrase = sodoken::BufRead::from(passphrase.as_bytes());
7773
let unlocked_bundle =
7874
match hc_seed_bundle::UnlockedSeedBundle::from_locked(locked_device_bundle)
79-
.await?
75+
.await
76+
.context("getting seed from locked device bundle")?
8077
.remove(0)
8178
{
82-
hc_seed_bundle::LockedSeedCipher::PwHash(cipher) => cipher.unlock(passphrase).await,
79+
hc_seed_bundle::LockedSeedCipher::PwHash(cipher) => {
80+
cipher.unlock(passphrase).await.context("unlocking cipher")
81+
}
8382
oth => bail!("unexpected cipher: {:?}", oth),
8483
}?;
8584

86-
let seed = get_seed_from_bundle(&unlocked_bundle)?;
85+
let seed =
86+
get_seed_from_bundle(&unlocked_bundle).context("getting seed from unlocked bundle")?;
8787

8888
Ok(seed)
8989
}
@@ -167,4 +167,19 @@ pub(crate) mod tests {
167167
))
168168
.unwrap_err();
169169
}
170+
171+
#[tokio::test(flavor = "multi_thread")]
172+
async fn extract_seed_from_locked_succeeds() {
173+
let encoded_device_bundle = generate_base64().await;
174+
let device_bundle =
175+
base64::decode_config(&encoded_device_bundle, base64::URL_SAFE_NO_PAD).unwrap();
176+
177+
let a = _get_seed_from_locked_device_bundle(&device_bundle, PASSPHRASE)
178+
.await
179+
.unwrap();
180+
181+
let b = unlock(&encoded_device_bundle, PASSPHRASE).await.unwrap();
182+
183+
assert_eq!(a, *b.as_bytes());
184+
}
170185
}

gen-cli/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,8 @@ sha2 = "0.8"
2020
clap = { version = "4.5.16", features = ["derive"] }
2121
base64 = { workspace = true }
2222
tokio = { workspace = true }
23+
24+
[dev-dependencies]
25+
assert_cmd = "2.0"
26+
predicates = "3.1"
27+
once_cell = "1.19"

gen-cli/src/main.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
use hpos_config_core::{
22
config::{ConfigDiscriminants, Seed},
3-
public_key,
4-
utils::get_seed_from_locked_device_bundle,
5-
Config,
3+
public_key, Config,
64
};
75

86
use clap::Parser;
@@ -61,7 +59,7 @@ struct ClapArgs {
6159

6260
#[arg(
6361
long,
64-
default_value_t = ConfigDiscriminants::V3,
62+
default_value_t = ConfigDiscriminants::default(),
6563
ignore_case = true,
6664
help = "Version specifier for the emitted config"
6765
)]
@@ -77,14 +75,16 @@ async fn main() -> Result<(), Error> {
7775
let derivation_path = if let Some(derivation_path) = args.derivation_path {
7876
derivation_path
7977
} else {
80-
hpos_config_core::utils::DEFAULT_DERIVATION_PATH_V2
78+
hpos_config_core::config::default_derivation_path(ConfigDiscriminants::default())
8179
};
8280

8381
// TODO: don't hardcode this
8482
let passphrase = "pass";
8583

8684
let device_bundle = if let Some(device_bundle) = args.device_bundle {
87-
seed = get_seed_from_locked_device_bundle(device_bundle.as_bytes(), passphrase).await?;
85+
seed = hpos_config_core::utils::unlock(&device_bundle, passphrase)
86+
.await?
87+
.to_scalar_bytes();
8888

8989
device_bundle
9090
} else {

0 commit comments

Comments
 (0)