Skip to main content

nym_validator_client/signing/
tx_signer.rs

1// Copyright 2023 - Nym Technologies SA <contact@nymtech.net>
2// SPDX-License-Identifier: Apache-2.0
3
4use crate::signing::signer::{OfflineSigner, SigningError};
5use crate::signing::SignerData;
6use cosmrs::tx::{SignDoc, SignerInfo};
7use cosmrs::{tx, AccountId, Any};
8
9// extension trait for the OfflineSigner to allow to sign transactions
10pub trait TxSigner: OfflineSigner {
11    fn signer_public_key(&self, signer_address: &AccountId) -> Option<tx::SignerPublicKey> {
12        let account = self.find_account(signer_address).ok()?;
13        Some(account.public_key().into())
14    }
15
16    fn sign_amino(
17        &self,
18        _signer_address: &AccountId,
19        _messages: Vec<Any>,
20        _fee: tx::Fee,
21        _memo: impl Into<String> + Send + 'static,
22        _signer_data: SignerData,
23    ) -> Result<tx::Raw, <Self as OfflineSigner>::Error> {
24        unimplemented!()
25    }
26
27    fn sign_direct(
28        &self,
29        signer_address: &AccountId,
30        messages: Vec<Any>,
31        fee: tx::Fee,
32        memo: impl Into<String> + Send + 'static,
33        signer_data: SignerData,
34    ) -> Result<tx::Raw, <Self as OfflineSigner>::Error> {
35        let account_from_signer = self.find_account(signer_address)?;
36
37        // TODO: experiment with this field
38        let timeout_height = 0u32;
39
40        let tx_body = tx::Body::new(messages, memo, timeout_height);
41        let signer_info =
42            SignerInfo::single_direct(Some(account_from_signer.public_key), signer_data.sequence);
43        let auth_info = signer_info.auth_info(fee);
44
45        let sign_doc = SignDoc::new(
46            &tx_body,
47            &auth_info,
48            &signer_data.chain_id,
49            signer_data.account_number,
50        )
51        .map_err(|source| SigningError::SignDocFailure { source })?;
52
53        self.sign_direct_with_account(account_from_signer, sign_doc)
54    }
55}
56
57impl<T> TxSigner for T where T: OfflineSigner {}