Skip to main content

oasis_core_runtime/enclave_rpc/
types.rs

1//! RPC protocol types.
2use rand::{rngs::OsRng, Rng};
3
4impl_bytes!(
5    SessionID,
6    32,
7    "Session identifier for multiplexing multiple sessions over the \
8     same transport"
9);
10
11impl SessionID {
12    /// Generate a random session identifier.
13    pub fn random() -> Self {
14        let mut session_id = [0u8; 32];
15        OsRng.fill(&mut session_id);
16
17        SessionID(session_id)
18    }
19}
20
21/// RPC call kind.
22#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, cbor::Encode, cbor::Decode)]
23#[cbor(with_default)]
24#[repr(u8)]
25#[derive(Default)]
26pub enum Kind {
27    /// A secure RPC call using an encrypted and authenticated Noise session.
28    #[default]
29    NoiseSession = 0,
30    /// An insecure RPC call where messages are sent in plain text.
31    InsecureQuery = 1,
32    /// A local RPC call.
33    LocalQuery = 2,
34}
35
36/// Frame.
37#[derive(Clone, Debug, Default, cbor::Encode, cbor::Decode)]
38pub struct Frame {
39    pub session: SessionID,
40    // The `untrusted_plaintext` field is only a temporary workaround until
41    // the snow library supports encrypting the payload with authenticated
42    // data.
43    // This field contains a plaintext copy of the Request's `method` field
44    // and is verified inside the enclave.  It is unused in other cases.
45    pub untrusted_plaintext: String,
46    pub payload: Vec<u8>,
47}
48
49#[derive(Clone, Debug, cbor::Encode, cbor::Decode)]
50#[cbor(no_default)]
51pub struct Request {
52    pub method: String,
53    pub args: cbor::Value,
54}
55
56#[derive(Clone, Debug, Default, cbor::Encode, cbor::Decode)]
57pub struct Error {
58    pub message: String,
59}
60
61#[derive(Clone, Debug, cbor::Encode, cbor::Decode)]
62pub enum Body {
63    Success(cbor::Value),
64    Error(String),
65}
66
67#[derive(Clone, Debug, cbor::Encode, cbor::Decode)]
68#[cbor(no_default)]
69pub struct Response {
70    pub body: Body,
71}
72
73/// Protocol message.
74#[derive(Clone, Debug, cbor::Encode, cbor::Decode)]
75pub enum Message {
76    Request(Request),
77    Response(Response),
78    Close,
79}
80
81/// Feedback on the peer that handled the last EnclaveRPC call.
82#[derive(Copy, Clone, Debug, PartialEq, Eq, cbor::Encode, cbor::Decode)]
83pub enum PeerFeedback {
84    Success = 0,
85    Failure = 1,
86    BadPeer = 2,
87}