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 2f3bbf6..bc5704a 100644 --- a/cid.go +++ b/cid.go @@ -47,19 +47,21 @@ 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 ( - Raw = 0x55 - - DagProtobuf = 0x70 - DagCBOR = 0x71 - Libp2pKey = 0x72 - - GitRaw = 0x78 - - DagJOSE = 0x85 + // common ones + 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 + + // other + GitRaw = 0x78 + DagJOSE = 0x85 // https://ipld.io/specs/codecs/dag-jose/spec/ EthBlock = 0x90 EthBlockList = 0x91 EthTxTrie = 0x92 @@ -81,64 +83,6 @@ const ( FilCommitmentSealed = 0xf102 ) -// Codecs maps the name of a codec to its type -var Codecs = map[string]uint64{ - "v0": DagProtobuf, - "raw": Raw, - "protobuf": DagProtobuf, - "cbor": DagCBOR, - "libp2p-key": Libp2pKey, - "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, -} - -// CodecToStr maps the numeric codec to its name -var CodecToStr = map[uint64]string{ - Raw: "raw", - DagProtobuf: "protobuf", - DagCBOR: "cbor", - 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", -} - // 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 1e40883..5503506 100644 --- a/cid_test.go +++ b/cid_test.go @@ -15,37 +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: "protobuf", - DagCBOR: "cbor", - Libp2pKey: "libp2p-key", - 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", -} - func assertEqual(t *testing.T, a, b Cid) { if a.Type() != b.Type() { t.Fatal("mismatch on type") @@ -60,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 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" }