Skip to content

Commit

Permalink
Improve EIP-712 type string parsing (#302)
Browse files Browse the repository at this point in the history
* Test some EIP-712 type string parsing

* Catch unmatched bracket in EIP-712 type string
  • Loading branch information
clehner committed Sep 22, 2021
1 parent 400effa commit c666c74
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/eip712.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ static EMPTY_32: [u8; 32] = [0; 32];
pub enum TypedDataParseError {
#[error("Unexpected null value")]
UnexpectedNull,
#[error("Unmatched bracket")]
UnmatchedBracket,
#[error("Unexpected number: {0:?}")]
Number(Number),
#[error("Unable to parse data type size: {0}")]
Expand All @@ -40,7 +42,7 @@ pub struct MemberVariable {
}

/// EIP-712 types
#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(untagged)]
#[serde(try_from = "String", into = "String")]
pub enum EIP712Type {
Expand Down Expand Up @@ -272,7 +274,8 @@ impl TryFrom<String> for EIP712Type {
if string.ends_with("]") {
let mut parts = string.rsplitn(2, "[");
let amount_str = parts.next().unwrap().split("]").next().unwrap();
let base = EIP712Type::try_from(parts.next().unwrap().to_string())?;
let inner = parts.next().ok_or(TypedDataParseError::UnmatchedBracket)?;
let base = EIP712Type::try_from(inner.to_string())?;
if amount_str.len() == 0 {
return Ok(EIP712Type::Array(Box::new(base)));
} else {
Expand Down Expand Up @@ -845,6 +848,18 @@ mod tests {
use super::*;
use serde_json::json;

#[test]
fn test_parse_type() {
let string_type = EIP712Type::try_from(String::from("string")).unwrap();
assert_eq!(string_type, EIP712Type::String);

let string_array_type = EIP712Type::try_from(String::from("string[]")).unwrap();
let string_array_type_expected = EIP712Type::Array(Box::new(EIP712Type::String));
assert_eq!(string_array_type, string_array_type_expected);

EIP712Type::try_from(String::from("string]")).unwrap_err();
}

#[test]
fn test_encode_type() {
let types = Types {
Expand Down

0 comments on commit c666c74

Please sign in to comment.