Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unrecognized even fields render inscriptions invalid #1107

Merged
merged 3 commits into from
Dec 29, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 36 additions & 24 deletions src/inscription.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,11 @@ impl Inscription {
#[derive(Debug, PartialEq)]
enum InscriptionError {
EmptyWitness,
InvalidInscription,
KeyPathSpend,
Script(script::Error),
NoInscription,
InvalidInscription,
Script(script::Error),
UnrecognizedEvenField,
}

type Result<T, E = InscriptionError> = std::result::Result<T, E>;
Expand Down Expand Up @@ -214,9 +215,20 @@ impl<'a> InscriptionParser<'a> {
}
}

let content = fields.remove(CONTENT_TAG);
let content_type = fields.remove(CONTENT_TYPE_TAG);

for tag in fields.keys() {
if let Some(lsb) = tag.first() {
if lsb % 2 == 0 {
return Err(InscriptionError::UnrecognizedEvenField);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So non-breaking changes cause an InscriptionError?

}
}
}

return Ok(Some(Inscription {
content: fields.remove(CONTENT_TAG),
content_type: fields.remove(CONTENT_TYPE_TAG),
content,
content_type,
}));
}

Expand Down Expand Up @@ -341,7 +353,7 @@ mod tests {
b"ord",
&[1],
b"text/plain;charset=utf-8",
&[2],
&[3],
b"bar",
&[],
b"ord",
Expand Down Expand Up @@ -537,25 +549,6 @@ mod tests {
);
}

#[test]
fn junk() {
assert_eq!(
InscriptionParser::parse(&container(&[
b"ord",
&[2],
&[1],
b"foo",
&[],
b"ord",
b"ord"
])),
Ok(Inscription {
content_type: None,
content: None,
}),
);
}

#[test]
fn extract_from_transaction() {
let tx = Transaction {
Expand Down Expand Up @@ -703,4 +696,23 @@ mod tests {
}
);
}

#[test]
fn unknown_odd_fields_are_ignored() {
assert_eq!(
InscriptionParser::parse(&container(&[b"ord", &[3], &[0]])),
Ok(Inscription {
content_type: None,
content: None,
}),
);
}

#[test]
fn unknown_even_fields_are_invalid() {
assert_eq!(
InscriptionParser::parse(&container(&[b"ord", &[2], &[0]])),
Err(InscriptionError::UnrecognizedEvenField),
);
}
}