21 Commits

Author SHA1 Message Date
2f347ee0d6 accept String or &str for ip_port and net_id 2022-07-13 11:08:45 +01:00
22d12f31ac Merge pull request 'Replace SsbMessageValue with SsbMessageKVT in stream examples' (#49) from fix_broken_stream_examples into main
Reviewed-on: #49
2022-07-07 08:16:32 +00:00
fbe7072995 Merge pull request 'Re-export kuska_ssb' (#50) from kuska_ssb into main
Reviewed-on: #50
2022-07-07 07:58:58 +00:00
f41f8b55d2 Reexport kuska_ssb 2022-07-06 12:45:53 +02:00
15acebbbfa replace SsbMessageValue with SsbMessageKVT in stream examples 2022-07-01 08:58:33 +01:00
1c44f0e56a Merge pull request 'Remove lockfile from repo' (#48) from remove_lockfile into main
Reviewed-on: #48
2022-07-01 07:41:28 +00:00
89a98dd6ab add lockfile to git ignore list 2022-06-29 16:01:42 +01:00
e03729907f bump patch version 2022-06-29 16:00:56 +01:00
175add2c72 remove lockfile 2022-06-29 15:58:39 +01:00
bf7d574064 Merge pull request 'Return KVT from create_history_stream()' (#47) from return_kvt_from_history_stream into main
Reviewed-on: #47
2022-06-29 13:38:55 +00:00
914ffb70cc fix formatting 2022-06-29 13:56:43 +01:00
2abe4b5e10 bump version to reflect api change 2022-06-29 13:49:38 +01:00
bb07839691 return kvt instead of value 2022-06-29 13:49:00 +01:00
ca4c1114dd Merge pull request 'Add support for custom keyfile paths' (#41) from keypaths into main
Reviewed-on: #41
2022-06-15 10:27:28 +00:00
1651b73426 Fix clippy warnings 2022-06-15 12:27:04 +02:00
1fc56ac8f1 Update kuska-ssb dependency 2022-06-15 12:12:18 +02:00
51dc82280a Remove debug statements 2022-05-26 23:11:18 +02:00
6daddeab9e Debug address 2022-05-26 23:03:43 +02:00
fb115b280f Prefix 127.0.0.1 to address if starts with : 2022-05-26 22:54:19 +02:00
b4987d514a Cargo.toml 2022-05-25 14:01:48 +02:00
3f3c18b8b7 Add support for custom keyfile paths 2022-05-25 13:49:27 +02:00
8 changed files with 61 additions and 1154 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
/target
Cargo.lock

1113
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
[package]
name = "golgi"
version = "0.1.1"
version = "0.2.2"
edition = "2021"
authors = ["Max Fowler <max@mfowler.info>", "Andrew Reid <glyph@mycelial.technology>"]
readme = "README.md"
@ -16,6 +16,7 @@ async-std = "1.10.0"
async-stream = "0.3.2"
base64 = "0.13.0"
futures = "0.3.21"
log = "0.4"
hex = "0.4.3"
kuska-handshake = { version = "0.2.0", features = ["async_std"] }
kuska-sodiumoxide = "0.2.5-0"

View File

@ -5,7 +5,7 @@ use futures::TryStreamExt;
use golgi::{
api::get_subset::{SubsetQuery, SubsetQueryOptions},
messages::{SsbMessageContentType, SsbMessageValue},
messages::{SsbMessageContentType, SsbMessageKVT},
sbot::Keystore,
GolgiError, Sbot,
};
@ -31,7 +31,7 @@ async fn run() -> Result<(), GolgiError> {
/* HISTORY STREAM EXAMPLE */
// Create an ordered stream of all messages authored by the `author`
// identity.
// identity. Messages are returned as KVTs (Key Value Timestamp).
let history_stream = sbot_client
.create_history_stream(author.to_string())
.await?;
@ -42,12 +42,12 @@ async fn run() -> Result<(), GolgiError> {
println!("looping through stream");
// Iterate through each element in the stream and match on the `Result`.
// In this case, each element has type `Result<SsbMessageValue, GolgiError>`.
// In this case, each element has type `Result<SsbMessageKVT, GolgiError>`.
while let Some(res) = history_stream.next().await {
match res {
Ok(value) => {
// Print the `SsbMessageValue` of this element to `stdout`.
println!("value: {:?}", value);
Ok(kvt) => {
// Print the `SsbMessageKVT` of this element to `stdout`.
println!("kvt: {:?}", kvt);
}
Err(err) => {
// Print the `GolgiError` of this element to `stderr`.
@ -64,12 +64,12 @@ async fn run() -> Result<(), GolgiError> {
.create_history_stream(author.to_string())
.await?;
// Collect the stream elements into a `Vec<SsbMessageValue>` using
// Collect the stream elements into a `Vec<SsbMessageKVT>` using
// `try_collect`. A `GolgiError` will be returned from the `run`
// function if any element contains an error.
let results: Vec<SsbMessageValue> = history_stream.try_collect().await?;
let results: Vec<SsbMessageKVT> = history_stream.try_collect().await?;
// Loop through the `SsbMessageValue` elements, printing each one
// Loop through the `SsbMessageKVT` elements, printing each one
// to `stdout`.
for x in results {
println!("x: {:?}", x);
@ -82,13 +82,14 @@ async fn run() -> Result<(), GolgiError> {
.await?;
// Iterate through the elements in the stream and use `map` to convert
// each `SsbMessageValue` element into a tuple of
// each `SsbMessageKVT` element into a tuple of
// `(String, SsbMessageContentType)`. This is an example of stream
// conversion.
let type_stream = history_stream.map(|msg| match msg {
Ok(val) => {
let message_type = val.get_message_type()?;
let tuple: (String, SsbMessageContentType) = (val.signature, message_type);
Ok(kvt) => {
let message_type = kvt.value.get_message_type()?;
// Return the message key and type.
let tuple: (String, SsbMessageContentType) = (kvt.key, message_type);
Ok(tuple)
}
Err(err) => Err(err),

View File

@ -7,15 +7,11 @@
use async_std::stream::Stream;
use kuska_ssb::api::dto::CreateHistoryStreamIn;
use crate::{
error::GolgiError,
messages::{SsbMessageKVT, SsbMessageValue},
sbot::Sbot,
utils,
};
use crate::{error::GolgiError, messages::SsbMessageKVT, sbot::Sbot, utils};
impl Sbot {
/// Call the `createHistoryStream` RPC method.
/// Call the `createHistoryStream` RPC method. Returns messages in the form
/// of KVTs (Key Value Timestamp).
///
/// # Example
///
@ -43,22 +39,16 @@ impl Sbot {
pub async fn create_history_stream(
&mut self,
id: String,
//) -> Result<impl Stream<Item = Result<SsbMessageValue, GolgiError>>, GolgiError> {
) -> Result<impl Stream<Item = Result<SsbMessageKVT, GolgiError>>, GolgiError> {
let mut sbot_connection = self.get_sbot_connection().await?;
let args = CreateHistoryStreamIn::new(id).keys_values(true, true);
//.limit(10);
let req_id = sbot_connection
.client
.create_history_stream_req_send(&args)
.await?;
let history_stream = utils::get_source_stream(
sbot_connection.rpc_reader,
req_id,
//utils::ssb_message_res_parse,
utils::kvt_res_parse,
)
.await;
let history_stream =
utils::get_source_stream(sbot_connection.rpc_reader, req_id, utils::kvt_res_parse)
.await;
Ok(history_stream)
}
}

View File

@ -48,7 +48,7 @@
//! // Call the `whoami` RPC method to retrieve the public key for the sbot
//! // identity.
//! let id = sbot_client.whoami().await?;
//!
//!
//! // Print the public key (identity) to `stdout`.
//! println!("{}", id);
//!
@ -76,3 +76,4 @@ pub mod sbot;
pub mod utils;
pub use crate::{error::GolgiError, sbot::Sbot};
pub use kuska_ssb;

View File

@ -20,6 +20,10 @@ pub enum Keystore {
Patchwork,
/// GoSbot default keystore path: `.ssb-go/secret` in the user's home directory.
GoSbot,
/// GoSbot keystore in a custom location
CustomGoSbot(String),
/// Patchwork keystore in a custom location
CustomPatchwork(String),
}
/// A struct representing a connection with a running sbot.
@ -49,30 +53,52 @@ 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(
pub async fn init<S, T>(
keystore: Keystore,
ip_port: Option<String>,
net_id: Option<String>,
) -> Result<Sbot, GolgiError> {
let address = if ip_port.is_none() {
ip_port: Option<S>,
net_id: Option<T>,
) -> Result<Sbot, GolgiError>
where
S: Into<String>,
T: Into<String>,
{
let mut address = if ip_port.is_none() {
"127.0.0.1:8008".to_string()
} else {
ip_port.unwrap()
ip_port.unwrap().into()
};
if address.starts_with(':') {
address = format!("127.0.0.1{}", address);
}
let network_id = if net_id.is_none() {
discovery::ssb_net_id()
} else {
auth::Key::from_slice(&hex::decode(net_id.unwrap()).unwrap()).unwrap()
auth::Key::from_slice(&hex::decode(net_id.unwrap().into()).unwrap()).unwrap()
};
let OwnedIdentity { pk, sk, id } = match keystore {
Keystore::Patchwork => keystore::from_patchwork_local()
.await
.expect("couldn't read local secret"),
.expect("couldn't read local patchwork secret from default location"),
Keystore::GoSbot => keystore::from_gosbot_local()
.await
.expect("couldn't read local secret"),
.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
.unwrap_or_else(|_| {
panic!("couldn't read local go-sbot secret from: {}", key_path)
})
}
Keystore::CustomPatchwork(key_path) => {
keystore::from_custom_patchwork_keypath(key_path.to_string())
.await
.unwrap_or_else(|_| {
panic!("couldn't read local patchwork secret from: {}", key_path)
})
}
};
Ok(Self {

View File

@ -76,7 +76,7 @@ where
if id == req_no {
match msg {
RecvMsg::RpcResponse(_type, body) => {
return f(&body).map_err(|err| err);
return f(&body);
}
RecvMsg::ErrorResponse(message) => {
return Err(GolgiError::Sbot(message));