1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
// This file is part of Astar.
// Copyright (C) Stake Technologies Pte.Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later
// Astar 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.
// Astar 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 Astar. If not, see <http://www.gnu.org/licenses/>.
use scale_info::prelude::format;
use sp_core::{Pair, Public};
use sp_runtime::traits::{IdentifyAccount, Verify};
use super::{AccountId, Signature};
type AccountPublic = <Signature as Verify>::Signer;
/// Helper function to generate a crypto pair from seed
pub fn get_from_seed<TPublic: Public>(seed: &str) -> <TPublic::Pair as Pair>::Public {
TPublic::Pair::from_string(&format!("//{}", seed), None)
.expect("static values are valid; qed")
.public()
}
/// Helper function to generate an account ID from seed
pub fn get_account_id_from_seed<TPublic: Public>(seed: &str) -> AccountId
where
AccountPublic: From<<TPublic::Pair as Pair>::Public>,
{
AccountPublic::from(get_from_seed::<TPublic>(seed)).into_account()
}
/// Helper struct for genesis configuration.
#[derive(Clone, PartialEq, Eq)]
pub struct GenesisAccount<TPublic: Public> {
/// Account ID
pub account_id: AccountId,
/// Public key
pub pub_key: <TPublic::Pair as Pair>::Public,
}
impl<TPublic: Public> GenesisAccount<TPublic>
where
AccountPublic: From<<TPublic::Pair as Pair>::Public>,
{
/// Create a new genesis account from a seed.
pub fn from_seed(seed: &str) -> Self {
let pub_key = get_from_seed::<TPublic>(seed);
let account_id = AccountPublic::from(pub_key.clone()).into_account();
Self {
account_id,
pub_key,
}
}
/// Return the `account Id` (address) of the genesis account.
pub fn account_id(&self) -> AccountId {
self.account_id.clone()
}
/// Return the `public key` of the genesis account.
pub fn pub_key(&self) -> <TPublic::Pair as Pair>::Public {
self.pub_key.clone()
}
}