Skip to content

Commit

Permalink
Add basic decode for its hub data.
Browse files Browse the repository at this point in the history
  • Loading branch information
raress96 committed Oct 18, 2024
1 parent 6263755 commit 78e18a0
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 60 deletions.
76 changes: 31 additions & 45 deletions ampd/src/stacks/its_verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ use crate::stacks::error::Error;
use crate::stacks::http_client::TransactionEvents;
use crate::stacks::verifier::{CONTRACT_CALL_TYPE, PRINT_TOPIC};

const MESSAGE_TYPE_INTERCHAIN_TRANSFER: u128 = 0;
const MESSAGE_TYPE_DEPLOY_INTERCHAIN_TOKEN: u128 = 1;
const MESSAGE_TYPE_DEPLOY_TOKEN_MANAGER: u128 = 2;
const MESSAGE_TYPE_SEND_TO_HUB: u128 = 3;

impl Message {
pub fn eq_its_hub_event(
&self,
Expand All @@ -20,29 +25,17 @@ impl Message {
}

let tuple_type_signature = TupleTypeSignature::try_from(vec![
(
ClarityName::from("type"),
TypeSignature::SequenceType(SequenceSubtype::StringType(StringSubtype::ASCII(
BufferLength::try_from(13u32)?,
))),
),
(ClarityName::from("sender"), TypeSignature::PrincipalType),
(ClarityName::from("type"), TypeSignature::UIntType),
(
ClarityName::from("destination-chain"),
TypeSignature::SequenceType(SequenceSubtype::BufferType(BufferLength::try_from(
18u32,
)?)),
),
(
ClarityName::from("destination-contract-address"),
TypeSignature::SequenceType(SequenceSubtype::BufferType(BufferLength::try_from(
96u32,
)?)),
),
(
ClarityName::from("payload-hash"),
ClarityName::from("payload"),
TypeSignature::SequenceType(SequenceSubtype::BufferType(BufferLength::try_from(
32u32,
10240u32,
)?)),
),
])?;
Expand All @@ -57,41 +50,34 @@ impl Message {
Value::try_deserialize_hex(hex, &TypeSignature::TupleType(tuple_type_signature), true)?;

if let Value::Tuple(data) = value {
if !data.get("type")?.eq(&Value::string_ascii_from_bytes(
CONTRACT_CALL_TYPE.as_bytes().to_vec(),
)?) {
return Ok(false);
}

if !data.get("sender")?.eq(&Value::from(PrincipalData::parse(
self.source_address.as_str(),
)?)) {
return Ok(false);
}

if !data.get("destination-chain")?.eq(&Value::buff_from(
self.destination_chain.as_ref().as_bytes().to_vec(),
)?) {
return Ok(false);
}

if !data
.get("destination-contract-address")?
.eq(&Value::buff_from(
self.destination_address.as_bytes().to_vec(),
)?)
{
// All messages should go through ITS hub
if !data.get("type")?.eq(&Value::UInt(MESSAGE_TYPE_SEND_TO_HUB)) {
return Ok(false);
}

if !data
.get("payload-hash")?
.eq(&Value::buff_from(self.payload_hash.as_bytes().to_vec())?)
{
return Ok(false);
let subtuple_type_signature = TupleTypeSignature::try_from(vec![(
ClarityName::from("type"),
TypeSignature::UIntType,
)])?;

let original_value = Value::try_deserialize_hex(
hex,
&TypeSignature::TupleType(subtuple_type_signature),
true,
)?;

// Unwrapp its payload
if let Value::Tuple(new_data) = original_value {
if new_data.get("type")?.eq(&Value::UInt(MESSAGE_TYPE_INTERCHAIN_TRANSFER)) {
// TODO: Decode and ABI encode this payload
} else if new_data.get("type")?.eq(&Value::UInt(MESSAGE_TYPE_DEPLOY_INTERCHAIN_TOKEN)) {
// TODO: Decode and ABI encode this payload
} else if new_data.get("type")?.eq(&Value::UInt(MESSAGE_TYPE_DEPLOY_TOKEN_MANAGER)) {
// TODO: Decode and ABI encode this payload
}
}

return Ok(true);
return Ok(false);
}

Ok(false)
Expand Down
28 changes: 13 additions & 15 deletions ampd/src/stacks/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,30 +191,28 @@ pub fn verify_message(

match find_event(transaction, gateway_address, message.event_index) {
Some(event) => {
// In case message is from its
if &message.source_address == its_address {
// In case messages is from Stacks -> Stacks and from ITS -> ITS, use custom logic
if &message.destination_chain == source_chain
&& &message.destination_address == its_address
{
if message.eq_its_verify_event(event).unwrap_or(false) {
return Vote::SucceededOnChain;
}

return Vote::NotFound;
// In case message is not from ITS
if &message.source_address != its_address {
if message.eq_event(event).unwrap_or(false) {
return Vote::SucceededOnChain;
}

// TODO: Should we check if the message is towards axelar and ITS Hub contract here?
// In other case, abi encode payload
return Vote::NotFound;
}

if message.eq_its_hub_event(event).unwrap_or(false) {
// In case messages is from Stacks -> Stacks and from ITS -> ITS, use custom logic
if &message.destination_chain == source_chain
&& &message.destination_address == its_address
{
if message.eq_its_verify_event(event).unwrap_or(false) {
return Vote::SucceededOnChain;
}

return Vote::NotFound;
}

if message.eq_event(event).unwrap_or(false) {
// In other case, abi encode payload coming from Stacks ITS
if message.eq_its_hub_event(event).unwrap_or(false) {
return Vote::SucceededOnChain;
}

Expand Down

0 comments on commit 78e18a0

Please sign in to comment.