github.com/status-im/status-go@v1.1.0/account/utils_test.go (about)

     1  package account
     2  
     3  // Basic imports
     4  import (
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/stretchr/testify/suite"
     9  
    10  	"github.com/status-im/status-go/account/generator"
    11  	"github.com/status-im/status-go/eth-node/crypto"
    12  	"github.com/status-im/status-go/eth-node/types"
    13  	"github.com/status-im/status-go/extkeys"
    14  )
    15  
    16  type AccountUtilsTestSuite struct {
    17  	suite.Suite
    18  	validKey string
    19  }
    20  
    21  func (suite *AccountUtilsTestSuite) SetupTest() {
    22  	suite.validKey = "0xF35E0325dad87e2661c4eF951d58727e6d583d5c"
    23  }
    24  
    25  func (suite *AccountUtilsTestSuite) TestToAddress() {
    26  	addr := ToAddress(suite.validKey)
    27  	suite.Equal(suite.validKey, addr.String())
    28  }
    29  
    30  func (suite *AccountUtilsTestSuite) TestToAddressInvalidAddress() {
    31  	addr := ToAddress("foobar")
    32  	suite.Nil(addr)
    33  }
    34  
    35  func (suite *AccountUtilsTestSuite) TestFromAddress() {
    36  	var flagtests = []struct {
    37  		in  string
    38  		out string
    39  	}{
    40  		{suite.validKey, suite.validKey},
    41  		{"foobar", "0x0000000000000000000000000000000000000000"},
    42  	}
    43  
    44  	for _, tt := range flagtests {
    45  		addr := FromAddress(tt.in)
    46  		suite.Equal(tt.out, addr.String())
    47  	}
    48  }
    49  
    50  func (suite *AccountUtilsTestSuite) TestHex() {
    51  	var addr *SelectedExtKey
    52  	cr, _ := crypto.GenerateKey()
    53  	var flagtests = []struct {
    54  		in  *SelectedExtKey
    55  		out string
    56  	}{
    57  		{&SelectedExtKey{
    58  			Address:    FromAddress(suite.validKey),
    59  			AccountKey: &types.Key{PrivateKey: cr},
    60  		}, suite.validKey},
    61  		{addr, "0x0"},
    62  	}
    63  
    64  	for _, tt := range flagtests {
    65  		suite.Equal(tt.in.Hex(), tt.out)
    66  	}
    67  }
    68  
    69  func TestAccountUtilsTestSuite(t *testing.T) {
    70  	suite.Run(t, new(AccountUtilsTestSuite))
    71  }
    72  
    73  func TestMnemonicPhraseLengthToEntropyStrength(t *testing.T) {
    74  	scenarios := []struct {
    75  		phraseLength     int
    76  		expectedStrength extkeys.EntropyStrength
    77  		expectedError    error
    78  	}{
    79  		{12, 128, nil},
    80  		{15, 160, nil},
    81  		{18, 192, nil},
    82  		{21, 224, nil},
    83  		{24, 256, nil},
    84  		// invalid
    85  		{11, 0, ErrInvalidMnemonicPhraseLength},
    86  		{14, 0, ErrInvalidMnemonicPhraseLength},
    87  		{25, 0, ErrInvalidMnemonicPhraseLength},
    88  	}
    89  
    90  	for _, s := range scenarios {
    91  		strength, err := generator.MnemonicPhraseLengthToEntropyStrength(s.phraseLength)
    92  		assert.Equal(t, s.expectedError, err)
    93  		assert.Equal(t, s.expectedStrength, strength)
    94  	}
    95  }