mod login_packet;
pub use self::login_packet::{LoginPacket, MAX_LOGIN_PACKET_BYTES};
use crate::{
AData, ADataAddress, ADataAppendOperation, ADataIndex, ADataOwner, ADataPubPermissions,
ADataUnpubPermissions, ADataUser, AppPermissions, Coins, Error, IData, IDataAddress, MData,
MDataAddress, MDataEntryActions, MDataPermissionSet, PublicKey, Response, TransactionId,
XorName,
};
use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize, Debug)]
pub enum Type {
PublicGet,
PrivateGet,
Mutation,
Transaction,
}
#[allow(clippy::large_enum_variant)]
#[derive(Hash, Eq, PartialEq, PartialOrd, Ord, Clone, Serialize, Deserialize)]
pub enum Request {
PutIData(IData),
GetIData(IDataAddress),
DeleteUnpubIData(IDataAddress),
PutMData(MData),
GetMData(MDataAddress),
GetMDataValue {
address: MDataAddress,
key: Vec<u8>,
},
DeleteMData(MDataAddress),
GetMDataShell(MDataAddress),
GetMDataVersion(MDataAddress),
ListMDataEntries(MDataAddress),
ListMDataKeys(MDataAddress),
ListMDataValues(MDataAddress),
SetMDataUserPermissions {
address: MDataAddress,
user: PublicKey,
permissions: MDataPermissionSet,
version: u64,
},
DelMDataUserPermissions {
address: MDataAddress,
user: PublicKey,
version: u64,
},
ListMDataPermissions(MDataAddress),
ListMDataUserPermissions {
address: MDataAddress,
user: PublicKey,
},
MutateMDataEntries {
address: MDataAddress,
actions: MDataEntryActions,
},
PutAData(AData),
GetAData(ADataAddress),
GetADataShell {
address: ADataAddress,
data_index: ADataIndex,
},
DeleteAData(ADataAddress),
GetADataRange {
address: ADataAddress,
range: (ADataIndex, ADataIndex),
},
GetADataValue {
address: ADataAddress,
key: Vec<u8>,
},
GetADataIndices(ADataAddress),
GetADataLastEntry(ADataAddress),
GetADataPermissions {
address: ADataAddress,
permissions_index: ADataIndex,
},
GetPubADataUserPermissions {
address: ADataAddress,
permissions_index: ADataIndex,
user: ADataUser,
},
GetUnpubADataUserPermissions {
address: ADataAddress,
permissions_index: ADataIndex,
public_key: PublicKey,
},
GetADataOwners {
address: ADataAddress,
owners_index: ADataIndex,
},
AddPubADataPermissions {
address: ADataAddress,
permissions: ADataPubPermissions,
permissions_index: u64,
},
AddUnpubADataPermissions {
address: ADataAddress,
permissions: ADataUnpubPermissions,
permissions_index: u64,
},
SetADataOwner {
address: ADataAddress,
owner: ADataOwner,
owners_index: u64,
},
AppendSeq {
append: ADataAppendOperation,
index: u64,
},
AppendUnseq(ADataAppendOperation),
TransferCoins {
destination: XorName,
amount: Coins,
transaction_id: TransactionId,
},
GetBalance,
CreateBalance {
new_balance_owner: PublicKey,
amount: Coins,
transaction_id: TransactionId,
},
CreateLoginPacket(LoginPacket),
CreateLoginPacketFor {
new_owner: PublicKey,
amount: Coins,
transaction_id: TransactionId,
new_login_packet: LoginPacket,
},
UpdateLoginPacket(LoginPacket),
GetLoginPacket(XorName),
ListAuthKeysAndVersion,
InsAuthKey {
key: PublicKey,
version: u64,
permissions: AppPermissions,
},
DelAuthKey {
key: PublicKey,
version: u64,
},
}
impl Request {
pub fn get_type(&self) -> Type {
use Request::*;
match *self {
GetIData(address) => {
if address.is_pub() {
Type::PublicGet
} else {
Type::PrivateGet
}
}
GetAData(address)
| GetADataShell { address, .. }
| GetADataRange { address, .. }
| GetADataValue { address, .. }
| GetADataIndices(address)
| GetADataLastEntry(address)
| GetADataPermissions { address, .. }
| GetPubADataUserPermissions { address, .. }
| GetUnpubADataUserPermissions { address, .. }
| GetADataOwners { address, .. } => {
if address.is_pub() {
Type::PublicGet
} else {
Type::PrivateGet
}
}
GetMData(_)
| GetMDataValue { .. }
| GetMDataShell(_)
| GetMDataVersion(_)
| ListMDataEntries(_)
| ListMDataKeys(_)
| ListMDataValues(_)
| ListMDataPermissions(_)
| ListMDataUserPermissions { .. } => Type::PrivateGet,
GetBalance |
GetLoginPacket(..) |
ListAuthKeysAndVersion => Type::PrivateGet,
TransferCoins { .. } | CreateBalance { .. } |
CreateLoginPacketFor { .. } => {
Type::Transaction
}
PutIData(_) |
DeleteUnpubIData(_) |
PutMData(_) |
DeleteMData(_) |
SetMDataUserPermissions { .. } |
DelMDataUserPermissions { .. } |
MutateMDataEntries { .. } |
PutAData(_) |
DeleteAData(_) |
AddPubADataPermissions { .. } |
AddUnpubADataPermissions { .. } |
SetADataOwner { .. } |
AppendSeq { .. } |
AppendUnseq(_) |
CreateLoginPacket { .. } |
UpdateLoginPacket { .. } |
InsAuthKey { .. } |
DelAuthKey { .. } => Type::Mutation,
}
}
pub fn error_response(&self, error: Error) -> Response {
use Request::*;
match *self {
GetIData(_) => Response::GetIData(Err(error)),
GetMData(_) => Response::GetMData(Err(error)),
GetMDataValue { .. } => Response::GetMDataValue(Err(error)),
GetMDataShell(_) => Response::GetMDataShell(Err(error)),
GetMDataVersion(_) => Response::GetMDataVersion(Err(error)),
ListMDataEntries(_) => Response::ListMDataEntries(Err(error)),
ListMDataKeys(_) => Response::ListMDataKeys(Err(error)),
ListMDataValues(_) => Response::ListMDataValues(Err(error)),
ListMDataPermissions(_) => Response::ListMDataPermissions(Err(error)),
ListMDataUserPermissions { .. } => Response::ListMDataUserPermissions(Err(error)),
GetAData(_) => Response::GetAData(Err(error)),
GetADataShell { .. } => Response::GetADataShell(Err(error)),
GetADataValue { .. } => Response::GetADataValue(Err(error)),
GetADataRange { .. } => Response::GetADataRange(Err(error)),
GetADataIndices(_) => Response::GetADataIndices(Err(error)),
GetADataLastEntry(_) => Response::GetADataLastEntry(Err(error)),
GetADataPermissions { .. } => Response::GetADataPermissions(Err(error)),
GetPubADataUserPermissions { .. } => Response::GetPubADataUserPermissions(Err(error)),
GetUnpubADataUserPermissions { .. } => {
Response::GetUnpubADataUserPermissions(Err(error))
}
GetADataOwners { .. } => Response::GetADataOwners(Err(error)),
GetBalance => Response::GetBalance(Err(error)),
GetLoginPacket(..) => Response::GetLoginPacket(Err(error)),
ListAuthKeysAndVersion => Response::ListAuthKeysAndVersion(Err(error)),
TransferCoins { .. } | CreateBalance { .. }
| CreateLoginPacketFor { .. } => Response::Transaction(Err(error)),
PutIData(_) |
DeleteUnpubIData(_) |
PutMData(_) |
DeleteMData(_) |
SetMDataUserPermissions { .. } |
DelMDataUserPermissions { .. } |
MutateMDataEntries { .. } |
PutAData(_) |
DeleteAData(_) |
AddPubADataPermissions { .. } |
AddUnpubADataPermissions { .. } |
SetADataOwner { .. } |
AppendSeq { .. } |
AppendUnseq(_) |
CreateLoginPacket { .. } |
UpdateLoginPacket { .. } |
InsAuthKey { .. } |
DelAuthKey { .. } => Response::Mutation(Err(error)),
}
}
}
impl fmt::Debug for Request {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
use Request::*;
write!(
formatter,
"Request::{}",
match *self {
PutIData(_) => "PutIData",
GetIData(_) => "GetIData",
DeleteUnpubIData(_) => "DeleteUnpubIData",
PutMData(_) => "PutMData",
GetMData(_) => "GetMData",
GetMDataValue { .. } => "GetMDataValue",
DeleteMData(_) => "DeleteMData",
GetMDataShell(_) => "GetMDataShell",
GetMDataVersion(_) => "GetMDataVersion",
ListMDataEntries(_) => "ListMDataEntries",
ListMDataKeys(_) => "ListMDataKeys",
ListMDataValues(_) => "ListMDataValues",
SetMDataUserPermissions { .. } => "SetMDataUserPermissions",
DelMDataUserPermissions { .. } => "DelMDataUserPermissions",
ListMDataPermissions(_) => "ListMDataPermissions",
ListMDataUserPermissions { .. } => "ListMDataUserPermissions",
MutateMDataEntries { .. } => "MutateMDataEntries",
PutAData(_) => "PutAData",
GetAData(_) => "GetAData",
GetADataShell { .. } => "GetADataShell",
GetADataValue { .. } => "GetADataValue ",
DeleteAData(_) => "DeleteAData",
GetADataRange { .. } => "GetADataRange",
GetADataIndices(_) => "GetADataIndices",
GetADataLastEntry(_) => "GetADataLastEntry",
GetADataPermissions { .. } => "GetADataPermissions",
GetPubADataUserPermissions { .. } => "GetPubADataUserPermissions",
GetUnpubADataUserPermissions { .. } => "GetUnpubADataUserPermissions",
GetADataOwners { .. } => "GetADataOwners",
AddPubADataPermissions { .. } => "AddPubADataPermissions",
AddUnpubADataPermissions { .. } => "AddUnpubADataPermissions",
SetADataOwner { .. } => "SetADataOwner",
AppendSeq { .. } => "AppendSeq",
AppendUnseq(_) => "AppendUnseq",
TransferCoins { .. } => "TransferCoins",
GetBalance => "GetBalance",
CreateBalance { .. } => "CreateBalance",
CreateLoginPacket { .. } => "CreateLoginPacket",
CreateLoginPacketFor { .. } => "CreateLoginPacketFor",
UpdateLoginPacket { .. } => "UpdateLoginPacket",
GetLoginPacket(..) => "GetLoginPacket",
ListAuthKeysAndVersion => "ListAuthKeysAndVersion",
InsAuthKey { .. } => "InsAuthKey",
DelAuthKey { .. } => "DelAuthKey",
}
)
}
}