forked from tauri-apps/plugins-workspace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstronghold.rs
71 lines (61 loc) · 1.9 KB
/
stronghold.rs
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
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
use std::{convert::TryFrom, ops::Deref, path::Path};
use iota_stronghold::{KeyProvider, SnapshotPath};
use serde::{Serialize, Serializer};
use zeroize::Zeroizing;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("stronghold not initialized")]
StrongholdNotInitialized,
#[error(transparent)]
Stronghold(#[from] iota_stronghold::ClientError),
#[error(transparent)]
Memory(#[from] iota_stronghold::MemoryError),
#[error(transparent)]
Procedure(#[from] iota_stronghold::procedures::ProcedureError),
}
impl Serialize for Error {
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(self.to_string().as_str())
}
}
pub struct Stronghold {
inner: iota_stronghold::Stronghold,
path: SnapshotPath,
keyprovider: KeyProvider,
}
impl Stronghold {
pub fn new<P: AsRef<Path>>(path: P, password: Vec<u8>) -> Result<Self> {
let path = SnapshotPath::from_path(path);
let stronghold = iota_stronghold::Stronghold::default();
let keyprovider = KeyProvider::try_from(Zeroizing::new(password))?;
if path.exists() {
stronghold.load_snapshot(&keyprovider, &path)?;
}
Ok(Self {
inner: stronghold,
path,
keyprovider,
})
}
pub fn save(&self) -> Result<()> {
self.inner
.commit_with_keyprovider(&self.path, &self.keyprovider)?;
Ok(())
}
pub fn inner(&self) -> &iota_stronghold::Stronghold {
&self.inner
}
}
impl Deref for Stronghold {
type Target = iota_stronghold::Stronghold;
fn deref(&self) -> &Self::Target {
&self.inner
}
}