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;
129
130contract!(
131 ReadSession,
132 0x415554480013,
133 "portable_session_read",
134 (),
135 Result<SessionView, model::SessionReadRefusal>,
136 EMPTY_LIMITS,
137 REPLY_LIMITS
138);
139
140pub struct Logout;
142
143contract!(
144 Logout,
145 0x415554480014,
146 "logout_portable_session",
147 (),
148 model::LogoutResult,
149 EMPTY_LIMITS,
150 REPLY_LIMITS
151);
152
153pub struct CredentialEnvelope;
155
156contract!(
157 CredentialEnvelope,
158 0x415554480001,
159 "credential",
160 model::PortableCredential,
161 model::PortableCredential,
162 CREDENTIAL_LIMITS,
163 CREDENTIAL_LIMITS
164);
165
166pub struct EstablishSession;
168
169contract!(
170 EstablishSession,
171 0x415554480012,
172 "establish_portable_session",
173 SessionInput,
174 Result<SessionView, model::AdmissionRefusal>,
175 CREDENTIAL_LIMITS,
176 REPLY_LIMITS
177);