use cid::Cid;
use multihash::Multihash;
use multihash_codetable::{Code, MultihashDigest};
use sha2::{Digest, Sha256};
pub const DAG_CBOR_CODEC: u64 = 0x71;
pub type ContentHash = [u8; 32];
pub type DagCborMultihash = Multihash<64>;
pub fn sha2_256_content_hash(bytes: &[u8]) -> ContentHash {
Sha256::digest(bytes).into()
}
pub fn dag_cbor_multihash(bytes: &[u8]) -> DagCborMultihash {
Code::Sha2_256.digest(bytes)
}
pub fn compute_cid_dag_cbor(bytes: &[u8]) -> String {
let hash = dag_cbor_multihash(bytes);
let cid = Cid::new_v1(DAG_CBOR_CODEC, hash);
cid.to_string().to_lowercase()
}
pub fn verify_dag_cbor_cid(cid_str: &str, bytes: &[u8]) -> (bool, String, String) {
let expected = compute_cid_dag_cbor(bytes);
let actual = cid_str.to_lowercase();
(expected == actual, expected, actual)
}
pub fn is_valid_cid_string(s: &str) -> bool {
Cid::try_from(s).is_ok()
}
pub fn try_parse_cid(s: &str) -> Option<Cid> {
Cid::try_from(s).ok()
}