github.com/klaytn/klaytn@v1.12.1/accounts/url.go (about) 1 // Modifications Copyright 2018 The klaytn Authors 2 // Copyright 2017 The go-ethereum Authors 3 // This file is part of the go-ethereum library. 4 // 5 // The go-ethereum 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-ethereum 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-ethereum library. If not, see <http://www.gnu.org/licenses/>. 17 // 18 // This file is derived from accounts/url.go (2018/06/04). 19 // Modified and improved for the klaytn development. 20 21 package accounts 22 23 import ( 24 "encoding/json" 25 "errors" 26 "fmt" 27 "strings" 28 ) 29 30 // URL represents the canonical identification URL of a wallet or account. 31 // 32 // It is a simplified version of url.URL, with the important limitations (which 33 // are considered features here) that it contains value-copyable components only, 34 // as well as that it doesn't do any URL encoding/decoding of special characters. 35 // 36 // The former is important to allow an account to be copied without leaving live 37 // references to the original version, whereas the latter is important to ensure 38 // one single canonical form opposed to many allowed ones by the RFC 3986 spec. 39 // 40 // As such, these URLs should not be used outside of the scope of a Klaytn 41 // wallet or account. 42 type URL struct { 43 Scheme string // Protocol scheme to identify a capable account backend 44 Path string // Path for the backend to identify a unique entity 45 } 46 47 // parseURL converts a user supplied URL into the accounts specific structure. 48 func parseURL(url string) (URL, error) { 49 parts := strings.Split(url, "://") 50 if len(parts) != 2 || parts[0] == "" { 51 return URL{}, errors.New("protocol scheme missing") 52 } 53 return URL{ 54 Scheme: parts[0], 55 Path: parts[1], 56 }, nil 57 } 58 59 // String implements the stringer interface. 60 func (u URL) String() string { 61 if u.Scheme != "" { 62 return fmt.Sprintf("%s://%s", u.Scheme, u.Path) 63 } 64 return u.Path 65 } 66 67 // TerminalString implements the log.TerminalStringer interface. 68 func (u URL) TerminalString() string { 69 url := u.String() 70 if len(url) > 32 { 71 return url[:31] + "…" 72 } 73 return url 74 } 75 76 // MarshalJSON implements the json.Marshaller interface. 77 func (u URL) MarshalJSON() ([]byte, error) { 78 return json.Marshal(u.String()) 79 } 80 81 // UnmarshalJSON parses url. 82 func (u *URL) UnmarshalJSON(input []byte) error { 83 var textURL string 84 err := json.Unmarshal(input, &textURL) 85 if err != nil { 86 return err 87 } 88 url, err := parseURL(textURL) 89 if err != nil { 90 return err 91 } 92 u.Scheme = url.Scheme 93 u.Path = url.Path 94 return nil 95 } 96 97 // Cmp compares x and y and returns: 98 // 99 // -1 if x < y 100 // 0 if x == y 101 // +1 if x > y 102 // 103 func (u URL) Cmp(url URL) int { 104 if u.Scheme == url.Scheme { 105 return strings.Compare(u.Path, url.Path) 106 } 107 return strings.Compare(u.Scheme, url.Scheme) 108 }