From b46ec4234df267525ee3d82ff3097b46345e5b23 Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Wed, 23 Jan 2019 13:42:18 -0800 Subject: [PATCH] fix: re-enable peer ID inlining but make it configurable This should: 1. Unblock OpenBazaar. They were relying on the non-inlining behavior but run a forked network so they can set `EnableDeprecatedKeyInlining` to `false`. 2. Unblock Textile. They rely on this feature being *enabled*. Re-enabling it by default will give us some time to transition away from it. --- peer.go | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/peer.go b/peer.go index 5becf8f..f1237fd 100644 --- a/peer.go +++ b/peer.go @@ -14,10 +14,22 @@ import ( var ( // ErrEmptyPeerID is an error for empty peer ID. ErrEmptyPeerID = errors.New("empty peer ID") - // ErrNoPublickKey is an error for peer IDs that don't embed public keys + // ErrNoPublicKey is an error for peer IDs that don't embed public keys ErrNoPublicKey = errors.New("public key is not embedded in peer ID") ) +// EnableDeprecatedKeyInlining enables automatically inlining keys shorter than +// 42 bytes into the peer ID (using the "identity" multihash function). +// +// DEPRECATED: This feature will eventually be removed in favor of using the +// hash function specified by the key itself. +// +// This currently defaults to true for backwards compatibility but will be set +// to false by default when an upgrade path is determined. +var EnableDeprecatedKeyInlining = true + +const maxInlineKeyLength = 42 + // ID is a libp2p peer identity. type ID string @@ -141,7 +153,11 @@ func IDFromPublicKey(pk ic.PubKey) (ID, error) { if err != nil { return "", err } - hash, _ := mh.Sum(b, mh.SHA2_256, -1) + var alg uint64 = mh.SHA2_256 + if EnableDeprecatedKeyInlining && len(b) <= maxInlineKeyLength { + alg = mh.ID + } + hash, _ := mh.Sum(b, alg, -1) return ID(hash), nil }