linera_wallet_json/
keystore.rs1use std::path::Path;
7
8use linera_base::{
9 crypto::{AccountPublicKey, AccountSignature, CryptoHash, InMemorySigner, Signer},
10 identifiers::AccountOwner,
11};
12use linera_persistent::{self as persistent, Persist as _};
13
14pub struct Keystore(persistent::File<InMemorySigner>);
16
17impl Signer for Keystore {
18 type Error = <InMemorySigner as Signer>::Error;
19
20 async fn sign(
21 &self,
22 owner: &AccountOwner,
23 value: &CryptoHash,
24 ) -> Result<AccountSignature, Self::Error> {
25 (*self.0).sign(owner, value).await
26 }
27
28 async fn contains_key(&self, owner: &AccountOwner) -> Result<bool, Self::Error> {
29 (*self.0).contains_key(owner).await
30 }
31}
32
33impl Keystore {
34 pub fn read(path: &Path) -> Result<Self, persistent::file::Error> {
36 Ok(Self(persistent::File::read(path)?))
37 }
38
39 pub fn create(
42 path: &Path,
43 testing_prng_seed: Option<u64>,
44 ) -> Result<Self, persistent::file::Error> {
45 Ok(Self(persistent::File::read_or_create(path, || {
46 Ok(InMemorySigner::new(testing_prng_seed))
47 })?))
48 }
49
50 pub async fn generate_key(&mut self) -> Result<AccountPublicKey, persistent::file::Error> {
52 let key = self.0.generate_new();
53 self.0.persist().await?;
54 Ok(key)
55 }
56
57 pub async fn generate_keys(
59 &mut self,
60 count: usize,
61 ) -> Result<Vec<AccountPublicKey>, persistent::file::Error> {
62 let keys: Vec<_> = std::iter::repeat_with(|| self.0.generate_new())
63 .take(count)
64 .collect();
65 self.0.persist().await?;
66 Ok(keys)
67 }
68
69 pub async fn save(&mut self) -> Result<(), persistent::file::Error> {
71 self.0.persist().await
72 }
73
74 pub fn into_signer(self) -> InMemorySigner {
76 self.0.into_value()
77 }
78}