Skip to content

Commit

Permalink
Check value not bigger than 0x8000'0000
Browse files Browse the repository at this point in the history
  • Loading branch information
rllola committed Mar 9, 2020
1 parent ce42876 commit a97ffbe
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ quick_error! {
InvalidVersion{
description("This version is not supported")
}
/// Invalid path
InvalidPath{
description("Invalid path value (bigger than 0x8000'0000)")
}
/// The message cannot be empty
InvalidEmptyMessage{
description("message cannot be empty")
Expand Down Expand Up @@ -158,16 +162,27 @@ pub struct BIP44Path {
pub index: u32,
}

fn serialize_bip44(path: &BIP44Path) -> Vec<u8> {
fn serialize_bip44(path: &BIP44Path) -> Result<Vec<u8>, Error> {
use byteorder::{LittleEndian, WriteBytesExt};
let mut m = Vec::new();
let harden = 0x8000_0000;

if path.purpose > harden
|| path.coin > harden
|| path.account > harden
|| path.change > harden
|| path.index > harden
{
return Err(Error::InvalidPath);
}

m.write_u32::<LittleEndian>(harden | path.purpose).unwrap();
m.write_u32::<LittleEndian>(harden | path.coin).unwrap();
m.write_u32::<LittleEndian>(path.account).unwrap();
m.write_u32::<LittleEndian>(path.change).unwrap();
m.write_u32::<LittleEndian>(path.index).unwrap();
m

Ok(m)
}

impl FilecoinApp {
Expand Down Expand Up @@ -209,7 +224,7 @@ impl FilecoinApp {

/// Retrieves the public key and address
pub fn address(&self, path: &BIP44Path, require_confirmation: bool) -> Result<Address, Error> {
let serialized_path = serialize_bip44(path);
let serialized_path = serialize_bip44(path)?;
let p1 = if require_confirmation { 1 } else { 0 };

let command = ApduCommand {
Expand Down Expand Up @@ -250,7 +265,7 @@ impl FilecoinApp {

/// Sign a transaction
pub fn sign(&self, path: &BIP44Path, message: &[u8]) -> Result<Signature, Error> {
let bip44path = serialize_bip44(&path);
let bip44path = serialize_bip44(&path)?;
let chunks = message.chunks(USER_MESSAGE_CHUNK_SIZE);

if chunks.len() > 255 {
Expand Down Expand Up @@ -335,7 +350,7 @@ mod tests {
change: 0,
index: 0x5678,
};
let serialized_path = serialize_bip44(&path);
let serialized_path = serialize_bip44(&path).unwrap();
assert_eq!(serialized_path.len(), 20);
assert_eq!(
to_hex_string(&serialized_path),
Expand Down

0 comments on commit a97ffbe

Please sign in to comment.