client/rpc/types/
provenance.rs

1// Copyright 2015-2019 Parity Technologies (UK) Ltd.
2// This file is part of Parity Ethereum.
3
4// Parity Ethereum is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// Parity Ethereum is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with Parity Ethereum.  If not, see <http://www.gnu.org/licenses/>.
16
17//! Request Provenance
18
19use cfx_types::H256;
20use serde::{Deserialize, Serialize};
21use std::{fmt, net::SocketAddr};
22
23/// RPC request origin
24#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
25#[serde(deny_unknown_fields)]
26#[serde(rename_all = "kebab-case")]
27pub enum Origin {
28    /// RPC server (includes request origin)
29    Rpc(String),
30    /// TCP server (includes peer address)
31    Tcp(SocketAddr),
32    /// WS server
33    Ws {
34        /// Session id
35        session: H256,
36    },
37    /// Signer (authorized WS server)
38    Signer {
39        /// Session id
40        session: H256,
41    },
42    /// From the C API
43    CApi,
44    /// Unknown
45    Unknown,
46}
47
48impl Default for Origin {
49    fn default() -> Self { Origin::Unknown }
50}
51
52impl fmt::Display for Origin {
53    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
54        match *self {
55            Origin::Rpc(ref origin) => write!(f, "{} via RPC", origin),
56            Origin::Tcp(ref address) => write!(f, "TCP (address: {})", address),
57            Origin::Ws { ref session } => {
58                write!(f, "WebSocket (session: {})", session)
59            }
60            Origin::Signer { ref session } => {
61                write!(f, "Secure Session (session: {})", session)
62            }
63            Origin::CApi => write!(f, "C API"),
64            Origin::Unknown => write!(f, "unknown origin"),
65        }
66    }
67}