use candid::{CandidType, Principal};
use serde::{Deserialize, Serialize};
mod bytes;
mod xid;
pub use bytes::*;
pub use xid::*;
#[derive(CandidType, Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
pub struct Delegation {
pub pubkey: ByteBufB64,
pub expiration: u64,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub targets: Option<Vec<Principal>>,
}
#[derive(CandidType, Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
pub struct SignedDelegation {
pub delegation: Delegation,
pub signature: ByteBufB64,
}
#[derive(CandidType, Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
pub struct SignInResponse {
pub expiration: u64,
pub user_key: ByteBufB64,
pub seed: ByteBufB64,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_delegation_formart() {
let d = Delegation {
pubkey: ByteBufB64(vec![1, 2, 3, 4]),
expiration: 99,
targets: Some(vec![Principal::management_canister()]),
};
let data = serde_json::to_string(&d).unwrap();
println!("{}", data);
assert_eq!(
data,
r#"{"pubkey":"AQIDBA==","expiration":99,"targets":["aaaaa-aa"]}"#
);
let d1: Delegation = serde_json::from_str(&data).unwrap();
assert_eq!(d, d1);
let mut data = Vec::new();
ciborium::into_writer(&d, &mut data).unwrap();
println!("{}", const_hex::encode(&data));
assert_eq!(
data,
const_hex::decode(
"a3667075626b657944010203046a65787069726174696f6e186367746172676574738140"
)
.unwrap()
);
let d1: Delegation = ciborium::from_reader(&data[..]).unwrap();
assert_eq!(d, d1);
}
}