code.vegaprotocol.io/vega@v0.79.0/core/nodewallets/eth/clef/wallet_test.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package clef_test
    17  
    18  import (
    19  	"context"
    20  	"fmt"
    21  	"testing"
    22  
    23  	"code.vegaprotocol.io/vega/core/nodewallets/eth/clef"
    24  	"code.vegaprotocol.io/vega/core/nodewallets/eth/clef/mocks"
    25  	"code.vegaprotocol.io/vega/core/nodewallets/registry"
    26  
    27  	ethCommon "github.com/ethereum/go-ethereum/common"
    28  	"github.com/golang/mock/gomock"
    29  	"github.com/stretchr/testify/assert"
    30  )
    31  
    32  var testAddress = ethCommon.HexToAddress("0x1Ff482D42D1237258A1686102Fa4ba925C23Bc42")
    33  
    34  func TestNewWallet(t *testing.T) {
    35  	t.Run("Success", testNewWalletSuccess)
    36  	t.Run("Returns an error if account is not found", testNewWalletAccountNotFound)
    37  	t.Run("Returns an error on RPC call failure", testNewWalletRPCError)
    38  }
    39  
    40  func testNewWalletSuccess(t *testing.T) {
    41  	a := assert.New(t)
    42  
    43  	ctrl := gomock.NewController(t)
    44  	clientMock := mocks.NewMockClient(ctrl)
    45  
    46  	clientMock.EXPECT().
    47  		CallContext(gomock.Any(), gomock.Any(), "account_list", gomock.Any()).
    48  		Times(1).
    49  		DoAndReturn(func(_ context.Context, accs *[]ethCommon.Address, s string, _ ...interface{}) error {
    50  			*accs = append(*accs, testAddress)
    51  
    52  			return nil
    53  		})
    54  
    55  	wallet, err := clef.NewWallet(clientMock, "http://127.0.0.1:8580", testAddress)
    56  	a.NoError(err)
    57  	a.NotNil(wallet)
    58  }
    59  
    60  func testNewWalletAccountNotFound(t *testing.T) {
    61  	a := assert.New(t)
    62  
    63  	ctrl := gomock.NewController(t)
    64  	clientMock := mocks.NewMockClient(ctrl)
    65  
    66  	clientMock.EXPECT().
    67  		CallContext(gomock.Any(), gomock.Any(), "account_list", gomock.Any()).
    68  		Times(1).
    69  		Return(nil)
    70  
    71  	wallet, err := clef.NewWallet(clientMock, "http://127.0.0.1:8580", testAddress)
    72  	a.EqualError(err, "account not found: wallet does not contain account \"0x1fF482d42D1237258a1686102FA4bA925c23bc42\"")
    73  	a.Nil(wallet)
    74  }
    75  
    76  func testNewWalletRPCError(t *testing.T) {
    77  	a := assert.New(t)
    78  
    79  	ctrl := gomock.NewController(t)
    80  	clientMock := mocks.NewMockClient(ctrl)
    81  
    82  	clientMock.EXPECT().
    83  		CallContext(gomock.Any(), gomock.Any(), "account_list", gomock.Any()).
    84  		Times(1).
    85  		Return(fmt.Errorf("something went wrong"))
    86  
    87  	wallet, err := clef.NewWallet(clientMock, "http://127.0.0.1:8580", testAddress)
    88  	a.EqualError(err, "account not found: failed to list accounts: failed to call client: something went wrong")
    89  	a.Nil(wallet)
    90  }
    91  
    92  func TestGenerateNewWallet(t *testing.T) {
    93  	t.Run("Success", testGenerateNewWalletSuccess)
    94  	t.Run("Returns an error on RPC call failure", testGenerateRPCError)
    95  }
    96  
    97  func testGenerateNewWalletSuccess(t *testing.T) {
    98  	a := assert.New(t)
    99  
   100  	ctrl := gomock.NewController(t)
   101  	clientMock := mocks.NewMockClient(ctrl)
   102  
   103  	clientMock.EXPECT().
   104  		CallContext(gomock.Any(), gomock.Any(), "account_new", gomock.Any()).
   105  		Times(1).
   106  		DoAndReturn(func(_ context.Context, addr *string, _ interface{}, _ ...interface{}) error {
   107  			*addr = testAddress.String()
   108  
   109  			return nil
   110  		})
   111  
   112  	wallet, err := clef.GenerateNewWallet(clientMock, "http://127.0.0.1:8580")
   113  	a.NoError(err)
   114  	a.NotNil(wallet)
   115  }
   116  
   117  func testGenerateRPCError(t *testing.T) {
   118  	a := assert.New(t)
   119  
   120  	ctrl := gomock.NewController(t)
   121  	clientMock := mocks.NewMockClient(ctrl)
   122  
   123  	clientMock.EXPECT().
   124  		CallContext(gomock.Any(), gomock.Any(), "account_new", gomock.Any()).
   125  		Times(1).
   126  		Return(fmt.Errorf("something went wrong"))
   127  
   128  	wallet, err := clef.GenerateNewWallet(clientMock, "http://127.0.0.1:8580")
   129  	a.EqualError(err, "failed to generate account: failed to call client: something went wrong")
   130  	a.Nil(wallet)
   131  }
   132  
   133  func TestVersion(t *testing.T) {
   134  	t.Run("Success", testVersionSuccess)
   135  }
   136  
   137  func testVersionSuccess(t *testing.T) {
   138  	a := assert.New(t)
   139  
   140  	ctrl := gomock.NewController(t)
   141  	clientMock := mocks.NewMockClient(ctrl)
   142  
   143  	testVersion := "v1.0.1"
   144  
   145  	clientMock.EXPECT().
   146  		CallContext(gomock.Any(), gomock.Any(), "account_list", gomock.Any()).
   147  		Times(1).
   148  		DoAndReturn(func(_ interface{}, accs *[]ethCommon.Address, _ interface{}, _ ...interface{}) error {
   149  			*accs = append(*accs, testAddress)
   150  
   151  			return nil
   152  		})
   153  
   154  	clientMock.EXPECT().
   155  		CallContext(gomock.Any(), gomock.Any(), "account_version", gomock.Any()).
   156  		Times(1).
   157  		DoAndReturn(func(_ context.Context, version *string, _ interface{}, _ ...interface{}) error {
   158  			*version = testVersion
   159  
   160  			return nil
   161  		})
   162  
   163  	wallet, err := clef.NewWallet(clientMock, "http://127.0.0.1:8580", testAddress)
   164  	a.NoError(err)
   165  	a.NotNil(wallet)
   166  
   167  	v, err := wallet.Version()
   168  	a.NoError(err)
   169  	a.Equal(testVersion, v)
   170  }
   171  
   172  func TestReloadWallet(t *testing.T) {
   173  	t.Run("Success", testReloadWalletSuccess)
   174  }
   175  
   176  func testReloadWalletSuccess(t *testing.T) {
   177  	a := assert.New(t)
   178  
   179  	ctrl := gomock.NewController(t)
   180  	clientMock := mocks.NewMockClient(ctrl)
   181  
   182  	reloadedAddress := ethCommon.HexToAddress("0x6d573e92918124496ffefb0f273846ddb23a48c5")
   183  
   184  	var mockedCallTimes int
   185  	clientMock.EXPECT().
   186  		CallContext(gomock.Any(), gomock.Any(), "account_list", gomock.Any()).
   187  		Times(2).
   188  		DoAndReturn(func(_ context.Context, accs *[]ethCommon.Address, s string, _ ...interface{}) error {
   189  			if mockedCallTimes == 0 {
   190  				*accs = append(*accs, testAddress)
   191  			}
   192  			*accs = append(*accs, reloadedAddress)
   193  
   194  			mockedCallTimes++
   195  
   196  			return nil
   197  		})
   198  
   199  	clefAddr := "http://127.0.0.1:8580"
   200  	wallet, err := clef.NewWallet(clientMock, clefAddr, testAddress)
   201  	a.NoError(err)
   202  	a.NotNil(wallet)
   203  	a.Equal(testAddress.Hex(), wallet.PubKey().Hex())
   204  
   205  	// reload key
   206  	wallet.Reload(registry.EthereumClefWallet{
   207  		Name:           wallet.Name(),
   208  		AccountAddress: reloadedAddress.Hex(),
   209  		ClefAddress:    clefAddr,
   210  	})
   211  
   212  	a.Equal(reloadedAddress.Hex(), wallet.PubKey().Hex())
   213  }