Skip to content
This repository has been archived by the owner on Dec 7, 2019. It is now read-only.

Commit

Permalink
remove magic constant from peer ID calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
Stebalien committed Feb 8, 2018
1 parent 583ec80 commit 0b7a227
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,18 @@ import (
mh "github.com/multiformats/go-multihash"
)

// MaxInlineKeyLength is the maximum length a key can be for it to be inlined in
// the peer ID.
//
// * When `len(pubKey.Bytes()) <= MaxInlineKeyLength`, the peer ID is the
// identity multihash hash of the public key.
// * When `len(pubKey.Bytes()) > MaxInlineKeyLength`, the peer ID is the
// sha2-256 multihash of the public key.
const MaxInlineKeyLength = 40

var log = logging.Logger("peer")

// ID is a libp2p peer identity.
type ID string

// Pretty returns a b58-encoded string of the ID
Expand Down Expand Up @@ -135,7 +145,7 @@ func IDFromPublicKey(pk ic.PubKey) (ID, error) {
return "", err
}
var alg uint64 = mh.SHA2_256
if len(b) <= 40 {
if len(b) <= MaxInlineKeyLength {
alg = mh.ID
}
hash, _ := mh.Sum(b, alg, -1)
Expand Down

0 comments on commit 0b7a227

Please sign in to comment.