Add serde feature flag to primitives (#13027)

* add serde_full feature flag

add serde_full to sp_runtime

add space to toml

add serde_full to application-crypto

add serde_full to arithmetic

fix arithmetic

add serde full to beefy

add serde full to consensus

add serde_full to core

add serdefull to finality grandpa

add serde_full to several primitives crates

make rpc no_std compatible

add scale info to runtime

make serializer no_std compatible

add serde full to storage

add full serde to version

add serde full to weights

add all serde_full features

add . to comment

add missing impl-serde

fix no-std build

fix build

add full_crypto to serde_full

serde_full also implements crypto

full_serde does not work with full_crytpo. needs std

no no_std serde impl possible

also for crypto std is necessary

no serde full for application crypto

fix arithmetic

fix tomls

fix some things

impl fmt for Signature

add serialize to Public

add impl_maybe_marker_serde_full

fix sp-application-crypto toml

add serde feature flag

fix clippy

fix toml grandpa

fix grandpa

rename if_std to if_serde

keystore is not no_std compatible

make keystore vrf no_std compatible

fix nopos-elections

fix rpc

fix serializer

fix test-primitives

fix version

add comment

add serde full only import for format string

remove all(serde_full and full_crypot) as serde_full enforces full_crypto

make comment better readable

even better comment

clean up rpc toml

clean up toml

clean up serializer toml

clean up storage toml

fix std build

update .lock

fix sp-version

move sp_std import

test extern crate alloc

replace sp_std with core

add missing core

sp_core: serde feature do not enforce full crypto

application-crypto: serde feature do not enforce full crypto

rename serde_full to serde

add dep:serde and alloc to default feature

add full_crypto and remove unnecessary debu/fmt impls for serde

update comment

remove obolsete change in display AccountId32

remove extra changes

minimize diff

revert keystore changes

remove std from keystore

remove full-crypto feature

fix serde import

fix comment

fix feature = serde

* rename serde_full to serde

* move #[doc(hidden)] back

* remove feature = full crypto require frm MultiSigner

* reorder serde and scale_info import

* fix bs58 missing alloc import in serde feature

* add `from_string` to serde feature and add unimplemented

* remove serde feature from fixed_point display

* Remove serde/alloc

Co-authored-by: Davide Galassi <davxy@datawok.net>

* Update primitives/consensus/babe/Cargo.toml

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update primitives/arithmetic/src/fixed_point.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* revert `from_string`fixed impl back to std only

* remove duplicate runtime string impl

* use sp_std::alloc

* remove no_std compatible rpc

* remove no_std compatibility from serializer

* rename mpl_maybe_marker_serde to std_or_serde

* update .lock

* add sp-std to executor

* fix sp-std import

* fix sp_std::format import

* use crate import

* add serde feature

* Update primitives/core/src/lib.rs

---------

Co-authored-by: Davide Galassi <davxy@datawok.net>
Co-authored-by: Bastian Köcher <git@kchr.de>
This commit is contained in:
Bigna Härdi
2023-05-17 13:31:12 +02:00
committed by GitHub
parent e17680fde7
commit 0e49ed72aa
47 changed files with 406 additions and 206 deletions
+34 -28
View File
@@ -33,13 +33,16 @@ pub use secrecy::{ExposeSecret, SecretString};
use sp_runtime_interface::pass_by::PassByInner;
#[doc(hidden)]
pub use sp_std::ops::Deref;
#[cfg(all(not(feature = "std"), feature = "serde"))]
use sp_std::{
alloc::{format, string::String},
vec,
};
use sp_std::{hash::Hash, str, vec::Vec};
pub use ss58_registry::{from_known_address_format, Ss58AddressFormat, Ss58AddressFormatRegistry};
/// Trait to zeroize a memory buffer.
pub use zeroize::Zeroize;
#[cfg(feature = "full_crypto")]
pub use ss58_registry::{from_known_address_format, Ss58AddressFormat, Ss58AddressFormatRegistry};
/// The root phrase for our publicly known keys.
pub const DEV_PHRASE: &str =
"bottom drive obey lake curtain smoke basket hold race lonely fit walk";
@@ -49,7 +52,6 @@ pub const DEV_ADDRESS: &str = "5DfhGyQdFobKM8NsWvEeAKk5EQQgYe9AydgJ7rMB6E1EqRzV"
/// The length of the junction identifier. Note that this is also referred to as the
/// `CHAIN_CODE_LENGTH` in the context of Schnorrkel.
#[cfg(feature = "full_crypto")]
pub const JUNCTION_ID_LEN: usize = 32;
/// Similar to `From`, except that the onus is on the part of the caller to ensure
@@ -113,7 +115,7 @@ pub enum DeriveError {
/// a new secret key from an existing secret key and, in the case of `SoftRaw` and `SoftIndex`
/// a new public key from an existing public key.
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug, Encode, Decode)]
#[cfg(feature = "full_crypto")]
#[cfg(any(feature = "full_crypto", feature = "serde"))]
pub enum DeriveJunction {
/// Soft (vanilla) derivation. Public keys have a correspondent derivation.
Soft([u8; JUNCTION_ID_LEN]),
@@ -121,7 +123,7 @@ pub enum DeriveJunction {
Hard([u8; JUNCTION_ID_LEN]),
}
#[cfg(feature = "full_crypto")]
#[cfg(any(feature = "full_crypto", feature = "serde"))]
impl DeriveJunction {
/// Consume self to return a soft derive junction with the same chain code.
pub fn soften(self) -> Self {
@@ -180,7 +182,7 @@ impl DeriveJunction {
}
}
#[cfg(feature = "full_crypto")]
#[cfg(any(feature = "full_crypto", feature = "serde"))]
impl<T: AsRef<str>> From<T> for DeriveJunction {
fn from(j: T) -> DeriveJunction {
let j = j.as_ref();
@@ -208,7 +210,7 @@ impl<T: AsRef<str>> From<T> for DeriveJunction {
#[cfg_attr(not(feature = "std"), derive(Debug))]
#[derive(Clone, Copy, Eq, PartialEq)]
#[allow(missing_docs)]
#[cfg(feature = "full_crypto")]
#[cfg(any(feature = "full_crypto", feature = "serde"))]
pub enum PublicError {
#[cfg_attr(feature = "std", error("Base 58 requirement is violated"))]
BadBase58,
@@ -247,7 +249,6 @@ impl sp_std::fmt::Debug for PublicError {
///
/// See <https://docs.substrate.io/v3/advanced/ss58/>
/// for information on the codec.
#[cfg(feature = "full_crypto")]
pub trait Ss58Codec: Sized + AsMut<[u8]> + AsRef<[u8]> + ByteArray {
/// A format filterer, can be used to ensure that `from_ss58check` family only decode for
/// allowed identifiers. By default just refuses the two reserved identifiers.
@@ -256,7 +257,7 @@ pub trait Ss58Codec: Sized + AsMut<[u8]> + AsRef<[u8]> + ByteArray {
}
/// Some if the string is a properly encoded SS58Check address.
#[cfg(feature = "std")]
#[cfg(feature = "serde")]
fn from_ss58check(s: &str) -> Result<Self, PublicError> {
Self::from_ss58check_with_version(s).and_then(|(r, v)| match v {
v if !v.is_custom() => Ok(r),
@@ -266,7 +267,7 @@ pub trait Ss58Codec: Sized + AsMut<[u8]> + AsRef<[u8]> + ByteArray {
}
/// Some if the string is a properly encoded SS58Check address.
#[cfg(feature = "std")]
#[cfg(feature = "serde")]
fn from_ss58check_with_version(s: &str) -> Result<(Self, Ss58AddressFormat), PublicError> {
const CHECKSUM_LEN: usize = 2;
let body_len = Self::LEN;
@@ -321,7 +322,7 @@ pub trait Ss58Codec: Sized + AsMut<[u8]> + AsRef<[u8]> + ByteArray {
}
/// Return the ss58-check string for this key.
#[cfg(feature = "std")]
#[cfg(feature = "serde")]
fn to_ss58check_with_version(&self, version: Ss58AddressFormat) -> String {
// We mask out the upper two bits of the ident - SS58 Prefix currently only supports 14-bits
let ident: u16 = u16::from(version) & 0b0011_1111_1111_1111;
@@ -344,7 +345,7 @@ pub trait Ss58Codec: Sized + AsMut<[u8]> + AsRef<[u8]> + ByteArray {
}
/// Return the ss58-check string for this key.
#[cfg(feature = "std")]
#[cfg(feature = "serde")]
fn to_ss58check(&self) -> String {
self.to_ss58check_with_version(default_ss58_version())
}
@@ -362,16 +363,16 @@ pub trait Derive: Sized {
/// Derive a child key from a series of given junctions.
///
/// Will be `None` for public keys if there are any hard junctions in there.
#[cfg(feature = "std")]
#[cfg(feature = "serde")]
fn derive<Iter: Iterator<Item = DeriveJunction>>(&self, _path: Iter) -> Option<Self> {
None
}
}
#[cfg(feature = "std")]
#[cfg(feature = "serde")]
const PREFIX: &[u8] = b"SS58PRE";
#[cfg(feature = "std")]
#[cfg(feature = "serde")]
fn ss58hash(data: &[u8]) -> Vec<u8> {
use blake2::{Blake2b512, Digest};
@@ -382,19 +383,19 @@ fn ss58hash(data: &[u8]) -> Vec<u8> {
}
/// Default prefix number
#[cfg(feature = "std")]
static DEFAULT_VERSION: core::sync::atomic::AtomicU16 = std::sync::atomic::AtomicU16::new(
#[cfg(feature = "serde")]
static DEFAULT_VERSION: core::sync::atomic::AtomicU16 = core::sync::atomic::AtomicU16::new(
from_known_address_format(Ss58AddressFormatRegistry::SubstrateAccount),
);
/// Returns default SS58 format used by the current active process.
#[cfg(feature = "std")]
#[cfg(feature = "serde")]
pub fn default_ss58_version() -> Ss58AddressFormat {
DEFAULT_VERSION.load(std::sync::atomic::Ordering::Relaxed).into()
DEFAULT_VERSION.load(core::sync::atomic::Ordering::Relaxed).into()
}
/// Returns either the input address format or the default.
#[cfg(feature = "std")]
#[cfg(feature = "serde")]
pub fn unwrap_or_default_ss58_version(network: Option<Ss58AddressFormat>) -> Ss58AddressFormat {
network.unwrap_or_else(default_ss58_version)
}
@@ -408,9 +409,9 @@ pub fn unwrap_or_default_ss58_version(network: Option<Ss58AddressFormat>) -> Ss5
/// This will enable the node to decode ss58 addresses with this prefix.
///
/// This SS58 version/format is also only used by the node and not by the runtime.
#[cfg(feature = "std")]
#[cfg(feature = "serde")]
pub fn set_default_ss58_version(new_default: Ss58AddressFormat) {
DEFAULT_VERSION.store(new_default.into(), std::sync::atomic::Ordering::Relaxed);
DEFAULT_VERSION.store(new_default.into(), core::sync::atomic::Ordering::Relaxed);
}
#[cfg(feature = "std")]
@@ -458,6 +459,11 @@ impl<T: Sized + AsMut<[u8]> + AsRef<[u8]> + Public + Derive> Ss58Codec for T {
}
}
// Use the default implementations of the trait in serde feature.
// The std implementation is not available because of std only crate Regex.
#[cfg(all(not(feature = "std"), feature = "serde"))]
impl<T: Sized + AsMut<[u8]> + AsRef<[u8]> + Public + Derive> Ss58Codec for T {}
/// Trait used for types that are really just a fixed-length array.
pub trait ByteArray: AsRef<[u8]> + AsMut<[u8]> + for<'a> TryFrom<&'a [u8], Error = ()> {
/// The "length" of the values of this type, which is always the same.
@@ -507,7 +513,7 @@ impl ByteArray for AccountId32 {
const LEN: usize = 32;
}
#[cfg(feature = "std")]
#[cfg(feature = "serde")]
impl Ss58Codec for AccountId32 {}
impl AsRef<[u8]> for AccountId32 {
@@ -591,7 +597,7 @@ impl sp_std::fmt::Debug for AccountId32 {
}
}
#[cfg(feature = "std")]
#[cfg(feature = "serde")]
impl serde::Serialize for AccountId32 {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
@@ -601,7 +607,7 @@ impl serde::Serialize for AccountId32 {
}
}
#[cfg(feature = "std")]
#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for AccountId32 {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
@@ -1063,7 +1069,7 @@ pub trait CryptoType {
crate::RuntimeDebug,
TypeInfo,
)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct KeyTypeId(pub [u8; 4]);
impl From<u32> for KeyTypeId {
@@ -1121,7 +1127,7 @@ pub trait VrfPublic: VrfCrypto {
/// An identifier for a specific cryptographic algorithm used by a key pair
#[derive(Debug, Copy, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Encode, Decode)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CryptoTypeId(pub [u8; 4]);
/// Known key types; this also functions as a global registry of key types for projects wishing to