dwyl / dart_cid

❄️ dart-lang implementation of cid ("content id") = human-friendly (readable/typeable) unique ID for distributed/decentralised systems
GNU General Public License v2.0
11 stars 0 forks source link

decode cid v1? #4

Closed lastmeta closed 1 year ago

lastmeta commented 1 year ago

what additions would we have to make to this package to be able to decode CIDv1? Or even convert them to CIDv0s?

LuchoTurtle commented 1 year ago

Hey there, thanks for opening an issue :) Yeah, as it stands, the package is really basic, just creates a cid and that's it.

Decoding a given cid should be fairly simple, it's just a matter of following the steps in https://github.com/multiformats/cid/blob/ef1b2002394b15b1e6c26c30545fd485f2c4c138/README.md#decoding-algorithm. Because CIDv0 and CIDv1 are pretty distinct, it should be simple to decode the information.

Converting a CIDv1 to CIDv0 is a different thing, though. Because CIDv0 has implicit assumptions, not all CIDv1 are "convertible" to CIDv0.

CIDv0

CIDv0 is a backwards-compatible version, where:

  • the multibase is always base58btc and implicit (not written)
  • the multicodec is always protobuf-mdag and implicit (not written)
  • the cid-version is always cidv0 and implicit (not written)
  • the multihash is written as is but is always a full (length 32) sha256 hash.
cidv0 ::= <multihash-content-address>

CIDv1

See the section: How does it work? - Protocol Description

<cidv1> ::= <multibase-prefix><cid-version><multicodec-content-type><multihash-content-address>

-> from https://github.com/multiformats/cid/blob/ef1b2002394b15b1e6c26c30545fd485f2c4c138/README.md#versions

But I agree, being able to decode a CIDv1 should be in this package. I'll work on this whenever I have time. Meanwhile, you can try to create a PR if you think you can do it :) You just ought to "decompose" a given cid string and return an object with its information, a la https://github.com/multiformats/cid/blob/ef1b2002394b15b1e6c26c30545fd485f2c4c138/README.md#how-does-it-work---protocol-description.