hd.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. "errors"
  19. "fmt"
  20. "math"
  21. "math/big"
  22. "strings"
  23. )
  24. // DefaultRootDerivationPath is the root path to which custom derivation endpoints
  25. // are appended. As such, the first account will be at m/44'/60'/0'/0, the second
  26. // at m/44'/60'/0'/1, etc.
  27. var DefaultRootDerivationPath = DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0}
  28. // DefaultBaseDerivationPath is the base path from which custom derivation endpoints
  29. // are incremented. As such, the first account will be at m/44'/60'/0'/0, the second
  30. // at m/44'/60'/0'/1, etc.
  31. var DefaultBaseDerivationPath = DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0}
  32. // DerivationPath represents the computer friendly version of a hierarchical
  33. // deterministic wallet account derivaion path.
  34. //
  35. // The BIP-32 spec https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki
  36. // defines derivation paths to be of the form:
  37. //
  38. // m / purpose' / coin_type' / account' / change / address_index
  39. //
  40. // The BIP-44 spec https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki
  41. // defines that the `purpose` be 44' (or 0x8000002C) for crypto currencies, and
  42. // SLIP-44 https://github.com/satoshilabs/slips/blob/master/slip-0044.md assigns
  43. // the `coin_type` 60' (or 0x8000003C) to Ethereum.
  44. //
  45. // The root path for Ethereum is m/44'/60'/0'/0 according to the specification
  46. // from https://github.com/ethereum/EIPs/issues/84, albeit it's not set in stone
  47. // yet whether accounts should increment the last component or the children of
  48. // that. We will go with the simpler approach of incrementing the last component.
  49. type DerivationPath []uint32
  50. // ParseDerivationPath converts a user specified derivation path string to the
  51. // internal binary representation.
  52. //
  53. // Full derivation paths need to start with the `m/` prefix, relative derivation
  54. // paths (which will get appended to the default root path) must not have prefixes
  55. // in front of the first element. Whitespace is ignored.
  56. func ParseDerivationPath(path string) (DerivationPath, error) {
  57. var result DerivationPath
  58. // Handle absolute or relative paths
  59. components := strings.Split(path, "/")
  60. switch {
  61. case len(components) == 0:
  62. return nil, errors.New("empty derivation path")
  63. case strings.TrimSpace(components[0]) == "":
  64. return nil, errors.New("ambiguous path: use 'm/' prefix for absolute paths, or no leading '/' for relative ones")
  65. case strings.TrimSpace(components[0]) == "m":
  66. components = components[1:]
  67. default:
  68. result = append(result, DefaultRootDerivationPath...)
  69. }
  70. // All remaining components are relative, append one by one
  71. if len(components) == 0 {
  72. return nil, errors.New("empty derivation path") // Empty relative paths
  73. }
  74. for _, component := range components {
  75. // Ignore any user added whitespace
  76. component = strings.TrimSpace(component)
  77. var value uint32
  78. // Handle hardened paths
  79. if strings.HasSuffix(component, "'") {
  80. value = 0x80000000
  81. component = strings.TrimSpace(strings.TrimSuffix(component, "'"))
  82. }
  83. // Handle the non hardened component
  84. bigval, ok := new(big.Int).SetString(component, 0)
  85. if !ok {
  86. return nil, fmt.Errorf("invalid component: %s", component)
  87. }
  88. max := math.MaxUint32 - value
  89. if bigval.Sign() < 0 || bigval.Cmp(big.NewInt(int64(max))) > 0 {
  90. if value == 0 {
  91. return nil, fmt.Errorf("component %v out of allowed range [0, %d]", bigval, max)
  92. }
  93. return nil, fmt.Errorf("component %v out of allowed hardened range [0, %d]", bigval, max)
  94. }
  95. value += uint32(bigval.Uint64())
  96. // Append and repeat
  97. result = append(result, value)
  98. }
  99. return result, nil
  100. }
  101. // String implements the stringer interface, converting a binary derivation path
  102. // to its canonical representation.
  103. func (path DerivationPath) String() string {
  104. result := "m"
  105. for _, component := range path {
  106. var hardened bool
  107. if component >= 0x80000000 {
  108. component -= 0x80000000
  109. hardened = true
  110. }
  111. result = fmt.Sprintf("%s/%d", result, component)
  112. if hardened {
  113. result += "'"
  114. }
  115. }
  116. return result
  117. }