hd.go 5.0 KB

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