Skip to content

External prover #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
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
10 changes: 2 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "multicid"
version = "1.0.5"
version = "1.1.0"
edition = "2021"
authors = ["Dave Grantham <[email protected]>"]
description = "Multicodec compatible content identifier implementation"
Expand All @@ -13,13 +13,7 @@ default = ["serde"]
dag_cbor = ["serde_cbor", "serde_cbor/tags"]

[dependencies]
multibase = { version = "1.0", git = "https://github.com/cryptidtech/rust-multibase.git" }
multicodec = { version = "1.0", git = "https://github.com/cryptidtech/rust-multicodec.git" }
multihash = { version = "1.0", git = "https://github.com/cryptidtech/multihash.git" }
multikey = { version = "1.0", git = "https://github.com/DougAnderson444/multikey.git" }
multisig = { version = "1.0", git = "https://github.com/DougAnderson444/multisig.git" }
multitrait = { version = "1.0", git = "https://github.com/cryptidtech/multitrait.git" }
multiutil = { version = "1.0", git = "https://github.com/cryptidtech/multiutil.git" }
multikey = { version = "1.0", git = "https://github.com/DougAnderson444/multikey.git", branch = "deps" }
rand = "0.8"
serde = { version = "1.0", default-features = false, features = [
"alloc",
Expand Down
41 changes: 35 additions & 6 deletions src/cid.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// SPDX-License-Idnetifier: Apache-2.0
use super::*;
use crate::{error::CidError, Error};
use core::fmt;
use multibase::Base;
Expand All @@ -18,7 +19,7 @@ pub type LegacyEncodedCid = BaseEncoded<Cid, Base58Encoder>;
pub type EncodedCid = BaseEncoded<Cid, DetectedEncoder>;

/// implementation of cid
#[derive(Clone, Eq, Ord, PartialOrd, PartialEq)]
#[derive(Clone, Eq, Ord, PartialOrd, PartialEq, Hash)]
pub struct Cid {
/// the version of the Cid
pub(crate) codec: Codec,
Expand Down Expand Up @@ -156,6 +157,13 @@ impl fmt::Debug for Cid {
}
}

impl fmt::Display for Cid {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let encoded = EncodedCid::new(self.encoding(), self.clone());
write!(f, "{}", encoded)
}
}

/// Hash builder that takes the codec and the data and produces a Multihash
#[derive(Clone, Debug, Default)]
pub struct Builder {
Expand Down Expand Up @@ -210,8 +218,7 @@ impl Builder {
return Err(CidError::LegacyCid.into());
}
Ok(EncodedCid::new(
self.base_encoding
.unwrap_or_else(Cid::preferred_encoding),
self.base_encoding.unwrap_or_else(Cid::preferred_encoding),
self.try_build()?,
))
}
Expand Down Expand Up @@ -275,17 +282,23 @@ mod tests {
assert_eq!(Codec::Sha2256, v0_1.hash.codec());

// this does not assume a multibase encoded CID
let v0_2 = EncodedCid::try_from("bafybeihcrr5owouhnms63areolshu2lp4jjbjqlhf4exegk7tnso5ja6py").unwrap();
let v0_2 =
EncodedCid::try_from("bafybeihcrr5owouhnms63areolshu2lp4jjbjqlhf4exegk7tnso5ja6py")
.unwrap();
assert_eq!(Codec::Cidv1, v0_2.codec());
assert_eq!(Codec::DagPb, v0_2.target_codec);
assert_eq!(Codec::Sha2256, v0_2.hash.codec());

let v0_3 = EncodedCid::try_from("f01701220e28c7aeb3a876b25ed822472e47a696fe25214c1672f0972195f9b64eea41e7e").unwrap();
let v0_3 = EncodedCid::try_from(
"f01701220e28c7aeb3a876b25ed822472e47a696fe25214c1672f0972195f9b64eea41e7e",
)
.unwrap();
assert_eq!(Codec::Cidv1, v0_3.codec());
assert_eq!(Codec::DagPb, v0_3.target_codec);
assert_eq!(Codec::Sha2256, v0_3.hash.codec());

let v0_4 = EncodedCid::try_from("uAXASIOKMeus6h2sl7YIkcuR6aW_iUhTBZy8Jchlfm2TupB5-").unwrap();
let v0_4 =
EncodedCid::try_from("uAXASIOKMeus6h2sl7YIkcuR6aW_iUhTBZy8Jchlfm2TupB5-").unwrap();
assert_eq!(Codec::Cidv1, v0_4.codec());
assert_eq!(Codec::DagPb, v0_4.target_codec);
assert_eq!(Codec::Sha2256, v0_4.hash.codec());
Expand Down Expand Up @@ -392,4 +405,20 @@ mod tests {
assert!(cid1 != cid2);
assert!(!cid2.is_null());
}

#[test]
fn test_string_roundtrip() {
let v1 = Builder::new(Codec::Cidv1)
.with_target_codec(Codec::DagCbor)
.with_hash(
&mh::Builder::new_from_bytes(Codec::Sha3512, b"for great justice, move every zig!")
.unwrap()
.try_build()
.unwrap(),
)
.try_build()
.unwrap();
let s = v1.to_string();
assert_eq!(s, EncodedCid::try_from(s.as_str()).unwrap().to_string());
}
}
1 change: 1 addition & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// SPDX-License-Idnetifier: Apache-2.0
use super::*;

/// Errors created by this library
#[derive(Clone, Debug, thiserror::Error)]
Expand Down
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,8 @@ pub mod prelude {
pub use multicodec::Codec;
pub use multiutil::BaseEncoded;
}

// Re-export all multi* crates to avoid dependency conflicts
pub use multikey;
pub use multikey::multicrates;
pub use multikey::multicrates::*;
6 changes: 4 additions & 2 deletions src/serde/de.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// SPDX-License-Idnetifier: Apache-2.0
use super::{multicodec, multihash};
use crate::{vlad, Cid, Vlad};

use core::fmt;
use multicodec::Codec;
use multihash::Multihash;
use multikey::Nonce;
#[cfg(feature = "dag_cbor")]
use multitrait::TryDecodeFrom;
use multikey::multicrates::multitrait::TryDecodeFrom;
use multikey::Nonce;
use serde::{
de::{Error, MapAccess, Visitor},
Deserialize, Deserializer,
Expand Down
30 changes: 18 additions & 12 deletions src/serde/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@
mod de;
mod ser;

use super::*;

#[cfg(test)]
mod tests {
use crate::{cid, vlad};
use super::*;
use crate::{
cid::{self, Cid, EncodedCid},
vlad::{self, EncodedVlad, Vlad},
};
use multicodec::Codec;
use multihash::mh;
use multikey::nonce;
Expand Down Expand Up @@ -261,7 +267,7 @@ mod tests {
let vlad = vlad::Builder::default()
.with_nonce(&nonce)
.with_cid(&cid)
.try_build_encoded()
.try_build_encoded(|cid| Ok(cid.clone().into()))
.unwrap();

assert_tokens(
Expand Down Expand Up @@ -292,7 +298,7 @@ mod tests {
let vlad = vlad::Builder::default()
.with_nonce(&nonce)
.with_cid(&cid)
.try_build()
.try_build(|cid| Ok(cid.clone().into()))
.unwrap();

assert_tokens(
Expand Down Expand Up @@ -355,7 +361,7 @@ mod tests {
let vlad = vlad::Builder::default()
.with_nonce(&nonce)
.with_cid(&cid)
.try_build()
.try_build(|cid| Ok(cid.clone().into()))
.unwrap();

let s = serde_json::to_string(&vlad).unwrap();
Expand Down Expand Up @@ -385,7 +391,7 @@ mod tests {
let vlad = vlad::Builder::default()
.with_nonce(&nonce)
.with_cid(&cid)
.try_build()
.try_build(|cid| Ok(cid.clone().into()))
.unwrap();

let v = serde_cbor::to_vec(&vlad).unwrap();
Expand Down Expand Up @@ -416,7 +422,7 @@ mod tests {
let vlad = vlad::Builder::default()
.with_nonce(&nonce)
.with_cid(&cid)
.try_build()
.try_build(|cid| Ok(cid.clone().into()))
.unwrap();

let v = serde_cbor::to_vec(&vlad).unwrap();
Expand All @@ -427,13 +433,13 @@ mod tests {

#[test]
fn test_null_cid_serde_compact() {
let c = cid::Cid::null();
let c = Cid::null();
assert_tokens(&c.compact(), &[Token::BorrowedBytes(&[1, 0, 0, 0])]);
}

#[test]
fn test_null_cid_serde_readable() {
let c = cid::Cid::null();
let c = Cid::null();
assert_tokens(
&c.readable(),
&[
Expand Down Expand Up @@ -462,13 +468,13 @@ mod tests {

#[test]
fn test_encoded_null_cid_serde_readable() {
let c: cid::EncodedCid = cid::Cid::null().into();
let c: EncodedCid = Cid::null().into();
assert_tokens(&c.readable(), &[Token::BorrowedStr("z2UzHM")]);
}

#[test]
fn test_null_vlad_serde_compact() {
let v = vlad::Vlad::null();
let v = Vlad::null();
assert_tokens(
&v.compact(),
&[Token::BorrowedBytes(&[135, 36, 187, 36, 0, 1, 0, 0, 0])],
Expand All @@ -477,7 +483,7 @@ mod tests {

#[test]
fn test_null_vlad_serde_readable() {
let v = vlad::Vlad::null();
let v = Vlad::null();
assert_tokens(
&v.readable(),
&[
Expand Down Expand Up @@ -520,7 +526,7 @@ mod tests {

#[test]
fn test_encoded_null_vlad_serde_readable() {
let v: vlad::EncodedVlad = vlad::Vlad::null().into();
let v: EncodedVlad = Vlad::null().into();
assert_tokens(&v.readable(), &[Token::BorrowedStr("bq4slwjaaaeaaaaa")]);
}
}
2 changes: 1 addition & 1 deletion src/serde/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl ser::Serialize for Cid {
} else {
#[cfg(feature = "dag_cbor")]
{
use multicodec::Codec;
use multikey::multicrates::multicodec::Codec;

// build the byte string for DAG-CBOR according to the spec
// https://github.com/ipld/specs/blob/master/block-layer/codecs/dag-cbor.md#links
Expand Down
Loading