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: u8,
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 crate::fixtures::{PAYLOAD_BORSH, SIGNATURE_BORSH, VALUE};
47    use borsh::{BorshDeserialize, BorshSerialize};
48
49    #[test]
50    fn fixture_payload_borsh_roundtrip() {
51        let decoded = AttestationPayload::try_from_slice(&PAYLOAD_BORSH).expect("decode payload");
52        assert_eq!(decoded.value, VALUE);
53        assert_eq!(decoded.registry_version, 12);
54        assert_eq!(decoded.signatures_required, 5);
55        assert_eq!(decoded.canonical_timestamp, 1_705_257_421);
56
57        let encoded = decoded.try_to_vec().expect("encode payload");
58        assert_eq!(encoded.as_slice(), PAYLOAD_BORSH.as_slice());
59    }
60
61    #[test]
62    fn fixture_signature_borsh_roundtrip() {
63        let decoded = SchnorrSignature::try_from_slice(&SIGNATURE_BORSH).expect("decode signature");
64        assert_eq!(decoded.signers_bitmap[30], 0x0f);
65        assert_eq!(decoded.signers_bitmap[31], 0xa8);
66
67        let encoded = decoded.try_to_vec().expect("encode signature");
68        assert_eq!(encoded.as_slice(), SIGNATURE_BORSH.as_slice());
69    }
70
71    #[test]
72    fn fixture_attestation_borsh_roundtrip() {
73        let payload = AttestationPayload::try_from_slice(&PAYLOAD_BORSH).expect("decode payload");
74        let signature =
75            SchnorrSignature::try_from_slice(&SIGNATURE_BORSH).expect("decode signature");
76        let attestation = Attestation { payload, signature };
77
78        let encoded = attestation.try_to_vec().expect("encode attestation");
79        let decoded = Attestation::try_from_slice(&encoded).expect("decode attestation");
80        assert_eq!(decoded, attestation);
81    }
82}