templar_common/oracle/redstone/config/
mod.rs1use 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)]
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
21pub const DATA_STALENESS: TimestampMillis = TimestampMillis::from_millis(30 * 60 * 60 * 1000);
22
23pub const FEED_TTL_SECS: u32 = 2 * 24 * 60 * 60;
24pub const FEED_TTL_THRESHOLD: u32 = FEED_TTL_SECS / 5;
25pub const FEED_TTL_EXTEND_TO: u32 = FEED_TTL_SECS * 3 / 10;
26
27pub struct NearCrypto;
28
29impl redstone::Crypto for NearCrypto {
30 type KeccakOutput = [u8; 32];
31
32 fn keccak256(&mut self, input: impl AsRef<[u8]>) -> Self::KeccakOutput {
33 near_sdk::env::keccak256_array(input.as_ref())
34 }
35
36 fn recover_public_key(
37 &mut self,
38 recovery_byte: u8,
39 signature_bytes: impl AsRef<[u8]>,
40 message_hash: Self::KeccakOutput,
41 ) -> Result<redstone::Bytes, redstone::CryptoError> {
42 use k256::ecdsa::{RecoveryId, Signature, VerifyingKey};
43
44 let signature_bytes = signature_bytes.as_ref();
45 let signature = Signature::try_from(signature_bytes)
46 .map_err(|_| redstone::CryptoError::Signature(signature_bytes.to_vec()))?;
47 let r_id = RecoveryId::try_from(recovery_byte)
48 .map_err(|_| redstone::CryptoError::RecoveryByte(recovery_byte))?;
49 let result = VerifyingKey::recover_from_prehash(&message_hash, &signature, r_id)
50 .map_err(|_| redstone::CryptoError::RecoverPreHash)?;
51
52 Ok(result.to_encoded_point(false).to_bytes().to_vec().into())
53 }
54}
55
56impl ConfigFactory<(), NearCrypto> for Config {
57 fn signer_count_threshold(&self) -> u8 {
58 self.signer_count_threshold
59 }
60
61 fn redstone_signers(&self) -> Vec<SignerAddress> {
62 self.signers.iter().map(|s| s.to_vec().into()).collect()
63 }
64
65 fn max_timestamp_delay_ms(&self) -> u64 {
66 self.max_timestamp_delay_ms
67 }
68
69 fn max_timestamp_ahead_ms(&self) -> u64 {
70 self.max_timestamp_ahead_ms
71 }
72
73 fn make_crypto((): ()) -> NearCrypto {
74 NearCrypto
75 }
76}