Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions .config/dictionaries/project.dic
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ adminer
admon
anypolicy
apskhem
asat
Arissara
asat
asyncio
Attributes
auditability
Expand All @@ -27,10 +27,10 @@ blockdiag
blockfetch
blosc
bmac
bytemuck
Bogale
bootstrapper
BROTLI
bytemuck
Cabe
cantopen
cardano
Expand All @@ -55,6 +55,7 @@ ciphertexts
Coap
codegen
codepoints
codetable
Collab
connexa
coti
Expand Down Expand Up @@ -255,8 +256,8 @@ pubk
pubkey
publickey
pubspec
pycardano
pwrite
pycardano
pycodestyle
pydantic
pydot
Expand Down
7 changes: 4 additions & 3 deletions rust/hermes-ipfs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "hermes-ipfs"
version = "0.0.10"
version = "0.0.11"
edition.workspace = true
license.workspace = true
authors.workspace = true
Expand All @@ -11,7 +11,7 @@ repository.workspace = true
workspace = true

[features]
doc-sync = ["minicbor", "ed25519-dalek", "catalyst-types", "blake3"]
doc-sync = ["ed25519-dalek", "catalyst-types", "blake3"]

[dependencies]
anyhow = "1.0.100"
Expand All @@ -25,12 +25,13 @@ tokio = "1.46.0"
futures = "0.3.31"
libp2p = "0.56.0"
connexa = { version = "0.4.1", features = ["full"] }
minicbor = { version = "0.25.1", features = ["alloc", "derive", "half"], optional = true }
minicbor = { version = "0.25.1", features = ["alloc", "derive", "half"] }
ed25519-dalek = { version = "2.1.1", optional = true }
catalyst-types = { version = "0.0.11", git = "https://github.com/input-output-hk/catalyst-libs.git", tag = "catalyst-types/v0.0.11", optional = true }
tracing = "0.1.43"
rand = "0.9.2"
blake3 = { version = "1.8.2", optional = true }
multihash-codetable = "0.1.4"

[dev-dependencies]
# Dependencies used by examples
Expand Down
11 changes: 9 additions & 2 deletions rust/hermes-ipfs/examples/add-file-with-pinning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ async fn main() -> anyhow::Result<()> {
println!("* Adding file to IPFS:");
println!("");
let ipfs_file = b"This is a demo file that is stored in IPFS.".to_vec();
let ipfs_path = hermes_ipfs.add_ipfs_file(ipfs_file.into()).await?;
let ipfs_path = hermes_ipfs.add_ipfs_file(ipfs_file).await?;
println!("* IPFS file published at {ipfs_path}");
let cid = ipfs_path.root().cid().ok_or(anyhow::anyhow!(
"ERROR! Could not extract CID from IPFS path."
Expand Down Expand Up @@ -64,7 +64,14 @@ async fn main() -> anyhow::Result<()> {
println!("* Get file from IPFS:");
println!("");
println!("* Retrieving from {ipfs_path}");
let get_file_bytes = hermes_ipfs.get_ipfs_file(ipfs_path.into()).await?;
let get_file_bytes = hermes_ipfs
.get_ipfs_file_cbor(
ipfs_path
.root()
.cid()
.ok_or(anyhow::anyhow!("Could not get CID"))?,
)
.await?;
println!("* Got file, {} bytes:", get_file_bytes.len());
let get_file = String::from_utf8(get_file_bytes)?;
println!("* FILE CONTENTS:");
Expand Down
14 changes: 10 additions & 4 deletions rust/hermes-ipfs/examples/hermes-ipfs-cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,21 @@ async fn main() -> anyhow::Result<()> {
Commands::AddFile => {
println!("Adding file");
let contents = lipsum(42);
let ipfs_path = hermes_node
.add_ipfs_file(contents.into_bytes().into())
.await?;
let ipfs_path = hermes_node.add_ipfs_file(contents.into_bytes()).await?;
println!("Added file: {ipfs_path}");
},
Commands::GetFile { ipfs_path_str } => {
println!("Getting file");
let ipfs_path: IpfsPath = ipfs_path_str.parse()?;
let get_file_bytes = hermes_node.get_ipfs_file(ipfs_path.into()).await?;
let get_file_bytes = hermes_node
.get_ipfs_file_cbor(
ipfs_path
.root()
.cid()
.ok_or(anyhow::anyhow!("Could not get CID"))?,
)
.await?;

println!("* Got file, {} bytes:", get_file_bytes.len());
let get_file = String::from_utf8(get_file_bytes)?;
println!("* FILE CONTENTS:");
Expand Down
12 changes: 7 additions & 5 deletions rust/hermes-ipfs/examples/provide-content-dht.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ async fn connect_node_a_upload_and_provide(
println!("***************************************");
println!("* Adding file to IPFS:");
println!("");
let ipfs_path = hermes_ipfs.add_ipfs_file(file_content.into()).await?;
let ipfs_path = hermes_ipfs.add_ipfs_file(file_content).await?;
println!("* IPFS file published at {ipfs_path}");
let cid = ipfs_path.root().cid().ok_or(anyhow::anyhow!(
"ERROR! Could not extract CID from IPFS path."
Expand Down Expand Up @@ -85,12 +85,14 @@ async fn main() -> anyhow::Result<()> {
println!("***************************************");
println!("* Get content from IPFS path {ipfs_path}");
println!("");
// For illustration, the `ipfs_path` can be obtained from a known CID
let ipfs_path_string = format!("{ipfs_path}");

// Fetch the content from the `ipfs_path`.
let fetched_bytes = hermes_ipfs_b
.get_ipfs_file(ipfs_path_string.parse()?)
.get_ipfs_file_cbor(
ipfs_path
.root()
.cid()
.ok_or(anyhow::anyhow!("Could not get CID"))?,
)
.await?;
assert_eq!(ipfs_file, fetched_bytes);
let fetched_file = String::from_utf8(fetched_bytes)?;
Expand Down
20 changes: 20 additions & 0 deletions rust/hermes-ipfs/src/constant.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//! Define constant needed for IPFS and Document Sync

/// Current document synchronization protocol version.
#[allow(dead_code)]
pub(crate) const PROTOCOL_VERSION: u8 = 1;

/// `CID` version that Doc Sync supports.
#[allow(dead_code)]
pub(crate) const CID_VERSION: u8 = 1;

/// `CID` multihash digest size that Doc Sync supports.
#[allow(dead_code)]
pub(crate) const CID_DIGEST_SIZE: u8 = 32;

/// Multihash SHA256.
#[allow(dead_code)]
pub(crate) const MULTIHASH_SHA256: u8 = 0x12;

/// Codec CBOR.
pub(crate) const CODEC_CBOR: u8 = 0x51;
15 changes: 9 additions & 6 deletions rust/hermes-ipfs/src/doc_sync/envelope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ use catalyst_types::uuid::{self, CborContext, UuidV7};
use ed25519_dalek::VerifyingKey;
use minicbor::{Decode, Encode, Encoder, data::Type, encode::Write};

use crate::doc_sync::{PROTOCOL_VERSION, PublicKey, Signature};
use crate::{
constant::PROTOCOL_VERSION,
doc_sync::{PublicKey, Signature},
};

/// The unsigned portion of the message envelope.
/// This structure corresponds to the **signature input** array:
Expand Down Expand Up @@ -47,7 +50,7 @@ impl EnvelopePayload {
Ok(Self {
peer: PublicKey(peer),
seq: UuidV7::new(),
ver: PROTOCOL_VERSION,
ver: PROTOCOL_VERSION.into(),
payload,
})
}
Expand Down Expand Up @@ -99,7 +102,7 @@ impl EnvelopePayload {
let seq: UuidV7 = decoder.decode_with(&mut CborContext::Tagged)?;
let ver = decoder.u64()?;

if ver != PROTOCOL_VERSION {
if ver != u64::from(PROTOCOL_VERSION) {
return Err(minicbor::decode::Error::message(format!(
"unsupported protocol version: {ver}"
)));
Expand Down Expand Up @@ -169,7 +172,7 @@ impl<'b, C> Decode<'b, C> for EnvelopePayload {
let seq: UuidV7 = d.decode_with(&mut CborContext::Tagged)?;
let ver = d.u64()?;

if ver != PROTOCOL_VERSION {
if ver != u64::from(PROTOCOL_VERSION) {
return Err(minicbor::decode::Error::message(format!(
"unsupported protocol version: {ver}"
)));
Expand Down Expand Up @@ -428,7 +431,7 @@ mod tests {
signed
.encode_with(payload.seq, &mut CborContext::Tagged)
.unwrap();
signed.u64(PROTOCOL_VERSION + 1).unwrap();
signed.u64(PROTOCOL_VERSION.into() + 1).unwrap();
<Vec<u8> as Write>::write_all(signed.writer_mut(), &payload.payload).unwrap();
signed.encode(Signature(signature)).unwrap();

Expand Down Expand Up @@ -512,7 +515,7 @@ mod tests {
bad_array
.encode_with(payload.seq, &mut CborContext::Tagged)
.unwrap();
bad_array.u64(PROTOCOL_VERSION).unwrap();
bad_array.u64(PROTOCOL_VERSION.into()).unwrap();
// Skip payload & signature to force length error or skip signature

let mut envelope = Encoder::new(Vec::new());
Expand Down
19 changes: 3 additions & 16 deletions rust/hermes-ipfs/src/doc_sync/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,13 @@ pub use envelope::{Envelope, EnvelopePayload};
use minicbor::{Decode, Encode, Encoder, decode, encode::Write};
pub use state_machine::{StateMachine, StateSnapshot, SyncState};

/// Current document synchronization protocol version.
const PROTOCOL_VERSION: u64 = 1;

/// `CID` version that Doc Sync supports.
const CID_VERSION: u8 = 1;

/// `CID` codec that Doc Sync supports (CBOR).
const CID_CODEC: u8 = 0x51;

/// `CID` multihash code that Doc Sync supports (SHA256).
const CID_MULTIHASH_CODE: u8 = 0x12;

/// `CID` multihash digest size that Doc Sync supports.
const CID_DIGEST_SIZE: u8 = 32;
use crate::constant::{CID_DIGEST_SIZE, CID_VERSION, CODEC_CBOR, MULTIHASH_SHA256};

/// Validates CID according to Doc Sync specification constraints.
fn validate_cid(cid: &crate::Cid) -> bool {
cid.version() as u8 == CID_VERSION
&& cid.codec() == u64::from(CID_CODEC)
&& cid.hash().code() == u64::from(CID_MULTIHASH_CODE)
&& cid.codec() == u64::from(CODEC_CBOR)
&& cid.hash().code() == u64::from(MULTIHASH_SHA256)
&& cid.hash().digest().len() == usize::from(CID_DIGEST_SIZE)
}

Expand Down
13 changes: 8 additions & 5 deletions rust/hermes-ipfs/src/doc_sync/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -544,13 +544,16 @@ mod tests {
mod body {
use minicbor::data::{Tag, Token};

use crate::constant::{CID_DIGEST_SIZE, CODEC_CBOR, MULTIHASH_SHA256, PROTOCOL_VERSION};

// Generates a valid Doc Sync `CID` (according to the spec).
const fn generate_cid(seed: u8) -> [u8; 36] {
const VERSION: u8 = 1;
const CODEC_CBOR: u8 = 0x51;
const CODE_SHA_256: u8 = 0x12;
const DIGEST_32: u8 = 0x20;
let prefix = [VERSION, CODEC_CBOR, CODE_SHA_256, DIGEST_32];
let prefix = [
PROTOCOL_VERSION,
CODEC_CBOR,
MULTIHASH_SHA256,
CID_DIGEST_SIZE,
];
let mut ret = [seed; 36];
ret.split_at_mut(prefix.len()).0.copy_from_slice(&prefix);
ret
Expand Down
Loading