Skip to content

Commit 6ffee40

Browse files
Update near deps to 0.26 (#26)
1 parent d5dd602 commit 6ffee40

File tree

4 files changed

+83
-47
lines changed

4 files changed

+83
-47
lines changed

examples/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ anyhow = "1"
99
serde_json = "1"
1010
tokio = { version = "1", features = ["full"] }
1111
near-account-id = "1.0.0"
12-
near-crypto = "0.23"
12+
near-crypto = "0.26"
1313
near-fetch = { path = "../near-fetch" }
14-
near-primitives = "0.23"
14+
near-primitives = "0.26"
1515

1616
[[example]]
1717
name = "various_views"

near-fetch/Cargo.toml

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "near-fetch"
3-
version = "0.5.1"
3+
version = "0.6.0"
44
edition = "2021"
55
license = "MIT OR Apache-2.0"
66
readme = "../README.md"
@@ -20,12 +20,12 @@ tokio = { version = "1", features = ["full"] }
2020
tokio-retry = "0.3"
2121

2222
near-account-id = "1"
23-
near-crypto = "0.23"
24-
near-gas = { version = "0.2", features = ["serde", "borsh", "schemars"] }
25-
near-primitives = "0.23"
26-
near-token = "0.2"
27-
near-jsonrpc-primitives = "0.23"
28-
near-jsonrpc-client = { version = "0.10.1", default-features = false }
23+
near-crypto = "0.26"
24+
near-gas = { version = "0.3", features = ["serde", "borsh", "schemars"] }
25+
near-primitives = "0.26"
26+
near-token = "0.3"
27+
near-jsonrpc-primitives = "0.26"
28+
near-jsonrpc-client = { version = "0.13", default-features = false }
2929

3030
[features]
3131
default = ["rustls-tls"]

near-fetch/src/lib.rs

+26-22
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use near_jsonrpc_primitives::types::query::QueryResponseKind;
1515
use near_jsonrpc_primitives::types::transactions::RpcTransactionError;
1616
use near_primitives::errors::{ActionError, ActionErrorKind, InvalidTxError, TxExecutionError};
1717
use near_primitives::hash::CryptoHash;
18-
use near_primitives::transaction::{Action, Transaction};
18+
use near_primitives::transaction::{Action, SignedTransaction, Transaction};
1919
use near_primitives::types::{BlockHeight, Finality, Nonce};
2020
use near_primitives::views::{
2121
AccessKeyView, ExecutionStatusView, FinalExecutionOutcomeView, FinalExecutionOutcomeViewEnum,
@@ -95,6 +95,28 @@ impl Client {
9595
}
9696
}
9797

98+
pub(crate) async fn sign_tx(
99+
&self,
100+
signer: &dyn SignerExt,
101+
receiver_id: &AccountId,
102+
actions: Vec<Action>,
103+
) -> Result<SignedTransaction> {
104+
let pk = signer.public_key();
105+
let (nonce, block_hash, _) = self.fetch_nonce(signer.account_id(), &pk).await?;
106+
107+
let tx = Transaction::V0(near_primitives::transaction::TransactionV0 {
108+
nonce,
109+
signer_id: signer.account_id().clone(),
110+
public_key: pk,
111+
receiver_id: receiver_id.clone(),
112+
block_hash,
113+
actions,
114+
});
115+
116+
let signature = signer.sign(tx.get_hash_and_size().0.as_ref());
117+
Ok(SignedTransaction::new(signature, tx))
118+
}
119+
98120
/// Send the transaction only once. No retrying involved.
99121
pub(crate) async fn send_tx_once(
100122
&self,
@@ -104,20 +126,12 @@ impl Client {
104126
wait_until: TxExecutionStatus,
105127
) -> Result<FinalExecutionOutcomeView> {
106128
let cache_key = (signer.account_id().clone(), signer.public_key());
107-
let (nonce, block_hash, _) = self.fetch_nonce(&cache_key.0, &cache_key.1).await?;
129+
let signed_transaction = self.sign_tx(signer, receiver_id, actions).await?;
108130

109131
let result = self
110132
.rpc_client
111133
.call(&methods::send_tx::RpcSendTransactionRequest {
112-
signed_transaction: Transaction {
113-
nonce,
114-
block_hash,
115-
signer_id: signer.account_id().clone(),
116-
public_key: signer.public_key(),
117-
receiver_id: receiver_id.clone(),
118-
actions: actions.clone(),
119-
}
120-
.sign(signer.as_signer()),
134+
signed_transaction,
121135
wait_until,
122136
})
123137
.await;
@@ -143,17 +157,7 @@ impl Client {
143157
// Note, the cache key's public-key part can be different per retry loop. For instance,
144158
// KeyRotatingSigner rotates secret_key and public_key after each `Signer::sign` call.
145159
let cache_key = (signer.account_id().clone(), signer.public_key());
146-
147-
let (nonce, block_hash, _) = self.fetch_nonce(&cache_key.0, &cache_key.1).await?;
148-
let signed_transaction = Transaction {
149-
nonce,
150-
block_hash,
151-
signer_id: signer.account_id().clone(),
152-
public_key: signer.public_key(),
153-
receiver_id: receiver_id.clone(),
154-
actions: actions.clone(),
155-
}
156-
.sign(signer.as_signer());
160+
let signed_transaction = self.sign_tx(signer, receiver_id, actions).await?;
157161
let tx_hash = signed_transaction.get_hash();
158162

159163
let result = self

near-fetch/src/signer.rs

+48-16
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,12 @@ impl KeyRotatingSigner {
5555
pub fn public_key(&self) -> &PublicKey {
5656
&self.current_signer().public_key
5757
}
58-
}
5958

60-
impl Signer for KeyRotatingSigner {
61-
fn sign(&self, data: &[u8]) -> Signature {
59+
pub fn sign(&self, data: &[u8]) -> Signature {
6260
self.fetch_and_rotate_signer().sign(data)
6361
}
6462

65-
fn public_key(&self) -> PublicKey {
66-
self.current_signer().public_key()
67-
}
68-
69-
fn compute_vrf_with_proof(&self, data: &[u8]) -> (vrf::Value, vrf::Proof) {
63+
pub fn compute_vrf_with_proof(&self, data: &[u8]) -> (vrf::Value, vrf::Proof) {
7064
self.current_signer().compute_vrf_with_proof(data)
7165
}
7266
}
@@ -82,21 +76,59 @@ impl ExposeAccountId for InMemorySigner {
8276
}
8377
}
8478

79+
impl ExposeAccountId for Signer {
80+
fn account_id(&self) -> &AccountId {
81+
match self {
82+
Signer::InMemory(signer) => signer.account_id(),
83+
Signer::Empty(_) => unimplemented!(),
84+
}
85+
}
86+
}
87+
8588
impl ExposeAccountId for KeyRotatingSigner {
8689
fn account_id(&self) -> &AccountId {
8790
self.current_signer().account_id()
8891
}
8992
}
9093

9194
/// A trait for extending the [`Signer`] trait with additional functionality.
92-
pub trait SignerExt: Signer + ExposeAccountId {
93-
fn as_signer(&self) -> &dyn Signer;
95+
pub trait SignerExt: ExposeAccountId + Send + Sync {
96+
fn sign(&self, data: &[u8]) -> Signature;
97+
fn public_key(&self) -> PublicKey;
9498
}
95-
impl<T> SignerExt for T
96-
where
97-
T: Signer + ExposeAccountId,
98-
{
99-
fn as_signer(&self) -> &dyn Signer {
100-
self
99+
100+
impl SignerExt for InMemorySigner {
101+
fn sign(&self, data: &[u8]) -> Signature {
102+
InMemorySigner::sign(self, data)
103+
}
104+
105+
fn public_key(&self) -> PublicKey {
106+
InMemorySigner::public_key(self).clone()
107+
}
108+
}
109+
110+
impl SignerExt for Signer {
111+
fn sign(&self, data: &[u8]) -> Signature {
112+
match self {
113+
Signer::InMemory(signer) => signer.sign(data),
114+
Signer::Empty(_) => unimplemented!(),
115+
}
116+
}
117+
118+
fn public_key(&self) -> PublicKey {
119+
match self {
120+
Signer::InMemory(signer) => signer.public_key.clone(),
121+
Signer::Empty(_) => unimplemented!(),
122+
}
123+
}
124+
}
125+
126+
impl SignerExt for KeyRotatingSigner {
127+
fn sign(&self, data: &[u8]) -> Signature {
128+
KeyRotatingSigner::sign(self, data)
129+
}
130+
131+
fn public_key(&self) -> PublicKey {
132+
KeyRotatingSigner::public_key(self).clone()
101133
}
102134
}

0 commit comments

Comments
 (0)