github.com/core-coin/go-core/v2@v2.1.9/mobile/discover.go (about)

     1  // Copyright 2016 by the Authors
     2  // This file is part of the go-core library.
     3  //
     4  // The go-core 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-core 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-core library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  // Contains all the wrappers from the accounts package to support client side enode
    18  // management on mobile platforms.
    19  
    20  package gocore
    21  
    22  import (
    23  	"errors"
    24  
    25  	"github.com/core-coin/go-core/v2/p2p/discv5"
    26  )
    27  
    28  // Enode represents a host on the network.
    29  type Enode struct {
    30  	node *discv5.Node
    31  }
    32  
    33  // NewEnode parses a node designator.
    34  //
    35  // There are two basic forms of node designators
    36  //   - incomplete nodes, which only have the public key (node ID)
    37  //   - complete nodes, which contain the public key and IP/Port information
    38  //
    39  // For incomplete nodes, the designator must look like one of these
    40  //
    41  //	enode://<hex node id>
    42  //	<hex node id>
    43  //
    44  // For complete nodes, the node ID is encoded in the username portion
    45  // of the URL, separated from the host by an @ sign. The hostname can
    46  // only be given as an IP address, DNS domain names are not allowed.
    47  // The port in the host name section is the TCP listening port. If the
    48  // TCP and UDP (discovery) ports differ, the UDP port is specified as
    49  // query parameter "discport".
    50  //
    51  // In the following example, the node URL describes
    52  // a node with IP address 10.3.58.6, TCP listening port 30300
    53  // and UDP discovery port 30330.
    54  //
    55  //	enode://<hex node id>@10.3.58.6:30300?discport=30330
    56  func NewEnode(rawurl string) (enode *Enode, _ error) {
    57  	node, err := discv5.ParseNode(rawurl)
    58  	if err != nil {
    59  		return nil, err
    60  	}
    61  	return &Enode{node}, nil
    62  }
    63  
    64  // Enodes represents a slice of accounts.
    65  type Enodes struct{ nodes []*discv5.Node }
    66  
    67  // NewEnodes creates a slice of uninitialized enodes.
    68  func NewEnodes(size int) *Enodes {
    69  	return &Enodes{
    70  		nodes: make([]*discv5.Node, size),
    71  	}
    72  }
    73  
    74  // NewEnodesEmpty creates an empty slice of Enode values.
    75  func NewEnodesEmpty() *Enodes {
    76  	return NewEnodes(0)
    77  }
    78  
    79  // Size returns the number of enodes in the slice.
    80  func (e *Enodes) Size() int {
    81  	return len(e.nodes)
    82  }
    83  
    84  // Get returns the enode at the given index from the slice.
    85  func (e *Enodes) Get(index int) (enode *Enode, _ error) {
    86  	if index < 0 || index >= len(e.nodes) {
    87  		return nil, errors.New("index out of bounds")
    88  	}
    89  	return &Enode{e.nodes[index]}, nil
    90  }
    91  
    92  // Set sets the enode at the given index in the slice.
    93  func (e *Enodes) Set(index int, enode *Enode) error {
    94  	if index < 0 || index >= len(e.nodes) {
    95  		return errors.New("index out of bounds")
    96  	}
    97  	e.nodes[index] = enode.node
    98  	return nil
    99  }
   100  
   101  // Append adds a new enode element to the end of the slice.
   102  func (e *Enodes) Append(enode *Enode) {
   103  	e.nodes = append(e.nodes, enode.node)
   104  }