@@ -107,10 +107,13 @@ pub fn create_and_store_seed(password: &SafeInputString, host_entropy: &[u8]) ->
107
107
}
108
108
109
109
pub fn copy_seed ( ) -> Result < zeroize:: Zeroizing < Vec < u8 > > , ( ) > {
110
- let mut seed = zeroize:: Zeroizing :: new ( [ 0u8 ; MAX_SEED_LENGTH ] ) ;
110
+ let mut seed = zeroize:: Zeroizing :: new ( [ 0u8 ; MAX_SEED_LENGTH ] . to_vec ( ) ) ;
111
111
let mut seed_len: usize = 0 ;
112
112
match unsafe { bitbox02_sys:: keystore_copy_seed ( seed. as_mut_ptr ( ) , & mut seed_len) } {
113
- true => Ok ( zeroize:: Zeroizing :: new ( seed[ ..seed_len] . to_vec ( ) ) ) ,
113
+ true => {
114
+ seed. truncate ( seed_len) ;
115
+ Ok ( seed)
116
+ }
114
117
false => Err ( ( ) ) ,
115
118
}
116
119
}
@@ -280,11 +283,7 @@ pub fn bip39_mnemonic_to_seed(mnemonic: &str) -> Result<zeroize::Zeroizing<Vec<u
280
283
281
284
pub fn encrypt_and_store_seed ( seed : & [ u8 ] , password : & SafeInputString ) -> Result < ( ) , Error > {
282
285
match unsafe {
283
- bitbox02_sys:: keystore_encrypt_and_store_seed (
284
- seed. as_ptr ( ) ,
285
- seed. len ( ) ,
286
- password. as_cstr ( ) ,
287
- )
286
+ bitbox02_sys:: keystore_encrypt_and_store_seed ( seed. as_ptr ( ) , seed. len ( ) , password. as_cstr ( ) )
288
287
} {
289
288
keystore_error_t:: KEYSTORE_OK => Ok ( ( ) ) ,
290
289
err => Err ( err. into ( ) ) ,
@@ -330,7 +329,7 @@ pub fn secp256k1_schnorr_bip86_pubkey(pubkey33: &[u8]) -> Result<[u8; 32], ()> {
330
329
#[ cfg( test) ]
331
330
mod tests {
332
331
use super :: * ;
333
- use crate :: testing:: { mock_unlocked, TEST_MNEMONIC } ;
332
+ use crate :: testing:: { mock_unlocked, mock_unlocked_using_mnemonic , TEST_MNEMONIC } ;
334
333
335
334
#[ test]
336
335
fn test_bip39_mnemonic_to_seed ( ) {
@@ -365,6 +364,35 @@ mod tests {
365
364
) ;
366
365
}
367
366
367
+ #[ test]
368
+ fn test_copy_seed ( ) {
369
+ // 12 words
370
+ mock_unlocked_using_mnemonic (
371
+ "trust cradle viable innocent stand equal little small junior frost laundry room" ,
372
+ ) ;
373
+ assert_eq ! (
374
+ copy_seed( ) . unwrap( ) . as_slice( ) ,
375
+ b"\xe9 \xa6 \x3f \xcd \x3a \x4d \x48 \x98 \x20 \xa6 \x63 \x79 \x2b \xad \xf6 \xdd " ,
376
+ ) ;
377
+
378
+ // 18 words
379
+ mock_unlocked_using_mnemonic (
380
+ "pupil parent toe bright slam plastic spy suspect verb battle nominee loan call crystal upset razor luggage join" ,
381
+ ) ;
382
+ assert_eq ! (
383
+ copy_seed( ) . unwrap( ) . as_slice( ) ,
384
+ b"\xad \xf4 \x07 \x8e \x0e \x0c \xb1 \x4c \x34 \xd6 \xd6 \xf2 \x82 \x6a \x57 \xc1 \x82 \x06 \x6a \xbb \xcd \x95 \x84 \xcf " ,
385
+ ) ;
386
+
387
+ mock_unlocked_using_mnemonic (
388
+ "purity concert above invest pigeon category peace tuition hazard vivid latin since legal speak nation session onion library travel spell region blast estate stay" ,
389
+ ) ;
390
+ assert_eq ! (
391
+ copy_seed( ) . unwrap( ) . as_slice( ) ,
392
+ b"\xae \x45 \xd4 \x02 \x3a \xfa \x4a \x48 \x68 \x77 \x51 \x69 \xfe \xa5 \xf5 \xe4 \x97 \xf7 \xa1 \xa4 \xd6 \x22 \x9a \xd0 \x23 \x9e \x68 \x9b \x48 \x2e \xd3 \x5e " ,
393
+ ) ;
394
+ }
395
+
368
396
#[ test]
369
397
fn test_get_bip39_mnemonic ( ) {
370
398
lock ( ) ;
0 commit comments