gitee.com/lh-her-team/common@v1.5.1/helper/libp2ppeer/peer.go (about) 1 package libp2ppeer 2 3 import ( 4 "gitee.com/lh-her-team/common/helper/libp2pcrypto" 5 b58 "github.com/mr-tron/base58/base58" 6 mh "github.com/multiformats/go-multihash" 7 ) 8 9 var AdvancedEnableInlining = true 10 11 const maxInlineKeyLength = 42 12 13 type ID string 14 15 // Pretty returns a base58-encoded string representation of the ID. 16 func (id ID) Pretty() string { 17 return IDB58Encode(id) 18 } 19 20 // IDB58Encode returns the base58-encoded multihash representation of the ID. 21 // 22 // Deprecated: Use Encode. 23 func IDB58Encode(id ID) string { 24 return b58.Encode([]byte(id)) 25 } 26 27 // IDFromPublicKey returns the Peer ID corresponding to the public key pk. 28 // nolint: staticcheck 29 func IDFromPublicKey(pk libp2pcrypto.PubKey) (ID, error) { 30 b, err := pk.Bytes() 31 if err != nil { 32 return "", err 33 } 34 var alg uint64 = mh.SHA2_256 35 if AdvancedEnableInlining && len(b) <= maxInlineKeyLength { 36 alg = mh.ID 37 } 38 hash, _ := mh.Sum(b, alg, -1) 39 return ID(hash), nil 40 }