add keystore selector for initialising sbot

This commit is contained in:
glyph 2022-05-11 14:14:03 +02:00
parent 77dd75bcd4
commit d6546733aa
1 changed files with 23 additions and 4 deletions

View File

@ -12,6 +12,16 @@ use kuska_ssb::{
use crate::error::GolgiError;
/// Keystore selector to specify the location of the secret file.
///
/// This enum is used when initiating a connection with an sbot instance.
pub enum Keystore {
/// Patchwork default keystore path: `.ssb/secret` in the user's home directory.
Patchwork,
/// GoSbot default keystore path: `.ssb-go/secret` in the user's home directory.
GoSbot,
}
/// A struct representing a connection with a running sbot.
/// A client and an rpc_reader can together be used to make requests to the sbot
/// and read the responses.
@ -39,7 +49,11 @@ impl Sbot {
/// Initiate a connection with an sbot instance. Define the IP address,
/// port and network key for the sbot, then retrieve the public key,
/// private key (secret) and identity from the `.ssb-go/secret` file.
pub async fn init(ip_port: Option<String>, net_id: Option<String>) -> Result<Sbot, GolgiError> {
pub async fn init(
keystore: Keystore,
ip_port: Option<String>,
net_id: Option<String>,
) -> Result<Sbot, GolgiError> {
let address = if ip_port.is_none() {
"127.0.0.1:8008".to_string()
} else {
@ -52,9 +66,14 @@ impl Sbot {
auth::Key::from_slice(&hex::decode(net_id.unwrap()).unwrap()).unwrap()
};
let OwnedIdentity { pk, sk, id } = keystore::from_gosbot_local()
.await
.expect("couldn't read local secret");
let OwnedIdentity { pk, sk, id } = match keystore {
Keystore::Patchwork => keystore::from_patchwork_local()
.await
.expect("couldn't read local secret"),
Keystore::GoSbot => keystore::from_gosbot_local()
.await
.expect("couldn't read local secret"),
};
Ok(Self {
id,