fix: dag.put create cidv0 for dagpb with sha2-256
@@ -17,17 +17,20 @@ async fn put_query<T: IpfsTypes>(
query: PutQuery,
mut form: multipart::FormData,
) -> Result<impl Reply, Rejection> {
- let format = match query.format.as_deref().unwrap_or("dag-cbor") {
- "dag-cbor" => Codec::DagCBOR,
- "dag-pb" => Codec::DagProtobuf,
- "dag-json" => Codec::DagJSON,
- "raw" => Codec::Raw,
+
+ use multihash::{Multihash, Sha2_256, Sha2_512, Sha3_512};
+
+ let (format, v0_fmt) = match query.format.as_deref().unwrap_or("dag-cbor") {
+ "dag-cbor" => (Codec::DagCBOR, false),
+ "dag-pb" => (Codec::DagProtobuf, true),
+ "dag-json" => (Codec::DagJSON, false),
+ "raw" => (Codec::Raw, false),
_ => return Err(StringError::from("unknown codec").into()),
};
- let hasher = match query.hash.as_deref().unwrap_or("sha2-256") {
- "sha2-256" => multihash::Sha2_256::digest,
- "sha2-512" => multihash::Sha2_512::digest,
- "sha3-512" => multihash::Sha3_512::digest,
+ let (hasher, v0_hash) = match query.hash.as_deref().unwrap_or("sha2-256") {
+ "sha2-256" => (Sha2_256::digest as fn(&[u8]) -> Multihash, true),
+ "sha2-512" => (Sha2_512::digest as fn(&[u8]) -> Multihash, false),
+ "sha3-512" => (Sha3_512::digest as fn(&[u8]) -> Multihash, false),
_ => return Err(StringError::from("unknown hash").into()),
};
let mut buf = form
@@ -41,7 +44,13 @@ async fn put_query<T: IpfsTypes>(
.map_err(|_| InvalidMultipartFormData)?;
let data = buf.to_bytes().as_ref().to_vec().into_boxed_slice();
let digest = hasher(&data);
- let cid = Cid::new_v1(format, digest);
+ let cid = if v0_fmt && v0_hash {
+ // this is quite ugly way but apparently js-ipfs generates a v0 cid for this combination
+ // which is also created by go-ipfs
+ Cid::new_v0(digest).expect("cidv0 creation cannot fail for dag-pb and sha2-256")
+ } else {
+ Cid::new_v1(format, digest)
+ };
let reply = json!({
"Cid": { "/": cid.to_string() }
});
