1pub mod model;
7pub mod projection;
8
9pub use ic_wire as wire;
10use ic_wire::{Direction, Encoding, Limits, List, Text};
11pub use model::SessionView;
12
13pub const BROWSER_LOGOUT_RESPONSE_BYTES: usize = 1024;
15
16pub const BROWSER_DEFAULT_CREDENTIAL_BYTES: usize = 131_072;
18pub const BROWSER_MAX_CONFIGURED_CREDENTIAL_BYTES: usize = 1_048_576;
20
21#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
23#[cfg_attr(
24 all(feature = "typescript", not(target_family = "wasm")),
25 derive(ic_wire::typescript::Wire)
26)]
27pub struct SessionInput {
28 pub credential: model::PortableCredential,
30 pub scopes: List<Text<64>, 16>,
32 pub ttl_secs: Option<u64>,
34}
35
36const CREDENTIAL_LIMITS: Limits = Limits {
37 bytes: 524_288,
38 allocation: 2_097_152,
39 nodes: 65_536,
40 depth: 32,
41};
42const EMPTY_LIMITS: Limits = Limits {
43 bytes: 11,
44 allocation: 128,
45 nodes: 8,
46 depth: 4,
47};
48const REPLY_LIMITS: Limits = Limits {
49 bytes: 4096,
50 allocation: 16384,
51 nodes: 128,
52 depth: 8,
53};
54
55macro_rules! contract {
58 ($contract:ident, $id:expr, $name:literal, $request:ty, $reply:ty, $request_limits:ident, $reply_limits:ident) => {
59 impl $contract {
60 pub const ID: u64 = $id;
62 pub const NAME: &'static str = $name;
64 pub const REQUEST_LIMITS: Limits = $request_limits;
66 pub const RESPONSE_LIMITS: Limits = $reply_limits;
68
69 pub fn encode_request(value: &$request) -> Result<Vec<u8>, ic_wire::Error> {
74 ic_wire::encode(
75 value,
76 Self::encoding(Direction::Request),
77 Self::REQUEST_LIMITS,
78 )
79 }
80
81 pub fn decode_request(bytes: &[u8]) -> Result<$request, ic_wire::Error> {
86 ic_wire::decode(
87 bytes,
88 Self::encoding(Direction::Request),
89 Self::REQUEST_LIMITS,
90 )
91 }
92
93 pub fn encode_response(value: &$reply) -> Result<Vec<u8>, ic_wire::Error> {
98 ic_wire::encode(
99 value,
100 Self::encoding(Direction::Response),
101 Self::RESPONSE_LIMITS,
102 )
103 }
104
105 pub fn decode_response(bytes: &[u8]) -> Result<$reply, ic_wire::Error> {
110 ic_wire::decode(
111 bytes,
112 Self::encoding(Direction::Response),
113 Self::RESPONSE_LIMITS,
114 )
115 }
116
117 const fn encoding(direction: Direction) -> Encoding {
118 Encoding::Frame {
119 method: Self::ID,
120 direction,
121 }
122 }
123 }
124 };
125}
126
127pub struct ReadSession;
129contract!(
130 ReadSession,
131 0x415554480013,
132 "portable_session_read",
133 (),
134 Result<SessionView, model::SessionReadRefusal>,
135 EMPTY_LIMITS,
136 REPLY_LIMITS
137);
138
139pub struct Logout;
141contract!(
142 Logout,
143 0x415554480014,
144 "logout_portable_session",
145 (),
146 model::LogoutResult,
147 EMPTY_LIMITS,
148 REPLY_LIMITS
149);
150
151pub struct CredentialEnvelope;
153contract!(
154 CredentialEnvelope,
155 0x415554480001,
156 "credential",
157 model::PortableCredential,
158 model::PortableCredential,
159 CREDENTIAL_LIMITS,
160 CREDENTIAL_LIMITS
161);
162
163pub struct EstablishSession;
165contract!(
166 EstablishSession,
167 0x415554480012,
168 "establish_portable_session",
169 SessionInput,
170 Result<SessionView, model::AdmissionRefusal>,
171 CREDENTIAL_LIMITS,
172 REPLY_LIMITS
173);