github.com/ethereum/go-ethereum@v1.14.3/accounts/hd_test.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  	"fmt"
    21  	"reflect"
    22  	"testing"
    23  )
    24  
    25  // Tests that HD derivation paths can be correctly parsed into our internal binary
    26  // representation.
    27  func TestHDPathParsing(t *testing.T) {
    28  	t.Parallel()
    29  	tests := []struct {
    30  		input  string
    31  		output DerivationPath
    32  	}{
    33  		// Plain absolute derivation paths
    34  		{"m/44'/60'/0'/0", DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0}},
    35  		{"m/44'/60'/0'/128", DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 128}},
    36  		{"m/44'/60'/0'/0'", DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0x80000000 + 0}},
    37  		{"m/44'/60'/0'/128'", DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0x80000000 + 128}},
    38  		{"m/2147483692/2147483708/2147483648/0", DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0}},
    39  		{"m/2147483692/2147483708/2147483648/2147483648", DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0x80000000 + 0}},
    40  
    41  		// Plain relative derivation paths
    42  		{"0", DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0, 0}},
    43  		{"128", DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0, 128}},
    44  		{"0'", DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0, 0x80000000 + 0}},
    45  		{"128'", DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0, 0x80000000 + 128}},
    46  		{"2147483648", DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0, 0x80000000 + 0}},
    47  
    48  		// Hexadecimal absolute derivation paths
    49  		{"m/0x2C'/0x3c'/0x00'/0x00", DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0}},
    50  		{"m/0x2C'/0x3c'/0x00'/0x80", DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 128}},
    51  		{"m/0x2C'/0x3c'/0x00'/0x00'", DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0x80000000 + 0}},
    52  		{"m/0x2C'/0x3c'/0x00'/0x80'", DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0x80000000 + 128}},
    53  		{"m/0x8000002C/0x8000003c/0x80000000/0x00", DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0}},
    54  		{"m/0x8000002C/0x8000003c/0x80000000/0x80000000", DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0x80000000 + 0}},
    55  
    56  		// Hexadecimal relative derivation paths
    57  		{"0x00", DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0, 0}},
    58  		{"0x80", DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0, 128}},
    59  		{"0x00'", DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0, 0x80000000 + 0}},
    60  		{"0x80'", DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0, 0x80000000 + 128}},
    61  		{"0x80000000", DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0, 0x80000000 + 0}},
    62  
    63  		// Weird inputs just to ensure they work
    64  		{"	m  /   44			'\n/\n   60	\n\n\t'   /\n0 ' /\t\t	0", DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0}},
    65  
    66  		// Invalid derivation paths
    67  		{"", nil},              // Empty relative derivation path
    68  		{"m", nil},             // Empty absolute derivation path
    69  		{"m/", nil},            // Missing last derivation component
    70  		{"/44'/60'/0'/0", nil}, // Absolute path without m prefix, might be user error
    71  		{"m/2147483648'", nil}, // Overflows 32 bit integer
    72  		{"m/-1'", nil},         // Cannot contain negative number
    73  	}
    74  	for i, tt := range tests {
    75  		if path, err := ParseDerivationPath(tt.input); !reflect.DeepEqual(path, tt.output) {
    76  			t.Errorf("test %d: parse mismatch: have %v (%v), want %v", i, path, err, tt.output)
    77  		} else if path == nil && err == nil {
    78  			t.Errorf("test %d: nil path and error: %v", i, err)
    79  		}
    80  	}
    81  }
    82  
    83  func testDerive(t *testing.T, next func() DerivationPath, expected []string) {
    84  	t.Helper()
    85  	for i, want := range expected {
    86  		if have := next(); fmt.Sprintf("%v", have) != want {
    87  			t.Errorf("step %d, have %v, want %v", i, have, want)
    88  		}
    89  	}
    90  }
    91  
    92  func TestHdPathIteration(t *testing.T) {
    93  	t.Parallel()
    94  	testDerive(t, DefaultIterator(DefaultBaseDerivationPath),
    95  		[]string{
    96  			"m/44'/60'/0'/0/0", "m/44'/60'/0'/0/1",
    97  			"m/44'/60'/0'/0/2", "m/44'/60'/0'/0/3",
    98  			"m/44'/60'/0'/0/4", "m/44'/60'/0'/0/5",
    99  			"m/44'/60'/0'/0/6", "m/44'/60'/0'/0/7",
   100  			"m/44'/60'/0'/0/8", "m/44'/60'/0'/0/9",
   101  		})
   102  
   103  	testDerive(t, DefaultIterator(LegacyLedgerBaseDerivationPath),
   104  		[]string{
   105  			"m/44'/60'/0'/0", "m/44'/60'/0'/1",
   106  			"m/44'/60'/0'/2", "m/44'/60'/0'/3",
   107  			"m/44'/60'/0'/4", "m/44'/60'/0'/5",
   108  			"m/44'/60'/0'/6", "m/44'/60'/0'/7",
   109  			"m/44'/60'/0'/8", "m/44'/60'/0'/9",
   110  		})
   111  
   112  	testDerive(t, LedgerLiveIterator(DefaultBaseDerivationPath),
   113  		[]string{
   114  			"m/44'/60'/0'/0/0", "m/44'/60'/1'/0/0",
   115  			"m/44'/60'/2'/0/0", "m/44'/60'/3'/0/0",
   116  			"m/44'/60'/4'/0/0", "m/44'/60'/5'/0/0",
   117  			"m/44'/60'/6'/0/0", "m/44'/60'/7'/0/0",
   118  			"m/44'/60'/8'/0/0", "m/44'/60'/9'/0/0",
   119  		})
   120  }