domain_test_service/
keyring.rs

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
//! Set of test accounts.
use fp_account::AccountId20;
use sp_core::ecdsa::{Pair, Public, Signature};
use sp_core::{ecdsa, keccak_256, Pair as PairT};

#[allow(missing_docs)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Keyring {
    Alice,
    Bob,
    Charlie,
    Dave,
    Eve,
    Ferdie,
    One,
    Two,
}

impl Keyring {
    /// Sign `msg`.
    pub fn sign(self, msg: &[u8]) -> Signature {
        let msg = keccak_256(msg);
        self.pair().sign_prehashed(&msg)
    }

    /// Return key pair.
    pub fn pair(self) -> Pair {
        ecdsa::Pair::from_string(self.to_seed().as_str(), None).unwrap()
    }

    /// Return public key.
    pub fn public(self) -> Public {
        self.pair().public()
    }

    /// Return seed string.
    pub fn to_seed(self) -> String {
        format!("//{:?}", self)
    }

    /// Return account id
    pub fn to_account_id(self) -> AccountId20 {
        self.public().into()
    }
}