github.com/zhiqiangxu/go-ethereum@v1.9.16-0.20210824055606-be91cfdebc48/p2p/discover/node.go (about)

     1  // Copyright 2015 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package discover
    18  
    19  import (
    20  	"crypto/ecdsa"
    21  	"crypto/elliptic"
    22  	"errors"
    23  	"math/big"
    24  	"net"
    25  	"time"
    26  
    27  	"github.com/zhiqiangxu/go-ethereum/common/math"
    28  	"github.com/zhiqiangxu/go-ethereum/crypto"
    29  	"github.com/zhiqiangxu/go-ethereum/p2p/enode"
    30  )
    31  
    32  // node represents a host on the network.
    33  // The fields of Node may not be modified.
    34  type node struct {
    35  	enode.Node
    36  	addedAt        time.Time // time when the node was added to the table
    37  	livenessChecks uint      // how often liveness was checked
    38  }
    39  
    40  type encPubkey [64]byte
    41  
    42  func encodePubkey(key *ecdsa.PublicKey) encPubkey {
    43  	var e encPubkey
    44  	math.ReadBits(key.X, e[:len(e)/2])
    45  	math.ReadBits(key.Y, e[len(e)/2:])
    46  	return e
    47  }
    48  
    49  func decodePubkey(curve elliptic.Curve, e encPubkey) (*ecdsa.PublicKey, error) {
    50  	p := &ecdsa.PublicKey{Curve: curve, X: new(big.Int), Y: new(big.Int)}
    51  	half := len(e) / 2
    52  	p.X.SetBytes(e[:half])
    53  	p.Y.SetBytes(e[half:])
    54  	if !p.Curve.IsOnCurve(p.X, p.Y) {
    55  		return nil, errors.New("invalid curve point")
    56  	}
    57  	return p, nil
    58  }
    59  
    60  func (e encPubkey) id() enode.ID {
    61  	return enode.ID(crypto.Keccak256Hash(e[:]))
    62  }
    63  
    64  func wrapNode(n *enode.Node) *node {
    65  	return &node{Node: *n}
    66  }
    67  
    68  func wrapNodes(ns []*enode.Node) []*node {
    69  	result := make([]*node, len(ns))
    70  	for i, n := range ns {
    71  		result[i] = wrapNode(n)
    72  	}
    73  	return result
    74  }
    75  
    76  func unwrapNode(n *node) *enode.Node {
    77  	return &n.Node
    78  }
    79  
    80  func unwrapNodes(ns []*node) []*enode.Node {
    81  	result := make([]*enode.Node, len(ns))
    82  	for i, n := range ns {
    83  		result[i] = unwrapNode(n)
    84  	}
    85  	return result
    86  }
    87  
    88  func (n *node) addr() *net.UDPAddr {
    89  	return &net.UDPAddr{IP: n.IP(), Port: n.UDP()}
    90  }
    91  
    92  func (n *node) String() string {
    93  	return n.Node.String()
    94  }