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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// This file is part of Gear.
//
// Copyright (C) 2021-2024 Gear Technologies Inc.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

//! Gear api with signer

use crate::{
    backtrace::Backtrace,
    config::GearConfig,
    result::{Error, Result},
    Api,
};
use calls::SignerCalls;
use core::ops::Deref;
pub use pair_signer::PairSigner;
use rpc::SignerRpc;
use sp_core::{crypto::Ss58Codec, sr25519::Pair, Pair as PairT};
use sp_runtime::AccountId32;
use std::sync::Arc;
use storage::SignerStorage;

mod calls;
mod pair_signer;
mod rpc;
mod storage;
mod utils;

/// Signer representation that provides access to gear API.
/// Implements low-level methods such as [`run_tx`](`Inner::run_tx`)
/// and [`force_batch`](`Signer.calls()::force_batch`).
/// Other higher-level calls are provided by [`Signer::storage`],
/// [`Signer::calls`], [`Signer::rpc`].
#[derive(Clone)]
pub struct Signer {
    signer: Arc<Inner>,
    /// Calls that get or set storage.
    pub storage: SignerStorage,
    /// Calls for interaction with on-chain programs.
    pub calls: SignerCalls,
    /// Calls to fetch data from node.
    pub rpc: SignerRpc,
}

/// Implementation of low-level calls for [`Signer`].
#[derive(Clone)]
pub struct Inner {
    api: Api,
    /// Current signer.
    signer: PairSigner<GearConfig, Pair>,
    nonce: Option<u64>,
    backtrace: Backtrace,
}

impl Signer {
    /// Get backtrace of the signer.
    pub fn backtrace(&self) -> Backtrace {
        self.calls.0.backtrace.clone()
    }

    /// New signer api.
    pub fn new(api: Api, suri: &str, passwd: Option<&str>) -> Result<Self> {
        let signer = Inner {
            api,
            signer: PairSigner::new(
                Pair::from_string(suri, passwd).map_err(|_| Error::InvalidSecret)?,
            ),
            nonce: None,
            backtrace: Default::default(),
        };

        Ok(Self::from_inner(signer))
    }

    fn from_inner(signer: Inner) -> Self {
        let signer = Arc::new(signer);

        Self {
            storage: SignerStorage(signer.clone()),
            calls: SignerCalls(signer.clone()),
            rpc: SignerRpc(signer.clone()),
            signer,
        }
    }

    fn replace_inner(&mut self, mut inner: Inner) {
        let backtrace = self.backtrace();
        inner.backtrace = backtrace;

        let Signer {
            signer,
            storage,
            calls,
            rpc,
        } = self;

        *signer = Arc::new(inner);
        *storage = SignerStorage(signer.clone());
        *calls = SignerCalls(signer.clone());
        *rpc = SignerRpc(signer.clone());
    }

    /// Change inner signer.
    pub fn change(mut self, suri: &str, passwd: Option<&str>) -> Result<Self> {
        let signer =
            PairSigner::new(Pair::from_string(suri, passwd).map_err(|_| Error::InvalidSecret)?);

        self.replace_inner(Inner {
            signer,
            ..self.signer.as_ref().clone()
        });

        Ok(self)
    }

    /// Set nonce of the signer
    pub fn set_nonce(&mut self, nonce: u64) {
        self.replace_inner(Inner {
            nonce: Some(nonce),
            ..self.signer.as_ref().clone()
        });
    }
}

impl Inner {
    /// Get address of the current signer
    pub fn address(&self) -> String {
        self.account_id().to_ss58check()
    }

    /// Get address of the current signer
    pub fn account_id(&self) -> &AccountId32 {
        self.signer.account_id()
    }

    /// Get reference to inner unsigned api
    pub fn api(&self) -> &Api {
        &self.api
    }

    pub fn signer(&self) -> &PairSigner<GearConfig, Pair> {
        &self.signer
    }
}

impl From<(Api, PairSigner<GearConfig, Pair>)> for Signer {
    fn from((api, signer): (Api, PairSigner<GearConfig, Pair>)) -> Self {
        let signer = Inner {
            api,
            signer,
            nonce: None,
            backtrace: Backtrace::default(),
        };

        Self::from_inner(signer)
    }
}

impl Deref for Signer {
    type Target = Inner;

    fn deref(&self) -> &Inner {
        self.signer.as_ref()
    }
}