-
Notifications
You must be signed in to change notification settings - Fork 75
feat: support encoding/decoding peer IDs as CIDs in text #41
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -5,7 +5,9 @@ import ( | |||||
"encoding/hex" | ||||||
"errors" | ||||||
"fmt" | ||||||
"strings" | ||||||
|
||||||
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" | ||||||
|
@@ -129,23 +131,24 @@ func IDFromBytes(b []byte) (ID, error) { | |||||
return ID(b), nil | ||||||
} | ||||||
|
||||||
// IDB58Decode accepts a base58-encoded multihash representing a peer ID | ||||||
// and returns the decoded ID if the input is valid. | ||||||
// IDB58Decode decodes a peer ID. | ||||||
// | ||||||
// Deprecated: Use Decode. | ||||||
func IDB58Decode(s string) (ID, error) { | ||||||
m, err := mh.FromB58String(s) | ||||||
if err != nil { | ||||||
return "", err | ||||||
} | ||||||
return ID(m), err | ||||||
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 { | ||||||
|
@@ -155,10 +158,66 @@ func IDHexDecode(s string) (ID, error) { | |||||
} | ||||||
|
||||||
// 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. | ||||||
// | ||||||
// The encoded peer ID can either be a CID of a key or a raw multihash (identity | ||||||
// or sha256-256). | ||||||
func Decode(s string) (ID, error) { | ||||||
if strings.HasPrefix(s, "Qm") || strings.HasPrefix(s, "1") { | ||||||
// base58 encoded sha256 or identity multihash | ||||||
m, err := mh.FromB58String(s) | ||||||
if err != nil { | ||||||
return "", fmt.Errorf("failed to parse peer ID: %s", err) | ||||||
} | ||||||
return ID(m), nil | ||||||
} | ||||||
|
||||||
c, err := cid.Decode(s) | ||||||
if err != nil { | ||||||
return "", fmt.Errorf("failed to parse peer ID: %s", err) | ||||||
} | ||||||
return FromCid(c) | ||||||
} | ||||||
|
||||||
// Encode encodes a peer ID as a string. | ||||||
// | ||||||
// 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) | ||||||
} | ||||||
|
||||||
// FromCid converts a CID to a peer ID, if possible. | ||||||
func FromCid(c cid.Cid) (ID, error) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
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) | ||||||
} | ||||||
return ID(c.Hash()), nil | ||||||
} | ||||||
|
||||||
// ToCid encodes a peer ID as a CID of the public key. | ||||||
// | ||||||
// If the peer ID is invalid (e.g., empty), this will return the empty CID. | ||||||
func ToCid(id ID) cid.Cid { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: go style is to capitalise acronyms. We should create an alias
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @raulk I don't think we should introduce new convention here. If we choose to change them to use capitalization, that needs to happen across all repos (in separate, coordinated PRs), otherwise we just introduce noise by mixing conventions. |
||||||
m, err := mh.Cast([]byte(id)) | ||||||
if err != nil { | ||||||
return cid.Cid{} | ||||||
} | ||||||
return cid.NewCidV1(cid.Libp2pKey, m) | ||||||
} | ||||||
|
||||||
// IDFromPublicKey returns the Peer ID corresponding to the public key pk. | ||||||
func IDFromPublicKey(pk ic.PubKey) (ID, error) { | ||||||
b, err := pk.Bytes() | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should really remove these. But that's a breaking change and we can do that later.