Skip to content

Commit

Permalink
fix: codec table and dag-json and dag-cbor
Browse files Browse the repository at this point in the history
Codec table was missing dag-json and it had invalid code for dag-cbor.
It also had invalid string representation of dag-pb -- it was using
'protobuf' which is a totally different code.

I fixed those bugs, and added generic variants for Protobuf, JSON and
CBOR, so go-cid finally match the source of truth at
https://github.com/multiformats/multicodec/blob/master/table.csv
  • Loading branch information
lidel committed Mar 23, 2022
1 parent dc3bb41 commit da55247
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 16 deletions.
46 changes: 32 additions & 14 deletions cid.go
Expand Up @@ -51,15 +51,22 @@ var (
// the codes described in the authoritative document:
// https://github.com/multiformats/multicodec/blob/master/table.csv
const (
Raw = 0x55

DagProtobuf = 0x70
DagCBOR = 0x71
Libp2pKey = 0x72

GitRaw = 0x78

DagJOSE = 0x85
// core IPLD
Raw = 0x55
DagProtobuf = 0x70 // https://ipld.io/docs/codecs/known/dag-pb/
DagCBOR = 0x71 // https://ipld.io/docs/codecs/known/dag-cbor/
DagJSON = 0x0129 // https://ipld.io/docs/codecs/known/dag-json/
Libp2pKey = 0x72 // https://github.com/libp2p/specs/blob/master/peer-ids/peer-ids.md#peer-ids

// generic, non-dag variants
Protobuf = 0x50
CBOR = 0x51
JSON = 0x0200

// other
GitRaw = 0x78
DagJOSE = 0x85 // https://ipld.io/specs/codecs/dag-jose/spec/
DagCOSE = 0x86
EthBlock = 0x90
EthBlockList = 0x91
EthTxTrie = 0x92
Expand All @@ -83,11 +90,15 @@ const (

// Codecs maps the name of a codec to its type
var Codecs = map[string]uint64{
"v0": DagProtobuf,
"v0": DagProtobuf, // TODO: remove?
"raw": Raw,
"protobuf": DagProtobuf,
"cbor": DagCBOR,
"dag-pb": DagProtobuf,
"dag-cbor": DagCBOR,
"dag-json": DagJSON,
"libp2p-key": Libp2pKey,
"protobuf": Protobuf,
"cbor": CBOR,
"json": JSON,
"git-raw": GitRaw,
"eth-block": EthBlock,
"eth-block-list": EthBlockList,
Expand All @@ -109,13 +120,19 @@ var Codecs = map[string]uint64{
"fil-commitment-unsealed": FilCommitmentUnsealed,
"fil-commitment-sealed": FilCommitmentSealed,
"dag-jose": DagJOSE,
"dag-cose": DagCOSE,
}

// CodecToStr maps the numeric codec to its name
var CodecToStr = map[uint64]string{
Raw: "raw",
DagProtobuf: "protobuf",
DagCBOR: "cbor",
DagProtobuf: "dag-pb",
DagCBOR: "dag-cbor",
DagJSON: "dag-json",
Libp2pKey: "libp2p-key",
Protobuf: "protobuf",
CBOR: "cbor",
JSON: "json",
GitRaw: "git-raw",
EthBlock: "eth-block",
EthBlockList: "eth-block-list",
Expand All @@ -137,6 +154,7 @@ var CodecToStr = map[uint64]string{
FilCommitmentUnsealed: "fil-commitment-unsealed",
FilCommitmentSealed: "fil-commitment-sealed",
DagJOSE: "dag-jose",
DagCOSE: "dag-cose",
}

// tryNewCidV0 tries to convert a multihash into a CIDv0 CID and returns an
Expand Down
9 changes: 7 additions & 2 deletions cid_test.go
Expand Up @@ -20,9 +20,13 @@ import (
// Makes it so changing the table accidentally has to happen twice.
var tCodecs = map[uint64]string{
Raw: "raw",
DagProtobuf: "protobuf",
DagCBOR: "cbor",
DagProtobuf: "dag-pb",
DagCBOR: "dag-cbor",
DagJSON: "dag-json",
Libp2pKey: "libp2p-key",
Protobuf: "protobuf",
CBOR: "cbor",
JSON: "json",
GitRaw: "git-raw",
EthBlock: "eth-block",
EthBlockList: "eth-block-list",
Expand All @@ -44,6 +48,7 @@ var tCodecs = map[uint64]string{
FilCommitmentUnsealed: "fil-commitment-unsealed",
FilCommitmentSealed: "fil-commitment-sealed",
DagJOSE: "dag-jose",
DagCOSE: "dag-cose",
}

func assertEqual(t *testing.T, a, b Cid) {
Expand Down

0 comments on commit da55247

Please sign in to comment.