gsdk/signer/
mod.rs

1// This file is part of Gear.
2//
3// Copyright (C) 2021-2025 Gear Technologies Inc.
4// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
5//
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10//
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15//
16// You should have received a copy of the GNU General Public License
17// along with this program. If not, see <https://www.gnu.org/licenses/>.
18
19//! Gear api with signer
20
21use 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/// Signer representation that provides access to gear API.
42/// Implements low-level methods such as [`run_tx`](`Inner::run_tx`)
43/// and [`force_batch`](`Signer.calls()::force_batch`).
44/// Other higher-level calls are provided by [`Inner::storage`],
45/// [`Inner::calls`], [`Inner::rpc`].
46#[derive(Clone)]
47pub struct Signer(Arc<Inner>);
48
49/// Implementation of low-level calls for [`Signer`].
50#[derive(Clone)]
51pub struct Inner {
52    api: Api,
53    /// Current signer.
54    signer: PairSigner<GearConfig, Pair>,
55    nonce: Option<u64>,
56    backtrace: Backtrace,
57}
58
59impl Signer {
60    /// Get backtrace of the signer.
61    pub fn backtrace(&self) -> Backtrace {
62        self.0.backtrace.clone()
63    }
64
65    /// New signer api.
66    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    /// Change inner signer.
74    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    /// Set nonce of the signer
81    pub fn set_nonce(&mut self, nonce: u64) {
82        Arc::make_mut(&mut self.0).nonce = Some(nonce);
83    }
84}
85
86impl Inner {
87    /// Returns signer's storage calls handle.
88    pub fn storage(&self) -> SignerStorage<'_> {
89        SignerStorage(self)
90    }
91
92    /// Returns signer's RPC calls handle.
93    pub fn rpc(&self) -> SignerRpc<'_> {
94        SignerRpc(self)
95    }
96
97    /// Returns signer's calls handle.
98    pub fn calls(&self) -> SignerCalls<'_> {
99        SignerCalls(self)
100    }
101
102    /// Get address of the current signer
103    pub fn address(&self) -> String {
104        self.account_id().to_ss58check()
105    }
106
107    /// Get address of the current signer
108    pub fn account_id(&self) -> &AccountId32 {
109        self.signer.account_id()
110    }
111
112    /// Get reference to inner unsigned api
113    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}