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