github.com/codingfuture/orig-energi3@v0.8.4/accounts/hd_test.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  	"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  	tests := []struct {
    29  		input  string
    30  		output DerivationPath
    31  	}{
    32  		// Plain absolute derivation paths
    33  		{"m/44'/9797'/0'/0", DerivationPath{0x80000000 + 44, 0x80000000 + 9797, 0x80000000 + 0, 0}},
    34  		{"m/44'/9797'/0'/128", DerivationPath{0x80000000 + 44, 0x80000000 + 9797, 0x80000000 + 0, 128}},
    35  		{"m/44'/9797'/0'/0'", DerivationPath{0x80000000 + 44, 0x80000000 + 9797, 0x80000000 + 0, 0x80000000 + 0}},
    36  		{"m/44'/9797'/0'/128'", DerivationPath{0x80000000 + 44, 0x80000000 + 9797, 0x80000000 + 0, 0x80000000 + 128}},
    37  		{"m/2147483692/2147493445/2147483648/0", DerivationPath{0x80000000 + 44, 0x80000000 + 9797, 0x80000000 + 0, 0}},
    38  		{"m/2147483692/2147493445/2147483648/2147483648", DerivationPath{0x80000000 + 44, 0x80000000 + 9797, 0x80000000 + 0, 0x80000000 + 0}},
    39  
    40  		// Plain relative derivation paths
    41  		{"0", DerivationPath{0x80000000 + 44, 0x80000000 + 9797, 0x80000000 + 0, 0, 0}},
    42  		{"128", DerivationPath{0x80000000 + 44, 0x80000000 + 9797, 0x80000000 + 0, 0, 128}},
    43  		{"0'", DerivationPath{0x80000000 + 44, 0x80000000 + 9797, 0x80000000 + 0, 0, 0x80000000 + 0}},
    44  		{"128'", DerivationPath{0x80000000 + 44, 0x80000000 + 9797, 0x80000000 + 0, 0, 0x80000000 + 128}},
    45  		{"2147483648", DerivationPath{0x80000000 + 44, 0x80000000 + 9797, 0x80000000 + 0, 0, 0x80000000 + 0}},
    46  
    47  		// Hexadecimal absolute derivation paths
    48  		{"m/0x2C'/0x2645'/0x00'/0x00", DerivationPath{0x80000000 + 44, 0x80000000 + 9797, 0x80000000 + 0, 0}},
    49  		{"m/0x2C'/0x2645'/0x00'/0x80", DerivationPath{0x80000000 + 44, 0x80000000 + 9797, 0x80000000 + 0, 128}},
    50  		{"m/0x2C'/0x2645'/0x00'/0x00'", DerivationPath{0x80000000 + 44, 0x80000000 + 9797, 0x80000000 + 0, 0x80000000 + 0}},
    51  		{"m/0x2C'/0x2645'/0x00'/0x80'", DerivationPath{0x80000000 + 44, 0x80000000 + 9797, 0x80000000 + 0, 0x80000000 + 128}},
    52  		{"m/0x8000002C/0x80002645/0x80000000/0x00", DerivationPath{0x80000000 + 44, 0x80000000 + 9797, 0x80000000 + 0, 0}},
    53  		{"m/0x8000002C/0x80002645/0x80000000/0x80000000", DerivationPath{0x80000000 + 44, 0x80000000 + 9797, 0x80000000 + 0, 0x80000000 + 0}},
    54  
    55  		// Hexadecimal relative derivation paths
    56  		{"0x00", DerivationPath{0x80000000 + 44, 0x80000000 + 9797, 0x80000000 + 0, 0, 0}},
    57  		{"0x80", DerivationPath{0x80000000 + 44, 0x80000000 + 9797, 0x80000000 + 0, 0, 128}},
    58  		{"0x00'", DerivationPath{0x80000000 + 44, 0x80000000 + 9797, 0x80000000 + 0, 0, 0x80000000 + 0}},
    59  		{"0x80'", DerivationPath{0x80000000 + 44, 0x80000000 + 9797, 0x80000000 + 0, 0, 0x80000000 + 128}},
    60  		{"0x80000000", DerivationPath{0x80000000 + 44, 0x80000000 + 9797, 0x80000000 + 0, 0, 0x80000000 + 0}},
    61  
    62  		// Weird inputs just to ensure they work
    63  		{"	m  /   44			'\n/\n   9797	\n\n\t'   /\n0 ' /\t\t	0", DerivationPath{0x80000000 + 44, 0x80000000 + 9797, 0x80000000 + 0, 0}},
    64  
    65  		// Invaid derivation paths
    66  		{"", nil},                // Empty relative derivation path
    67  		{"m", nil},               // Empty absolute derivation path
    68  		{"m/", nil},              // Missing last derivation component
    69  		{"/44'/9797'/0'/0", nil}, // Absolute path without m prefix, might be user error
    70  		{"m/2147483648'", nil},   // Overflows 32 bit integer
    71  		{"m/-1'", nil},           // Cannot contain negative number
    72  	}
    73  	for i, tt := range tests {
    74  		if path, err := ParseDerivationPath(tt.input); !reflect.DeepEqual(path, tt.output) {
    75  			t.Errorf("test %d: parse mismatch: have %v (%v), want %v", i, path, err, tt.output)
    76  		} else if path == nil && err == nil {
    77  			t.Errorf("test %d: nil path and error: %v", i, err)
    78  		}
    79  	}
    80  }