mirror of
https://github.com/aljazceru/cdk.git
synced 2025-12-19 05:35:18 +01:00
Keysets V2 (#702)
--------- Co-authored-by: thesimplekid <tsk@thesimplekid.com>
This commit is contained in:
@@ -121,7 +121,7 @@ impl MintAuthDatabase for MintSqliteAuthDatabase {
|
||||
.bind(":unit", keyset.unit.to_string())
|
||||
.bind(":active", keyset.active)
|
||||
.bind(":valid_from", keyset.valid_from as i64)
|
||||
.bind(":valid_to", keyset.valid_to.map(|v| v as i64))
|
||||
.bind(":valid_to", keyset.final_expiry.map(|v| v as i64))
|
||||
.bind(":derivation_path", keyset.derivation_path.to_string())
|
||||
.bind(":max_order", keyset.max_order)
|
||||
.bind(":derivation_path_index", keyset.derivation_path_index)
|
||||
|
||||
@@ -214,7 +214,7 @@ impl MintKeysDatabase for MintSqliteDatabase {
|
||||
.bind(":unit", keyset.unit.to_string())
|
||||
.bind(":active", keyset.active)
|
||||
.bind(":valid_from", keyset.valid_from as i64)
|
||||
.bind(":valid_to", keyset.valid_to.map(|v| v as i64))
|
||||
.bind(":valid_to", keyset.final_expiry.map(|v| v as i64))
|
||||
.bind(":derivation_path", keyset.derivation_path.to_string())
|
||||
.bind(":max_order", keyset.max_order)
|
||||
.bind(":input_fee_ppk", keyset.input_fee_ppk as i64)
|
||||
@@ -1134,11 +1134,11 @@ fn sqlite_row_to_keyset_info(row: Vec<Column>) -> Result<MintKeySetInfo, Error>
|
||||
unit: column_as_string!(unit, CurrencyUnit::from_str),
|
||||
active: matches!(active, Column::Integer(1)),
|
||||
valid_from: column_as_number!(valid_from),
|
||||
valid_to: column_as_nullable_number!(valid_to),
|
||||
derivation_path: column_as_string!(derivation_path, DerivationPath::from_str),
|
||||
derivation_path_index: column_as_nullable_number!(derivation_path_index),
|
||||
max_order: column_as_number!(max_order),
|
||||
input_fee_ppk: column_as_number!(row_keyset_ppk),
|
||||
final_expiry: column_as_nullable_number!(valid_to),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1319,11 +1319,11 @@ mod tests {
|
||||
unit: CurrencyUnit::Sat,
|
||||
active: true,
|
||||
valid_from: 0,
|
||||
valid_to: None,
|
||||
derivation_path: bitcoin::bip32::DerivationPath::from_str("m/0'/0'/0'").unwrap(),
|
||||
derivation_path_index: Some(0),
|
||||
max_order: 32,
|
||||
input_fee_ppk: 0,
|
||||
final_expiry: None,
|
||||
};
|
||||
db.add_keyset_info(keyset_info).await.unwrap();
|
||||
|
||||
@@ -1387,11 +1387,11 @@ mod tests {
|
||||
unit: CurrencyUnit::Sat,
|
||||
active: true,
|
||||
valid_from: 0,
|
||||
valid_to: None,
|
||||
derivation_path: bitcoin::bip32::DerivationPath::from_str("m/0'/0'/0'").unwrap(),
|
||||
derivation_path_index: Some(0),
|
||||
max_order: 32,
|
||||
input_fee_ppk: 0,
|
||||
final_expiry: None,
|
||||
};
|
||||
db.add_keyset_info(keyset_info).await.unwrap();
|
||||
|
||||
|
||||
@@ -16,4 +16,5 @@ pub static MIGRATIONS: &[(&str, &str)] = &[
|
||||
("20250314082116_allow_pending_spent.sql", include_str!(r#"./migrations/20250314082116_allow_pending_spent.sql"#)),
|
||||
("20250323152040_wallet_dleq_proofs.sql", include_str!(r#"./migrations/20250323152040_wallet_dleq_proofs.sql"#)),
|
||||
("20250401120000_add_transactions_table.sql", include_str!(r#"./migrations/20250401120000_add_transactions_table.sql"#)),
|
||||
("20250616144830_add_keyset_expiry.sql", include_str!(r#"./migrations/20250616144830_add_keyset_expiry.sql"#)),
|
||||
];
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
ALTER TABLE keyset ADD COLUMN final_expiry INTEGER DEFAULT NULL;
|
||||
@@ -14,8 +14,8 @@ use cdk_common::nuts::{MeltQuoteState, MintQuoteState};
|
||||
use cdk_common::secret::Secret;
|
||||
use cdk_common::wallet::{self, MintQuote, Transaction, TransactionDirection, TransactionId};
|
||||
use cdk_common::{
|
||||
database, Amount, CurrencyUnit, Id, KeySetInfo, Keys, MintInfo, Proof, ProofDleq, PublicKey,
|
||||
SecretKey, SpendingConditions, State,
|
||||
database, Amount, CurrencyUnit, Id, KeySet, KeySetInfo, Keys, MintInfo, Proof, ProofDleq,
|
||||
PublicKey, SecretKey, SpendingConditions, State,
|
||||
};
|
||||
use error::Error;
|
||||
use tracing::instrument;
|
||||
@@ -294,14 +294,15 @@ ON CONFLICT(mint_url) DO UPDATE SET
|
||||
Statement::new(
|
||||
r#"
|
||||
INSERT INTO keyset
|
||||
(mint_url, id, unit, active, input_fee_ppk)
|
||||
(mint_url, id, unit, active, input_fee_ppk, final_expiry)
|
||||
VALUES
|
||||
(:mint_url, :id, :unit, :active, :input_fee_ppk)
|
||||
(:mint_url, :id, :unit, :active, :input_fee_ppk, :final_expiry)
|
||||
ON CONFLICT(id) DO UPDATE SET
|
||||
mint_url = excluded.mint_url,
|
||||
unit = excluded.unit,
|
||||
active = excluded.active,
|
||||
input_fee_ppk = excluded.input_fee_ppk;
|
||||
input_fee_ppk = excluded.input_fee_ppk,
|
||||
final_expiry = excluded.final_expiry;
|
||||
"#,
|
||||
)
|
||||
.bind(":mint_url", mint_url.to_string())
|
||||
@@ -309,6 +310,7 @@ ON CONFLICT(mint_url) DO UPDATE SET
|
||||
.bind(":unit", keyset.unit.to_string())
|
||||
.bind(":active", keyset.active)
|
||||
.bind(":input_fee_ppk", keyset.input_fee_ppk as i64)
|
||||
.bind(":final_expiry", keyset.final_expiry.map(|v| v as i64))
|
||||
.execute(&conn)
|
||||
.map_err(Error::Sqlite)?;
|
||||
}
|
||||
@@ -327,7 +329,8 @@ ON CONFLICT(mint_url) DO UPDATE SET
|
||||
id,
|
||||
unit,
|
||||
active,
|
||||
input_fee_ppk
|
||||
input_fee_ppk,
|
||||
final_expiry
|
||||
FROM
|
||||
keyset
|
||||
WHERE mint_url = :mint_url
|
||||
@@ -354,7 +357,8 @@ ON CONFLICT(mint_url) DO UPDATE SET
|
||||
id,
|
||||
unit,
|
||||
active,
|
||||
input_fee_ppk
|
||||
input_fee_ppk,
|
||||
final_expiry
|
||||
FROM
|
||||
keyset
|
||||
WHERE id = :id
|
||||
@@ -528,7 +532,10 @@ ON CONFLICT(id) DO UPDATE SET
|
||||
}
|
||||
|
||||
#[instrument(skip_all)]
|
||||
async fn add_keys(&self, keys: Keys) -> Result<(), Self::Err> {
|
||||
async fn add_keys(&self, keyset: KeySet) -> Result<(), Self::Err> {
|
||||
// Recompute ID for verification
|
||||
keyset.verify_id()?;
|
||||
|
||||
Statement::new(
|
||||
r#"
|
||||
INSERT INTO key
|
||||
@@ -539,8 +546,11 @@ ON CONFLICT(id) DO UPDATE SET
|
||||
keys = excluded.keys
|
||||
"#,
|
||||
)
|
||||
.bind(":id", Id::from(&keys).to_string())
|
||||
.bind(":keys", serde_json::to_string(&keys).map_err(Error::from)?)
|
||||
.bind(":id", keyset.id.to_string())
|
||||
.bind(
|
||||
":keys",
|
||||
serde_json::to_string(&keyset.keys).map_err(Error::from)?,
|
||||
)
|
||||
.execute(&self.pool.get().map_err(Error::Pool)?)
|
||||
.map_err(Error::Sqlite)?;
|
||||
|
||||
@@ -909,13 +919,15 @@ fn sqlite_row_to_mint_info(row: Vec<Column>) -> Result<MintInfo, Error> {
|
||||
})
|
||||
}
|
||||
|
||||
#[instrument(skip_all)]
|
||||
fn sqlite_row_to_keyset(row: Vec<Column>) -> Result<KeySetInfo, Error> {
|
||||
unpack_into!(
|
||||
let (
|
||||
id,
|
||||
unit,
|
||||
active,
|
||||
input_fee_ppk
|
||||
input_fee_ppk,
|
||||
final_expiry
|
||||
) = row
|
||||
);
|
||||
|
||||
@@ -924,6 +936,7 @@ fn sqlite_row_to_keyset(row: Vec<Column>) -> Result<KeySetInfo, Error> {
|
||||
unit: column_as_string!(unit, CurrencyUnit::from_str),
|
||||
active: matches!(active, Column::Integer(1)),
|
||||
input_fee_ppk: column_as_nullable_number!(input_fee_ppk).unwrap_or_default(),
|
||||
final_expiry: column_as_nullable_number!(final_expiry),
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user