Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
d3af49f
Remove Default for AccountId
gavofyork Dec 2, 2021
f6827f1
More removals of default
gavofyork Dec 2, 2021
d397c5d
Update frame/authorship/src/lib.rs
gavofyork Dec 2, 2021
c446fff
Update frame/authorship/src/lib.rs
gavofyork Dec 2, 2021
19023db
Update frame/authorship/src/lib.rs
gavofyork Dec 2, 2021
80df1fb
Update frame/authorship/src/lib.rs
gavofyork Dec 2, 2021
2cbe13a
More work
gavofyork Dec 2, 2021
0930529
More work
gavofyork Dec 2, 2021
016a1ee
Remove old code
gavofyork Dec 2, 2021
594deb9
More work
gavofyork Dec 2, 2021
daa2942
pallet-asset-tx-payment
gavofyork Dec 2, 2021
0f270da
tips
gavofyork Dec 2, 2021
393ed87
sc-consensus-babe
gavofyork Dec 2, 2021
7169d84
sc-finality-grandpa
gavofyork Dec 2, 2021
a853bcc
sc-consensus-babe-rpc
gavofyork Dec 2, 2021
4cb42ff
sc-cli
gavofyork Dec 2, 2021
2a04445
make npos crates accept non-default account (#10420)
kianenigma Dec 4, 2021
452651c
more work
gavofyork Dec 4, 2021
227f360
more work
gavofyork Dec 4, 2021
345c87b
Tests build
gavofyork Dec 6, 2021
c12d8cf
Fix imonline tests
gavofyork Dec 7, 2021
2cfd1d0
Formatting
gavofyork Dec 7, 2021
b537356
Merge branch 'master' into gav-no-default-accountid
gavofyork Dec 7, 2021
f5758f1
Fixes
gavofyork Dec 7, 2021
9965af3
Merge branch 'gav-no-default-accountid' of github.com:paritytech/subs…
gavofyork Dec 7, 2021
3dc3969
Fixes
gavofyork Dec 7, 2021
b7fe115
Merge remote-tracking branch 'origin/master' into gav-no-default-acco…
gavofyork Dec 9, 2021
c9c7ba7
Fix bench
kianenigma Dec 9, 2021
ea520fa
Fixes
gavofyork Dec 9, 2021
b694599
Fixes
gavofyork Dec 9, 2021
97ec65a
Fixes
gavofyork Dec 10, 2021
b9c1e17
Fixes
gavofyork Dec 10, 2021
ddfbfa9
Fixes
gavofyork Dec 10, 2021
2316103
Formatting
gavofyork Dec 10, 2021
d302551
Fixes
gavofyork Dec 10, 2021
915bf1c
Formatting
gavofyork Dec 10, 2021
cfb1418
Fixes
gavofyork Dec 10, 2021
ed72cad
Formatting
gavofyork Dec 10, 2021
6e1b9e7
Fixes
gavofyork Dec 10, 2021
5fab98c
Formatting
gavofyork Dec 10, 2021
8eccc1d
Merge remote-tracking branch 'origin/master' into gav-no-default-acco…
gavofyork Dec 11, 2021
67492d1
Fixes
gavofyork Dec 11, 2021
b465b2d
Formatting
gavofyork Dec 11, 2021
26053ee
Merge branch 'master' into gav-no-default-accountid
gavofyork Dec 11, 2021
87351bb
Update client/keystore/src/local.rs
gavofyork Dec 12, 2021
f7939bb
Update client/finality-grandpa/src/lib.rs
gavofyork Dec 12, 2021
5e157c9
Update client/keystore/src/local.rs
gavofyork Dec 12, 2021
3557e13
Update client/keystore/src/local.rs
gavofyork Dec 12, 2021
c147de9
Update frame/staking/src/lib.rs
gavofyork Dec 12, 2021
fa596c1
Update frame/staking/src/lib.rs
gavofyork Dec 12, 2021
aa7ac28
Update primitives/runtime/src/traits.rs
gavofyork Dec 12, 2021
175596a
Formatting
gavofyork Dec 13, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Remove Default for AccountId
  • Loading branch information
gavofyork committed Dec 2, 2021
commit d3af49fdab8deac42b262ec8b4b59e21c5b2e062
2 changes: 1 addition & 1 deletion client/consensus/aura/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ use sp_consensus::{
BlockOrigin, CanAuthorWith, Environment, Error as ConsensusError, Proposer, SelectChain,
};
use sp_consensus_slots::Slot;
use sp_core::crypto::{Pair, Public};
use sp_core::crypto::{Pair, Public, ByteArray};
use sp_inherents::CreateInherentDataProviders;
use sp_keystore::{SyncCryptoStore, SyncCryptoStorePtr};
use sp_runtime::{
Expand Down
2 changes: 1 addition & 1 deletion client/consensus/babe/src/verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use sp_consensus_babe::{
make_transcript, AuthorityId, AuthorityPair, AuthoritySignature,
};
use sp_consensus_slots::Slot;
use sp_core::{Pair, Public};
use sp_core::{Pair, Public, ByteArray};
use sp_runtime::{traits::Header, DigestItem};

/// BABE verification parameters
Expand Down
19 changes: 11 additions & 8 deletions client/keystore/src/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use async_trait::async_trait;
use parking_lot::RwLock;
use sp_application_crypto::{ecdsa, ed25519, sr25519, AppKey, AppPair, IsWrappedBy};
use sp_core::{
crypto::{CryptoTypePublicPair, ExposeSecret, KeyTypeId, Pair as PairT, Public, SecretString},
crypto::{CryptoTypePublicPair, ExposeSecret, KeyTypeId, Pair as PairT, ByteArray, SecretString},
sr25519::{Pair as Sr25519Pair, Public as Sr25519Public},
Encode,
};
Expand Down Expand Up @@ -189,7 +189,8 @@ impl SyncCryptoStore for LocalKeystore {
) -> std::result::Result<Option<Vec<u8>>, TraitError> {
match key.0 {
ed25519::CRYPTO_ID => {
let pub_key = ed25519::Public::from_slice(key.1.as_slice());
let pub_key = ed25519::Public::from_slice(key.1.as_slice())
.map_err(|()| TraitError::Other("Corrupted key - Invalid size".into()))?;
let key_pair = self
.0
.read()
Expand All @@ -198,7 +199,8 @@ impl SyncCryptoStore for LocalKeystore {
key_pair.map(|k| k.sign(msg).encode()).map(Ok).transpose()
},
sr25519::CRYPTO_ID => {
let pub_key = sr25519::Public::from_slice(key.1.as_slice());
let pub_key = sr25519::Public::from_slice(key.1.as_slice())
.map_err(|()| TraitError::Other("Corrupted key - Invalid size".into()))?;
let key_pair = self
.0
.read()
Expand All @@ -207,7 +209,8 @@ impl SyncCryptoStore for LocalKeystore {
key_pair.map(|k| k.sign(msg).encode()).map(Ok).transpose()
},
ecdsa::CRYPTO_ID => {
let pub_key = ecdsa::Public::from_slice(key.1.as_slice());
let pub_key = ecdsa::Public::from_slice(key.1.as_slice())
.map_err(|()| TraitError::Other("Corrupted key - Invalid size".into()))?;
let key_pair = self
.0
.read()
Expand All @@ -223,7 +226,7 @@ impl SyncCryptoStore for LocalKeystore {
self.0
.read()
.raw_public_keys(key_type)
.map(|v| v.into_iter().map(|k| sr25519::Public::from_slice(k.as_slice())).collect())
.map(|v| v.into_iter().filter_map(|k| sr25519::Public::from_slice(k.as_slice()).ok()).collect())
.unwrap_or_default()
}

Expand All @@ -246,7 +249,7 @@ impl SyncCryptoStore for LocalKeystore {
self.0
.read()
.raw_public_keys(key_type)
.map(|v| v.into_iter().map(|k| ed25519::Public::from_slice(k.as_slice())).collect())
.map(|v| v.into_iter().filter_map(|k| ed25519::Public::from_slice(k.as_slice()).ok()).collect())
.unwrap_or_default()
}

Expand All @@ -269,7 +272,7 @@ impl SyncCryptoStore for LocalKeystore {
self.0
.read()
.raw_public_keys(key_type)
.map(|v| v.into_iter().map(|k| ecdsa::Public::from_slice(k.as_slice())).collect())
.map(|v| v.into_iter().filter_map(|k| ecdsa::Public::from_slice(k.as_slice()).ok()).collect())
.unwrap_or_default()
}

Expand Down Expand Up @@ -562,7 +565,7 @@ mod tests {

fn public_keys<Public: AppPublic>(&self) -> Result<Vec<Public>> {
self.raw_public_keys(Public::ID)
.map(|v| v.into_iter().map(|k| Public::from_slice(k.as_slice())).collect())
.map(|v| v.into_iter().filter_map(|k| Public::from_slice(k.as_slice()).ok()).collect())
}

fn generate<Pair: AppPair>(&mut self) -> Result<Pair> {
Expand Down
8 changes: 5 additions & 3 deletions frame/examples/parallel/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,15 @@ pub struct EnlistedParticipant {

impl EnlistedParticipant {
fn verify(&self, event_id: &[u8]) -> bool {
use sp_core::Public;
use sp_core::ByteArray;
use sp_runtime::traits::Verify;

match sp_core::sr25519::Signature::try_from(&self.signature[..]) {
Ok(signature) => {
let public = sp_core::sr25519::Public::from_slice(self.account.as_ref());
signature.verify(event_id, &public)
match sp_core::sr25519::Public::from_slice(self.account.as_ref()) {
Err(()) => false,
Ok(signer) => signature.verify(event_id, &signer),
}
},
_ => false,
}
Expand Down
15 changes: 9 additions & 6 deletions frame/session/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,9 @@ impl<AId> SessionHandler<AId> for Tuple {
for_tuples!(
#(
let our_keys: Box<dyn Iterator<Item=_>> = Box::new(validators.iter()
.map(|k| (&k.0, k.1.get::<Tuple::Key>(<Tuple::Key as RuntimeAppPublic>::ID)
.unwrap_or_default())));
.filter_map(|k|
(&k.0, k.1.get::<Tuple::Key>(<Tuple::Key as RuntimeAppPublic>::ID)))
);

Tuple::on_genesis_session(our_keys);
)*
Expand All @@ -328,11 +329,13 @@ impl<AId> SessionHandler<AId> for Tuple {
for_tuples!(
#(
let our_keys: Box<dyn Iterator<Item=_>> = Box::new(validators.iter()
.map(|k| (&k.0, k.1.get::<Tuple::Key>(<Tuple::Key as RuntimeAppPublic>::ID)
.unwrap_or_default())));
.filter_map(|k|
(&k.0, k.1.get::<Tuple::Key>(<Tuple::Key as RuntimeAppPublic>::ID)))
);
let queued_keys: Box<dyn Iterator<Item=_>> = Box::new(queued_validators.iter()
.map(|k| (&k.0, k.1.get::<Tuple::Key>(<Tuple::Key as RuntimeAppPublic>::ID)
.unwrap_or_default())));
.filter_map(|k|
(&k.0, k.1.get::<Tuple::Key>(<Tuple::Key as RuntimeAppPublic>::ID)))
);
Tuple::on_new_session(changed, our_keys, queued_keys);
)*
)
Expand Down
2 changes: 1 addition & 1 deletion frame/support/src/traits/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ pub trait VerifySeal<Header, Author> {
/// A session handler for specific key type.
pub trait OneSessionHandler<ValidatorId>: BoundToRuntimeAppPublic {
/// The key type expected.
type Key: Decode + Default + RuntimeAppPublic;
type Key: Decode + RuntimeAppPublic;

/// The given validator set will be used for the genesis session.
/// It is guaranteed that the given validator set will also be used
Expand Down
1 change: 0 additions & 1 deletion frame/system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,6 @@ pub mod pallet {
+ Debug
+ MaybeDisplay
+ Ord
+ Default
+ MaxEncodedLen;

/// Converting trait to take a source type and convert to `AccountId`.
Expand Down
2 changes: 1 addition & 1 deletion primitives/application-crypto/src/ecdsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,6 @@ impl RuntimePublic for Public {
}

fn to_raw_vec(&self) -> Vec<u8> {
sp_core::crypto::Public::to_raw_vec(self)
sp_core::crypto::ByteArray::to_raw_vec(self)
}
}
2 changes: 1 addition & 1 deletion primitives/application-crypto/src/ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,6 @@ impl RuntimePublic for Public {
}

fn to_raw_vec(&self) -> Vec<u8> {
sp_core::crypto::Public::to_raw_vec(self)
sp_core::crypto::ByteArray::to_raw_vec(self)
}
}
21 changes: 10 additions & 11 deletions primitives/application-crypto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub use sp_core::crypto::{DeriveJunction, Pair, SecretStringError, Ss58Codec};
#[doc(hidden)]
pub use sp_core::{
self,
crypto::{CryptoType, CryptoTypePublicPair, Derive, IsWrappedBy, Public, Wraps},
crypto::{CryptoType, CryptoTypePublicPair, Derive, IsWrappedBy, Public, Wraps, ByteArray},
RuntimeDebug,
};

Expand Down Expand Up @@ -221,7 +221,7 @@ macro_rules! app_crypto_public_full_crypto {
$crate::wrap! {
/// A generic `AppPublic` wrapper type over $public crypto; this has no specific App.
#[derive(
Clone, Default, Eq, Hash, PartialEq, PartialOrd, Ord,
Clone, Eq, Hash, PartialEq, PartialOrd, Ord,
$crate::codec::Encode,
$crate::codec::Decode,
$crate::RuntimeDebug,
Expand Down Expand Up @@ -258,7 +258,7 @@ macro_rules! app_crypto_public_not_full_crypto {
$crate::wrap! {
/// A generic `AppPublic` wrapper type over $public crypto; this has no specific App.
#[derive(
Clone, Default, Eq, PartialEq, Ord, PartialOrd,
Clone, Eq, PartialEq, Ord, PartialOrd,
$crate::codec::Encode,
$crate::codec::Decode,
$crate::RuntimeDebug,
Expand Down Expand Up @@ -301,13 +301,12 @@ macro_rules! app_crypto_public_common {
}
}

impl $crate::ByteArray for Public {
const LEN: usize = <$public>::LEN;
}
impl $crate::Public for Public {
fn from_slice(x: &[u8]) -> Self {
Self(<$public>::from_slice(x))
}

fn to_public_crypto_pair(&self) -> $crate::CryptoTypePublicPair {
$crate::CryptoTypePublicPair($crypto_type, self.to_raw_vec())
$crate::CryptoTypePublicPair($crypto_type, $crate::ByteArray::to_raw_vec(self))
}
}

Expand Down Expand Up @@ -357,7 +356,7 @@ macro_rules! app_crypto_public_common {

impl From<&Public> for $crate::CryptoTypePublicPair {
fn from(key: &Public) -> Self {
$crate::CryptoTypePublicPair($crypto_type, $crate::Public::to_raw_vec(key))
$crate::CryptoTypePublicPair($crypto_type, $crate::ByteArray::to_raw_vec(key))
}
}

Expand Down Expand Up @@ -435,7 +434,7 @@ macro_rules! app_crypto_signature_full_crypto {
($sig:ty, $key_type:expr, $crypto_type:expr) => {
$crate::wrap! {
/// A generic `AppPublic` wrapper type over $public crypto; this has no specific App.
#[derive(Clone, Default, Eq, PartialEq,
#[derive(Clone, Eq, PartialEq,
$crate::codec::Encode,
$crate::codec::Decode,
$crate::RuntimeDebug,
Expand Down Expand Up @@ -470,7 +469,7 @@ macro_rules! app_crypto_signature_not_full_crypto {
($sig:ty, $key_type:expr, $crypto_type:expr) => {
$crate::wrap! {
/// A generic `AppPublic` wrapper type over $public crypto; this has no specific App.
#[derive(Clone, Default, Eq, PartialEq,
#[derive(Clone, Eq, PartialEq,
$crate::codec::Encode,
$crate::codec::Decode,
$crate::scale_info::TypeInfo,
Expand Down
2 changes: 1 addition & 1 deletion primitives/application-crypto/src/sr25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,6 @@ impl RuntimePublic for Public {
}

fn to_raw_vec(&self) -> Vec<u8> {
sp_core::crypto::Public::to_raw_vec(self)
sp_core::crypto::ByteArray::to_raw_vec(self)
}
}
71 changes: 33 additions & 38 deletions primitives/core/src/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ pub enum 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]> + Default {
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.
fn format_is_allowed(f: Ss58AddressFormat) -> bool {
Expand All @@ -243,10 +243,7 @@ pub trait Ss58Codec: Sized + AsMut<[u8]> + AsRef<[u8]> + Default {
#[cfg(feature = "std")]
fn from_ss58check_with_version(s: &str) -> Result<(Self, Ss58AddressFormat), PublicError> {
const CHECKSUM_LEN: usize = 2;
let mut res = Self::default();

// Must decode to our type.
let body_len = res.as_mut().len();
let body_len = Self::LEN;

let data = s.from_base58().map_err(|_| PublicError::BadBase58)?;
if data.len() < 2 {
Expand Down Expand Up @@ -280,8 +277,9 @@ pub trait Ss58Codec: Sized + AsMut<[u8]> + AsRef<[u8]> + Default {
// Invalid checksum.
return Err(PublicError::InvalidChecksum)
}
res.as_mut().copy_from_slice(&data[prefix_len..body_len + prefix_len]);
Ok((res, format))

let result = Self::from_slice(&data[prefix_len..body_len + prefix_len]).map_err(|()| PublicError::BadLength)?;
Ok((result, format))
}

/// Some if the string is a properly encoded SS58Check address, optionally with
Expand Down Expand Up @@ -391,19 +389,13 @@ lazy_static::lazy_static! {
}

#[cfg(feature = "std")]
impl<T: Sized + AsMut<[u8]> + AsRef<[u8]> + Default + Derive> Ss58Codec for T {
impl<T: Sized + AsMut<[u8]> + AsRef<[u8]> + Public + Derive> Ss58Codec for T {
fn from_string(s: &str) -> Result<Self, PublicError> {
let cap = SS58_REGEX.captures(s).ok_or(PublicError::InvalidFormat)?;
let s = cap.name("ss58").map(|r| r.as_str()).unwrap_or(DEV_ADDRESS);
let addr = if let Some(stripped) = s.strip_prefix("0x") {
let d = hex::decode(stripped).map_err(|_| PublicError::InvalidFormat)?;
let mut r = Self::default();
if d.len() == r.as_ref().len() {
r.as_mut().copy_from_slice(&d);
r
} else {
return Err(PublicError::BadLength)
}
Self::from_slice(&d).map_err(|()| PublicError::BadLength)?
} else {
Self::from_ss58check(s)?
};
Expand Down Expand Up @@ -431,25 +423,15 @@ impl<T: Sized + AsMut<[u8]> + AsRef<[u8]> + Default + Derive> Ss58Codec for T {
}
}

/// Trait suitable for typical cryptographic PKI key public type.
pub trait Public:
AsRef<[u8]>
+ AsMut<[u8]>
+ Default
+ Derive
+ CryptoType
+ PartialEq
+ Eq
+ Clone
+ Send
+ Sync
+ for<'a> TryFrom<&'a [u8]>
{
/// A new instance from the given slice.
///
/// NOTE: No checking goes on to ensure this is a real public key. Only use it if
/// you are certain that the array actually is a pubkey. GIGO!
fn from_slice(data: &[u8]) -> Self;
/// 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.
const LEN: usize;

/// A new instance from the given slice that should be `Self::LEN` bytes long.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// A new instance from the given slice that should be `Self::LEN` bytes long.
/// A new instance from the given slice that should be `Self::LEN` bytes long.
///
/// Returns an error if the slice length doesn't match.

fn from_slice(data: &[u8]) -> Result<Self, ()> {
Self::try_from(data)
}

/// Return a `Vec<u8>` filled with raw data.
fn to_raw_vec(&self) -> Vec<u8> {
Expand All @@ -460,6 +442,10 @@ pub trait Public:
fn as_slice(&self) -> &[u8] {
self.as_ref()
}
}

/// Trait suitable for typical cryptographic PKI key public type.
pub trait Public: ByteArray + Derive + CryptoType + PartialEq + Eq + Clone + Send + Sync {
/// Return `CryptoTypePublicPair` from public key.
fn to_public_crypto_pair(&self) -> CryptoTypePublicPair;
}
Expand Down Expand Up @@ -487,6 +473,12 @@ impl UncheckedFrom<crate::hash::H256> for AccountId32 {
}
}

impl ByteArray for AccountId32 {
const LEN: usize = 32;


}

#[cfg(feature = "std")]
impl Ss58Codec for AccountId32 {}

Expand Down Expand Up @@ -649,9 +641,10 @@ mod dummy {

impl Derive for Dummy {}

impl Public for Dummy {
fn from_slice(_: &[u8]) -> Self {
Self
impl ByteArray for Dummy {
const LEN: usize = 0;
fn from_slice(_: &[u8]) -> Result<Self, ()> {
Ok(Self)
}
#[cfg(feature = "std")]
fn to_raw_vec(&self) -> Vec<u8> {
Expand All @@ -660,8 +653,10 @@ mod dummy {
fn as_slice(&self) -> &[u8] {
b""
}
}
impl Public for Dummy {
fn to_public_crypto_pair(&self) -> CryptoTypePublicPair {
CryptoTypePublicPair(CryptoTypeId(*b"dumm"), Public::to_raw_vec(self))
CryptoTypePublicPair(CryptoTypeId(*b"dumm"), <Self as ByteArray>::to_raw_vec(self))
}
}

Expand Down
Loading