forked from Haroldwonder/TrustLink
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage.rs
More file actions
1151 lines (999 loc) · 44.3 KB
/
Copy pathstorage.rs
File metadata and controls
1151 lines (999 loc) · 44.3 KB
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
//! Storage helpers for TrustLink.
//!
//! Single point of contact between contract logic and on-chain storage.
use crate::constants::{DAY_IN_LEDGERS, DEFAULT_INSTANCE_LIFETIME};
use crate::types::{
AdminCouncil, Attestation, AttestationRequest, AttestationTemplate, AuditEntry, ClaimTypeInfo,
Endorsement, Error, ExpirationHook, FeeConfig, GlobalStats, IssuerMetadata, IssuerStats,
IssuerTier, MultiSigProposal, PendingAdminTransfer, RateLimitConfig, StorageLimits, TtlConfig,
CouncilProposal,
};
use soroban_sdk::{contracttype, Address, Env, String, Vec};
#[contracttype]
pub enum StorageKey {
Admin,
AdminCouncil,
Version,
FeeConfig,
TtlConfig,
ContractConfig,
Issuer(Address),
Bridge(Address),
Attestation(String),
SubjectAttestations(Address),
IssuerAttestations(Address),
IssuerMetadata(Address),
SubjectAttestationChunk(Address, u32),
IssuerAttestationChunk(Address, u32),
ClaimType(String),
ClaimTypeList,
IssuerList,
MultisigTtlDays,
IssuerTier(Address),
IssuerStats(Address),
GlobalStats,
ExpirationHook(Address),
Endorsements(String),
Limits,
StorageLimits,
RateLimitConfig,
LastIssuance(Address),
LastIssuanceTime(Address),
IssuerWhitelistEnabled(Address),
/// Whitelist mode flag (alias for IssuerWhitelistEnabled).
IssuerWhitelistMode(Address),
/// Whitelist entry for a (issuer, subject) pair.
IssuerWhitelist(Address, Address),
/// Audit log entries for an attestation.
AuditLog(String),
/// Multi-sig proposal keyed by proposal ID.
MultiSigProposal(String),
/// An attestation request record.
AttestationRequest(String),
IssuerPendingRequests(Address),
PendingRequests(Address),
/// Contract paused flag.
Paused,
/// Council proposal by numeric ID.
CouncilProposal(u32),
CouncilProposalStr(String),
ProposalCounter,
PendingAdminTransfer,
AttestationTemplate(Address, String),
AttestationTemplateList(Address),
Delegation(Address, Address, String),
/// Ordered list of all registered bridge contract addresses.
BridgeList,
/// Per-claim-type rate limit override (claim_type -> min_issuance_interval).
ClaimTypeRateLimit(String),
/// Subject-scoped index of attestation IDs that are neither revoked nor deleted.
ValidAttestations(Address),
/// Per-delegator index of (delegate, claim_type) pairs for efficient lookup.
DelegatorIndex(Address),
/// Per-endorser index of endorsements they have made.
EndorserIndex(Address),
/// Count of issued attestations by claim type.
ClaimTypeCount(String),
}
fn get_ttl_lifetime(env: &Env) -> u32 {
if let Some(config) = env
.storage()
.instance()
.get::<StorageKey, TtlConfig>(&StorageKey::TtlConfig)
{
DAY_IN_LEDGERS * config.ttl_days
} else {
DEFAULT_INSTANCE_LIFETIME
}
}
pub struct Storage;
impl Storage {
pub fn has_admin(env: &Env) -> bool {
if let Ok(council) = Self::get_admin_council(env) {
!council.is_empty()
} else {
false
}
}
pub fn set_admin(env: &Env, admin: &Address) {
let _ttl = get_ttl_lifetime(env);
let mut council = Vec::new(env);
council.push_back(admin.clone());
Self::set_admin_council(env, &council);
}
pub fn get_admin_council(env: &Env) -> Result<AdminCouncil, Error> {
env.storage()
.instance()
.get(&StorageKey::AdminCouncil)
.ok_or(Error::NotInitialized)
}
pub fn set_admin_council(env: &Env, council: &AdminCouncil) {
let ttl = get_ttl_lifetime(env);
env.storage().instance().set(&StorageKey::AdminCouncil, council);
env.storage().instance().extend_ttl(ttl, ttl);
}
pub fn is_admin(env: &Env, address: &Address) -> bool {
if let Ok(council) = Self::get_admin_council(env) {
for admin in council.iter() {
if &admin == address { return true; }
}
}
false
}
pub fn add_admin(env: &Env, admin: &Address) {
let mut council = Self::get_admin_council(env).unwrap_or(Vec::new(env));
for a in council.iter() {
if &a == admin { return; }
}
council.push_back(admin.clone());
Self::set_admin_council(env, &council);
}
pub fn remove_admin(env: &Env, admin: &Address) {
let council = Self::get_admin_council(env).unwrap_or(Vec::new(env));
let mut new_council = Vec::new(env);
for a in council.iter() {
if &a != admin { new_council.push_back(a); }
}
Self::set_admin_council(env, &new_council);
}
pub fn get_admin(env: &Env) -> Result<Address, Error> {
let council = Self::get_admin_council(env)?;
council.first().ok_or(Error::NotInitialized)
}
pub fn get_council(env: &Env) -> Option<AdminCouncil> {
env.storage().instance().get(&StorageKey::AdminCouncil)
}
pub fn set_council(env: &Env, council: &AdminCouncil) {
Self::set_admin_council(env, council);
}
pub fn set_version(env: &Env, version: &String) {
env.storage().instance().set(&StorageKey::Version, version);
}
pub fn get_version(env: &Env) -> Option<String> {
env.storage().instance().get(&StorageKey::Version)
}
pub fn set_fee_config(env: &Env, fee_config: &FeeConfig) {
let ttl = get_ttl_lifetime(env);
env.storage().instance().set(&StorageKey::FeeConfig, fee_config);
env.storage().instance().extend_ttl(ttl, ttl);
}
pub fn get_fee_config(env: &Env) -> Option<FeeConfig> {
env.storage().instance().get(&StorageKey::FeeConfig)
}
pub fn set_ttl_config(env: &Env, ttl_config: &TtlConfig) {
let ttl = get_ttl_lifetime(env);
env.storage().instance().set(&StorageKey::TtlConfig, ttl_config);
env.storage().instance().extend_ttl(ttl, ttl);
}
pub fn get_ttl_config(env: &Env) -> Option<TtlConfig> {
env.storage().instance().get(&StorageKey::TtlConfig)
}
pub fn set_contract_config(env: &Env, config: &crate::types::ContractConfig) {
let ttl = get_ttl_lifetime(env);
env.storage().instance().set(&StorageKey::ContractConfig, config);
env.storage().instance().extend_ttl(ttl, ttl);
}
pub fn get_contract_config(env: &Env) -> Option<crate::types::ContractConfig> {
env.storage().instance().get(&StorageKey::ContractConfig)
}
pub fn is_issuer(env: &Env, address: &Address) -> bool {
env.storage().persistent().has(&StorageKey::Issuer(address.clone()))
}
pub fn add_issuer(env: &Env, issuer: &Address) {
let key = StorageKey::Issuer(issuer.clone());
let ttl = get_ttl_lifetime(env);
env.storage().persistent().set(&key, &true);
env.storage().persistent().extend_ttl(&key, ttl, ttl);
// Maintain ordered IssuerList
let mut list = Self::get_issuer_list(env);
for existing in list.iter() {
if &existing == issuer {
return;
}
}
list.push_back(issuer.clone());
let list_key = StorageKey::IssuerList;
env.storage().persistent().set(&list_key, &list);
env.storage().persistent().extend_ttl(&list_key, ttl, ttl);
}
pub fn remove_issuer(env: &Env, issuer: &Address) {
env.storage().persistent().remove(&StorageKey::Issuer(issuer.clone()));
// Remove from IssuerList
let existing = Self::get_issuer_list(env);
let mut updated = Vec::new(env);
for addr in existing.iter() {
if &addr != issuer {
updated.push_back(addr);
}
}
let list_key = StorageKey::IssuerList;
let ttl = get_ttl_lifetime(env);
env.storage().persistent().set(&list_key, &updated);
env.storage().persistent().extend_ttl(&list_key, ttl, ttl);
}
pub fn get_issuer_list(env: &Env) -> Vec<Address> {
env.storage()
.persistent()
.get(&StorageKey::IssuerList)
.unwrap_or(Vec::new(env))
}
pub fn is_bridge(env: &Env, address: &Address) -> bool {
env.storage().persistent().has(&StorageKey::Bridge(address.clone()))
}
pub fn add_bridge(env: &Env, bridge: &Address) {
let key = StorageKey::Bridge(bridge.clone());
let ttl = get_ttl_lifetime(env);
env.storage().persistent().set(&key, &true);
env.storage().persistent().extend_ttl(&key, ttl, ttl);
// Maintain ordered BridgeList
let mut list = Self::get_bridge_list(env);
for existing in list.iter() {
if &existing == bridge {
return;
}
}
list.push_back(bridge.clone());
let list_key = StorageKey::BridgeList;
env.storage().persistent().set(&list_key, &list);
env.storage().persistent().extend_ttl(&list_key, ttl, ttl);
}
pub fn get_bridge_list(env: &Env) -> Vec<Address> {
env.storage()
.persistent()
.get(&StorageKey::BridgeList)
.unwrap_or(Vec::new(env))
}
pub fn has_attestation(env: &Env, id: &String) -> bool {
env.storage().persistent().has(&StorageKey::Attestation(id.clone()))
}
pub fn set_attestation(env: &Env, attestation: &Attestation) {
let key = StorageKey::Attestation(attestation.id.clone());
let ttl = get_ttl_lifetime(env);
env.storage().persistent().set(&key, attestation);
env.storage().persistent().extend_ttl(&key, ttl, ttl);
}
pub fn get_attestation(env: &Env, id: &String) -> Result<Attestation, Error> {
env.storage().persistent().get(&StorageKey::Attestation(id.clone())).ok_or(Error::NotFound)
}
pub fn get_subject_attestations(env: &Env, subject: &Address) -> Vec<String> {
env.storage().persistent().get(&StorageKey::SubjectAttestations(subject.clone())).unwrap_or(Vec::new(env))
}
pub fn add_subject_attestation(env: &Env, subject: &Address, attestation_id: &String) {
let key = StorageKey::SubjectAttestations(subject.clone());
let ttl = get_ttl_lifetime(env);
let mut list = Self::get_subject_attestations(env, subject);
list.push_back(attestation_id.clone());
env.storage().persistent().set(&key, &list);
env.storage().persistent().extend_ttl(&key, ttl, ttl);
}
pub fn remove_subject_attestation(env: &Env, subject: &Address, attestation_id: &String) {
let key = StorageKey::SubjectAttestations(subject.clone());
let ttl = get_ttl_lifetime(env);
let existing = Self::get_subject_attestations(env, subject);
let mut updated = Vec::new(env);
for id in existing.iter() {
if &id != attestation_id { updated.push_back(id); }
}
env.storage().persistent().set(&key, &updated);
env.storage().persistent().extend_ttl(&key, ttl, ttl);
}
pub fn get_issuer_attestations(env: &Env, issuer: &Address) -> Vec<String> {
env.storage().persistent().get(&StorageKey::IssuerAttestations(issuer.clone())).unwrap_or(Vec::new(env))
}
pub fn add_issuer_attestation(env: &Env, issuer: &Address, attestation_id: &String) {
let key = StorageKey::IssuerAttestations(issuer.clone());
let ttl = get_ttl_lifetime(env);
let mut list = Self::get_issuer_attestations(env, issuer);
list.push_back(attestation_id.clone());
env.storage().persistent().set(&key, &list);
env.storage().persistent().extend_ttl(&key, ttl, ttl);
}
/// Append multiple attestation IDs to the issuer index in a single write.
///
/// Used by `create_attestations_batch` to replace N per-item writes with
/// one read + one write regardless of batch size.
pub fn add_issuer_attestations_bulk(env: &Env, issuer: &Address, attestation_ids: &Vec<String>) {
if attestation_ids.is_empty() {
return;
}
let key = StorageKey::IssuerAttestations(issuer.clone());
let ttl = get_ttl_lifetime(env);
let mut list = Self::get_issuer_attestations(env, issuer);
for id in attestation_ids.iter() {
list.push_back(id);
}
env.storage().persistent().set(&key, &list);
env.storage().persistent().extend_ttl(&key, ttl, ttl);
}
/// Increment the issuer's `total_issued` counter by `count` in a single write.
///
/// Used by `create_attestations_batch` to replace N per-item stat writes.
pub fn increment_issuer_stats(env: &Env, issuer: &Address, count: u64) {
let mut stats = Self::get_issuer_stats(env, issuer);
stats.total_issued = stats.total_issued.saturating_add(count);
Self::set_issuer_stats(env, issuer, &stats);
}
/// Remove an attestation ID from the issuer's attestation index.
///
/// Used when transferring attestation ownership to a new issuer.
pub fn remove_issuer_attestation(env: &Env, issuer: &Address, attestation_id: &String) {
let key = StorageKey::IssuerAttestations(issuer.clone());
let ttl = get_ttl_lifetime(env);
let existing = Self::get_issuer_attestations(env, issuer);
let mut updated = Vec::new(env);
for id in existing.iter() {
if &id != attestation_id {
updated.push_back(id);
}
}
env.storage().persistent().set(&key, &updated);
env.storage().persistent().extend_ttl(&key, ttl, ttl);
}
/// Persist `metadata` for `issuer` and refresh its TTL.
pub fn set_issuer_metadata(env: &Env, issuer: &Address, metadata: &IssuerMetadata) {
let key = StorageKey::IssuerMetadata(issuer.clone());
let ttl = get_ttl_lifetime(env);
env.storage().persistent().set(&key, metadata);
env.storage().persistent().extend_ttl(&key, ttl, ttl);
}
pub fn get_issuer_metadata(env: &Env, issuer: &Address) -> Option<IssuerMetadata> {
env.storage().persistent().get(&StorageKey::IssuerMetadata(issuer.clone()))
}
pub fn set_claim_type(env: &Env, info: &ClaimTypeInfo) {
let key = StorageKey::ClaimType(info.claim_type.clone());
let is_new = !env.storage().persistent().has(&key);
let ttl = get_ttl_lifetime(env);
env.storage().persistent().set(&key, info);
env.storage().persistent().extend_ttl(&key, ttl, ttl);
if is_new {
let list_key = StorageKey::ClaimTypeList;
let mut list: Vec<String> = env.storage().persistent().get(&list_key).unwrap_or(Vec::new(env));
list.push_back(info.claim_type.clone());
env.storage().persistent().set(&list_key, &list);
env.storage().persistent().extend_ttl(&list_key, ttl, ttl);
}
}
pub fn get_claim_type(env: &Env, claim_type: &String) -> Option<ClaimTypeInfo> {
env.storage().persistent().get(&StorageKey::ClaimType(claim_type.clone()))
}
pub fn get_claim_type_list(env: &Env) -> Vec<String> {
env.storage().persistent().get(&StorageKey::ClaimTypeList).unwrap_or(Vec::new(env))
}
pub fn set_whitelist_mode(env: &Env, issuer: &Address, enabled: bool) {
let key = StorageKey::IssuerWhitelistMode(issuer.clone());
let ttl = get_ttl_lifetime(env);
env.storage().persistent().set(&key, &enabled);
env.storage().persistent().extend_ttl(&key, ttl, ttl);
}
pub fn is_whitelist_mode(env: &Env, issuer: &Address) -> bool {
env.storage().persistent().get(&StorageKey::IssuerWhitelistMode(issuer.clone())).unwrap_or(false)
}
pub fn set_whitelist_enabled(env: &Env, issuer: &Address, enabled: bool) {
Self::set_whitelist_mode(env, issuer, enabled);
}
pub fn is_whitelist_enabled(env: &Env, issuer: &Address) -> bool {
Self::is_whitelist_mode(env, issuer)
}
pub fn is_whitelisted(env: &Env, issuer: &Address, subject: &Address) -> bool {
env.storage().persistent().has(&StorageKey::IssuerWhitelist(issuer.clone(), subject.clone()))
}
pub fn add_to_whitelist(env: &Env, issuer: &Address, subject: &Address) {
let key = StorageKey::IssuerWhitelist(issuer.clone(), subject.clone());
let ttl = get_ttl_lifetime(env);
env.storage().persistent().set(&key, &true);
env.storage().persistent().extend_ttl(&key, ttl, ttl);
}
/// Retrieve a council proposal by ID.
pub fn get_proposal(env: &Env, id: u32) -> Option<CouncilProposal> {
env.storage().persistent().get(&StorageKey::CouncilProposal(id))
}
/// Persist a council proposal.
pub fn set_proposal(env: &Env, proposal: &CouncilProposal) {
let key = StorageKey::CouncilProposal(proposal.id);
let ttl = get_ttl_lifetime(env);
env.storage().persistent().set(&key, proposal);
env.storage().persistent().extend_ttl(&key, ttl, ttl);
}
pub fn remove_from_whitelist(env: &Env, issuer: &Address, subject: &Address) {
env.storage().persistent().remove(&StorageKey::IssuerWhitelist(issuer.clone(), subject.clone()));
}
pub fn is_subject_whitelisted(env: &Env, issuer: &Address, subject: &Address) -> bool {
Self::is_whitelisted(env, issuer, subject)
}
pub fn add_subject_to_whitelist(env: &Env, issuer: &Address, subject: &Address) {
Self::add_to_whitelist(env, issuer, subject);
}
pub fn remove_subject_from_whitelist(env: &Env, issuer: &Address, subject: &Address) {
Self::remove_from_whitelist(env, issuer, subject);
}
pub fn set_paused(env: &Env, paused: bool) {
env.storage().instance().set(&StorageKey::Paused, &paused);
env.storage().instance().extend_ttl(DEFAULT_INSTANCE_LIFETIME, DEFAULT_INSTANCE_LIFETIME);
}
pub fn is_paused(env: &Env) -> bool {
env.storage().instance().get(&StorageKey::Paused).unwrap_or(false)
}
pub fn get_global_stats(env: &Env) -> GlobalStats {
env.storage().instance()
.get(&StorageKey::GlobalStats)
.unwrap_or(GlobalStats { total_attestations: 0, total_revocations: 0, total_issuers: 0 })
}
pub fn set_global_stats(env: &Env, stats: &GlobalStats) {
Self::set_global_stats_raw(env, stats)
}
pub fn get_global_stats_raw(env: &Env) -> GlobalStats {
Self::get_global_stats(env)
}
fn set_global_stats_raw(env: &Env, stats: &GlobalStats) {
let ttl = get_ttl_lifetime(env);
env.storage().instance().set(&StorageKey::GlobalStats, stats);
env.storage().instance().extend_ttl(ttl, ttl);
}
/// Increment `total_attestations` by `count`.
pub fn increment_total_attestations(env: &Env, count: u64) {
let mut stats = Self::get_global_stats(env);
stats.total_attestations = stats.total_attestations.saturating_add(count);
Self::set_global_stats(env, &stats);
}
pub fn increment_total_revocations(env: &Env, by: u64) {
let mut s = Self::get_global_stats_raw(env);
s.total_revocations = s.total_revocations.saturating_add(by);
Self::set_global_stats_raw(env, &s);
}
pub fn increment_total_issuers(env: &Env) {
let mut s = Self::get_global_stats_raw(env);
s.total_issuers = s.total_issuers.saturating_add(1);
Self::set_global_stats_raw(env, &s);
}
pub fn decrement_total_issuers(env: &Env) {
let mut s = Self::get_global_stats_raw(env);
s.total_issuers = s.total_issuers.saturating_sub(1);
Self::set_global_stats_raw(env, &s);
}
pub fn get_issuer_stats(env: &Env, issuer: &Address) -> IssuerStats {
env.storage().persistent().get(&StorageKey::IssuerStats(issuer.clone()))
.unwrap_or(IssuerStats { total_issued: 0 })
}
pub fn set_issuer_stats(env: &Env, issuer: &Address, stats: &IssuerStats) {
let key = StorageKey::IssuerStats(issuer.clone());
let ttl = get_ttl_lifetime(env);
env.storage().persistent().set(&key, stats);
env.storage().persistent().extend_ttl(&key, ttl, ttl);
}
pub fn set_issuer_tier(env: &Env, issuer: &Address, tier: &IssuerTier) {
let key = StorageKey::IssuerTier(issuer.clone());
let ttl = get_ttl_lifetime(env);
env.storage().persistent().set(&key, tier);
env.storage().persistent().extend_ttl(&key, ttl, ttl);
}
pub fn get_issuer_tier(env: &Env, issuer: &Address) -> Option<IssuerTier> {
env.storage().persistent().get(&StorageKey::IssuerTier(issuer.clone()))
}
pub fn get_limits(env: &Env) -> StorageLimits {
env.storage().instance().get(&StorageKey::StorageLimits).unwrap_or_default()
}
pub fn set_limits(env: &Env, limits: &StorageLimits) {
let ttl = get_ttl_lifetime(env);
env.storage().instance().set(&StorageKey::StorageLimits, limits);
env.storage().instance().extend_ttl(ttl, ttl);
}
pub fn get_rate_limit_config(env: &Env) -> Option<RateLimitConfig> {
env.storage().instance().get(&StorageKey::RateLimitConfig)
}
pub fn set_rate_limit_config(env: &Env, config: &RateLimitConfig) {
let ttl = get_ttl_lifetime(env);
env.storage().instance().set(&StorageKey::RateLimitConfig, config);
env.storage().instance().extend_ttl(ttl, ttl);
}
/// Get the per-claim-type rate limit override for a claim type, or None if not set.
pub fn get_claim_type_rate_limit(env: &Env, claim_type: &String) -> Option<u64> {
env.storage()
.instance()
.get(&StorageKey::ClaimTypeRateLimit(claim_type.clone()))
}
/// Set a per-claim-type rate limit override.
pub fn set_claim_type_rate_limit(env: &Env, claim_type: &String, interval_secs: u64) {
let ttl = get_ttl_lifetime(env);
env.storage()
.instance()
.set(&StorageKey::ClaimTypeRateLimit(claim_type.clone()), &interval_secs);
env.storage().instance().extend_ttl(ttl, ttl);
}
pub fn get_last_issuance_time(env: &Env, issuer: &Address) -> Option<u64> {
env.storage().persistent().get(&StorageKey::LastIssuanceTime(issuer.clone()))
}
pub fn set_last_issuance_time(env: &Env, issuer: &Address, timestamp: u64) {
let key = StorageKey::LastIssuanceTime(issuer.clone());
let ttl = get_ttl_lifetime(env);
env.storage().persistent().set(&key, ×tamp);
env.storage().persistent().extend_ttl(&key, ttl, ttl);
}
pub fn get_audit_log(env: &Env, attestation_id: &String) -> Vec<AuditEntry> {
env.storage().persistent().get(&StorageKey::AuditLog(attestation_id.clone())).unwrap_or(Vec::new(env))
}
pub fn append_audit_entry(env: &Env, attestation_id: &String, entry: &AuditEntry) {
let key = StorageKey::AuditLog(attestation_id.clone());
let ttl = get_ttl_lifetime(env);
let mut log = Self::get_audit_log(env, attestation_id);
log.push_back(entry.clone());
env.storage().persistent().set(&key, &log);
env.storage().persistent().extend_ttl(&key, ttl, ttl);
}
pub fn get_expiration_hook(env: &Env, subject: &Address) -> Option<ExpirationHook> {
env.storage().persistent().get(&StorageKey::ExpirationHook(subject.clone()))
}
pub fn set_expiration_hook(env: &Env, subject: &Address, hook: &ExpirationHook) {
let key = StorageKey::ExpirationHook(subject.clone());
let ttl = get_ttl_lifetime(env);
env.storage().persistent().set(&key, hook);
env.storage().persistent().extend_ttl(&key, ttl, ttl);
}
pub fn remove_expiration_hook(env: &Env, subject: &Address) {
env.storage().persistent().remove(&StorageKey::ExpirationHook(subject.clone()));
}
pub fn get_multisig_proposal(env: &Env, proposal_id: &String) -> Result<MultiSigProposal, Error> {
env.storage().persistent().get(&StorageKey::MultiSigProposal(proposal_id.clone())).ok_or(Error::NotFound)
}
pub fn set_multisig_proposal(env: &Env, proposal: &MultiSigProposal) {
let key = StorageKey::MultiSigProposal(proposal.id.clone());
let ttl = get_ttl_lifetime(env);
env.storage().persistent().set(&key, proposal);
env.storage().persistent().extend_ttl(&key, ttl, ttl);
}
pub fn get_multisig_ttl_days(env: &Env) -> u32 {
env.storage().instance().get(&StorageKey::MultisigTtlDays).unwrap_or(7)
}
pub fn get_endorsements(env: &Env, attestation_id: &String) -> Vec<Endorsement> {
env.storage().persistent().get(&StorageKey::Endorsements(attestation_id.clone())).unwrap_or(Vec::new(env))
}
pub fn add_endorsement(env: &Env, attestation_id: &String, endorsement: &Endorsement) {
let key = StorageKey::Endorsements(attestation_id.clone());
let ttl = get_ttl_lifetime(env);
let mut list = Self::get_endorsements(env, attestation_id);
list.push_back(endorsement.clone());
env.storage().persistent().set(&key, &list);
env.storage().persistent().extend_ttl(&key, ttl, ttl);
let endorser_key = StorageKey::EndorserIndex(endorsement.endorser.clone());
let mut endorser_list: Vec<Endorsement> = env.storage().persistent().get(&endorser_key).unwrap_or(Vec::new(env));
endorser_list.push_back(endorsement.clone());
env.storage().persistent().set(&endorser_key, &endorser_list);
env.storage().persistent().extend_ttl(&endorser_key, ttl, ttl);
}
pub fn next_proposal_id(env: &Env) -> u32 {
let current: u32 = env.storage().instance().get(&StorageKey::ProposalCounter).unwrap_or(0);
let next = current + 1;
env.storage().instance().set(&StorageKey::ProposalCounter, &next);
next
}
// -------------------------------------------------------------------------
// Attestation requests
// -------------------------------------------------------------------------
pub fn get_attestation_request(env: &Env, request_id: &String) -> Result<AttestationRequest, Error> {
env.storage()
.persistent()
.get(&StorageKey::AttestationRequest(request_id.clone()))
.ok_or(Error::NotFound)
}
pub fn set_attestation_request(env: &Env, request: &AttestationRequest) {
let key = StorageKey::AttestationRequest(request.id.clone());
let ttl = get_ttl_lifetime(env);
env.storage().persistent().set(&key, request);
env.storage().persistent().extend_ttl(&key, ttl, ttl);
}
pub fn get_issuer_pending_requests(env: &Env, issuer: &Address) -> Vec<String> {
env.storage()
.persistent()
.get(&StorageKey::IssuerPendingRequests(issuer.clone()))
.unwrap_or(Vec::new(env))
}
pub fn add_issuer_pending_request(env: &Env, issuer: &Address, request_id: &String) {
let key = StorageKey::IssuerPendingRequests(issuer.clone());
let ttl = get_ttl_lifetime(env);
let mut list = Self::get_issuer_pending_requests(env, issuer);
list.push_back(request_id.clone());
env.storage().persistent().set(&key, &list);
env.storage().persistent().extend_ttl(&key, ttl, ttl);
}
pub fn remove_issuer_pending_request(env: &Env, issuer: &Address, request_id: &String) {
let key = StorageKey::IssuerPendingRequests(issuer.clone());
let ttl = get_ttl_lifetime(env);
let existing = Self::get_issuer_pending_requests(env, issuer);
let mut updated = Vec::new(env);
for id in existing.iter() {
if &id != request_id {
updated.push_back(id);
}
}
env.storage().persistent().set(&key, &updated);
env.storage().persistent().extend_ttl(&key, ttl, ttl);
}
// ── Delegation ────────────────────────────────────────────────────────────
pub fn set_delegation(env: &Env, delegation: &crate::types::Delegation) {
let key = StorageKey::Delegation(
delegation.delegator.clone(),
delegation.delegate.clone(),
delegation.claim_type.clone(),
);
let ttl = get_ttl_lifetime(env);
env.storage().persistent().set(&key, delegation);
env.storage().persistent().extend_ttl(&key, ttl, ttl);
let idx_key = StorageKey::DelegatorIndex(delegation.delegator.clone());
let mut index: Vec<(Address, String)> = env.storage().persistent().get(&idx_key).unwrap_or(Vec::new(env));
let entry = (delegation.delegate.clone(), delegation.claim_type.clone());
if !index.contains(&entry) {
index.push_back(entry);
env.storage().persistent().set(&idx_key, &index);
env.storage().persistent().extend_ttl(&idx_key, ttl, ttl);
}
}
pub fn get_delegation(
env: &Env,
delegator: &Address,
delegate: &Address,
claim_type: &String,
) -> Option<crate::types::Delegation> {
let key = StorageKey::Delegation(delegator.clone(), delegate.clone(), claim_type.clone());
env.storage().persistent().get(&key)
}
pub fn remove_delegation(
env: &Env,
delegator: &Address,
delegate: &Address,
claim_type: &String,
) {
let key = StorageKey::Delegation(delegator.clone(), delegate.clone(), claim_type.clone());
env.storage().persistent().remove(&key);
}
// ── Attestation requests ──────────────────────────────────────────────────
pub fn get_request(env: &Env, request_id: &String) -> Result<crate::types::AttestationRequest, crate::types::Error> {
env.storage()
.persistent()
.get(&StorageKey::AttestationRequest(request_id.clone()))
.ok_or(crate::types::Error::NotFound)
}
pub fn set_request(env: &Env, request: &crate::types::AttestationRequest) {
let key = StorageKey::AttestationRequest(request.id.clone());
let ttl = get_ttl_lifetime(env);
env.storage().persistent().set(&key, request);
env.storage().persistent().extend_ttl(&key, ttl, ttl);
}
pub fn get_pending_request_ids(env: &Env, issuer: &Address) -> Vec<String> {
env.storage()
.persistent()
.get(&StorageKey::IssuerPendingRequests(issuer.clone()))
.unwrap_or(Vec::new(env))
}
pub fn add_pending_request(env: &Env, issuer: &Address, request_id: &String) {
let key = StorageKey::IssuerPendingRequests(issuer.clone());
let ttl = get_ttl_lifetime(env);
let mut list = Self::get_pending_request_ids(env, issuer);
list.push_back(request_id.clone());
env.storage().persistent().set(&key, &list);
env.storage().persistent().extend_ttl(&key, ttl, ttl);
}
pub fn remove_pending_request(env: &Env, issuer: &Address, request_id: &String) {
let key = StorageKey::IssuerPendingRequests(issuer.clone());
let ttl = get_ttl_lifetime(env);
let existing = Self::get_pending_request_ids(env, issuer);
let mut updated = Vec::new(env);
for id in existing.iter() {
if &id != request_id {
updated.push_back(id);
}
}
env.storage().persistent().set(&key, &updated);
env.storage().persistent().extend_ttl(&key, ttl, ttl);
}
// ── Pending admin transfer ────────────────────────────────────────────────
pub fn set_pending_admin_transfer(env: &Env, transfer: &PendingAdminTransfer) {
let ttl = get_ttl_lifetime(env);
env.storage().instance().set(&StorageKey::PendingAdminTransfer, transfer);
env.storage().instance().extend_ttl(ttl, ttl);
}
pub fn get_pending_admin_transfer(env: &Env) -> Option<PendingAdminTransfer> {
env.storage().instance().get(&StorageKey::PendingAdminTransfer)
}
pub fn remove_pending_admin_transfer(env: &Env) {
env.storage().instance().remove(&StorageKey::PendingAdminTransfer);
}
// ── Attestation templates ─────────────────────────────────────────────────
pub fn set_template(env: &Env, issuer: &Address, template_id: &String, template: &AttestationTemplate) {
let key = StorageKey::AttestationTemplate(issuer.clone(), template_id.clone());
let ttl = get_ttl_lifetime(env);
env.storage().persistent().set(&key, template);
env.storage().persistent().extend_ttl(&key, ttl, ttl);
}
pub fn get_template(env: &Env, issuer: &Address, template_id: &String) -> Option<AttestationTemplate> {
env.storage().persistent().get(&StorageKey::AttestationTemplate(issuer.clone(), template_id.clone()))
}
pub fn add_to_template_registry(env: &Env, issuer: &Address, template_id: &String) {
let key = StorageKey::AttestationTemplateList(issuer.clone());
let ttl = get_ttl_lifetime(env);
let mut list: Vec<String> = env.storage().persistent().get(&key).unwrap_or(Vec::new(env));
list.push_back(template_id.clone());
env.storage().persistent().set(&key, &list);
env.storage().persistent().extend_ttl(&key, ttl, ttl);
}
pub fn get_template_registry(env: &Env, issuer: &Address) -> Vec<String> {
env.storage()
.persistent()
.get(&StorageKey::AttestationTemplateList(issuer.clone()))
.unwrap_or(Vec::new(env))
}
// ── Valid attestations index ──────────────────────────────────────────────
pub fn get_valid_attestations(env: &Env, subject: &Address) -> Vec<String> {
env.storage()
.persistent()
.get(&StorageKey::ValidAttestations(subject.clone()))
.unwrap_or(Vec::new(env))
}
pub fn add_valid_attestation(env: &Env, subject: &Address, attestation_id: &String) {
let key = StorageKey::ValidAttestations(subject.clone());
let ttl = get_ttl_lifetime(env);
let mut list = Self::get_valid_attestations(env, subject);
list.push_back(attestation_id.clone());
env.storage().persistent().set(&key, &list);
env.storage().persistent().extend_ttl(&key, ttl, ttl);
}
pub fn remove_valid_attestation(env: &Env, subject: &Address, attestation_id: &String) {
let key = StorageKey::ValidAttestations(subject.clone());
let ttl = get_ttl_lifetime(env);
let existing = Self::get_valid_attestations(env, subject);
let mut updated = Vec::new(env);
for id in existing.iter() {
if &id != attestation_id {
updated.push_back(id);
}
}
env.storage().persistent().set(&key, &updated);
env.storage().persistent().extend_ttl(&key, ttl, ttl);
}
// ── Delegator index ───────────────────────────────────────────────────────
pub fn get_delegator_index(env: &Env, delegator: &Address) -> Vec<(Address, String)> {
env.storage()
.persistent()
.get(&StorageKey::DelegatorIndex(delegator.clone()))
.unwrap_or(Vec::new(env))
}
// ── Endorser index ────────────────────────────────────────────────────────
pub fn get_endorsements_by_endorser(env: &Env, endorser: &Address) -> Vec<crate::types::Endorsement> {
env.storage()
.persistent()
.get(&StorageKey::EndorserIndex(endorser.clone()))
.unwrap_or(Vec::new(env))
}
// ── Claim type counts ─────────────────────────────────────────────────────
pub fn get_claim_type_count(env: &Env, claim_type: &String) -> u64 {
env.storage()
.persistent()
.get(&StorageKey::ClaimTypeCount(claim_type.clone()))
.unwrap_or(0u64)
}
pub fn increment_claim_type_count(env: &Env, claim_type: &String) {
let key = StorageKey::ClaimTypeCount(claim_type.clone());
let ttl = get_ttl_lifetime(env);
let current = Self::get_claim_type_count(env, claim_type);
env.storage().persistent().set(&key, ¤t.saturating_add(1));
env.storage().persistent().extend_ttl(&key, ttl, ttl);
}
pub fn decrement_claim_type_count(env: &Env, claim_type: &String) {
let key = StorageKey::ClaimTypeCount(claim_type.clone());
let ttl = get_ttl_lifetime(env);
let current = Self::get_claim_type_count(env, claim_type);
env.storage().persistent().set(&key, ¤t.saturating_sub(1));
env.storage().persistent().extend_ttl(&key, ttl, ttl);
}
}
pub fn paginate(env: &Env, list: &Vec<String>, start: u32, limit: u32) -> Vec<String> {
let mut result = Vec::new(env);
let len = list.len();
if start >= len {
return result;
}
let end = (start + limit).min(len);
for i in start..end {
if let Some(item) = list.get(i) {
result.push_back(item);
}
}
result
}
pub fn paginate_addresses(env: &Env, list: &Vec<Address>, start: u32, limit: u32) -> Vec<Address> {
let mut result = Vec::new(env);
let len = list.len();
if start >= len {
return result;
}
let end = (start + limit).min(len);
for i in start..end {
if let Some(item) = list.get(i) {
result.push_back(item);
}
}
result
}
const CHUNKED_INDEX_CHUNK_SIZE: u32 = 50;
pub struct ChunkedIndex;
impl ChunkedIndex {
fn subject_chunk_key(subject: &Address, chunk_index: u32) -> StorageKey {
StorageKey::SubjectAttestationChunk(subject.clone(), chunk_index)
}
fn issuer_chunk_key(issuer: &Address, chunk_index: u32) -> StorageKey {
StorageKey::IssuerAttestationChunk(issuer.clone(), chunk_index)
}
fn get_subject_chunk(env: &Env, subject: &Address, chunk_index: u32) -> Vec<String> {
env.storage()
.persistent()
.get(&Self::subject_chunk_key(subject, chunk_index))
.unwrap_or(Vec::new(env))
}
fn get_issuer_chunk(env: &Env, issuer: &Address, chunk_index: u32) -> Vec<String> {
env.storage()
.persistent()
.get(&Self::issuer_chunk_key(issuer, chunk_index))
.unwrap_or(Vec::new(env))
}
fn set_subject_chunk(env: &Env, subject: &Address, chunk_index: u32, chunk: &Vec<String>) {
let ttl = get_ttl_lifetime(env);
let key = Self::subject_chunk_key(subject, chunk_index);
env.storage().persistent().set(&key, chunk);
env.storage().persistent().extend_ttl(&key, ttl, ttl);
}
fn set_issuer_chunk(env: &Env, issuer: &Address, chunk_index: u32, chunk: &Vec<String>) {
let ttl = get_ttl_lifetime(env);
let key = Self::issuer_chunk_key(issuer, chunk_index);
env.storage().persistent().set(&key, chunk);
env.storage().persistent().extend_ttl(&key, ttl, ttl);
}
fn write_subject_chunks(env: &Env, subject: &Address, ids: &Vec<String>) {
let ttl = get_ttl_lifetime(env);
let mut chunk_index = 0;
let mut offset = 0;
while offset < ids.len() {
let mut chunk = Vec::new(env);
for _ in 0..CHUNKED_INDEX_CHUNK_SIZE {
if let Some(id) = ids.get(offset) {
chunk.push_back(id.clone());
offset += 1;
} else {
break;
}
}
if chunk.len() == 0 {