From da5524780a4ae73d6b2ca765f1df8e7e8b7b0bcd Mon Sep 17 00:00:00 2001 From: Marcin Rataj Date: Wed, 23 Mar 2022 23:10:47 +0100 Subject: [PATCH 1/4] 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 | 46 ++++++++++++++++++++++++++++++++-------------- cid_test.go | 9 +++++++-- 2 files changed, 39 insertions(+), 16 deletions(-) diff --git a/cid.go b/cid.go index 2f3bbf6..c774ea7 100644 --- a/cid.go +++ b/cid.go @@ -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 @@ -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, @@ -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", @@ -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 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) { From 6ca2617460d553e1aa10d4fdced1dcfedd300197 Mon Sep 17 00:00:00 2001 From: Marcin Rataj Date: Thu, 24 Mar 2022 22:07:40 +0100 Subject: [PATCH 2/4] refactor: remove Codecs table People should use https://github.com/multiformats/go-multicodec instead. --- cid.go | 75 ----------------------------------------------------- cid_test.go | 56 --------------------------------------- 2 files changed, 131 deletions(-) diff --git a/cid.go b/cid.go index c774ea7..71ee4ba 100644 --- a/cid.go +++ b/cid.go @@ -58,15 +58,9 @@ const ( 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 @@ -88,75 +82,6 @@ const ( FilCommitmentSealed = 0xf102 ) -// Codecs maps the name of a codec to its type -var Codecs = map[string]uint64{ - "v0": DagProtobuf, // TODO: remove? - "raw": Raw, - "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, - "eth-tx-trie": EthTxTrie, - "eth-tx": EthTx, - "eth-tx-receipt-trie": EthTxReceiptTrie, - "eth-tx-receipt": EthTxReceipt, - "eth-state-trie": EthStateTrie, - "eth-account-snapshot": EthAccountSnapshot, - "eth-storage-trie": EthStorageTrie, - "bitcoin-block": BitcoinBlock, - "bitcoin-tx": BitcoinTx, - "zcash-block": ZcashBlock, - "zcash-tx": ZcashTx, - "decred-block": DecredBlock, - "decred-tx": DecredTx, - "dash-block": DashBlock, - "dash-tx": DashTx, - "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: "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", - EthTxTrie: "eth-tx-trie", - EthTx: "eth-tx", - EthTxReceiptTrie: "eth-tx-receipt-trie", - EthTxReceipt: "eth-tx-receipt", - EthStateTrie: "eth-state-trie", - EthAccountSnapshot: "eth-account-snapshot", - EthStorageTrie: "eth-storage-trie", - BitcoinBlock: "bitcoin-block", - BitcoinTx: "bitcoin-tx", - ZcashBlock: "zcash-block", - ZcashTx: "zcash-tx", - DecredBlock: "decred-block", - DecredTx: "decred-tx", - DashBlock: "dash-block", - DashTx: "dash-tx", - 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 // error on failure. func tryNewCidV0(mhash mh.Multihash) (Cid, error) { diff --git a/cid_test.go b/cid_test.go index 096518c..5503506 100644 --- a/cid_test.go +++ b/cid_test.go @@ -15,42 +15,6 @@ import ( mh "github.com/multiformats/go-multihash" ) -// Copying the "silly test" idea from -// https://github.com/multiformats/go-multihash/blob/7aa9f26a231c6f34f4e9fad52bf580fd36627285/multihash_test.go#L13 -// Makes it so changing the table accidentally has to happen twice. -var tCodecs = map[uint64]string{ - Raw: "raw", - 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", - EthTxTrie: "eth-tx-trie", - EthTx: "eth-tx", - EthTxReceiptTrie: "eth-tx-receipt-trie", - EthTxReceipt: "eth-tx-receipt", - EthStateTrie: "eth-state-trie", - EthAccountSnapshot: "eth-account-snapshot", - EthStorageTrie: "eth-storage-trie", - BitcoinBlock: "bitcoin-block", - BitcoinTx: "bitcoin-tx", - ZcashBlock: "zcash-block", - ZcashTx: "zcash-tx", - DecredBlock: "decred-block", - DecredTx: "decred-tx", - DashBlock: "dash-block", - DashTx: "dash-tx", - FilCommitmentUnsealed: "fil-commitment-unsealed", - FilCommitmentSealed: "fil-commitment-sealed", - DagJOSE: "dag-jose", - DagCOSE: "dag-cose", -} - func assertEqual(t *testing.T, a, b Cid) { if a.Type() != b.Type() { t.Fatal("mismatch on type") @@ -65,26 +29,6 @@ func assertEqual(t *testing.T, a, b Cid) { } } -func TestTable(t *testing.T) { - if len(tCodecs) != len(Codecs)-1 { - t.Errorf("Item count mismatch in the Table of Codec. Should be %d, got %d", len(tCodecs)+1, len(Codecs)) - } - - for k, v := range tCodecs { - if Codecs[v] != k { - t.Errorf("Table mismatch: 0x%x %s", k, v) - } - } -} - -// The table returns cid.DagProtobuf for "v0" -// so we test it apart -func TestTableForV0(t *testing.T) { - if Codecs["v0"] != DagProtobuf { - t.Error("Table mismatch: Codecs[\"v0\"] should resolve to DagProtobuf (0x70)") - } -} - func TestPrefixSum(t *testing.T) { // Test creating CIDs both manually and with Prefix. // Tests: https://github.com/ipfs/go-cid/issues/83 From 85198b907298a7071d2b5fa0405d92e5eb8aecec Mon Sep 17 00:00:00 2001 From: Marcin Rataj Date: Thu, 31 Mar 2022 13:40:47 +0200 Subject: [PATCH 3/4] docs: point people at go-multicodec --- README.md | 9 ++++++++- cid.go | 9 +++++---- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 4f54343..89da041 100644 --- a/README.md +++ b/README.md @@ -59,10 +59,17 @@ fmt.Println("Got CID: ", c) #### Creating a CID from scratch ```go + +import ( + cid "github.com/ipfs/go-cid" + mc "github.com/multiformats/go-multicodec" + mh "github.com/multiformats/go-multihash" +) + // Create a cid manually by specifying the 'prefix' parameters pref := cid.Prefix{ Version: 1, - Codec: cid.Raw, + Codec: mc.Raw, MhType: mh.SHA2_256, MhLength: -1, // default length } diff --git a/cid.go b/cid.go index 71ee4ba..bc5704a 100644 --- a/cid.go +++ b/cid.go @@ -47,11 +47,12 @@ var ( ErrInvalidEncoding = errors.New("invalid base encoding") ) -// These are multicodec-packed content types. The should match -// the codes described in the authoritative document: -// https://github.com/multiformats/multicodec/blob/master/table.csv +// Consts below are DEPRECATED and left only for legacy reasons: +// +// Modern code should use consts from go-multicodec instead: +// const ( - // core IPLD + // common ones Raw = 0x55 DagProtobuf = 0x70 // https://ipld.io/docs/codecs/known/dag-pb/ DagCBOR = 0x71 // https://ipld.io/docs/codecs/known/dag-cbor/ From fdd8972cd76c99015d73492d75f46acf963396e6 Mon Sep 17 00:00:00 2001 From: Marcin Rataj Date: Thu, 21 Apr 2022 22:29:52 +0200 Subject: [PATCH 4/4] chore: go-cid 0.2.0 --- version.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/version.json b/version.json index 557859c..1437d5b 100644 --- a/version.json +++ b/version.json @@ -1,3 +1,3 @@ { - "version": "v0.1.0" + "version": "v0.2.0" }