Skip to content

Commit 436195d

Browse files
committed
Merge branch 'call'
2 parents af6b171 + 4ea9611 commit 436195d

File tree

2 files changed

+49
-36
lines changed

2 files changed

+49
-36
lines changed

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,12 +119,6 @@ pub async fn process_packet(usb_in: Vec<u8>) -> Vec<u8> {
119119
_ => (),
120120
}
121121

122-
// No other message than the attestation and unlock calls shall pass until the device is
123-
// unlocked or ready to be initialized.
124-
if bitbox02::memory::is_initialized() && bitbox02::keystore::is_locked() {
125-
return Vec::new();
126-
}
127-
128122
let mut out = [OP_STATUS_SUCCESS].to_vec();
129123
match noise::process(usb_in, &mut out).await {
130124
Ok(()) => out,
@@ -417,7 +411,14 @@ mod tests {
417411

418412
// Can't reboot when initialized but locked.
419413
bitbox02::keystore::lock();
420-
assert!(make_request(reboot_request.encode_to_vec().as_ref()).is_err());
414+
let response_encoded = make_request(&reboot_request.encode_to_vec()).unwrap();
415+
let response = crate::pb::Response::decode(&response_encoded[..]).unwrap();
416+
assert_eq!(
417+
response,
418+
crate::pb::Response {
419+
response: Some(api::error::make_error(api::error::Error::InvalidState))
420+
},
421+
);
421422

422423
// Unlock.
423424
assert_eq!(

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

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -85,20 +85,27 @@ async fn process_api_btc(_request: &Request) -> Result<Response, Error> {
8585

8686
/// Checks if the device is ready to accept/handle an api endpoint.
8787
fn can_call(request: &Request) -> bool {
88-
// We have three main states:
89-
// Creating a wallet on an uninitialized device goes through those states in order.
90-
// Restoring a backup skips the seeded state and goes straight to `initialized`.
88+
// We have four main states:
89+
// Creating a wallet on an uninitialized device goes from Uninitialized to Seeded, and when the
90+
// backup is created to `Initialized*`.
91+
// Restoring a backup skips the seeded state and goes straight to `Initialized*`.
9192
// Each state has a set of valid api calls associated.
9293
enum State {
9394
// Uninitialized (reset).
9495
Uninitialized,
9596
// Seeded (password defined, seed created/loaded).
9697
Seeded,
97-
// Initialized (seed backuped up on SD card).
98-
Initialized,
98+
// InitializedAndLocked (seed backuped up on SD card, keystore locked).
99+
InitializedAndLocked,
100+
// InitializedAndUnlocked (seed backuped up on SD card, keystore unlocked).
101+
InitializedAndUnlocked,
99102
}
100103
let state: State = if bitbox02::memory::is_initialized() {
101-
State::Initialized
104+
if bitbox02::keystore::is_locked() {
105+
State::InitializedAndLocked
106+
} else {
107+
State::InitializedAndUnlocked
108+
}
102109
} else if bitbox02::memory::is_seeded() {
103110
State::Seeded
104111
} else {
@@ -108,33 +115,38 @@ fn can_call(request: &Request) -> bool {
108115
match request {
109116
// Deprecated call, last used in v1.0.0.
110117
Request::PerformAttestation(_) => false,
111-
Request::DeviceInfo(_) => true,
112-
Request::Reboot(_) => true,
113-
Request::DeviceName(_) => true,
114-
Request::DeviceLanguage(_) => true,
115-
Request::CheckSdcard(_) => true,
116-
Request::InsertRemoveSdcard(_) => true,
117-
Request::ListBackups(_) => true,
118-
Request::SetPassword(_) => matches!(state, State::Uninitialized | State::Seeded),
119-
Request::RestoreBackup(_) => matches!(state, State::Uninitialized | State::Seeded),
120-
Request::RestoreFromMnemonic(_) => matches!(state, State::Uninitialized | State::Seeded),
121-
Request::CreateBackup(_) => matches!(state, State::Seeded | State::Initialized),
122-
Request::ShowMnemonic(_) => matches!(state, State::Seeded | State::Initialized),
123-
Request::Fingerprint(_) => matches!(state, State::Initialized),
124-
Request::ElectrumEncryptionKey(_) => matches!(state, State::Initialized),
125-
Request::BtcPub(_) | Request::Btc(_) | Request::BtcSignInit(_) => {
126-
matches!(state, State::Initialized)
118+
Request::DeviceInfo(_)
119+
| Request::Reboot(_)
120+
| Request::DeviceName(_)
121+
| Request::DeviceLanguage(_)
122+
| Request::CheckSdcard(_)
123+
| Request::InsertRemoveSdcard(_)
124+
| Request::ListBackups(_) => matches!(
125+
state,
126+
State::Uninitialized | State::Seeded | State::InitializedAndUnlocked
127+
),
128+
Request::SetPassword(_) | Request::RestoreBackup(_) | Request::RestoreFromMnemonic(_) => {
129+
matches!(state, State::Uninitialized | State::Seeded)
130+
}
131+
Request::CreateBackup(_) | Request::ShowMnemonic(_) => {
132+
matches!(state, State::Seeded | State::InitializedAndUnlocked)
133+
}
134+
Request::Fingerprint(_)
135+
| Request::ElectrumEncryptionKey(_)
136+
| Request::BtcPub(_)
137+
| Request::Btc(_)
138+
| Request::BtcSignInit(_)
139+
| Request::CheckBackup(_)
140+
| Request::SetMnemonicPassphraseEnabled(_)
141+
| Request::Eth(_)
142+
| Request::Reset(_)
143+
| Request::Cardano(_)
144+
| Request::Bip85(_) => {
145+
matches!(state, State::InitializedAndUnlocked)
127146
}
128147
// These are streamed asynchronously using the `next_request()` primitive in
129148
// bitcoin/signtx.rs and are not handled directly.
130149
Request::BtcSignInput(_) | Request::BtcSignOutput(_) => false,
131-
132-
Request::CheckBackup(_) => matches!(state, State::Initialized),
133-
Request::SetMnemonicPassphraseEnabled(_) => matches!(state, State::Initialized),
134-
Request::Eth(_) => matches!(state, State::Initialized),
135-
Request::Reset(_) => matches!(state, State::Initialized),
136-
Request::Cardano(_) => matches!(state, State::Initialized),
137-
Request::Bip85(_) => matches!(state, State::Initialized),
138150
}
139151
}
140152

0 commit comments

Comments
 (0)