use super::{AuthorisationKind, Type};
use crate::{Coins, Error, PublicKey, Response, Result, Signature, TransactionId, XorName};
use serde::{Deserialize, Serialize};
use std::{borrow::Cow, fmt};
pub const MAX_LOGIN_PACKET_BYTES: usize = 1024 * 1024;
#[allow(clippy::large_enum_variant)]
#[derive(Hash, Eq, PartialEq, PartialOrd, Ord, Clone, Serialize, Deserialize)]
pub enum LoginPacketRequest {
Create(LoginPacket),
CreateFor {
new_owner: PublicKey,
amount: Coins,
transaction_id: TransactionId,
new_login_packet: LoginPacket,
},
Update(LoginPacket),
Get(XorName),
}
impl LoginPacketRequest {
pub fn get_type(&self) -> Type {
use LoginPacketRequest::*;
match *self {
Get(..) => Type::PrivateGet,
CreateFor { .. } => Type::Transaction,
Create { .. } | Update { .. } => Type::Mutation,
}
}
pub fn error_response(&self, error: Error) -> Response {
use LoginPacketRequest::*;
match *self {
Get(..) => Response::GetLoginPacket(Err(error)),
CreateFor { .. } => Response::Transaction(Err(error)),
Create { .. } | Update { .. } => Response::Mutation(Err(error)),
}
}
pub fn authorisation_kind(&self) -> AuthorisationKind {
use LoginPacketRequest::*;
match *self {
Create { .. } | Update { .. } => AuthorisationKind::Mutation,
CreateFor { amount, .. } => {
if amount.as_nano() == 0 {
AuthorisationKind::Mutation
} else {
AuthorisationKind::MutAndTransferCoins
}
}
Get(_) => AuthorisationKind::GetPriv,
}
}
pub fn dest_address(&self) -> Option<Cow<XorName>> {
use LoginPacketRequest::*;
match self {
Create(login_packet) => Some(Cow::Borrowed(login_packet.destination())),
CreateFor {
new_login_packet, ..
} => Some(Cow::Borrowed(new_login_packet.destination())),
Update(login_packet) => Some(Cow::Borrowed(login_packet.destination())),
Get(ref name) => Some(Cow::Borrowed(name)),
}
}
}
impl fmt::Debug for LoginPacketRequest {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
use LoginPacketRequest::*;
write!(
formatter,
"Request::{}",
match *self {
Create { .. } => "CreateLoginPacket",
CreateFor { .. } => "CreateLoginPacketFor",
Update { .. } => "UpdateLoginPacket",
Get(..) => "GetLoginPacket",
}
)
}
}
#[derive(Hash, Eq, PartialEq, PartialOrd, Ord, Clone, Serialize, Deserialize)]
pub struct LoginPacket {
destination: XorName,
authorised_getter: PublicKey, data: Vec<u8>,
signature: Signature,
}
impl LoginPacket {
pub fn new(
destination: XorName,
authorised_getter: PublicKey,
data: Vec<u8>,
signature: Signature,
) -> Result<Self> {
let login_packet_data = Self {
destination,
authorised_getter,
data,
signature,
};
if login_packet_data.size_is_valid() {
Ok(login_packet_data)
} else {
Err(Error::ExceededSize)
}
}
pub fn size_is_valid(&self) -> bool {
self.data.len() <= MAX_LOGIN_PACKET_BYTES
}
pub fn destination(&self) -> &XorName {
&self.destination
}
pub fn authorised_getter(&self) -> &PublicKey {
&self.authorised_getter
}
pub fn data(&self) -> &[u8] {
&self.data
}
pub fn signature(&self) -> &Signature {
&self.signature
}
pub fn into_data_and_signature(self) -> (Vec<u8>, Signature) {
(self.data, self.signature)
}
}
#[cfg(test)]
mod tests {
use super::{LoginPacket, MAX_LOGIN_PACKET_BYTES};
use crate::{ClientFullId, Error};
#[test]
fn exceed_size_limit() {
let our_id = ClientFullId::new_ed25519(&mut rand::thread_rng());
let acc_data = vec![0; MAX_LOGIN_PACKET_BYTES + 1];
let signature = our_id.sign(&acc_data);
let res = LoginPacket::new(
rand::random(),
*our_id.public_id().public_key(),
acc_data,
signature,
);
match res {
Err(Error::ExceededSize) => (),
Ok(_) => panic!("Unexpected success"),
Err(e) => panic!("Unexpected error: {:?}", e),
}
}
#[test]
fn valid() {
let our_id = ClientFullId::new_ed25519(&mut rand::thread_rng());
let acc_data = vec![1; 16];
let signature = our_id.sign(&acc_data);
let res = LoginPacket::new(
rand::random(),
*our_id.public_id().public_key(),
acc_data.clone(),
signature,
);
match res {
Ok(ad) => {
assert_eq!(ad.data(), acc_data.as_slice());
}
Err(e) => panic!("Unexpected error: {:?}", e),
}
}
}