Js-ipfs: dag-cbor - number conversion different in go and js implementations?

Created on 1 Apr 2019  路  3Comments  路  Source: ipfs/js-ipfs

禄 jsipfs --version
0.34.4
禄 ipfs --version
ipfs version 0.4.19
禄 node --version
v11.6.0

OS: Mac OS X v10.14.3 (18D109).

Seems like the js and go implementations serialise numbers differently when using dag interface.

Take the following json content:

{
  "a": 1,
}

and let's put it to IPFS using the following command:

$ echo "{\"a\": 1}" | ipfs dag put --format=dag-cbor --hash=sha2-256
zdpuAxTWBh3d49ZCgPP44BT8AAa9qiqxB167yZCFdxHxVg6gN

Then the same content, but now using jsipfs with the help of the following script:

const ipfsClient = require('ipfs-http-client')

// or connect with multiaddr
const ipfs = ipfsClient('/ip4/127.0.0.1/tcp/5001')

const obj = {
  a: 1
}

ipfs.dag.put(obj, { format: 'dag-cbor', hashAlg: 'sha2-256' }, (err, cid) => {
  console.log(cid.toBaseEncodedString())
  process.exit()
})

process.stdin.resume()

The output is: zdpuB2H7FsgxU1PVuGcwk4TDYGgv7xrQ7naFJ1YHfRV71t7Rf.

Finally, when I try to reconstruct the CID myself using:

const dagCBOR = require("ipld-dag-cbor")
const multihashes = require("multihashes")
const sha256 = require("js-sha256").sha256
const CID = require("cids")

const obj = {
  a: 1
}

const getDagCBOR = obj => new Promise((resolve, reject) => {
    dagCBOR.util.serialize(obj, (err, serialized) => {
      if (err) {
        reject(err)
      }
      resolve(serialized)
    })
})

const serialized = await getDagCBOR(obj)
const hash = Buffer.from(sha256.arrayBuffer(serialized))
const mhash = multihashes.encode(hash, 'sha2-256')
const cid = new CID(1, 'dag-cbor', mhash)
const encodedCid = cid.toBaseEncodedString()
console.log('encodedCid=', encodedCid)

I get zdpuB2H7FsgxU1PVuGcwk4TDYGgv7xrQ7naFJ1YHfRV71t7Rf as well (so the same as via ipfs-http-client).

Now, if I change the content to be:

{
  "a": "1",
}

then all three methods return the same CID: zdpuAqnunWR8B95ZbsDpCrHewXcVyRgixLwbThCdjZ37PCx8G

P1 dihard kinbug statuready

Most helpful comment

All 3 comments

@vmx @rvagg any chance you can look into this? CBOR encoding seems to differ between JS/Go.

ipfs-http-client does the serialization client side so the two JS code examples above are effectively the same.

Exploring these two gives identical CID info with a differing hash:

Screenshot 2019-04-01 at 18 55 16

https://explore.ipld.io/#/explore/zdpuB2H7FsgxU1PVuGcwk4TDYGgv7xrQ7naFJ1YHfRV71t7Rf

Screenshot 2019-04-01 at 18 55 24

https://explore.ipld.io/#/explore/zdpuAxTWBh3d49ZCgPP44BT8AAa9qiqxB167yZCFdxHxVg6gN

Thanks @vmx for pointing out. I will be following.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ya7ya picture ya7ya  路  3Comments

daviddias picture daviddias  路  3Comments

OliverUv picture OliverUv  路  3Comments

Ntmf picture Ntmf  路  4Comments

lifeBCE picture lifeBCE  路  3Comments