Skip to main content

molpha_verifier/
payload.rs

1//! Plain attestation payload and signature structs.
2//!
3//! Field order and types match the on-chain attestation instruction arguments so a mechanical
4//! field copy converts between the two. With the `borsh` feature enabled, structs derive
5//! `BorshSerialize` / `BorshDeserialize` for wire-format decode/encode.
6
7/// Signed oracle attestation payload.
8#[derive(Clone, Debug, PartialEq, Eq)]
9#[cfg_attr(
10    feature = "borsh",
11    derive(borsh::BorshSerialize, borsh::BorshDeserialize)
12)]
13pub struct AttestationPayload {
14    pub value: [u8; 32],
15    pub source_id: [u8; 32],
16    pub registry_version: u32,
17    pub signatures_required: u32,
18    pub canonical_timestamp: u64,
19}
20
21#[derive(Clone, Debug, PartialEq, Eq)]
22#[cfg_attr(
23    feature = "borsh",
24    derive(borsh::BorshSerialize, borsh::BorshDeserialize)
25)]
26pub struct SchnorrSignature {
27    pub agg_sig_s: [u8; 32],
28    pub commitment_addr: [u8; 20],
29    pub signers_bitmap: [u8; 32],
30}
31
32/// A signed attestation: payload plus aggregate Schnorr signature.
33#[derive(Clone, Debug, PartialEq, Eq)]
34#[cfg_attr(
35    feature = "borsh",
36    derive(borsh::BorshSerialize, borsh::BorshDeserialize)
37)]
38pub struct Attestation {
39    pub payload: AttestationPayload,
40    pub signature: SchnorrSignature,
41}
42
43#[cfg(all(test, feature = "borsh"))]
44mod tests {
45    use super::*;
46    use borsh::{BorshDeserialize, BorshSerialize};
47
48    /// Fixture `value` bytes (hashed as-is in `compute_message_hash`).
49    const FIXTURE_VALUE: [u8; 32] = [
50        0xe1, 0xcd, 0x5b, 0x4f, 0x67, 0xac, 0xdc, 0x78, 0x68, 0xc3, 0xb1, 0x5f, 0x7b, 0x6c, 0xc2,
51        0xdc, 0x27, 0x70, 0x54, 0x53, 0x71, 0x34, 0x2c, 0xab, 0x76, 0x62, 0x71, 0xbb, 0x3f, 0xd5,
52        0xe7, 0x34,
53    ];
54
55    /// 80-byte borsh encoding used by `examples/verify_attestation.rs`.
56    const FIXTURE_PAYLOAD_BORSH: [u8; 80] = [
57        0xe1, 0xcd, 0x5b, 0x4f, 0x67, 0xac, 0xdc, 0x78, 0x68, 0xc3, 0xb1, 0x5f, 0x7b, 0x6c, 0xc2,
58        0xdc, 0x27, 0x70, 0x54, 0x53, 0x71, 0x34, 0x2c, 0xab, 0x76, 0x62, 0x71, 0xbb, 0x3f, 0xd5,
59        0xe7, 0x34, 0x0b, 0x0c, 0x5c, 0x4a, 0x0e, 0x67, 0x58, 0x69, 0xda, 0xc2, 0x27, 0x2a, 0x40,
60        0x04, 0x63, 0x65, 0xa2, 0x9c, 0x8a, 0xe7, 0x63, 0x5e, 0x52, 0xc4, 0x94, 0xd8, 0x40, 0xda,
61        0x2e, 0xc8, 0x26, 0xcb, 0x0c, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x7c, 0x06, 0xd6,
62        0x65, 0x00, 0x00, 0x00, 0x00,
63    ];
64
65    /// 84-byte borsh encoding used by `examples/verify_attestation.rs`.
66    const FIXTURE_SIGNATURE_BORSH: [u8; 84] = [
67        0x1b, 0x8d, 0xd2, 0x78, 0xb3, 0x67, 0xb3, 0x4d, 0x4e, 0xce, 0x69, 0xb8, 0x8c, 0x28, 0xff,
68        0x13, 0x01, 0xb6, 0x72, 0x51, 0xfc, 0x3d, 0x79, 0x26, 0xac, 0xb5, 0x25, 0xd1, 0x1f, 0xd3,
69        0x17, 0x1d, 0x51, 0xbe, 0x44, 0x69, 0x33, 0x1a, 0x9e, 0xb3, 0xed, 0x48, 0xb1, 0xd4, 0xe1,
70        0x1e, 0xc9, 0xa0, 0xa5, 0x95, 0x2d, 0xf4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
71        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
72        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0x1d,
73    ];
74
75    #[test]
76    fn fixture_payload_borsh_roundtrip() {
77        let decoded =
78            AttestationPayload::try_from_slice(&FIXTURE_PAYLOAD_BORSH).expect("decode payload");
79        assert_eq!(decoded.value, FIXTURE_VALUE);
80        assert_eq!(decoded.registry_version, 12);
81        assert_eq!(decoded.signatures_required, 5);
82        assert_eq!(decoded.canonical_timestamp, 1_708_525_180);
83
84        let encoded = decoded.try_to_vec().expect("encode payload");
85        assert_eq!(encoded.as_slice(), FIXTURE_PAYLOAD_BORSH.as_slice());
86    }
87
88    #[test]
89    fn fixture_signature_borsh_roundtrip() {
90        let decoded =
91            SchnorrSignature::try_from_slice(&FIXTURE_SIGNATURE_BORSH).expect("decode signature");
92        assert_eq!(decoded.signers_bitmap[30], 0x0e);
93        assert_eq!(decoded.signers_bitmap[31], 0x1d);
94
95        let encoded = decoded.try_to_vec().expect("encode signature");
96        assert_eq!(encoded.as_slice(), FIXTURE_SIGNATURE_BORSH.as_slice());
97    }
98
99    #[test]
100    fn fixture_attestation_borsh_roundtrip() {
101        let payload =
102            AttestationPayload::try_from_slice(&FIXTURE_PAYLOAD_BORSH).expect("decode payload");
103        let signature =
104            SchnorrSignature::try_from_slice(&FIXTURE_SIGNATURE_BORSH).expect("decode signature");
105        let attestation = Attestation { payload, signature };
106
107        let encoded = attestation.try_to_vec().expect("encode attestation");
108        let decoded = Attestation::try_from_slice(&encoded).expect("decode attestation");
109        assert_eq!(decoded, attestation);
110    }
111}