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