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
76
77
78
use crate::{
api::{config::GearConfig, Api},
keystore,
result::{Error, Result},
};
use std::ops::{Deref, DerefMut};
use subxt::{
sp_core::{crypto::Ss58Codec, sr25519::Pair, Pair as PairT},
PairSigner,
};
mod calls;
mod rpc;
mod utils;
pub struct Signer {
api: Api,
pub signer: PairSigner<GearConfig, Pair>,
}
impl Signer {
pub fn new(api: Api, suri: &str, passwd: Option<&str>) -> Result<Self> {
Ok(Self {
api,
signer: PairSigner::new(
Pair::from_string(suri, passwd).map_err(|_| Error::InvalidSecret)?,
),
})
}
pub fn cache(api: Api, passwd: Option<&str>) -> Result<Self> {
Ok(Self {
api,
signer: keystore::cache(passwd)?,
})
}
pub fn keyring(api: Api, passwd: Option<&str>) -> Result<Self> {
Ok(Self {
api,
signer: keystore::keyring(passwd)?,
})
}
pub fn try_new(api: Api, passwd: Option<&str>) -> Result<Self> {
if let Ok(s) = Self::cache(api.clone(), passwd) {
Ok(s)
} else {
Self::keyring(api, passwd)
}
}
pub fn address(&self) -> String {
self.signer.account_id().to_ss58check()
}
}
impl Deref for Signer {
type Target = Api;
fn deref(&self) -> &Self::Target {
&self.api
}
}
impl DerefMut for Signer {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.api
}
}