From cc764070dea17d04311487964f869e14dadd0482 Mon Sep 17 00:00:00 2001 From: Justin Date: Wed, 21 Jun 2017 14:43:21 +0100 Subject: [PATCH] Do not publish public keys extractable from ID Initial implementation for https://github.com/ipfs/go-ipfs/issues/3896 The function `peer.ExtractPublicKey()` is part of PR https://github.com/libp2p/go-libp2p-peer/pull/14 cc @whyrusleeping --- namesys/publisher.go | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/namesys/publisher.go b/namesys/publisher.go index cba463492d5..6cd3cee8810 100644 --- a/namesys/publisher.go +++ b/namesys/publisher.go @@ -150,21 +150,36 @@ func PutRecordToRouting(ctx context.Context, k ci.PrivKey, value path.Path, seqn entry.Ttl = proto.Uint64(uint64(ttl.Nanoseconds())) } - errs := make(chan error, 2) + var errorChanSize int + var errorChan chan error - go func() { - errs <- PublishEntry(ctx, r, ipnskey, entry) - }() + // Attempt to extract the public key from the ID + var extractedPublicKey = peer.ExtractPublicKey() + + if extractedPublicKey == nil { + errorChanSize = 2 // IPNS and public key + } else { + errorChanSize = 1 // IPNS only + } + + errorChan = make(chan error, errorChanSize) go func() { - errs <- PublishPublicKey(ctx, r, namekey, k.GetPublic()) + errorChan <- PublishEntry(ctx, r, ipnskey, entry) }() - if err := waitOnErrChan(ctx, errs); err != nil { - return err + // Publish the public key if a public key cannot be extracted from the ID + if extractedPublicKey == nil { + go func() { + errorChan <- PublishPublicKey(ctx, r, namekey, k.GetPublic()) + }() + + if err := waitOnErrChan(ctx, errorChan); err != nil { + return err + } } - return waitOnErrChan(ctx, errs) + return waitOnErrChan(ctx, errorChan) } func waitOnErrChan(ctx context.Context, errs chan error) error {