url.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. // UnmarshalJSON parses url.
  70. func (u *URL) UnmarshalJSON(input []byte) error {
  71. var textUrl string
  72. err := json.Unmarshal(input, &textUrl)
  73. if err != nil {
  74. return err
  75. }
  76. url, err := parseURL(textUrl)
  77. if err != nil {
  78. return err
  79. }
  80. u.Scheme = url.Scheme
  81. u.Path = url.Path
  82. return nil
  83. }
  84. // Cmp compares x and y and returns:
  85. //
  86. // -1 if x < y
  87. // 0 if x == y
  88. // +1 if x > y
  89. //
  90. func (u URL) Cmp(url URL) int {
  91. if u.Scheme == url.Scheme {
  92. return strings.Compare(u.Path, url.Path)
  93. }
  94. return strings.Compare(u.Scheme, url.Scheme)
  95. }