How to auto resolve ipld refs by dag.get

#1

I’ve seen many cases where code like this works:

const obj = { simple: 'object' };
const dagParams = { format: 'dag-cbor', hashAlg: 'sha3-512' };
const cid = await ipfs.dag.put(obj, dagParams);
const cidString = cid.toBaseEncodedString();

const obj2 = {
  complex: 'object',
  link: {
    '/' : cidString
  }
}
const cid2 = await ipfs.dag.put(obj2, dagParams)
const cid2String = cid2.toBaseEncodedString();

const obj2 = await ipfs.dag.get(cid2String)
console.log(obj2.value)
// {
//   complex: "object",
//   link: {
//     "simple" : "object"
//   }
// }

But now, after many great updates - i got something like this in response:

{  complex: "object",
     link:
      CID {
        codec: 'dag-cbor',
        version: 1,
        multihash:
         <Buffer 12 20 14 da 02 e8 8d 43 e5 a5 c1 b7 c1 af 93 a4 5b 53 7c 3e 4c e2 87 03 a3 1c 29 04 59 6e b6 f1 90 65> } }

Can anybody help me with 2 cases:

  1. Auto resolve CID objects that points to ipld data.
  2. Convert all CID objects in structure toBaseEncodedString

I tried to work with js-ipfs@0.35.0 and ipfs-http-client@30.1.4, but result the same.

Whether there is a some callback that i can use for resolve or convert to base encoded string?

#2

@lidel - this smells like a potential CID versioning issue. Any suggestions for Jonybang?

#3

I don’t believe there’s a way to get back an object with all CID links resolved.

You can always await ipfs.dag.get(cid2String, 'link') to get back the linked obj.

There is also ipfs.dag.tree, which will give you back all the travserable paths in an object and you can detect if something is a CID by using CID.isCID(obj2.link) for example.

See https://github.com/ipfs/interface-js-ipfs-core/blob/master/SPEC/DAG.md for more info.

#4

Okay, thank you. But in some of previous versions - did it work? It was refused due to inefficiency or security concerns?
And what about loadFormat function in ipld config, may it used for resolve or convert to string cid properties?

#5

But in some of previous versions - did it work? It was refused due to inefficiency or security concerns?

No, as far as I’m aware that’s never been a feature.

And what about loadFormat function in ipld config, may it used for resolve or convert to string cid properties?

loadFormat is for loading a resolver for a particular IPLD format, allowing IPFS to read git, ethereum, zcash or other data.

see https://github.com/ipld/js-ipld#optionsloadformatcodec

#6

So i could to make my own resolver for ipld for convert cid objects to strings or resolve them to objects with data?