1
// Copyright (C) Moondance Labs Ltd.
2
// This file is part of Tanssi.
3

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

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

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

            
17
pub use tc_service_container_chain::chain_spec::Extensions;
18
use {
19
    dancebox_runtime::{AccountId, Signature},
20
    nimbus_primitives::NimbusId,
21
    sp_core::{sr25519, Pair, Public},
22
    sp_runtime::traits::{IdentifyAccount, Verify},
23
};
24

            
25
pub mod dancebox;
26
pub mod flashbox;
27

            
28
/// Helper function to generate a crypto pair from seed
29
546
pub fn get_from_seed<TPublic: Public>(seed: &str) -> <TPublic::Pair as Pair>::Public {
30
546
    TPublic::Pair::from_string(&format!("//{}", seed), None)
31
546
        .expect("static values are valid; qed")
32
546
        .public()
33
546
}
34

            
35
type AccountPublic = <Signature as Verify>::Signer;
36

            
37
/// Generate collator keys from seed.
38
///
39
/// This function's return type must always match the session keys of the chain in tuple format.
40
104
pub fn get_collator_keys_from_seed(seed: &str) -> NimbusId {
41
104
    get_from_seed::<NimbusId>(seed)
42
104
}
43

            
44
/// Helper function to generate an account ID from seed
45
442
pub fn get_account_id_from_seed<TPublic: Public>(seed: &str) -> AccountId
46
442
where
47
442
    AccountPublic: From<<TPublic::Pair as Pair>::Public>,
48
442
{
49
442
    AccountPublic::from(get_from_seed::<TPublic>(seed)).into_account()
50
442
}
51

            
52
/// Helper function to turn a list of names into a list of `(AccountId, NimbusId)`
53
26
pub fn invulnerables_from_seeds<S: AsRef<str>, I: Iterator<Item = S>>(
54
26
    names: I,
55
26
) -> Vec<(AccountId, NimbusId)> {
56
26
    names
57
104
        .map(|name| {
58
104
            let name = name.as_ref();
59
104
            (
60
104
                get_account_id_from_seed::<sr25519::Public>(name),
61
104
                get_collator_keys_from_seed(name),
62
104
            )
63
104
        })
64
26
        .collect()
65
26
}
66

            
67
/// Helper function to turn a list of names into a list of `AccountId`
68
26
pub fn account_ids(names: &[&str]) -> Vec<AccountId> {
69
26
    names
70
26
        .iter()
71
312
        .map(|name| get_account_id_from_seed::<sr25519::Public>(name))
72
26
        .collect()
73
26
}