-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use of heap feature for simpler code
- Loading branch information
1 parent
57d01bd
commit 9ec3374
Showing
8 changed files
with
63 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,42 @@ | ||
use alloc::vec::Vec; | ||
|
||
use crate::AppSW; | ||
use core::str::from_utf8; | ||
|
||
/// BIP32 path stored as an array of [`u32`]. | ||
/// | ||
/// # Generic arguments | ||
/// | ||
/// * `S` - Maximum possible path length, i.e. the capacity of the internal buffer. | ||
pub struct Bip32Path<const S: usize = 10> { | ||
buffer: [u32; S], | ||
len: usize, | ||
} | ||
#[derive(Default)] | ||
pub struct Bip32Path(Vec<u32>); | ||
|
||
impl AsRef<[u32]> for Bip32Path { | ||
fn as_ref(&self) -> &[u32] { | ||
&self.buffer[..self.len] | ||
} | ||
} | ||
|
||
impl<const S: usize> Default for Bip32Path<S> { | ||
fn default() -> Self { | ||
Self { | ||
buffer: [0u32; S], | ||
len: 0, | ||
} | ||
&self.0 | ||
} | ||
} | ||
|
||
impl<const S: usize> TryFrom<&[u8]> for Bip32Path<S> { | ||
impl TryFrom<&[u8]> for Bip32Path { | ||
type Error = AppSW; | ||
|
||
/// Constructs a [`Bip32Path`] from a given byte array. | ||
/// | ||
/// This method will return an error in the following cases: | ||
/// - the input array is empty, | ||
/// - the number of bytes in the input array is not a multiple of 4, | ||
/// - the input array exceeds the capacity of the [`Bip32Path`] internal buffer. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `data` - Encoded BIP32 path. First byte is the length of the path, as encoded by ragger. | ||
fn try_from(data: &[u8]) -> Result<Self, Self::Error> { | ||
let input_path_len = (data.len() - 1) / 4; | ||
// Check data length | ||
if data.is_empty() // At least the length byte is required | ||
|| (input_path_len > S) | ||
|| (data[0] as usize * 4 != data.len() - 1) | ||
{ | ||
return Err(AppSW::WrongApduLength); | ||
} | ||
|
||
let mut path = [0; S]; | ||
for (chunk, p) in data[1..].chunks(4).zip(path.iter_mut()) { | ||
*p = u32::from_be_bytes(chunk.try_into().unwrap()); | ||
} | ||
|
||
Ok(Self { | ||
buffer: path, | ||
len: input_path_len, | ||
}) | ||
Ok(Bip32Path( | ||
data[1..] | ||
.chunks(4) | ||
.map(|chunk| u32::from_be_bytes(chunk.try_into().unwrap())) | ||
.collect(), | ||
)) | ||
} | ||
} | ||
|
||
/// Returns concatenated strings, or an error if the concatenation buffer is too small. | ||
pub fn concatenate<'a>(strings: &[&str], output: &'a mut [u8]) -> Result<&'a str, ()> { | ||
let mut offset = 0; | ||
|
||
for s in strings { | ||
let s_len = s.len(); | ||
if offset + s_len > output.len() { | ||
return Err(()); | ||
} | ||
|
||
output[offset..offset + s_len].copy_from_slice(s.as_bytes()); | ||
offset += s_len; | ||
} | ||
|
||
Ok(from_utf8(&output[..offset]).unwrap()) | ||
} |