starknet-accounts 0.16.0

Types for handling Starknet account abstraction
Documentation
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
use async_trait::async_trait;
use auto_impl::auto_impl;
use starknet_core::types::{
    contract::ComputeClassHashError, BlockId, BlockTag, Call, Felt, FlattenedSierraClass,
};
use starknet_providers::{Provider, ProviderError};
use starknet_signers::SignerInteractivityContext;
use std::{error::Error, sync::Arc};

mod declaration;
mod execution;

/// The standard Starknet account contract interface. It makes no assumption about the underlying
/// signer or provider. Account implementations that come with an active connection to the network
/// should also implement [`ConnectedAccount`] for useful functionalities like estimating fees and
/// sending transactions.
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
pub trait Account: ExecutionEncoder + Sized {
    /// Possible errors for signing transactions.
    type SignError: Error + Send + Sync;

    /// Gets the account contract's address.
    fn address(&self) -> Felt;

    /// Gets the chain ID of the network where the account contract was deployed.
    fn chain_id(&self) -> Felt;

    /// Signs an execution request to authorize an `INVOKE` v3 transaction that pays transaction
    /// fees in `STRK`.
    ///
    /// If `query_only` is `true`, the commitment must be constructed in a way that a real state-
    /// changing transaction cannot be authenticated. This is to prevent replay attacks.
    async fn sign_execution_v3(
        &self,
        execution: &RawExecutionV3,
        query_only: bool,
    ) -> Result<Vec<Felt>, Self::SignError>;

    /// Signs an execution request to authorize an `DECLARE` v3 transaction that pays transaction
    /// fees in `STRK` for declaring Cairo 1 classes.
    ///
    /// If `query_only` is `true`, the commitment must be constructed in a way that a real state-
    /// changing transaction cannot be authenticated. This is to prevent replay attacks.
    async fn sign_declaration_v3(
        &self,
        declaration: &RawDeclarationV3,
        query_only: bool,
    ) -> Result<Vec<Felt>, Self::SignError>;

    /// Whether the underlying signer implementation is interactive, such as a hardware wallet.
    /// Implementations should return `true` if the signing operation is very expensive, even if not
    /// strictly "interactive" as in requiring human input.
    ///
    /// This affects how an account makes decision on whether to request a real signature for
    /// estimation/simulation purposes.
    fn is_signer_interactive(&self, context: SignerInteractivityContext<'_>) -> bool;

    /// Generates an instance of [`ExecutionV3`] for sending `INVOKE` v3 transactions. Pays
    /// transaction fees in `STRK`.
    fn execute_v3(&self, calls: Vec<Call>) -> ExecutionV3<'_, Self> {
        ExecutionV3::new(calls, self)
    }

    /// Generates an instance of [`ExecutionV3`] for sending `INVOKE` v3 transactions. Pays
    /// transaction fees in `STRK`.
    #[deprecated = "transaction version used might change unexpectedly; use `execute_v3` instead"]
    fn execute(&self, calls: Vec<Call>) -> ExecutionV3<'_, Self> {
        self.execute_v3(calls)
    }

    /// Generates an instance of [`DeclarationV3`] for sending `DECLARE` v3 transactions. Pays
    /// transaction fees in `STRK`.
    ///
    /// To declare a Sierra (Cairo 1) class, a `compiled_class_hash` must be provided. This can be
    /// obtained by compiling the Sierra class to obtain a CASM class, and then hashing it.
    ///
    /// The compilation of Sierra to CASM can either be done interactively via the
    /// `starknet-sierra-compile` command from the Cairo toolchain, or programmatically through the
    /// Cairo crates.
    ///
    /// Hashing the resulting CASM class is supported in the `starknet-core` crate. It can also be
    /// done interactively via Starkli with its `starkli class-hash` command.
    ///
    /// This method is only used for declaring Sierra (Cairo 1) classes. Declaring legacy (Cairo 0)
    /// classes is no longer supported.
    fn declare_v3(
        &self,
        contract_class: Arc<FlattenedSierraClass>,
        compiled_class_hash: Felt,
    ) -> DeclarationV3<'_, Self> {
        DeclarationV3::new(contract_class, compiled_class_hash, self)
    }

    /// Generates an instance of [`DeclarationV3`] for sending `DECLARE` v3 transactions. Pays
    /// transaction fees in `STRK`.
    ///
    /// To declare a Sierra (Cairo 1) class, a `compiled_class_hash` must be provided. This can be
    /// obtained by compiling the Sierra class to obtain a CASM class, and then hashing it.
    ///
    /// The compilation of Sierra to CASM can either be done interactively via the
    /// `starknet-sierra-compile` command from the Cairo toolchain, or programmatically through the
    /// Cairo crates.
    ///
    /// Hashing the resulting CASM class is supported in the `starknet-core` crate. It can also be
    /// done interactively via Starkli with its `starkli class-hash` command.
    ///
    /// This method is only used for declaring Sierra (Cairo 1) classes. Declaring legacy (Cairo 0)
    /// classes is no longer supported.
    #[deprecated = "transaction version used might change unexpectedly; use `declare_v3` instead"]
    fn declare(
        &self,
        contract_class: Arc<FlattenedSierraClass>,
        compiled_class_hash: Felt,
    ) -> DeclarationV3<'_, Self> {
        self.declare_v3(contract_class, compiled_class_hash)
    }
}

/// An abstraction over different ways to encode [`Vec<Call>`] into [`Vec<Felt>`].
///
/// Standard Cairo 0 and Cairo 1 account contracts encodes calls differently. Custom account
/// contract implementations might also impose arbitrary encoding rules.
#[auto_impl(&, Box, Arc)]
pub trait ExecutionEncoder {
    /// Encodes the list of [`Call`] into a list of [`Felt`] to be used as calldata to the account's
    /// `__execute__` entrypoint.
    fn encode_calls(&self, calls: &[Call]) -> Vec<Felt>;
}

/// An [`Account`] implementation that also comes with a [`Provider`]. Functionalities that require
/// a connection to the sequencer or node are offloaded to this trait to keep the base [`Account`]
/// clean and flexible.
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
pub trait ConnectedAccount: Account {
    /// The [`Provider`] type attached to this account.
    type Provider: Provider + Sync;

    /// Gets a reference to the attached [`Provider`] instance.
    fn provider(&self) -> &Self::Provider;

    /// Gets block ID to use when checking nonce and estimating fees.
    fn block_id(&self) -> BlockId {
        BlockId::Tag(BlockTag::Latest)
    }

    /// Gets the next available nonce to be used.
    async fn get_nonce(&self) -> Result<Felt, ProviderError> {
        self.provider()
            .get_nonce(self.block_id(), self.address())
            .await
    }
}

/// Abstraction over `INVOKE` transactions from accounts for invoking contracts. This struct uses
/// v3 `INVOKE` transactions under the hood, and hence pays transaction fees in STRK.
///
/// This is an intermediate type allowing users to optionally specify `nonce` and transaction fee
/// options.
#[must_use]
#[derive(Debug)]
pub struct ExecutionV3<'a, A> {
    account: &'a A,
    calls: Vec<Call>,
    nonce: Option<Felt>,
    l1_gas: Option<u64>,
    l1_gas_price: Option<u128>,
    l2_gas: Option<u64>,
    l2_gas_price: Option<u128>,
    l1_data_gas: Option<u64>,
    l1_data_gas_price: Option<u128>,
    gas_estimate_multiplier: f64,
    gas_price_estimate_multiplier: f64,
    tip: Option<u64>,
}

/// Abstraction over `DECLARE` transactions from accounts for invoking contracts. This struct uses
/// v3 `DECLARE` transactions under the hood, and hence pays transaction fees in STRK.
///
/// This is an intermediate type allowing users to optionally specify `nonce` and transaction fee
/// options.
#[must_use]
#[derive(Debug)]
pub struct DeclarationV3<'a, A> {
    account: &'a A,
    contract_class: Arc<FlattenedSierraClass>,
    compiled_class_hash: Felt,
    nonce: Option<Felt>,
    l1_gas: Option<u64>,
    l1_gas_price: Option<u128>,
    l2_gas: Option<u64>,
    l2_gas_price: Option<u128>,
    l1_data_gas: Option<u64>,
    l1_data_gas_price: Option<u128>,
    gas_estimate_multiplier: f64,
    gas_price_estimate_multiplier: f64,
    tip: Option<u64>,
}

/// [`ExecutionV3`] but with `nonce` and other transaction fee options already determined.
#[derive(Debug)]
pub struct RawExecutionV3 {
    calls: Vec<Call>,
    nonce: Felt,
    l1_gas: u64,
    l1_gas_price: u128,
    l2_gas: u64,
    l2_gas_price: u128,
    l1_data_gas: u64,
    l1_data_gas_price: u128,
    tip: u64,
}

/// [`DeclarationV3`] but with `nonce` and other transaction fee options already determined.
#[derive(Debug)]
pub struct RawDeclarationV3 {
    contract_class: Arc<FlattenedSierraClass>,
    compiled_class_hash: Felt,
    nonce: Felt,
    l1_gas: u64,
    l1_gas_price: u128,
    l2_gas: u64,
    l2_gas_price: u128,
    l1_data_gas: u64,
    l1_data_gas_price: u128,
    tip: u64,
}

/// [`RawExecutionV3`] but with an account associated.
#[derive(Debug)]
pub struct PreparedExecutionV3<'a, A> {
    account: &'a A,
    inner: RawExecutionV3,
}

/// [`RawDeclarationV3`] but with an account associated.
#[derive(Debug)]
pub struct PreparedDeclarationV3<'a, A> {
    account: &'a A,
    inner: RawDeclarationV3,
}

/// Errors using Starknet accounts.
#[derive(Debug, thiserror::Error)]
pub enum AccountError<S> {
    /// An error is encountered when signing a request.
    #[error(transparent)]
    Signing(S),
    /// An error is encountered with communicating with the network.
    #[error(transparent)]
    Provider(ProviderError),
    /// Unable to calculate the class hash for declaration.
    #[error(transparent)]
    ClassHashCalculation(ComputeClassHashError),
    /// Transaction fee calculation overflow.
    #[error("fee calculation overflow")]
    FeeOutOfRange,
}

#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
impl<A> Account for &A
where
    A: Account + Sync,
{
    type SignError = A::SignError;

    fn address(&self) -> Felt {
        (*self).address()
    }

    fn chain_id(&self) -> Felt {
        (*self).chain_id()
    }

    async fn sign_execution_v3(
        &self,
        execution: &RawExecutionV3,
        query_only: bool,
    ) -> Result<Vec<Felt>, Self::SignError> {
        (*self).sign_execution_v3(execution, query_only).await
    }

    async fn sign_declaration_v3(
        &self,
        declaration: &RawDeclarationV3,
        query_only: bool,
    ) -> Result<Vec<Felt>, Self::SignError> {
        (*self).sign_declaration_v3(declaration, query_only).await
    }

    fn is_signer_interactive(&self, context: SignerInteractivityContext<'_>) -> bool {
        (*self).is_signer_interactive(context)
    }
}

#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
impl<A> Account for Box<A>
where
    A: Account + Sync + Send,
{
    type SignError = A::SignError;

    fn address(&self) -> Felt {
        self.as_ref().address()
    }

    fn chain_id(&self) -> Felt {
        self.as_ref().chain_id()
    }

    async fn sign_execution_v3(
        &self,
        execution: &RawExecutionV3,
        query_only: bool,
    ) -> Result<Vec<Felt>, Self::SignError> {
        self.as_ref().sign_execution_v3(execution, query_only).await
    }

    async fn sign_declaration_v3(
        &self,
        declaration: &RawDeclarationV3,
        query_only: bool,
    ) -> Result<Vec<Felt>, Self::SignError> {
        self.as_ref()
            .sign_declaration_v3(declaration, query_only)
            .await
    }

    fn is_signer_interactive(&self, context: SignerInteractivityContext<'_>) -> bool {
        self.as_ref().is_signer_interactive(context)
    }
}

#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
impl<A> Account for Arc<A>
where
    A: Account + Sync + Send,
{
    type SignError = A::SignError;

    fn address(&self) -> Felt {
        self.as_ref().address()
    }

    fn chain_id(&self) -> Felt {
        self.as_ref().chain_id()
    }

    async fn sign_execution_v3(
        &self,
        execution: &RawExecutionV3,
        query_only: bool,
    ) -> Result<Vec<Felt>, Self::SignError> {
        self.as_ref().sign_execution_v3(execution, query_only).await
    }

    async fn sign_declaration_v3(
        &self,
        declaration: &RawDeclarationV3,
        query_only: bool,
    ) -> Result<Vec<Felt>, Self::SignError> {
        self.as_ref()
            .sign_declaration_v3(declaration, query_only)
            .await
    }

    fn is_signer_interactive(&self, context: SignerInteractivityContext<'_>) -> bool {
        self.as_ref().is_signer_interactive(context)
    }
}

#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
impl<A> ConnectedAccount for &A
where
    A: ConnectedAccount + Sync,
{
    type Provider = A::Provider;

    fn provider(&self) -> &Self::Provider {
        (*self).provider()
    }

    fn block_id(&self) -> BlockId {
        (*self).block_id()
    }

    async fn get_nonce(&self) -> Result<Felt, ProviderError> {
        (*self).get_nonce().await
    }
}

#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
impl<A> ConnectedAccount for Box<A>
where
    A: ConnectedAccount + Sync + Send,
{
    type Provider = A::Provider;

    fn provider(&self) -> &Self::Provider {
        self.as_ref().provider()
    }

    fn block_id(&self) -> BlockId {
        self.as_ref().block_id()
    }

    async fn get_nonce(&self) -> Result<Felt, ProviderError> {
        self.as_ref().get_nonce().await
    }
}

#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
impl<A> ConnectedAccount for Arc<A>
where
    A: ConnectedAccount + Sync + Send,
{
    type Provider = A::Provider;

    fn provider(&self) -> &Self::Provider {
        self.as_ref().provider()
    }

    fn block_id(&self) -> BlockId {
        self.as_ref().block_id()
    }

    async fn get_nonce(&self) -> Result<Felt, ProviderError> {
        self.as_ref().get_nonce().await
    }
}