aboutsummaryrefslogtreecommitdiffstats
path: root/p2p/discover/node.go
blob: 1c556083966598573daf3114fbe6d501f5b76e29 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// 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"
    "time"

    "github.com/dexon-foundation/dexon/common/math"
    "github.com/dexon-foundation/dexon/crypto"
    "github.com/dexon-foundation/dexon/crypto/secp256k1"
    "github.com/dexon-foundation/dexon/p2p/enode"
)

// node represents a host on the network.
// The fields of Node may not be modified.
type node struct {
    enode.Node
    addedAt        time.Time // time when the node was added to the table
    livenessChecks uint      // how often liveness was checked
}

type encPubkey [64]byte

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
}

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")
    }
    return p, nil
}

func (e encPubkey) id() enode.ID {
    return enode.ID(crypto.Keccak256Hash(e[:]))
}

// 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)
    if err != nil {
        return key, err
    }
    copy(key[:], pubkey[1:])
    return key, nil
}

func wrapNode(n *enode.Node) *node {
    return &node{Node: *n}
}

func wrapNodes(ns []*enode.Node) []*node {
    result := make([]*node, len(ns))
    for i, n := range ns {
        result[i] = wrapNode(n)
    }
    return result
}

func unwrapNode(n *node) *enode.Node {
    return &n.Node
}

func unwrapNodes(ns []*node) []*enode.Node {
    result := make([]*enode.Node, len(ns))
    for i, n := range ns {
        result[i] = unwrapNode(n)
    }
    return result
}

func (n *node) addr() *net.UDPAddr {
    return &net.UDPAddr{IP: n.IP(), Port: n.UDP()}
}

func (n *node) String() string {
    return n.Node.String()
}