1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
//
// Wildland Project
//
// Copyright © 2022 Golem Foundation
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 3 as published by
// the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

use std::fmt::{Debug, Display};

use super::{api::LocalSecureStorage, result::LssResult};
use crate::{
    storage::StorageTemplate, ForestRetrievalError, LssError, WildlandIdentity, DEFAULT_FOREST_KEY,
};
use serde::{de::DeserializeOwned, Serialize};
use uuid::Uuid;
use wildland_catlib::{Forest, IForest, Identity};
use wildland_crypto::identity::SigningKeypair;

const STORAGE_TEMPLATE_PREFIX: &str = "wildland.storage_template.";
#[derive(Clone)]
pub struct LssService {
    lss: &'static dyn LocalSecureStorage,
}

const THIS_DEVICE_KEYPAIR_KEY: &str = "wildland.device.keypair";
const THIS_DEVICE_NAME_KEY: &str = "wildland.device.name";

impl LssService {
    pub fn new(lss: &'static dyn LocalSecureStorage) -> Self {
        tracing::debug!("created new instance");
        Self { lss }
    }

    #[tracing::instrument(level = "debug", skip(self, wildland_identity))]
    pub fn save_identity(&self, wildland_identity: &WildlandIdentity) -> LssResult<bool> {
        let key = match wildland_identity {
            WildlandIdentity::Forest(_, _) => wildland_identity.to_string(),
            WildlandIdentity::Device(device_name, _) => {
                self.serialize_and_save(THIS_DEVICE_NAME_KEY, &device_name)?;

                THIS_DEVICE_KEYPAIR_KEY.to_owned()
            }
        };
        self.serialize_and_save(key, &wildland_identity.get_keypair())
    }

    #[tracing::instrument(level = "debug", skip(self))]
    pub fn get_default_forest_identity(
        &self,
    ) -> Result<Option<WildlandIdentity>, ForestRetrievalError> {
        tracing::trace!("Getting default forest identity.");

        let optional_default_forest_identity = self.get_default_forest_keypair()?;
        optional_default_forest_identity.map_or(Ok(None), |default_forest_value| {
            Ok(Some(WildlandIdentity::Forest(0, default_forest_value)))
        })
    }

    #[tracing::instrument(level = "debug", skip(self, forest))]
    pub fn save_forest_uuid(&self, forest: &Forest) -> LssResult<bool> {
        tracing::trace!("Saving forest uuid");
        self.serialize_and_save(forest.owner().encode(), &forest.uuid())
    }

    #[tracing::instrument(level = "debug", skip(self, forest_identity))]
    pub fn get_forest_uuid_by_identity(
        &self,
        forest_identity: &WildlandIdentity,
    ) -> LssResult<Option<Uuid>> {
        self.get_parsed(Identity::from(forest_identity.get_public_key()).encode())
    }

    #[tracing::instrument(level = "debug", skip(self))]
    pub fn get_this_device_identity(&self) -> LssResult<Option<WildlandIdentity>> {
        tracing::trace!("Getting this device identity.");

        let optional_this_device_identity = self.get_this_device_keypair()?;
        optional_this_device_identity.map_or(Ok(None), |this_device_identity| {
            let device_name = self
                .get_this_device_name()?
                .ok_or_else(|| LssError("Could not retrieve device name from LSS".to_owned()))?;
            Ok(Some(WildlandIdentity::Device(
                device_name,
                this_device_identity,
            )))
        })
    }

    #[tracing::instrument(level = "debug", skip(self, storage_template))]
    pub fn save_storage_template(&self, storage_template: &StorageTemplate) -> LssResult<bool> {
        tracing::trace!("Saving storage template");
        self.serialize_and_save(
            format!("{STORAGE_TEMPLATE_PREFIX}{}", storage_template.uuid()),
            storage_template,
        )
    }

    #[tracing::instrument(level = "debug", skip(self))]
    fn get_this_device_name(&self) -> LssResult<Option<String>> {
        self.get_parsed(THIS_DEVICE_NAME_KEY)
    }

    #[tracing::instrument(level = "debug", skip(self))]
    fn get_this_device_keypair(&self) -> LssResult<Option<SigningKeypair>> {
        self.get_parsed(THIS_DEVICE_KEYPAIR_KEY)
    }

    #[tracing::instrument(level = "debug", skip(self))]
    fn get_default_forest_keypair(&self) -> LssResult<Option<SigningKeypair>> {
        self.get_parsed(DEFAULT_FOREST_KEY)
    }

    /// serializes an `obj` and saves it in LSS
    /// this is the only method which should use `serde_json::to_vec` function so it could be easily replaced
    #[tracing::instrument(level = "debug", skip(self, obj))]
    fn serialize_and_save(
        &self,
        key: impl Display + Debug,
        obj: &impl Serialize,
    ) -> LssResult<bool> {
        self.lss
            .insert(
                key.to_string(),
                serde_json::to_vec(obj)
                    .map_err(|e| LssError(format!("Could not serialize object: {e}")))?,
            )
            .map(|bytes| bytes.is_some())
    }

    /// retrieves bytes from LSS, deserializes them as json and parses as a type specified with template parameter
    /// this is the only method which should use `serde_json::from_slice` function so it could be easily replaced
    #[tracing::instrument(level = "debug", skip(self))]
    fn get_parsed<'a, T: DeserializeOwned>(
        &self,
        key: impl Display + Debug,
    ) -> LssResult<Option<T>> {
        self.lss.get(key.to_string()).and_then(|optional_bytes| {
            optional_bytes.map_or(Ok(None), |bytes| {
                serde_json::from_slice(bytes.as_slice())
                    .map_err(|e| LssError(format!("Could not parse LSS entry: {e}")))
            })
        })
    }
}

#[cfg(test)]
mod tests {
    use std::{
        cell::RefCell,
        collections::{HashMap, HashSet},
        rc::Rc,
    };

    use uuid::Uuid;
    use wildland_catlib::{CatLib, IForest, Identity};
    use wildland_crypto::identity::SigningKeypair;

    use crate::{
        lss::service::{THIS_DEVICE_KEYPAIR_KEY, THIS_DEVICE_NAME_KEY},
        storage::{StorageTemplate, StorageTemplateTrait},
        LocalSecureStorage, LssResult, LssService, WildlandIdentity, DEFAULT_FOREST_KEY,
    };

    #[derive(Default)]
    struct LssStub {
        storage: RefCell<HashMap<String, Vec<u8>>>,
    }

    impl LocalSecureStorage for LssStub {
        fn insert(&self, key: String, value: Vec<u8>) -> LssResult<Option<Vec<u8>>> {
            Ok(self.storage.borrow_mut().insert(key, value))
        }

        fn get(&self, key: String) -> LssResult<Option<Vec<u8>>> {
            Ok(self.storage.try_borrow().unwrap().get(&key).cloned())
        }

        fn contains_key(&self, key: String) -> LssResult<bool> {
            Ok(self.storage.borrow().contains_key(&key))
        }

        fn keys(&self) -> LssResult<Vec<String>> {
            Ok(self.storage.borrow().keys().cloned().collect())
        }

        fn remove(&self, key: String) -> LssResult<Option<Vec<u8>>> {
            Ok(self.storage.borrow_mut().remove(&key))
        }

        fn len(&self) -> LssResult<usize> {
            Ok(self.storage.borrow().len())
        }

        fn is_empty(&self) -> LssResult<bool> {
            Ok(self.storage.borrow().is_empty())
        }
    }

    #[test]
    fn test_save_forest_identity() {
        let lss = LssStub::default(); // LSS must live through the whole test
        let lss_ref: &'static LssStub = unsafe { std::mem::transmute(&lss) };
        let service = LssService::new(lss_ref);

        let keypair = SigningKeypair::try_from_bytes_slices([1; 32], [2; 32]).unwrap();
        let forest_identity = WildlandIdentity::Forest(5, SigningKeypair::from(&keypair));
        service.save_identity(&forest_identity).unwrap();

        let expected_key = "wildland.forest.5".to_string();

        let deserialized_keypair: SigningKeypair =
            serde_json::from_slice(&lss.get(expected_key).unwrap().unwrap()).unwrap();
        assert_eq!(deserialized_keypair, keypair);
    }

    #[test]
    fn test_save_device_identity() {
        let lss = LssStub::default(); // LSS must live through the whole test
        let lss_ref: &'static LssStub = unsafe { std::mem::transmute(&lss) };
        let service = LssService::new(lss_ref);

        let device_name = "some device".to_owned();
        let keypair = SigningKeypair::try_from_bytes_slices([1; 32], [2; 32]).unwrap();
        let device_identity =
            WildlandIdentity::Device(device_name.clone(), SigningKeypair::from(&keypair));
        service.save_identity(&device_identity).unwrap();

        let deserialized_keypair: SigningKeypair = serde_json::from_slice(
            &lss.get(THIS_DEVICE_KEYPAIR_KEY.to_string())
                .unwrap()
                .unwrap(),
        )
        .unwrap();
        assert_eq!(deserialized_keypair, keypair);

        let deserialized_name: String =
            serde_json::from_slice(&lss.get(THIS_DEVICE_NAME_KEY.to_owned()).unwrap().unwrap())
                .unwrap();
        assert_eq!(deserialized_name, device_name);
    }

    #[test]
    fn get_default_forest_should_return_none() {
        let lss = LssStub::default(); // LSS must live through the whole test
        let lss_ref: &'static LssStub = unsafe { std::mem::transmute(&lss) };
        let service = LssService::new(lss_ref);

        let default_forest = service.get_default_forest_identity().unwrap();

        assert!(default_forest.is_none())
    }

    #[test]
    fn get_default_forest_should_return_identity() {
        let lss = LssStub::default(); // LSS must live through the whole test
        let lss_ref: &'static LssStub = unsafe { std::mem::transmute(&lss) };
        let service = LssService::new(lss_ref);

        let keypair = SigningKeypair::try_from_bytes_slices([1; 32], [2; 32]).unwrap();
        lss.insert(
            DEFAULT_FOREST_KEY.to_owned(),
            serde_json::to_vec(&keypair).unwrap(),
        )
        .unwrap();

        let default_forest = service.get_default_forest_identity().unwrap();

        let expecte_forest_identity = WildlandIdentity::Forest(0, SigningKeypair::from(&keypair));
        assert_eq!(default_forest.unwrap(), expecte_forest_identity)
    }

    #[test]
    fn test_save_forest_uuid() {
        let lss = LssStub::default(); // LSS must live through the whole test
        let lss_ref: &'static LssStub = unsafe { std::mem::transmute(&lss) };
        let service = LssService::new(lss_ref);

        let tmp = tempfile::tempdir().unwrap().path().into();
        let catlib = CatLib::new(tmp);

        let forest_identity = Identity([1; 32]);
        let forest = catlib
            .create_forest(forest_identity.clone(), HashSet::new(), vec![])
            .unwrap();

        service.save_forest_uuid(&forest).unwrap();

        let retrieved_uuid: Uuid =
            serde_json::from_slice(&lss.get(forest_identity.encode()).unwrap().unwrap()).unwrap();

        assert_eq!(retrieved_uuid, forest.uuid());
    }

    #[test]
    fn test_get_forest_uuid_by_identity() {
        let lss = LssStub::default(); // LSS must live through the whole test
        let lss_ref: &'static LssStub = unsafe { std::mem::transmute(&lss) };
        let service = LssService::new(lss_ref);

        let forest_uuid = Uuid::new_v4();
        let forest_pubkey = [1; 32];
        let forest_identity = Identity(forest_pubkey);
        lss.insert(
            forest_identity.encode(),
            serde_json::to_vec(&forest_uuid).unwrap(),
        )
        .unwrap();

        let retrieved_uuid = service
            .get_forest_uuid_by_identity(&WildlandIdentity::Forest(
                5,
                SigningKeypair::try_from_bytes_slices(forest_pubkey, [2; 32]).unwrap(),
            ))
            .unwrap()
            .unwrap();

        assert_eq!(retrieved_uuid, forest_uuid);
    }

    #[test]
    fn test_get_this_device_identity_should_return_none() {
        let lss = LssStub::default(); // LSS must live through the whole test
        let lss_ref: &'static LssStub = unsafe { std::mem::transmute(&lss) };
        let service = LssService::new(lss_ref);

        let device_identity = service.get_this_device_identity().unwrap();

        assert!(device_identity.is_none())
    }

    #[test]
    fn test_get_this_device_identity_should_return_identity() {
        let lss = LssStub::default(); // LSS must live through the whole test
        let lss_ref: &'static LssStub = unsafe { std::mem::transmute(&lss) };
        let service = LssService::new(lss_ref);

        let device_name = "some device".to_owned();
        let keypair = SigningKeypair::try_from_bytes_slices([1; 32], [2; 32]).unwrap();

        lss.insert(
            THIS_DEVICE_NAME_KEY.to_owned(),
            serde_json::to_vec(&device_name).unwrap(),
        )
        .unwrap();
        lss.insert(
            THIS_DEVICE_KEYPAIR_KEY.to_owned(),
            serde_json::to_vec(&keypair).unwrap(),
        )
        .unwrap();

        let device_identity = service.get_this_device_identity().unwrap().unwrap();
        let expected_device_identity =
            WildlandIdentity::Device(device_name, SigningKeypair::from(&keypair));
        assert_eq!(device_identity, expected_device_identity);
    }

    struct StorageTemplateTestImpl;
    impl StorageTemplateTrait for StorageTemplateTestImpl {
        fn uuid(&self) -> Uuid {
            Uuid::from_u128(2)
        }

        fn data(&self) -> Vec<u8> {
            vec![1, 2, 3]
        }
    }

    #[test]
    fn test_save_storage_template() {
        let lss = LssStub::default(); // LSS must live through the whole test
        let lss_ref: &'static LssStub = unsafe { std::mem::transmute(&lss) };
        let service = LssService::new(lss_ref);

        let storage_template = StorageTemplate::new(Rc::new(StorageTemplateTestImpl {}));

        service.save_storage_template(&storage_template).unwrap();

        let expected_uuid = Uuid::from_u128(2);
        let retrieved_storage_template_data = lss
            .get(format!("wildland.storage_template.{expected_uuid}"))
            .unwrap()
            .unwrap();

        let expected_data = serde_json::to_vec(&vec![1, 2, 3]).unwrap();
        assert_eq!(retrieved_storage_template_data, expected_data);
    }
}