templar_common/oracle/redstone/config/
mod.rs

1use near_sdk::near;
2use redstone::{ConfigFactory, SignerAddress, TimestampMillis};
3
4mod config_prod;
5pub use config_prod::prod;
6mod config_test;
7pub use config_test::test;
8
9pub type SignerAddressBs = [u8; 20];
10
11#[derive(Debug, Clone, PartialEq, Eq)]
12#[near(serializers = [borsh, json])]
13pub struct Config {
14    pub signer_count_threshold: u8,
15    pub signers: Vec<SignerAddressBs>,
16    pub max_timestamp_delay_ms: u64,
17    pub max_timestamp_ahead_ms: u64,
18    pub min_interval_between_updates_ms: u64,
19}
20
21#[derive(Debug, thiserror::Error)]
22pub enum ConfigValidationError {
23    #[error("signer_count_threshold must be greater than zero")]
24    ZeroSignerThreshold,
25    #[error(transparent)]
26    InvalidSigners(#[from] redstone::network::error::Error),
27}
28
29impl Config {
30    pub fn validate(&self) -> Result<(), ConfigValidationError> {
31        if self.signer_count_threshold == 0 {
32            return Err(ConfigValidationError::ZeroSignerThreshold);
33        }
34        redstone::contract::verification::verify_signers_config(
35            &self.redstone_signers(),
36            self.signer_count_threshold,
37        )?;
38        Ok(())
39    }
40}
41
42#[cfg(test)]
43mod tests;
44
45pub const DATA_STALENESS: TimestampMillis = TimestampMillis::from_millis(30 * 60 * 60 * 1000);
46
47pub const FEED_TTL_SECS: u32 = 2 * 24 * 60 * 60;
48pub const FEED_TTL_THRESHOLD: u32 = FEED_TTL_SECS / 5;
49pub const FEED_TTL_EXTEND_TO: u32 = FEED_TTL_SECS * 3 / 10;
50
51pub struct NearCrypto;
52
53impl redstone::Crypto for NearCrypto {
54    type KeccakOutput = [u8; 32];
55
56    fn keccak256(&mut self, input: impl AsRef<[u8]>) -> Self::KeccakOutput {
57        near_sdk::env::keccak256_array(input.as_ref())
58    }
59
60    fn recover_public_key(
61        &mut self,
62        recovery_byte: u8,
63        signature_bytes: impl AsRef<[u8]>,
64        message_hash: Self::KeccakOutput,
65    ) -> Result<redstone::Bytes, redstone::CryptoError> {
66        use k256::ecdsa::{RecoveryId, Signature, VerifyingKey};
67
68        let signature_bytes = signature_bytes.as_ref();
69        let signature = Signature::try_from(signature_bytes)
70            .map_err(|_| redstone::CryptoError::Signature(signature_bytes.to_vec()))?;
71        let r_id = RecoveryId::try_from(recovery_byte)
72            .map_err(|_| redstone::CryptoError::RecoveryByte(recovery_byte))?;
73        let result = VerifyingKey::recover_from_prehash(&message_hash, &signature, r_id)
74            .map_err(|_| redstone::CryptoError::RecoverPreHash)?;
75
76        Ok(result.to_encoded_point(false).to_bytes().to_vec().into())
77    }
78}
79
80impl ConfigFactory<(), NearCrypto> for Config {
81    fn signer_count_threshold(&self) -> u8 {
82        self.signer_count_threshold
83    }
84
85    fn redstone_signers(&self) -> Vec<SignerAddress> {
86        self.signers.iter().map(|s| s.to_vec().into()).collect()
87    }
88
89    fn max_timestamp_delay_ms(&self) -> u64 {
90        self.max_timestamp_delay_ms
91    }
92
93    fn max_timestamp_ahead_ms(&self) -> u64 {
94        self.max_timestamp_ahead_ms
95    }
96
97    fn make_crypto((): ()) -> NearCrypto {
98        NearCrypto
99    }
100}