|
| 1 | +//! Core credential operations: issue, revoke, query, and status checks. |
| 2 | +
|
| 3 | +use soroban_sdk::{symbol_short, Address, Bytes, BytesN, Env}; |
| 4 | + |
| 5 | +use crate::events::{Crediss, Credrev}; |
| 6 | + |
| 7 | +/// A credential record stored on-chain: |
| 8 | +/// `(issuer_did, subject_did, metadata_ptr, expires_at, status)` |
| 9 | +type CredRecord = (Bytes, Bytes, Bytes, i128, i32); |
| 10 | + |
| 11 | +pub struct CredentialManager; |
| 12 | + |
| 13 | +impl CredentialManager { |
| 14 | + /// Issue a new credential. |
| 15 | + /// |
| 16 | + /// Panics if a credential with the same hash already exists. |
| 17 | + pub fn issue( |
| 18 | + env: &Env, |
| 19 | + credential_hash: BytesN<32>, |
| 20 | + issuer: Address, |
| 21 | + issuer_did: Bytes, |
| 22 | + subject_did: Bytes, |
| 23 | + metadata_ptr: Bytes, |
| 24 | + expires_at: i128, |
| 25 | + ) { |
| 26 | + issuer.require_auth(); |
| 27 | + |
| 28 | + let key = (symbol_short!("cred"), credential_hash.clone()); |
| 29 | + assert!( |
| 30 | + !env.storage().persistent().has(&key), |
| 31 | + "credential already exists" |
| 32 | + ); |
| 33 | + |
| 34 | + let record: CredRecord = ( |
| 35 | + issuer_did.clone(), |
| 36 | + subject_did.clone(), |
| 37 | + metadata_ptr.clone(), |
| 38 | + expires_at, |
| 39 | + 0i32, |
| 40 | + ); |
| 41 | + env.storage().persistent().set(&key, &record); |
| 42 | + |
| 43 | + Crediss { |
| 44 | + credential_hash, |
| 45 | + issuer_did, |
| 46 | + subject_did, |
| 47 | + metadata_ptr, |
| 48 | + expires_at, |
| 49 | + } |
| 50 | + .publish(env); |
| 51 | + } |
| 52 | + |
| 53 | + /// Revoke an existing credential. |
| 54 | + /// |
| 55 | + /// Panics if the credential does not exist. |
| 56 | + pub fn revoke(env: &Env, credential_hash: BytesN<32>, issuer: Address) { |
| 57 | + issuer.require_auth(); |
| 58 | + |
| 59 | + let key = (symbol_short!("cred"), credential_hash.clone()); |
| 60 | + let opt: Option<CredRecord> = env.storage().persistent().get(&key); |
| 61 | + |
| 62 | + match opt { |
| 63 | + Some((issuer_did, subject_did, metadata_ptr, expires_at, _)) => { |
| 64 | + let record: CredRecord = |
| 65 | + (issuer_did.clone(), subject_did.clone(), metadata_ptr, expires_at, 1i32); |
| 66 | + env.storage().persistent().set(&key, &record); |
| 67 | + |
| 68 | + Credrev { |
| 69 | + credential_hash, |
| 70 | + issuer_did, |
| 71 | + subject_did, |
| 72 | + } |
| 73 | + .publish(env); |
| 74 | + } |
| 75 | + None => panic!("credential not found"), |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + /// Retrieve a raw credential record, or `None` if not found. |
| 80 | + pub fn get(env: &Env, credential_hash: BytesN<32>) -> Option<CredRecord> { |
| 81 | + let key = (symbol_short!("cred"), credential_hash); |
| 82 | + env.storage().persistent().get(&key) |
| 83 | + } |
| 84 | + |
| 85 | + /// Return `true` if the credential exists, is not revoked, and has not expired. |
| 86 | + pub fn is_active(env: &Env, credential_hash: BytesN<32>, now_ts: i128) -> bool { |
| 87 | + match Self::get(env, credential_hash) { |
| 88 | + Some((_, _, _, expires_at, status)) => { |
| 89 | + if status == 1 { |
| 90 | + return false; |
| 91 | + } |
| 92 | + if expires_at > 0 && now_ts > expires_at { |
| 93 | + return false; |
| 94 | + } |
| 95 | + true |
| 96 | + } |
| 97 | + None => false, |
| 98 | + } |
| 99 | + } |
| 100 | +} |
0 commit comments