referrerpolicy=no-referrer-when-downgrade

sc_keystore/
lib.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//! Keystore (and session key management) for ed25519 based chains like Polkadot.
20
21#![warn(missing_docs)]
22use sp_core::crypto::KeyTypeId;
23use sp_keystore::Error as TraitError;
24use std::io;
25
26/// Local keystore implementation
27mod local;
28pub use local::LocalKeystore;
29pub use sp_keystore::Keystore;
30
31/// Keystore error.
32#[derive(Debug, thiserror::Error)]
33pub enum Error {
34	/// IO error.
35	#[error(transparent)]
36	Io(#[from] io::Error),
37	/// JSON error.
38	#[error(transparent)]
39	Json(#[from] serde_json::Error),
40	/// Invalid password.
41	#[error(
42		"Requested public key and public key of the loaded private key do not match. \n
43			This means either that the keystore password is incorrect or that the private key was stored under a wrong public key."
44	)]
45	PublicKeyMismatch,
46	/// Invalid BIP39 phrase
47	#[error("Invalid recovery phrase (BIP39) data")]
48	InvalidPhrase,
49	/// Invalid seed
50	#[error("Invalid seed")]
51	InvalidSeed,
52	/// Public key type is not supported
53	#[error("Key crypto type is not supported")]
54	KeyNotSupported(KeyTypeId),
55	/// Keystore unavailable
56	#[error("Keystore unavailable")]
57	Unavailable,
58}
59
60/// Keystore Result
61pub type Result<T> = std::result::Result<T, Error>;
62
63impl From<Error> for TraitError {
64	fn from(error: Error) -> Self {
65		match error {
66			Error::KeyNotSupported(id) => TraitError::KeyNotSupported(id),
67			Error::InvalidSeed | Error::InvalidPhrase | Error::PublicKeyMismatch =>
68				TraitError::ValidationError(error.to_string()),
69			Error::Unavailable => TraitError::Unavailable,
70			Error::Io(e) => TraitError::Other(e.to_string()),
71			Error::Json(e) => TraitError::Other(e.to_string()),
72		}
73	}
74}