Skip to content

Commit

Permalink
Update near deps to 0.26 (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChaoticTempest authored Oct 9, 2024
1 parent d5dd602 commit 6ffee40
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 47 deletions.
4 changes: 2 additions & 2 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ anyhow = "1"
serde_json = "1"
tokio = { version = "1", features = ["full"] }
near-account-id = "1.0.0"
near-crypto = "0.23"
near-crypto = "0.26"
near-fetch = { path = "../near-fetch" }
near-primitives = "0.23"
near-primitives = "0.26"

[[example]]
name = "various_views"
Expand Down
14 changes: 7 additions & 7 deletions near-fetch/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "near-fetch"
version = "0.5.1"
version = "0.6.0"
edition = "2021"
license = "MIT OR Apache-2.0"
readme = "../README.md"
Expand All @@ -20,12 +20,12 @@ tokio = { version = "1", features = ["full"] }
tokio-retry = "0.3"

near-account-id = "1"
near-crypto = "0.23"
near-gas = { version = "0.2", features = ["serde", "borsh", "schemars"] }
near-primitives = "0.23"
near-token = "0.2"
near-jsonrpc-primitives = "0.23"
near-jsonrpc-client = { version = "0.10.1", default-features = false }
near-crypto = "0.26"
near-gas = { version = "0.3", features = ["serde", "borsh", "schemars"] }
near-primitives = "0.26"
near-token = "0.3"
near-jsonrpc-primitives = "0.26"
near-jsonrpc-client = { version = "0.13", default-features = false }

[features]
default = ["rustls-tls"]
Expand Down
48 changes: 26 additions & 22 deletions near-fetch/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use near_jsonrpc_primitives::types::query::QueryResponseKind;
use near_jsonrpc_primitives::types::transactions::RpcTransactionError;
use near_primitives::errors::{ActionError, ActionErrorKind, InvalidTxError, TxExecutionError};
use near_primitives::hash::CryptoHash;
use near_primitives::transaction::{Action, Transaction};
use near_primitives::transaction::{Action, SignedTransaction, Transaction};
use near_primitives::types::{BlockHeight, Finality, Nonce};
use near_primitives::views::{
AccessKeyView, ExecutionStatusView, FinalExecutionOutcomeView, FinalExecutionOutcomeViewEnum,
Expand Down Expand Up @@ -95,6 +95,28 @@ impl Client {
}
}

pub(crate) async fn sign_tx(
&self,
signer: &dyn SignerExt,
receiver_id: &AccountId,
actions: Vec<Action>,
) -> Result<SignedTransaction> {
let pk = signer.public_key();
let (nonce, block_hash, _) = self.fetch_nonce(signer.account_id(), &pk).await?;

let tx = Transaction::V0(near_primitives::transaction::TransactionV0 {
nonce,
signer_id: signer.account_id().clone(),
public_key: pk,
receiver_id: receiver_id.clone(),
block_hash,
actions,
});

let signature = signer.sign(tx.get_hash_and_size().0.as_ref());
Ok(SignedTransaction::new(signature, tx))
}

/// Send the transaction only once. No retrying involved.
pub(crate) async fn send_tx_once(
&self,
Expand All @@ -104,20 +126,12 @@ impl Client {
wait_until: TxExecutionStatus,
) -> Result<FinalExecutionOutcomeView> {
let cache_key = (signer.account_id().clone(), signer.public_key());
let (nonce, block_hash, _) = self.fetch_nonce(&cache_key.0, &cache_key.1).await?;
let signed_transaction = self.sign_tx(signer, receiver_id, actions).await?;

let result = self
.rpc_client
.call(&methods::send_tx::RpcSendTransactionRequest {
signed_transaction: Transaction {
nonce,
block_hash,
signer_id: signer.account_id().clone(),
public_key: signer.public_key(),
receiver_id: receiver_id.clone(),
actions: actions.clone(),
}
.sign(signer.as_signer()),
signed_transaction,
wait_until,
})
.await;
Expand All @@ -143,17 +157,7 @@ impl Client {
// Note, the cache key's public-key part can be different per retry loop. For instance,
// KeyRotatingSigner rotates secret_key and public_key after each `Signer::sign` call.
let cache_key = (signer.account_id().clone(), signer.public_key());

let (nonce, block_hash, _) = self.fetch_nonce(&cache_key.0, &cache_key.1).await?;
let signed_transaction = Transaction {
nonce,
block_hash,
signer_id: signer.account_id().clone(),
public_key: signer.public_key(),
receiver_id: receiver_id.clone(),
actions: actions.clone(),
}
.sign(signer.as_signer());
let signed_transaction = self.sign_tx(signer, receiver_id, actions).await?;
let tx_hash = signed_transaction.get_hash();

let result = self
Expand Down
64 changes: 48 additions & 16 deletions near-fetch/src/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,12 @@ impl KeyRotatingSigner {
pub fn public_key(&self) -> &PublicKey {
&self.current_signer().public_key
}
}

impl Signer for KeyRotatingSigner {
fn sign(&self, data: &[u8]) -> Signature {
pub fn sign(&self, data: &[u8]) -> Signature {
self.fetch_and_rotate_signer().sign(data)
}

fn public_key(&self) -> PublicKey {
self.current_signer().public_key()
}

fn compute_vrf_with_proof(&self, data: &[u8]) -> (vrf::Value, vrf::Proof) {
pub fn compute_vrf_with_proof(&self, data: &[u8]) -> (vrf::Value, vrf::Proof) {
self.current_signer().compute_vrf_with_proof(data)
}
}
Expand All @@ -82,21 +76,59 @@ impl ExposeAccountId for InMemorySigner {
}
}

impl ExposeAccountId for Signer {
fn account_id(&self) -> &AccountId {
match self {
Signer::InMemory(signer) => signer.account_id(),
Signer::Empty(_) => unimplemented!(),
}
}
}

impl ExposeAccountId for KeyRotatingSigner {
fn account_id(&self) -> &AccountId {
self.current_signer().account_id()
}
}

/// A trait for extending the [`Signer`] trait with additional functionality.
pub trait SignerExt: Signer + ExposeAccountId {
fn as_signer(&self) -> &dyn Signer;
pub trait SignerExt: ExposeAccountId + Send + Sync {
fn sign(&self, data: &[u8]) -> Signature;
fn public_key(&self) -> PublicKey;
}
impl<T> SignerExt for T
where
T: Signer + ExposeAccountId,
{
fn as_signer(&self) -> &dyn Signer {
self

impl SignerExt for InMemorySigner {
fn sign(&self, data: &[u8]) -> Signature {
InMemorySigner::sign(self, data)
}

fn public_key(&self) -> PublicKey {
InMemorySigner::public_key(self).clone()
}
}

impl SignerExt for Signer {
fn sign(&self, data: &[u8]) -> Signature {
match self {
Signer::InMemory(signer) => signer.sign(data),
Signer::Empty(_) => unimplemented!(),
}
}

fn public_key(&self) -> PublicKey {
match self {
Signer::InMemory(signer) => signer.public_key.clone(),
Signer::Empty(_) => unimplemented!(),
}
}
}

impl SignerExt for KeyRotatingSigner {
fn sign(&self, data: &[u8]) -> Signature {
KeyRotatingSigner::sign(self, data)
}

fn public_key(&self) -> PublicKey {
KeyRotatingSigner::public_key(self).clone()
}
}

0 comments on commit 6ffee40

Please sign in to comment.