github.com/amazechain/amc@v0.1.3/internal/p2p/enode/urlv4.go (about)

     1  // Copyright 2018 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 enode
    18  
    19  import (
    20  	"crypto/ecdsa"
    21  	"encoding/hex"
    22  	"errors"
    23  	"fmt"
    24  	"github.com/amazechain/amc/common/crypto"
    25  	"github.com/amazechain/amc/common/math"
    26  	"github.com/amazechain/amc/internal/p2p/enr"
    27  	"net"
    28  	"net/url"
    29  	"regexp"
    30  	"strconv"
    31  )
    32  
    33  var (
    34  	incompleteNodeURL = regexp.MustCompile("(?i)^(?:enode://)?([0-9a-f]+)$")
    35  	lookupIPFunc      = net.LookupIP
    36  )
    37  
    38  // MustParseV4 parses a node URL. It panics if the URL is not valid.
    39  func MustParseV4(rawurl string) *Node {
    40  	n, err := ParseV4(rawurl)
    41  	if err != nil {
    42  		panic("invalid node URL: " + err.Error())
    43  	}
    44  	return n
    45  }
    46  
    47  // ParseV4 parses a node URL.
    48  //
    49  // There are two basic forms of node URLs:
    50  //
    51  //   - incomplete nodes, which only have the public key (node ID)
    52  //   - complete nodes, which contain the public key and IP/Port information
    53  //
    54  // For incomplete nodes, the designator must look like one of these
    55  //
    56  //	enode://<hex node id>
    57  //	<hex node id>
    58  //
    59  // For complete nodes, the node ID is encoded in the username portion
    60  // of the URL, separated from the host by an @ sign. The hostname can
    61  // only be given as an IP address or using DNS domain name.
    62  // The port in the host name section is the TCP listening port. If the
    63  // TCP and UDP (discovery) ports differ, the UDP port is specified as
    64  // query parameter "discport".
    65  //
    66  // In the following example, the node URL describes
    67  // a node with IP address 10.3.58.6, TCP listening port 30303
    68  // and UDP discovery port 30301.
    69  //
    70  //	enode://<hex node id>@10.3.58.6:30303?discport=30301
    71  func ParseV4(rawurl string) (*Node, error) {
    72  	if m := incompleteNodeURL.FindStringSubmatch(rawurl); m != nil {
    73  		id, err := parsePubkey(m[1])
    74  		if err != nil {
    75  			return nil, fmt.Errorf("invalid public key (%v)", err)
    76  		}
    77  		return NewV4(id, nil, 0, 0), nil
    78  	}
    79  	return parseComplete(rawurl)
    80  }
    81  
    82  // NewV4 creates a node from discovery v4 node information. The record
    83  // contained in the node has a zero-length signature.
    84  func NewV4(pubkey *ecdsa.PublicKey, ip net.IP, tcp, udp int) *Node {
    85  	var r enr.Record
    86  	if len(ip) > 0 {
    87  		r.Set(enr.IP(ip))
    88  	}
    89  	if udp != 0 {
    90  		r.Set(enr.UDP(udp))
    91  	}
    92  	if tcp != 0 {
    93  		r.Set(enr.TCP(tcp))
    94  	}
    95  	signV4Compat(&r, pubkey)
    96  	n, err := New(v4CompatID{}, &r)
    97  	if err != nil {
    98  		panic(err)
    99  	}
   100  	return n
   101  }
   102  
   103  // isNewV4 returns true for nodes created by NewV4.
   104  func isNewV4(n *Node) bool {
   105  	var k s256raw
   106  	return n.r.IdentityScheme() == "" && n.r.Load(&k) == nil && len(n.r.Signature()) == 0
   107  }
   108  
   109  func parseComplete(rawurl string) (*Node, error) {
   110  	var (
   111  		id               *ecdsa.PublicKey
   112  		tcpPort, udpPort uint64
   113  	)
   114  	u, err := url.Parse(rawurl)
   115  	if err != nil {
   116  		return nil, err
   117  	}
   118  	if u.Scheme != "enode" {
   119  		return nil, errors.New("invalid URL scheme, want \"enode\"")
   120  	}
   121  	// Parse the Node ID from the user portion.
   122  	if u.User == nil {
   123  		return nil, errors.New("does not contain node ID")
   124  	}
   125  	if id, err = parsePubkey(u.User.String()); err != nil {
   126  		return nil, fmt.Errorf("invalid public key (%v)", err)
   127  	}
   128  	// Parse the IP address.
   129  	ip := net.ParseIP(u.Hostname())
   130  	if ip == nil {
   131  		ips, err := lookupIPFunc(u.Hostname())
   132  		if err != nil {
   133  			return nil, err
   134  		}
   135  		ip = ips[0]
   136  	}
   137  	// Ensure the IP is 4 bytes long for IPv4 addresses.
   138  	if ipv4 := ip.To4(); ipv4 != nil {
   139  		ip = ipv4
   140  	}
   141  	// Parse the port numbers.
   142  	if tcpPort, err = strconv.ParseUint(u.Port(), 10, 16); err != nil {
   143  		return nil, errors.New("invalid port")
   144  	}
   145  	udpPort = tcpPort
   146  	qv := u.Query()
   147  	if qv.Get("discport") != "" {
   148  		udpPort, err = strconv.ParseUint(qv.Get("discport"), 10, 16)
   149  		if err != nil {
   150  			return nil, errors.New("invalid discport in query")
   151  		}
   152  	}
   153  	return NewV4(id, ip, int(tcpPort), int(udpPort)), nil
   154  }
   155  
   156  // parsePubkey parses a hex-encoded secp256k1 public key.
   157  func parsePubkey(in string) (*ecdsa.PublicKey, error) {
   158  	b, err := hex.DecodeString(in)
   159  	if err != nil {
   160  		return nil, err
   161  	} else if len(b) != 64 {
   162  		return nil, fmt.Errorf("wrong length, want %d hex chars", 128)
   163  	}
   164  	b = append([]byte{0x4}, b...)
   165  	return crypto.UnmarshalPubkey(b)
   166  }
   167  
   168  func (n *Node) URLv4() string {
   169  	var (
   170  		scheme enr.ID
   171  		nodeid string
   172  		key    ecdsa.PublicKey
   173  	)
   174  	n.Load(&scheme)
   175  	n.Load((*Secp256k1)(&key))
   176  	switch {
   177  	case scheme == "v4" || key != ecdsa.PublicKey{}:
   178  		nodeid = fmt.Sprintf("%x", crypto.FromECDSAPub(&key)[1:])
   179  	default:
   180  		nodeid = fmt.Sprintf("%s.%x", scheme, n.id[:])
   181  	}
   182  	u := url.URL{Scheme: "enode"}
   183  	if n.Incomplete() {
   184  		u.Host = nodeid
   185  	} else {
   186  		addr := net.TCPAddr{IP: n.IP(), Port: n.TCP()}
   187  		u.User = url.User(nodeid)
   188  		u.Host = addr.String()
   189  		if n.UDP() != n.TCP() {
   190  			u.RawQuery = "discport=" + strconv.Itoa(n.UDP())
   191  		}
   192  	}
   193  	return u.String()
   194  }
   195  
   196  // PubkeyToIDV4 derives the v4 node address from the given public key.
   197  func PubkeyToIDV4(key *ecdsa.PublicKey) ID {
   198  	e := make([]byte, 64)
   199  	math.ReadBits(key.X, e[:len(e)/2])
   200  	math.ReadBits(key.Y, e[len(e)/2:])
   201  	return ID(crypto.Keccak256Hash(e))
   202  }