Skip to content

Commit 9facd79

Browse files
committed
WIP: needs refactoring
1 parent b82bebd commit 9facd79

File tree

6 files changed

+250
-17
lines changed

6 files changed

+250
-17
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) -> usize {
66+
match config {
67+
// TODO: what should these be?
68+
ConfigDiscriminants::V1 => 3,
69+
ConfigDiscriminants::V2 => 3,
70+
ConfigDiscriminants::V3 => 3,
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: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
11
use ed25519_dalek::SigningKey;
2-
use failure::bail;
2+
use failure::{bail, ResultExt};
33

44
use crate::{
55
config::Seed,
66
types::{SeedExplorerError, SeedExplorerResult},
77
};
88
use hc_seed_bundle::{LockedSeedCipher, UnlockedSeedBundle};
99

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

@@ -68,21 +62,25 @@ pub async fn generate_device_bundle(
6862
}
6963

7064
/// Unlock the given device bundle with the given password.
71-
pub async fn get_seed_from_locked_device_bundle(
65+
async fn _get_seed_from_locked_device_bundle(
7266
locked_device_bundle: &[u8],
7367
passphrase: &str,
7468
) -> Result<Seed, failure::Error> {
7569
let passphrase = sodoken::BufRead::from(passphrase.as_bytes());
7670
let unlocked_bundle =
7771
match hc_seed_bundle::UnlockedSeedBundle::from_locked(locked_device_bundle)
78-
.await?
72+
.await
73+
.context("getting seed from locked device bundle")?
7974
.remove(0)
8075
{
81-
hc_seed_bundle::LockedSeedCipher::PwHash(cipher) => cipher.unlock(passphrase).await,
76+
hc_seed_bundle::LockedSeedCipher::PwHash(cipher) => {
77+
cipher.unlock(passphrase).await.context("unlocking cipher")
78+
}
8279
oth => bail!("unexpected cipher: {:?}", oth),
8380
}?;
8481

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

8785
Ok(seed)
8886
}
@@ -160,4 +158,19 @@ pub(crate) mod tests {
160158
))
161159
.unwrap_err();
162160
}
161+
162+
#[tokio::test(flavor = "multi_thread")]
163+
async fn extract_seed_from_locked_succeeds() {
164+
let encoded_device_bundle = generate_base64().await;
165+
let device_bundle =
166+
base64::decode_config(&encoded_device_bundle, base64::URL_SAFE_NO_PAD).unwrap();
167+
168+
let a = _get_seed_from_locked_device_bundle(&device_bundle, PASSPHRASE)
169+
.await
170+
.unwrap();
171+
172+
let b = unlock(&encoded_device_bundle, PASSPHRASE).await.unwrap();
173+
174+
assert_eq!(a, *b.as_bytes());
175+
}
163176
}

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: 5 additions & 5 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
)]
@@ -84,7 +82,9 @@ async fn main() -> Result<(), Error> {
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)