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