feat: implement nut-06 time

This commit is contained in:
Pavol Rusnak
2024-09-02 18:43:36 +02:00
committed by thesimplekid
parent 69be0838c4
commit e67dc15ce6
5 changed files with 33 additions and 4 deletions

View File

@@ -79,6 +79,7 @@ impl JsMintInfo {
nuts: JsValue, nuts: JsValue,
mint_icon_url: Option<String>, mint_icon_url: Option<String>,
motd: Option<String>, motd: Option<String>,
time: Option<u64>,
) -> Result<JsMintInfo> { ) -> Result<JsMintInfo> {
Ok(JsMintInfo { Ok(JsMintInfo {
inner: MintInfo { inner: MintInfo {
@@ -92,6 +93,7 @@ impl JsMintInfo {
nuts: serde_wasm_bindgen::from_value(nuts).map_err(into_err)?, nuts: serde_wasm_bindgen::from_value(nuts).map_err(into_err)?,
mint_icon_url, mint_icon_url,
motd, motd,
time,
}, },
}) })
} }
@@ -152,6 +154,12 @@ impl JsMintInfo {
pub fn motd(&self) -> Option<String> { pub fn motd(&self) -> Option<String> {
self.inner.motd.clone() self.inner.motd.clone()
} }
/// Get time
#[wasm_bindgen(getter)]
pub fn time(&self) -> Option<u64> {
self.inner.time
}
} }
#[wasm_bindgen(js_name = ContactInfo)] #[wasm_bindgen(js_name = ContactInfo)]

View File

@@ -392,7 +392,7 @@ pub async fn post_check(
} }
pub async fn get_mint_info(State(state): State<MintState>) -> Result<Json<MintInfo>, Response> { pub async fn get_mint_info(State(state): State<MintState>) -> Result<Json<MintInfo>, Response> {
Ok(Json(state.mint.mint_info().clone())) Ok(Json(state.mint.mint_info().clone().time(unix_time())))
} }
pub async fn post_swap( pub async fn post_swap(

View File

@@ -0,0 +1 @@
ALTER TABLE mint ADD mint_time INTEGER;

View File

@@ -92,6 +92,7 @@ impl WalletDatabase for WalletSqliteDatabase {
nuts, nuts,
mint_icon_url, mint_icon_url,
motd, motd,
time,
) = match mint_info { ) = match mint_info {
Some(mint_info) => { Some(mint_info) => {
let MintInfo { let MintInfo {
@@ -104,6 +105,7 @@ impl WalletDatabase for WalletSqliteDatabase {
nuts, nuts,
mint_icon_url, mint_icon_url,
motd, motd,
time,
} = mint_info; } = mint_info;
( (
@@ -116,16 +118,17 @@ impl WalletDatabase for WalletSqliteDatabase {
serde_json::to_string(&nuts).ok(), serde_json::to_string(&nuts).ok(),
mint_icon_url, mint_icon_url,
motd, motd,
time,
) )
} }
None => (None, None, None, None, None, None, None, None, None), None => (None, None, None, None, None, None, None, None, None, None),
}; };
sqlx::query( sqlx::query(
r#" r#"
INSERT OR REPLACE INTO mint INSERT OR REPLACE INTO mint
(mint_url, name, pubkey, version, description, description_long, contact, nuts, mint_icon_url, motd) (mint_url, name, pubkey, version, description, description_long, contact, nuts, mint_icon_url, motd, mint_time)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?); VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);
"#, "#,
) )
.bind(mint_url.to_string()) .bind(mint_url.to_string())
@@ -138,6 +141,7 @@ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);
.bind(nuts) .bind(nuts)
.bind(mint_icon_url) .bind(mint_icon_url)
.bind(motd) .bind(motd)
.bind(time.map(|v| v as i64))
.execute(&self.pool) .execute(&self.pool)
.await .await
.map_err(Error::from)?; .map_err(Error::from)?;
@@ -771,6 +775,7 @@ fn sqlite_row_to_mint_info(row: &SqliteRow) -> Result<MintInfo, Error> {
let row_nuts: Option<String> = row.try_get("nuts").map_err(Error::from)?; let row_nuts: Option<String> = row.try_get("nuts").map_err(Error::from)?;
let mint_icon_url: Option<String> = row.try_get("mint_icon_url").map_err(Error::from)?; let mint_icon_url: Option<String> = row.try_get("mint_icon_url").map_err(Error::from)?;
let motd: Option<String> = row.try_get("motd").map_err(Error::from)?; let motd: Option<String> = row.try_get("motd").map_err(Error::from)?;
let time: Option<i64> = row.try_get("mint_time").map_err(Error::from)?;
Ok(MintInfo { Ok(MintInfo {
name, name,
@@ -784,6 +789,7 @@ fn sqlite_row_to_mint_info(row: &SqliteRow) -> Result<MintInfo, Error> {
.unwrap_or_default(), .unwrap_or_default(),
mint_icon_url, mint_icon_url,
motd, motd,
time: time.map(|t| t as u64),
}) })
} }

View File

@@ -79,6 +79,9 @@ pub struct MintInfo {
/// message of the day that the wallet must display to the user /// message of the day that the wallet must display to the user
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
pub motd: Option<String>, pub motd: Option<String>,
/// server unix timestamp
#[serde(skip_serializing_if = "Option::is_none")]
pub time: Option<u64>,
} }
impl MintInfo { impl MintInfo {
@@ -170,6 +173,17 @@ impl MintInfo {
..self ..self
} }
} }
/// Set time
pub fn time<S>(self, time: S) -> Self
where
S: Into<u64>,
{
Self {
time: Some(time.into()),
..self
}
}
} }
/// Supported nuts and settings /// Supported nuts and settings