From ab3f744167ee4aa792551effb8a675c6519ecdb4 Mon Sep 17 00:00:00 2001 From: Marcin Rataj Date: Wed, 23 Mar 2022 23:10:47 +0100 Subject: [PATCH] fix: codec table and dag-json and dag-cbor 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 --- cid.go | 47 +++++++++++++++++++++++++++++++++-------------- cid_test.go | 9 +++++++-- 2 files changed, 40 insertions(+), 16 deletions(-) diff --git a/cid.go b/cid.go index 2f3bbf6..8d8e6cb 100644 --- a/cid.go +++ b/cid.go @@ -51,15 +51,23 @@ 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 + Identity = 0x00 + 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 @@ -83,11 +91,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, @@ -109,13 +121,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", @@ -137,6 +155,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 diff --git a/cid_test.go b/cid_test.go index 1e40883..096518c 100644 --- a/cid_test.go +++ b/cid_test.go @@ -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", @@ -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) {