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
}
}