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

Commit

Permalink
remove deprecated functions in the peer package (#205)
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann committed Jul 22, 2021
1 parent e952b37 commit 6cef973
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 47 deletions.
2 changes: 1 addition & 1 deletion peer/addrinfo.go
Expand Up @@ -87,7 +87,7 @@ func AddrInfoFromP2pAddr(m ma.Multiaddr) (*AddrInfo, error) {
// AddrInfoToP2pAddrs converts an AddrInfo to a list of Multiaddrs.
func AddrInfoToP2pAddrs(pi *AddrInfo) ([]ma.Multiaddr, error) {
var addrs []ma.Multiaddr
p2ppart, err := ma.NewComponent("p2p", IDB58Encode(pi.ID))
p2ppart, err := ma.NewComponent("p2p", Encode(pi.ID))
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions peer/addrinfo_test.go
Expand Up @@ -15,11 +15,11 @@ var (

func init() {
var err error
testID, err = IDB58Decode("QmS3zcG7LhYZYSJMhyRZvTddvbNUqtt8BJpaSs6mi1K5Va")
testID, err = Decode("QmS3zcG7LhYZYSJMhyRZvTddvbNUqtt8BJpaSs6mi1K5Va")
if err != nil {
panic(err)
}
maddrPeer = ma.StringCast("/p2p/" + IDB58Encode(testID))
maddrPeer = ma.StringCast("/p2p/" + Encode(testID))
maddrTpt = ma.StringCast("/ip4/127.0.0.1/tcp/1234")
maddrFull = maddrTpt.Encapsulate(maddrPeer)
}
Expand Down
40 changes: 3 additions & 37 deletions peer/peer.go
Expand Up @@ -2,12 +2,11 @@
package peer

import (
"encoding/hex"
"errors"
"fmt"
"strings"

cid "github.com/ipfs/go-cid"
"github.com/ipfs/go-cid"
ic "github.com/libp2p/go-libp2p-core/crypto"
b58 "github.com/mr-tron/base58/base58"
mh "github.com/multiformats/go-multihash"
Expand Down Expand Up @@ -43,7 +42,7 @@ type ID string

// Pretty returns a base58-encoded string representation of the ID.
func (id ID) Pretty() string {
return IDB58Encode(id)
return Encode(id)
}

// Loggable returns a pretty peer ID string in loggable JSON format.
Expand Down Expand Up @@ -131,39 +130,6 @@ func IDFromBytes(b []byte) (ID, error) {
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
// valid.
//
Expand Down Expand Up @@ -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
// switch to encoding it as a CID by default.
func Encode(id ID) string {
return IDB58Encode(id)
return b58.Encode([]byte(id))
}

// FromCid converts a CID to a peer ID, if possible.
Expand Down
8 changes: 4 additions & 4 deletions peer/peer_serde.go
Expand Up @@ -47,26 +47,26 @@ func (id ID) Size() int {
}

func (id ID) MarshalJSON() ([]byte, error) {
return json.Marshal(IDB58Encode(id))
return json.Marshal(Encode(id))
}

func (id *ID) UnmarshalJSON(data []byte) (err error) {
var v string
if err = json.Unmarshal(data, &v); err != nil {
return err
}
*id, err = IDB58Decode(v)
*id, err = Decode(v)
return err
}

// MarshalText returns the text encoding of the ID.
func (id ID) MarshalText() ([]byte, error) {
return []byte(IDB58Encode(id)), nil
return []byte(Encode(id)), nil
}

// UnmarshalText restores the ID from its text encoding.
func (id *ID) UnmarshalText(data []byte) error {
pid, err := IDB58Decode(string(data))
pid, err := Decode(string(data))
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions peer/peer_test.go
Expand Up @@ -90,7 +90,7 @@ func (ks *keyset) load(hpkp, skBytesStr string) error {
func TestIDMatchesPublicKey(t *testing.T) {

test := func(ks keyset) {
p1, err := IDB58Decode(ks.hpkp)
p1, err := Decode(ks.hpkp)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -125,7 +125,7 @@ func TestIDMatchesPublicKey(t *testing.T) {
func TestIDMatchesPrivateKey(t *testing.T) {

test := func(ks keyset) {
p1, err := IDB58Decode(ks.hpkp)
p1, err := Decode(ks.hpkp)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -155,7 +155,7 @@ func TestIDMatchesPrivateKey(t *testing.T) {

func TestIDEncoding(t *testing.T) {
test := func(ks keyset) {
p1, err := IDB58Decode(ks.hpkp)
p1, err := Decode(ks.hpkp)
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit 6cef973

Please sign in to comment.