From bc9d4b4682aa62d159e32c52e462d60d40045308 Mon Sep 17 00:00:00 2001 From: Simon Wicky Date: Mon, 23 Oct 2023 14:03:44 +0200 Subject: [PATCH] add secret-key for ecash credential storage --- common/bandwidth-controller/src/acquire/mod.rs | 2 +- common/bandwidth-controller/src/lib.rs | 2 -- .../migrations/20231020120000_add_ecash_table.sql | 2 +- common/credential-storage/src/backends/memory.rs | 13 ++++++------- common/credential-storage/src/backends/sqlite.rs | 14 ++++++++------ common/credential-storage/src/ephemeral_storage.rs | 4 ++-- common/credential-storage/src/models.rs | 2 +- .../credential-storage/src/persistent_storage.rs | 4 ++-- common/credential-storage/src/storage.rs | 4 ++-- .../nym_offline_compact_ecash/src/scheme/keygen.rs | 12 ++++++++++++ 10 files changed, 35 insertions(+), 24 deletions(-) diff --git a/common/bandwidth-controller/src/acquire/mod.rs b/common/bandwidth-controller/src/acquire/mod.rs index 6ea775606a..64abed0711 100644 --- a/common/bandwidth-controller/src/acquire/mod.rs +++ b/common/bandwidth-controller/src/acquire/mod.rs @@ -85,9 +85,9 @@ where .await?; storage .insert_ecash_credential( - state.voucher.get_voucher_value(), ECASH_INFO.to_string(), wallet.to_bs58(), + state.voucher.ecash_keypair().secret_key().to_bs58(), epoch_id.to_string(), ) .await diff --git a/common/bandwidth-controller/src/lib.rs b/common/bandwidth-controller/src/lib.rs index fb13bbe84b..a33ebef055 100644 --- a/common/bandwidth-controller/src/lib.rs +++ b/common/bandwidth-controller/src/lib.rs @@ -41,8 +41,6 @@ impl BandwidthController { .get_next_ecash_credential() .await .map_err(|err| BandwidthControllerError::CredentialStorageError(Box::new(err)))?; - let voucher_value = u64::from_str(&ecash_credential.voucher_value) - .map_err(|_| StorageError::InconsistentData)?; let voucher_info = ecash_credential.voucher_info.clone(); let wallet = Wallet::try_from_bs58(ecash_credential.wallet)?; let epoch_id = u64::from_str(&ecash_credential.epoch_id) diff --git a/common/credential-storage/migrations/20231020120000_add_ecash_table.sql b/common/credential-storage/migrations/20231020120000_add_ecash_table.sql index 1b94abec73..c96b8a1bf6 100644 --- a/common/credential-storage/migrations/20231020120000_add_ecash_table.sql +++ b/common/credential-storage/migrations/20231020120000_add_ecash_table.sql @@ -6,9 +6,9 @@ CREATE TABLE ecash_credentials ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, - voucher_value TEXT NOT NULL, voucher_info TEXT NOT NULL, wallet TEXT NOT NULL UNIQUE, + secret_key TEXT NOT NULL, epoch_id TEXT NOT NULL, consumed BOOLEAN NOT NULL ); \ No newline at end of file diff --git a/common/credential-storage/src/backends/memory.rs b/common/credential-storage/src/backends/memory.rs index 5faaddeee8..31edb545c3 100644 --- a/common/credential-storage/src/backends/memory.rs +++ b/common/credential-storage/src/backends/memory.rs @@ -56,25 +56,24 @@ impl CoconutCredentialManager { /// /// # Arguments /// - /// * `voucher_value`: Plaintext bandwidth value of the credential. - /// * `voucher_info`: Plaintext information of the credential. - /// * `serial_number`: Base58 representation of the serial number attribute. - /// * `binding_number`: Base58 representation of the binding number attribute. - /// * `signature`: Coconut credential in the form of a signature. + /// * `voucher_info`: What type of credential it is. + /// * `signature`: Ecash wallet credential in the form of a wallet. + /// * `secret_key`: The secret key with which this credential has been created. + /// * `epoch_id`: The epoch when it was signed. pub async fn insert_ecash_credential( &self, - voucher_value: String, voucher_info: String, wallet: String, + secret_key: String, epoch_id: String, ) { let mut creds = self.ecash.write().await; let id = creds.len() as i64; creds.push(EcashCredential { id, - voucher_value, voucher_info, wallet, + secret_key, epoch_id, consumed: false, }); diff --git a/common/credential-storage/src/backends/sqlite.rs b/common/credential-storage/src/backends/sqlite.rs index 7c045f84ea..29cbfcb9d7 100644 --- a/common/credential-storage/src/backends/sqlite.rs +++ b/common/credential-storage/src/backends/sqlite.rs @@ -49,19 +49,21 @@ impl CoconutCredentialManager { /// /// # Arguments /// - /// * `voucher_value`: Plaintext bandwidth value of the credential. - /// * `voucher_info`: Plaintext information of the credential. - /// * `signature`: Coconut credential in the form of a signature. + /// * `voucher_info`: What type of credential it is. + /// * `signature`: Ecash wallet credential in the form of a wallet. + /// * `secret_key`: The secret key with which this credential has been created. + /// * `epoch_id`: The epoch when it was signed. + pub async fn insert_ecash_credential( &self, - voucher_value: String, voucher_info: String, wallet: String, + secret_key: String, epoch_id: String, ) -> Result<(), sqlx::Error> { sqlx::query!( - "INSERT INTO ecash_credentials(voucher_value, voucher_info, wallet, epoch_id, consumed) VALUES (?, ?, ?, ?, ?)", - voucher_value, voucher_info, wallet, epoch_id, false + "INSERT INTO ecash_credentials(voucher_info, wallet, secret_key, epoch_id, consumed) VALUES (?, ?, ?, ?, ?)", + voucher_info, wallet, secret_key, epoch_id, false ) .execute(&self.connection_pool) .await?; diff --git a/common/credential-storage/src/ephemeral_storage.rs b/common/credential-storage/src/ephemeral_storage.rs index 89f8c87c6c..8d2d221939 100644 --- a/common/credential-storage/src/ephemeral_storage.rs +++ b/common/credential-storage/src/ephemeral_storage.rs @@ -52,13 +52,13 @@ impl Storage for EphemeralStorage { async fn insert_ecash_credential( &self, - voucher_value: String, voucher_info: String, wallet: String, + secret_key: String, epoch_id: String, ) -> Result<(), StorageError> { self.coconut_credential_manager - .insert_ecash_credential(voucher_value, voucher_info, wallet, epoch_id) + .insert_ecash_credential(voucher_info, wallet, secret_key, epoch_id) .await; Ok(()) diff --git a/common/credential-storage/src/models.rs b/common/credential-storage/src/models.rs index 741f7242fd..6fa068ca02 100644 --- a/common/credential-storage/src/models.rs +++ b/common/credential-storage/src/models.rs @@ -18,9 +18,9 @@ pub struct CoconutCredential { pub struct EcashCredential { #[allow(dead_code)] pub id: i64, - pub voucher_value: String, pub voucher_info: String, pub wallet: String, + pub secret_key: String, pub epoch_id: String, pub consumed: bool, } diff --git a/common/credential-storage/src/persistent_storage.rs b/common/credential-storage/src/persistent_storage.rs index 9e8e25a896..ab2068893b 100644 --- a/common/credential-storage/src/persistent_storage.rs +++ b/common/credential-storage/src/persistent_storage.rs @@ -83,13 +83,13 @@ impl Storage for PersistentStorage { async fn insert_ecash_credential( &self, - voucher_value: String, voucher_info: String, wallet: String, + secret_key: String, epoch_id: String, ) -> Result<(), StorageError> { self.coconut_credential_manager - .insert_ecash_credential(voucher_value, voucher_info, wallet, epoch_id) + .insert_ecash_credential(voucher_info, wallet, secret_key, epoch_id) .await?; Ok(()) diff --git a/common/credential-storage/src/storage.rs b/common/credential-storage/src/storage.rs index 66b806728a..1ebbd8f342 100644 --- a/common/credential-storage/src/storage.rs +++ b/common/credential-storage/src/storage.rs @@ -33,15 +33,15 @@ pub trait Storage: Send + Sync { /// /// # Arguments /// - /// * `voucher_value`: How much bandwidth is in the credential. /// * `voucher_info`: What type of credential it is. /// * `signature`: Ecash wallet credential in the form of a wallet. + /// * `secret_key`: The secret key with which this credential has been created. /// * `epoch_id`: The epoch when it was signed. async fn insert_ecash_credential( &self, - voucher_value: String, voucher_info: String, signature: String, + secret_key: String, epoch_id: String, ) -> Result<(), Self::StorageError>; diff --git a/common/nym_offline_compact_ecash/src/scheme/keygen.rs b/common/nym_offline_compact_ecash/src/scheme/keygen.rs index 284228058b..fa1f8aec6b 100644 --- a/common/nym_offline_compact_ecash/src/scheme/keygen.rs +++ b/common/nym_offline_compact_ecash/src/scheme/keygen.rs @@ -355,6 +355,18 @@ impl SecretKeyUser { } } +impl Bytable for SecretKeyUser { + fn to_byte_vec(&self) -> Vec { + self.to_bytes().to_vec() + } + + fn try_from_byte_slice(slice: &[u8]) -> std::result::Result { + Self::from_bytes(slice) + } +} + +impl Base58 for SecretKeyUser {} + #[derive(Debug, Eq, PartialEq, Clone, Hash)] pub struct PublicKeyUser { pub(crate) pk: G1Projective,