From bd459b6c87bbeae0f30c5bee7df1f8486c0f71ef Mon Sep 17 00:00:00 2001 From: Shawn Hsu Date: Fri, 3 May 2024 22:07:25 +0800 Subject: [PATCH] fix(grpc): update CreateWallet without mnemonic --- node/node.go | 2 +- wallet/manager.go | 56 ++++++++++++++++++++++++++++++----------- www/grpc/server_test.go | 2 +- www/grpc/wallet.go | 20 +++++---------- www/grpc/wallet_test.go | 19 -------------- www/http/http_test.go | 2 +- 6 files changed, 50 insertions(+), 51 deletions(-) diff --git a/node/node.go b/node/node.go index 2e824734f..b3d5bfdfe 100644 --- a/node/node.go +++ b/node/node.go @@ -77,7 +77,7 @@ func NewNode(genDoc *genesis.Genesis, conf *config.Config, } consMgr := consensus.NewManager(conf.Consensus, st, valKeys, rewardAddrs, messageCh) - walletMgr := wallet.NewWalletManager(chainType) + walletMgr := wallet.NewWalletManager(chainType, conf.GRPC.WalletsDir) syn, err := sync.NewSynchronizer(conf.Sync, valKeys, st, consMgr, net, messageCh) if err != nil { diff --git a/wallet/manager.go b/wallet/manager.go index bd9e79ce3..da5941391 100644 --- a/wallet/manager.go +++ b/wallet/manager.go @@ -1,25 +1,50 @@ package wallet import ( + "path/filepath" + "github.com/pactus-project/pactus/crypto" "github.com/pactus-project/pactus/crypto/bls" "github.com/pactus-project/pactus/genesis" "github.com/pactus-project/pactus/types/tx" + "github.com/pactus-project/pactus/util" "github.com/pactus-project/pactus/wallet/vault" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" ) type Manager struct { - wallets map[string]*Wallet - chainType genesis.ChainType + wallets map[string]*Wallet + chainType genesis.ChainType + walletDirectory string } -func NewWalletManager(chainType genesis.ChainType) *Manager { +func NewWalletManager(chainType genesis.ChainType, walletDir string) *Manager { return &Manager{ - wallets: make(map[string]*Wallet), - chainType: chainType, + wallets: make(map[string]*Wallet), + chainType: chainType, + walletDirectory: walletDir, + } +} + +func (w *Manager) getWalletPath(walletName string) string { + return util.MakeAbs(filepath.Join(w.walletDirectory, walletName)) +} + +func (w *Manager) createWalletWithMnemonic( + mnemonic, password, walletName string, +) error { + walletPath := w.getWalletPath(walletName) + wlt, err := Create(walletPath, mnemonic, password, w.chainType) + if err != nil { + return err + } + + if err := wlt.Save(); err != nil { + return err } + + return nil } func (w *Manager) GetValidatorAddress( @@ -34,23 +59,23 @@ func (w *Manager) GetValidatorAddress( } func (w *Manager) CreateWallet( - mnemonic, language, - password, walletPath string, -) error { - wlt, err := Create(walletPath, mnemonic, language, w.chainType) + walletName, password string, +) (string, error) { + mnemonic, err := GenerateMnemonic(128) if err != nil { - return err + return "", err } - if err := wlt.UpdatePassword("", password); err != nil { - return err + walletPath := w.getWalletPath(walletName) + if isExists := util.PathExists(walletPath); isExists { + return "", status.Errorf(codes.AlreadyExists, "wallet already exists") } - if err := wlt.Save(); err != nil { - return err + if err := w.createWalletWithMnemonic(mnemonic, password, walletName); err != nil { + return "", err } - return nil + return mnemonic, nil } func (w *Manager) LoadWallet( @@ -61,6 +86,7 @@ func (w *Manager) LoadWallet( return status.Errorf(codes.AlreadyExists, "wallet already loaded") } + walletPath := util.MakeAbs(filepath.Join(w.walletDirectory, walletName)) wlt, err := Open(walletPath, true) if err != nil { return err diff --git a/www/grpc/server_test.go b/www/grpc/server_test.go index e459f76af..6a6139a17 100644 --- a/www/grpc/server_test.go +++ b/www/grpc/server_test.go @@ -80,7 +80,7 @@ func setup(t *testing.T, conf *Config) *testData { server := NewServer( conf, mockState, mockSync, mockNet, - mockConsMgr, wallet.NewWalletManager(mockState.Genesis().ChainType()), + mockConsMgr, wallet.NewWalletManager(mockState.Genesis().ChainType(), conf.WalletsDir), ) err = server.startListening(listener) assert.NoError(t, err) diff --git a/www/grpc/wallet.go b/www/grpc/wallet.go index f8f3319d3..2c5a6e7b7 100644 --- a/www/grpc/wallet.go +++ b/www/grpc/wallet.go @@ -3,10 +3,8 @@ package grpc import ( "context" "fmt" - "path/filepath" "github.com/pactus-project/pactus/crypto" - "github.com/pactus-project/pactus/util" "github.com/pactus-project/pactus/wallet" pactus "github.com/pactus-project/pactus/www/grpc/gen/go" "google.golang.org/grpc/codes" @@ -28,10 +26,6 @@ func newWalletServer(server *Server, manager *wallet.Manager) *walletServer { } } -func (s *walletServer) walletPath(name string) string { - return util.MakeAbs(filepath.Join(s.config.WalletsDir, name)) -} - func (s *walletServer) mapHistoryInfo(hi []wallet.HistoryInfo) []*pactus.HistoryInfo { historyInfo := make([]*pactus.HistoryInfo, 0) for _, hi := range hi { @@ -67,24 +61,22 @@ func (s *walletServer) CreateWallet(_ context.Context, return nil, fmt.Errorf("wallet name is required") } - if err := s.walletManager.CreateWallet( - req.Mnemonic, req.Language, - req.Password, s.walletPath(req.WalletName), - ); err != nil { + mnemonic, err := s.walletManager.CreateWallet( + req.WalletName, req.Password, + ) + if err != nil { return nil, err } return &pactus.CreateWalletResponse{ - WalletName: req.WalletName, + Mnemonic: mnemonic, }, nil } func (s *walletServer) LoadWallet(_ context.Context, req *pactus.LoadWalletRequest, ) (*pactus.LoadWalletResponse, error) { - if err := s.walletManager.LoadWallet( - req.WalletName, s.walletPath(req.WalletName), - ); err != nil { + if err := s.walletManager.LoadWallet(req.WalletName); err != nil { return nil, err } diff --git a/www/grpc/wallet_test.go b/www/grpc/wallet_test.go index a83cdaed8..487b4f753 100644 --- a/www/grpc/wallet_test.go +++ b/www/grpc/wallet_test.go @@ -6,7 +6,6 @@ import ( "github.com/pactus-project/pactus/crypto" "github.com/pactus-project/pactus/types/tx" - "github.com/pactus-project/pactus/wallet" pactus "github.com/pactus-project/pactus/www/grpc/gen/go" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -21,11 +20,9 @@ func TestDisableWallet(t *testing.T) { td := setup(t, conf) conn, client := td.walletClient(t) - mnemonic, _ := wallet.GenerateMnemonic(128) res, err := client.CreateWallet(context.Background(), &pactus.CreateWalletRequest{ WalletName: "TestWallet", - Mnemonic: mnemonic, }) assert.ErrorIs(t, err, status.Error(codes.Unimplemented, "unknown service pactus.Wallet")) assert.Nil(t, res) @@ -41,44 +38,28 @@ func TestCreateWallet(t *testing.T) { td := setup(t, conf) conn, client := td.walletClient(t) - t.Run("Invalid mnemonic", func(t *testing.T) { - res, err := client.CreateWallet(context.Background(), - &pactus.CreateWalletRequest{ - WalletName: "test", - Mnemonic: "not valid", - }) - assert.Error(t, err) - assert.Nil(t, res) - }) - t.Run("No name, should return an error", func(t *testing.T) { - mnemonic, _ := wallet.GenerateMnemonic(128) res, err := client.CreateWallet(context.Background(), &pactus.CreateWalletRequest{ WalletName: "", - Mnemonic: mnemonic, }) assert.Error(t, err) assert.Nil(t, res) }) t.Run("Bad name, should return an error", func(t *testing.T) { - mnemonic, _ := wallet.GenerateMnemonic(128) res, err := client.CreateWallet(context.Background(), &pactus.CreateWalletRequest{ WalletName: "..", - Mnemonic: mnemonic, }) assert.Error(t, err) assert.Nil(t, res) }) t.Run("Should create wallet", func(t *testing.T) { - mnemonic, _ := wallet.GenerateMnemonic(128) res, err := client.CreateWallet(context.Background(), &pactus.CreateWalletRequest{ WalletName: "test", - Mnemonic: mnemonic, }) assert.NoError(t, err) assert.NotNil(t, res) diff --git a/www/http/http_test.go b/www/http/http_test.go index de817c229..e4fbff89c 100644 --- a/www/http/http_test.go +++ b/www/http/http_test.go @@ -63,7 +63,7 @@ func setup(t *testing.T) *testData { gRPCServer := grpc.NewServer( grpcConf, mockState, mockSync, mockNet, - mockConsMgr, wallet.NewWalletManager(mockState.Genesis().ChainType()), + mockConsMgr, wallet.NewWalletManager(mockState.Genesis().ChainType(), ""), ) assert.NoError(t, gRPCServer.StartServer())