Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion api-demo/src/demo/envoy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ impl Envoy {
loop {
chapter_title("💸 Envoy tells Passport the USD exchange rate.");
let msg = EnvoyMessage::new(
QuantumLinkMessage::ExchangeRate(ExchangeRate::new("USD", 65432.21)),
QuantumLinkMessage::ExchangeRate(ExchangeRate {
currency_code: String::from("USD"),
rate: 65432.21,
timestamp: 0,
}),
12345,
);

Expand Down
29 changes: 15 additions & 14 deletions api/src/api/fx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,22 @@ pub struct ExchangeRate {
pub currency_code: String,
#[n(1)]
pub rate: f32,
#[n(2)]
pub timestamp: u64,
}

impl ExchangeRate {
pub fn new(currency_code: &str, rate: f32) -> Self {
Self {
currency_code: currency_code.to_string(),
rate,
}
}

pub fn currency_code(&self) -> &str {
&self.currency_code
}
#[quantum_link]
pub struct ExchangeRateHistory {
#[n(0)]
pub history: Vec<PricePoint>,
#[n(1)]
pub currency_code: String,
}

pub fn rate(&self) -> f32 {
self.rate
}
#[quantum_link]
pub struct PricePoint {
#[n(0)]
pub rate: f32,
#[n(1)]
pub timestamp: u64,
}
5 changes: 4 additions & 1 deletion api/src/api/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::{
FirmwareFetchEvent, FirmwareFetchRequest, FirmwareUpdateCheckRequest,
FirmwareUpdateCheckResponse, FirmwareUpdateResult,
},
fx::ExchangeRate,
fx::{ExchangeRate, ExchangeRateHistory},
pairing::{PairingRequest, PairingResponse},
scv::SecurityCheck,
status::{DeviceStatus, EnvoyStatus},
Expand Down Expand Up @@ -74,6 +74,9 @@ impl PassportMessage {
pub enum QuantumLinkMessage {
#[n(0)]
ExchangeRate(#[n(0)] ExchangeRate),
#[n(26)]
ExchangeRateHistory(#[n(0)] ExchangeRateHistory),

#[n(1)]
FirmwareUpdateCheckRequest(#[n(0)] FirmwareUpdateCheckRequest),
#[n(2)]
Expand Down
34 changes: 27 additions & 7 deletions api/src/api/quantum_link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,11 @@ mod tests {

#[test]
fn test_encode_decode_quantumlink_message() {
let fx_rate = ExchangeRate::new("USD", 0.85);
let fx_rate = ExchangeRate {
currency_code: String::from("USD"),
rate: 0.85,
timestamp: 0,
};
let original_message = QuantumLinkMessage::ExchangeRate(fx_rate.clone());

// Encode the message
Expand All @@ -271,15 +275,19 @@ mod tests {
};

// Assert that the original and decoded messages are the same
assert_eq!(fx_rate.rate(), fx_rate_decoded.rate());
assert_eq!(fx_rate.rate, fx_rate_decoded.rate);
}

#[test]
fn test_seal_unseal_quantumlink_message() {
let envoy = QuantumLinkIdentity::generate();
let passport = QuantumLinkIdentity::generate();

let fx_rate = ExchangeRate::new("USD", 0.85);
let fx_rate = ExchangeRate {
currency_code: String::from("USD"),
rate: 0.85,
timestamp: 0,
};
let original_message = EnvoyMessage {
message: QuantumLinkMessage::ExchangeRate(fx_rate.clone()),
timestamp: 123456,
Expand All @@ -302,7 +310,7 @@ mod tests {
};

// Assert that the original and decoded messages are the same
assert_eq!(fx_rate.rate(), fx_rate_decoded.rate());
assert_eq!(fx_rate.rate, fx_rate_decoded.rate);
}

#[test]
Expand All @@ -325,7 +333,11 @@ mod tests {
let passport = QuantumLinkIdentity::generate();
let mut arid_cache = ARIDCache::new();

let fx_rate = ExchangeRate::new("USD", 0.85);
let fx_rate = ExchangeRate {
currency_code: String::from("USD"),
rate: 0.85,
timestamp: 0,
};
let original_message = EnvoyMessage {
message: QuantumLinkMessage::ExchangeRate(fx_rate.clone()),
timestamp: 123456,
Expand Down Expand Up @@ -366,7 +378,11 @@ mod tests {
let passport = QuantumLinkIdentity::generate();

// Create and seal multiple messages
let fx_rate = ExchangeRate::new("USD", 0.85);
let fx_rate = ExchangeRate {
currency_code: String::from("USD"),
rate: 0.85,
timestamp: 0,
};
let message1 = EnvoyMessage {
message: QuantumLinkMessage::ExchangeRate(fx_rate.clone()),
timestamp: 123456,
Expand Down Expand Up @@ -439,7 +455,11 @@ fn test_replay_check() {
let envoy = QuantumLinkIdentity::generate();
let passport = QuantumLinkIdentity::generate();

let fx_rate = ExchangeRate::new("USD", 0.85);
let fx_rate = ExchangeRate {
currency_code: String::from("USD"),
rate: 0.85,
timestamp: 0,
};
let message = EnvoyMessage {
message: QuantumLinkMessage::ExchangeRate(fx_rate),
timestamp: 123456,
Expand Down