Skip to content

Commit

Permalink
rhio-client can announce blob hashes now as well
Browse files Browse the repository at this point in the history
  • Loading branch information
adzialocha committed Aug 29, 2024
1 parent f12ef7d commit 75964a2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
14 changes: 12 additions & 2 deletions rhio-client/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use anyhow::Result;
use async_nats::jetstream::Context;
use p2panda_core::PrivateKey;
use p2panda_core::{Hash, PrivateKey};
use p2panda_store::MemoryStore;
use rhio_core::{create_message, encode_operation, LogId, RhioExtensions};
use rhio_core::{
create_blob_announcement, create_message, encode_operation, LogId, RhioExtensions,
};

pub struct Client {
jetstream: JetStream,
Expand Down Expand Up @@ -33,6 +35,14 @@ impl Client {
self.jetstream.publish(subject, encoded_operation).await?;
Ok(())
}

pub async fn announce_blob(&mut self, subject: String, hash: Hash) -> Result<()> {
let operation =
create_blob_announcement(&mut self.store, &self.private_key, &subject, hash)?;
let encoded_operation = encode_operation(operation.header, operation.body)?;
self.jetstream.publish(subject, encoded_operation).await?;
Ok(())
}
}

pub struct JetStream {
Expand Down
19 changes: 16 additions & 3 deletions rhio-client/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use std::str::FromStr;

use anyhow::{Context, Result};
use clap::Parser;
use p2panda_core::Hash;
use rhio_client::Client;
use tokio::sync::mpsc;

Expand Down Expand Up @@ -35,9 +38,19 @@ async fn main() -> Result<()> {
loop {
tokio::select! {
Some(payload) = line_rx.recv() => {
client
.publish(args.subject.clone(), payload.as_bytes())
.await?;
// If user writes a string, starting with "blob" (4 characters), followed by a
// space (1 character) and then ending with an hex-encoded BLAKE3 hash (64
// characters), then we interpret this as a blob announcement!
if payload.len() == 4 + 1 + 64 && payload.to_lowercase().starts_with("blob") {
let file_name = &payload[5..];
client
.announce_blob(args.subject.clone(), file_name)
.await?;
} else {
client
.publish(args.subject.clone(), payload.as_bytes())
.await?;
}
}
_ = tokio::signal::ctrl_c() => {
break;
Expand Down
4 changes: 2 additions & 2 deletions rhio-core/src/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ pub fn create_blob_announcement<S>(
store: &mut S,
private_key: &PrivateKey,
subject: &str,
blob_hash: Option<Hash>,
blob_hash: Hash,
) -> Result<Operation<RhioExtensions>>
where
S: OperationStore<LogId, RhioExtensions> + LogStore<LogId, RhioExtensions>,
{
create_operation(store, private_key, subject, blob_hash, None)
create_operation(store, private_key, subject, Some(blob_hash), None)
}

pub fn create_message<S>(
Expand Down

0 comments on commit 75964a2

Please sign in to comment.