use crate::{MainPubkey, NanoTokens, Transfer};
use libp2p::{identity::PublicKey, PeerId};
use serde::{Deserialize, Serialize};
use std::time::SystemTime;
use xor_name::XorName;
pub const QUOTE_EXPIRATION_SECS: u64 = 3600;
const LIVE_TIME_MARGIN: u64 = 10;
#[derive(Clone, Serialize, Deserialize, Eq, PartialEq, custom_debug::Debug)]
pub struct Payment {
#[debug(skip)]
pub transfers: Vec<Transfer>,
pub quote: PaymentQuote,
}
#[derive(Clone, Serialize, Deserialize)]
pub struct PaymentDetails {
pub recipient: MainPubkey,
pub peer_id_bytes: Vec<u8>,
pub transfer: (Transfer, NanoTokens),
pub royalties: (Transfer, NanoTokens),
pub quote: PaymentQuote,
}
impl PaymentDetails {
pub fn to_payment(&self) -> Payment {
Payment {
transfers: vec![self.transfer.0.clone(), self.royalties.0.clone()],
quote: self.quote.clone(),
}
}
}
pub type QuoteSignature = Vec<u8>;
#[derive(
Clone, Eq, PartialEq, PartialOrd, Ord, Hash, Serialize, Deserialize, custom_debug::Debug,
)]
pub struct QuotingMetrics {
pub close_records_stored: usize,
pub max_records: usize,
pub received_payment_count: usize,
pub live_time: u64,
}
impl QuotingMetrics {
pub fn new() -> Self {
Self {
close_records_stored: 0,
max_records: 0,
received_payment_count: 0,
live_time: 0,
}
}
}
impl Default for QuotingMetrics {
fn default() -> Self {
Self::new()
}
}
#[derive(
Clone, Eq, PartialEq, PartialOrd, Ord, Hash, Serialize, Deserialize, custom_debug::Debug,
)]
pub struct PaymentQuote {
pub content: XorName,
pub cost: NanoTokens,
pub timestamp: SystemTime,
pub quoting_metrics: QuotingMetrics,
pub owner: String,
#[debug(skip)]
pub pub_key: Vec<u8>,
#[debug(skip)]
pub signature: QuoteSignature,
}
impl PaymentQuote {
pub fn zero() -> Self {
Self {
content: Default::default(),
cost: NanoTokens::zero(),
timestamp: SystemTime::now(),
quoting_metrics: Default::default(),
owner: Default::default(),
pub_key: vec![],
signature: vec![],
}
}
pub fn bytes_for_signing(
xorname: XorName,
cost: NanoTokens,
timestamp: SystemTime,
quoting_metrics: &QuotingMetrics,
owner: String,
) -> Vec<u8> {
let mut bytes = xorname.to_vec();
bytes.extend_from_slice(&cost.to_bytes());
bytes.extend_from_slice(
×tamp
.duration_since(SystemTime::UNIX_EPOCH)
.expect("Unix epoch to be in the past")
.as_secs()
.to_le_bytes(),
);
let serialised_quoting_metrics = match rmp_serde::to_vec(quoting_metrics) {
Ok(quoting_metrics_vec) => quoting_metrics_vec,
Err(_err) => vec![],
};
bytes.extend_from_slice(&serialised_quoting_metrics);
bytes.extend_from_slice(&owner.into_bytes());
bytes
}
pub fn check_is_signed_by_claimed_peer(&self, claimed_peer: PeerId) -> bool {
let pub_key = if let Ok(pub_key) = PublicKey::try_decode_protobuf(&self.pub_key) {
pub_key
} else {
error!("Cann't parse PublicKey from protobuf");
return false;
};
let self_peer_id = PeerId::from(pub_key.clone());
if self_peer_id != claimed_peer {
error!("This quote {self:?} of {self_peer_id:?} is not signed by {claimed_peer:?}");
return false;
}
let bytes = Self::bytes_for_signing(
self.content,
self.cost,
self.timestamp,
&self.quoting_metrics,
self.owner.clone(),
);
if !pub_key.verify(&bytes, &self.signature) {
error!("Signature is not signed by claimed pub_key");
return false;
}
true
}
pub fn has_expired(&self) -> bool {
let now = std::time::SystemTime::now();
let dur_s = match now.duration_since(self.timestamp) {
Ok(dur) => dur.as_secs(),
Err(_) => return true,
};
dur_s > QUOTE_EXPIRATION_SECS
}
pub fn test_dummy(xorname: XorName, cost: NanoTokens) -> Self {
Self {
content: xorname,
cost,
timestamp: SystemTime::now(),
quoting_metrics: Default::default(),
owner: Default::default(),
pub_key: vec![],
signature: vec![],
}
}
pub fn is_newer_than(&self, other: &Self) -> bool {
self.timestamp > other.timestamp
}
pub fn historical_verify(&self, other: &Self) -> bool {
let self_is_newer = self.is_newer_than(other);
let (old_quote, new_quote) = if self_is_newer {
(other, self)
} else {
(self, other)
};
if new_quote.quoting_metrics.live_time < old_quote.quoting_metrics.live_time {
info!("Claimed live_time out of sequence");
return false;
}
let old_elapsed = if let Ok(elapsed) = old_quote.timestamp.elapsed() {
elapsed
} else {
info!("timestamp failure");
return false;
};
let new_elapsed = if let Ok(elapsed) = new_quote.timestamp.elapsed() {
elapsed
} else {
info!("timestamp failure");
return false;
};
let time_diff = old_elapsed.as_secs().saturating_sub(new_elapsed.as_secs());
let live_time_diff =
new_quote.quoting_metrics.live_time - old_quote.quoting_metrics.live_time;
if live_time_diff > time_diff + LIVE_TIME_MARGIN {
info!("claimed live_time out of sync with the timestamp");
return false;
}
debug!(
"The new quote has {} close records stored, meanwhile old one has {}.",
new_quote.quoting_metrics.close_records_stored,
old_quote.quoting_metrics.close_records_stored
);
if new_quote.quoting_metrics.received_payment_count
< old_quote.quoting_metrics.received_payment_count
{
info!("claimed received_payment_count out of sequence");
return false;
}
true
}
}
#[cfg(test)]
mod tests {
use super::*;
use libp2p::identity::Keypair;
use std::{thread::sleep, time::Duration};
#[test]
fn test_is_newer_than() {
let old_quote = PaymentQuote::zero();
sleep(Duration::from_millis(100));
let new_quote = PaymentQuote::zero();
assert!(new_quote.is_newer_than(&old_quote));
assert!(!old_quote.is_newer_than(&new_quote));
}
#[test]
fn test_is_signed_by_claimed_peer() {
let keypair = Keypair::generate_ed25519();
let peer_id = keypair.public().to_peer_id();
let false_peer = PeerId::random();
let mut quote = PaymentQuote::zero();
let bytes = PaymentQuote::bytes_for_signing(
quote.content,
quote.cost,
quote.timestamp,
"e.quoting_metrics,
quote.owner.clone(),
);
let signature = if let Ok(sig) = keypair.sign(&bytes) {
sig
} else {
panic!("Cannot sign the quote!");
};
assert!(!quote.check_is_signed_by_claimed_peer(peer_id));
assert!(!quote.check_is_signed_by_claimed_peer(false_peer));
quote.pub_key = keypair.public().encode_protobuf();
assert!(!quote.check_is_signed_by_claimed_peer(peer_id));
assert!(!quote.check_is_signed_by_claimed_peer(false_peer));
quote.signature = signature;
assert!(quote.check_is_signed_by_claimed_peer(peer_id));
assert!(!quote.check_is_signed_by_claimed_peer(false_peer));
quote.pub_key = Keypair::generate_ed25519().public().encode_protobuf();
assert!(!quote.check_is_signed_by_claimed_peer(peer_id));
assert!(!quote.check_is_signed_by_claimed_peer(false_peer));
}
#[test]
fn test_historical_verify() {
let mut old_quote = PaymentQuote::zero();
sleep(Duration::from_millis(100));
let mut new_quote = PaymentQuote::zero();
assert!(new_quote.historical_verify(&old_quote));
assert!(old_quote.historical_verify(&new_quote));
old_quote.quoting_metrics.received_payment_count = 10;
new_quote.quoting_metrics.received_payment_count = 9;
assert!(!new_quote.historical_verify(&old_quote));
assert!(!old_quote.historical_verify(&new_quote));
new_quote.quoting_metrics.received_payment_count = 11;
assert!(new_quote.historical_verify(&old_quote));
assert!(old_quote.historical_verify(&new_quote));
new_quote.quoting_metrics.live_time = 10;
old_quote.quoting_metrics.live_time = 11;
assert!(!new_quote.historical_verify(&old_quote));
assert!(!old_quote.historical_verify(&new_quote));
new_quote.quoting_metrics.live_time = 11 + LIVE_TIME_MARGIN + 1;
assert!(!new_quote.historical_verify(&old_quote));
assert!(!old_quote.historical_verify(&new_quote));
new_quote.quoting_metrics.live_time = 11 + LIVE_TIME_MARGIN - 1;
assert!(new_quote.historical_verify(&old_quote));
assert!(old_quote.historical_verify(&new_quote));
}
}