Skip to content

Commit 1d0096a

Browse files
committed
rust/keystore: move mock_unlocked() etc to bitbox02_rust::keystore
1 parent 9e2343c commit 1d0096a

28 files changed

+101
-94
lines changed

src/rust/bitbox02-rust/src/bip39.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// limitations under the License.
1414

1515
use alloc::string::{String, ToString};
16+
use alloc::vec::Vec;
1617

1718
/// `idx` must be smaller than BIP39_WORDLIST_LEN.
1819
pub fn get_word(idx: u16) -> Result<zeroize::Zeroizing<String>, ()> {
@@ -25,6 +26,14 @@ pub fn get_word(idx: u16) -> Result<zeroize::Zeroizing<String>, ()> {
2526
))
2627
}
2728

29+
/// Decode a BIP39 mnemonic.
30+
pub fn mnemonic_to_seed(mnemonic: &str) -> Result<zeroize::Zeroizing<Vec<u8>>, ()> {
31+
let mnemonic =
32+
bip39::Mnemonic::parse_in_normalized(bip39::Language::English, mnemonic).map_err(|_| ())?;
33+
let (seed, seed_len) = mnemonic.to_entropy_array();
34+
Ok(zeroize::Zeroizing::new(seed[..seed_len].to_vec()))
35+
}
36+
2837
// C API
2938

3039
#[unsafe(no_mangle)]
@@ -93,4 +102,37 @@ mod tests {
93102
assert_eq!(get_word(2047).unwrap().as_ref() as &str, "zoo");
94103
assert_eq!(get_word(563).unwrap().as_ref() as &str, "edit");
95104
}
105+
106+
#[test]
107+
fn test_mnemonic_to_seed() {
108+
assert!(mnemonic_to_seed("invalid").is_err());
109+
110+
// Zero seed
111+
assert_eq!(
112+
mnemonic_to_seed("abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about").unwrap().as_ref() as &[u8],
113+
&[0u8; 16],
114+
);
115+
116+
// 12 words
117+
assert_eq!(
118+
mnemonic_to_seed(
119+
"trust cradle viable innocent stand equal little small junior frost laundry room"
120+
)
121+
.unwrap()
122+
.as_ref() as &[u8],
123+
b"\xe9\xa6\x3f\xcd\x3a\x4d\x48\x98\x20\xa6\x63\x79\x2b\xad\xf6\xdd",
124+
);
125+
126+
// 18 words
127+
assert_eq!(
128+
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],
129+
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",
130+
);
131+
132+
// 24 words
133+
assert_eq!(
134+
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],
135+
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",
136+
);
137+
}
96138
}

src/rust/bitbox02-rust/src/hww/api/backup.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,10 @@ mod tests {
170170
use super::*;
171171

172172
use crate::hal::testing::TestingHal;
173+
use crate::keystore::testing::{mock_unlocked, mock_unlocked_using_mnemonic};
173174
use crate::workflow::testing::Screen;
174175
use alloc::boxed::Box;
175-
use bitbox02::testing::{mock_memory, mock_unlocked, mock_unlocked_using_mnemonic};
176+
use bitbox02::testing::mock_memory;
176177
use util::bb02_async::block_on;
177178

178179
/// Test backup creation on a uninitialized keystore.

src/rust/bitbox02-rust/src/hww/api/bitcoin.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -333,12 +333,11 @@ mod tests {
333333

334334
use crate::bip32::parse_xpub;
335335
use crate::hal::testing::TestingHal;
336+
use crate::keystore::testing::{TEST_MNEMONIC, mock_unlocked, mock_unlocked_using_mnemonic};
336337
use crate::workflow::testing::Screen;
337338
use alloc::boxed::Box;
338339
use alloc::vec::Vec;
339-
use bitbox02::testing::{
340-
TEST_MNEMONIC, mock_memory, mock_unlocked, mock_unlocked_using_mnemonic,
341-
};
340+
use bitbox02::testing::mock_memory;
342341
use pb::btc_script_config::multisig::ScriptType as MultisigScriptType;
343342
use util::bb02_async::block_on;
344343
use util::bip32::HARDENED;

src/rust/bitbox02-rust/src/hww/api/bitcoin/common.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ fn encode_segwit_addr(
318318
mod tests {
319319
use super::*;
320320

321-
use bitbox02::testing::mock_unlocked_using_mnemonic;
321+
use crate::keystore::testing::mock_unlocked_using_mnemonic;
322322
use util::bip32::HARDENED;
323323

324324
#[test]

src/rust/bitbox02-rust/src/hww/api/bitcoin/multisig.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,9 @@ pub fn pkscript(
328328
mod tests {
329329
use super::*;
330330

331+
use crate::keystore::testing::mock_unlocked_using_mnemonic;
331332
use bip32::parse_xpub;
332-
use bitbox02::testing::{mock_memory, mock_unlocked_using_mnemonic};
333+
use bitbox02::testing::mock_memory;
333334
use util::bip32::HARDENED;
334335

335336
#[test]

src/rust/bitbox02-rust/src/hww/api/bitcoin/policies.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,7 @@ mod tests {
769769
use super::*;
770770

771771
use crate::bip32::parse_xpub;
772-
use bitbox02::testing::{mock_unlocked, mock_unlocked_using_mnemonic};
772+
use crate::keystore::testing::{mock_unlocked, mock_unlocked_using_mnemonic};
773773

774774
const SOME_XPUB_1: &str = "tpubDFj9SBQssRHA5EB1ox58mcgF9sB61br9RGz6UrBukcNKmFe4fPgskZ4wigxQ1jSUzLdjnvvDHL8Z6L3ey5Ev5FNNqrDrePxwXsNHiLZhBTc";
775775
const SOME_XPUB_2: &str = "tpubDCmDXtvJLH9yHLNLnGVRoXBvvacvWskjV4hq4WAmGXcRbfa5uaiybZ7kjGRAFbLaoiw1LcwV56H88avibGh7GC7nqqz2Jcs1dWu33cRKYm4";

src/rust/bitbox02-rust/src/hww/api/bitcoin/registration.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,8 @@ mod tests {
190190
use super::*;
191191

192192
use crate::bip32::parse_xpub;
193-
use bitbox02::testing::{mock_memory, mock_unlocked_using_mnemonic};
193+
use crate::keystore::testing::mock_unlocked_using_mnemonic;
194+
use bitbox02::testing::mock_memory;
194195
use util::bip32::HARDENED;
195196

196197
use pb::btc_script_config::{Multisig, multisig::ScriptType};

src/rust/bitbox02-rust/src/hww/api/bitcoin/script_configs.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ impl ValidatedScriptConfigWithKeypath<'_> {
7575
mod tests {
7676
use super::*;
7777
use crate::bip32::parse_xpub;
78-
use bitbox02::testing::{mock_memory, mock_unlocked};
78+
use crate::keystore::testing::mock_unlocked;
79+
use bitbox02::testing::mock_memory;
7980

8081
#[test]
8182
fn test_self_transfer_representation_simple_type() {

src/rust/bitbox02-rust/src/hww/api/bitcoin/signmsg.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,9 @@ mod tests {
138138
use super::*;
139139

140140
use crate::hal::testing::TestingHal;
141+
use crate::keystore::testing::mock_unlocked;
141142
use crate::workflow::testing::Screen;
142143
use alloc::boxed::Box;
143-
use bitbox02::testing::mock_unlocked;
144144
use util::bb02_async::block_on;
145145
use util::bip32::HARDENED;
146146

src/rust/bitbox02-rust/src/hww/api/bitcoin/signtx.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1294,9 +1294,10 @@ mod tests {
12941294
use super::*;
12951295
use crate::bip32::parse_xpub;
12961296
use crate::hal::testing::TestingHal;
1297+
use crate::keystore::testing::{mock_unlocked, mock_unlocked_using_mnemonic};
12971298
use crate::workflow::testing::Screen;
12981299
use alloc::boxed::Box;
1299-
use bitbox02::testing::{mock_memory, mock_unlocked, mock_unlocked_using_mnemonic};
1300+
use bitbox02::testing::mock_memory;
13001301
use hex_lit::hex;
13011302
use pb::btc_payment_request_request::{Memo, memo};
13021303
use util::bb02_async::block_on;

0 commit comments

Comments
 (0)