Skip to content

Commit fcf2d8b

Browse files
committed
Merge branch 'mock_unlocked'
2 parents 3302de3 + c4c1180 commit fcf2d8b

29 files changed

+137
-131
lines changed

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

Lines changed: 75 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,20 @@ pub fn get_word(idx: u16) -> Result<zeroize::Zeroizing<String>, ()> {
2526
))
2627
}
2728

29+
/// Encode a seed as a BIP39 mnemonic.
30+
pub fn mnemonic_from_seed(seed: &[u8]) -> Result<zeroize::Zeroizing<String>, ()> {
31+
let mnemonic = bip39::Mnemonic::from_entropy(seed).map_err(|_| ())?;
32+
Ok(zeroize::Zeroizing::new(mnemonic.to_string()))
33+
}
34+
35+
/// Decode a BIP39 mnemonic.
36+
pub fn mnemonic_to_seed(mnemonic: &str) -> Result<zeroize::Zeroizing<Vec<u8>>, ()> {
37+
let mnemonic =
38+
bip39::Mnemonic::parse_in_normalized(bip39::Language::English, mnemonic).map_err(|_| ())?;
39+
let (seed, seed_len) = mnemonic.to_entropy_array();
40+
Ok(zeroize::Zeroizing::new(seed[..seed_len].to_vec()))
41+
}
42+
2843
// C API
2944

3045
#[unsafe(no_mangle)]
@@ -93,4 +108,64 @@ mod tests {
93108
assert_eq!(get_word(2047).unwrap().as_ref() as &str, "zoo");
94109
assert_eq!(get_word(563).unwrap().as_ref() as &str, "edit");
95110
}
111+
112+
#[test]
113+
fn test_mnemonic_from_seed() {
114+
// 12 words
115+
let seed = b"\xae\x6a\x40\x26\x1f\x0a\xcc\x16\x57\x04\x9c\xb2\x1a\xf5\xfb\xf7";
116+
assert_eq!(
117+
mnemonic_from_seed(seed).unwrap().as_str(),
118+
"purpose faith another dignity proud arctic foster near rare stumble leave urge",
119+
);
120+
121+
// 18 words
122+
let seed = b"\x2a\x3e\x07\xa9\xe7\x5e\xd7\x3a\xa6\xb2\xe1\xaf\x90\x3d\x50\x17\xde\x80\x4f\xdf\x2b\x45\xc2\x4b";
123+
assert_eq!(
124+
mnemonic_from_seed(seed).unwrap().as_str(),
125+
"clay usual tuna solid uniform outer onion found question limit favorite cook trend child lake hamster seat foot",
126+
);
127+
128+
// 24 words
129+
let seed = b"\x24\x1d\x5b\x78\x35\x90\xc2\x1f\x79\x69\x8e\x7c\xe8\x92\xdd\x03\xfb\x2c\x8f\xad\xc2\x44\x0e\xc2\x3a\xa5\xde\x9e\x2d\x23\x81\xb0";
130+
assert_eq!(
131+
mnemonic_from_seed(seed).unwrap().as_str(),
132+
"catch turn task hen around autumn toss crack language duty resemble among ready elephant require embrace attract balcony practice rule tissue mushroom almost athlete",
133+
);
134+
135+
// Invalid seed side
136+
assert!(mnemonic_from_seed(b"foo").is_err());
137+
}
138+
139+
#[test]
140+
fn test_mnemonic_to_seed() {
141+
assert!(mnemonic_to_seed("invalid").is_err());
142+
143+
// Zero seed
144+
assert_eq!(
145+
mnemonic_to_seed("abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about").unwrap().as_ref() as &[u8],
146+
&[0u8; 16],
147+
);
148+
149+
// 12 words
150+
assert_eq!(
151+
mnemonic_to_seed(
152+
"trust cradle viable innocent stand equal little small junior frost laundry room"
153+
)
154+
.unwrap()
155+
.as_ref() as &[u8],
156+
b"\xe9\xa6\x3f\xcd\x3a\x4d\x48\x98\x20\xa6\x63\x79\x2b\xad\xf6\xdd",
157+
);
158+
159+
// 18 words
160+
assert_eq!(
161+
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],
162+
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",
163+
);
164+
165+
// 24 words
166+
assert_eq!(
167+
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],
168+
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",
169+
);
170+
}
96171
}

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)