remove deprecated functions in the peer package (#205)

This commit is contained in:
Marten Seemann 2021-07-22 21:17:36 +02:00 committed by GitHub
parent e952b3702c
commit 6cef973b92
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 13 additions and 47 deletions

View File

@ -87,7 +87,7 @@ func AddrInfoFromP2pAddr(m ma.Multiaddr) (*AddrInfo, error) {
// AddrInfoToP2pAddrs converts an AddrInfo to a list of Multiaddrs. // AddrInfoToP2pAddrs converts an AddrInfo to a list of Multiaddrs.
func AddrInfoToP2pAddrs(pi *AddrInfo) ([]ma.Multiaddr, error) { func AddrInfoToP2pAddrs(pi *AddrInfo) ([]ma.Multiaddr, error) {
var addrs []ma.Multiaddr var addrs []ma.Multiaddr
p2ppart, err := ma.NewComponent("p2p", IDB58Encode(pi.ID)) p2ppart, err := ma.NewComponent("p2p", Encode(pi.ID))
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -15,11 +15,11 @@ var (
func init() { func init() {
var err error var err error
testID, err = IDB58Decode("QmS3zcG7LhYZYSJMhyRZvTddvbNUqtt8BJpaSs6mi1K5Va") testID, err = Decode("QmS3zcG7LhYZYSJMhyRZvTddvbNUqtt8BJpaSs6mi1K5Va")
if err != nil { if err != nil {
panic(err) panic(err)
} }
maddrPeer = ma.StringCast("/p2p/" + IDB58Encode(testID)) maddrPeer = ma.StringCast("/p2p/" + Encode(testID))
maddrTpt = ma.StringCast("/ip4/127.0.0.1/tcp/1234") maddrTpt = ma.StringCast("/ip4/127.0.0.1/tcp/1234")
maddrFull = maddrTpt.Encapsulate(maddrPeer) maddrFull = maddrTpt.Encapsulate(maddrPeer)
} }

View File

@ -2,12 +2,11 @@
package peer package peer
import ( import (
"encoding/hex"
"errors" "errors"
"fmt" "fmt"
"strings" "strings"
cid "github.com/ipfs/go-cid" "github.com/ipfs/go-cid"
ic "github.com/libp2p/go-libp2p-core/crypto" ic "github.com/libp2p/go-libp2p-core/crypto"
b58 "github.com/mr-tron/base58/base58" b58 "github.com/mr-tron/base58/base58"
mh "github.com/multiformats/go-multihash" mh "github.com/multiformats/go-multihash"
@ -43,7 +42,7 @@ type ID string
// Pretty returns a base58-encoded string representation of the ID. // Pretty returns a base58-encoded string representation of the ID.
func (id ID) Pretty() string { func (id ID) Pretty() string {
return IDB58Encode(id) return Encode(id)
} }
// Loggable returns a pretty peer ID string in loggable JSON format. // Loggable returns a pretty peer ID string in loggable JSON format.
@ -131,39 +130,6 @@ func IDFromBytes(b []byte) (ID, error) {
return ID(b), nil return ID(b), nil
} }
// IDB58Decode decodes a peer ID.
//
// Deprecated: Use Decode.
func IDB58Decode(s string) (ID, error) {
return Decode(s)
}
// IDB58Encode returns the base58-encoded multihash representation of the ID.
//
// Deprecated: Use Encode.
func IDB58Encode(id ID) string {
return b58.Encode([]byte(id))
}
// IDHexDecode accepts a hex-encoded multihash representing a peer ID
// and returns the decoded ID if the input is valid.
//
// Deprecated: Don't raw-hex encode peer IDs, use base16 CIDs.
func IDHexDecode(s string) (ID, error) {
m, err := mh.FromHexString(s)
if err != nil {
return "", err
}
return ID(m), err
}
// IDHexEncode returns the hex-encoded multihash representation of the ID.
//
// Deprecated: Don't raw-hex encode peer IDs, use base16 CIDs.
func IDHexEncode(id ID) string {
return hex.EncodeToString([]byte(id))
}
// Decode accepts an encoded peer ID and returns the decoded ID if the input is // Decode accepts an encoded peer ID and returns the decoded ID if the input is
// valid. // valid.
// //
@ -191,7 +157,7 @@ func Decode(s string) (ID, error) {
// At the moment, it base58 encodes the peer ID but, in the future, it will // At the moment, it base58 encodes the peer ID but, in the future, it will
// switch to encoding it as a CID by default. // switch to encoding it as a CID by default.
func Encode(id ID) string { func Encode(id ID) string {
return IDB58Encode(id) return b58.Encode([]byte(id))
} }
// FromCid converts a CID to a peer ID, if possible. // FromCid converts a CID to a peer ID, if possible.

View File

@ -47,7 +47,7 @@ func (id ID) Size() int {
} }
func (id ID) MarshalJSON() ([]byte, error) { func (id ID) MarshalJSON() ([]byte, error) {
return json.Marshal(IDB58Encode(id)) return json.Marshal(Encode(id))
} }
func (id *ID) UnmarshalJSON(data []byte) (err error) { func (id *ID) UnmarshalJSON(data []byte) (err error) {
@ -55,18 +55,18 @@ func (id *ID) UnmarshalJSON(data []byte) (err error) {
if err = json.Unmarshal(data, &v); err != nil { if err = json.Unmarshal(data, &v); err != nil {
return err return err
} }
*id, err = IDB58Decode(v) *id, err = Decode(v)
return err return err
} }
// MarshalText returns the text encoding of the ID. // MarshalText returns the text encoding of the ID.
func (id ID) MarshalText() ([]byte, error) { func (id ID) MarshalText() ([]byte, error) {
return []byte(IDB58Encode(id)), nil return []byte(Encode(id)), nil
} }
// UnmarshalText restores the ID from its text encoding. // UnmarshalText restores the ID from its text encoding.
func (id *ID) UnmarshalText(data []byte) error { func (id *ID) UnmarshalText(data []byte) error {
pid, err := IDB58Decode(string(data)) pid, err := Decode(string(data))
if err != nil { if err != nil {
return err return err
} }

View File

@ -90,7 +90,7 @@ func (ks *keyset) load(hpkp, skBytesStr string) error {
func TestIDMatchesPublicKey(t *testing.T) { func TestIDMatchesPublicKey(t *testing.T) {
test := func(ks keyset) { test := func(ks keyset) {
p1, err := IDB58Decode(ks.hpkp) p1, err := Decode(ks.hpkp)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -125,7 +125,7 @@ func TestIDMatchesPublicKey(t *testing.T) {
func TestIDMatchesPrivateKey(t *testing.T) { func TestIDMatchesPrivateKey(t *testing.T) {
test := func(ks keyset) { test := func(ks keyset) {
p1, err := IDB58Decode(ks.hpkp) p1, err := Decode(ks.hpkp)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -155,7 +155,7 @@ func TestIDMatchesPrivateKey(t *testing.T) {
func TestIDEncoding(t *testing.T) { func TestIDEncoding(t *testing.T) {
test := func(ks keyset) { test := func(ks keyset) {
p1, err := IDB58Decode(ks.hpkp) p1, err := Decode(ks.hpkp)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }