2022-02-08 08:02:42 +00:00
|
|
|
//! Sbot type and connection-related methods.
|
|
|
|
use async_std::net::TcpStream;
|
2022-05-26 21:03:43 +00:00
|
|
|
use log::debug;
|
2022-02-08 08:02:42 +00:00
|
|
|
|
|
|
|
use kuska_handshake::async_std::BoxStream;
|
|
|
|
use kuska_sodiumoxide::crypto::{auth, sign::ed25519};
|
|
|
|
use kuska_ssb::{
|
|
|
|
api::ApiCaller,
|
|
|
|
discovery, keystore,
|
|
|
|
keystore::OwnedIdentity,
|
|
|
|
rpc::{RpcReader, RpcWriter},
|
|
|
|
};
|
|
|
|
|
|
|
|
use crate::error::GolgiError;
|
|
|
|
|
2022-05-11 12:14:03 +00:00
|
|
|
/// 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,
|
2022-05-25 11:49:27 +00:00
|
|
|
/// GoSbot keystore in a custom location
|
|
|
|
CustomGoSbot(String),
|
|
|
|
/// Patchwork keystore in a custom location
|
|
|
|
CustomPatchwork(String),
|
2022-05-11 12:14:03 +00:00
|
|
|
}
|
|
|
|
|
2022-02-08 08:02:42 +00:00
|
|
|
/// 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.
|
|
|
|
/// Note there can be multiple SbotConnection at the same time.
|
|
|
|
pub struct SbotConnection {
|
2022-02-08 13:04:44 +00:00
|
|
|
/// Client for writing requests to go-bot
|
2022-02-08 08:02:42 +00:00
|
|
|
pub client: ApiCaller<TcpStream>,
|
|
|
|
/// RpcReader object for reading responses from go-sbot
|
|
|
|
pub rpc_reader: RpcReader<TcpStream>,
|
|
|
|
}
|
|
|
|
|
2022-02-08 09:54:02 +00:00
|
|
|
/// Holds the Scuttlebutt identity, keys and configuration parameters for
|
|
|
|
/// connecting to a local sbot and implements all Golgi API methods.
|
2022-02-08 08:02:42 +00:00
|
|
|
pub struct Sbot {
|
|
|
|
/// The ID (public key value) of the account associated with the local sbot instance.
|
|
|
|
pub id: String,
|
|
|
|
public_key: ed25519::PublicKey,
|
|
|
|
private_key: ed25519::SecretKey,
|
|
|
|
address: String,
|
|
|
|
// aka caps key (scuttleverse identifier)
|
|
|
|
network_id: auth::Key,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Sbot {
|
2022-02-08 09:54:02 +00:00
|
|
|
/// 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.
|
2022-05-11 12:14:03 +00:00
|
|
|
pub async fn init(
|
|
|
|
keystore: Keystore,
|
|
|
|
ip_port: Option<String>,
|
|
|
|
net_id: Option<String>,
|
|
|
|
) -> Result<Sbot, GolgiError> {
|
2022-05-26 20:54:19 +00:00
|
|
|
let mut address = if ip_port.is_none() {
|
2022-02-08 08:02:42 +00:00
|
|
|
"127.0.0.1:8008".to_string()
|
|
|
|
} else {
|
|
|
|
ip_port.unwrap()
|
|
|
|
};
|
|
|
|
|
2022-05-26 20:54:19 +00:00
|
|
|
if address.starts_with(":") {
|
|
|
|
address = format!("127.0.0.1{}", address);
|
2022-05-26 21:03:43 +00:00
|
|
|
debug!("prefixing address: {}", address);
|
2022-05-26 20:54:19 +00:00
|
|
|
}
|
|
|
|
|
2022-02-08 08:02:42 +00:00
|
|
|
let network_id = if net_id.is_none() {
|
|
|
|
discovery::ssb_net_id()
|
|
|
|
} else {
|
|
|
|
auth::Key::from_slice(&hex::decode(net_id.unwrap()).unwrap()).unwrap()
|
|
|
|
};
|
|
|
|
|
2022-05-11 12:14:03 +00:00
|
|
|
let OwnedIdentity { pk, sk, id } = match keystore {
|
|
|
|
Keystore::Patchwork => keystore::from_patchwork_local()
|
|
|
|
.await
|
2022-05-25 11:49:27 +00:00
|
|
|
.expect("couldn't read local patchwork secret from default location"),
|
2022-05-11 12:14:03 +00:00
|
|
|
Keystore::GoSbot => keystore::from_gosbot_local()
|
|
|
|
.await
|
2022-05-25 11:49:27 +00:00
|
|
|
.expect("couldn't read local go-sbot secret from default location"),
|
|
|
|
Keystore::CustomGoSbot(key_path) => {
|
|
|
|
keystore::from_custom_gosbot_keypath(key_path.to_string())
|
|
|
|
.await
|
|
|
|
.expect(&format!(
|
|
|
|
"couldn't read local go-sbot secret from: {}",
|
|
|
|
key_path
|
|
|
|
))
|
|
|
|
}
|
|
|
|
Keystore::CustomPatchwork(key_path) => {
|
|
|
|
keystore::from_custom_patchwork_keypath(key_path.to_string())
|
|
|
|
.await
|
|
|
|
.expect(&format!(
|
|
|
|
"couldn't read local patchwork secret from: {}",
|
|
|
|
key_path
|
|
|
|
))
|
|
|
|
}
|
2022-05-11 12:14:03 +00:00
|
|
|
};
|
2022-02-08 08:02:42 +00:00
|
|
|
|
|
|
|
Ok(Self {
|
|
|
|
id,
|
|
|
|
public_key: pk,
|
|
|
|
private_key: sk,
|
|
|
|
address,
|
|
|
|
network_id,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-02-08 09:54:02 +00:00
|
|
|
/// Creates a new connection with the sbot, using the address, network_id,
|
|
|
|
/// public_key and private_key supplied when Sbot was initialized.
|
2022-02-08 08:02:42 +00:00
|
|
|
///
|
|
|
|
/// Note that a single Sbot can have multiple SbotConnection at the same time.
|
|
|
|
pub async fn get_sbot_connection(&self) -> Result<SbotConnection, GolgiError> {
|
|
|
|
let address = self.address.clone();
|
|
|
|
let network_id = self.network_id.clone();
|
|
|
|
let public_key = self.public_key;
|
|
|
|
let private_key = self.private_key.clone();
|
|
|
|
Sbot::_get_sbot_connection_helper(address, network_id, public_key, private_key).await
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Private helper function which creates a new connection with sbot,
|
|
|
|
/// but with all variables passed as arguments.
|
2022-02-08 12:17:46 +00:00
|
|
|
///
|
|
|
|
/// Open a TCP stream to the sbot and perform the secret handshake. If
|
|
|
|
/// successful, create a box stream and split it into a writer and reader.
|
|
|
|
/// Return RPC handles to the sbot as part of the `struct` output.
|
2022-02-08 08:02:42 +00:00
|
|
|
async fn _get_sbot_connection_helper(
|
|
|
|
address: String,
|
|
|
|
network_id: auth::Key,
|
|
|
|
public_key: ed25519::PublicKey,
|
|
|
|
private_key: ed25519::SecretKey,
|
|
|
|
) -> Result<SbotConnection, GolgiError> {
|
|
|
|
let socket = TcpStream::connect(&address)
|
|
|
|
.await
|
|
|
|
.map_err(|source| GolgiError::Io {
|
|
|
|
source,
|
2022-02-08 13:04:44 +00:00
|
|
|
context: "failed to initiate tcp stream connection".to_string(),
|
2022-02-08 08:02:42 +00:00
|
|
|
})?;
|
|
|
|
|
|
|
|
let handshake = kuska_handshake::async_std::handshake_client(
|
|
|
|
&mut &socket,
|
|
|
|
network_id.clone(),
|
|
|
|
public_key,
|
|
|
|
private_key.clone(),
|
|
|
|
public_key,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.map_err(GolgiError::Handshake)?;
|
|
|
|
|
|
|
|
let (box_stream_read, box_stream_write) =
|
|
|
|
BoxStream::from_handshake(socket.clone(), socket, handshake, 0x8000).split_read_write();
|
|
|
|
|
|
|
|
let rpc_reader = RpcReader::new(box_stream_read);
|
|
|
|
let client = ApiCaller::new(RpcWriter::new(box_stream_write));
|
|
|
|
let sbot_connection = SbotConnection { rpc_reader, client };
|
|
|
|
Ok(sbot_connection)
|
|
|
|
}
|
|
|
|
}
|