Struct InMemorySigner

Source
pub struct InMemorySigner {
    pub account_id: AccountId,
    pub public_key: PublicKey,
    pub secret_key: SecretKey,
}
Expand description

Signer that keeps secret key in memory.

Fields§

§account_id: AccountId§public_key: PublicKey§secret_key: SecretKey

Implementations§

Source§

impl InMemorySigner

Source

pub fn from_seed( account_id: AccountId, key_type: KeyType, seed: &str, ) -> InMemorySigner

Source

pub fn from_secret_key( account_id: AccountId, secret_key: SecretKey, ) -> InMemorySigner

Examples found in repository?
examples/create_account.rs (line 20)
12async fn main() -> Result<(), Box<dyn std::error::Error>> {
13    env_logger::init();
14
15    let signer_account_id: AccountId = utils::input("Enter the signer Account ID: ")?.parse()?;
16    let signer_secret_key = utils::input("Enter the signer's private key: ")?.parse()?;
17    //To-do, implement account exist check.
18    let new_account_id: AccountId = utils::input("Enter new account name: ")?.parse()?;
19
20    let signer = InMemorySigner::from_secret_key(signer_account_id.clone(), signer_secret_key);
21
22    // Amount to transfer to the new account
23    let gas: Gas = 100_000_000_000_000; // Example amount in yoctoNEAR
24    let amount: Balance = 10_000_000_000_000_000_000_000; // Example amount in yoctoNEAR
25
26    let new_secret_key = near_crypto::SecretKey::from_random(near_crypto::KeyType::ED25519);
27    let provider = Arc::new(JsonRpcProvider::new("https://rpc.testnet.near.org"));
28    let signer = Arc::new(signer);
29
30    let account = Account::new(signer_account_id, signer, provider);
31
32    let contract_id: AccountId = "testnet".parse::<AccountId>()?;
33    let method_name = "create_account".to_string();
34
35    let args_json = json!({
36        "new_account_id": new_account_id,
37        "new_public_key": new_secret_key.public_key()
38    });
39
40    let result = account
41        .function_call(contract_id, method_name, args_json, gas, amount)
42        .await;
43
44    println!("response: {:#?}", result);
45    println!("New Account ID: {}", new_account_id);
46    println!("Secret Key: {}", new_secret_key);
47
48    Ok(())
49}
More examples
Hide additional examples
examples/contract_change_method_commit.rs (line 27)
17async fn main() -> Result<(), Box<dyn std::error::Error>> {
18    env_logger::init();
19
20    let client = JsonRpcClient::connect("https://rpc.testnet.near.org");
21
22    let provider = JsonRpcProvider::new("https://rpc.testnet.near.org");
23
24    let signer_account_id = utils::input("Enter the signer Account ID: ")?.parse()?;
25    let signer_secret_key = utils::input("Enter the signer's private key: ")?.parse()?;
26
27    let signer = near_crypto::InMemorySigner::from_secret_key(signer_account_id, signer_secret_key);
28
29    let access_key_query_response = client
30        .call(methods::query::RpcQueryRequest {
31            block_reference: BlockReference::latest(),
32            request: near_primitives::views::QueryRequest::ViewAccessKey {
33                account_id: signer.account_id.clone(),
34                public_key: signer.public_key.clone(),
35            },
36        })
37        .await?;
38
39    let current_nonce = match access_key_query_response.kind {
40        QueryResponseKind::AccessKey(access_key) => access_key.nonce,
41        _ => Err("failed to extract current nonce")?,
42    };
43
44    let other_account = utils::input("Enter the account to be rated: ")?;
45    let rating = utils::input("Enter a rating: ")?.parse::<f32>()?;
46
47    let transaction = Transaction {
48        signer_id: signer.account_id.clone(),
49        public_key: signer.public_key.clone(),
50        nonce: current_nonce + 1,
51        receiver_id: "nosedive.testnet".parse()?,
52        block_hash: access_key_query_response.block_hash,
53        actions: vec![Action::FunctionCall(Box::new(FunctionCallAction {
54            method_name: "rate".to_string(),
55            args: json!({
56                "account_id": other_account,
57                "rating": rating,
58            })
59            .to_string()
60            .into_bytes(),
61            gas: 100_000_000_000_000, // 100 TeraGas
62            deposit: 0,
63        }))],
64    };
65
66    let response = provider.send_transaction(transaction.sign(&signer)).await?;
67
68    println!("response: {:#?}", response);
69
70    Ok(())
71}
Source

pub fn from_file(path: &Path) -> Result<InMemorySigner, Error>

Source§

impl InMemorySigner

Source

pub fn from_random(account_id: AccountId, key_type: KeyType) -> InMemorySigner

Trait Implementations§

Source§

impl Clone for InMemorySigner

Source§

fn clone(&self) -> InMemorySigner

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'de> Deserialize<'de> for InMemorySigner

Source§

fn deserialize<__D>( __deserializer: __D, ) -> Result<InMemorySigner, <__D as Deserializer<'de>>::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl From<KeyFile> for InMemorySigner

Source§

fn from(key_file: KeyFile) -> InMemorySigner

Converts to this type from the input type.
Source§

impl PartialEq for InMemorySigner

Source§

fn eq(&self, other: &InMemorySigner) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for InMemorySigner

Source§

fn serialize<__S>( &self, __serializer: __S, ) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Signer for InMemorySigner

Source§

fn public_key(&self) -> PublicKey

Source§

fn sign(&self, data: &[u8]) -> Signature

Source§

fn compute_vrf_with_proof(&self, data: &[u8]) -> (Value, Proof)

Source§

fn write_to_file(&self, path: &Path) -> Result<(), Error>

Used by test infrastructure, only implement if make sense for testing otherwise raise unimplemented.
Source§

fn verify(&self, data: &[u8], signature: &Signature) -> bool

Source§

impl Eq for InMemorySigner

Source§

impl StructuralPartialEq for InMemorySigner

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> ErasedDestructor for T
where T: 'static,