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