near_api/signer/
access_keyfile_signer.rs

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
use std::path::PathBuf;

use near_crypto::{PublicKey, SecretKey};
use near_primitives::{hash::CryptoHash, transaction::Transaction, types::Nonce};
use tracing::{debug, instrument, trace};

use super::{AccountKeyPair, SignerTrait};
use crate::{
    errors::{AccessKeyFileError, SignerError},
    types::transactions::PrepopulateTransaction,
};

const ACCESS_KEYFILE_SIGNER_TARGET: &str = "near_api::signer::access_keyfile";

#[derive(Debug, Clone)]
pub struct AccessKeyFileSigner {
    keypair: AccountKeyPair,
}

impl AccessKeyFileSigner {
    #[instrument(skip(path), fields(path = %path.display()))]
    pub fn new(path: PathBuf) -> Result<Self, AccessKeyFileError> {
        let keypair = AccountKeyPair::load_access_key_file(&path)?;
        debug!(target: ACCESS_KEYFILE_SIGNER_TARGET, "Access key file loaded successfully");

        Ok(Self { keypair })
    }
}

#[async_trait::async_trait]
impl SignerTrait for AccessKeyFileSigner {
    #[instrument(skip(self, tr), fields(signer_id = %tr.signer_id, receiver_id = %tr.receiver_id))]
    fn tx_and_secret(
        &self,
        tr: PrepopulateTransaction,
        public_key: PublicKey,
        nonce: Nonce,
        block_hash: CryptoHash,
    ) -> Result<(Transaction, SecretKey), SignerError> {
        debug!(target: ACCESS_KEYFILE_SIGNER_TARGET, "Creating transaction");
        let mut transaction = Transaction::new_v0(
            tr.signer_id.clone(),
            public_key,
            tr.receiver_id,
            nonce,
            block_hash,
        );
        *transaction.actions_mut() = tr.actions;

        trace!(target: ACCESS_KEYFILE_SIGNER_TARGET, "Transaction created, returning with secret key");
        Ok((transaction, self.keypair.private_key.to_owned()))
    }

    #[instrument(skip(self))]
    fn get_public_key(&self) -> Result<PublicKey, SignerError> {
        debug!(target: ACCESS_KEYFILE_SIGNER_TARGET, "Retrieving public key");
        Ok(self.keypair.public_key.clone())
    }
}