Repository navigation
Expand file tree
/
Copy pathtypes.rs
More file actions
211 lines (192 loc) · 6.5 KB
/
Copy pathtypes.rs
File metadata and controls
211 lines (192 loc) · 6.5 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
use soroban_sdk::{contracttype, contracterror, Address, Env, String};
#[contracterror]
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum Error {
NotInitialized = 1,
AlreadyInitialized = 2,
Unauthorized = 3,
NotFound = 4,
AlreadyRevoked = 5,
DuplicateAttestation = 6,
InvalidValidFrom = 7,
InvalidExpiration = 8,
}
#[contracttype]
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum AttestationStatus {
Valid,
Expired,
Revoked,
Pending,
}
#[contracttype]
#[derive(Clone, Debug)]
pub struct Attestation {
pub id: String,
pub issuer: Address,
pub subject: Address,
pub claim_type: String,
pub timestamp: u64,
pub expiration: Option<u64>,
pub revoked: bool,
pub valid_from: Option<u64>,
}
impl Attestation {
pub fn get_status(&self, current_time: u64) -> AttestationStatus {
if let Some(vf) = self.valid_from {
if current_time < vf {
return AttestationStatus::Pending;
}
}
if self.revoked {
return AttestationStatus::Revoked;
}
if let Some(exp) = self.expiration {
if current_time >= exp {
return AttestationStatus::Expired;
}
}
AttestationStatus::Valid
}
use soroban_sdk::{contracterror, contracttype, Address, Env, String};
/// Contract metadata returned by `get_contract_metadata`.
#[contracttype]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ContractMetadata {
/// Contract name.
pub name: String,
/// Semver version string, e.g. `"1.0.0"`.
pub version: String,
/// Short description of the contract.
pub description: String,
}
/// A single attestation record stored on-chain.
#[contracttype]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Attestation {
/// Deterministic hash-based identifier for this attestation.
pub id: String,
/// Address that created the attestation.
pub issuer: Address,
/// Address the attestation is about.
pub subject: Address,
/// Free-form claim label, e.g. `"KYC_PASSED"`.
pub claim_type: String,
/// Ledger timestamp (seconds) when the attestation was created.
pub timestamp: u64,
/// Optional Unix timestamp after which the attestation is expired.
pub expiration: Option<u64>,
/// `true` if the issuer has explicitly revoked this attestation.
pub revoked: bool,
}
/// Metadata an issuer can associate with their address so consumers can
/// identify who issued an attestation.
#[contracttype]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct IssuerMetadata {
/// Human-readable display name for the issuer.
pub name: String,
/// URL pointing to the issuer's website or documentation.
pub url: String,
/// Short description of the issuer and the claims they issue.
pub description: String,
}
/// The current validity state of an attestation.
#[contracttype]
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum AttestationStatus {
/// Attestation is active and has not expired.
Valid,
/// Attestation has passed its expiration timestamp.
Expired,
/// Attestation was explicitly revoked by its issuer.
Revoked,
}
/// Errors returned by TrustLink contract functions.
#[contracterror]
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[repr(u32)]
pub enum Error {
/// [`initialize`](crate::TrustLinkContract::initialize) was called more than once.
AlreadyInitialized = 1,
/// A function was called before [`initialize`](crate::TrustLinkContract::initialize).
NotInitialized = 2,
/// The caller lacks the required admin or issuer role.
Unauthorized = 3,
/// No attestation exists with the requested ID.
NotFound = 4,
/// An attestation with the same deterministic ID already exists.
DuplicateAttestation = 5,
/// The attestation has already been revoked.
AlreadyRevoked = 6,
/// The attestation has passed its expiration timestamp.
Expired = 7,
}
impl Attestation {
/// Generate a deterministic attestation ID by SHA-256 hashing the tuple
/// `(issuer, subject, claim_type, timestamp)`.
///
/// The first 16 bytes of the hash are used as the ID to keep it compact
/// while still being collision-resistant for practical purposes.
///
/// # Parameters
/// - `issuer` — issuer address.
/// - `subject` — subject address.
/// - `claim_type` — claim label string.
/// - `timestamp` — ledger timestamp at creation time.
///
/// # Returns
/// A [`String`] containing the raw 16-byte ID.
pub fn generate_id(
env: &Env,
issuer: &Address,
subject: &Address,
claim_type: &String,
timestamp: u64,
) -> String {
use soroban_sdk::xdr::ToXdr;
use soroban_sdk::Bytes;
let mut data = Bytes::new(env);
let issuer_xdr = issuer.clone().to_xdr(env);
data.append(&issuer_xdr);
let subject_xdr = subject.clone().to_xdr(env);
data.append(&subject_xdr);
let claim_bytes = claim_type.clone().to_xdr(env);
data.append(&claim_bytes);
let ts_bytes = timestamp.to_be_bytes();
data.append(&Bytes::from_array(env, &ts_bytes));
let hash = env.crypto().sha256(&data);
let hex_chars: &[u8] = b"0123456789abcdef";
let hash_bytes = hash.to_array();
let mut arr = [0u8; 64];
for (i, byte) in hash_bytes.iter().enumerate() {
arr[i * 2] = hex_chars[(byte >> 4) as usize];
arr[i * 2 + 1] = hex_chars[(byte & 0xf) as usize];
}
String::from_bytes(env, &arr)
let data = (issuer.clone(), subject.clone(), claim_type.clone(), timestamp);
let xdr_bytes = data.to_xdr(env);
let hash = env.crypto().sha256(&xdr_bytes);
let hash_bytes = hash.to_array();
// Use first 16 bytes as ID
String::from_bytes(env, &hash_bytes[..16])
}
/// Compute the current [`AttestationStatus`] given `current_time`.
///
/// Revocation takes precedence: a revoked attestation always returns
/// [`AttestationStatus::Revoked`] regardless of its expiration field.
///
/// # Parameters
/// - `current_time` — current ledger timestamp in seconds.
pub fn get_status(&self, current_time: u64) -> AttestationStatus {
if self.revoked {
return AttestationStatus::Revoked;
}
if let Some(exp) = self.expiration {
if current_time > exp {
return AttestationStatus::Expired;
}
}
AttestationStatus::Valid
}
}