From 205ddec344e0e4500e97571d6ce972b34b3e9189 Mon Sep 17 00:00:00 2001 From: Gavin Wood Date: Sun, 22 Dec 2019 23:11:34 +0100 Subject: [PATCH] Twitter field for IdentityInfo (in a back-compat way) (#4476) --- substrate/frame/identity/src/lib.rs | 31 +++++++++++++-- substrate/primitives/runtime/src/traits.rs | 46 ++++++++++++++++++++++ 2 files changed, 74 insertions(+), 3 deletions(-) diff --git a/substrate/frame/identity/src/lib.rs b/substrate/frame/identity/src/lib.rs index 3fda978b13..1e98e60b5e 100644 --- a/substrate/frame/identity/src/lib.rs +++ b/substrate/frame/identity/src/lib.rs @@ -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` 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 { + fn decode(input: &mut I) -> sp_std::result::Result { + let (account, fee, fields) = Decode::decode(&mut AppendZerosInput::new(input))?; + Ok(Self { account, fee, fields }) + } +} + decl_storage! { trait Store for Module 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(|| { diff --git a/substrate/primitives/runtime/src/traits.rs b/substrate/primitives/runtime/src/traits.rs index 22cd2814e7..a40cd35882 100644 --- a/substrate/primitives/runtime/src/traits.rs +++ b/substrate/primitives/runtime/src/traits.rs @@ -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, 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]);