Browse Source

Move token_manager to the config

Cesar Rodas 9 months ago
parent
commit
929b456ea0
4 changed files with 19 additions and 6 deletions
  1. 3 1
      utxo/src/config.rs
  2. 3 5
      utxo/src/ledger.rs
  3. 4 0
      utxo/src/storage/mod.rs
  4. 9 0
      utxo/src/token.rs

+ 3 - 1
utxo/src/config.rs

@@ -1,4 +1,4 @@
-use crate::{status::StatusManager, storage::Storage};
+use crate::{status::StatusManager, storage::Storage, token::TokenManager};
 
 #[derive(Debug)]
 pub struct Config<S>
@@ -6,6 +6,7 @@ where
     S: Storage + Sync + Send,
 {
     pub storage: S,
+    pub token_manager: TokenManager,
     pub status: StatusManager,
 }
 
@@ -16,6 +17,7 @@ where
     fn from(storage: S) -> Self {
         Self {
             storage,
+            token_manager: TokenManager(b"test".to_vec()),
             status: StatusManager::default(),
         }
     }

+ 3 - 5
utxo/src/ledger.rs

@@ -4,7 +4,6 @@ use crate::{
     config::Config,
     status::{InternalStatus, StatusManager},
     storage::{AccountTransactionType, Batch, ReceivedPaymentStatus, Storage},
-    token::TokenManager,
     transaction::{Error as TxError, Type},
     worker::WorkerManager,
     AccountId, Amount, Error, Filter, PaymentFrom, PaymentId, RevId, Status, Tag, Transaction,
@@ -20,7 +19,6 @@ where
     S: Storage + Sync + Send,
 {
     config: Config<S>,
-    token_manager: TokenManager,
     broadcaster: WorkerManager<Broadcaster>,
 }
 
@@ -32,7 +30,6 @@ where
     pub fn new(config: Config<S>) -> Arc<Self> {
         Arc::new(Self {
             config,
-            token_manager: TokenManager(b"test".to_vec()),
             broadcaster: WorkerManager::new(Broadcaster::default()),
         })
     }
@@ -279,7 +276,8 @@ where
                 .revision
                 .locked
             {
-                self.token_manager
+                self.config
+                    .token_manager
                     .verify(lock_token, &transaction.revision.update_token)?
             }
         }
@@ -502,7 +500,7 @@ where
             .await?
             .pop()
             .ok_or(Error::TxNotFound)?
-            .lock_transaction(&self.token_manager)?;
+            .lock_transaction(&self.config.token_manager)?;
         self.store(new_revision).await?;
         Ok(secret)
     }

+ 4 - 0
utxo/src/storage/mod.rs

@@ -383,6 +383,7 @@ pub mod test {
     {
         let config = Config {
             storage,
+            token_manager: Default::default(),
             status: StatusManager::default(),
         };
 
@@ -633,6 +634,7 @@ pub mod test {
         let ledger = Ledger::new(Config {
             storage,
             status: Default::default(),
+            token_manager: Default::default(),
         });
 
         let (_, mut subscription) = ledger
@@ -739,6 +741,7 @@ pub mod test {
         let ledger = Ledger::new(Config {
             storage,
             status: Default::default(),
+            token_manager: Default::default(),
         });
 
         for i in 0..10 {
@@ -830,6 +833,7 @@ pub mod test {
         let ledger = Ledger::new(Config {
             storage,
             status: Default::default(),
+            token_manager: Default::default(),
         });
 
         for i in 0..10 {

+ 9 - 0
utxo/src/token.rs

@@ -29,6 +29,15 @@ pub enum Error {
 /// Lock Token Manager
 pub struct TokenManager(pub Vec<u8>);
 
+impl Default for TokenManager {
+    fn default() -> Self {
+        let mut rng = rand::thread_rng();
+        let mut payload = [0u8; 10];
+        rng.fill(&mut payload);
+        Self(payload.to_vec())
+    }
+}
+
 impl TokenManager {
     /// Checks if the given token is valid and still not expired
     pub fn verify(&self, token: Token, update_token: &Option<Vec<u8>>) -> Result<(), Error> {