Skip to content

Commit a5df506

Browse files
lidelmvdan
andauthored
fix: switch to go-multicodec mappings (#240)
The mappings in go-cid were maintained by hand and are invalid. More details in ipfs/go-cid#137 This is switching to go-multicodec which has correct mappings that are generated, not written by hand. Co-authored-by: Daniel Martí <[email protected]>
1 parent 0b87f23 commit a5df506

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

core/peer/peer.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/ipfs/go-cid"
1010
ic "github.com/libp2p/go-libp2p-core/crypto"
1111
b58 "github.com/mr-tron/base58/base58"
12+
mc "github.com/multiformats/go-multicodec"
1213
mh "github.com/multiformats/go-multihash"
1314
)
1415

@@ -162,13 +163,9 @@ func Encode(id ID) string {
162163

163164
// FromCid converts a CID to a peer ID, if possible.
164165
func FromCid(c cid.Cid) (ID, error) {
165-
ty := c.Type()
166-
if ty != cid.Libp2pKey {
167-
s := cid.CodecToStr[ty]
168-
if s == "" {
169-
s = fmt.Sprintf("[unknown multicodec %d]", ty)
170-
}
171-
return "", fmt.Errorf("can't convert CID of type %s to a peer ID", s)
166+
code := mc.Code(c.Type())
167+
if code != mc.Libp2pKey {
168+
return "", fmt.Errorf("can't convert CID of type %q to a peer ID", code)
172169
}
173170
return ID(c.Hash()), nil
174171
}

0 commit comments

Comments
 (0)