mirror of
https://github.com/trezor/trezor-firmware.git
synced 2025-08-05 05:15:27 +00:00
WIP - couple getbool and setbool functions
This commit is contained in:
parent
a82450bf8b
commit
4e86a1c97d
@ -30,6 +30,15 @@ static void _librust_qstrs(void) {
|
|||||||
MP_QSTR_get_label;
|
MP_QSTR_get_label;
|
||||||
MP_QSTR_set_label;
|
MP_QSTR_set_label;
|
||||||
MP_QSTR_get_mnemonic_secret;
|
MP_QSTR_get_mnemonic_secret;
|
||||||
|
MP_QSTR_is_passphrase_enabled;
|
||||||
|
MP_QSTR_set_passphrase_enabled;
|
||||||
|
MP_QSTR_get_passphrase_always_on_device;
|
||||||
|
MP_QSTR_set_passphrase_always_on_device;
|
||||||
|
MP_QSTR_unfinished_backup;
|
||||||
|
MP_QSTR_set_unfinished_backup;
|
||||||
|
MP_QSTR_needs_backup;
|
||||||
|
MP_QSTR_set_backed_up;
|
||||||
|
MP_QSTR_no_backup;
|
||||||
|
|
||||||
MP_QSTR_set_timer_fn;
|
MP_QSTR_set_timer_fn;
|
||||||
MP_QSTR_touch_event;
|
MP_QSTR_touch_event;
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
error::Error,
|
|
||||||
micropython::{buffer::Buffer, map::Map, module::Module, obj::Obj, qstr::Qstr},
|
micropython::{buffer::Buffer, map::Map, module::Module, obj::Obj, qstr::Qstr},
|
||||||
trezorhal::secbool,
|
trezorhal::secbool,
|
||||||
util,
|
util,
|
||||||
@ -7,17 +6,19 @@ use crate::{
|
|||||||
|
|
||||||
use heapless::Vec;
|
use heapless::Vec;
|
||||||
|
|
||||||
use cstr_core::cstr;
|
|
||||||
|
|
||||||
use core::convert::{TryFrom, TryInto};
|
use core::convert::{TryFrom, TryInto};
|
||||||
|
|
||||||
const FLAG_PUBLIC: u8 = 0x80;
|
const FLAG_PUBLIC: u8 = 0x80;
|
||||||
|
|
||||||
const APP_DEVICE: u8 = 0x01;
|
const APP_DEVICE: u8 = 0x01;
|
||||||
|
|
||||||
// Longest possible entry in storage
|
// Longest possible entry in storage (will need to increase because of
|
||||||
|
// homescreen)
|
||||||
const MAX_LEN: usize = 300;
|
const MAX_LEN: usize = 300;
|
||||||
|
|
||||||
|
const _FALSE_BYTE: u8 = 0x00;
|
||||||
|
const _TRUE_BYTE: u8 = 0x01;
|
||||||
|
|
||||||
// TODO: transfer this into a struct with field specifying data type and
|
// TODO: transfer this into a struct with field specifying data type and
|
||||||
// `max_length`, possibly even `is_public`
|
// `max_length`, possibly even `is_public`
|
||||||
// impl get a impl set
|
// impl get a impl set
|
||||||
@ -26,7 +27,7 @@ const MAX_LEN: usize = 300;
|
|||||||
const DEVICE_ID: u8 = 0x00;
|
const DEVICE_ID: u8 = 0x00;
|
||||||
const _VERSION: u8 = 0x01;
|
const _VERSION: u8 = 0x01;
|
||||||
const _MNEMONIC_SECRET: u8 = 0x02;
|
const _MNEMONIC_SECRET: u8 = 0x02;
|
||||||
const _LANGUAGE: u8 = 0x03;
|
const _LANGUAGE: u8 = 0x03; // seems it is not used at all
|
||||||
const _LABEL: u8 = 0x04;
|
const _LABEL: u8 = 0x04;
|
||||||
const _USE_PASSPHRASE: u8 = 0x05;
|
const _USE_PASSPHRASE: u8 = 0x05;
|
||||||
const _HOMESCREEN: u8 = 0x06;
|
const _HOMESCREEN: u8 = 0x06;
|
||||||
@ -81,11 +82,11 @@ extern "C" fn storagedevice_get_version() -> Obj {
|
|||||||
unsafe { util::try_or_raise(block) }
|
unsafe { util::try_or_raise(block) }
|
||||||
}
|
}
|
||||||
|
|
||||||
extern "C" fn storagedevice_set_version(value: Obj) -> Obj {
|
extern "C" fn storagedevice_set_version(version: Obj) -> Obj {
|
||||||
let block = || {
|
let block = || {
|
||||||
let value = Buffer::try_from(value)?;
|
let version = Buffer::try_from(version)?;
|
||||||
let len = value.len() as u16;
|
let len = version.len() as u16;
|
||||||
let val = value.as_ptr();
|
let val = version.as_ptr();
|
||||||
|
|
||||||
let key = _get_appkey(_VERSION, false);
|
let key = _get_appkey(_VERSION, false);
|
||||||
let result = storagedevice_storage_set(key, val, len);
|
let result = storagedevice_storage_set(key, val, len);
|
||||||
@ -122,19 +123,19 @@ extern "C" fn storagedevice_get_rotation() -> Obj {
|
|||||||
unsafe { util::try_or_raise(block) }
|
unsafe { util::try_or_raise(block) }
|
||||||
}
|
}
|
||||||
|
|
||||||
extern "C" fn storagedevice_set_rotation(value: Obj) -> Obj {
|
extern "C" fn storagedevice_set_rotation(rotation: Obj) -> Obj {
|
||||||
let block = || {
|
let block = || {
|
||||||
let value = u16::try_from(value)?;
|
let rotation = u16::try_from(rotation)?;
|
||||||
|
|
||||||
// TODO: how to raise a micropython exception?
|
// TODO: how to raise a micropython exception?
|
||||||
if ![0, 90, 180, 270].contains(&value) {
|
if ![0, 90, 180, 270].contains(&rotation) {
|
||||||
// return Error::ValueError(cstr!("Not valid rotation"));
|
// return Error::ValueError(cstr!("Not valid rotation"));
|
||||||
}
|
}
|
||||||
|
|
||||||
let val = &value.to_be_bytes();
|
let val = &rotation.to_be_bytes();
|
||||||
|
|
||||||
let key = _get_appkey(_ROTATION, true);
|
let key = _get_appkey(_ROTATION, true);
|
||||||
let result = storagedevice_storage_set(key, val as *const _, val.len() as u16);
|
let result = storagedevice_storage_set(key, val as *const _, 2);
|
||||||
Ok(result.into())
|
Ok(result.into())
|
||||||
};
|
};
|
||||||
unsafe { util::try_or_raise(block) }
|
unsafe { util::try_or_raise(block) }
|
||||||
@ -150,11 +151,11 @@ extern "C" fn storagedevice_get_label() -> Obj {
|
|||||||
unsafe { util::try_or_raise(block) }
|
unsafe { util::try_or_raise(block) }
|
||||||
}
|
}
|
||||||
|
|
||||||
extern "C" fn storagedevice_set_label(value: Obj) -> Obj {
|
extern "C" fn storagedevice_set_label(label: Obj) -> Obj {
|
||||||
let block = || {
|
let block = || {
|
||||||
let value = Buffer::try_from(value)?;
|
let label = Buffer::try_from(label)?;
|
||||||
let len = value.len() as u16;
|
let len = label.len() as u16;
|
||||||
let val = value.as_ptr();
|
let val = label.as_ptr();
|
||||||
|
|
||||||
let key = _get_appkey(_LABEL, true);
|
let key = _get_appkey(_LABEL, true);
|
||||||
let result = storagedevice_storage_set(key, val, len);
|
let result = storagedevice_storage_set(key, val, len);
|
||||||
@ -173,6 +174,100 @@ extern "C" fn storagedevice_get_mnemonic_secret() -> Obj {
|
|||||||
unsafe { util::try_or_raise(block) }
|
unsafe { util::try_or_raise(block) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extern "C" fn storagedevice_is_passphrase_enabled() -> Obj {
|
||||||
|
let block = || {
|
||||||
|
let key = _get_appkey(_USE_PASSPHRASE, false);
|
||||||
|
let result = storagedevice_storage_get_bool(key);
|
||||||
|
Ok(result.into())
|
||||||
|
};
|
||||||
|
unsafe { util::try_or_raise(block) }
|
||||||
|
}
|
||||||
|
|
||||||
|
extern "C" fn storagedevice_set_passphrase_enabled(enable: Obj) -> Obj {
|
||||||
|
let block = || {
|
||||||
|
let enable = bool::try_from(enable)?;
|
||||||
|
|
||||||
|
let key = _get_appkey(_USE_PASSPHRASE, false);
|
||||||
|
let result = storagedevice_storage_set_bool(key, enable);
|
||||||
|
|
||||||
|
if !enable {
|
||||||
|
// TODO: could we reuse storagedevice_set_passphrase_always_on_device?
|
||||||
|
let key = _get_appkey(_PASSPHRASE_ALWAYS_ON_DEVICE, false);
|
||||||
|
storagedevice_storage_set_bool(key, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(result.into())
|
||||||
|
};
|
||||||
|
unsafe { util::try_or_raise(block) }
|
||||||
|
}
|
||||||
|
|
||||||
|
extern "C" fn storagedevice_get_passphrase_always_on_device() -> Obj {
|
||||||
|
let block = || {
|
||||||
|
let key = _get_appkey(_PASSPHRASE_ALWAYS_ON_DEVICE, false);
|
||||||
|
let result = storagedevice_storage_get_bool(key);
|
||||||
|
Ok(result.into())
|
||||||
|
};
|
||||||
|
unsafe { util::try_or_raise(block) }
|
||||||
|
}
|
||||||
|
|
||||||
|
extern "C" fn storagedevice_set_passphrase_always_on_device(enable: Obj) -> Obj {
|
||||||
|
let block = || {
|
||||||
|
let enable = bool::try_from(enable)?;
|
||||||
|
|
||||||
|
let key = _get_appkey(_PASSPHRASE_ALWAYS_ON_DEVICE, false);
|
||||||
|
let result = storagedevice_storage_set_bool(key, enable);
|
||||||
|
Ok(result.into())
|
||||||
|
};
|
||||||
|
unsafe { util::try_or_raise(block) }
|
||||||
|
}
|
||||||
|
|
||||||
|
extern "C" fn storagedevice_unfinished_backup() -> Obj {
|
||||||
|
let block = || {
|
||||||
|
let key = _get_appkey(_UNFINISHED_BACKUP, false);
|
||||||
|
let result = storagedevice_storage_get_bool(key);
|
||||||
|
Ok(result.into())
|
||||||
|
};
|
||||||
|
unsafe { util::try_or_raise(block) }
|
||||||
|
}
|
||||||
|
|
||||||
|
extern "C" fn storagedevice_set_unfinished_backup(state: Obj) -> Obj {
|
||||||
|
let block = || {
|
||||||
|
let state = bool::try_from(state)?;
|
||||||
|
|
||||||
|
let key = _get_appkey(_UNFINISHED_BACKUP, false);
|
||||||
|
let result = storagedevice_storage_set_bool(key, state);
|
||||||
|
Ok(result.into())
|
||||||
|
};
|
||||||
|
unsafe { util::try_or_raise(block) }
|
||||||
|
}
|
||||||
|
|
||||||
|
extern "C" fn storagedevice_needs_backup() -> Obj {
|
||||||
|
let block = || {
|
||||||
|
let key = _get_appkey(_NEEDS_BACKUP, false);
|
||||||
|
let result = storagedevice_storage_get_bool(key);
|
||||||
|
Ok(result.into())
|
||||||
|
};
|
||||||
|
unsafe { util::try_or_raise(block) }
|
||||||
|
}
|
||||||
|
|
||||||
|
extern "C" fn storagedevice_set_backed_up() -> Obj {
|
||||||
|
let block = || {
|
||||||
|
let key = _get_appkey(_NEEDS_BACKUP, false);
|
||||||
|
let result = storagedevice_storage_delete(key);
|
||||||
|
Ok(result.into())
|
||||||
|
};
|
||||||
|
unsafe { util::try_or_raise(block) }
|
||||||
|
}
|
||||||
|
|
||||||
|
extern "C" fn storagedevice_no_backup() -> Obj {
|
||||||
|
let block = || {
|
||||||
|
let key = _get_appkey(_NO_BACKUP, false);
|
||||||
|
let result = storagedevice_storage_get_bool(key);
|
||||||
|
Ok(result.into())
|
||||||
|
};
|
||||||
|
unsafe { util::try_or_raise(block) }
|
||||||
|
}
|
||||||
|
|
||||||
pub fn storagedevice_storage_get(key: u16) -> Vec<u8, MAX_LEN> {
|
pub fn storagedevice_storage_get(key: u16) -> Vec<u8, MAX_LEN> {
|
||||||
let mut buf: [u8; MAX_LEN] = [0; MAX_LEN];
|
let mut buf: [u8; MAX_LEN] = [0; MAX_LEN];
|
||||||
let mut len: u16 = 0;
|
let mut len: u16 = 0;
|
||||||
@ -189,6 +284,15 @@ pub fn storagedevice_storage_get(key: u16) -> Vec<u8, MAX_LEN> {
|
|||||||
vector_result
|
vector_result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn storagedevice_storage_get_bool(key: u16) -> bool {
|
||||||
|
let result = storagedevice_storage_get(key);
|
||||||
|
if result.len() == 1 && result[0] == _TRUE_BYTE {
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn storagedevice_storage_set(key: u16, val: *const u8, len: u16) -> bool {
|
pub fn storagedevice_storage_set(key: u16, val: *const u8, len: u16) -> bool {
|
||||||
match unsafe { storage_set(key, val, len) } {
|
match unsafe { storage_set(key, val, len) } {
|
||||||
secbool::TRUE => true,
|
secbool::TRUE => true,
|
||||||
@ -196,6 +300,14 @@ pub fn storagedevice_storage_set(key: u16, val: *const u8, len: u16) -> bool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn storagedevice_storage_set_bool(key: u16, val: bool) -> bool {
|
||||||
|
let val = if val { [_TRUE_BYTE] } else { [_FALSE_BYTE] };
|
||||||
|
match unsafe { storage_set(key, &val as *const _, 1) } {
|
||||||
|
secbool::TRUE => true,
|
||||||
|
_ => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn storagedevice_storage_has(key: u16) -> bool {
|
pub fn storagedevice_storage_has(key: u16) -> bool {
|
||||||
match unsafe { storage_has(key) } {
|
match unsafe { storage_has(key) } {
|
||||||
secbool::TRUE => true,
|
secbool::TRUE => true,
|
||||||
@ -225,6 +337,8 @@ fn _get_appkey(key: u8, is_public: bool) -> u16 {
|
|||||||
pub static mp_module_trezorstoragedevice: Module = obj_module! {
|
pub static mp_module_trezorstoragedevice: Module = obj_module! {
|
||||||
Qstr::MP_QSTR___name_storage__ => Qstr::MP_QSTR_trezorstoragedevice.to_obj(),
|
Qstr::MP_QSTR___name_storage__ => Qstr::MP_QSTR_trezorstoragedevice.to_obj(),
|
||||||
|
|
||||||
|
// TODO: should we return None or True in case of set_xxx?
|
||||||
|
|
||||||
/// def is_version_stored() -> bool:
|
/// def is_version_stored() -> bool:
|
||||||
/// """Whether version is in storage."""
|
/// """Whether version is in storage."""
|
||||||
Qstr::MP_QSTR_is_version_stored => obj_fn_0!(storagedevice_is_version_stored).as_obj(),
|
Qstr::MP_QSTR_is_version_stored => obj_fn_0!(storagedevice_is_version_stored).as_obj(),
|
||||||
@ -237,7 +351,7 @@ pub static mp_module_trezorstoragedevice: Module = obj_module! {
|
|||||||
/// """Get version."""
|
/// """Get version."""
|
||||||
Qstr::MP_QSTR_get_version => obj_fn_0!(storagedevice_get_version).as_obj(),
|
Qstr::MP_QSTR_get_version => obj_fn_0!(storagedevice_get_version).as_obj(),
|
||||||
|
|
||||||
/// def set_version(value: bytes) -> bool:
|
/// def set_version(version: bytes) -> bool:
|
||||||
/// """Set version."""
|
/// """Set version."""
|
||||||
Qstr::MP_QSTR_set_version => obj_fn_1!(storagedevice_set_version).as_obj(),
|
Qstr::MP_QSTR_set_version => obj_fn_1!(storagedevice_set_version).as_obj(),
|
||||||
|
|
||||||
@ -245,7 +359,7 @@ pub static mp_module_trezorstoragedevice: Module = obj_module! {
|
|||||||
/// """Get rotation."""
|
/// """Get rotation."""
|
||||||
Qstr::MP_QSTR_get_rotation => obj_fn_0!(storagedevice_get_rotation).as_obj(),
|
Qstr::MP_QSTR_get_rotation => obj_fn_0!(storagedevice_get_rotation).as_obj(),
|
||||||
|
|
||||||
/// def set_rotation(value: int) -> bool:
|
/// def set_rotation(rotation: int) -> bool:
|
||||||
/// """Set rotation."""
|
/// """Set rotation."""
|
||||||
Qstr::MP_QSTR_set_rotation => obj_fn_1!(storagedevice_set_rotation).as_obj(),
|
Qstr::MP_QSTR_set_rotation => obj_fn_1!(storagedevice_set_rotation).as_obj(),
|
||||||
|
|
||||||
@ -253,13 +367,49 @@ pub static mp_module_trezorstoragedevice: Module = obj_module! {
|
|||||||
/// """Get label."""
|
/// """Get label."""
|
||||||
Qstr::MP_QSTR_get_label => obj_fn_0!(storagedevice_get_label).as_obj(),
|
Qstr::MP_QSTR_get_label => obj_fn_0!(storagedevice_get_label).as_obj(),
|
||||||
|
|
||||||
/// def set_label(value: str) -> bool:
|
/// def set_label(label: str) -> bool:
|
||||||
/// """Set label."""
|
/// """Set label."""
|
||||||
Qstr::MP_QSTR_set_label => obj_fn_1!(storagedevice_set_label).as_obj(),
|
Qstr::MP_QSTR_set_label => obj_fn_1!(storagedevice_set_label).as_obj(),
|
||||||
|
|
||||||
/// def get_mnemonic_secret() -> bytes:
|
/// def get_mnemonic_secret() -> bytes:
|
||||||
/// """Get mnemonic secret."""
|
/// """Get mnemonic secret."""
|
||||||
Qstr::MP_QSTR_get_mnemonic_secret => obj_fn_0!(storagedevice_get_mnemonic_secret).as_obj(),
|
Qstr::MP_QSTR_get_mnemonic_secret => obj_fn_0!(storagedevice_get_mnemonic_secret).as_obj(),
|
||||||
|
|
||||||
|
/// def is_passphrase_enabled() -> bool:
|
||||||
|
/// """Whether passphrase is enabled."""
|
||||||
|
Qstr::MP_QSTR_is_passphrase_enabled => obj_fn_0!(storagedevice_is_passphrase_enabled).as_obj(),
|
||||||
|
|
||||||
|
/// def set_passphrase_enabled(enable: bool) -> bool:
|
||||||
|
/// """Set whether passphrase is enabled."""
|
||||||
|
Qstr::MP_QSTR_set_passphrase_enabled => obj_fn_1!(storagedevice_set_passphrase_enabled).as_obj(),
|
||||||
|
|
||||||
|
/// def get_passphrase_always_on_device() -> bool:
|
||||||
|
/// """Whether passphrase is on device."""
|
||||||
|
Qstr::MP_QSTR_get_passphrase_always_on_device => obj_fn_0!(storagedevice_get_passphrase_always_on_device).as_obj(),
|
||||||
|
|
||||||
|
/// def set_passphrase_always_on_device(enable: bool) -> bool:
|
||||||
|
/// """Set whether passphrase is on device."""
|
||||||
|
Qstr::MP_QSTR_set_passphrase_always_on_device => obj_fn_1!(storagedevice_set_passphrase_always_on_device).as_obj(),
|
||||||
|
|
||||||
|
/// def unfinished_backup() -> bool:
|
||||||
|
/// """Whether backup is still in progress."""
|
||||||
|
Qstr::MP_QSTR_unfinished_backup => obj_fn_0!(storagedevice_unfinished_backup).as_obj(),
|
||||||
|
|
||||||
|
/// def set_unfinished_backup(state: bool) -> bool:
|
||||||
|
/// """Set backup state."""
|
||||||
|
Qstr::MP_QSTR_set_unfinished_backup => obj_fn_1!(storagedevice_set_unfinished_backup).as_obj(),
|
||||||
|
|
||||||
|
/// def needs_backup() -> bool:
|
||||||
|
/// """Whether backup is needed."""
|
||||||
|
Qstr::MP_QSTR_needs_backup => obj_fn_0!(storagedevice_needs_backup).as_obj(),
|
||||||
|
|
||||||
|
/// def set_backed_up() -> bool:
|
||||||
|
/// """Signal that backup is finished."""
|
||||||
|
Qstr::MP_QSTR_set_backed_up => obj_fn_0!(storagedevice_set_backed_up).as_obj(),
|
||||||
|
|
||||||
|
/// def no_backup() -> bool:
|
||||||
|
/// """Whether there is no backup."""
|
||||||
|
Qstr::MP_QSTR_no_backup => obj_fn_0!(storagedevice_no_backup).as_obj(),
|
||||||
};
|
};
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -17,7 +17,7 @@ def get_version() -> bytes:
|
|||||||
|
|
||||||
|
|
||||||
# rust/src/storagedevice/storagedevice.rs
|
# rust/src/storagedevice/storagedevice.rs
|
||||||
def set_version(value: bytes) -> bool:
|
def set_version(version: bytes) -> bool:
|
||||||
"""Set version."""
|
"""Set version."""
|
||||||
|
|
||||||
|
|
||||||
@ -27,7 +27,7 @@ def get_rotation() -> int:
|
|||||||
|
|
||||||
|
|
||||||
# rust/src/storagedevice/storagedevice.rs
|
# rust/src/storagedevice/storagedevice.rs
|
||||||
def set_rotation(value: int) -> bool:
|
def set_rotation(rotation: int) -> bool:
|
||||||
"""Set rotation."""
|
"""Set rotation."""
|
||||||
|
|
||||||
|
|
||||||
@ -37,10 +37,55 @@ def get_label() -> str:
|
|||||||
|
|
||||||
|
|
||||||
# rust/src/storagedevice/storagedevice.rs
|
# rust/src/storagedevice/storagedevice.rs
|
||||||
def set_label(value: str) -> bool:
|
def set_label(label: str) -> bool:
|
||||||
"""Set label."""
|
"""Set label."""
|
||||||
|
|
||||||
|
|
||||||
# rust/src/storagedevice/storagedevice.rs
|
# rust/src/storagedevice/storagedevice.rs
|
||||||
def get_mnemonic_secret() -> bytes:
|
def get_mnemonic_secret() -> bytes:
|
||||||
"""Get mnemonic secret."""
|
"""Get mnemonic secret."""
|
||||||
|
|
||||||
|
|
||||||
|
# rust/src/storagedevice/storagedevice.rs
|
||||||
|
def is_passphrase_enabled() -> bool:
|
||||||
|
"""Whether passphrase is enabled."""
|
||||||
|
|
||||||
|
|
||||||
|
# rust/src/storagedevice/storagedevice.rs
|
||||||
|
def set_passphrase_enabled(enable: bool) -> bool:
|
||||||
|
"""Set whether passphrase is enabled."""
|
||||||
|
|
||||||
|
|
||||||
|
# rust/src/storagedevice/storagedevice.rs
|
||||||
|
def get_passphrase_always_on_device() -> bool:
|
||||||
|
"""Whether passphrase is on device."""
|
||||||
|
|
||||||
|
|
||||||
|
# rust/src/storagedevice/storagedevice.rs
|
||||||
|
def set_passphrase_always_on_device(enable: bool) -> bool:
|
||||||
|
"""Set whether passphrase is on device."""
|
||||||
|
|
||||||
|
|
||||||
|
# rust/src/storagedevice/storagedevice.rs
|
||||||
|
def unfinished_backup() -> bool:
|
||||||
|
"""Whether backup is still in progress."""
|
||||||
|
|
||||||
|
|
||||||
|
# rust/src/storagedevice/storagedevice.rs
|
||||||
|
def set_unfinished_backup(state: bool) -> bool:
|
||||||
|
"""Set backup state."""
|
||||||
|
|
||||||
|
|
||||||
|
# rust/src/storagedevice/storagedevice.rs
|
||||||
|
def needs_backup() -> bool:
|
||||||
|
"""Whether backup is needed."""
|
||||||
|
|
||||||
|
|
||||||
|
# rust/src/storagedevice/storagedevice.rs
|
||||||
|
def set_backed_up() -> bool:
|
||||||
|
"""Signal that backup is finished."""
|
||||||
|
|
||||||
|
|
||||||
|
# rust/src/storagedevice/storagedevice.rs
|
||||||
|
def no_backup() -> bool:
|
||||||
|
"""Whether there is no backup."""
|
||||||
|
@ -81,16 +81,16 @@ def get_features() -> Features:
|
|||||||
# private fields:
|
# private fields:
|
||||||
if config.is_unlocked():
|
if config.is_unlocked():
|
||||||
# passphrase_protection is private, see #1807
|
# passphrase_protection is private, see #1807
|
||||||
f.passphrase_protection = storage.device.is_passphrase_enabled()
|
f.passphrase_protection = storagedevice.is_passphrase_enabled()
|
||||||
f.needs_backup = storage.device.needs_backup()
|
f.needs_backup = storagedevice.needs_backup()
|
||||||
f.unfinished_backup = storage.device.unfinished_backup()
|
f.unfinished_backup = storagedevice.unfinished_backup()
|
||||||
f.no_backup = storage.device.no_backup()
|
f.no_backup = storagedevice.no_backup()
|
||||||
f.flags = storage.device.get_flags()
|
f.flags = storage.device.get_flags()
|
||||||
f.recovery_mode = storage.recovery.is_in_progress()
|
f.recovery_mode = storage.recovery.is_in_progress()
|
||||||
f.backup_type = mnemonic.get_type()
|
f.backup_type = mnemonic.get_type()
|
||||||
f.sd_protection = storage.sd_salt.is_enabled()
|
f.sd_protection = storage.sd_salt.is_enabled()
|
||||||
f.wipe_code_protection = config.has_wipe_code()
|
f.wipe_code_protection = config.has_wipe_code()
|
||||||
f.passphrase_always_on_device = storage.device.get_passphrase_always_on_device()
|
f.passphrase_always_on_device = storagedevice.get_passphrase_always_on_device()
|
||||||
f.safety_checks = safety_checks.read_setting()
|
f.safety_checks = safety_checks.read_setting()
|
||||||
f.auto_lock_delay_ms = storage.device.get_autolock_delay_ms()
|
f.auto_lock_delay_ms = storage.device.get_autolock_delay_ms()
|
||||||
f.display_rotation = storagedevice.get_rotation()
|
f.display_rotation = storagedevice.get_rotation()
|
||||||
|
@ -1,13 +1,12 @@
|
|||||||
from micropython import const
|
from micropython import const
|
||||||
|
|
||||||
import storage.device
|
from trezor import storagedevice, wire, workflow
|
||||||
from trezor import wire, workflow
|
|
||||||
|
|
||||||
_MAX_PASSPHRASE_LEN = const(50)
|
_MAX_PASSPHRASE_LEN = const(50)
|
||||||
|
|
||||||
|
|
||||||
def is_enabled() -> bool:
|
def is_enabled() -> bool:
|
||||||
return storage.device.is_passphrase_enabled()
|
return storagedevice.is_passphrase_enabled()
|
||||||
|
|
||||||
|
|
||||||
async def get(ctx: wire.Context) -> str:
|
async def get(ctx: wire.Context) -> str:
|
||||||
@ -19,7 +18,7 @@ async def get(ctx: wire.Context) -> str:
|
|||||||
|
|
||||||
async def _request_from_user(ctx: wire.Context) -> str:
|
async def _request_from_user(ctx: wire.Context) -> str:
|
||||||
workflow.close_others() # request exclusive UI access
|
workflow.close_others() # request exclusive UI access
|
||||||
if storage.device.get_passphrase_always_on_device():
|
if storagedevice.get_passphrase_always_on_device():
|
||||||
from trezor.ui.layouts import request_passphrase_on_device
|
from trezor.ui.layouts import request_passphrase_on_device
|
||||||
|
|
||||||
passphrase = await request_passphrase_on_device(ctx, _MAX_PASSPHRASE_LEN)
|
passphrase = await request_passphrase_on_device(ctx, _MAX_PASSPHRASE_LEN)
|
||||||
|
@ -42,7 +42,7 @@ async def load_device(ctx: wire.Context, msg: LoadDevice) -> Success:
|
|||||||
needs_backup=msg.needs_backup is True,
|
needs_backup=msg.needs_backup is True,
|
||||||
no_backup=msg.no_backup is True,
|
no_backup=msg.no_backup is True,
|
||||||
)
|
)
|
||||||
storage.device.set_passphrase_enabled(bool(msg.passphrase_protection))
|
storagedevice.set_passphrase_enabled(bool(msg.passphrase_protection))
|
||||||
storage.device.set_label(msg.label or "")
|
storage.device.set_label(msg.label or "")
|
||||||
if msg.pin:
|
if msg.pin:
|
||||||
config.change_pin("", msg.pin, None, None)
|
config.change_pin("", msg.pin, None, None)
|
||||||
|
@ -38,11 +38,11 @@ class Homescreen(HomescreenBase):
|
|||||||
|
|
||||||
def do_render(self) -> None:
|
def do_render(self) -> None:
|
||||||
# warning bar on top
|
# warning bar on top
|
||||||
if storagedevice.is_initialized() and storage.device.no_backup():
|
if storagedevice.is_initialized() and storagedevice.no_backup():
|
||||||
ui.header_error("SEEDLESS")
|
ui.header_error("SEEDLESS")
|
||||||
elif storagedevice.is_initialized() and storage.device.unfinished_backup():
|
elif storagedevice.is_initialized() and storagedevice.unfinished_backup():
|
||||||
ui.header_error("BACKUP FAILED!")
|
ui.header_error("BACKUP FAILED!")
|
||||||
elif storagedevice.is_initialized() and storage.device.needs_backup():
|
elif storagedevice.is_initialized() and storagedevice.needs_backup():
|
||||||
ui.header_warning("NEEDS BACKUP!")
|
ui.header_warning("NEEDS BACKUP!")
|
||||||
elif storagedevice.is_initialized() and not config.has_pin():
|
elif storagedevice.is_initialized() and not config.has_pin():
|
||||||
ui.header_warning("PIN NOT SET!")
|
ui.header_warning("PIN NOT SET!")
|
||||||
|
@ -64,15 +64,15 @@ async def apply_settings(ctx: wire.Context, msg: ApplySettings) -> Success:
|
|||||||
|
|
||||||
if msg.use_passphrase is not None:
|
if msg.use_passphrase is not None:
|
||||||
await require_confirm_change_passphrase(ctx, msg.use_passphrase)
|
await require_confirm_change_passphrase(ctx, msg.use_passphrase)
|
||||||
storage.device.set_passphrase_enabled(msg.use_passphrase)
|
storagedevice.set_passphrase_enabled(msg.use_passphrase)
|
||||||
|
|
||||||
if msg.passphrase_always_on_device is not None:
|
if msg.passphrase_always_on_device is not None:
|
||||||
if not storage.device.is_passphrase_enabled():
|
if not storagedevice.is_passphrase_enabled():
|
||||||
raise wire.DataError("Passphrase is not enabled")
|
raise wire.DataError("Passphrase is not enabled")
|
||||||
await require_confirm_change_passphrase_source(
|
await require_confirm_change_passphrase_source(
|
||||||
ctx, msg.passphrase_always_on_device
|
ctx, msg.passphrase_always_on_device
|
||||||
)
|
)
|
||||||
storage.device.set_passphrase_always_on_device(msg.passphrase_always_on_device)
|
storagedevice.set_passphrase_always_on_device(msg.passphrase_always_on_device)
|
||||||
|
|
||||||
if msg.auto_lock_delay_ms is not None:
|
if msg.auto_lock_delay_ms is not None:
|
||||||
if msg.auto_lock_delay_ms < storage.device.AUTOLOCK_DELAY_MINIMUM:
|
if msg.auto_lock_delay_ms < storage.device.AUTOLOCK_DELAY_MINIMUM:
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
from typing import TYPE_CHECKING
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
import storage
|
|
||||||
import storage.device
|
|
||||||
from trezor import storagedevice, wire
|
from trezor import storagedevice, wire
|
||||||
from trezor.messages import Success
|
from trezor.messages import Success
|
||||||
|
|
||||||
@ -16,19 +14,19 @@ if TYPE_CHECKING:
|
|||||||
async def backup_device(ctx: wire.Context, msg: BackupDevice) -> Success:
|
async def backup_device(ctx: wire.Context, msg: BackupDevice) -> Success:
|
||||||
if not storagedevice.is_initialized():
|
if not storagedevice.is_initialized():
|
||||||
raise wire.NotInitialized("Device is not initialized")
|
raise wire.NotInitialized("Device is not initialized")
|
||||||
if not storage.device.needs_backup():
|
if not storagedevice.needs_backup():
|
||||||
raise wire.ProcessError("Seed already backed up")
|
raise wire.ProcessError("Seed already backed up")
|
||||||
|
|
||||||
mnemonic_secret, mnemonic_type = mnemonic.get()
|
mnemonic_secret, mnemonic_type = mnemonic.get()
|
||||||
if mnemonic_secret is None:
|
if mnemonic_secret is None:
|
||||||
raise RuntimeError
|
raise RuntimeError
|
||||||
|
|
||||||
storage.device.set_unfinished_backup(True)
|
storagedevice.set_unfinished_backup(True)
|
||||||
storage.device.set_backed_up()
|
storagedevice.set_backed_up()
|
||||||
|
|
||||||
await backup_seed(ctx, mnemonic_type, mnemonic_secret)
|
await backup_seed(ctx, mnemonic_type, mnemonic_secret)
|
||||||
|
|
||||||
storage.device.set_unfinished_backup(False)
|
storagedevice.set_unfinished_backup(False)
|
||||||
|
|
||||||
await layout.show_backup_success(ctx)
|
await layout.show_backup_success(ctx)
|
||||||
|
|
||||||
|
@ -56,7 +56,7 @@ async def recovery_device(ctx: wire.Context, msg: RecoveryDevice) -> Success:
|
|||||||
newpin = await request_pin_confirm(ctx, allow_cancel=False)
|
newpin = await request_pin_confirm(ctx, allow_cancel=False)
|
||||||
config.change_pin("", newpin, None, None)
|
config.change_pin("", newpin, None, None)
|
||||||
|
|
||||||
storage.device.set_passphrase_enabled(bool(msg.passphrase_protection))
|
storagedevice.set_passphrase_enabled(bool(msg.passphrase_protection))
|
||||||
if msg.u2f_counter is not None:
|
if msg.u2f_counter is not None:
|
||||||
storage.device.set_u2f_counter(msg.u2f_counter)
|
storage.device.set_u2f_counter(msg.u2f_counter)
|
||||||
if msg.label is not None:
|
if msg.label is not None:
|
||||||
|
@ -85,7 +85,7 @@ async def reset_device(ctx: wire.Context, msg: ResetDevice) -> Success:
|
|||||||
# write settings and master secret into storage
|
# write settings and master secret into storage
|
||||||
if msg.label is not None:
|
if msg.label is not None:
|
||||||
storage.device.set_label(msg.label)
|
storage.device.set_label(msg.label)
|
||||||
storage.device.set_passphrase_enabled(bool(msg.passphrase_protection))
|
storagedevice.set_passphrase_enabled(bool(msg.passphrase_protection))
|
||||||
storage.device.store_mnemonic_secret(
|
storage.device.store_mnemonic_secret(
|
||||||
secret, # for SLIP-39, this is the EMS
|
secret, # for SLIP-39, this is the EMS
|
||||||
msg.backup_type,
|
msg.backup_type,
|
||||||
|
@ -138,14 +138,14 @@ def get_backup_type() -> BackupType:
|
|||||||
return backup_type # type: ignore [int-into-enum]
|
return backup_type # type: ignore [int-into-enum]
|
||||||
|
|
||||||
|
|
||||||
def is_passphrase_enabled() -> bool:
|
# def is_passphrase_enabled() -> bool:
|
||||||
return common.get_bool(_NAMESPACE, _USE_PASSPHRASE)
|
# return common.get_bool(_NAMESPACE, _USE_PASSPHRASE)
|
||||||
|
|
||||||
|
|
||||||
def set_passphrase_enabled(enable: bool) -> None:
|
# def set_passphrase_enabled(enable: bool) -> None:
|
||||||
common.set_bool(_NAMESPACE, _USE_PASSPHRASE, enable)
|
# common.set_bool(_NAMESPACE, _USE_PASSPHRASE, enable)
|
||||||
if not enable:
|
# if not enable:
|
||||||
set_passphrase_always_on_device(False)
|
# set_passphrase_always_on_device(False)
|
||||||
|
|
||||||
|
|
||||||
def get_homescreen() -> bytes | None:
|
def get_homescreen() -> bytes | None:
|
||||||
@ -173,38 +173,38 @@ def store_mnemonic_secret(
|
|||||||
common.set_true_or_delete(_NAMESPACE, _NEEDS_BACKUP, needs_backup)
|
common.set_true_or_delete(_NAMESPACE, _NEEDS_BACKUP, needs_backup)
|
||||||
|
|
||||||
|
|
||||||
def needs_backup() -> bool:
|
# def needs_backup() -> bool:
|
||||||
return common.get_bool(_NAMESPACE, _NEEDS_BACKUP)
|
# return common.get_bool(_NAMESPACE, _NEEDS_BACKUP)
|
||||||
|
|
||||||
|
|
||||||
def set_backed_up() -> None:
|
# def set_backed_up() -> None:
|
||||||
common.delete(_NAMESPACE, _NEEDS_BACKUP)
|
# common.delete(_NAMESPACE, _NEEDS_BACKUP)
|
||||||
|
|
||||||
|
|
||||||
def unfinished_backup() -> bool:
|
# def unfinished_backup() -> bool:
|
||||||
return common.get_bool(_NAMESPACE, _UNFINISHED_BACKUP)
|
# return common.get_bool(_NAMESPACE, _UNFINISHED_BACKUP)
|
||||||
|
|
||||||
|
|
||||||
def set_unfinished_backup(state: bool) -> None:
|
# def set_unfinished_backup(state: bool) -> None:
|
||||||
common.set_bool(_NAMESPACE, _UNFINISHED_BACKUP, state)
|
# common.set_bool(_NAMESPACE, _UNFINISHED_BACKUP, state)
|
||||||
|
|
||||||
|
|
||||||
def no_backup() -> bool:
|
# def no_backup() -> bool:
|
||||||
return common.get_bool(_NAMESPACE, _NO_BACKUP)
|
# return common.get_bool(_NAMESPACE, _NO_BACKUP)
|
||||||
|
|
||||||
|
|
||||||
def get_passphrase_always_on_device() -> bool:
|
# def get_passphrase_always_on_device() -> bool:
|
||||||
"""
|
# """
|
||||||
This is backwards compatible with _PASSPHRASE_SOURCE:
|
# This is backwards compatible with _PASSPHRASE_SOURCE:
|
||||||
- If ASK(0) => returns False, the check against b"\x01" in get_bool fails.
|
# - If ASK(0) => returns False, the check against b"\x01" in get_bool fails.
|
||||||
- If DEVICE(1) => returns True, the check against b"\x01" in get_bool succeeds.
|
# - If DEVICE(1) => returns True, the check against b"\x01" in get_bool succeeds.
|
||||||
- If HOST(2) => returns False, the check against b"\x01" in get_bool fails.
|
# - If HOST(2) => returns False, the check against b"\x01" in get_bool fails.
|
||||||
"""
|
# """
|
||||||
return common.get_bool(_NAMESPACE, _PASSPHRASE_ALWAYS_ON_DEVICE)
|
# return common.get_bool(_NAMESPACE, _PASSPHRASE_ALWAYS_ON_DEVICE)
|
||||||
|
|
||||||
|
|
||||||
def set_passphrase_always_on_device(enable: bool) -> None:
|
# def set_passphrase_always_on_device(enable: bool) -> None:
|
||||||
common.set_bool(_NAMESPACE, _PASSPHRASE_ALWAYS_ON_DEVICE, enable)
|
# common.set_bool(_NAMESPACE, _PASSPHRASE_ALWAYS_ON_DEVICE, enable)
|
||||||
|
|
||||||
|
|
||||||
def get_flags() -> int:
|
def get_flags() -> int:
|
||||||
|
Loading…
Reference in New Issue
Block a user