Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
9 changes: 9 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions substrate/runtime/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ substrate-runtime-std = { path = "../../runtime-std", default_features = false }
substrate-runtime-io = { path = "../../runtime-io", default_features = false }
substrate-runtime-support = { path = "../../runtime-support", default_features = false }
log = {version = "0.3", optional = true }
base58 = "0.1"
byte-num = "0.1"

[dev-dependencies]
serde_json = "1.0"
Expand Down
74 changes: 74 additions & 0 deletions substrate/runtime/primitives/src/address_format.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright 2017 Parity Technologies (UK) Ltd.
// This file is part of Substrate Demo.

// Substrate Demo is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Substrate Demo is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Substrate Demo. If not, see <http://www.gnu.org/licenses/>.

use base58::ToBase58;
use byte_num::convert::IntoAscii;
use testing::Digest;
use substrate_primitives::H256;
use generic::Digest as GenericDigest;


#[derive(Clone, Debug)]
pub struct Base58Compatible ( pub Vec<u8>);

pub trait Base58 : Into<Base58Compatible> {
fn encode(self) -> String {
self.into().0.to_base58()
}
}

impl From<u64> for Base58Compatible {
fn from(x: u64) -> Self {
Base58Compatible(x.itoa())
}
}

impl Base58 for u64 {}

impl From<Digest> for Base58Compatible {
fn from(x: Digest) -> Self {
Base58Compatible(x.logs.iter().map(|digest| digest.itoa()).flatten().collect())
}
}
impl Base58 for Digest {}

impl From<H256> for Base58Compatible {
fn from(x: H256) -> Self {
Base58Compatible(x[..].to_vec())
}
}

impl Base58 for H256 {}

impl<Item: Into<u8>> From<GenericDigest<Item>> for Base58Compatible where
Vec<u8> : From<Vec<Item>>
{
fn from(x: GenericDigest<Item>) -> Self {
Base58Compatible(x.into())
}
}

impl<Item: Into<u8>> Base58 for GenericDigest<Item> where
Vec<u8>: From<Vec<Item>>
{}

impl<Item: Into<u8>> From<GenericDigest<Item>> for Vec<u8>
{
fn from(x: GenericDigest<Item>) -> Self {
x.logs.into_iter().map(|element| Into::<u8>::into(element)).collect()
}

}
2 changes: 1 addition & 1 deletion substrate/runtime/primitives/src/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ impl<Number, Hash, DigestItem> Encode for Header<Number, Hash, DigestItem> where
impl<Number, Hash, DigestItem> traits::Header for Header<Number, Hash, DigestItem> where
Number: Member + ::rstd::hash::Hash + Copy + Codec + MaybeDisplay + SimpleArithmetic + Codec,
Hash: HashT,
DigestItem: Member + Default + Codec,
DigestItem: Member + Default + Codec + Into<Vec<u8>>,
Hash::Output: Default + ::rstd::hash::Hash + Copy + Member + MaybeDisplay + SimpleBitOps + Codec,
{
type Number = Number;
Expand Down
3 changes: 3 additions & 0 deletions substrate/runtime/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ extern crate substrate_runtime_io as runtime_io;
extern crate substrate_runtime_support as runtime_support;
extern crate substrate_codec as codec;
extern crate substrate_primitives;
extern crate base58;
extern crate byte_num;

#[cfg(test)]
extern crate serde_json;
Expand All @@ -59,6 +61,7 @@ pub mod testing;
pub mod traits;
pub mod generic;
pub mod bft;
pub mod address_format;

use traits::{Verify, Lazy};

Expand Down
1 change: 1 addition & 0 deletions substrate/runtime/primitives/src/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use std::fmt::Debug;
use codec::Codec;
use runtime_support::AuxDispatchable;
use traits::{self, Checkable, Applyable, BlakeTwo256};
use address_format::Base58;

pub use substrate_primitives::H256;

Expand Down
3 changes: 2 additions & 1 deletion substrate/runtime/primitives/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub use integer_sqrt::IntegerSquareRoot;
pub use num_traits::{Zero, One, Bounded};
pub use num_traits::ops::checked::{CheckedAdd, CheckedSub, CheckedMul, CheckedDiv};
use rstd::ops::{Add, Sub, Mul, Div, Rem, AddAssign, SubAssign, MulAssign, DivAssign, RemAssign};
use address_format::Base58;

/// A lazy value.
pub trait Lazy<T: ?Sized> {
Expand Down Expand Up @@ -426,4 +427,4 @@ pub trait Applyable: Sized + Send + Sync {
fn index(&self) -> &Self::Index;
fn sender(&self) -> &Self::AccountId;
fn apply(self) -> Result<(), &'static str>;
}
}
1 change: 1 addition & 0 deletions substrate/runtime/staking/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ authors = ["Parity Technologies <admin@parity.io>"]

[dependencies]
hex-literal = "0.1.0"
base58 = "0.1"
serde = { version = "1.0", default_features = false }
serde_derive = { version = "1.0", optional = true }
safe-mix = { version = "1.0", default_features = false}
Expand Down
59 changes: 46 additions & 13 deletions substrate/runtime/staking/src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@
#[cfg(feature = "std")]
use std::fmt;
use super::{Member, Decode, Encode, As, Input, Output};
use primitives::address_format::{Base58, Base58Compatible};


/// A vetted and verified extrinsic from the external world.
#[derive(PartialEq, Eq, Clone)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug, Hash))]
pub enum Address<AccountId, AccountIndex> where
AccountId: Member,
AccountIndex: Member,
AccountId: Member + Base58,
AccountIndex: Member + Base58,
{
/// It's an account ID (pubkey).
#[cfg_attr(feature = "std", serde(deserialize_with="AccountId::deserialize"))]
Expand All @@ -37,17 +39,18 @@ pub enum Address<AccountId, AccountIndex> where

#[cfg(feature = "std")]
impl<AccountId, AccountIndex> fmt::Display for Address<AccountId, AccountIndex> where
AccountId: Member,
AccountIndex: Member,
AccountId: Member + Sized + Base58 + Into<Vec<u8>> + Default + fmt::Debug + Sync,
AccountIndex: Member + Sized + Base58 + Into<Vec<u8>> + fmt::Debug + Sync,
Vec<u8>: From<AccountIndex>
{
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(f, "{:?}", self)
write!(f, "{:?}", &self.clone().encode())
}
}

impl<AccountId, AccountIndex> From<AccountId> for Address<AccountId, AccountIndex> where
AccountId: Member,
AccountIndex: Member,
AccountId: Member + Base58,
AccountIndex: Member + Base58,
{
fn from(a: AccountId) -> Self {
Address::Id(a)
Expand All @@ -59,8 +62,8 @@ fn need_more_than<T: PartialOrd>(a: T, b: T) -> Option<T> {
}

impl<AccountId, AccountIndex> Decode for Address<AccountId, AccountIndex> where
AccountId: Member + Decode,
AccountIndex: Member + Decode + PartialOrd<AccountIndex> + Ord + As<u32> + As<u16> + As<u8> + Copy,
AccountId: Member + Base58 + Decode,
AccountIndex: Member + Base58 + Decode + PartialOrd<AccountIndex> + Ord + As<u32> + As<u16> + As<u8> + Copy,
{
fn decode<I: Input>(input: &mut I) -> Option<Self> {
Some(match input.read_byte()? {
Expand All @@ -75,8 +78,8 @@ impl<AccountId, AccountIndex> Decode for Address<AccountId, AccountIndex> where
}

impl<AccountId, AccountIndex> Encode for Address<AccountId, AccountIndex> where
AccountId: Member + Encode,
AccountIndex: Member + Encode + PartialOrd<AccountIndex> + Ord + As<u32> + As<u16> + As<u8> + Copy,
AccountId: Member + Base58 + Encode,
AccountIndex: Member + Base58 + Encode + PartialOrd<AccountIndex> + Ord + As<u32> + As<u16> + As<u8> + Copy,
{
fn encode_to<T: Output>(&self, dest: &mut T) {
match *self {
Expand All @@ -102,10 +105,40 @@ impl<AccountId, AccountIndex> Encode for Address<AccountId, AccountIndex> where
}

impl<AccountId, AccountIndex> Default for Address<AccountId, AccountIndex> where
AccountId: Member + Default,
AccountIndex: Member,
AccountId: Member + Base58 + Default,
AccountIndex: Member + Base58,
{
fn default() -> Self {
Address::Id(Default::default())
}
}

impl<AccountId, AccountIndex> Into<Vec<u8>> for Address<AccountId, AccountIndex> where
AccountId: Member + Base58 + Into<Vec<u8>> + Default + fmt::Debug + Sync,
AccountIndex: Member + Base58 + Into<Vec<u8>> + fmt::Debug + Sync,
Vec<u8>: From<AccountIndex>

{
fn into(self) -> Vec<u8> {
match self {
Address::Id(id) => Into::<Vec<u8>>::into(id),
Address::Index(id) => Into::<Vec<u8>>::into(id)
}
}
}

impl<AccountId, AccountIndex> Base58 for Address<AccountId, AccountIndex> where
AccountId: Member + Base58 + Into<Vec<u8>> + Default + fmt::Debug + Sync,
AccountIndex: Member + Base58 + Into<Vec<u8>> + fmt::Debug + Sync,
Vec<u8>: From<AccountIndex>
{}

impl<AccountId, AccountIndex> Into<Base58Compatible> for Address<AccountId, AccountIndex> where
AccountId: Member + Base58 + Into<Vec<u8>> + Default + fmt::Debug + Sync,
AccountIndex: Member + Base58 + Into<Vec<u8>> + fmt::Debug + Sync,
Vec<u8>: From<AccountIndex>
{
fn into(self) -> Base58Compatible {
Base58Compatible(self.into())
}
}
9 changes: 7 additions & 2 deletions substrate/runtime/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ extern crate serde_derive;
#[cfg(test)]
extern crate wabt;

extern crate base58;

#[macro_use]
extern crate substrate_runtime_support as runtime_support;

Expand All @@ -53,6 +55,7 @@ use runtime_support::dispatch::Result;
use session::OnSessionChange;
use primitives::traits::{Zero, One, Bounded, RefInto, SimpleArithmetic, Executable, MakePayment,
As, AuxLookup, Member, CheckedAdd, CheckedSub};
use primitives::address_format::Base58;
use address::Address as RawAddress;

mod mock;
Expand Down Expand Up @@ -103,7 +106,7 @@ pub trait Trait: system::Trait + session::Trait {
type Balance: Parameter + SimpleArithmetic + Codec + Default + Copy + As<Self::AccountIndex> + As<usize> + As<u64>;
/// Type used for storing an account's index; implies the maximum number of accounts the system
/// can hold.
type AccountIndex: Parameter + Member + Codec + SimpleArithmetic + As<u8> + As<u16> + As<u32> + As<u64> + As<usize> + Copy;
type AccountIndex: Parameter + Member + Codec + SimpleArithmetic + As<u8> + As<u16> + As<u32> + As<u64> + As<usize> + Copy + Base58;
/// A function which is invoked when the given account is dead.
///
/// Gives a chance to clean up resources associated with the given account.
Expand Down Expand Up @@ -845,7 +848,9 @@ impl<T: Trait> OnSessionChange<T::Moment, T::AccountId> for Module<T> {
}
}

impl<T: Trait> AuxLookup for Module<T> {
impl<T: Trait> AuxLookup for Module<T>
where <T as system::Trait>::AccountId: primitives::address_format::Base58
{
type Source = address::Address<T::AccountId, T::AccountIndex>;
type Target = T::AccountId;
fn lookup(a: Self::Source) -> result::Result<Self::Target, &'static str> {
Expand Down
5 changes: 3 additions & 2 deletions substrate/runtime/system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ extern crate safe_mix;
use rstd::prelude::*;
use primitives::traits::{self, CheckEqual, SimpleArithmetic, SimpleBitOps, Zero, One, Bounded,
Hash, Member, MaybeDisplay};
use primitives::address_format::Base58;
use runtime_support::{StorageValue, StorageMap, Parameter};
use safe_mix::TripletMix;

Expand Down Expand Up @@ -68,7 +69,7 @@ pub trait Trait: Eq + Clone {
type Hash: Parameter + Member + MaybeDisplay + SimpleBitOps + Default + Copy + CheckEqual + rstd::hash::Hash + AsRef<[u8]>;
type Hashing: Hash<Output = Self::Hash>;
type Digest: Parameter + Member + Default + traits::Digest;
type AccountId: Parameter + Member + MaybeDisplay + Ord + Default;
type AccountId: Parameter + Member + MaybeDisplay + Ord + Default + Base58;
type Header: Parameter + traits::Header<
Number = Self::BlockNumber,
Hash = Self::Hash,
Expand Down Expand Up @@ -212,7 +213,7 @@ impl<T: Trait> primitives::BuildStorage for GenesisConfig<T>

Ok(map![
Self::hash(&<BlockHash<T>>::key_for(T::BlockNumber::zero())).to_vec() => [69u8; 32].encode(),
Self::hash(<Number<T>>::key()).to_vec() => 1u64.encode(),
Self::hash(<Number<T>>::key()).to_vec() => Encode::encode(&1u64),
Self::hash(<ParentHash<T>>::key()).to_vec() => [69u8; 32].encode(),
Self::hash(<RandomSeed<T>>::key()).to_vec() => [0u8; 32].encode(),
Self::hash(<ExtrinsicIndex<T>>::key()).to_vec() => [0u8; 4].encode()
Expand Down