SignerTrait

Trait SignerTrait 

Source
pub trait SignerTrait {
    // Required methods
    fn get_secret_key<'life0, 'life1, 'life2, 'async_trait>(
        &'life0 self,
        signer_id: &'life1 AccountId,
        public_key: &'life2 PublicKey,
    ) -> Pin<Box<dyn Future<Output = Result<SecretKey, SignerError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait;
    fn get_public_key(&self) -> Result<PublicKey, SignerError>;

    // Provided methods
    fn sign_meta<'life0, 'async_trait>(
        &'life0 self,
        tr: PrepopulateTransaction,
        public_key: PublicKey,
        nonce: Nonce,
        block_hash: CryptoHash,
        max_block_height: BlockHeight,
    ) -> Pin<Box<dyn Future<Output = Result<SignedDelegateAction, MetaSignError>> + Send + 'async_trait>>
       where Self: Sync + 'async_trait,
             'life0: 'async_trait { ... }
    fn sign<'life0, 'async_trait>(
        &'life0 self,
        tr: PrepopulateTransaction,
        public_key: PublicKey,
        nonce: Nonce,
        block_hash: CryptoHash,
    ) -> Pin<Box<dyn Future<Output = Result<SignedTransaction, SignerError>> + Send + 'async_trait>>
       where Self: Sync + 'async_trait,
             'life0: 'async_trait { ... }
    fn sign_message_nep413<'life0, 'async_trait>(
        &'life0 self,
        signer_id: AccountId,
        public_key: PublicKey,
        payload: NEP413Payload,
    ) -> Pin<Box<dyn Future<Output = Result<Signature, SignerError>> + Send + 'async_trait>>
       where Self: Sync + 'async_trait,
             'life0: 'async_trait { ... }
}
Expand description

A trait for implementing custom signing logic.

This trait provides the core functionality needed to sign transactions and delegate actions. It is used by the Signer to abstract over different signing methods (secret key, ledger, keystore, etc.).

§Examples

§Implementing a custom signer

use near_api::{*, signer::*, types::transaction::{PrepopulateTransaction, Transaction}, errors::SignerError};

struct CustomSigner {
    secret_key: SecretKey,
}

#[async_trait::async_trait]
impl SignerTrait for CustomSigner {
    async fn get_secret_key(
        &self,
        _signer_id: &AccountId,
        _public_key: &PublicKey
    ) -> Result<SecretKey, SignerError> {
        Ok(self.secret_key.clone())
    }

    fn get_public_key(&self) -> Result<PublicKey, SignerError> {
        Ok(self.secret_key.public_key().into())
    }
}

§Using a custom signer

let secret_key = "ed25519:2vVTQWpoZvYZBS4HYFZtzU2rxpoQSrhyFWdaHLqSdyaEfgjefbSKiFpuVatuRqax3HFvVq2tkkqWH2h7tso2nK8q".parse()?;
let custom_signer = CustomSigner::new(secret_key);
let signer = Signer::new(custom_signer)?;

§Example of implementing sign_meta and sign methods

The default implementation of sign_meta and sign methods should work for most cases. If you need to implement custom logic, you can override these methods. See near_ledger implementation for an example.

Required Methods§

Source

fn get_secret_key<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, signer_id: &'life1 AccountId, public_key: &'life2 PublicKey, ) -> Pin<Box<dyn Future<Output = Result<SecretKey, SignerError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Returns the secret key associated with this signer. This is a helper method that should be implemented by the signer or fail with SignerError. As long as this method works, the default implementation of the sign_meta and sign methods should work.

If you can’t provide a SecretKey for some reason (E.g. Ledger``), you can fail with SignerError and override sign_metaandsign, sign_message_nep413` methods.

Source

fn get_public_key(&self) -> Result<PublicKey, SignerError>

Returns the public key associated with this signer.

This method is used by the Signer to manage the pool of signing keys.

Provided Methods§

Source

fn sign_meta<'life0, 'async_trait>( &'life0 self, tr: PrepopulateTransaction, public_key: PublicKey, nonce: Nonce, block_hash: CryptoHash, max_block_height: BlockHeight, ) -> Pin<Box<dyn Future<Output = Result<SignedDelegateAction, MetaSignError>> + Send + 'async_trait>>
where Self: Sync + 'async_trait, 'life0: 'async_trait,

Signs a delegate action for meta transactions.

This method is used for meta-transactions where one account can delegate transaction delivery and gas payment to another account. The delegate action is signed with a maximum block height to ensure the delegation expiration after some point in time.

The default implementation should work for most cases.

Source

fn sign<'life0, 'async_trait>( &'life0 self, tr: PrepopulateTransaction, public_key: PublicKey, nonce: Nonce, block_hash: CryptoHash, ) -> Pin<Box<dyn Future<Output = Result<SignedTransaction, SignerError>> + Send + 'async_trait>>
where Self: Sync + 'async_trait, 'life0: 'async_trait,

Signs a regular transaction.

This method is used for standard transactions. It creates a signed transaction that can be sent to the NEAR network.

The default implementation should work for most cases.

Source

fn sign_message_nep413<'life0, 'async_trait>( &'life0 self, signer_id: AccountId, public_key: PublicKey, payload: NEP413Payload, ) -> Pin<Box<dyn Future<Output = Result<Signature, SignerError>> + Send + 'async_trait>>
where Self: Sync + 'async_trait, 'life0: 'async_trait,

Signs a NEP413 message that is widely used for the authentication and off-chain proof of account ownership.

The default implementation should work for most cases.

Implementors§