1 Commits

Author SHA1 Message Date
b0b2532976 messy implementation used for lykin testing 2022-05-18 09:03:18 +02:00
8 changed files with 1154 additions and 61 deletions

1
.gitignore vendored
View File

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

1113
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
[package]
name = "golgi"
version = "0.2.2"
version = "0.1.1"
edition = "2021"
authors = ["Max Fowler <max@mfowler.info>", "Andrew Reid <glyph@mycelial.technology>"]
readme = "README.md"
@ -16,7 +16,6 @@ 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, SsbMessageKVT},
messages::{SsbMessageContentType, SsbMessageValue},
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. Messages are returned as KVTs (Key Value Timestamp).
// identity.
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<SsbMessageKVT, GolgiError>`.
// In this case, each element has type `Result<SsbMessageValue, GolgiError>`.
while let Some(res) = history_stream.next().await {
match res {
Ok(kvt) => {
// Print the `SsbMessageKVT` of this element to `stdout`.
println!("kvt: {:?}", kvt);
Ok(value) => {
// Print the `SsbMessageValue` of this element to `stdout`.
println!("value: {:?}", value);
}
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<SsbMessageKVT>` using
// Collect the stream elements into a `Vec<SsbMessageValue>` using
// `try_collect`. A `GolgiError` will be returned from the `run`
// function if any element contains an error.
let results: Vec<SsbMessageKVT> = history_stream.try_collect().await?;
let results: Vec<SsbMessageValue> = history_stream.try_collect().await?;
// Loop through the `SsbMessageKVT` elements, printing each one
// Loop through the `SsbMessageValue` elements, printing each one
// to `stdout`.
for x in results {
println!("x: {:?}", x);
@ -82,14 +82,13 @@ async fn run() -> Result<(), GolgiError> {
.await?;
// Iterate through the elements in the stream and use `map` to convert
// each `SsbMessageKVT` element into a tuple of
// each `SsbMessageValue` element into a tuple of
// `(String, SsbMessageContentType)`. This is an example of stream
// conversion.
let type_stream = history_stream.map(|msg| match msg {
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(val) => {
let message_type = val.get_message_type()?;
let tuple: (String, SsbMessageContentType) = (val.signature, message_type);
Ok(tuple)
}
Err(err) => Err(err),

View File

@ -7,11 +7,15 @@
use async_std::stream::Stream;
use kuska_ssb::api::dto::CreateHistoryStreamIn;
use crate::{error::GolgiError, messages::SsbMessageKVT, sbot::Sbot, utils};
use crate::{
error::GolgiError,
messages::{SsbMessageKVT, SsbMessageValue},
sbot::Sbot,
utils,
};
impl Sbot {
/// Call the `createHistoryStream` RPC method. Returns messages in the form
/// of KVTs (Key Value Timestamp).
/// Call the `createHistoryStream` RPC method.
///
/// # Example
///
@ -39,16 +43,22 @@ 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::kvt_res_parse)
.await;
let history_stream = utils::get_source_stream(
sbot_connection.rpc_reader,
req_id,
//utils::ssb_message_res_parse,
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,4 +76,3 @@ pub mod sbot;
pub mod utils;
pub use crate::{error::GolgiError, sbot::Sbot};
pub use kuska_ssb;

View File

@ -20,10 +20,6 @@ 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.
@ -53,52 +49,30 @@ 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<S, T>(
pub async fn init(
keystore: Keystore,
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() {
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 {
ip_port.unwrap().into()
ip_port.unwrap()
};
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().into()).unwrap()).unwrap()
auth::Key::from_slice(&hex::decode(net_id.unwrap()).unwrap()).unwrap()
};
let OwnedIdentity { pk, sk, id } = match keystore {
Keystore::Patchwork => keystore::from_patchwork_local()
.await
.expect("couldn't read local patchwork secret from default location"),
.expect("couldn't read local secret"),
Keystore::GoSbot => keystore::from_gosbot_local()
.await
.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)
})
}
.expect("couldn't read local secret"),
};
Ok(Self {

View File

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