Twitter field for IdentityInfo (in a back-compat way) (#4476)

This commit is contained in:
Gavin Wood
2019-12-22 23:11:34 +01:00
committed by GitHub
parent 3c70800eab
commit 205ddec344
2 changed files with 74 additions and 3 deletions
+28 -3
View File
@@ -69,7 +69,8 @@ use sp_std::prelude::*;
use sp_std::{fmt::Debug, ops::Add, iter::once};
use enumflags2::BitFlags;
use codec::{Encode, Decode};
use sp_runtime::{DispatchResult, traits::{StaticLookup, EnsureOrigin, Zero}, RuntimeDebug};
use sp_runtime::{DispatchResult, RuntimeDebug};
use sp_runtime::traits::{StaticLookup, EnsureOrigin, Zero, AppendZerosInput};
use frame_support::{
decl_module, decl_event, decl_storage, ensure, decl_error,
traits::{Currency, ReservableCurrency, OnUnbalanced, Get},
@@ -246,6 +247,7 @@ pub enum IdentityField {
Email = 0b0000000000000000000000000000000000000000000000000000000000010000,
PgpFingerprint = 0b0000000000000000000000000000000000000000000000000000000000100000,
Image = 0b0000000000000000000000000000000000000000000000000000000001000000,
Twitter = 0b0000000000000000000000000000000000000000000000000000000010000000,
}
/// Wrapper type for `BitFlags<IdentityField>` that implements `Codec`.
@@ -296,7 +298,7 @@ pub struct IdentityInfo {
/// Stored as UTF-8.
pub web: Data,
/// The Riot handle held by the controller of the account.
/// The Riot/Matrix handle held by the controller of the account.
///
/// Stored as UTF-8.
pub riot: Data,
@@ -312,6 +314,9 @@ pub struct IdentityInfo {
/// A graphic image representing the controller of the account. Should be a company,
/// organization or project logo or a headshot in the case of a human.
pub image: Data,
/// The Twitter identity. The leading `@` character may be elided.
pub twitter: Data,
}
/// Information concerning the identity of the controller of an account.
@@ -344,7 +349,7 @@ impl <
}
/// Information concerning a registrar.
#[derive(Clone, Encode, Decode, Eq, PartialEq, RuntimeDebug)]
#[derive(Clone, Encode, Eq, PartialEq, RuntimeDebug)]
pub struct RegistrarInfo<
Balance: Encode + Decode + Clone + Debug + Eq + PartialEq,
AccountId: Encode + Decode + Clone + Debug + Eq + PartialEq
@@ -360,6 +365,16 @@ pub struct RegistrarInfo<
pub fields: IdentityFields,
}
impl<
Balance: Encode + Decode + Clone + Debug + Eq + PartialEq,
AccountId: Encode + Decode + Clone + Debug + Eq + PartialEq
> Decode for RegistrarInfo<Balance, AccountId> {
fn decode<I: codec::Input>(input: &mut I) -> sp_std::result::Result<Self, codec::Error> {
let (account, fee, fields) = Decode::decode(&mut AppendZerosInput::new(input))?;
Ok(Self { account, fee, fields })
}
}
decl_storage! {
trait Store for Module<T: Trait> as Sudo {
/// Information that is pertinent to identify the entity behind an account.
@@ -958,6 +973,16 @@ mod tests {
}
}
#[test]
fn trailing_zeros_decodes_into_default_data() {
let encoded = Data::Raw(b"Hello".to_vec()).encode();
assert!(<(Data, Data)>::decode(&mut &encoded[..]).is_err());
let input = &mut &encoded[..];
let (a, b) = <(Data, Data)>::decode(&mut AppendZerosInput::new(input)).unwrap();
assert_eq!(a, Data::Raw(b"Hello".to_vec()));
assert_eq!(b, Data::None);
}
#[test]
fn adding_registrar_should_work() {
new_test_ext().execute_with(|| {
@@ -999,6 +999,52 @@ pub trait OpaqueKeys: Clone {
fn ownership_proof_is_valid(&self, _proof: &[u8]) -> bool { true }
}
/// Input that adds infinite number of zero after wrapped input.
///
/// This can add an infinite stream of zeros onto any input, not just a slice as with
/// `TrailingZerosInput`.
pub struct AppendZerosInput<'a, T>(&'a mut T);
impl<'a, T> AppendZerosInput<'a, T> {
/// Create a new instance from the given byte array.
pub fn new(input: &'a mut T) -> Self {
Self(input)
}
}
impl<'a, T: codec::Input> codec::Input for AppendZerosInput<'a, T> {
fn remaining_len(&mut self) -> Result<Option<usize>, codec::Error> {
Ok(None)
}
fn read(&mut self, into: &mut [u8]) -> Result<(), codec::Error> {
let remaining = self.0.remaining_len()?;
let completed = if let Some(n) = remaining {
let readable = into.len().min(n);
// this should never fail if `remaining_len` API is implemented correctly.
self.0.read(&mut into[..readable])?;
readable
} else {
// Fill it byte-by-byte.
let mut i = 0;
while i < into.len() {
if let Ok(b) = self.0.read_byte() {
into[i] = b;
i += 1;
} else {
break;
}
}
i
};
// Fill the rest with zeros.
for i in &mut into[completed..] {
*i = 0;
}
Ok(())
}
}
/// Input that adds infinite number of zero after wrapped input.
pub struct TrailingZeroInput<'a>(&'a [u8]);