diff --git a/solana/bridge/client/src/main.rs b/solana/bridge/client/src/main.rs index ad1e716b..89023ad3 100644 --- a/solana/bridge/client/src/main.rs +++ b/solana/bridge/client/src/main.rs @@ -11,9 +11,9 @@ use borsh::BorshDeserialize; use bridge::{ accounts::{ Bridge, + BridgeData, FeeCollector, }, - types::BridgeData, }; use clap::{ crate_description, diff --git a/solana/bridge/program/src/accounts.rs b/solana/bridge/program/src/accounts.rs index 1d34a4df..ceca0b90 100644 --- a/solana/bridge/program/src/accounts.rs +++ b/solana/bridge/program/src/accounts.rs @@ -1,86 +1,17 @@ -use crate::{ - types, - types::{ - BridgeData, - PostedMessageData, - PostedVAAData, - SequenceTracker, - }, -}; -use solana_program::pubkey::Pubkey; -use solitaire::{ - processors::seeded::Seeded, - AccountState, - Data, - Derive, - Info, -}; +pub mod bridge; +pub mod claim; +pub mod fee_collector; +pub mod guardian_set; +pub mod posted_message; +pub mod posted_vaa; +pub mod sequence; +pub mod signature_set; -pub type FeeCollector<'a> = Derive, "fee_collector">; -pub type Bridge<'a, const State: AccountState> = Derive, "Bridge">; - -pub type GuardianSet<'b, const State: AccountState> = Data<'b, types::GuardianSetData, { State }>; - -pub struct GuardianSetDerivationData { - pub index: u32, -} - -impl<'b, const State: AccountState> Seeded<&GuardianSetDerivationData> - for GuardianSet<'b, { State }> -{ - fn seeds(data: &GuardianSetDerivationData) -> Vec> { - vec![ - "GuardianSet".as_bytes().to_vec(), - data.index.to_be_bytes().to_vec(), - ] - } -} - -pub type Claim<'b, const State: AccountState> = Data<'b, types::ClaimData, { State }>; - -pub struct ClaimDerivationData { - pub emitter_address: [u8; 32], - pub emitter_chain: u16, - pub sequence: u64, -} - -impl<'b, const State: AccountState> Seeded<&ClaimDerivationData> for Claim<'b, { State }> { - fn seeds(data: &ClaimDerivationData) -> Vec> { - return vec![ - data.emitter_address.to_vec(), - data.emitter_chain.to_be_bytes().to_vec(), - data.sequence.to_be_bytes().to_vec(), - ]; - } -} - -pub type SignatureSet<'b, const State: AccountState> = Data<'b, types::SignatureSet, { State }>; - -pub type PostedMessage<'b, const State: AccountState> = Data<'b, PostedMessageData, { State }>; - -pub type PostedVAA<'b, const State: AccountState> = Data<'b, PostedVAAData, { State }>; - -pub struct PostedVAADerivationData { - pub payload_hash: Vec, -} - -impl<'b, const State: AccountState> Seeded<&PostedVAADerivationData> for PostedVAA<'b, { State }> { - fn seeds(data: &PostedVAADerivationData) -> Vec> { - vec!["PostedVAA".as_bytes().to_vec(), data.payload_hash.to_vec()] - } -} - -pub type Sequence<'b> = Data<'b, SequenceTracker, { AccountState::MaybeInitialized }>; - -pub struct SequenceDerivationData<'a> { - pub emitter_key: &'a Pubkey, -} - -impl<'b> Seeded<&SequenceDerivationData<'b>> for Sequence<'b> { - fn seeds(data: &SequenceDerivationData) -> Vec> { - vec![ - "Sequence".as_bytes().to_vec(), - data.emitter_key.to_bytes().to_vec(), - ] - } -} +pub use bridge::*; +pub use claim::*; +pub use fee_collector::*; +pub use guardian_set::*; +pub use posted_message::*; +pub use posted_vaa::*; +pub use sequence::*; +pub use signature_set::*; diff --git a/solana/bridge/program/src/accounts/bridge.rs b/solana/bridge/program/src/accounts/bridge.rs new file mode 100644 index 00000000..b074463f --- /dev/null +++ b/solana/bridge/program/src/accounts/bridge.rs @@ -0,0 +1,59 @@ +//! The Bridge account contains the main state for the wormhole bridge, as well as tracking +//! configuration options for how the bridge should behave. + +use borsh::{ + BorshDeserialize, + BorshSerialize, +}; +use serde::{ + Deserialize, + Serialize, +}; +use solitaire::{ + AccountOwner, + AccountState, + Data, + Derive, + Owned, +}; + +pub type Bridge<'a, const State: AccountState> = Derive, "Bridge">; + +#[derive(Clone, Default, BorshSerialize, BorshDeserialize, Serialize, Deserialize)] +pub struct BridgeData { + /// The current guardian set index, used to decide which signature sets to accept. + pub guardian_set_index: u32, + + /// Lamports in the collection account + pub last_lamports: u64, + + /// Bridge configuration, which is set once upon initialization. + pub config: BridgeConfig, +} + +#[cfg(not(feature = "cpi"))] +impl Owned for BridgeData { + fn owner(&self) -> AccountOwner { + AccountOwner::This + } +} + +#[cfg(feature = "cpi")] +impl Owned for BridgeData { + fn owner(&self) -> AccountOwner { + use std::str::FromStr; + use solana_program::pubkey::Pubkey; + AccountOwner::Other(Pubkey::from_str(env!("BRIDGE_ADDRESS")).unwrap()) + } +} + +#[derive(Clone, Default, BorshSerialize, BorshDeserialize, Serialize, Deserialize)] +pub struct BridgeConfig { + /// Period for how long a guardian set is valid after it has been replaced by a new one. This + /// guarantees that VAAs issued by that set can still be submitted for a certain period. In + /// this period we still trust the old guardian set. + pub guardian_set_expiration_time: u32, + + /// Amount of lamports that needs to be paid to the protocol to post a message + pub fee: u64, +} diff --git a/solana/bridge/program/src/accounts/claim.rs b/solana/bridge/program/src/accounts/claim.rs new file mode 100644 index 00000000..9048b24c --- /dev/null +++ b/solana/bridge/program/src/accounts/claim.rs @@ -0,0 +1,47 @@ +//! ClaimData accounts are one off markers that can be combined with other accounts to represent +//! data that can only be used once. + +use borsh::{ + BorshDeserialize, + BorshSerialize, +}; +use serde::{ + Deserialize, + Serialize, +}; +use solitaire::{ + processors::seeded::Seeded, + AccountOwner, + AccountState, + Data, + Owned, +}; + +pub type Claim<'a, const State: AccountState> = Data<'a, ClaimData, { State }>; + +#[derive(Default, Clone, Copy, BorshDeserialize, BorshSerialize, Serialize, Deserialize)] +pub struct ClaimData { + pub claimed: bool, +} + +impl Owned for ClaimData { + fn owner(&self) -> AccountOwner { + AccountOwner::This + } +} + +pub struct ClaimDerivationData { + pub emitter_address: [u8; 32], + pub emitter_chain: u16, + pub sequence: u64, +} + +impl<'b, const State: AccountState> Seeded<&ClaimDerivationData> for Claim<'b, { State }> { + fn seeds(data: &ClaimDerivationData) -> Vec> { + return vec![ + data.emitter_address.to_vec(), + data.emitter_chain.to_be_bytes().to_vec(), + data.sequence.to_be_bytes().to_vec(), + ]; + } +} diff --git a/solana/bridge/program/src/accounts/fee_collector.rs b/solana/bridge/program/src/accounts/fee_collector.rs new file mode 100644 index 00000000..a7256860 --- /dev/null +++ b/solana/bridge/program/src/accounts/fee_collector.rs @@ -0,0 +1,8 @@ +//! The FeeCollector is a simple account that collects SOL fees. + +use solitaire::{ + Derive, + Info, +}; + +pub type FeeCollector<'a> = Derive, "fee_collector">; diff --git a/solana/bridge/program/src/accounts/guardian_set.rs b/solana/bridge/program/src/accounts/guardian_set.rs new file mode 100644 index 00000000..fd945948 --- /dev/null +++ b/solana/bridge/program/src/accounts/guardian_set.rs @@ -0,0 +1,65 @@ +//! GuardianSet represents an account containing information about the current active guardians +//! responsible for signing wormhole VAAs. + +use crate::types::GuardianPublicKey; +use borsh::{ + BorshDeserialize, + BorshSerialize, +}; +use serde::{ + Deserialize, + Serialize, +}; +use solitaire::{ + processors::seeded::Seeded, + AccountOwner, + AccountState, + Data, + Owned, +}; + +pub type GuardianSet<'b, const State: AccountState> = Data<'b, GuardianSetData, { State }>; + +#[derive(Default, BorshSerialize, BorshDeserialize, Serialize, Deserialize)] +pub struct GuardianSetData { + /// Index representing an incrementing version number for this guardian set. + pub index: u32, + + /// ETH style public keys + pub keys: Vec, + + /// Timestamp representing the time this guardian became active. + pub creation_time: u32, + + /// Expiration time when VAAs issued by this set are no longer valid. + pub expiration_time: u32, +} + +/// GuardianSet account PDAs are indexed by their version number. +pub struct GuardianSetDerivationData { + pub index: u32, +} + +impl<'a, const State: AccountState> Seeded<&GuardianSetDerivationData> + for GuardianSet<'a, { State }> +{ + fn seeds(data: &GuardianSetDerivationData) -> Vec> { + vec![ + "GuardianSet".as_bytes().to_vec(), + data.index.to_be_bytes().to_vec(), + ] + } +} + +impl GuardianSetData { + /// Number of guardians in the set + pub fn num_guardians(&self) -> u8 { + self.keys.iter().filter(|v| **v != [0u8; 20]).count() as u8 + } +} + +impl Owned for GuardianSetData { + fn owner(&self) -> AccountOwner { + AccountOwner::This + } +} diff --git a/solana/bridge/program/src/accounts/posted_message.rs b/solana/bridge/program/src/accounts/posted_message.rs new file mode 100644 index 00000000..0da02b38 --- /dev/null +++ b/solana/bridge/program/src/accounts/posted_message.rs @@ -0,0 +1,119 @@ +use borsh::{ + BorshDeserialize, + BorshSerialize, +}; +use serde::{ + Deserialize, + Serialize, +}; +use solana_program::pubkey::Pubkey; +use solitaire::{ + AccountOwner, + AccountState, + Data, + Owned, +}; +use std::{ + io::Write, + ops::{ + Deref, + DerefMut, + }, +}; + +pub type PostedMessage<'a, const State: AccountState> = Data<'a, PostedMessageData, { State }>; + +// This is using the same payload as the PostedVAA for backwards compatibility. +// This will be deprecated in a future release. +#[repr(transparent)] +pub struct PostedMessageData(pub MessageData); + +#[derive(Default, BorshSerialize, BorshDeserialize, Clone, Serialize, Deserialize)] +pub struct MessageData { + /// Header of the posted VAA + pub vaa_version: u8, + + /// Level of consistency requested by the emitter + pub consistency_level: u8, + + /// Time the vaa was submitted + pub vaa_time: u32, + + /// Account where signatures are stored + pub vaa_signature_account: Pubkey, + + /// Time the posted message was created + pub submission_time: u32, + + /// Unique nonce for this message + pub nonce: u32, + + /// Sequence number of this message + pub sequence: u64, + + /// Emitter of the message + pub emitter_chain: u16, + + /// Emitter of the message + pub emitter_address: [u8; 32], + + /// Message payload + pub payload: Vec, +} + +impl BorshSerialize for PostedMessageData { + fn serialize(&self, writer: &mut W) -> std::io::Result<()> { + writer.write(b"msg")?; + BorshSerialize::serialize(&self.0, writer) + } +} + +impl BorshDeserialize for PostedMessageData { + fn deserialize(buf: &mut &[u8]) -> std::io::Result { + *buf = &buf[3..]; + Ok(PostedMessageData( + ::deserialize(buf)?, + )) + } +} + +impl Deref for PostedMessageData { + type Target = MessageData; + + fn deref(&self) -> &Self::Target { + unsafe { std::mem::transmute(&self.0) } + } +} + +impl DerefMut for PostedMessageData { + fn deref_mut(&mut self) -> &mut Self::Target { + unsafe { std::mem::transmute(&mut self.0) } + } +} + +impl Default for PostedMessageData { + fn default() -> Self { + PostedMessageData(MessageData::default()) + } +} + +impl Clone for PostedMessageData { + fn clone(&self) -> Self { + PostedMessageData(self.0.clone()) + } +} + +#[cfg(not(feature = "cpi"))] +impl Owned for PostedMessageData { + fn owner(&self) -> AccountOwner { + AccountOwner::This + } +} + +#[cfg(feature = "cpi")] +impl Owned for PostedMessageData { + fn owner(&self) -> AccountOwner { + use std::str::FromStr; + AccountOwner::Other(Pubkey::from_str(env!("BRIDGE_ADDRESS")).unwrap()) + } +} diff --git a/solana/bridge/program/src/accounts/posted_vaa.rs b/solana/bridge/program/src/accounts/posted_vaa.rs new file mode 100644 index 00000000..c0ae23cf --- /dev/null +++ b/solana/bridge/program/src/accounts/posted_vaa.rs @@ -0,0 +1,91 @@ +use crate::MessageData; +use borsh::{ + BorshDeserialize, + BorshSerialize, +}; +use solitaire::{ + processors::seeded::Seeded, + AccountOwner, + AccountState, + Data, + Owned, +}; +use std::{ + io::Write, + ops::{ + Deref, + DerefMut, + }, +}; + +pub type PostedVAA<'b, const State: AccountState> = Data<'b, PostedVAAData, { State }>; + +pub struct PostedVAADerivationData { + pub payload_hash: Vec, +} + +impl<'a, const State: AccountState> Seeded<&PostedVAADerivationData> for PostedVAA<'a, { State }> { + fn seeds(data: &PostedVAADerivationData) -> Vec> { + vec!["PostedVAA".as_bytes().to_vec(), data.payload_hash.to_vec()] + } +} + +#[repr(transparent)] +pub struct PostedVAAData(pub MessageData); + +impl BorshSerialize for PostedVAAData { + fn serialize(&self, writer: &mut W) -> std::io::Result<()> { + writer.write(b"vaa")?; + BorshSerialize::serialize(&self.0, writer) + } +} + +impl BorshDeserialize for PostedVAAData { + fn deserialize(buf: &mut &[u8]) -> std::io::Result { + *buf = &buf[3..]; + Ok(PostedVAAData( + ::deserialize(buf)?, + )) + } +} + +impl Deref for PostedVAAData { + type Target = MessageData; + + fn deref(&self) -> &Self::Target { + unsafe { std::mem::transmute(&self.0) } + } +} + +impl DerefMut for PostedVAAData { + fn deref_mut(&mut self) -> &mut Self::Target { + unsafe { std::mem::transmute(&mut self.0) } + } +} + +impl Default for PostedVAAData { + fn default() -> Self { + PostedVAAData(MessageData::default()) + } +} + +impl Clone for PostedVAAData { + fn clone(&self) -> Self { + PostedVAAData(self.0.clone()) + } +} +#[cfg(not(feature = "cpi"))] +impl Owned for PostedVAAData { + fn owner(&self) -> AccountOwner { + AccountOwner::This + } +} + +#[cfg(feature = "cpi")] +impl Owned for PostedVAAData { + fn owner(&self) -> AccountOwner { + use std::str::FromStr; + use solana_program::pubkey::Pubkey; + AccountOwner::Other(Pubkey::from_str(env!("BRIDGE_ADDRESS")).unwrap()) + } +} diff --git a/solana/bridge/program/src/accounts/sequence.rs b/solana/bridge/program/src/accounts/sequence.rs new file mode 100644 index 00000000..4da59253 --- /dev/null +++ b/solana/bridge/program/src/accounts/sequence.rs @@ -0,0 +1,38 @@ +use borsh::{ + BorshDeserialize, + BorshSerialize, +}; +use solana_program::pubkey::Pubkey; +use solitaire::{ + processors::seeded::Seeded, + AccountState, + AccountOwner, + Data, + Owned, +}; + +pub type Sequence<'b> = Data<'b, SequenceTracker, { AccountState::MaybeInitialized }>; + +#[derive(Default, Clone, Copy, BorshDeserialize, BorshSerialize)] +pub struct SequenceTracker { + pub sequence: u64, +} + +pub struct SequenceDerivationData<'a> { + pub emitter_key: &'a Pubkey, +} + +impl<'b> Seeded<&SequenceDerivationData<'b>> for Sequence<'b> { + fn seeds(data: &SequenceDerivationData) -> Vec> { + vec![ + "Sequence".as_bytes().to_vec(), + data.emitter_key.to_bytes().to_vec(), + ] + } +} + +impl Owned for SequenceTracker { + fn owner(&self) -> AccountOwner { + AccountOwner::This + } +} diff --git a/solana/bridge/program/src/accounts/signature_set.rs b/solana/bridge/program/src/accounts/signature_set.rs new file mode 100644 index 00000000..a45129e6 --- /dev/null +++ b/solana/bridge/program/src/accounts/signature_set.rs @@ -0,0 +1,32 @@ +//! PostedMessage + +use borsh::{ + BorshDeserialize, + BorshSerialize, +}; +use solitaire::{ + AccountOwner, + AccountState, + Data, + Owned, +}; + +pub type SignatureSet<'b, const State: AccountState> = Data<'b, SignatureSetData, { State }>; + +#[derive(Default, BorshSerialize, BorshDeserialize)] +pub struct SignatureSetData { + /// Signatures of validators + pub signatures: Vec, + + /// Hash of the data + pub hash: [u8; 32], + + /// Index of the guardian set + pub guardian_set_index: u32, +} + +impl Owned for SignatureSetData { + fn owner(&self) -> AccountOwner { + AccountOwner::This + } +} diff --git a/solana/bridge/program/src/api/initialize.rs b/solana/bridge/program/src/api/initialize.rs index 9b67a808..31155ba7 100644 --- a/solana/bridge/program/src/api/initialize.rs +++ b/solana/bridge/program/src/api/initialize.rs @@ -1,12 +1,12 @@ use crate::{ accounts::{ Bridge, + BridgeConfig, FeeCollector, GuardianSet, GuardianSetDerivationData, }, error::Error::TooManyGuardians, - types::*, MAX_LEN_GUARDIAN_KEYS, }; use solana_program::sysvar::clock::Clock; diff --git a/solana/bridge/program/src/api/verify_signature.rs b/solana/bridge/program/src/api/verify_signature.rs index 085e5fc1..ed3acac2 100644 --- a/solana/bridge/program/src/api/verify_signature.rs +++ b/solana/bridge/program/src/api/verify_signature.rs @@ -1,11 +1,9 @@ use solitaire::*; use crate::{ - accounts::{ - GuardianSet, - GuardianSetDerivationData, - SignatureSet, - }, + GuardianSet, + GuardianSetDerivationData, + SignatureSet, error::Error::{ GuardianSetMismatch, InstructionAtWrongIndex, diff --git a/solana/bridge/program/src/lib.rs b/solana/bridge/program/src/lib.rs index 7f129e47..188846bf 100644 --- a/solana/bridge/program/src/lib.rs +++ b/solana/bridge/program/src/lib.rs @@ -2,11 +2,10 @@ #![allow(non_upper_case_globals)] #![allow(incomplete_features)] -pub mod accounts; -pub mod api; -pub mod error; -pub mod types; -pub mod vaa; +use solitaire::*; + +pub const MAX_LEN_GUARDIAN_KEYS: usize = 19; +pub const CHAIN_ID_SOLANA: u16 = 1; #[cfg(feature = "no-entrypoint")] pub mod instructions; @@ -19,8 +18,30 @@ extern crate wasm_bindgen; #[cfg(all(target_arch = "wasm32", target_os = "unknown"))] pub mod wasm; -use solitaire::*; +pub mod accounts; +pub use accounts::{ + BridgeConfig, + BridgeData, + Claim, + ClaimData, + ClaimDerivationData, + FeeCollector, + GuardianSet, + GuardianSetData, + GuardianSetDerivationData, + PostedMessage, + PostedMessageData, + MessageData, + PostedVAA, + PostedVAAData, + Sequence, + SequenceTracker, + SequenceDerivationData, + SignatureSet, + SignatureSetData, +}; +pub mod api; pub use api::{ initialize, post_message, @@ -50,6 +71,10 @@ pub use api::{ VerifySignaturesData, }; +pub mod error; +pub mod types; +pub mod vaa; + pub use vaa::{ DeserializeGovernancePayload, DeserializePayload, @@ -58,9 +83,6 @@ pub use vaa::{ SerializePayload, }; -pub const MAX_LEN_GUARDIAN_KEYS: usize = 19; -pub const CHAIN_ID_SOLANA: u16 = 1; - solitaire! { Initialize(InitializeData) => initialize, PostMessage(PostMessageData) => post_message, diff --git a/solana/bridge/program/src/types.rs b/solana/bridge/program/src/types.rs index e33954e9..db488fdb 100644 --- a/solana/bridge/program/src/types.rs +++ b/solana/bridge/program/src/types.rs @@ -24,284 +24,24 @@ use solana_program::{ program_error::ProgramError::InvalidAccountData, pubkey::Pubkey, }; -use solitaire::{ - processors::seeded::{ - AccountOwner, - Owned, - }, - SolitaireError, -}; +use solitaire::SolitaireError; use std::{ + self, io::{ Cursor, Read, Write, }, - ops::{ - Deref, - DerefMut, - }, }; -#[derive(Default, BorshSerialize, BorshDeserialize, Serialize, Deserialize)] -pub struct GuardianSetData { - /// Version number of this guardian set. - pub index: u32, +/// Type representing an Ethereum style public key for Guardians. +pub type GuardianPublicKey = [u8; 20]; - /// public key hashes of the guardian set - pub keys: Vec<[u8; 20]>, - - /// creation time - pub creation_time: u32, - - /// expiration time when VAAs issued by this set are no longer valid - pub expiration_time: u32, -} - -impl GuardianSetData { - /// Number of guardians in the set - pub fn num_guardians(&self) -> u8 { - self.keys.iter().filter(|v| **v != [0u8; 20]).count() as u8 - } -} - -impl Owned for GuardianSetData { - fn owner(&self) -> AccountOwner { - AccountOwner::This - } -} - -#[derive(Clone, Default, BorshSerialize, BorshDeserialize, Serialize, Deserialize)] -pub struct BridgeConfig { - /// Period for how long a guardian set is valid after it has been replaced by a new one. This - /// guarantees that VAAs issued by that set can still be submitted for a certain period. In - /// this period we still trust the old guardian set. - pub guardian_set_expiration_time: u32, - - /// Amount of lamports that needs to be paid to the protocol to post a message - pub fee: u64, -} - -#[derive(Clone, Default, BorshSerialize, BorshDeserialize, Serialize, Deserialize)] -pub struct BridgeData { - /// The current guardian set index, used to decide which signature sets to accept. - pub guardian_set_index: u32, - - /// Lamports in the collection account - pub last_lamports: u64, - - /// Bridge configuration, which is set once upon initialization. - pub config: BridgeConfig, -} - -#[cfg(not(feature = "cpi"))] -impl Owned for BridgeData { - fn owner(&self) -> AccountOwner { - AccountOwner::This - } -} - -#[cfg(feature = "cpi")] -impl Owned for BridgeData { - fn owner(&self) -> AccountOwner { - use std::str::FromStr; - AccountOwner::Other(Pubkey::from_str(env!("BRIDGE_ADDRESS")).unwrap()) - } -} - -#[derive(Default, BorshSerialize, BorshDeserialize)] -pub struct SignatureSet { - /// Signatures of validators - pub signatures: Vec, - - /// Hash of the data - pub hash: [u8; 32], - - /// Index of the guardian set - pub guardian_set_index: u32, -} - -impl Owned for SignatureSet { - fn owner(&self) -> AccountOwner { - AccountOwner::This - } -} - -// This is using the same payload as the PostedVAA for backwards compatibility. -// This will be deprecated in a future release. -#[repr(transparent)] -pub struct PostedMessageData(pub MessageData); - -impl BorshSerialize for PostedMessageData { - fn serialize(&self, writer: &mut W) -> std::io::Result<()> { - writer.write(b"msg")?; - BorshSerialize::serialize(&self.0, writer) - } -} - -impl BorshDeserialize for PostedMessageData { - fn deserialize(buf: &mut &[u8]) -> std::io::Result { - *buf = &buf[3..]; - Ok(PostedMessageData( - ::deserialize(buf)?, - )) - } -} - -impl Deref for PostedMessageData { - type Target = MessageData; - - fn deref(&self) -> &Self::Target { - unsafe { std::mem::transmute(&self.0) } - } -} - -impl DerefMut for PostedMessageData { - fn deref_mut(&mut self) -> &mut Self::Target { - unsafe { std::mem::transmute(&mut self.0) } - } -} - -impl Default for PostedMessageData { - fn default() -> Self { - PostedMessageData(MessageData::default()) - } -} - -impl Clone for PostedMessageData { - fn clone(&self) -> Self { - PostedMessageData(self.0.clone()) - } -} - -#[cfg(not(feature = "cpi"))] -impl Owned for PostedMessageData { - fn owner(&self) -> AccountOwner { - AccountOwner::This - } -} - -#[cfg(feature = "cpi")] -impl Owned for PostedMessageData { - fn owner(&self) -> AccountOwner { - use std::str::FromStr; - AccountOwner::Other(Pubkey::from_str(env!("BRIDGE_ADDRESS")).unwrap()) - } -} - -#[repr(transparent)] -pub struct PostedVAAData(pub MessageData); - -impl BorshSerialize for PostedVAAData { - fn serialize(&self, writer: &mut W) -> std::io::Result<()> { - writer.write(b"vaa")?; - BorshSerialize::serialize(&self.0, writer) - } -} - -impl BorshDeserialize for PostedVAAData { - fn deserialize(buf: &mut &[u8]) -> std::io::Result { - *buf = &buf[3..]; - Ok(PostedVAAData( - ::deserialize(buf)?, - )) - } -} - -impl Deref for PostedVAAData { - type Target = MessageData; - - fn deref(&self) -> &Self::Target { - unsafe { std::mem::transmute(&self.0) } - } -} - -impl DerefMut for PostedVAAData { - fn deref_mut(&mut self) -> &mut Self::Target { - unsafe { std::mem::transmute(&mut self.0) } - } -} - -impl Default for PostedVAAData { - fn default() -> Self { - PostedVAAData(MessageData::default()) - } -} - -impl Clone for PostedVAAData { - fn clone(&self) -> Self { - PostedVAAData(self.0.clone()) - } -} - -#[derive(Default, BorshSerialize, BorshDeserialize, Clone, Serialize, Deserialize)] -pub struct MessageData { - /// Header of the posted VAA - pub vaa_version: u8, - - /// Level of consistency requested by the emitter - pub consistency_level: u8, - - /// Time the vaa was submitted - pub vaa_time: u32, - - /// Account where signatures are stored - pub vaa_signature_account: Pubkey, - - /// Time the posted message was created - pub submission_time: u32, - - /// Unique nonce for this message - pub nonce: u32, - - /// Sequence number of this message - pub sequence: u64, - - /// Emitter of the message - pub emitter_chain: u16, - - /// Emitter of the message - pub emitter_address: [u8; 32], - - /// Message payload - pub payload: Vec, -} - -#[cfg(not(feature = "cpi"))] -impl Owned for PostedVAAData { - fn owner(&self) -> AccountOwner { - AccountOwner::This - } -} - -#[cfg(feature = "cpi")] -impl Owned for PostedVAAData { - fn owner(&self) -> AccountOwner { - use std::str::FromStr; - AccountOwner::Other(Pubkey::from_str(env!("BRIDGE_ADDRESS")).unwrap()) - } -} - -#[derive(Default, Clone, Copy, BorshDeserialize, BorshSerialize)] -pub struct SequenceTracker { - pub sequence: u64, -} - -impl Owned for SequenceTracker { - fn owner(&self) -> AccountOwner { - AccountOwner::This - } -} - -#[derive(Default, Clone, Copy, BorshDeserialize, BorshSerialize, Serialize, Deserialize)] -pub struct ClaimData { - pub claimed: bool, -} - -impl Owned for ClaimData { - fn owner(&self) -> AccountOwner { - AccountOwner::This - } +#[repr(u8)] +#[derive(BorshSerialize, BorshDeserialize, Clone, Serialize, Deserialize)] +pub enum ConsistencyLevel { + Confirmed, + Finalized, } pub struct GovernancePayloadUpgrade { @@ -493,10 +233,3 @@ impl SerializeGovernancePayload for GovernancePayloadTransferFees { impl DeserializeGovernancePayload for GovernancePayloadTransferFees { } - -#[repr(u8)] -#[derive(BorshSerialize, BorshDeserialize, Clone, Serialize, Deserialize)] -pub enum ConsistencyLevel { - Confirmed, - Finalized, -} diff --git a/solana/bridge/program/src/vaa.rs b/solana/bridge/program/src/vaa.rs index 73f03ce8..0f323a46 100644 --- a/solana/bridge/program/src/vaa.rs +++ b/solana/bridge/program/src/vaa.rs @@ -1,8 +1,4 @@ use crate::{ - accounts::{ - Claim, - ClaimDerivationData, - }, api::{ post_vaa::PostVAAData, ForeignAddress, @@ -13,7 +9,9 @@ use crate::{ InvalidGovernanceModule, VAAAlreadyExecuted, }, - types::PostedVAAData, + Claim, + ClaimDerivationData, + PostedVAAData, Result, CHAIN_ID_SOLANA, }; diff --git a/solana/bridge/program/src/wasm.rs b/solana/bridge/program/src/wasm.rs index 93704750..9367f418 100644 --- a/solana/bridge/program/src/wasm.rs +++ b/solana/bridge/program/src/wasm.rs @@ -20,21 +20,16 @@ use std::io::Write; use crate::{ accounts::{ + Bridge, + BridgeData, + FeeCollector, GuardianSet, + GuardianSetData, GuardianSetDerivationData, PostedVAA, + PostedVAAData, PostedVAADerivationData, }, - types::ConsistencyLevel, - PostVAAData, - VerifySignaturesData, -}; - -use crate::{ - accounts::{ - Bridge, - FeeCollector, - }, instructions::{ hash_vaa, post_message, @@ -46,13 +41,13 @@ use crate::{ verify_signatures, }, types::{ - BridgeData, + ConsistencyLevel, GovernancePayloadGuardianSetChange, GovernancePayloadTransferFees, GovernancePayloadUpgrade, - GuardianSetData, - PostedVAAData, }, + PostVAAData, + VerifySignaturesData, }; use byteorder::LittleEndian; use wasm_bindgen::prelude::*; diff --git a/solana/modules/nft_bridge/program/src/accounts.rs b/solana/modules/nft_bridge/program/src/accounts.rs index fcd383c7..9077e58f 100644 --- a/solana/modules/nft_bridge/program/src/accounts.rs +++ b/solana/modules/nft_bridge/program/src/accounts.rs @@ -1,7 +1,7 @@ use crate::types::*; use bridge::{ + accounts::BridgeData, api::ForeignAddress, - types::BridgeData, vaa::{ DeserializePayload, PayloadMessage, diff --git a/solana/modules/nft_bridge/program/src/instructions.rs b/solana/modules/nft_bridge/program/src/instructions.rs index dd0d7afb..523f6feb 100644 --- a/solana/modules/nft_bridge/program/src/instructions.rs +++ b/solana/modules/nft_bridge/program/src/instructions.rs @@ -35,20 +35,18 @@ use borsh::BorshSerialize; use bridge::{ accounts::{ Bridge, + BridgeConfig, Claim, ClaimDerivationData, FeeCollector, PostedVAA, + PostedVAAData, PostedVAADerivationData, Sequence, SequenceDerivationData, }, api::ForeignAddress, instructions::hash_vaa, - types::{ - BridgeConfig, - PostedVAAData, - }, vaa::{ ClaimableVAA, PayloadMessage, diff --git a/solana/modules/token_bridge/program/src/accounts.rs b/solana/modules/token_bridge/program/src/accounts.rs index 807afbe1..d8994b53 100644 --- a/solana/modules/token_bridge/program/src/accounts.rs +++ b/solana/modules/token_bridge/program/src/accounts.rs @@ -1,7 +1,7 @@ use crate::types::*; use bridge::{ + accounts::BridgeData, api::ForeignAddress, - types::BridgeData, vaa::{ DeserializePayload, PayloadMessage, diff --git a/solana/modules/token_bridge/program/src/instructions.rs b/solana/modules/token_bridge/program/src/instructions.rs index 8ac6ed6d..edf78e65 100644 --- a/solana/modules/token_bridge/program/src/instructions.rs +++ b/solana/modules/token_bridge/program/src/instructions.rs @@ -38,20 +38,18 @@ use borsh::BorshSerialize; use bridge::{ accounts::{ Bridge, + BridgeConfig, Claim, ClaimDerivationData, FeeCollector, PostedVAA, + PostedVAAData, PostedVAADerivationData, Sequence, SequenceDerivationData, }, api::ForeignAddress, instructions::hash_vaa, - types::{ - BridgeConfig, - PostedVAAData, - }, vaa::{ ClaimableVAA, PayloadMessage, diff --git a/solana/pyth2wormhole/program/src/attest.rs b/solana/pyth2wormhole/program/src/attest.rs index bed644e8..c0ffb9a6 100644 --- a/solana/pyth2wormhole/program/src/attest.rs +++ b/solana/pyth2wormhole/program/src/attest.rs @@ -22,10 +22,8 @@ use solana_program::{ }; use bridge::{ - types::{ - BridgeData, - ConsistencyLevel, - }, + accounts::BridgeData, + types::ConsistencyLevel, PostMessageData, };