Skip to content

Commit

Permalink
fix(xrpl-ampd): gracefully return false if memo type is not valid hex
Browse files Browse the repository at this point in the history
  • Loading branch information
k4m4 committed Jan 15, 2025
1 parent a5071d9 commit 84bd100
Showing 1 changed file with 21 additions and 25 deletions.
46 changes: 21 additions & 25 deletions ampd/src/xrpl/verifier.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::collections::HashMap;

use sha3::{Digest, Keccak256};
use xrpl_http_client::{Amount, ResultCategory};
use xrpl_http_client::{Memo, Transaction::Payment, Transaction};
use axelar_wasm_std::voting::Vote;
Expand Down Expand Up @@ -92,31 +91,28 @@ pub fn verify_amount(amount: Amount, message: &XRPLUserMessage) -> bool {
pub fn verify_memos(memos: Vec<Memo>, message: &XRPLUserMessage) -> bool {
let memo_kv: HashMap<String, String> = memos
.into_iter()
.filter(|m| m.memo_type.is_some() && m.memo_data.is_some())
.map(|m| (String::from_utf8(hex::decode(m.memo_type.unwrap()).expect("Memo value should be hex")).ok(), m.memo_data))
.filter_map(|(k, v)| {
match (k, v) {
(Some(k), Some(v)) => Some((k, v)),
_ => None,
}
.filter_map(|m| {
let memo_type = m
.memo_type
.and_then(|t| hex::decode(t).ok())
.and_then(|bytes| String::from_utf8(bytes).ok());

let memo_data = m.memo_data;
memo_type.zip(memo_data)
})
.collect();

|| -> Option<bool> {
let payload_hash: Option<[u8; 32]> = match memo_kv.get("payload_hash").clone() {
Some(hash) => hex::decode(&remove_0x_prefix(hash.clone())).ok()?.try_into().ok(),
None => match memo_kv.get("payload") {
Some(payload) => Keccak256::digest(hex::decode(&remove_0x_prefix(payload.clone())).ok()?).as_slice().try_into().ok(),
None => None,
},
};

Some(
memo_kv.get("destination_address")? == &remove_0x_prefix(message.destination_address.to_string()).to_uppercase()
&& memo_kv.get("destination_chain")? == &hex::encode_upper(message.destination_chain.to_string())
&& payload_hash == message.payload_hash
)
}().unwrap_or(false)
.collect::<HashMap<String, String>>();

let destination_address = remove_0x_prefix(message.destination_address.to_string()).to_uppercase();
let destination_chain = hex::encode_upper(message.destination_chain.to_string());

memo_kv.get("destination_address") == Some(&destination_address)
&& memo_kv.get("destination_chain") == Some(&destination_chain)
&& memo_kv
.get("payload_hash")
.and_then(|payload_hash_hex| {
hex::decode(remove_0x_prefix(payload_hash_hex.to_owned())).ok()
})
.map_or(false, |payload_hash| message.payload_hash == payload_hash.try_into().ok())
}

#[cfg(test)]
Expand Down

0 comments on commit 84bd100

Please sign in to comment.