github.com/codingfuture/orig-energi3@v0.8.4/accounts/hd.go (about)

     1  // Copyright 2018 The Energi Core Authors
     2  // Copyright 2017 The go-ethereum Authors
     3  // This file is part of the Energi Core library.
     4  //
     5  // The Energi Core library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Lesser General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // The Energi Core library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  // GNU Lesser General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public License
    16  // along with the Energi Core library. If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package accounts
    19  
    20  import (
    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'/9797'/0'/0, the second
    30  // at m/44'/9797'/0'/1, etc.
    31  var DefaultRootDerivationPath = DerivationPath{0x80000000 + 44, 0x80000000 + 9797, 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'/9797'/0'/0/0, the second
    35  // at m/44'/9797'/0'/0/1, etc.
    36  var DefaultBaseDerivationPath = DerivationPath{0x80000000 + 44, 0x80000000 + 9797, 0x80000000 + 0, 0, 0}
    37  
    38  // DefaultLedgerBaseDerivationPath is the base path from which custom derivation endpoints
    39  // are incremented. As such, the first account will be at m/44'/9797'/0'/0, the second
    40  // at m/44'/9797'/0'/1, etc.
    41  var DefaultLedgerBaseDerivationPath = DerivationPath{0x80000000 + 44, 0x80000000 + 9797, 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` 9797' (or 0x80002645) to Energi.
    55  //
    56  // The root path for Ethereum is m/44'/60'/0'/0 according to the specification
    57  // from https://github.com/ethereum/EIPs/issues/84, albeit it's not set in stone
    58  // yet whether accounts should increment the last component or the children of
    59  // that. We will go with the simpler approach of incrementing the last component.
    60  type DerivationPath []uint32
    61  
    62  // ParseDerivationPath converts a user specified derivation path string to the
    63  // internal binary representation.
    64  //
    65  // Full derivation paths need to start with the `m/` prefix, relative derivation
    66  // paths (which will get appended to the default root path) must not have prefixes
    67  // in front of the first element. Whitespace is ignored.
    68  func ParseDerivationPath(path string) (DerivationPath, error) {
    69  	var result DerivationPath
    70  
    71  	// Handle absolute or relative paths
    72  	components := strings.Split(path, "/")
    73  	switch {
    74  	case len(components) == 0:
    75  		return nil, errors.New("empty derivation path")
    76  
    77  	case strings.TrimSpace(components[0]) == "":
    78  		return nil, errors.New("ambiguous path: use 'm/' prefix for absolute paths, or no leading '/' for relative ones")
    79  
    80  	case strings.TrimSpace(components[0]) == "m":
    81  		components = components[1:]
    82  
    83  	default:
    84  		result = append(result, DefaultRootDerivationPath...)
    85  	}
    86  	// All remaining components are relative, append one by one
    87  	if len(components) == 0 {
    88  		return nil, errors.New("empty derivation path") // Empty relative paths
    89  	}
    90  	for _, component := range components {
    91  		// Ignore any user added whitespace
    92  		component = strings.TrimSpace(component)
    93  		var value uint32
    94  
    95  		// Handle hardened paths
    96  		if strings.HasSuffix(component, "'") {
    97  			value = 0x80000000
    98  			component = strings.TrimSpace(strings.TrimSuffix(component, "'"))
    99  		}
   100  		// Handle the non hardened component
   101  		bigval, ok := new(big.Int).SetString(component, 0)
   102  		if !ok {
   103  			return nil, fmt.Errorf("invalid component: %s", component)
   104  		}
   105  		max := math.MaxUint32 - value
   106  		if bigval.Sign() < 0 || bigval.Cmp(big.NewInt(int64(max))) > 0 {
   107  			if value == 0 {
   108  				return nil, fmt.Errorf("component %v out of allowed range [0, %d]", bigval, max)
   109  			}
   110  			return nil, fmt.Errorf("component %v out of allowed hardened range [0, %d]", bigval, max)
   111  		}
   112  		value += uint32(bigval.Uint64())
   113  
   114  		// Append and repeat
   115  		result = append(result, value)
   116  	}
   117  	return result, nil
   118  }
   119  
   120  // String implements the stringer interface, converting a binary derivation path
   121  // to its canonical representation.
   122  func (path DerivationPath) String() string {
   123  	result := "m"
   124  	for _, component := range path {
   125  		var hardened bool
   126  		if component >= 0x80000000 {
   127  			component -= 0x80000000
   128  			hardened = true
   129  		}
   130  		result = fmt.Sprintf("%s/%d", result, component)
   131  		if hardened {
   132  			result += "'"
   133  		}
   134  	}
   135  	return result
   136  }