mirror of
https://github.com/aljazceru/cdk.git
synced 2025-12-19 05:35:18 +01:00
Update the signatory.proto file to match NUT-XXX (#1032)
* Update the signatory.proto file to match NUT-XXX Source: https://github.com/cashubtc/nuts/pull/250/files * Add unit tests as requested in https://github.com/cashubtc/cdk/pull/1032#discussion_r2321436860 * Remove unused types from proto file
This commit is contained in:
@@ -284,6 +284,7 @@ where
|
||||
derivation_path,
|
||||
derivation_path_index,
|
||||
max_order,
|
||||
amounts,
|
||||
input_fee_ppk
|
||||
FROM
|
||||
keyset
|
||||
@@ -308,6 +309,7 @@ where
|
||||
derivation_path,
|
||||
derivation_path_index,
|
||||
max_order,
|
||||
amounts,
|
||||
input_fee_ppk
|
||||
FROM
|
||||
keyset
|
||||
|
||||
@@ -29,4 +29,6 @@ pub static MIGRATIONS: &[(&str, &str, &str)] = &[
|
||||
("sqlite", "20250819200000_remove_request_lookup_kind_constraints.sql", include_str!(r#"./migrations/sqlite/20250819200000_remove_request_lookup_kind_constraints.sql"#)),
|
||||
("sqlite", "20250901090000_add_kv_store.sql", include_str!(r#"./migrations/sqlite/20250901090000_add_kv_store.sql"#)),
|
||||
("postgres", "20250901090000_add_kv_store.sql", include_str!(r#"./migrations/postgres/20250901090000_add_kv_store.sql"#)),
|
||||
("sqlite", "20250903200000_add_signatory_amounts.sql", include_str!(r#"./migrations/sqlite/20250903200000_add_signatory_amounts.sql"#)),
|
||||
("postgres", "20250903200000_add_signatory_amounts.sql", include_str!(r#"./migrations/postgres/20250903200000_add_signatory_amounts.sql"#)),
|
||||
];
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
ALTER TABLE keyset ADD COLUMN amounts TEXT DEFAULT NULL;
|
||||
@@ -0,0 +1,33 @@
|
||||
CREATE TABLE keyset_new (
|
||||
id TEXT PRIMARY KEY,
|
||||
unit TEXT NOT NULL,
|
||||
active BOOL NOT NULL,
|
||||
valid_from INTEGER NOT NULL,
|
||||
valid_to INTEGER,
|
||||
max_order INTEGER NOT NULL,
|
||||
amounts TEXT DEFAULT NULL,
|
||||
input_fee_ppk INTEGER,
|
||||
derivation_path TEXT NOT NULL,
|
||||
derivation_path_index INTEGER
|
||||
);
|
||||
|
||||
|
||||
INSERT INTO keyset_new SELECT
|
||||
id,
|
||||
unit,
|
||||
active,
|
||||
valid_from,
|
||||
valid_to,
|
||||
max_order,
|
||||
NULL,
|
||||
input_fee_ppk,
|
||||
derivation_path,
|
||||
derivation_path_index
|
||||
FROM keyset;
|
||||
|
||||
DROP TABLE keyset;
|
||||
|
||||
ALTER TABLE keyset_new RENAME TO keyset;
|
||||
|
||||
CREATE INDEX unit_index ON keyset(unit);
|
||||
CREATE INDEX active_index ON keyset(active);
|
||||
@@ -383,11 +383,11 @@ where
|
||||
INSERT INTO
|
||||
keyset (
|
||||
id, unit, active, valid_from, valid_to, derivation_path,
|
||||
max_order, input_fee_ppk, derivation_path_index
|
||||
max_order, amounts, input_fee_ppk, derivation_path_index
|
||||
)
|
||||
VALUES (
|
||||
:id, :unit, :active, :valid_from, :valid_to, :derivation_path,
|
||||
:max_order, :input_fee_ppk, :derivation_path_index
|
||||
:max_order, :amounts, :input_fee_ppk, :derivation_path_index
|
||||
)
|
||||
ON CONFLICT(id) DO UPDATE SET
|
||||
unit = excluded.unit,
|
||||
@@ -396,6 +396,7 @@ where
|
||||
valid_to = excluded.valid_to,
|
||||
derivation_path = excluded.derivation_path,
|
||||
max_order = excluded.max_order,
|
||||
amounts = excluded.amounts,
|
||||
input_fee_ppk = excluded.input_fee_ppk,
|
||||
derivation_path_index = excluded.derivation_path_index
|
||||
"#,
|
||||
@@ -407,6 +408,7 @@ where
|
||||
.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("amounts", serde_json::to_string(&keyset.amounts).ok())
|
||||
.bind("input_fee_ppk", keyset.input_fee_ppk as i64)
|
||||
.bind("derivation_path_index", keyset.derivation_path_index)
|
||||
.execute(&self.inner)
|
||||
@@ -496,6 +498,7 @@ where
|
||||
derivation_path,
|
||||
derivation_path_index,
|
||||
max_order,
|
||||
amounts,
|
||||
input_fee_ppk
|
||||
FROM
|
||||
keyset
|
||||
@@ -520,6 +523,7 @@ where
|
||||
derivation_path,
|
||||
derivation_path_index,
|
||||
max_order,
|
||||
amounts,
|
||||
input_fee_ppk
|
||||
FROM
|
||||
keyset
|
||||
@@ -1837,10 +1841,16 @@ fn sql_row_to_keyset_info(row: Vec<Column>) -> Result<MintKeySetInfo, Error> {
|
||||
derivation_path,
|
||||
derivation_path_index,
|
||||
max_order,
|
||||
amounts,
|
||||
row_keyset_ppk
|
||||
) = row
|
||||
);
|
||||
|
||||
let max_order: u8 = column_as_number!(max_order);
|
||||
let amounts = column_as_nullable_string!(amounts)
|
||||
.and_then(|str| serde_json::from_str(&str).ok())
|
||||
.unwrap_or_else(|| (0..max_order).map(|m| 2u64.pow(m.into())).collect());
|
||||
|
||||
Ok(MintKeySetInfo {
|
||||
id: column_as_string!(id, Id::from_str, Id::from_bytes),
|
||||
unit: column_as_string!(unit, CurrencyUnit::from_str),
|
||||
@@ -1848,7 +1858,8 @@ fn sql_row_to_keyset_info(row: Vec<Column>) -> Result<MintKeySetInfo, Error> {
|
||||
valid_from: column_as_number!(valid_from),
|
||||
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),
|
||||
max_order,
|
||||
amounts,
|
||||
input_fee_ppk: column_as_number!(row_keyset_ppk),
|
||||
final_expiry: column_as_nullable_number!(valid_to),
|
||||
})
|
||||
@@ -2062,3 +2073,97 @@ fn sql_row_to_blind_signature(row: Vec<Column>) -> Result<BlindSignature, Error>
|
||||
dleq,
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
||||
mod max_order_to_amounts_migrations {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn legacy_payload() {
|
||||
let result = sql_row_to_keyset_info(vec![
|
||||
Column::Text("0083a60439303340".to_owned()),
|
||||
Column::Text("sat".to_owned()),
|
||||
Column::Integer(1),
|
||||
Column::Integer(1749844864),
|
||||
Column::Null,
|
||||
Column::Text("0'/0'/0'".to_owned()),
|
||||
Column::Integer(0),
|
||||
Column::Integer(32),
|
||||
Column::Null,
|
||||
Column::Integer(0),
|
||||
]);
|
||||
assert!(result.is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn migrated_payload() {
|
||||
let legacy = sql_row_to_keyset_info(vec![
|
||||
Column::Text("0083a60439303340".to_owned()),
|
||||
Column::Text("sat".to_owned()),
|
||||
Column::Integer(1),
|
||||
Column::Integer(1749844864),
|
||||
Column::Null,
|
||||
Column::Text("0'/0'/0'".to_owned()),
|
||||
Column::Integer(0),
|
||||
Column::Integer(32),
|
||||
Column::Null,
|
||||
Column::Integer(0),
|
||||
]);
|
||||
assert!(legacy.is_ok());
|
||||
|
||||
let amounts = (0..32).map(|x| 2u64.pow(x)).collect::<Vec<_>>();
|
||||
let migrated = sql_row_to_keyset_info(vec![
|
||||
Column::Text("0083a60439303340".to_owned()),
|
||||
Column::Text("sat".to_owned()),
|
||||
Column::Integer(1),
|
||||
Column::Integer(1749844864),
|
||||
Column::Null,
|
||||
Column::Text("0'/0'/0'".to_owned()),
|
||||
Column::Integer(0),
|
||||
Column::Integer(32),
|
||||
Column::Text(serde_json::to_string(&amounts).expect("valid json")),
|
||||
Column::Integer(0),
|
||||
]);
|
||||
assert!(migrated.is_ok());
|
||||
assert_eq!(legacy.unwrap(), migrated.unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn amounts_over_max_order() {
|
||||
let legacy = sql_row_to_keyset_info(vec![
|
||||
Column::Text("0083a60439303340".to_owned()),
|
||||
Column::Text("sat".to_owned()),
|
||||
Column::Integer(1),
|
||||
Column::Integer(1749844864),
|
||||
Column::Null,
|
||||
Column::Text("0'/0'/0'".to_owned()),
|
||||
Column::Integer(0),
|
||||
Column::Integer(32),
|
||||
Column::Null,
|
||||
Column::Integer(0),
|
||||
]);
|
||||
assert!(legacy.is_ok());
|
||||
|
||||
let amounts = (0..16).map(|x| 2u64.pow(x)).collect::<Vec<_>>();
|
||||
let migrated = sql_row_to_keyset_info(vec![
|
||||
Column::Text("0083a60439303340".to_owned()),
|
||||
Column::Text("sat".to_owned()),
|
||||
Column::Integer(1),
|
||||
Column::Integer(1749844864),
|
||||
Column::Null,
|
||||
Column::Text("0'/0'/0'".to_owned()),
|
||||
Column::Integer(0),
|
||||
Column::Integer(32),
|
||||
Column::Text(serde_json::to_string(&amounts).expect("valid json")),
|
||||
Column::Integer(0),
|
||||
]);
|
||||
assert!(migrated.is_ok());
|
||||
let migrated = migrated.unwrap();
|
||||
assert_ne!(legacy.unwrap(), migrated);
|
||||
assert_eq!(migrated.amounts.len(), 16);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user