Skip to content

Commit b2064d7

Browse files
authored
fix: remove invalid multicodec2string mappings (#137)
* refactor: remove Codecs table * chore: go-cid 0.2.0 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. This removes invalid mappings entirely. https://github.com/multiformats/go-multicodec should be used instead.
1 parent ddd9ef7 commit b2064d7

File tree

4 files changed

+23
-123
lines changed

4 files changed

+23
-123
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,17 @@ fmt.Println("Got CID: ", c)
5959
#### Creating a CID from scratch
6060

6161
```go
62+
63+
import (
64+
cid "github.com/ipfs/go-cid"
65+
mc "github.com/multiformats/go-multicodec"
66+
mh "github.com/multiformats/go-multihash"
67+
)
68+
6269
// Create a cid manually by specifying the 'prefix' parameters
6370
pref := cid.Prefix{
6471
Version: 1,
65-
Codec: cid.Raw,
72+
Codec: mc.Raw,
6673
MhType: mh.SHA2_256,
6774
MhLength: -1, // default length
6875
}

cid.go

Lines changed: 14 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,21 @@ var (
4747
ErrInvalidEncoding = errors.New("invalid base encoding")
4848
)
4949

50-
// These are multicodec-packed content types. The should match
51-
// the codes described in the authoritative document:
52-
// https://github.com/multiformats/multicodec/blob/master/table.csv
50+
// Consts below are DEPRECATED and left only for legacy reasons:
51+
// <https://github.com/ipfs/go-cid/pull/137>
52+
// Modern code should use consts from go-multicodec instead:
53+
// <https://github.com/multiformats/go-multicodec>
5354
const (
54-
Raw = 0x55
55-
56-
DagProtobuf = 0x70
57-
DagCBOR = 0x71
58-
Libp2pKey = 0x72
59-
60-
GitRaw = 0x78
61-
62-
DagJOSE = 0x85
55+
// common ones
56+
Raw = 0x55
57+
DagProtobuf = 0x70 // https://ipld.io/docs/codecs/known/dag-pb/
58+
DagCBOR = 0x71 // https://ipld.io/docs/codecs/known/dag-cbor/
59+
DagJSON = 0x0129 // https://ipld.io/docs/codecs/known/dag-json/
60+
Libp2pKey = 0x72 // https://github.com/libp2p/specs/blob/master/peer-ids/peer-ids.md#peer-ids
61+
62+
// other
63+
GitRaw = 0x78
64+
DagJOSE = 0x85 // https://ipld.io/specs/codecs/dag-jose/spec/
6365
EthBlock = 0x90
6466
EthBlockList = 0x91
6567
EthTxTrie = 0x92
@@ -81,64 +83,6 @@ const (
8183
FilCommitmentSealed = 0xf102
8284
)
8385

84-
// Codecs maps the name of a codec to its type
85-
var Codecs = map[string]uint64{
86-
"v0": DagProtobuf,
87-
"raw": Raw,
88-
"protobuf": DagProtobuf,
89-
"cbor": DagCBOR,
90-
"libp2p-key": Libp2pKey,
91-
"git-raw": GitRaw,
92-
"eth-block": EthBlock,
93-
"eth-block-list": EthBlockList,
94-
"eth-tx-trie": EthTxTrie,
95-
"eth-tx": EthTx,
96-
"eth-tx-receipt-trie": EthTxReceiptTrie,
97-
"eth-tx-receipt": EthTxReceipt,
98-
"eth-state-trie": EthStateTrie,
99-
"eth-account-snapshot": EthAccountSnapshot,
100-
"eth-storage-trie": EthStorageTrie,
101-
"bitcoin-block": BitcoinBlock,
102-
"bitcoin-tx": BitcoinTx,
103-
"zcash-block": ZcashBlock,
104-
"zcash-tx": ZcashTx,
105-
"decred-block": DecredBlock,
106-
"decred-tx": DecredTx,
107-
"dash-block": DashBlock,
108-
"dash-tx": DashTx,
109-
"fil-commitment-unsealed": FilCommitmentUnsealed,
110-
"fil-commitment-sealed": FilCommitmentSealed,
111-
"dag-jose": DagJOSE,
112-
}
113-
114-
// CodecToStr maps the numeric codec to its name
115-
var CodecToStr = map[uint64]string{
116-
Raw: "raw",
117-
DagProtobuf: "protobuf",
118-
DagCBOR: "cbor",
119-
GitRaw: "git-raw",
120-
EthBlock: "eth-block",
121-
EthBlockList: "eth-block-list",
122-
EthTxTrie: "eth-tx-trie",
123-
EthTx: "eth-tx",
124-
EthTxReceiptTrie: "eth-tx-receipt-trie",
125-
EthTxReceipt: "eth-tx-receipt",
126-
EthStateTrie: "eth-state-trie",
127-
EthAccountSnapshot: "eth-account-snapshot",
128-
EthStorageTrie: "eth-storage-trie",
129-
BitcoinBlock: "bitcoin-block",
130-
BitcoinTx: "bitcoin-tx",
131-
ZcashBlock: "zcash-block",
132-
ZcashTx: "zcash-tx",
133-
DecredBlock: "decred-block",
134-
DecredTx: "decred-tx",
135-
DashBlock: "dash-block",
136-
DashTx: "dash-tx",
137-
FilCommitmentUnsealed: "fil-commitment-unsealed",
138-
FilCommitmentSealed: "fil-commitment-sealed",
139-
DagJOSE: "dag-jose",
140-
}
141-
14286
// tryNewCidV0 tries to convert a multihash into a CIDv0 CID and returns an
14387
// error on failure.
14488
func tryNewCidV0(mhash mh.Multihash) (Cid, error) {

cid_test.go

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -15,37 +15,6 @@ import (
1515
mh "github.com/multiformats/go-multihash"
1616
)
1717

18-
// Copying the "silly test" idea from
19-
// https://github.com/multiformats/go-multihash/blob/7aa9f26a231c6f34f4e9fad52bf580fd36627285/multihash_test.go#L13
20-
// Makes it so changing the table accidentally has to happen twice.
21-
var tCodecs = map[uint64]string{
22-
Raw: "raw",
23-
DagProtobuf: "protobuf",
24-
DagCBOR: "cbor",
25-
Libp2pKey: "libp2p-key",
26-
GitRaw: "git-raw",
27-
EthBlock: "eth-block",
28-
EthBlockList: "eth-block-list",
29-
EthTxTrie: "eth-tx-trie",
30-
EthTx: "eth-tx",
31-
EthTxReceiptTrie: "eth-tx-receipt-trie",
32-
EthTxReceipt: "eth-tx-receipt",
33-
EthStateTrie: "eth-state-trie",
34-
EthAccountSnapshot: "eth-account-snapshot",
35-
EthStorageTrie: "eth-storage-trie",
36-
BitcoinBlock: "bitcoin-block",
37-
BitcoinTx: "bitcoin-tx",
38-
ZcashBlock: "zcash-block",
39-
ZcashTx: "zcash-tx",
40-
DecredBlock: "decred-block",
41-
DecredTx: "decred-tx",
42-
DashBlock: "dash-block",
43-
DashTx: "dash-tx",
44-
FilCommitmentUnsealed: "fil-commitment-unsealed",
45-
FilCommitmentSealed: "fil-commitment-sealed",
46-
DagJOSE: "dag-jose",
47-
}
48-
4918
func assertEqual(t *testing.T, a, b Cid) {
5019
if a.Type() != b.Type() {
5120
t.Fatal("mismatch on type")
@@ -60,26 +29,6 @@ func assertEqual(t *testing.T, a, b Cid) {
6029
}
6130
}
6231

63-
func TestTable(t *testing.T) {
64-
if len(tCodecs) != len(Codecs)-1 {
65-
t.Errorf("Item count mismatch in the Table of Codec. Should be %d, got %d", len(tCodecs)+1, len(Codecs))
66-
}
67-
68-
for k, v := range tCodecs {
69-
if Codecs[v] != k {
70-
t.Errorf("Table mismatch: 0x%x %s", k, v)
71-
}
72-
}
73-
}
74-
75-
// The table returns cid.DagProtobuf for "v0"
76-
// so we test it apart
77-
func TestTableForV0(t *testing.T) {
78-
if Codecs["v0"] != DagProtobuf {
79-
t.Error("Table mismatch: Codecs[\"v0\"] should resolve to DagProtobuf (0x70)")
80-
}
81-
}
82-
8332
func TestPrefixSum(t *testing.T) {
8433
// Test creating CIDs both manually and with Prefix.
8534
// Tests: https://github.com/ipfs/go-cid/issues/83

version.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"version": "v0.1.0"
2+
"version": "v0.2.0"
33
}

0 commit comments

Comments
 (0)