From a5df506bd32e61daed16f9fc5bc134347958fa39 Mon Sep 17 00:00:00 2001 From: Marcin Rataj Date: Thu, 31 Mar 2022 16:04:14 +0200 Subject: [PATCH] fix: switch to go-multicodec mappings (#240) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The mappings in go-cid were maintained by hand and are invalid. More details in https://github.com/ipfs/go-cid/pull/137 This is switching to go-multicodec which has correct mappings that are generated, not written by hand. Co-authored-by: Daniel Martí --- core/peer/peer.go | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/core/peer/peer.go b/core/peer/peer.go index 2ab22fef1f..7d4bdfff89 100644 --- a/core/peer/peer.go +++ b/core/peer/peer.go @@ -9,6 +9,7 @@ import ( "github.com/ipfs/go-cid" ic "github.com/libp2p/go-libp2p-core/crypto" b58 "github.com/mr-tron/base58/base58" + mc "github.com/multiformats/go-multicodec" mh "github.com/multiformats/go-multihash" ) @@ -162,13 +163,9 @@ func Encode(id ID) string { // FromCid converts a CID to a peer ID, if possible. func FromCid(c cid.Cid) (ID, error) { - ty := c.Type() - if ty != cid.Libp2pKey { - s := cid.CodecToStr[ty] - if s == "" { - s = fmt.Sprintf("[unknown multicodec %d]", ty) - } - return "", fmt.Errorf("can't convert CID of type %s to a peer ID", s) + code := mc.Code(c.Type()) + if code != mc.Libp2pKey { + return "", fmt.Errorf("can't convert CID of type %q to a peer ID", code) } return ID(c.Hash()), nil }