Skip to content
This repository was archived by the owner on Sep 6, 2022. It is now read-only.

Commit d9f3ebc

Browse files
committed
fix: switch to go-multicodec mappings
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.
1 parent 4a03a82 commit d9f3ebc

File tree

3 files changed

+7
-7
lines changed

3 files changed

+7
-7
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ require (
1414
github.com/minio/sha256-simd v0.1.1
1515
github.com/mr-tron/base58 v1.2.0
1616
github.com/multiformats/go-multiaddr v0.4.1
17+
github.com/multiformats/go-multicodec v0.4.1
1718
github.com/multiformats/go-multihash v0.0.14
1819
github.com/multiformats/go-varint v0.0.6
1920
github.com/stretchr/testify v1.7.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ github.com/multiformats/go-multiaddr v0.4.1 h1:Pq37uLx3hsyNlTDir7FZyU8+cFCTqd5y1
5050
github.com/multiformats/go-multiaddr v0.4.1/go.mod h1:3afI9HfVW8csiF8UZqtpYRiDyew8pRX7qLIGHu9FLuM=
5151
github.com/multiformats/go-multibase v0.0.3 h1:l/B6bJDQjvQ5G52jw4QGSYeOTZoAwIO77RblWplfIqk=
5252
github.com/multiformats/go-multibase v0.0.3/go.mod h1:5+1R4eQrT3PkYZ24C3W2Ue2tPwIdYQD509ZjSb5y9Oc=
53+
github.com/multiformats/go-multicodec v0.4.1 h1:BSJbf+zpghcZMZrwTYBGwy0CPcVZGWiC72Cp8bBd4R4=
54+
github.com/multiformats/go-multicodec v0.4.1/go.mod h1:1Hj/eHRaVWSXiSNNfcEPcwZleTmdNP81xlxDLnWU9GQ=
5355
github.com/multiformats/go-multihash v0.0.13/go.mod h1:VdAWLKTwram9oKAatUcLxBNUjdtcVwxObEQBtRfuyjc=
5456
github.com/multiformats/go-multihash v0.0.14 h1:QoBceQYQQtNUuf6s7wHxnE2c8bhbMqhfGzNI032se/I=
5557
github.com/multiformats/go-multihash v0.0.14/go.mod h1:VdAWLKTwram9oKAatUcLxBNUjdtcVwxObEQBtRfuyjc=

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 %s to a peer ID", code.String())
172169
}
173170
return ID(c.Hash()), nil
174171
}

0 commit comments

Comments
 (0)