From 26839604c815a8939baa9ae216cd3d870ffa58cc Mon Sep 17 00:00:00 2001 From: Conor Patrick Date: Wed, 15 Mar 2023 16:55:12 +0000 Subject: [PATCH] accountant: export governance modules from sdk to double check in contract --- .../contracts/global-accountant/src/contract.rs | 17 ++++++++++++----- sdk/rust/core/src/accountant.rs | 16 ++++++++-------- sdk/rust/core/src/nft.rs | 12 ++++-------- sdk/rust/core/src/token.rs | 8 ++++---- 4 files changed, 28 insertions(+), 25 deletions(-) diff --git a/cosmwasm/contracts/global-accountant/src/contract.rs b/cosmwasm/contracts/global-accountant/src/contract.rs index a00fa21e8..23966a51a 100644 --- a/cosmwasm/contracts/global-accountant/src/contract.rs +++ b/cosmwasm/contracts/global-accountant/src/contract.rs @@ -334,14 +334,21 @@ fn handle_vaa( let mut evt = if body.emitter_chain == Chain::Solana && body.emitter_address == wormhole::GOVERNANCE_EMITTER { - if let Ok(govpacket) = serde_wormhole::from_slice::(body.payload) { + if body.payload.len() < 32 { + bail!("governance module missing"); + } + let module = &body.payload[..32]; + + if module == token::MODULE { + let govpacket = serde_wormhole::from_slice(body.payload) + .context("failed to parse tokenbridge governance packet")?; handle_token_governance_vaa(deps.branch(), body.with_payload(govpacket))? - } else if let Ok(govpacket) = - serde_wormhole::from_slice::(body.payload) - { + } else if module == accountant_module::MODULE { + let govpacket = serde_wormhole::from_slice(body.payload) + .context("failed to parse accountant governance packet")?; handle_accountant_governance_vaa(deps.branch(), info, body.with_payload(govpacket))? } else { - bail!("Unknown governance module") + bail!("unknown governance module") } } else { let msg = serde_wormhole::from_slice(body.payload) diff --git a/sdk/rust/core/src/accountant.rs b/sdk/rust/core/src/accountant.rs index 89cd0a08e..3f7267ef5 100644 --- a/sdk/rust/core/src/accountant.rs +++ b/sdk/rust/core/src/accountant.rs @@ -54,7 +54,7 @@ impl<'de> Deserialize<'de> for ModificationKind { } } -/// Represents a governance action targeted at the NFT bridge. +/// Represents a governance action targeted at the Accountant. #[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub enum Action { // Modify balance for accountant @@ -71,7 +71,7 @@ pub enum Action { }, } -/// Represents the payload for a governance VAA targeted at the NFT bridge. +/// Represents the payload for a governance VAA targeted at the Accountant. #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct GovernancePacket { /// The chain on which the governance action should be carried out. @@ -81,6 +81,10 @@ pub struct GovernancePacket { pub action: Action, } +// MODULE = "GlobalAccountant" +pub const MODULE: [u8; 32] = + *b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00GlobalAccountant"; + // The wire format for GovernancePackets is wonky and doesn't lend itself well to auto-deriving // Serialize / Deserialize so we implement it manually here. mod governance_packet_impl { @@ -93,14 +97,10 @@ mod governance_packet_impl { }; use crate::{ - accountant::{Action, GovernancePacket}, + accountant::{Action, GovernancePacket, MODULE}, Address, Amount, }; - // MODULE = "GlobalAccountant" - const MODULE: [u8; 32] = - *b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00GlobalAccountant"; - struct Module; impl Serialize for Module { @@ -123,7 +123,7 @@ mod governance_packet_impl { Ok(Module) } else { Err(Error::custom( - "invalid governance module, expected \"NFTBridge\"", + "invalid governance module, expected \"GlobalAccountant\"", )) } } diff --git a/sdk/rust/core/src/nft.rs b/sdk/rust/core/src/nft.rs index d5ce1e35b..0c3ae906c 100644 --- a/sdk/rust/core/src/nft.rs +++ b/sdk/rust/core/src/nft.rs @@ -78,6 +78,9 @@ pub struct GovernancePacket { pub action: Action, } +// MODULE = "NFTBridge" +pub const MODULE: [u8; 32] = *b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00NFTBridge"; + // The wire format for GovernancePackets is wonky and doesn't lend itself well to auto-deriving // Serialize / Deserialize so we implement it manually here. mod governance_packet_impl { @@ -90,17 +93,10 @@ mod governance_packet_impl { }; use crate::{ - nft::{Action, GovernancePacket}, + nft::{Action, GovernancePacket, MODULE}, Address, Chain, }; - // MODULE = "NFTBridge" - const MODULE: [u8; 32] = [ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4e, 0x46, 0x54, 0x42, 0x72, 0x69, 0x64, - 0x67, 0x65, - ]; - struct Module; impl Serialize for Module { diff --git a/sdk/rust/core/src/token.rs b/sdk/rust/core/src/token.rs index 2634e6463..968fc372c 100644 --- a/sdk/rust/core/src/token.rs +++ b/sdk/rust/core/src/token.rs @@ -228,6 +228,9 @@ pub struct GovernancePacket { pub action: Action, } +// MODULE = "TokenBridge" +pub const MODULE: [u8; 32] = *b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00TokenBridge"; + // The wire format for GovernancePackets is wonky and doesn't lend itself well to auto-deriving // Serialize / Deserialize so we implement it manually here. mod governance_packet_impl { @@ -240,13 +243,10 @@ mod governance_packet_impl { }; use crate::{ - token::{Action, GovernancePacket}, + token::{Action, GovernancePacket, MODULE}, Address, Chain, }; - // MODULE = "TokenBridge" - const MODULE: [u8; 32] = *b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00TokenBridge"; - struct Module; impl Serialize for Module {