-
Notifications
You must be signed in to change notification settings - Fork 100
/
argent.rs
112 lines (97 loc) · 2.99 KB
/
argent.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
use crate::{
AccountFactory, PreparedAccountDeploymentV1, PreparedAccountDeploymentV3,
RawAccountDeploymentV1, RawAccountDeploymentV3,
};
use async_trait::async_trait;
use starknet_core::types::{BlockId, BlockTag, Felt};
use starknet_providers::Provider;
use starknet_signers::{Signer, SignerInteractivityContext};
/// [`AccountFactory`] implementation for deploying `Argent X` account contracts.
#[derive(Debug)]
pub struct ArgentAccountFactory<S, P> {
class_hash: Felt,
chain_id: Felt,
owner_public_key: Felt,
guardian_public_key: Felt,
signer: S,
provider: P,
block_id: BlockId,
}
impl<S, P> ArgentAccountFactory<S, P>
where
S: Signer,
{
/// Constructs a new [`ArgentAccountFactory`].
pub async fn new(
class_hash: Felt,
chain_id: Felt,
guardian_public_key: Felt,
signer: S,
provider: P,
) -> Result<Self, S::GetPublicKeyError> {
let signer_public_key = signer.get_public_key().await?;
Ok(Self {
class_hash,
chain_id,
owner_public_key: signer_public_key.scalar(),
guardian_public_key,
signer,
provider,
block_id: BlockId::Tag(BlockTag::Latest),
})
}
/// Sets a new block ID to run queries against.
pub fn set_block_id(&mut self, block_id: BlockId) -> &Self {
self.block_id = block_id;
self
}
}
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
impl<S, P> AccountFactory for ArgentAccountFactory<S, P>
where
S: Signer + Sync + Send,
P: Provider + Sync + Send,
{
type Provider = P;
type SignError = S::SignError;
fn class_hash(&self) -> Felt {
self.class_hash
}
fn calldata(&self) -> Vec<Felt> {
vec![self.owner_public_key, self.guardian_public_key]
}
fn chain_id(&self) -> Felt {
self.chain_id
}
fn provider(&self) -> &Self::Provider {
&self.provider
}
fn is_signer_interactive(&self) -> bool {
self.signer
.is_interactive(SignerInteractivityContext::Other)
}
fn block_id(&self) -> BlockId {
self.block_id
}
async fn sign_deployment_v1(
&self,
deployment: &RawAccountDeploymentV1,
query_only: bool,
) -> Result<Vec<Felt>, Self::SignError> {
let tx_hash = PreparedAccountDeploymentV1::from_raw(deployment.clone(), self)
.transaction_hash(query_only);
let signature = self.signer.sign_hash(&tx_hash).await?;
Ok(vec![signature.r, signature.s])
}
async fn sign_deployment_v3(
&self,
deployment: &RawAccountDeploymentV3,
query_only: bool,
) -> Result<Vec<Felt>, Self::SignError> {
let tx_hash = PreparedAccountDeploymentV3::from_raw(deployment.clone(), self)
.transaction_hash(query_only);
let signature = self.signer.sign_hash(&tx_hash).await?;
Ok(vec![signature.r, signature.s])
}
}