Skip to content
This repository was archived by the owner on Mar 28, 2023. It is now read-only.

Commit dd38112

Browse files
committed
[#1626] Fix secio handshake to properly fallback to alt ID generation
OpenBazaar currently uses hashed ID but intends to later switch to the default inline IDs. During this transition, this commit ensures that nodes of one type should always interop with the other.
1 parent 910261e commit dd38112

File tree

3 files changed

+33
-19
lines changed
  • ipfs
  • vendor/gx/ipfs
    • QmSVaJe1aRjc78cZARTtf4pqvXERYwihyYhZWoVWceHnsK/go-libp2p-secio
    • QmYVXrKrKHDC9FobgmcmshCDyWwdrfwfanNQN4oxJ9Fk3h/go-libp2p-peer

3 files changed

+33
-19
lines changed

ipfs/identity.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ func init() {
1717
}
1818

1919
func IdentityFromKey(privkey []byte) (config.Identity, error) {
20-
2120
ident := config.Identity{}
2221
sk, err := crypto.UnmarshalPrivateKey(privkey)
2322
if err != nil {

vendor/gx/ipfs/QmSVaJe1aRjc78cZARTtf4pqvXERYwihyYhZWoVWceHnsK/go-libp2p-secio/protocol.go

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -205,26 +205,18 @@ func (s *secureSession) runHandshakeSync() error {
205205
// No peer set. We're accepting a remote connection.
206206
s.remotePeer = actualRemotePeer
207207
default:
208-
// OpenBazaar: check that this peerID isn't the old style
209-
// If it is then we're good. If not then we error.
210-
// This code will be removed after enough OpenBazaar nodes
211-
// upgrade to inline pubkeys.
212-
pubkeyBytes, err := s.remote.permanentPubKey.Bytes()
208+
// OpenBazaar: we are transitioning from hashed IDs to inline IDs, as a last
209+
// resort, we should check that the peer isn't using the other variety. Both ID methods
210+
// change their behavior based on peer.AdvancedEnableInlining but are written to
211+
// operate opposite of each other. This will allow old nodes to successfully connect to
212+
// new nodes, and vice versa.
213+
altID, err := peer.AlternativeIDFromPublicKey(s.remote.permanentPubKey)
213214
if err != nil {
214215
return err
215216
}
216-
oldMultihash, err := mh.Sum(pubkeyBytes, mh.SHA2_256, 32)
217-
if err != nil {
218-
return err
219-
}
220-
oldStylePeer, err := peer.IDB58Decode(oldMultihash.B58String())
221-
if err != nil {
222-
return err
223-
}
224-
if s.remotePeer != oldStylePeer {
225-
// Peer mismatch. Bail.
217+
if s.remotePeer != altID {
226218
s.insecure.Close()
227-
log.Debugf("expected peer %s, got peer %s", s.remotePeer, actualRemotePeer)
219+
log.Debugf("expected peer %s, but ID produced from pubkey doesn't match", s.remotePeer)
228220
return ErrWrongPeer
229221
}
230222
}

vendor/gx/ipfs/QmYVXrKrKHDC9FobgmcmshCDyWwdrfwfanNQN4oxJ9Fk3h/go-libp2p-peer/peer.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,21 @@ func IDFromPublicKey(pk ic.PubKey) (ID, error) {
169169
return ID(hash), nil
170170
}
171171

172-
// OpenBazaar: temporary helper function to remain forward compatible with
173-
// inline keys
172+
// HashedIDFromPublicKey will always return the SHA256 hash of
173+
// the pubkey bytes. OpenBazaar: temporary helper to isolate the
174+
// hash-producing ID behavior.
175+
func HashedIDFromPublicKey(pk ic.PubKey) (ID, error) {
176+
b, err := pk.Bytes()
177+
if err != nil {
178+
return "", err
179+
}
180+
hash, _ := mh.Sum(b, mh.SHA2_256, -1)
181+
return ID(hash), nil
182+
}
183+
184+
// InlineIDFromPublicKey will always return the new inline ID format
185+
// of the pubkey bytes. OpenBazaar: temporary helper function to
186+
// remain forward compatible with inline keys
174187
func InlineIDFromPublicKey(pk ic.PubKey) (ID, error) {
175188
b, err := pk.Bytes()
176189
if err != nil {
@@ -180,6 +193,16 @@ func InlineIDFromPublicKey(pk ic.PubKey) (ID, error) {
180193
return ID(hash), nil
181194
}
182195

196+
// AlternativeIDFromPublicKey returns SHA256 hash ID when AdvancedEnableInlining
197+
// is true, and returns new InlineID otherwise. This allows legacy IDs to be compared
198+
// after they are no longer available by the default IDFromPublicKey function.
199+
func AlternativeIDFromPublicKey(pubkey ic.PubKey) (ID, error) {
200+
if AdvancedEnableInlining {
201+
return HashedIDFromPublicKey(pubkey)
202+
}
203+
return InlineIDFromPublicKey(pubkey)
204+
}
205+
183206
// IDFromPrivateKey returns the Peer ID corresponding to sk
184207
func IDFromPrivateKey(sk ic.PrivKey) (ID, error) {
185208
return IDFromPublicKey(sk.GetPublic())

0 commit comments

Comments
 (0)