11use ed25519_dalek:: SigningKey ;
2- use failure:: bail;
2+ use failure:: { bail, ResultExt } ;
33use log:: debug;
44
55use crate :: {
6- config:: Seed ,
6+ config:: { self , ConfigDiscriminants , Seed } ,
77 types:: { SeedExplorerError , SeedExplorerResult } ,
88} ;
99use 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-
1711pub 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}
0 commit comments