2018-01-29 19:44:18 +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"
|
|
|
|
"errors"
|
|
|
|
"math/big"
|
|
|
|
"net"
|
2018-03-07 21:29:21 +00:00
|
|
|
"time"
|
2018-01-29 19:44:18 +00:00
|
|
|
|
2018-11-21 15:30:00 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common/math"
|
2018-01-29 19:44:18 +00:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto/secp256k1"
|
2018-11-21 15:30:00 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
2018-01-29 19:44:18 +00:00
|
|
|
)
|
|
|
|
|
2018-11-21 15:30:00 +00:00
|
|
|
// node represents a host on the network.
|
2018-01-29 19:44:18 +00:00
|
|
|
// The fields of Node may not be modified.
|
2018-11-21 15:30:00 +00:00
|
|
|
type node struct {
|
|
|
|
enode.Node
|
2019-03-03 09:37:27 +00:00
|
|
|
addedAt time.Time // time when the node was added to the table
|
|
|
|
livenessChecks uint // how often liveness was checked
|
2018-01-29 19:44:18 +00:00
|
|
|
}
|
|
|
|
|
2018-11-21 15:30:00 +00:00
|
|
|
type encPubkey [64]byte
|
2018-01-29 19:44:18 +00:00
|
|
|
|
2018-11-21 15:30:00 +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
|
2018-01-29 19:44:18 +00:00
|
|
|
}
|
|
|
|
|
2018-11-21 15:30:00 +00:00
|
|
|
func decodePubkey(e encPubkey) (*ecdsa.PublicKey, error) {
|
|
|
|
p := &ecdsa.PublicKey{Curve: crypto.S256(), X: new(big.Int), Y: new(big.Int)}
|
|
|
|
half := len(e) / 2
|
|
|
|
p.X.SetBytes(e[:half])
|
|
|
|
p.Y.SetBytes(e[half:])
|
|
|
|
if !p.Curve.IsOnCurve(p.X, p.Y) {
|
|
|
|
return nil, errors.New("invalid secp256k1 curve point")
|
2018-01-29 19:44:18 +00:00
|
|
|
}
|
2018-11-21 15:30:00 +00:00
|
|
|
return p, nil
|
2018-01-29 19:44:18 +00:00
|
|
|
}
|
|
|
|
|
2018-11-21 15:30:00 +00:00
|
|
|
func (e encPubkey) id() enode.ID {
|
|
|
|
return enode.ID(crypto.Keccak256Hash(e[:]))
|
2018-01-29 19:44:18 +00:00
|
|
|
}
|
|
|
|
|
2018-11-21 15:30:00 +00:00
|
|
|
// recoverNodeKey computes the public key used to sign the
|
|
|
|
// given hash from the signature.
|
|
|
|
func recoverNodeKey(hash, sig []byte) (key encPubkey, err error) {
|
|
|
|
pubkey, err := secp256k1.RecoverPubkey(hash, sig)
|
2018-01-29 19:44:18 +00:00
|
|
|
if err != nil {
|
2018-11-21 15:30:00 +00:00
|
|
|
return key, err
|
2018-01-29 19:44:18 +00:00
|
|
|
}
|
2018-11-21 15:30:00 +00:00
|
|
|
copy(key[:], pubkey[1:])
|
|
|
|
return key, nil
|
2018-01-29 19:44:18 +00:00
|
|
|
}
|
|
|
|
|
2018-11-21 15:30:00 +00:00
|
|
|
func wrapNode(n *enode.Node) *node {
|
|
|
|
return &node{Node: *n}
|
2018-01-29 19:44:18 +00:00
|
|
|
}
|
|
|
|
|
2018-11-21 15:30:00 +00:00
|
|
|
func wrapNodes(ns []*enode.Node) []*node {
|
|
|
|
result := make([]*node, len(ns))
|
|
|
|
for i, n := range ns {
|
|
|
|
result[i] = wrapNode(n)
|
2018-01-29 19:44:18 +00:00
|
|
|
}
|
2018-11-21 15:30:00 +00:00
|
|
|
return result
|
2018-01-29 19:44:18 +00:00
|
|
|
}
|
|
|
|
|
2018-11-21 15:30:00 +00:00
|
|
|
func unwrapNode(n *node) *enode.Node {
|
|
|
|
return &n.Node
|
2018-01-29 19:44:18 +00:00
|
|
|
}
|
|
|
|
|
2018-11-21 15:30:00 +00:00
|
|
|
func unwrapNodes(ns []*node) []*enode.Node {
|
|
|
|
result := make([]*enode.Node, len(ns))
|
|
|
|
for i, n := range ns {
|
|
|
|
result[i] = unwrapNode(n)
|
2018-01-29 19:44:18 +00:00
|
|
|
}
|
2018-11-21 15:30:00 +00:00
|
|
|
return result
|
2018-01-29 19:44:18 +00:00
|
|
|
}
|
|
|
|
|
2018-11-21 15:30:00 +00:00
|
|
|
func (n *node) addr() *net.UDPAddr {
|
|
|
|
return &net.UDPAddr{IP: n.IP(), Port: n.UDP()}
|
2018-01-29 19:44:18 +00:00
|
|
|
}
|
|
|
|
|
2018-11-21 15:30:00 +00:00
|
|
|
func (n *node) String() string {
|
|
|
|
return n.Node.String()
|
2018-01-29 19:44:18 +00:00
|
|
|
}
|