url.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Copyright 2017 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. package accounts
  17. import (
  18. "encoding/json"
  19. "errors"
  20. "fmt"
  21. "strings"
  22. )
  23. // URL represents the canonical identification URL of a wallet or account.
  24. //
  25. // It is a simplified version of url.URL, with the important limitations (which
  26. // are considered features here) that it contains value-copyable components only,
  27. // as well as that it doesn't do any URL encoding/decoding of special characters.
  28. //
  29. // The former is important to allow an account to be copied without leaving live
  30. // references to the original version, whereas the latter is important to ensure
  31. // one single canonical form opposed to many allowed ones by the RFC 3986 spec.
  32. //
  33. // As such, these URLs should not be used outside of the scope of an Ethereum
  34. // wallet or account.
  35. type URL struct {
  36. Scheme string // Protocol scheme to identify a capable account backend
  37. Path string // Path for the backend to identify a unique entity
  38. }
  39. // parseURL converts a user supplied URL into the accounts specific structure.
  40. func parseURL(url string) (URL, error) {
  41. parts := strings.Split(url, "://")
  42. if len(parts) != 2 || parts[0] == "" {
  43. return URL{}, errors.New("protocol scheme missing")
  44. }
  45. return URL{
  46. Scheme: parts[0],
  47. Path: parts[1],
  48. }, nil
  49. }
  50. // String implements the stringer interface.
  51. func (u URL) String() string {
  52. if u.Scheme != "" {
  53. return fmt.Sprintf("%s://%s", u.Scheme, u.Path)
  54. }
  55. return u.Path
  56. }
  57. // TerminalString implements the log.TerminalStringer interface.
  58. func (u URL) TerminalString() string {
  59. url := u.String()
  60. if len(url) > 32 {
  61. return url[:31] + "…"
  62. }
  63. return url
  64. }
  65. // MarshalJSON implements the json.Marshaller interface.
  66. func (u URL) MarshalJSON() ([]byte, error) {
  67. return json.Marshal(u.String())
  68. }
  69. // Cmp compares x and y and returns:
  70. //
  71. // -1 if x < y
  72. // 0 if x == y
  73. // +1 if x > y
  74. //
  75. func (u URL) Cmp(url URL) int {
  76. if u.Scheme == url.Scheme {
  77. return strings.Compare(u.Path, url.Path)
  78. }
  79. return strings.Compare(u.Scheme, url.Scheme)
  80. }