molpha-verifier 0.3.0-dev.1

Solana program library for verifying Molpha threshold-signed oracle updates
Documentation
//! Plain `DataUpdate` payload struct.
//!
//! Field order and types match the on-chain `SubmitDataUpdateArgs` instruction argument so a
//! mechanical field copy converts between the two. With the `borsh` feature enabled, `value`
//! serializes as standard Borsh `Vec<u8>` (u32 little-endian length prefix + bytes), matching
//! Anchor `Vec<u8>` on-chain. The crate does not rely on that wire layout for verification.

/// A signed Molpha data update.
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(
    feature = "borsh",
    derive(borsh::BorshSerialize, borsh::BorshDeserialize)
)]
pub struct DataUpdate {
    pub source_id: [u8; 32],
    /// Arbitrary-length payload bytes (hashed as-is into the EVM message).
    pub value: Vec<u8>,
    pub canonical_timestamp: i64,
    pub registry_version: u32,
    pub signatures_required: u8,
}

#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(
    feature = "borsh",
    derive(borsh::BorshSerialize, borsh::BorshDeserialize)
)]
pub struct SchnorrSignature {
    pub agg_sig_s: [u8; 32],
    pub commitment_addr: [u8; 20],
    pub signers_bitmap: [u8; 32],
}

#[cfg(all(test, feature = "borsh"))]
mod tests {
    use super::*;
    use borsh::{BorshDeserialize, BorshSerialize};

    /// Same 32-byte padded "solana-compat-val" as the EVM-compat fixture (hashed as-is).
    const FIXTURE_VALUE: [u8; 32] = [
        0x73, 0x6f, 0x6c, 0x61, 0x6e, 0x61, 0x2d, 0x63, 0x6f, 0x6d, 0x70, 0x61, 0x74, 0x2d, 0x76,
        0x61, 0x6c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00,
    ];

    /// 81-byte borsh encoding used by `examples/verify_data_update.rs`.
    const FIXTURE_PAYLOAD_BORSH: [u8; 81] = [
        0x73, 0x6f, 0x6c, 0x61, 0x6e, 0x61, 0x2d, 0x63, 0x6f, 0x6d, 0x70, 0x61, 0x74, 0x2d, 0x6a,
        0x6f, 0x62, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x73, 0x6f, 0x6c, 0x61, 0x6e, 0x61, 0x2d, 0x63, 0x6f,
        0x6d, 0x70, 0x61, 0x74, 0x2d, 0x76, 0x61, 0x6c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7b, 0xf1, 0x53, 0x65, 0x00, 0x00, 0x00,
        0x00, 0x01, 0x00, 0x00, 0x00, 0x08,
    ];

    /// 84-byte borsh encoding used by `examples/verify_data_update.rs`.
    const FIXTURE_SIGNATURE_BORSH: [u8; 84] = [
        0xc7, 0xe0, 0x99, 0x60, 0x3c, 0xee, 0xd2, 0xa1, 0x13, 0xd7, 0x5a, 0x9d, 0x95, 0xe2, 0x0f,
        0x92, 0x00, 0x6b, 0x06, 0xc5, 0x49, 0x7a, 0xdd, 0x09, 0x81, 0x7d, 0xa8, 0x90, 0x8d, 0x39,
        0x0d, 0xa5, 0xc6, 0xb9, 0x4f, 0xea, 0x5d, 0xd5, 0xf9, 0x65, 0xd8, 0x67, 0x14, 0xb1, 0xd9,
        0x9d, 0xcf, 0xaf, 0x1e, 0x72, 0xee, 0x35, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff,
    ];

    #[test]
    fn fixture_payload_borsh_roundtrip() {
        let decoded = DataUpdate::try_from_slice(&FIXTURE_PAYLOAD_BORSH).expect("decode payload");
        assert_eq!(decoded.value.as_slice(), FIXTURE_VALUE.as_slice());
        assert_eq!(decoded.registry_version, 1);
        assert_eq!(decoded.signatures_required, 8);
        assert_eq!(decoded.canonical_timestamp, 1_700_000_123);

        let encoded = decoded.try_to_vec().expect("encode payload");
        assert_eq!(encoded.as_slice(), FIXTURE_PAYLOAD_BORSH.as_slice());
    }

    #[test]
    fn fixture_signature_borsh_roundtrip() {
        let decoded =
            SchnorrSignature::try_from_slice(&FIXTURE_SIGNATURE_BORSH).expect("decode signature");
        assert_eq!(decoded.signers_bitmap[31], 0xff);

        let encoded = decoded.try_to_vec().expect("encode signature");
        assert_eq!(encoded.as_slice(), FIXTURE_SIGNATURE_BORSH.as_slice());
    }
}