Skip to content

Commit

Permalink
Enforce v size during create
Browse files Browse the repository at this point in the history
  • Loading branch information
diehuxx committed May 14, 2024
1 parent e318447 commit 20dc8e1
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions crates/dids/src/method/dht/bep44.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ impl Bep44Message {
where
F: Fn(Vec<u8>) -> Result<Vec<u8>, KeyError>,
{
let message_len = message.len();
if message_len > MAX_V_LEN {
return Err(Bep44EncodingError::SizeError(message_len));
}

let seq = SystemTime::now().duration_since(UNIX_EPOCH)?.as_secs();

let signable = signable(seq, message);
Expand Down Expand Up @@ -156,6 +161,18 @@ mod tests {
assert!(verify_result.is_ok());
}

#[test]
fn test_new_message_too_big() {
let too_big = vec![0; 10_000];
let error = Bep44Message::new(&too_big, |_| -> Result<Vec<u8>, KeyError> { Ok(vec![]) })
.expect_err("Should have returned error for malformed signature");

match error {
Bep44EncodingError::SizeError(size) => assert_eq!(size, 10_000),
_ => panic!(),
}
}

#[test]
fn test_new_sign_fails() {
let message = "Hello World".as_bytes();
Expand Down

0 comments on commit 20dc8e1

Please sign in to comment.