github.com/aitimate-0/go-ethereum@v1.9.7/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  	"errors"
    22  	"math/big"
    23  	"net"
    24  	"time"
    25  
    26  	"github.com/ethereum/go-ethereum/common/math"
    27  	"github.com/ethereum/go-ethereum/crypto"
    28  	"github.com/ethereum/go-ethereum/p2p/enode"
    29  )
    30  
    31  // node represents a host on the network.
    32  // The fields of Node may not be modified.
    33  type node struct {
    34  	enode.Node
    35  	addedAt        time.Time // time when the node was added to the table
    36  	livenessChecks uint      // how often liveness was checked
    37  }
    38  
    39  type encPubkey [64]byte
    40  
    41  func encodePubkey(key *ecdsa.PublicKey) encPubkey {
    42  	var e encPubkey
    43  	math.ReadBits(key.X, e[:len(e)/2])
    44  	math.ReadBits(key.Y, e[len(e)/2:])
    45  	return e
    46  }
    47  
    48  func decodePubkey(e encPubkey) (*ecdsa.PublicKey, error) {
    49  	p := &ecdsa.PublicKey{Curve: crypto.S256(), X: new(big.Int), Y: new(big.Int)}
    50  	half := len(e) / 2
    51  	p.X.SetBytes(e[:half])
    52  	p.Y.SetBytes(e[half:])
    53  	if !p.Curve.IsOnCurve(p.X, p.Y) {
    54  		return nil, errors.New("invalid secp256k1 curve point")
    55  	}
    56  	return p, nil
    57  }
    58  
    59  func (e encPubkey) id() enode.ID {
    60  	return enode.ID(crypto.Keccak256Hash(e[:]))
    61  }
    62  
    63  // recoverNodeKey computes the public key used to sign the
    64  // given hash from the signature.
    65  func recoverNodeKey(hash, sig []byte) (key encPubkey, err error) {
    66  	pubkey, err := crypto.Ecrecover(hash, sig)
    67  	if err != nil {
    68  		return key, err
    69  	}
    70  	copy(key[:], pubkey[1:])
    71  	return key, nil
    72  }
    73  
    74  func wrapNode(n *enode.Node) *node {
    75  	return &node{Node: *n}
    76  }
    77  
    78  func wrapNodes(ns []*enode.Node) []*node {
    79  	result := make([]*node, len(ns))
    80  	for i, n := range ns {
    81  		result[i] = wrapNode(n)
    82  	}
    83  	return result
    84  }
    85  
    86  func unwrapNode(n *node) *enode.Node {
    87  	return &n.Node
    88  }
    89  
    90  func unwrapNodes(ns []*node) []*enode.Node {
    91  	result := make([]*enode.Node, len(ns))
    92  	for i, n := range ns {
    93  		result[i] = unwrapNode(n)
    94  	}
    95  	return result
    96  }
    97  
    98  func (n *node) addr() *net.UDPAddr {
    99  	return &net.UDPAddr{IP: n.IP(), Port: n.UDP()}
   100  }
   101  
   102  func (n *node) String() string {
   103  	return n.Node.String()
   104  }