rust/keystore: move mock_unlocked() etc to bitbox02_rust::keystore
What changed, and why it matters
This commit is a code cleanup and refactoring change. It moves test-only helper functions (used to simulate an unlocked device during automated tests) from one internal Rust module to another, closer to where they are actually used. It also moves a BIP39 mnemonic-to-seed conversion function into a more appropriate module. There is no change to the actual device firmware behavior, user-facing functionality, or security logic.
No security action required. This is a routine refactoring commit affecting only test infrastructure. Standard code review approval is sufficient.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit relocates mock_unlocked(), mock_unlocked_using_mnemonic(), and TEST_MNEMONIC from bitbox02::testing to bitbox02_rust::keystore::testing, and moves bip39_mnemonic_to_seed() from bitbox02::keystore to bitbox02_rust::bip39 (renamed mnemonic_to_seed). Correspondingly, all #[cfg(test)] modules update their imports. The implementation of mock_unlocked_using_mnemonic is slightly adjusted to call the new location’s mnemonic_to_seed and keystore::unlock_bip39. The C FFI function keystore_mock_unlocked is now wrapped in a new bitbox02::keystore::mock_unlocked helper gated by #[cfg(feature = "testing")]. No production code paths or cryptographic operations are modified.
Changed components
src/rust/bitbox02-rust/src/bip39.rssrc/rust/bitbox02-rust/src/keystore.rssrc/rust/bitbox02-rust/src/keystore/testing module (new)src/rust/bitbox02/src/keystore.rssrc/rust/bitbox02/src/testing.rsVarious Rust test modules updating importsInspect captured patch +101 / −94
diff --git a/src/rust/bitbox02-rust/src/bip39.rs b/src/rust/bitbox02-rust/src/bip39.rs
index 4b68542..5916c5e 100644
--- a/src/rust/bitbox02-rust/src/bip39.rs
+++ b/src/rust/bitbox02-rust/src/bip39.rs
@@ -13,6 +13,7 @@
// limitations under the License.
use alloc::string::{String, ToString};
+use alloc::vec::Vec;
/// `idx` must be smaller than BIP39_WORDLIST_LEN.
pub fn get_word(idx: u16) -> Result<zeroize::Zeroizing<String>, ()> {
@@ -25,6 +26,14 @@ pub fn get_word(idx: u16) -> Result<zeroize::Zeroizing<String>, ()> {
))
}
+/// Decode a BIP39 mnemonic.
+pub fn mnemonic_to_seed(mnemonic: &str) -> Result<zeroize::Zeroizing<Vec<u8>>, ()> {
+ let mnemonic =
+ bip39::Mnemonic::parse_in_normalized(bip39::Language::English, mnemonic).map_err(|_| ())?;
+ let (seed, seed_len) = mnemonic.to_entropy_array();
+ Ok(zeroize::Zeroizing::new(seed[..seed_len].to_vec()))
+}
+
// C API
#[unsafe(no_mangle)]
@@ -93,4 +102,37 @@ mod tests {
assert_eq!(get_word(2047).unwrap().as_ref() as &str, "zoo");
assert_eq!(get_word(563).unwrap().as_ref() as &str, "edit");
}
+
+ #[test]
+ fn test_mnemonic_to_seed() {
+ assert!(mnemonic_to_seed("invalid").is_err());
+
+ // Zero seed
+ assert_eq!(
+ mnemonic_to_seed("abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about").unwrap().as_ref() as &[u8],
+ &[0u8; 16],
+ );
+
+ // 12 words
+ assert_eq!(
+ mnemonic_to_seed(
+ "trust cradle viable innocent stand equal little small junior frost laundry room"
+ )
+ .unwrap()
+ .as_ref() as &[u8],
+ b"\xe9\xa6\x3f\xcd\x3a\x4d\x48\x98\x20\xa6\x63\x79\x2b\xad\xf6\xdd",
+ );
+
+ // 18 words
+ assert_eq!(
+ mnemonic_to_seed("pupil parent toe bright slam plastic spy suspect verb battle nominee loan call crystal upset razor luggage join").unwrap().as_ref() as &[u8],
+ 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",
+ );
+
+ // 24 words
+ assert_eq!(
+ mnemonic_to_seed("purity concert above invest pigeon category peace tuition hazard vivid latin since legal speak nation session onion library travel spell region blast estate stay").unwrap().as_ref() as &[u8],
+ 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",
+ );
+ }
}
diff --git a/src/rust/bitbox02-rust/src/hww/api/backup.rs b/src/rust/bitbox02-rust/src/hww/api/backup.rs
index 690734e..d0c445d 100644
--- a/src/rust/bitbox02-rust/src/hww/api/backup.rs
+++ b/src/rust/bitbox02-rust/src/hww/api/backup.rs
@@ -170,9 +170,10 @@ mod tests {
use super::*;
use crate::hal::testing::TestingHal;
+ use crate::keystore::testing::{mock_unlocked, mock_unlocked_using_mnemonic};
use crate::workflow::testing::Screen;
use alloc::boxed::Box;
- use bitbox02::testing::{mock_memory, mock_unlocked, mock_unlocked_using_mnemonic};
+ use bitbox02::testing::mock_memory;
use util::bb02_async::block_on;
/// Test backup creation on a uninitialized keystore.
diff --git a/src/rust/bitbox02-rust/src/hww/api/bitcoin.rs b/src/rust/bitbox02-rust/src/hww/api/bitcoin.rs
index 5f80d58..71bc7fa 100644
--- a/src/rust/bitbox02-rust/src/hww/api/bitcoin.rs
+++ b/src/rust/bitbox02-rust/src/hww/api/bitcoin.rs
@@ -333,12 +333,11 @@ mod tests {
use crate::bip32::parse_xpub;
use crate::hal::testing::TestingHal;
+ use crate::keystore::testing::{TEST_MNEMONIC, mock_unlocked, mock_unlocked_using_mnemonic};
use crate::workflow::testing::Screen;
use alloc::boxed::Box;
use alloc::vec::Vec;
- use bitbox02::testing::{
- TEST_MNEMONIC, mock_memory, mock_unlocked, mock_unlocked_using_mnemonic,
- };
+ use bitbox02::testing::mock_memory;
use pb::btc_script_config::multisig::ScriptType as MultisigScriptType;
use util::bb02_async::block_on;
use util::bip32::HARDENED;
diff --git a/src/rust/bitbox02-rust/src/hww/api/bitcoin/common.rs b/src/rust/bitbox02-rust/src/hww/api/bitcoin/common.rs
index 86a48e1..a8827f1 100644
--- a/src/rust/bitbox02-rust/src/hww/api/bitcoin/common.rs
+++ b/src/rust/bitbox02-rust/src/hww/api/bitcoin/common.rs
@@ -318,7 +318,7 @@ fn encode_segwit_addr(
mod tests {
use super::*;
- use bitbox02::testing::mock_unlocked_using_mnemonic;
+ use crate::keystore::testing::mock_unlocked_using_mnemonic;
use util::bip32::HARDENED;
#[test]
diff --git a/src/rust/bitbox02-rust/src/hww/api/bitcoin/multisig.rs b/src/rust/bitbox02-rust/src/hww/api/bitcoin/multisig.rs
index 03fb2de..2448f72 100644
--- a/src/rust/bitbox02-rust/src/hww/api/bitcoin/multisig.rs
+++ b/src/rust/bitbox02-rust/src/hww/api/bitcoin/multisig.rs
@@ -328,8 +328,9 @@ pub fn pkscript(
mod tests {
use super::*;
+ use crate::keystore::testing::mock_unlocked_using_mnemonic;
use bip32::parse_xpub;
- use bitbox02::testing::{mock_memory, mock_unlocked_using_mnemonic};
+ use bitbox02::testing::mock_memory;
use util::bip32::HARDENED;
#[test]
diff --git a/src/rust/bitbox02-rust/src/hww/api/bitcoin/policies.rs b/src/rust/bitbox02-rust/src/hww/api/bitcoin/policies.rs
index 0e4af88..8c446a4 100644
--- a/src/rust/bitbox02-rust/src/hww/api/bitcoin/policies.rs
+++ b/src/rust/bitbox02-rust/src/hww/api/bitcoin/policies.rs
@@ -769,7 +769,7 @@ mod tests {
use super::*;
use crate::bip32::parse_xpub;
- use bitbox02::testing::{mock_unlocked, mock_unlocked_using_mnemonic};
+ use crate::keystore::testing::{mock_unlocked, mock_unlocked_using_mnemonic};
const SOME_XPUB_1: &str = "tpubDFj9SBQssRHA5EB1ox58mcgF9sB61br9RGz6UrBukcNKmFe4fPgskZ4wigxQ1jSUzLdjnvvDHL8Z6L3ey5Ev5FNNqrDrePxwXsNHiLZhBTc";
const SOME_XPUB_2: &str = "tpubDCmDXtvJLH9yHLNLnGVRoXBvvacvWskjV4hq4WAmGXcRbfa5uaiybZ7kjGRAFbLaoiw1LcwV56H88avibGh7GC7nqqz2Jcs1dWu33cRKYm4";
diff --git a/src/rust/bitbox02-rust/src/hww/api/bitcoin/registration.rs b/src/rust/bitbox02-rust/src/hww/api/bitcoin/registration.rs
index c7bd227..5370e7e 100644
--- a/src/rust/bitbox02-rust/src/hww/api/bitcoin/registration.rs
+++ b/src/rust/bitbox02-rust/src/hww/api/bitcoin/registration.rs
@@ -190,7 +190,8 @@ mod tests {
use super::*;
use crate::bip32::parse_xpub;
- use bitbox02::testing::{mock_memory, mock_unlocked_using_mnemonic};
+ use crate::keystore::testing::mock_unlocked_using_mnemonic;
+ use bitbox02::testing::mock_memory;
use util::bip32::HARDENED;
use pb::btc_script_config::{Multisig, multisig::ScriptType};
diff --git a/src/rust/bitbox02-rust/src/hww/api/bitcoin/script_configs.rs b/src/rust/bitbox02-rust/src/hww/api/bitcoin/script_configs.rs
index f89ba63..9955b9a 100644
--- a/src/rust/bitbox02-rust/src/hww/api/bitcoin/script_configs.rs
+++ b/src/rust/bitbox02-rust/src/hww/api/bitcoin/script_configs.rs
@@ -75,7 +75,8 @@ impl ValidatedScriptConfigWithKeypath<'_> {
mod tests {
use super::*;
use crate::bip32::parse_xpub;
- use bitbox02::testing::{mock_memory, mock_unlocked};
+ use crate::keystore::testing::mock_unlocked;
+ use bitbox02::testing::mock_memory;
#[test]
fn test_self_transfer_representation_simple_type() {
diff --git a/src/rust/bitbox02-rust/src/hww/api/bitcoin/signmsg.rs b/src/rust/bitbox02-rust/src/hww/api/bitcoin/signmsg.rs
index be1a668..72c3a91 100644
--- a/src/rust/bitbox02-rust/src/hww/api/bitcoin/signmsg.rs
+++ b/src/rust/bitbox02-rust/src/hww/api/bitcoin/signmsg.rs
@@ -138,9 +138,9 @@ mod tests {
use super::*;
use crate::hal::testing::TestingHal;
+ use crate::keystore::testing::mock_unlocked;
use crate::workflow::testing::Screen;
use alloc::boxed::Box;
- use bitbox02::testing::mock_unlocked;
use util::bb02_async::block_on;
use util::bip32::HARDENED;
diff --git a/src/rust/bitbox02-rust/src/hww/api/bitcoin/signtx.rs b/src/rust/bitbox02-rust/src/hww/api/bitcoin/signtx.rs
index 19a1fec..fa823ba 100644
--- a/src/rust/bitbox02-rust/src/hww/api/bitcoin/signtx.rs
+++ b/src/rust/bitbox02-rust/src/hww/api/bitcoin/signtx.rs
@@ -1294,9 +1294,10 @@ mod tests {
use super::*;
use crate::bip32::parse_xpub;
use crate::hal::testing::TestingHal;
+ use crate::keystore::testing::{mock_unlocked, mock_unlocked_using_mnemonic};
use crate::workflow::testing::Screen;
use alloc::boxed::Box;
- use bitbox02::testing::{mock_memory, mock_unlocked, mock_unlocked_using_mnemonic};
+ use bitbox02::testing::mock_memory;
use hex_lit::hex;
use pb::btc_payment_request_request::{Memo, memo};
use util::bb02_async::block_on;
diff --git a/src/rust/bitbox02-rust/src/hww/api/bitcoin/xpubs.rs b/src/rust/bitbox02-rust/src/hww/api/bitcoin/xpubs.rs
index 7546ee3..d5d7139 100644
--- a/src/rust/bitbox02-rust/src/hww/api/bitcoin/xpubs.rs
+++ b/src/rust/bitbox02-rust/src/hww/api/bitcoin/xpubs.rs
@@ -68,7 +68,8 @@ pub async fn process_xpubs(request: &pb::BtcXpubsRequest) -> Result<Response, Er
mod tests {
use super::*;
- use bitbox02::testing::{mock_memory, mock_unlocked, mock_unlocked_using_mnemonic};
+ use crate::keystore::testing::{mock_unlocked, mock_unlocked_using_mnemonic};
+ use bitbox02::testing::mock_memory;
use util::bb02_async::block_on;
use util::bip32::HARDENED;
diff --git a/src/rust/bitbox02-rust/src/hww/api/cardano/address.rs b/src/rust/bitbox02-rust/src/hww/api/cardano/address.rs
index b765944..e0ae4ac 100644
--- a/src/rust/bitbox02-rust/src/hww/api/cardano/address.rs
+++ b/src/rust/bitbox02-rust/src/hww/api/cardano/address.rs
@@ -403,9 +403,9 @@ pub async fn process(
mod tests {
use super::*;
use crate::hal::testing::TestingHal;
+ use crate::keystore::testing::mock_unlocked;
use crate::workflow::testing::Screen;
use alloc::boxed::Box;
- use bitbox02::testing::mock_unlocked;
use util::bb02_async::block_on;
use util::bip32::HARDENED;
diff --git a/src/rust/bitbox02-rust/src/hww/api/cardano/sign_transaction.rs b/src/rust/bitbox02-rust/src/hww/api/cardano/sign_transaction.rs
index b3a5042..401780f 100644
--- a/src/rust/bitbox02-rust/src/hww/api/cardano/sign_transaction.rs
+++ b/src/rust/bitbox02-rust/src/hww/api/cardano/sign_transaction.rs
@@ -331,9 +331,9 @@ pub async fn process(
mod tests {
use super::*;
use crate::hal::testing::TestingHal;
+ use crate::keystore::testing::mock_unlocked;
use crate::workflow::testing::Screen;
use alloc::boxed::Box;
- use bitbox02::testing::mock_unlocked;
use util::bb02_async::block_on;
use util::bip32::HARDENED;
diff --git a/src/rust/bitbox02-rust/src/hww/api/cardano/xpubs.rs b/src/rust/bitbox02-rust/src/hww/api/cardano/xpubs.rs
index 8bd114c..644434c 100644
--- a/src/rust/bitbox02-rust/src/hww/api/cardano/xpubs.rs
+++ b/src/rust/bitbox02-rust/src/hww/api/cardano/xpubs.rs
@@ -42,7 +42,7 @@ pub fn process(request: &pb::CardanoXpubsRequest) -> Result<Response, Error> {
mod tests {
use super::*;
- use bitbox02::testing::mock_unlocked;
+ use crate::keystore::testing::mock_unlocked;
use util::bip32::HARDENED;
#[test]
diff --git a/src/rust/bitbox02-rust/src/hww/api/electrum.rs b/src/rust/bitbox02-rust/src/hww/api/electrum.rs
index 640f158..a1e583e 100644
--- a/src/rust/bitbox02-rust/src/hww/api/electrum.rs
+++ b/src/rust/bitbox02-rust/src/hww/api/electrum.rs
@@ -52,8 +52,8 @@ pub async fn process(
mod tests {
use super::*;
+ use crate::keystore::testing::mock_unlocked;
use alloc::boxed::Box;
- use bitbox02::testing::mock_unlocked;
use util::bb02_async::block_on;
#[test]
diff --git a/src/rust/bitbox02-rust/src/hww/api/ethereum/pubrequest.rs b/src/rust/bitbox02-rust/src/hww/api/ethereum/pubrequest.rs
index cc8cc01..8b09707 100644
--- a/src/rust/bitbox02-rust/src/hww/api/ethereum/pubrequest.rs
+++ b/src/rust/bitbox02-rust/src/hww/api/ethereum/pubrequest.rs
@@ -102,9 +102,9 @@ mod tests {
use super::*;
use crate::hal::testing::TestingHal;
+ use crate::keystore::testing::mock_unlocked;
use crate::workflow::testing::Screen;
use alloc::boxed::Box;
- use bitbox02::testing::mock_unlocked;
use util::bb02_async::block_on;
use util::bip32::HARDENED;
diff --git a/src/rust/bitbox02-rust/src/hww/api/ethereum/sign.rs b/src/rust/bitbox02-rust/src/hww/api/ethereum/sign.rs
index 319ce8d..c8aa2f4 100644
--- a/src/rust/bitbox02-rust/src/hww/api/ethereum/sign.rs
+++ b/src/rust/bitbox02-rust/src/hww/api/ethereum/sign.rs
@@ -438,9 +438,9 @@ mod tests {
use super::*;
use crate::hal::testing::TestingHal;
+ use crate::keystore::testing::mock_unlocked;
use crate::workflow::testing::Screen;
use alloc::boxed::Box;
- use bitbox02::testing::mock_unlocked;
use util::bb02_async::block_on;
use util::bip32::HARDENED;
diff --git a/src/rust/bitbox02-rust/src/hww/api/ethereum/sign_typed_msg.rs b/src/rust/bitbox02-rust/src/hww/api/ethereum/sign_typed_msg.rs
index 01ec872..a2430e5 100644
--- a/src/rust/bitbox02-rust/src/hww/api/ethereum/sign_typed_msg.rs
+++ b/src/rust/bitbox02-rust/src/hww/api/ethereum/sign_typed_msg.rs
@@ -600,8 +600,8 @@ mod tests {
use super::*;
use crate::hal::testing::TestingHal;
+ use crate::keystore::testing::mock_unlocked;
use crate::workflow::testing::Screen;
- use bitbox02::testing::mock_unlocked;
use util::bb02_async::block_on;
use util::bip32::HARDENED;
diff --git a/src/rust/bitbox02-rust/src/hww/api/ethereum/signmsg.rs b/src/rust/bitbox02-rust/src/hww/api/ethereum/signmsg.rs
index 32ea16c..2a15ddf 100644
--- a/src/rust/bitbox02-rust/src/hww/api/ethereum/signmsg.rs
+++ b/src/rust/bitbox02-rust/src/hww/api/ethereum/signmsg.rs
@@ -105,9 +105,9 @@ mod tests {
use super::*;
use crate::hal::testing::TestingHal;
+ use crate::keystore::testing::mock_unlocked;
use crate::workflow::testing::Screen;
use alloc::boxed::Box;
- use bitbox02::testing::mock_unlocked;
use util::bb02_async::block_on;
use util::bip32::HARDENED;
diff --git a/src/rust/bitbox02-rust/src/hww/api/restore.rs b/src/rust/bitbox02-rust/src/hww/api/restore.rs
index 2b98e6b..f59176a 100644
--- a/src/rust/bitbox02-rust/src/hww/api/restore.rs
+++ b/src/rust/bitbox02-rust/src/hww/api/restore.rs
@@ -115,7 +115,7 @@ pub async fn from_mnemonic(
}
let mnemonic = mnemonic::get(hal).await?;
- let seed = match bitbox02::keystore::bip39_mnemonic_to_seed(&mnemonic) {
+ let seed = match crate::bip39::mnemonic_to_seed(&mnemonic) {
Ok(seed) => seed,
Err(()) => {
hal.ui().status("Recovery words\ninvalid", false).await;
diff --git a/src/rust/bitbox02-rust/src/hww/api/rootfingerprint.rs b/src/rust/bitbox02-rust/src/hww/api/rootfingerprint.rs
index 4c7f7b3..0ff1d96 100644
--- a/src/rust/bitbox02-rust/src/hww/api/rootfingerprint.rs
+++ b/src/rust/bitbox02-rust/src/hww/api/rootfingerprint.rs
@@ -33,7 +33,7 @@ pub fn process() -> Result<Response, Error> {
mod tests {
use super::*;
- use bitbox02::testing::mock_unlocked_using_mnemonic;
+ use crate::keystore::testing::mock_unlocked_using_mnemonic;
#[test]
fn test_process() {
diff --git a/src/rust/bitbox02-rust/src/keystore.rs b/src/rust/bitbox02-rust/src/keystore.rs
index 52a8ab0..ec992f0 100644
--- a/src/rust/bitbox02-rust/src/keystore.rs
+++ b/src/rust/bitbox02-rust/src/keystore.rs
@@ -425,15 +425,32 @@ pub extern "C" fn rust_keystore_get_u2f_seed(mut seed_out: util::bytes::BytesMut
}
}
+#[cfg(feature = "testing")]
+pub mod testing {
+ /// This mocks an unlocked keystore with the given bip39 recovery words and bip39 passphrase.
+ pub fn mock_unlocked_using_mnemonic(mnemonic: &str, passphrase: &str) {
+ let seed = crate::bip39::mnemonic_to_seed(mnemonic).unwrap();
+ bitbox02::keystore::mock_unlocked(&seed);
+ util::bb02_async::block_on(super::unlock_bip39(&seed, passphrase, async || {})).unwrap();
+ }
+
+ pub const TEST_MNEMONIC: &str = "purity concert above invest pigeon category peace tuition hazard vivid latin since legal speak nation session onion library travel spell region blast estate stay";
+
+ /// This mocks an unlocked keystore with a fixed bip39 seed based on these bip39 recovery words:
+ /// `purity concert above invest pigeon category peace tuition hazard vivid latin since legal speak nation session onion library travel spell region blast estate stay`
+ pub fn mock_unlocked() {
+ mock_unlocked_using_mnemonic(TEST_MNEMONIC, "")
+ }
+}
+
#[cfg(test)]
mod tests {
use super::*;
use hex_lit::hex;
- use bitbox02::testing::{
- TEST_MNEMONIC, mock_memory, mock_unlocked, mock_unlocked_using_mnemonic,
- };
+ use bitbox02::testing::mock_memory;
+ use testing::{TEST_MNEMONIC, mock_unlocked, mock_unlocked_using_mnemonic};
use util::bb02_async::block_on;
use bitcoin::secp256k1;
diff --git a/src/rust/bitbox02-rust/src/keystore/ed25519.rs b/src/rust/bitbox02-rust/src/keystore/ed25519.rs
index f5532b3..26eb86f 100644
--- a/src/rust/bitbox02-rust/src/keystore/ed25519.rs
+++ b/src/rust/bitbox02-rust/src/keystore/ed25519.rs
@@ -92,8 +92,8 @@ pub fn sign(keypath: &[u32], msg: &[u8; 32]) -> Result<SignResult, ()> {
mod tests {
use super::*;
+ use crate::keystore::testing::{mock_unlocked, mock_unlocked_using_mnemonic};
use bip32_ed25519::HARDENED_OFFSET;
- use bitbox02::testing::{mock_unlocked, mock_unlocked_using_mnemonic};
use digest::Digest;
#[test]
diff --git a/src/rust/bitbox02-rust/src/workflow/mnemonic.rs b/src/rust/bitbox02-rust/src/workflow/mnemonic.rs
index e1fc57f..09010db 100644
--- a/src/rust/bitbox02-rust/src/workflow/mnemonic.rs
+++ b/src/rust/bitbox02-rust/src/workflow/mnemonic.rs
@@ -186,7 +186,7 @@ fn lastword_choices(entered_words: &[&str]) -> Vec<u16> {
entered_words.join(" "),
crate::bip39::get_word(i).unwrap().as_str(),
));
- if let Ok(seed) = bitbox02::keystore::bip39_mnemonic_to_seed(&mnemonic) {
+ if let Ok(seed) = crate::bip39::mnemonic_to_seed(&mnemonic) {
break seed;
}
i += 1;
diff --git a/src/rust/bitbox02-rust/src/workflow/unlock.rs b/src/rust/bitbox02-rust/src/workflow/unlock.rs
index f7b6886..9d62f26 100644
--- a/src/rust/bitbox02-rust/src/workflow/unlock.rs
+++ b/src/rust/bitbox02-rust/src/workflow/unlock.rs
@@ -184,9 +184,10 @@ mod tests {
use super::*;
use crate::hal::testing::TestingHal;
+ use crate::keystore::testing::{mock_unlocked, mock_unlocked_using_mnemonic};
use crate::workflow::testing::Screen;
use alloc::boxed::Box;
- use bitbox02::testing::{mock_memory, mock_unlocked, mock_unlocked_using_mnemonic};
+ use bitbox02::testing::mock_memory;
use util::bb02_async::block_on;
#[test]
diff --git a/src/rust/bitbox02-rust/src/xpubcache.rs b/src/rust/bitbox02-rust/src/xpubcache.rs
index 93edfb0..03e3018 100644
--- a/src/rust/bitbox02-rust/src/xpubcache.rs
+++ b/src/rust/bitbox02-rust/src/xpubcache.rs
@@ -143,7 +143,7 @@ mod tests {
use super::*;
use crate::bip32;
- use bitbox02::testing::mock_unlocked;
+ use crate::keystore::testing::mock_unlocked;
use util::bip32::HARDENED;
#[test]
diff --git a/src/rust/bitbox02/src/keystore.rs b/src/rust/bitbox02/src/keystore.rs
index 0639bc7..98ada42 100644
--- a/src/rust/bitbox02/src/keystore.rs
+++ b/src/rust/bitbox02/src/keystore.rs
@@ -288,13 +288,6 @@ pub fn _secp256k1_nonce_commit(
}
}
-pub fn bip39_mnemonic_to_seed(mnemonic: &str) -> Result<zeroize::Zeroizing<Vec<u8>>, ()> {
- let mnemonic =
- bip39::Mnemonic::parse_in_normalized(bip39::Language::English, mnemonic).map_err(|_| ())?;
- let (seed, seed_len) = mnemonic.to_entropy_array();
- Ok(zeroize::Zeroizing::new(seed[..seed_len].to_vec()))
-}
-
pub fn _encrypt_and_store_seed(seed: &[u8], password: &str) -> Result<(), Error> {
match unsafe {
bitbox02_sys::keystore_encrypt_and_store_seed(
@@ -311,6 +304,13 @@ pub fn _encrypt_and_store_seed(seed: &[u8], password: &str) -> Result<(), Error>
}
}
+#[cfg(feature = "testing")]
+pub fn mock_unlocked(seed: &[u8]) {
+ unsafe {
+ bitbox02_sys::keystore_mock_unlocked(seed.as_ptr(), seed.len() as _, core::ptr::null())
+ }
+}
+
#[cfg(test)]
mod tests {
use super::*;
@@ -318,39 +318,6 @@ mod tests {
use util::bb02_async::block_on;
- #[test]
- fn test_bip39_mnemonic_to_seed() {
- assert!(bip39_mnemonic_to_seed("invalid").is_err());
-
- // Zero seed
- assert_eq!(
- bip39_mnemonic_to_seed("abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about").unwrap().as_ref() as &[u8],
- &[0u8; 16],
- );
-
- // 12 words
- assert_eq!(
- bip39_mnemonic_to_seed(
- "trust cradle viable innocent stand equal little small junior frost laundry room"
- )
- .unwrap()
- .as_ref() as &[u8],
- b"\xe9\xa6\x3f\xcd\x3a\x4d\x48\x98\x20\xa6\x63\x79\x2b\xad\xf6\xdd",
- );
-
- // 18 words
- assert_eq!(
- bip39_mnemonic_to_seed("pupil parent toe bright slam plastic spy suspect verb battle nominee loan call crystal upset razor luggage join").unwrap().as_ref() as &[u8],
- 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",
- );
-
- // 24 words
- assert_eq!(
- bip39_mnemonic_to_seed("purity concert above invest pigeon category peace tuition hazard vivid latin since legal speak nation session onion library travel spell region blast estate stay").unwrap().as_ref() as &[u8],
- 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",
- );
- }
-
#[test]
fn test_bip39_mnemonic_from_seed() {
// 12 words
diff --git a/src/rust/bitbox02/src/testing.rs b/src/rust/bitbox02/src/testing.rs
index 462c7bc..2f449db 100644
--- a/src/rust/bitbox02/src/testing.rs
+++ b/src/rust/bitbox02/src/testing.rs
@@ -14,31 +14,6 @@
//! Small mocking infrastructure for testing.
-use crate::keystore;
-
-/// This mocks an unlocked keystore with the given bip39 recovery words and bip39 passphrase.
-pub fn mock_unlocked_using_mnemonic(mnemonic: &str, passphrase: &str) {
- let seed = keystore::bip39_mnemonic_to_seed(mnemonic).unwrap();
- unsafe {
- bitbox02_sys::keystore_mock_unlocked(seed.as_ptr(), seed.len() as _, core::ptr::null())
- }
- util::bb02_async::block_on(keystore::_unlock_bip39(
- &bitcoin::secp256k1::Secp256k1::new(),
- &seed,
- passphrase,
- async || {},
- ))
- .unwrap();
-}
-
-pub const TEST_MNEMONIC: &str = "purity concert above invest pigeon category peace tuition hazard vivid latin since legal speak nation session onion library travel spell region blast estate stay";
-
-/// This mocks an unlocked keystore with a fixed bip39 seed based on these bip39 recovery words:
-/// `purity concert above invest pigeon category peace tuition hazard vivid latin since legal speak nation session onion library travel spell region blast estate stay`
-pub fn mock_unlocked() {
- mock_unlocked_using_mnemonic(TEST_MNEMONIC, "")
-}
-
unsafe extern "C" fn c_mock_random_32_bytes(buf_out: *mut u8) {
let s = unsafe { core::slice::from_raw_parts_mut(buf_out, 32) };
s.copy_from_slice(b"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.