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(feature = "typescript", derive(ic_wire::typescript::Wire))]
24pub struct SessionInput {
25 pub credential: model::PortableCredential,
27 pub scopes: List<Text<64>, 16>,
29 pub ttl_secs: Option<u64>,
31}
32
33const CREDENTIAL_LIMITS: Limits = Limits {
34 bytes: 524_288,
35 allocation: 2_097_152,
36 nodes: 65_536,
37 depth: 32,
38};
39const EMPTY_LIMITS: Limits = Limits {
40 bytes: 11,
41 allocation: 128,
42 nodes: 8,
43 depth: 4,
44};
45const REPLY_LIMITS: Limits = Limits {
46 bytes: 4096,
47 allocation: 16384,
48 nodes: 128,
49 depth: 8,
50};
51
52macro_rules! contract {
55 ($contract:ident, $id:expr, $name:literal, $request:ty, $reply:ty, $request_limits:ident, $reply_limits:ident) => {
56 impl $contract {
57 pub const ID: u64 = $id;
59 pub const NAME: &'static str = $name;
61 pub const REQUEST_LIMITS: Limits = $request_limits;
63 pub const RESPONSE_LIMITS: Limits = $reply_limits;
65
66 pub fn encode_request(value: &$request) -> Result<Vec<u8>, ic_wire::Error> {
71 ic_wire::encode(
72 value,
73 Self::encoding(Direction::Request),
74 Self::REQUEST_LIMITS,
75 )
76 }
77
78 pub fn decode_request(bytes: &[u8]) -> Result<$request, ic_wire::Error> {
83 ic_wire::decode(
84 bytes,
85 Self::encoding(Direction::Request),
86 Self::REQUEST_LIMITS,
87 )
88 }
89
90 pub fn encode_response(value: &$reply) -> Result<Vec<u8>, ic_wire::Error> {
95 ic_wire::encode(
96 value,
97 Self::encoding(Direction::Response),
98 Self::RESPONSE_LIMITS,
99 )
100 }
101
102 pub fn decode_response(bytes: &[u8]) -> Result<$reply, ic_wire::Error> {
107 ic_wire::decode(
108 bytes,
109 Self::encoding(Direction::Response),
110 Self::RESPONSE_LIMITS,
111 )
112 }
113
114 const fn encoding(direction: Direction) -> Encoding {
115 Encoding::Frame {
116 method: Self::ID,
117 direction,
118 }
119 }
120 }
121 };
122}
123
124pub struct ReadSession;
126
127contract!(
128 ReadSession,
129 0x415554480013,
130 "portable_session_read",
131 (),
132 Result<SessionView, model::SessionReadRefusal>,
133 EMPTY_LIMITS,
134 REPLY_LIMITS
135);
136
137pub struct Logout;
139
140contract!(
141 Logout,
142 0x415554480014,
143 "logout_portable_session",
144 (),
145 model::LogoutResult,
146 EMPTY_LIMITS,
147 REPLY_LIMITS
148);
149
150pub struct CredentialEnvelope;
152
153contract!(
154 CredentialEnvelope,
155 0x415554480001,
156 "credential",
157 model::PortableCredential,
158 model::PortableCredential,
159 CREDENTIAL_LIMITS,
160 CREDENTIAL_LIMITS
161);
162
163pub struct EstablishSession;
165
166contract!(
167 EstablishSession,
168 0x415554480012,
169 "establish_portable_session",
170 SessionInput,
171 Result<SessionView, model::AdmissionRefusal>,
172 CREDENTIAL_LIMITS,
173 REPLY_LIMITS
174);