sc_rpc_spec_v2/chain_spec/chain_spec.rs
1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
5
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License
17// along with this program. If not, see <https://www.gnu.org/licenses/>.
18
19//! API implementation for the specification of a chain.
20
21use crate::chain_spec::api::ChainSpecApiServer;
22use jsonrpsee::core::RpcResult;
23use sc_chain_spec::Properties;
24
25/// An API for chain spec RPC calls.
26pub struct ChainSpec {
27 /// The name of the chain.
28 name: String,
29 /// The hexadecimal encoded hash of the genesis block.
30 genesis_hash: String,
31 /// Chain properties.
32 properties: Properties,
33}
34
35impl ChainSpec {
36 /// Creates a new [`ChainSpec`].
37 pub fn new<Hash: AsRef<[u8]>>(
38 name: String,
39 genesis_hash: Hash,
40 properties: Properties,
41 ) -> Self {
42 let genesis_hash = format!("0x{}", hex::encode(genesis_hash));
43
44 Self { name, properties, genesis_hash }
45 }
46}
47
48impl ChainSpecApiServer for ChainSpec {
49 fn chain_spec_v1_chain_name(&self) -> RpcResult<String> {
50 Ok(self.name.clone())
51 }
52
53 fn chain_spec_v1_genesis_hash(&self) -> RpcResult<String> {
54 Ok(self.genesis_hash.clone())
55 }
56
57 fn chain_spec_v1_properties(&self) -> RpcResult<Properties> {
58 Ok(self.properties.clone())
59 }
60}