Skip to content

Commit 5334aaf

Browse files
committed
Merge branch 'copy-seed'
2 parents 38c13da + 927bd62 commit 5334aaf

File tree

1 file changed

+36
-8
lines changed

1 file changed

+36
-8
lines changed

src/rust/bitbox02/src/keystore.rs

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,13 @@ pub fn create_and_store_seed(password: &SafeInputString, host_entropy: &[u8]) ->
107107
}
108108

109109
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());
111111
let mut seed_len: usize = 0;
112112
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+
}
114117
false => Err(()),
115118
}
116119
}
@@ -280,11 +283,7 @@ pub fn bip39_mnemonic_to_seed(mnemonic: &str) -> Result<zeroize::Zeroizing<Vec<u
280283

281284
pub fn encrypt_and_store_seed(seed: &[u8], password: &SafeInputString) -> Result<(), Error> {
282285
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())
288287
} {
289288
keystore_error_t::KEYSTORE_OK => Ok(()),
290289
err => Err(err.into()),
@@ -330,7 +329,7 @@ pub fn secp256k1_schnorr_bip86_pubkey(pubkey33: &[u8]) -> Result<[u8; 32], ()> {
330329
#[cfg(test)]
331330
mod tests {
332331
use super::*;
333-
use crate::testing::{mock_unlocked, TEST_MNEMONIC};
332+
use crate::testing::{mock_unlocked, mock_unlocked_using_mnemonic, TEST_MNEMONIC};
334333

335334
#[test]
336335
fn test_bip39_mnemonic_to_seed() {
@@ -365,6 +364,35 @@ mod tests {
365364
);
366365
}
367366

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+
368396
#[test]
369397
fn test_get_bip39_mnemonic() {
370398
lock();

0 commit comments

Comments
 (0)