1use crate::{
22 Api,
23 backtrace::Backtrace,
24 config::GearConfig,
25 result::Result,
26 signer::{calls::SignerCalls, storage::SignerStorage},
27};
28use core::ops::Deref;
29pub use pair_signer::PairSigner;
30use rpc::SignerRpc;
31use sp_core::{Pair as PairT, crypto::Ss58Codec, sr25519::Pair};
32use sp_runtime::AccountId32;
33use std::sync::Arc;
34
35mod calls;
36mod pair_signer;
37mod rpc;
38mod storage;
39mod utils;
40
41#[derive(Clone)]
47pub struct Signer(Arc<Inner>);
48
49#[derive(Clone)]
51pub struct Inner {
52 api: Api,
53 signer: PairSigner<GearConfig, Pair>,
55 nonce: Option<u64>,
56 backtrace: Backtrace,
57}
58
59impl Signer {
60 pub fn backtrace(&self) -> Backtrace {
62 self.0.backtrace.clone()
63 }
64
65 pub fn new(api: Api, suri: &str, passwd: Option<&str>) -> Result<Self> {
67 Ok(Self::from((
68 api,
69 PairSigner::new(Pair::from_string(suri, passwd)?),
70 )))
71 }
72
73 pub fn change(mut self, suri: &str, passwd: Option<&str>) -> Result<Self> {
75 Arc::make_mut(&mut self.0).signer = PairSigner::new(Pair::from_string(suri, passwd)?);
76
77 Ok(self)
78 }
79
80 pub fn set_nonce(&mut self, nonce: u64) {
82 Arc::make_mut(&mut self.0).nonce = Some(nonce);
83 }
84}
85
86impl Inner {
87 pub fn storage(&self) -> SignerStorage<'_> {
89 SignerStorage(self)
90 }
91
92 pub fn rpc(&self) -> SignerRpc<'_> {
94 SignerRpc(self)
95 }
96
97 pub fn calls(&self) -> SignerCalls<'_> {
99 SignerCalls(self)
100 }
101
102 pub fn address(&self) -> String {
104 self.account_id().to_ss58check()
105 }
106
107 pub fn account_id(&self) -> &AccountId32 {
109 self.signer.account_id()
110 }
111
112 pub fn api(&self) -> &Api {
114 &self.api
115 }
116
117 pub fn signer(&self) -> &PairSigner<GearConfig, Pair> {
118 &self.signer
119 }
120}
121
122impl From<(Api, PairSigner<GearConfig, Pair>)> for Signer {
123 fn from((api, signer): (Api, PairSigner<GearConfig, Pair>)) -> Self {
124 Self(
125 Inner {
126 api,
127 signer,
128 nonce: None,
129 backtrace: Backtrace::default(),
130 }
131 .into(),
132 )
133 }
134}
135
136impl Deref for Signer {
137 type Target = Inner;
138
139 fn deref(&self) -> &Inner {
140 &self.0
141 }
142}