2016-06-14 16:17:44 +00:00
|
|
|
// Copyright 2015 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package discover
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
2021-06-28 06:53:50 +00:00
|
|
|
"crypto/elliptic"
|
2016-06-14 16:17:44 +00:00
|
|
|
"errors"
|
|
|
|
"math/big"
|
|
|
|
"net"
|
2018-02-27 10:39:30 +00:00
|
|
|
"time"
|
2016-06-14 16:17:44 +00:00
|
|
|
|
2018-11-14 07:03:58 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common/math"
|
2016-06-14 16:17:44 +00:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2018-11-14 07:03:58 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
2016-06-14 16:17:44 +00:00
|
|
|
)
|
|
|
|
|
2018-11-14 07:03:58 +00:00
|
|
|
// node represents a host on the network.
|
2016-06-14 16:17:44 +00:00
|
|
|
// The fields of Node may not be modified.
|
2018-11-14 07:03:58 +00:00
|
|
|
type node struct {
|
|
|
|
enode.Node
|
2019-03-14 06:13:05 +00:00
|
|
|
addedAt time.Time // time when the node was added to the table
|
|
|
|
livenessChecks uint // how often liveness was checked
|
2016-06-14 16:17:44 +00:00
|
|
|
}
|
|
|
|
|
2018-11-14 07:03:58 +00:00
|
|
|
type encPubkey [64]byte
|
2016-06-14 16:17:44 +00:00
|
|
|
|
2018-11-14 07:03:58 +00:00
|
|
|
func encodePubkey(key *ecdsa.PublicKey) encPubkey {
|
|
|
|
var e encPubkey
|
|
|
|
math.ReadBits(key.X, e[:len(e)/2])
|
|
|
|
math.ReadBits(key.Y, e[len(e)/2:])
|
|
|
|
return e
|
2017-05-01 11:09:48 +00:00
|
|
|
}
|
|
|
|
|
2021-06-28 06:53:50 +00:00
|
|
|
func decodePubkey(curve elliptic.Curve, e []byte) (*ecdsa.PublicKey, error) {
|
|
|
|
if len(e) != len(encPubkey{}) {
|
|
|
|
return nil, errors.New("wrong size public key data")
|
|
|
|
}
|
|
|
|
p := &ecdsa.PublicKey{Curve: curve, X: new(big.Int), Y: new(big.Int)}
|
2018-11-14 07:03:58 +00:00
|
|
|
half := len(e) / 2
|
|
|
|
p.X.SetBytes(e[:half])
|
|
|
|
p.Y.SetBytes(e[half:])
|
|
|
|
if !p.Curve.IsOnCurve(p.X, p.Y) {
|
2021-06-28 06:53:50 +00:00
|
|
|
return nil, errors.New("invalid curve point")
|
2017-10-17 09:58:04 +00:00
|
|
|
}
|
2018-11-14 07:03:58 +00:00
|
|
|
return p, nil
|
2017-10-17 09:58:04 +00:00
|
|
|
}
|
|
|
|
|
2018-11-14 07:03:58 +00:00
|
|
|
func (e encPubkey) id() enode.ID {
|
|
|
|
return enode.ID(crypto.Keccak256Hash(e[:]))
|
2016-06-14 16:17:44 +00:00
|
|
|
}
|
|
|
|
|
2018-11-14 07:03:58 +00:00
|
|
|
func wrapNode(n *enode.Node) *node {
|
|
|
|
return &node{Node: *n}
|
2016-06-14 16:17:44 +00:00
|
|
|
}
|
|
|
|
|
2018-11-14 07:03:58 +00:00
|
|
|
func wrapNodes(ns []*enode.Node) []*node {
|
|
|
|
result := make([]*node, len(ns))
|
|
|
|
for i, n := range ns {
|
|
|
|
result[i] = wrapNode(n)
|
2016-06-14 16:17:44 +00:00
|
|
|
}
|
2018-11-14 07:03:58 +00:00
|
|
|
return result
|
2016-06-14 16:17:44 +00:00
|
|
|
}
|
|
|
|
|
2018-11-14 07:03:58 +00:00
|
|
|
func unwrapNode(n *node) *enode.Node {
|
|
|
|
return &n.Node
|
2016-06-14 16:17:44 +00:00
|
|
|
}
|
|
|
|
|
2018-11-14 07:03:58 +00:00
|
|
|
func unwrapNodes(ns []*node) []*enode.Node {
|
|
|
|
result := make([]*enode.Node, len(ns))
|
|
|
|
for i, n := range ns {
|
|
|
|
result[i] = unwrapNode(n)
|
2016-06-14 16:17:44 +00:00
|
|
|
}
|
2018-11-14 07:03:58 +00:00
|
|
|
return result
|
2016-06-14 16:17:44 +00:00
|
|
|
}
|
|
|
|
|
2018-11-14 07:03:58 +00:00
|
|
|
func (n *node) addr() *net.UDPAddr {
|
|
|
|
return &net.UDPAddr{IP: n.IP(), Port: n.UDP()}
|
2016-06-14 16:17:44 +00:00
|
|
|
}
|
|
|
|
|
2018-11-14 07:03:58 +00:00
|
|
|
func (n *node) String() string {
|
|
|
|
return n.Node.String()
|
2016-06-14 16:17:44 +00:00
|
|
|
}
|