Skip to main content

molpha_verifier/
payload.rs

1//! Plain `DataUpdate` payload struct.
2//!
3//! Field order and types match the on-chain `SubmitDataUpdateArgs` instruction argument so a
4//! mechanical field copy converts between the two. With the `borsh` feature enabled, `value`
5//! serializes as standard Borsh `Vec<u8>` (u32 little-endian length prefix + bytes), matching
6//! Anchor `Vec<u8>` on-chain. The crate does not rely on that wire layout for verification.
7
8/// A signed Molpha data update.
9#[derive(Clone, Debug, PartialEq, Eq)]
10#[cfg_attr(
11    feature = "borsh",
12    derive(borsh::BorshSerialize, borsh::BorshDeserialize)
13)]
14pub struct DataUpdate {
15    pub feed_id: [u8; 32],
16    pub registry_version: u32,
17    /// Arbitrary-length payload bytes (hashed as-is into the EVM message).
18    pub value: Vec<u8>,
19    pub canonical_timestamp: i64,
20    pub signatures_required: u8,
21
22    /// Aggregate Schnorr signature scalar `s`.
23    pub agg_sig_s: [u8; 32],
24    /// Ethereum-style commitment address (20 bytes).
25    pub commitment_addr: [u8; 20],
26    /// EVM `uint256` bitmap (big-endian) of which nodes signed.
27    pub signers_bitmap: [u8; 32],
28}
29
30#[cfg(all(test, feature = "borsh"))]
31mod tests {
32    use super::*;
33    use borsh::{BorshDeserialize, BorshSerialize};
34
35    /// Same 32-byte padded "solana-compat-val" as the EVM-compat fixture (hashed as-is).
36    const FIXTURE_VALUE: [u8; 32] = [
37        0x73, 0x6f, 0x6c, 0x61, 0x6e, 0x61, 0x2d, 0x63, 0x6f, 0x6d, 0x70, 0x61, 0x74, 0x2d, 0x76,
38        0x61, 0x6c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
39        0x00, 0x00,
40    ];
41
42    /// 165-byte borsh encoding used by `examples/verify_data_update.rs`.
43    const FIXTURE_BORSH: [u8; 165] = [
44        0x73, 0x6f, 0x6c, 0x61, 0x6e, 0x61, 0x2d, 0x63, 0x6f, 0x6d, 0x70, 0x61, 0x74, 0x2d, 0x6a,
45        0x6f, 0x62, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
46        0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x73, 0x6f, 0x6c, 0x61, 0x6e,
47        0x61, 0x2d, 0x63, 0x6f, 0x6d, 0x70, 0x61, 0x74, 0x2d, 0x76, 0x61, 0x6c, 0x00, 0x00, 0x00,
48        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7b, 0xf1, 0x53,
49        0x65, 0x00, 0x00, 0x00, 0x00, 0x08, 0xc7, 0xe0, 0x99, 0x60, 0x3c, 0xee, 0xd2, 0xa1, 0x13,
50        0xd7, 0x5a, 0x9d, 0x95, 0xe2, 0x0f, 0x92, 0x00, 0x6b, 0x06, 0xc5, 0x49, 0x7a, 0xdd, 0x09,
51        0x81, 0x7d, 0xa8, 0x90, 0x8d, 0x39, 0x0d, 0xa5, 0xc6, 0xb9, 0x4f, 0xea, 0x5d, 0xd5, 0xf9,
52        0x65, 0xd8, 0x67, 0x14, 0xb1, 0xd9, 0x9d, 0xcf, 0xaf, 0x1e, 0x72, 0xee, 0x35, 0x00, 0x00,
53        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
54        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff,
55    ];
56
57    #[test]
58    fn fixture_borsh_roundtrip() {
59        let decoded = DataUpdate::try_from_slice(&FIXTURE_BORSH).expect("decode");
60        assert_eq!(decoded.value.as_slice(), FIXTURE_VALUE.as_slice());
61        assert_eq!(decoded.registry_version, 1);
62        assert_eq!(decoded.signatures_required, 8);
63        assert_eq!(decoded.canonical_timestamp, 1_700_000_123);
64
65        let encoded = decoded.try_to_vec().expect("encode");
66        assert_eq!(encoded.as_slice(), FIXTURE_BORSH.as_slice());
67    }
68}