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