github.com/ethersphere/bee/v2@v2.2.0/pkg/api/p2p.go (about)

     1  // Copyright 2020 The Swarm Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package api
     6  
     7  import (
     8  	"encoding/hex"
     9  	"net/http"
    10  
    11  	"github.com/ethereum/go-ethereum/common"
    12  	"github.com/ethersphere/bee/v2/pkg/crypto"
    13  	"github.com/ethersphere/bee/v2/pkg/jsonhttp"
    14  	"github.com/ethersphere/bee/v2/pkg/swarm"
    15  	"github.com/multiformats/go-multiaddr"
    16  )
    17  
    18  type addressesResponse struct {
    19  	Overlay      *swarm.Address        `json:"overlay"`
    20  	Underlay     []multiaddr.Multiaddr `json:"underlay"`
    21  	Ethereum     common.Address        `json:"ethereum"`
    22  	PublicKey    string                `json:"publicKey"`
    23  	PSSPublicKey string                `json:"pssPublicKey"`
    24  }
    25  
    26  func (s *Service) addressesHandler(w http.ResponseWriter, _ *http.Request) {
    27  	logger := s.logger.WithValues("get_addresses").Build()
    28  
    29  	// initialize variable to json encode as [] instead null if p2p is nil
    30  	underlay := make([]multiaddr.Multiaddr, 0)
    31  	// addresses endpoint is exposed before p2p service is configured
    32  	// to provide information about other addresses.
    33  	if s.p2p != nil {
    34  		u, err := s.p2p.Addresses()
    35  		if err != nil {
    36  			logger.Debug("get address failed", "error", err)
    37  			jsonhttp.InternalServerError(w, err)
    38  			return
    39  		}
    40  		underlay = u
    41  	}
    42  	jsonhttp.OK(w, addressesResponse{
    43  		Overlay:      s.overlay,
    44  		Underlay:     underlay,
    45  		Ethereum:     s.ethereumAddress,
    46  		PublicKey:    hex.EncodeToString(crypto.EncodeSecp256k1PublicKey(&s.publicKey)),
    47  		PSSPublicKey: hex.EncodeToString(crypto.EncodeSecp256k1PublicKey(&s.pssPublicKey)),
    48  	})
    49  }