github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/mobile/discover.go (about) 1 // Copyright 2018 The go-ethereum Authors 2 // Copyright 2019 The go-aigar Authors 3 // This file is part of the go-aigar library. 4 // 5 // The go-aigar library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Lesser General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // The go-aigar library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with the go-aigar library. If not, see <http://www.gnu.org/licenses/>. 17 18 // Contains all the wrappers from the accounts package to support client side enode 19 // management on mobile platforms. 20 21 package geth 22 23 import ( 24 "errors" 25 26 "github.com/AigarNetwork/aigar/p2p/discv5" 27 ) 28 29 // Enode represents a host on the network. 30 type Enode struct { 31 node *discv5.Node 32 } 33 34 // NewEnode parses a node designator. 35 // 36 // There are two basic forms of node designators 37 // - incomplete nodes, which only have the public key (node ID) 38 // - complete nodes, which contain the public key and IP/Port information 39 // 40 // For incomplete nodes, the designator must look like one of these 41 // 42 // enode://<hex node id> 43 // <hex node id> 44 // 45 // For complete nodes, the node ID is encoded in the username portion 46 // of the URL, separated from the host by an @ sign. The hostname can 47 // only be given as an IP address, DNS domain names are not allowed. 48 // The port in the host name section is the TCP listening port. If the 49 // TCP and UDP (discovery) ports differ, the UDP port is specified as 50 // query parameter "discport". 51 // 52 // In the following example, the node URL describes 53 // a node with IP address 10.3.58.6, TCP listening port 30303 54 // and UDP discovery port 30301. 55 // 56 // enode://<hex node id>@10.3.58.6:30303?discport=30301 57 func NewEnode(rawurl string) (enode *Enode, _ error) { 58 node, err := discv5.ParseNode(rawurl) 59 if err != nil { 60 return nil, err 61 } 62 return &Enode{node}, nil 63 } 64 65 // Enodes represents a slice of accounts. 66 type Enodes struct{ nodes []*discv5.Node } 67 68 // NewEnodes creates a slice of uninitialized enodes. 69 func NewEnodes(size int) *Enodes { 70 return &Enodes{ 71 nodes: make([]*discv5.Node, size), 72 } 73 } 74 75 // NewEnodesEmpty creates an empty slice of Enode values. 76 func NewEnodesEmpty() *Enodes { 77 return NewEnodes(0) 78 } 79 80 // Size returns the number of enodes in the slice. 81 func (e *Enodes) Size() int { 82 return len(e.nodes) 83 } 84 85 // Get returns the enode at the given index from the slice. 86 func (e *Enodes) Get(index int) (enode *Enode, _ error) { 87 if index < 0 || index >= len(e.nodes) { 88 return nil, errors.New("index out of bounds") 89 } 90 return &Enode{e.nodes[index]}, nil 91 } 92 93 // Set sets the enode at the given index in the slice. 94 func (e *Enodes) Set(index int, enode *Enode) error { 95 if index < 0 || index >= len(e.nodes) { 96 return errors.New("index out of bounds") 97 } 98 e.nodes[index] = enode.node 99 return nil 100 } 101 102 // Append adds a new enode element to the end of the slice. 103 func (e *Enodes) Append(enode *Enode) { 104 e.nodes = append(e.nodes, enode.node) 105 }