-
Notifications
You must be signed in to change notification settings - Fork 234
/
transaction.rs
1545 lines (1430 loc) · 56.3 KB
/
transaction.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
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use fake::{Dummy, Fake, Faker};
use pathfinder_crypto::hash::{HashChain as PedersenHasher, PoseidonHasher};
use pathfinder_crypto::Felt;
use primitive_types::H256;
use crate::prelude::*;
use crate::{
felt_bytes,
AccountDeploymentDataElem,
PaymasterDataElem,
ResourceAmount,
ResourcePricePerUnit,
Tip,
};
#[derive(Clone, Debug, PartialEq, Eq, Dummy)]
pub struct Transaction {
pub hash: TransactionHash,
pub variant: TransactionVariant,
}
impl Transaction {
/// Verifies the transaction hash against the transaction data.
#[must_use = "Should act on verification result"]
pub fn verify_hash(&self, chain_id: ChainId) -> bool {
self.variant.verify_hash(chain_id, self.hash)
}
pub fn version(&self) -> TransactionVersion {
match &self.variant {
TransactionVariant::DeclareV0(_) => TransactionVersion::ZERO,
TransactionVariant::DeclareV1(_) => TransactionVersion::ONE,
TransactionVariant::DeclareV2(_) => TransactionVersion::TWO,
TransactionVariant::DeclareV3(_) => TransactionVersion::THREE,
TransactionVariant::DeployV0(_) => TransactionVersion::ZERO,
TransactionVariant::DeployV1(_) => TransactionVersion::ONE,
TransactionVariant::DeployAccountV1(_) => TransactionVersion::ONE,
TransactionVariant::DeployAccountV3(_) => TransactionVersion::THREE,
TransactionVariant::InvokeV0(_) => TransactionVersion::ZERO,
TransactionVariant::InvokeV1(_) => TransactionVersion::ONE,
TransactionVariant::InvokeV3(_) => TransactionVersion::THREE,
TransactionVariant::L1Handler(_) => TransactionVersion::ZERO,
}
}
}
#[derive(Clone, Debug, PartialEq, Eq, Dummy)]
pub enum TransactionVariant {
DeclareV0(DeclareTransactionV0V1),
DeclareV1(DeclareTransactionV0V1),
DeclareV2(DeclareTransactionV2),
DeclareV3(DeclareTransactionV3),
DeployV0(DeployTransactionV0),
DeployV1(DeployTransactionV1),
DeployAccountV1(DeployAccountTransactionV1),
DeployAccountV3(DeployAccountTransactionV3),
InvokeV0(InvokeTransactionV0),
InvokeV1(InvokeTransactionV1),
InvokeV3(InvokeTransactionV3),
L1Handler(L1HandlerTransaction),
}
impl Default for TransactionVariant {
fn default() -> Self {
Self::DeclareV0(Default::default())
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum TransactionKind {
Declare,
Deploy,
DeployAccount,
Invoke,
L1Handler,
}
impl TransactionVariant {
#[must_use = "Should act on verification result"]
fn verify_hash(&self, chain_id: ChainId, expected: TransactionHash) -> bool {
if expected == self.calculate_hash(chain_id, false) {
return true;
}
// Some transaction variants had a different hash calculation in ancient times.
if Some(expected) == self.calculate_legacy_hash(chain_id) {
return true;
}
// L1 Handlers had a specific hash calculation for Starknet v0.7 blocks.
if let Self::L1Handler(l1_handler) = self {
if expected == l1_handler.calculate_v07_hash(chain_id) {
return true;
}
}
false
}
pub fn calculate_hash(&self, chain_id: ChainId, query_only: bool) -> TransactionHash {
match self {
TransactionVariant::DeclareV0(tx) => tx.calculate_hash_v0(chain_id, query_only),
TransactionVariant::DeclareV1(tx) => tx.calculate_hash_v1(chain_id, query_only),
TransactionVariant::DeclareV2(tx) => tx.calculate_hash(chain_id, query_only),
TransactionVariant::DeclareV3(tx) => tx.calculate_hash(chain_id, query_only),
TransactionVariant::DeployV0(tx) => tx.calculate_hash(chain_id, query_only),
TransactionVariant::DeployV1(tx) => tx.calculate_hash(chain_id, query_only),
TransactionVariant::DeployAccountV1(tx) => tx.calculate_hash(chain_id, query_only),
TransactionVariant::DeployAccountV3(tx) => tx.calculate_hash(chain_id, query_only),
TransactionVariant::InvokeV0(tx) => tx.calculate_hash(chain_id, query_only),
TransactionVariant::InvokeV1(tx) => tx.calculate_hash(chain_id, query_only),
TransactionVariant::InvokeV3(tx) => tx.calculate_hash(chain_id, query_only),
TransactionVariant::L1Handler(tx) => tx.calculate_hash(chain_id),
}
}
pub fn kind(&self) -> TransactionKind {
match self {
TransactionVariant::DeclareV0(_) => TransactionKind::Declare,
TransactionVariant::DeclareV1(_) => TransactionKind::Declare,
TransactionVariant::DeclareV2(_) => TransactionKind::Declare,
TransactionVariant::DeclareV3(_) => TransactionKind::Declare,
TransactionVariant::DeployV0(_) => TransactionKind::Deploy,
TransactionVariant::DeployV1(_) => TransactionKind::Deploy,
TransactionVariant::DeployAccountV1(_) => TransactionKind::DeployAccount,
TransactionVariant::DeployAccountV3(_) => TransactionKind::DeployAccount,
TransactionVariant::InvokeV0(_) => TransactionKind::Invoke,
TransactionVariant::InvokeV1(_) => TransactionKind::Invoke,
TransactionVariant::InvokeV3(_) => TransactionKind::Invoke,
TransactionVariant::L1Handler(_) => TransactionKind::L1Handler,
}
}
/// Some variants had a different hash calculations for blocks around
/// Starknet v0.8 and earlier. The hash excluded the transaction version
/// and nonce.
fn calculate_legacy_hash(&self, chain_id: ChainId) -> Option<TransactionHash> {
let hash = match self {
TransactionVariant::DeployV0(tx) => tx.calculate_legacy_hash(chain_id),
TransactionVariant::DeployV1(tx) => tx.calculate_legacy_hash(chain_id),
TransactionVariant::InvokeV0(tx) => tx.calculate_legacy_hash(chain_id),
TransactionVariant::L1Handler(tx) => tx.calculate_legacy_hash(chain_id),
_ => return None,
};
Some(hash)
}
}
impl From<DeclareTransactionV2> for TransactionVariant {
fn from(value: DeclareTransactionV2) -> Self {
Self::DeclareV2(value)
}
}
impl From<DeclareTransactionV3> for TransactionVariant {
fn from(value: DeclareTransactionV3) -> Self {
Self::DeclareV3(value)
}
}
impl From<DeployTransactionV0> for TransactionVariant {
fn from(value: DeployTransactionV0) -> Self {
Self::DeployV0(value)
}
}
impl From<DeployTransactionV1> for TransactionVariant {
fn from(value: DeployTransactionV1) -> Self {
Self::DeployV1(value)
}
}
impl From<DeployAccountTransactionV1> for TransactionVariant {
fn from(value: DeployAccountTransactionV1) -> Self {
Self::DeployAccountV1(value)
}
}
impl From<DeployAccountTransactionV3> for TransactionVariant {
fn from(value: DeployAccountTransactionV3) -> Self {
Self::DeployAccountV3(value)
}
}
impl From<InvokeTransactionV0> for TransactionVariant {
fn from(value: InvokeTransactionV0) -> Self {
Self::InvokeV0(value)
}
}
impl From<InvokeTransactionV1> for TransactionVariant {
fn from(value: InvokeTransactionV1) -> Self {
Self::InvokeV1(value)
}
}
impl From<InvokeTransactionV3> for TransactionVariant {
fn from(value: InvokeTransactionV3) -> Self {
Self::InvokeV3(value)
}
}
impl From<L1HandlerTransaction> for TransactionVariant {
fn from(value: L1HandlerTransaction) -> Self {
Self::L1Handler(value)
}
}
#[derive(Clone, Default, Debug, PartialEq, Eq)]
pub struct DeclareTransactionV0V1 {
pub class_hash: ClassHash,
pub max_fee: Fee,
pub nonce: TransactionNonce,
pub signature: Vec<TransactionSignatureElem>,
pub sender_address: ContractAddress,
}
#[derive(Clone, Default, Debug, PartialEq, Eq, Dummy)]
pub struct DeclareTransactionV2 {
pub class_hash: ClassHash,
pub max_fee: Fee,
pub nonce: TransactionNonce,
pub signature: Vec<TransactionSignatureElem>,
pub sender_address: ContractAddress,
pub compiled_class_hash: CasmHash,
}
#[derive(Clone, Default, Debug, PartialEq, Eq, Dummy)]
pub struct DeclareTransactionV3 {
pub class_hash: ClassHash,
pub nonce: TransactionNonce,
pub nonce_data_availability_mode: DataAvailabilityMode,
pub fee_data_availability_mode: DataAvailabilityMode,
pub resource_bounds: ResourceBounds,
pub tip: Tip,
pub paymaster_data: Vec<PaymasterDataElem>,
pub signature: Vec<TransactionSignatureElem>,
pub account_deployment_data: Vec<AccountDeploymentDataElem>,
pub sender_address: ContractAddress,
pub compiled_class_hash: CasmHash,
}
#[derive(Clone, Default, Debug, PartialEq, Eq)]
pub struct DeployTransactionV0 {
pub class_hash: ClassHash,
pub contract_address: ContractAddress,
pub contract_address_salt: ContractAddressSalt,
pub constructor_calldata: Vec<ConstructorParam>,
}
#[derive(Clone, Default, Debug, PartialEq, Eq)]
pub struct DeployTransactionV1 {
pub class_hash: ClassHash,
pub contract_address: ContractAddress,
pub contract_address_salt: ContractAddressSalt,
pub constructor_calldata: Vec<ConstructorParam>,
}
#[derive(Clone, Default, Debug, PartialEq, Eq)]
pub struct DeployAccountTransactionV1 {
pub contract_address: ContractAddress,
pub max_fee: Fee,
pub signature: Vec<TransactionSignatureElem>,
pub nonce: TransactionNonce,
pub contract_address_salt: ContractAddressSalt,
pub constructor_calldata: Vec<CallParam>,
pub class_hash: ClassHash,
}
#[derive(Clone, Default, Debug, PartialEq, Eq)]
pub struct DeployAccountTransactionV3 {
pub contract_address: ContractAddress,
pub signature: Vec<TransactionSignatureElem>,
pub nonce: TransactionNonce,
pub nonce_data_availability_mode: DataAvailabilityMode,
pub fee_data_availability_mode: DataAvailabilityMode,
pub resource_bounds: ResourceBounds,
pub tip: Tip,
pub paymaster_data: Vec<PaymasterDataElem>,
pub contract_address_salt: ContractAddressSalt,
pub constructor_calldata: Vec<CallParam>,
pub class_hash: ClassHash,
}
#[derive(Clone, Default, Debug, PartialEq, Eq)]
pub struct InvokeTransactionV0 {
pub calldata: Vec<CallParam>,
pub sender_address: ContractAddress,
pub entry_point_selector: EntryPoint,
pub entry_point_type: Option<EntryPointType>,
pub max_fee: Fee,
pub signature: Vec<TransactionSignatureElem>,
}
#[derive(Clone, Default, Debug, PartialEq, Eq, Dummy)]
pub struct InvokeTransactionV1 {
pub calldata: Vec<CallParam>,
pub sender_address: ContractAddress,
pub max_fee: Fee,
pub signature: Vec<TransactionSignatureElem>,
pub nonce: TransactionNonce,
}
#[derive(Clone, Default, Debug, PartialEq, Eq, Dummy)]
pub struct InvokeTransactionV3 {
pub signature: Vec<TransactionSignatureElem>,
pub nonce: TransactionNonce,
pub nonce_data_availability_mode: DataAvailabilityMode,
pub fee_data_availability_mode: DataAvailabilityMode,
pub resource_bounds: ResourceBounds,
pub tip: Tip,
pub paymaster_data: Vec<PaymasterDataElem>,
pub account_deployment_data: Vec<AccountDeploymentDataElem>,
pub calldata: Vec<CallParam>,
pub sender_address: ContractAddress,
}
#[derive(Clone, Default, Debug, PartialEq, Eq, Dummy)]
pub struct L1HandlerTransaction {
pub contract_address: ContractAddress,
pub entry_point_selector: EntryPoint,
pub nonce: TransactionNonce,
pub calldata: Vec<CallParam>,
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Dummy)]
pub enum EntryPointType {
External,
L1Handler,
}
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Dummy)]
pub struct ResourceBounds {
pub l1_gas: ResourceBound,
pub l2_gas: ResourceBound,
}
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Dummy)]
pub struct ResourceBound {
pub max_amount: ResourceAmount,
pub max_price_per_unit: ResourcePricePerUnit,
}
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Dummy)]
pub enum DataAvailabilityMode {
#[default]
L1,
L2,
}
impl From<DataAvailabilityMode> for u64 {
fn from(value: DataAvailabilityMode) -> Self {
match value {
DataAvailabilityMode::L1 => 0,
DataAvailabilityMode::L2 => 1,
}
}
}
impl DeclareTransactionV0V1 {
fn calculate_hash_v0(&self, chain_id: ChainId, query_only: bool) -> TransactionHash {
PreV3Hasher {
prefix: felt_bytes!(b"declare"),
version: TransactionVersion::ZERO.with_query_only(query_only),
address: self.sender_address,
data_hash: PedersenHasher::default().finalize(),
nonce_or_class: Some(self.class_hash.0),
..Default::default()
}
.hash(chain_id)
}
fn calculate_hash_v1(&self, chain_id: ChainId, query_only: bool) -> TransactionHash {
PreV3Hasher {
prefix: felt_bytes!(b"declare"),
version: TransactionVersion::ONE.with_query_only(query_only),
address: self.sender_address,
data_hash: PedersenHasher::single(self.class_hash.0),
max_fee: self.max_fee,
nonce_or_class: Some(self.nonce.0),
..Default::default()
}
.hash(chain_id)
}
}
impl DeclareTransactionV2 {
fn calculate_hash(&self, chain_id: ChainId, query_only: bool) -> TransactionHash {
PreV3Hasher {
prefix: felt_bytes!(b"declare"),
version: TransactionVersion::TWO.with_query_only(query_only),
address: self.sender_address,
data_hash: PedersenHasher::single(self.class_hash.0),
max_fee: self.max_fee,
nonce_or_class: Some(self.nonce.0),
casm_hash: Some(self.compiled_class_hash),
..Default::default()
}
.hash(chain_id)
}
}
impl DeployTransactionV0 {
fn calculate_hash(&self, chain_id: ChainId, query_only: bool) -> TransactionHash {
PreV3Hasher {
prefix: felt_bytes!(b"deploy"),
version: TransactionVersion::ZERO.with_query_only(query_only),
address: self.contract_address,
entry_point: EntryPoint::CONSTRUCTOR,
data_hash: self.constructor_calldata_hash(),
..Default::default()
}
.hash(chain_id)
}
fn calculate_legacy_hash(&self, chain_id: ChainId) -> TransactionHash {
LegacyHasher {
prefix: felt_bytes!(b"deploy"),
address: self.contract_address,
entry_point: EntryPoint::CONSTRUCTOR,
data_hash: self.constructor_calldata_hash(),
nonce: None,
}
.hash(chain_id)
}
fn constructor_calldata_hash(&self) -> Felt {
self.constructor_calldata
.iter()
.fold(PedersenHasher::default(), |hasher, data| {
hasher.chain_update(data.0)
})
.finalize()
}
}
impl DeployTransactionV1 {
fn calculate_hash(&self, chain_id: ChainId, query_only: bool) -> TransactionHash {
PreV3Hasher {
prefix: felt_bytes!(b"deploy"),
version: TransactionVersion::ONE.with_query_only(query_only),
address: self.contract_address,
entry_point: EntryPoint::CONSTRUCTOR,
data_hash: self.constructor_calldata_hash(),
..Default::default()
}
.hash(chain_id)
}
fn calculate_legacy_hash(&self, chain_id: ChainId) -> TransactionHash {
LegacyHasher {
prefix: felt_bytes!(b"deploy"),
address: self.contract_address,
entry_point: EntryPoint::CONSTRUCTOR,
data_hash: self.constructor_calldata_hash(),
nonce: None,
}
.hash(chain_id)
}
fn constructor_calldata_hash(&self) -> Felt {
self.constructor_calldata
.iter()
.fold(PedersenHasher::default(), |hasher, data| {
hasher.chain_update(data.0)
})
.finalize()
}
}
impl DeployAccountTransactionV1 {
fn calculate_hash(&self, chain_id: ChainId, query_only: bool) -> TransactionHash {
let constructor_calldata_hash = std::iter::once(self.class_hash.0)
.chain(std::iter::once(self.contract_address_salt.0))
.chain(self.constructor_calldata.iter().map(|x| x.0))
.fold(PedersenHasher::default(), |hasher, data| {
hasher.chain_update(data)
})
.finalize();
PreV3Hasher {
prefix: felt_bytes!(b"deploy_account"),
version: TransactionVersion::ONE.with_query_only(query_only),
address: self.contract_address,
data_hash: constructor_calldata_hash,
max_fee: self.max_fee,
nonce_or_class: Some(self.nonce.0),
..Default::default()
}
.hash(chain_id)
}
}
impl InvokeTransactionV0 {
fn calculate_hash(&self, chain_id: ChainId, query_only: bool) -> TransactionHash {
PreV3Hasher {
prefix: felt_bytes!(b"invoke"),
version: TransactionVersion::ZERO.with_query_only(query_only),
address: self.sender_address,
entry_point: self.entry_point_selector,
data_hash: self.calldata_hash(),
max_fee: self.max_fee,
..Default::default()
}
.hash(chain_id)
}
fn calculate_legacy_hash(&self, chain_id: ChainId) -> TransactionHash {
LegacyHasher {
prefix: felt_bytes!(b"invoke"),
address: self.sender_address,
entry_point: self.entry_point_selector,
data_hash: self.calldata_hash(),
nonce: None,
}
.hash(chain_id)
}
fn calldata_hash(&self) -> Felt {
self.calldata
.iter()
.fold(PedersenHasher::default(), |hasher, data| {
hasher.chain_update(data.0)
})
.finalize()
}
}
impl L1HandlerTransaction {
pub fn calculate_message_hash(&self) -> H256 {
use sha3::{Digest, Keccak256};
let Some((from_address, payload)) = self.calldata.split_first() else {
// This would indicate a pretty severe error in the L1 transaction.
// But since we haven't encoded this during serialization, this could in
// theory mess us up here.
//
// We should incorporate this into the deserialization instead. Returning an
// error here is unergonomic and far too late.
return H256::zero();
};
let mut hash = Keccak256::new();
// This is an ethereum address
hash.update(from_address.0.as_be_bytes());
hash.update(self.contract_address.0.as_be_bytes());
hash.update(self.nonce.0.as_be_bytes());
hash.update(self.entry_point_selector.0.as_be_bytes());
// Pad the u64 to 32 bytes to match a felt.
hash.update([0u8; 24]);
hash.update((payload.len() as u64).to_be_bytes());
for elem in payload {
hash.update(elem.0.as_be_bytes());
}
let hash = <[u8; 32]>::from(hash.finalize());
hash.into()
}
pub fn calculate_hash(&self, chain_id: ChainId) -> TransactionHash {
PreV3Hasher {
prefix: felt_bytes!(b"l1_handler"),
version: TransactionVersion::ZERO,
address: self.contract_address,
entry_point: self.entry_point_selector,
data_hash: self.calldata_hash(),
nonce_or_class: Some(self.nonce.0),
..Default::default()
}
.hash(chain_id)
}
fn calculate_legacy_hash(&self, chain_id: ChainId) -> TransactionHash {
LegacyHasher {
// Old L1 handler's were actually invokes under the hood.
prefix: felt_bytes!(b"invoke"),
address: self.contract_address,
entry_point: self.entry_point_selector,
data_hash: self.calldata_hash(),
nonce: None,
}
.hash(chain_id)
}
// L1 handlers had a slightly different hash for Starknet v0.7.
fn calculate_v07_hash(&self, chain_id: ChainId) -> TransactionHash {
LegacyHasher {
prefix: felt_bytes!(b"l1_handler"),
address: self.contract_address,
entry_point: self.entry_point_selector,
data_hash: self.calldata_hash(),
nonce: Some(self.nonce),
}
.hash(chain_id)
}
fn calldata_hash(&self) -> Felt {
self.calldata
.iter()
.fold(PedersenHasher::default(), |hasher, data| {
hasher.chain_update(data.0)
})
.finalize()
}
}
impl DeclareTransactionV3 {
fn calculate_hash(&self, chain_id: ChainId, query_only: bool) -> TransactionHash {
let deployment_hash = self
.account_deployment_data
.iter()
.fold(PoseidonHasher::default(), |hasher, data| {
hasher.chain(data.0.into())
})
.finish()
.into();
V3Hasher {
prefix: felt_bytes!(b"declare"),
sender_address: self.sender_address,
nonce: self.nonce,
data_hashes: &[
deployment_hash,
self.class_hash.0,
self.compiled_class_hash.0,
],
tip: self.tip,
paymaster_data: &self.paymaster_data,
nonce_data_availability_mode: self.nonce_data_availability_mode,
fee_data_availability_mode: self.fee_data_availability_mode,
resource_bounds: self.resource_bounds,
query_only,
}
.hash(chain_id)
}
}
impl DeployAccountTransactionV3 {
fn calculate_hash(&self, chain_id: ChainId, query_only: bool) -> TransactionHash {
let deployment_hash = self
.constructor_calldata
.iter()
.fold(PoseidonHasher::default(), |hasher, data| {
hasher.chain(data.0.into())
})
.finish()
.into();
V3Hasher {
prefix: felt_bytes!(b"deploy_account"),
sender_address: self.contract_address,
nonce: self.nonce,
data_hashes: &[
deployment_hash,
self.class_hash.0,
self.contract_address_salt.0,
],
tip: self.tip,
paymaster_data: &self.paymaster_data,
nonce_data_availability_mode: self.nonce_data_availability_mode,
fee_data_availability_mode: self.fee_data_availability_mode,
resource_bounds: self.resource_bounds,
query_only,
}
.hash(chain_id)
}
}
impl InvokeTransactionV3 {
fn calculate_hash(&self, chain_id: ChainId, query_only: bool) -> TransactionHash {
let deployment_hash = self
.account_deployment_data
.iter()
.fold(PoseidonHasher::default(), |hasher, data| {
hasher.chain(data.0.into())
})
.finish()
.into();
let calldata_hash = self
.calldata
.iter()
.fold(PoseidonHasher::default(), |hasher, data| {
hasher.chain(data.0.into())
})
.finish()
.into();
V3Hasher {
prefix: felt_bytes!(b"invoke"),
sender_address: self.sender_address,
nonce: self.nonce,
data_hashes: &[deployment_hash, calldata_hash],
tip: self.tip,
paymaster_data: &self.paymaster_data,
nonce_data_availability_mode: self.nonce_data_availability_mode,
fee_data_availability_mode: self.fee_data_availability_mode,
resource_bounds: self.resource_bounds,
query_only,
}
.hash(chain_id)
}
}
impl InvokeTransactionV1 {
fn calculate_hash(&self, chain_id: ChainId, query_only: bool) -> TransactionHash {
let list_hash = self
.calldata
.iter()
.fold(PedersenHasher::default(), |hasher, data| {
hasher.chain_update(data.0)
})
.finalize();
PreV3Hasher {
prefix: felt_bytes!(b"invoke"),
version: TransactionVersion::ONE.with_query_only(query_only),
address: self.sender_address,
data_hash: list_hash,
max_fee: self.max_fee,
nonce_or_class: Some(self.nonce.0),
..Default::default()
}
.hash(chain_id)
}
}
#[derive(Default)]
struct LegacyHasher {
pub prefix: Felt,
pub address: ContractAddress,
pub entry_point: EntryPoint,
pub data_hash: Felt,
pub nonce: Option<TransactionNonce>,
}
impl LegacyHasher {
fn hash(self, chain_id: ChainId) -> TransactionHash {
let mut hasher = PedersenHasher::default()
.chain_update(self.prefix)
.chain_update(*self.address.get())
.chain_update(self.entry_point.0)
.chain_update(self.data_hash)
.chain_update(chain_id.0);
if let Some(nonce) = self.nonce {
hasher.update(nonce.0);
}
TransactionHash(hasher.finalize())
}
}
#[derive(Default)]
struct PreV3Hasher {
pub prefix: Felt,
pub version: TransactionVersion,
pub address: ContractAddress,
pub entry_point: EntryPoint,
pub data_hash: Felt,
pub max_fee: Fee,
pub nonce_or_class: Option<Felt>,
pub casm_hash: Option<CasmHash>,
}
impl PreV3Hasher {
fn hash(self, chain_id: ChainId) -> TransactionHash {
let mut hash = PedersenHasher::default()
.chain_update(self.prefix)
.chain_update(self.version.0)
.chain_update(self.address.0)
.chain_update(self.entry_point.0)
.chain_update(self.data_hash)
.chain_update(self.max_fee.0)
.chain_update(chain_id.0);
if let Some(felt) = self.nonce_or_class {
hash.update(felt);
}
if let Some(felt) = self.casm_hash {
hash.update(felt.0);
}
TransactionHash(hash.finalize())
}
}
/// Provides hashing for V3 transactions.
struct V3Hasher<'a> {
pub prefix: Felt,
pub sender_address: ContractAddress,
pub nonce: TransactionNonce,
pub data_hashes: &'a [Felt],
pub tip: Tip,
pub paymaster_data: &'a [PaymasterDataElem],
pub nonce_data_availability_mode: DataAvailabilityMode,
pub fee_data_availability_mode: DataAvailabilityMode,
pub resource_bounds: ResourceBounds,
pub query_only: bool,
}
impl V3Hasher<'_> {
fn hash(self, chain_id: ChainId) -> TransactionHash {
let hasher = PoseidonHasher::default()
.chain(self.prefix.into())
.chain(
TransactionVersion::THREE
.with_query_only(self.query_only)
.0
.into(),
)
.chain(self.sender_address.0.into())
.chain(self.hash_fee_fields().into())
.chain(self.hash_paymaster_data().into())
.chain(chain_id.0.into())
.chain(self.nonce.0.into())
.chain(self.pack_data_availability().into());
let hash = self
.data_hashes
.iter()
.fold(hasher, |hasher, &data| hasher.chain(data.into()))
.finish();
TransactionHash(hash.into())
}
fn pack_data_availability(&self) -> u64 {
let nonce = u64::from(self.nonce_data_availability_mode) << 32;
let fee = u64::from(self.fee_data_availability_mode);
nonce + fee
}
fn hash_paymaster_data(&self) -> Felt {
self.paymaster_data
.iter()
.fold(PoseidonHasher::default(), |hasher, data| {
hasher.chain(data.0.into())
})
.finish()
.into()
}
fn hash_fee_fields(&self) -> Felt {
PoseidonHasher::default()
.chain(self.tip.0.into())
.chain(Self::pack_gas_bound(b"L1_GAS", &self.resource_bounds.l1_gas).into())
.chain(Self::pack_gas_bound(b"L2_GAS", &self.resource_bounds.l2_gas).into())
.finish()
.into()
}
fn pack_gas_bound(name: &[u8], bound: &ResourceBound) -> Felt {
let mut buffer: [u8; 32] = Default::default();
let (remainder, max_price) = buffer.split_at_mut(128 / 8);
let (gas_kind, max_amount) = remainder.split_at_mut(64 / 8);
let padding = gas_kind.len() - name.len();
gas_kind[padding..].copy_from_slice(name);
max_amount.copy_from_slice(&bound.max_amount.0.to_be_bytes());
max_price.copy_from_slice(&bound.max_price_per_unit.0.to_be_bytes());
Felt::from_be_bytes(buffer).expect("Packed resource should fit into felt")
}
}
impl<T> Dummy<T> for DeclareTransactionV0V1 {
fn dummy_with_rng<R: rand::Rng + ?Sized>(_: &T, rng: &mut R) -> Self {
Self {
class_hash: Faker.fake_with_rng(rng),
max_fee: Faker.fake_with_rng(rng),
// This is to keep DeclareV0 p2p compliant
nonce: TransactionNonce::ZERO,
signature: Faker.fake_with_rng(rng),
sender_address: Faker.fake_with_rng(rng),
}
}
}
impl<T> Dummy<T> for DeployTransactionV0 {
fn dummy_with_rng<R: rand::Rng + ?Sized>(_: &T, rng: &mut R) -> Self {
let class_hash = Faker.fake_with_rng(rng);
let contract_address_salt = Faker.fake_with_rng(rng);
let constructor_calldata: Vec<ConstructorParam> = Faker.fake_with_rng(rng);
let contract_address = ContractAddress::deployed_contract_address(
constructor_calldata.iter().map(|d| CallParam(d.0)),
&contract_address_salt,
&class_hash,
);
Self {
class_hash,
contract_address,
contract_address_salt,
constructor_calldata,
}
}
}
impl<T> Dummy<T> for DeployTransactionV1 {
fn dummy_with_rng<R: rand::Rng + ?Sized>(_: &T, rng: &mut R) -> Self {
let class_hash = Faker.fake_with_rng(rng);
let contract_address_salt = Faker.fake_with_rng(rng);
let constructor_calldata: Vec<ConstructorParam> = Faker.fake_with_rng(rng);
let contract_address = ContractAddress::deployed_contract_address(
constructor_calldata.iter().map(|d| CallParam(d.0)),
&contract_address_salt,
&class_hash,
);
Self {
class_hash,
contract_address,
contract_address_salt,
constructor_calldata,
}
}
}
impl<T> Dummy<T> for DeployAccountTransactionV1 {
fn dummy_with_rng<R: rand::Rng + ?Sized>(_: &T, rng: &mut R) -> Self {
let class_hash = Faker.fake_with_rng(rng);
let contract_address_salt = Faker.fake_with_rng(rng);
let constructor_calldata: Vec<CallParam> = Faker.fake_with_rng(rng);
let contract_address = ContractAddress::deployed_contract_address(
constructor_calldata.iter().map(|d| CallParam(d.0)),
&contract_address_salt,
&class_hash,
);
Self {
contract_address,
max_fee: Faker.fake_with_rng(rng),
signature: Faker.fake_with_rng(rng),
nonce: Faker.fake_with_rng(rng),
contract_address_salt,
constructor_calldata,
class_hash,
}
}
}
impl<T> Dummy<T> for DeployAccountTransactionV3 {
fn dummy_with_rng<R: rand::Rng + ?Sized>(_: &T, rng: &mut R) -> Self {
let class_hash = Faker.fake_with_rng(rng);
let contract_address_salt = Faker.fake_with_rng(rng);
let constructor_calldata: Vec<CallParam> = Faker.fake_with_rng(rng);
let contract_address = ContractAddress::deployed_contract_address(
constructor_calldata.iter().map(|d| CallParam(d.0)),
&contract_address_salt,
&class_hash,
);
Self {
contract_address,
signature: Faker.fake_with_rng(rng),
nonce: Faker.fake_with_rng(rng),
nonce_data_availability_mode: Faker.fake_with_rng(rng),
fee_data_availability_mode: Faker.fake_with_rng(rng),
resource_bounds: Faker.fake_with_rng(rng),
tip: Faker.fake_with_rng(rng),
paymaster_data: Faker.fake_with_rng(rng),
contract_address_salt,
constructor_calldata,
class_hash,
}
}
}
impl<T> Dummy<T> for InvokeTransactionV0 {
fn dummy_with_rng<R: rand::Rng + ?Sized>(_: &T, rng: &mut R) -> Self {
Self {
calldata: Faker.fake_with_rng(rng),
sender_address: Faker.fake_with_rng(rng),
entry_point_selector: Faker.fake_with_rng(rng),
// This is a legacy field, not used in p2p
entry_point_type: None,
max_fee: Faker.fake_with_rng(rng),
signature: Faker.fake_with_rng(rng),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::macro_prelude::*;
// Goerli support was removed, however some of the fixtures originally come from
// Goerli.
const GOERLI_TESTNET: ChainId = ChainId(match Felt::from_be_slice(b"SN_GOERLI") {
Ok(chain_id) => chain_id,
Err(_) => unreachable!(),
});
#[rstest::rstest]
#[test]
#[case::declare_v0(declare_v0(), GOERLI_TESTNET)]
#[case::declare_v1(declare_v1(), ChainId::SEPOLIA_TESTNET)]
#[case::declare_v2(declare_v2(), ChainId::SEPOLIA_TESTNET)]
#[case::declare_v3(declare_v3(), GOERLI_TESTNET)]
#[case::deploy(deploy(), GOERLI_TESTNET)]
#[case::deploy_legacy(deploy_legacy(), GOERLI_TESTNET)]
#[case::deploy_account_v1(deploy_account_v1(), ChainId::MAINNET)]