|
| 1 | +// Copyright 2020 Nym Technologies SA |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +//use directory_client::presence::providers::MixProviderClient; |
| 16 | +//use log::*; |
| 17 | +//use nymsphinx::{DestinationAddressBytes, DESTINATION_ADDRESS_LENGTH}; |
| 18 | +//use sfw_provider_requests::auth_token::{AuthToken, AUTH_TOKEN_SIZE}; |
| 19 | +//use std::path::PathBuf; |
| 20 | + |
| 21 | +#[derive(Debug)] |
| 22 | +pub(crate) enum ClientLedgerError { |
| 23 | + DbReadError(sled::Error), |
| 24 | + DbWriteError(sled::Error), |
| 25 | + DbOpenError(sled::Error), |
| 26 | +} |
| 27 | + |
| 28 | +#[derive(Debug, Clone)] |
| 29 | +// Note: you should NEVER create more than a single instance of this using 'new()'. |
| 30 | +// You should always use .clone() to create additional instances |
| 31 | +pub(crate) struct ClientLedger { |
| 32 | + db: sled::Db, |
| 33 | +} |
| 34 | +// |
| 35 | +//impl ClientLedger { |
| 36 | +// pub(crate) fn load(file: PathBuf) -> Result<Self, ClientLedgerError> { |
| 37 | +// let db = match sled::open(file) { |
| 38 | +// Err(e) => return Err(ClientLedgerError::DbOpenError(e)), |
| 39 | +// Ok(db) => db, |
| 40 | +// }; |
| 41 | +// |
| 42 | +// let ledger = ClientLedger { db }; |
| 43 | +// |
| 44 | +// ledger.db.iter().keys().for_each(|key| { |
| 45 | +// println!( |
| 46 | +// "key: {:?}", |
| 47 | +// ledger |
| 48 | +// .read_destination_address_bytes(key.unwrap()) |
| 49 | +// .to_base58_string() |
| 50 | +// ); |
| 51 | +// }); |
| 52 | +// |
| 53 | +// Ok(ledger) |
| 54 | +// } |
| 55 | +// |
| 56 | +// fn read_auth_token(&self, raw_token: sled::IVec) -> AuthToken { |
| 57 | +// let token_bytes_ref = raw_token.as_ref(); |
| 58 | +// // if this fails it means we have some database corruption and we |
| 59 | +// // absolutely can't continue |
| 60 | +// if token_bytes_ref.len() != AUTH_TOKEN_SIZE { |
| 61 | +// error!("CLIENT LEDGER DATA CORRUPTION - TOKEN HAS INVALID LENGTH"); |
| 62 | +// panic!("CLIENT LEDGER DATA CORRUPTION - TOKEN HAS INVALID LENGTH"); |
| 63 | +// } |
| 64 | +// |
| 65 | +// let mut token_bytes = [0u8; AUTH_TOKEN_SIZE]; |
| 66 | +// token_bytes.copy_from_slice(token_bytes_ref); |
| 67 | +// AuthToken::from_bytes(token_bytes) |
| 68 | +// } |
| 69 | +// |
| 70 | +// fn read_destination_address_bytes( |
| 71 | +// &self, |
| 72 | +// raw_destination: sled::IVec, |
| 73 | +// ) -> DestinationAddressBytes { |
| 74 | +// let destination_ref = raw_destination.as_ref(); |
| 75 | +// // if this fails it means we have some database corruption and we |
| 76 | +// // absolutely can't continue |
| 77 | +// if destination_ref.len() != DESTINATION_ADDRESS_LENGTH { |
| 78 | +// error!("CLIENT LEDGER DATA CORRUPTION - CLIENT ADDRESS HAS INVALID LENGTH"); |
| 79 | +// panic!("CLIENT LEDGER DATA CORRUPTION - CLIENT ADDRESS HAS INVALID LENGTH"); |
| 80 | +// } |
| 81 | +// |
| 82 | +// let mut destination_bytes = [0u8; DESTINATION_ADDRESS_LENGTH]; |
| 83 | +// destination_bytes.copy_from_slice(destination_ref); |
| 84 | +// DestinationAddressBytes::from_bytes(destination_bytes) |
| 85 | +// } |
| 86 | +// |
| 87 | +// pub(crate) fn verify_token( |
| 88 | +// &self, |
| 89 | +// auth_token: &AuthToken, |
| 90 | +// client_address: &DestinationAddressBytes, |
| 91 | +// ) -> Result<bool, ClientLedgerError> { |
| 92 | +// match self.db.get(&client_address.to_bytes()) { |
| 93 | +// Err(e) => Err(ClientLedgerError::DbReadError(e)), |
| 94 | +// Ok(token) => match token { |
| 95 | +// Some(token_ivec) => Ok(&self.read_auth_token(token_ivec) == auth_token), |
| 96 | +// None => Ok(false), |
| 97 | +// }, |
| 98 | +// } |
| 99 | +// } |
| 100 | +// |
| 101 | +// pub(crate) fn insert_token( |
| 102 | +// &mut self, |
| 103 | +// auth_token: AuthToken, |
| 104 | +// client_address: DestinationAddressBytes, |
| 105 | +// ) -> Result<Option<AuthToken>, ClientLedgerError> { |
| 106 | +// let insertion_result = match self |
| 107 | +// .db |
| 108 | +// .insert(&client_address.to_bytes(), &auth_token.to_bytes()) |
| 109 | +// { |
| 110 | +// Err(e) => Err(ClientLedgerError::DbWriteError(e)), |
| 111 | +// Ok(existing_token) => { |
| 112 | +// Ok(existing_token.map(|existing_token| self.read_auth_token(existing_token))) |
| 113 | +// } |
| 114 | +// }; |
| 115 | +// |
| 116 | +// // registration doesn't happen that often so might as well flush it to the disk to be sure |
| 117 | +// self.db.flush().unwrap(); |
| 118 | +// insertion_result |
| 119 | +// } |
| 120 | +// |
| 121 | +// pub(crate) fn remove_token( |
| 122 | +// &mut self, |
| 123 | +// client_address: &DestinationAddressBytes, |
| 124 | +// ) -> Result<Option<AuthToken>, ClientLedgerError> { |
| 125 | +// let removal_result = match self.db.remove(&client_address.to_bytes()) { |
| 126 | +// Err(e) => Err(ClientLedgerError::DbWriteError(e)), |
| 127 | +// Ok(existing_token) => { |
| 128 | +// Ok(existing_token.map(|existing_token| self.read_auth_token(existing_token))) |
| 129 | +// } |
| 130 | +// }; |
| 131 | +// |
| 132 | +// // removing of tokens happens extremely rarely, so flush is also fine here |
| 133 | +// self.db.flush().unwrap(); |
| 134 | +// removal_result |
| 135 | +// } |
| 136 | +// |
| 137 | +// pub(crate) fn current_clients(&self) -> Result<Vec<MixProviderClient>, ClientLedgerError> { |
| 138 | +// let clients = self.db.iter().keys(); |
| 139 | +// |
| 140 | +// let mut client_vec = Vec::new(); |
| 141 | +// for client in clients { |
| 142 | +// match client { |
| 143 | +// Err(e) => return Err(ClientLedgerError::DbWriteError(e)), |
| 144 | +// Ok(client_entry) => client_vec.push(MixProviderClient { |
| 145 | +// pub_key: self |
| 146 | +// .read_destination_address_bytes(client_entry) |
| 147 | +// .to_base58_string(), |
| 148 | +// }), |
| 149 | +// } |
| 150 | +// } |
| 151 | +// |
| 152 | +// Ok(client_vec) |
| 153 | +// } |
| 154 | +// |
| 155 | +// #[cfg(test)] |
| 156 | +// pub(crate) fn create_temporary() -> Self { |
| 157 | +// let cfg = sled::Config::new().temporary(true); |
| 158 | +// ClientLedger { |
| 159 | +// db: cfg.open().unwrap(), |
| 160 | +// } |
| 161 | +// } |
| 162 | +//} |
0 commit comments