use crate::Error;
#[repr(u8)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum WalletCall {
CreateAction = 1,
SignAction = 2,
AbortAction = 3,
ListActions = 4,
InternalizeAction = 5,
ListOutputs = 6,
RelinquishOutput = 7,
GetPublicKey = 8,
RevealCounterpartyKeyLinkage = 9,
RevealSpecificKeyLinkage = 10,
Encrypt = 11,
Decrypt = 12,
CreateHmac = 13,
VerifyHmac = 14,
CreateSignature = 15,
VerifySignature = 16,
AcquireCertificate = 17,
ListCertificates = 18,
ProveCertificate = 19,
RelinquishCertificate = 20,
DiscoverByIdentityKey = 21,
DiscoverByAttributes = 22,
IsAuthenticated = 23,
WaitForAuthentication = 24,
GetHeight = 25,
GetHeaderForHeight = 26,
GetNetwork = 27,
GetVersion = 28,
}
impl WalletCall {
pub fn as_u8(self) -> u8 {
self as u8
}
pub fn method_name(self) -> &'static str {
match self {
WalletCall::CreateAction => "createAction",
WalletCall::SignAction => "signAction",
WalletCall::AbortAction => "abortAction",
WalletCall::ListActions => "listActions",
WalletCall::InternalizeAction => "internalizeAction",
WalletCall::ListOutputs => "listOutputs",
WalletCall::RelinquishOutput => "relinquishOutput",
WalletCall::GetPublicKey => "getPublicKey",
WalletCall::RevealCounterpartyKeyLinkage => "revealCounterpartyKeyLinkage",
WalletCall::RevealSpecificKeyLinkage => "revealSpecificKeyLinkage",
WalletCall::Encrypt => "encrypt",
WalletCall::Decrypt => "decrypt",
WalletCall::CreateHmac => "createHmac",
WalletCall::VerifyHmac => "verifyHmac",
WalletCall::CreateSignature => "createSignature",
WalletCall::VerifySignature => "verifySignature",
WalletCall::AcquireCertificate => "acquireCertificate",
WalletCall::ListCertificates => "listCertificates",
WalletCall::ProveCertificate => "proveCertificate",
WalletCall::RelinquishCertificate => "relinquishCertificate",
WalletCall::DiscoverByIdentityKey => "discoverByIdentityKey",
WalletCall::DiscoverByAttributes => "discoverByAttributes",
WalletCall::IsAuthenticated => "isAuthenticated",
WalletCall::WaitForAuthentication => "waitForAuthentication",
WalletCall::GetHeight => "getHeight",
WalletCall::GetHeaderForHeight => "getHeaderForHeight",
WalletCall::GetNetwork => "getNetwork",
WalletCall::GetVersion => "getVersion",
}
}
}
impl TryFrom<u8> for WalletCall {
type Error = Error;
fn try_from(value: u8) -> Result<Self, Self::Error> {
match value {
1 => Ok(WalletCall::CreateAction),
2 => Ok(WalletCall::SignAction),
3 => Ok(WalletCall::AbortAction),
4 => Ok(WalletCall::ListActions),
5 => Ok(WalletCall::InternalizeAction),
6 => Ok(WalletCall::ListOutputs),
7 => Ok(WalletCall::RelinquishOutput),
8 => Ok(WalletCall::GetPublicKey),
9 => Ok(WalletCall::RevealCounterpartyKeyLinkage),
10 => Ok(WalletCall::RevealSpecificKeyLinkage),
11 => Ok(WalletCall::Encrypt),
12 => Ok(WalletCall::Decrypt),
13 => Ok(WalletCall::CreateHmac),
14 => Ok(WalletCall::VerifyHmac),
15 => Ok(WalletCall::CreateSignature),
16 => Ok(WalletCall::VerifySignature),
17 => Ok(WalletCall::AcquireCertificate),
18 => Ok(WalletCall::ListCertificates),
19 => Ok(WalletCall::ProveCertificate),
20 => Ok(WalletCall::RelinquishCertificate),
21 => Ok(WalletCall::DiscoverByIdentityKey),
22 => Ok(WalletCall::DiscoverByAttributes),
23 => Ok(WalletCall::IsAuthenticated),
24 => Ok(WalletCall::WaitForAuthentication),
25 => Ok(WalletCall::GetHeight),
26 => Ok(WalletCall::GetHeaderForHeight),
27 => Ok(WalletCall::GetNetwork),
28 => Ok(WalletCall::GetVersion),
_ => Err(Error::WalletError(format!(
"invalid call code: expected 1-28, got {}",
value
))),
}
}
}
impl std::fmt::Display for WalletCall {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.method_name())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_call_codes() {
assert_eq!(WalletCall::CreateAction.as_u8(), 1);
assert_eq!(WalletCall::GetVersion.as_u8(), 28);
}
#[test]
fn test_try_from_valid() {
assert_eq!(WalletCall::try_from(1).unwrap(), WalletCall::CreateAction);
assert_eq!(WalletCall::try_from(28).unwrap(), WalletCall::GetVersion);
}
#[test]
fn test_try_from_invalid() {
assert!(WalletCall::try_from(0).is_err());
assert!(WalletCall::try_from(29).is_err());
assert!(WalletCall::try_from(255).is_err());
}
#[test]
fn test_method_names() {
assert_eq!(WalletCall::CreateAction.method_name(), "createAction");
assert_eq!(WalletCall::GetVersion.method_name(), "getVersion");
}
#[test]
fn test_roundtrip() {
for code in 1..=28u8 {
let call = WalletCall::try_from(code).unwrap();
assert_eq!(call.as_u8(), code);
}
}
}