message.rs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. //! Index-based PDA for storing unreliable wormhole message
  2. //!
  3. //! The main goal of this PDA is to take advantage of wormhole message
  4. //! reuse securely. This is achieved by tying the account derivation
  5. //! data to the payer account of the attest() instruction. Inside
  6. //! attest(), payer must be a signer, and the message account must be
  7. //! derived with their address as message_owner in
  8. //! `P2WMessageDrvData`.
  9. use borsh::{
  10. BorshDeserialize,
  11. BorshSerialize,
  12. };
  13. use bridge::PostedMessageUnreliable;
  14. use solana_program::pubkey::Pubkey;
  15. use solitaire::{
  16. processors::seeded::Seeded,
  17. AccountState,
  18. Data,
  19. Info,
  20. Mut,
  21. Signer,
  22. };
  23. pub type P2WMessage<'a> = Mut<PostedMessageUnreliable<'a, { AccountState::MaybeInitialized }>>;
  24. #[derive(BorshDeserialize, BorshSerialize)]
  25. pub struct P2WMessageDrvData {
  26. /// The key owning this message account
  27. pub message_owner: Pubkey,
  28. /// Size of the batch. It is important that all messages have the same size
  29. ///
  30. /// NOTE: 2022-09-05
  31. /// Currently wormhole does not resize accounts if they have different
  32. /// payload sizes; this (along with versioning the seed literal below) is
  33. /// a workaround to have different PDAs for different batch sizes.
  34. pub batch_size: u16,
  35. /// Index for keeping many accounts per owner
  36. pub id: u64,
  37. }
  38. impl<'a> Seeded<&P2WMessageDrvData> for P2WMessage<'a> {
  39. fn seeds(data: &P2WMessageDrvData) -> Vec<Vec<u8>> {
  40. vec![
  41. // See the note at 2022-09-05 above.
  42. // Change the version in the literal whenever you change the
  43. // price attestation data.
  44. "p2w-message-v1".as_bytes().to_vec(),
  45. data.message_owner.to_bytes().to_vec(),
  46. data.batch_size.to_be_bytes().to_vec(),
  47. data.id.to_be_bytes().to_vec(),
  48. ]
  49. }
  50. }