pub mod model;
pub mod projection;
pub use ic_wire as wire;
use ic_wire::{Direction, Encoding, Limits, List, Text};
pub use model::SessionView;
pub const BROWSER_LOGOUT_RESPONSE_BYTES: usize = 1024;
pub const BROWSER_DEFAULT_CREDENTIAL_BYTES: usize = 131_072;
pub const BROWSER_MAX_CONFIGURED_CREDENTIAL_BYTES: usize = 1_048_576;
#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize, ic_wire::Wire)]
pub struct SessionInput {
pub credential: model::PortableCredential,
pub scopes: List<Text<64>, 16>,
pub ttl_secs: Option<u64>,
}
const CREDENTIAL_LIMITS: Limits = Limits {
bytes: 524_288,
allocation: 2_097_152,
nodes: 65_536,
depth: 32,
};
const EMPTY_LIMITS: Limits = Limits {
bytes: 12,
allocation: 8192,
nodes: 32,
depth: 8,
};
const REPLY_LIMITS: Limits = Limits {
bytes: 4096,
allocation: 16384,
nodes: 128,
depth: 8,
};
const SESSION_REPLY_LIMITS: Limits = Limits {
bytes: 8192,
allocation: 131072,
..REPLY_LIMITS
};
const CREDENTIAL_WRITE_LIMITS: Limits = Limits {
allocation: 3 * 1024 * 1024,
..CREDENTIAL_LIMITS
};
macro_rules! contract {
(@writer CREDENTIAL_LIMITS) => { CREDENTIAL_WRITE_LIMITS };
(@writer $limits:ident) => { $limits };
(@encode direct, $value:expr, $encoding:expr, $limits:expr,) => { ic_wire::encode($value, $encoding, $limits) };
(@decode direct, $bytes:expr, $encoding:expr, $limits:expr,) => { ic_wire::decode($bytes, $encoding, $limits) };
(@encode wrapped, $value:expr, $encoding:expr, $limits:expr,) => { ic_wire::encode(&ic_wire::Message($value.clone()), $encoding, $limits) };
(@decode wrapped, $bytes:expr, $encoding:expr, $limits:expr,) => { ic_wire::decode::<ic_wire::Message<_>>($bytes, $encoding, $limits).map(|m| m.0) };
($contract:ident, $id:expr, $name:literal, $request:ty, $reqroot:ident, $reply:ty, $reproot:ident, $request_limits:ident, $reply_limits:ident) => {
impl $contract {
pub const ID: u64 = $id;
pub const NAME: &'static str = $name;
pub const REQUEST_LIMITS: Limits = $request_limits;
pub const RESPONSE_LIMITS: Limits = $reply_limits;
pub const REQUEST_WRITE_LIMITS: Limits = contract!(@writer $request_limits);
pub const RESPONSE_WRITE_LIMITS: Limits = contract!(@writer $reply_limits);
pub fn encode_request(value: &$request) -> Result<Vec<u8>, ic_wire::Error> {
contract!(@encode $reqroot, value,
Self::encoding(Direction::Request),
Self::REQUEST_WRITE_LIMITS,
)
}
pub fn decode_request(bytes: &[u8]) -> Result<$request, ic_wire::Error> {
contract!(@decode $reqroot, bytes,
Self::encoding(Direction::Request),
Self::REQUEST_LIMITS,
)
}
pub fn encode_response(value: &$reply) -> Result<Vec<u8>, ic_wire::Error> {
contract!(@encode $reproot, value,
Self::encoding(Direction::Response),
Self::RESPONSE_WRITE_LIMITS,
)
}
pub fn decode_response(bytes: &[u8]) -> Result<$reply, ic_wire::Error> {
contract!(@decode $reproot, bytes,
Self::encoding(Direction::Response),
Self::RESPONSE_LIMITS,
)
}
const fn encoding(direction: Direction) -> Encoding {
Encoding::Frame {
method: Self::ID,
direction,
}
}
}
};
}
pub struct ReadSession;
contract!(
ReadSession,
0x415554480113,
"portable_session_read",
(), wrapped,
Result<SessionView, model::SessionReadRefusal>, wrapped,
EMPTY_LIMITS,
SESSION_REPLY_LIMITS
);
pub struct Logout;
contract!(
Logout,
0x415554480114,
"logout_portable_session",
(),
wrapped,
model::LogoutResult,
wrapped,
EMPTY_LIMITS,
REPLY_LIMITS
);
pub struct CredentialEnvelope;
contract!(
CredentialEnvelope,
0x415554480101,
"credential",
model::PortableCredential,
direct,
model::PortableCredential,
direct,
CREDENTIAL_LIMITS,
CREDENTIAL_LIMITS
);
pub struct EstablishSession;
contract!(
EstablishSession,
0x415554480112,
"establish_portable_session",
SessionInput, direct,
Result<SessionView, model::AdmissionRefusal>, wrapped,
CREDENTIAL_LIMITS,
SESSION_REPLY_LIMITS
);