78 lines
2.3 KiB
Rust
78 lines
2.3 KiB
Rust
// Copyright 2022-2024 - Nym Technologies SA <contact@nymtech.net>
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
use crate::models::StoredIssuedCredential;
|
|
|
|
#[derive(Clone)]
|
|
pub struct CoconutCredentialManager {
|
|
connection_pool: sqlx::SqlitePool,
|
|
}
|
|
|
|
impl CoconutCredentialManager {
|
|
/// Creates new instance of the `CoconutCredentialManager` with the provided sqlite connection pool.
|
|
///
|
|
/// # Arguments
|
|
///
|
|
/// * `connection_pool`: database connection pool to use.
|
|
pub fn new(connection_pool: sqlx::SqlitePool) -> Self {
|
|
CoconutCredentialManager { connection_pool }
|
|
}
|
|
|
|
pub async fn insert_issued_credential(
|
|
&self,
|
|
credential_type: String,
|
|
serialization_revision: u8,
|
|
credential_data: &[u8],
|
|
epoch_id: u32,
|
|
) -> Result<(), sqlx::Error> {
|
|
sqlx::query!(
|
|
r#"
|
|
INSERT INTO coconut_credentials(serialization_revision, credential_type, credential_data, epoch_id, consumed, expired)
|
|
VALUES (?, ?, ?, ?, false, false)
|
|
"#,
|
|
serialization_revision, credential_type, credential_data, epoch_id
|
|
).execute(&self.connection_pool).await?;
|
|
Ok(())
|
|
}
|
|
|
|
pub async fn get_next_unspent_credential(
|
|
&self,
|
|
) -> Result<Option<StoredIssuedCredential>, sqlx::Error> {
|
|
sqlx::query_as(
|
|
"SELECT * FROM coconut_credentials WHERE NOT consumed AND NOT expired LIMIT 1",
|
|
)
|
|
.fetch_optional(&self.connection_pool)
|
|
.await
|
|
}
|
|
|
|
/// Consumes in the database the specified credential.
|
|
///
|
|
/// # Arguments
|
|
///
|
|
/// * `id`: Database id.
|
|
pub async fn consume_coconut_credential(&self, id: i64) -> Result<(), sqlx::Error> {
|
|
sqlx::query!(
|
|
"UPDATE coconut_credentials SET consumed = TRUE WHERE id = ?",
|
|
id
|
|
)
|
|
.execute(&self.connection_pool)
|
|
.await?;
|
|
Ok(())
|
|
}
|
|
|
|
/// Marks the specified credential as expired
|
|
///
|
|
/// # Arguments
|
|
///
|
|
/// * `id`: Id of the credential to mark as expired.
|
|
pub async fn mark_expired(&self, id: i64) -> Result<(), sqlx::Error> {
|
|
sqlx::query!(
|
|
"UPDATE coconut_credentials SET expired = TRUE WHERE id = ?",
|
|
id
|
|
)
|
|
.execute(&self.connection_pool)
|
|
.await?;
|
|
Ok(())
|
|
}
|
|
}
|