p2p.go 1.5 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2020 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package debugapi

import (
8
	"encoding/hex"
9 10
	"net/http"

11
	"github.com/ethereum/go-ethereum/common"
12
	"github.com/ethersphere/bee/pkg/crypto"
13
	"github.com/ethersphere/bee/pkg/jsonhttp"
14
	"github.com/ethersphere/bee/pkg/swarm"
15 16 17 18
	"github.com/multiformats/go-multiaddr"
)

type addressesResponse struct {
19
	Overlay      *swarm.Address        `json:"overlay"`
20 21
	Underlay     []multiaddr.Multiaddr `json:"underlay"`
	Ethereum     common.Address        `json:"ethereum"`
22 23
	PublicKey    string                `json:"publicKey"`
	PSSPublicKey string                `json:"pssPublicKey"`
24 25
}

26
func (s *Service) addressesHandler(w http.ResponseWriter, r *http.Request) {
27 28 29 30
	// initialize variable to json encode as [] instead null if p2p is nil
	underlay := make([]multiaddr.Multiaddr, 0)
	// addresses endpoint is exposed before p2p service is configured
	// to provide information about other addresses.
31 32
	if s.p2p != nil {
		u, err := s.p2p.Addresses()
33
		if err != nil {
34
			s.logger.Debugf("debug api: p2p addresses: %v", err)
35 36 37 38
			jsonhttp.InternalServerError(w, err)
			return
		}
		underlay = u
39 40
	}
	jsonhttp.OK(w, addressesResponse{
41
		Overlay:      s.overlay,
42
		Underlay:     underlay,
43 44 45
		Ethereum:     s.ethereumAddress,
		PublicKey:    hex.EncodeToString(crypto.EncodeSecp256k1PublicKey(&s.publicKey)),
		PSSPublicKey: hex.EncodeToString(crypto.EncodeSecp256k1PublicKey(&s.pssPublicKey)),
46 47
	})
}