Skip to main content

soil_client/keystore/
mod.rs

1// This file is part of Soil.
2
3// Copyright (C) Soil contributors.
4// Copyright (C) Parity Technologies (UK) Ltd.
5// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
6
7//! Keystore (and session key management) for ed25519 based chains like Polkadot.
8
9#![warn(missing_docs)]
10use std::io;
11use subsoil::core::crypto::KeyTypeId;
12use subsoil::keystore::Error as TraitError;
13
14/// Local keystore implementation
15mod local;
16pub use local::LocalKeystore;
17pub use subsoil::keystore::Keystore;
18
19/// Keystore error.
20#[derive(Debug, thiserror::Error)]
21pub enum Error {
22	/// IO error.
23	#[error(transparent)]
24	Io(#[from] io::Error),
25	/// JSON error.
26	#[error(transparent)]
27	Json(#[from] serde_json::Error),
28	/// Invalid password.
29	#[error(
30		"Requested public key and public key of the loaded private key do not match. \n
31			This means either that the keystore password is incorrect or that the private key was stored under a wrong public key."
32	)]
33	PublicKeyMismatch,
34	/// Invalid BIP39 phrase
35	#[error("Invalid recovery phrase (BIP39) data")]
36	InvalidPhrase,
37	/// Invalid seed
38	#[error("Invalid seed")]
39	InvalidSeed,
40	/// Public key type is not supported
41	#[error("Key crypto type is not supported")]
42	KeyNotSupported(KeyTypeId),
43	/// Keystore unavailable
44	#[error("Keystore unavailable")]
45	Unavailable,
46}
47
48/// Keystore Result
49pub type Result<T> = std::result::Result<T, Error>;
50
51impl From<Error> for TraitError {
52	fn from(error: Error) -> Self {
53		match error {
54			Error::KeyNotSupported(id) => TraitError::KeyNotSupported(id),
55			Error::InvalidSeed | Error::InvalidPhrase | Error::PublicKeyMismatch => {
56				TraitError::ValidationError(error.to_string())
57			},
58			Error::Unavailable => TraitError::Unavailable,
59			Error::Io(e) => TraitError::Other(e.to_string()),
60			Error::Json(e) => TraitError::Other(e.to_string()),
61		}
62	}
63}