github.com/alexdevranger/node-1.8.27@v0.0.0-20221128213301-aa5841e41d2d/p2p/discover/node.go (about) 1 // Copyright 2015 The go-ethereum Authors 2 // This file is part of the go-dubxcoin library. 3 // 4 // The go-dubxcoin 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-dubxcoin 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-dubxcoin 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/alexdevranger/node-1.8.27/common/math" 27 "github.com/alexdevranger/node-1.8.27/crypto" 28 "github.com/alexdevranger/node-1.8.27/crypto/secp256k1" 29 "github.com/alexdevranger/node-1.8.27/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(e encPubkey) (*ecdsa.PublicKey, error) { 50 p := &ecdsa.PublicKey{Curve: crypto.S256(), 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 secp256k1 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 // recoverNodeKey computes the public key used to sign the 65 // given hash from the signature. 66 func recoverNodeKey(hash, sig []byte) (key encPubkey, err error) { 67 pubkey, err := secp256k1.RecoverPubkey(hash, sig) 68 if err != nil { 69 return key, err 70 } 71 copy(key[:], pubkey[1:]) 72 return key, nil 73 } 74 75 func wrapNode(n *enode.Node) *node { 76 return &node{Node: *n} 77 } 78 79 func wrapNodes(ns []*enode.Node) []*node { 80 result := make([]*node, len(ns)) 81 for i, n := range ns { 82 result[i] = wrapNode(n) 83 } 84 return result 85 } 86 87 func unwrapNode(n *node) *enode.Node { 88 return &n.Node 89 } 90 91 func unwrapNodes(ns []*node) []*enode.Node { 92 result := make([]*enode.Node, len(ns)) 93 for i, n := range ns { 94 result[i] = unwrapNode(n) 95 } 96 return result 97 } 98 99 func (n *node) addr() *net.UDPAddr { 100 return &net.UDPAddr{IP: n.IP(), Port: n.UDP()} 101 } 102 103 func (n *node) String() string { 104 return n.Node.String() 105 }