tet_application_crypto/
traits.rs

1// This file is part of Tetcore.
2
3// Copyright (C) 2019-2021 Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18#[cfg(feature = "full_crypto")]
19use tet_core::crypto::Pair;
20
21use codec::Codec;
22use tet_core::crypto::{KeyTypeId, CryptoType, CryptoTypeId, IsWrappedBy, Public};
23use tetcore_std::{fmt::Debug, vec::Vec};
24
25/// An application-specific key.
26pub trait AppKey: 'static + Send + Sync + Sized + CryptoType + Clone {
27	/// The corresponding type as a generic crypto type.
28	type UntypedGeneric: IsWrappedBy<Self>;
29
30	/// The corresponding public key type in this application scheme.
31	type Public: AppPublic;
32
33	/// The corresponding key pair type in this application scheme.
34	#[cfg(feature = "full_crypto")]
35	type Pair: AppPair;
36
37	/// The corresponding signature type in this application scheme.
38	type Signature: AppSignature;
39
40	/// An identifier for this application-specific key type.
41	const ID: KeyTypeId;
42	/// The identifier of the crypto type of this application-specific key type.
43	const CRYPTO_ID: CryptoTypeId;
44}
45
46/// Type which implements Hash in std, not when no-std (std variant).
47#[cfg(any(feature = "std", feature = "full_crypto"))]
48pub trait MaybeHash: tetcore_std::hash::Hash {}
49#[cfg(any(feature = "std", feature = "full_crypto"))]
50impl<T: tetcore_std::hash::Hash> MaybeHash for T {}
51
52/// Type which implements Hash in std, not when no-std (no-std variant).
53#[cfg(all(not(feature = "std"), not(feature = "full_crypto")))]
54pub trait MaybeHash {}
55#[cfg(all(not(feature = "std"), not(feature = "full_crypto")))]
56impl<T> MaybeHash for T {}
57
58/// Type which implements Debug and Hash in std, not when no-std (no-std variant with crypto).
59#[cfg(all(not(feature = "std"), feature = "full_crypto"))]
60pub trait MaybeDebugHash: tetcore_std::hash::Hash  {}
61#[cfg(all(not(feature = "std"), feature = "full_crypto"))]
62impl<T: tetcore_std::hash::Hash> MaybeDebugHash for T {}
63
64/// A application's public key.
65pub trait AppPublic:
66	AppKey + Public + Ord + PartialOrd + Eq + PartialEq + Debug + MaybeHash + codec::Codec
67{
68	/// The wrapped type which is just a plain instance of `Public`.
69	type Generic:
70		IsWrappedBy<Self> + Public + Ord + PartialOrd + Eq + PartialEq + Debug + MaybeHash + codec::Codec;
71}
72
73/// A application's key pair.
74#[cfg(feature = "full_crypto")]
75pub trait AppPair: AppKey + Pair<Public=<Self as AppKey>::Public> {
76	/// The wrapped type which is just a plain instance of `Pair`.
77	type Generic: IsWrappedBy<Self> + Pair<Public = <<Self as AppKey>::Public as AppPublic>::Generic>;
78}
79
80/// A application's signature.
81pub trait AppSignature: AppKey + Eq + PartialEq + Debug + MaybeHash {
82	/// The wrapped type which is just a plain instance of `Signature`.
83	type Generic: IsWrappedBy<Self> + Eq + PartialEq + Debug + MaybeHash;
84}
85
86/// A runtime interface for a public key.
87pub trait RuntimePublic: Sized {
88	/// The signature that will be generated when signing with the corresponding private key.
89	type Signature: Codec + Debug + MaybeHash + Eq + PartialEq + Clone;
90
91	/// Returns all public keys for the given key type in the keystore.
92	fn all(key_type: KeyTypeId) -> crate::Vec<Self>;
93
94	/// Generate a public/private pair for the given key type with an optional `seed` and
95	/// store it in the keystore.
96	///
97	/// The `seed` needs to be valid utf8.
98	///
99	/// Returns the generated public key.
100	fn generate_pair(key_type: KeyTypeId, seed: Option<Vec<u8>>) -> Self;
101
102	/// Sign the given message with the corresponding private key of this public key.
103	///
104	/// The private key will be requested from the keystore using the given key type.
105	///
106	/// Returns the signature or `None` if the private key could not be found or some other error
107	/// occurred.
108	fn sign<M: AsRef<[u8]>>(&self, key_type: KeyTypeId, msg: &M) -> Option<Self::Signature>;
109
110	/// Verify that the given signature matches the given message using this public key.
111	fn verify<M: AsRef<[u8]>>(&self, msg: &M, signature: &Self::Signature) -> bool;
112
113	/// Returns `Self` as raw vec.
114	fn to_raw_vec(&self) -> Vec<u8>;
115}
116
117/// A runtime interface for an application's public key.
118pub trait RuntimeAppPublic: Sized {
119	/// An identifier for this application-specific key type.
120	const ID: KeyTypeId;
121	/// The identifier of the crypto type of this application-specific key type.
122	const CRYPTO_ID: CryptoTypeId;
123
124	/// The signature that will be generated when signing with the corresponding private key.
125	type Signature: Codec + Debug + MaybeHash + Eq + PartialEq + Clone;
126
127	/// Returns all public keys for this application in the keystore.
128	fn all() -> crate::Vec<Self>;
129
130	/// Generate a public/private pair with an optional `seed` and store it in the keystore.
131	///
132	/// The `seed` needs to be valid utf8.
133	///
134	/// Returns the generated public key.
135	fn generate_pair(seed: Option<Vec<u8>>) -> Self;
136
137	/// Sign the given message with the corresponding private key of this public key.
138	///
139	/// The private key will be requested from the keystore.
140	///
141	/// Returns the signature or `None` if the private key could not be found or some other error
142	/// occurred.
143	fn sign<M: AsRef<[u8]>>(&self, msg: &M) -> Option<Self::Signature>;
144
145	/// Verify that the given signature matches the given message using this public key.
146	fn verify<M: AsRef<[u8]>>(&self, msg: &M, signature: &Self::Signature) -> bool;
147
148	/// Returns `Self` as raw vec.
149	fn to_raw_vec(&self) -> Vec<u8>;
150}
151
152/// Something that bound to a fixed `RuntimeAppPublic`.
153pub trait BoundToRuntimeAppPublic {
154	/// The `RuntimeAppPublic` this type is bound to.
155	type Public: RuntimeAppPublic;
156}