Skip to content

Commit

Permalink
feat(grpc): remove mnemonic from create wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
ambersun1234 committed Apr 30, 2024
1 parent 347ccef commit 68a18dd
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 55 deletions.
2 changes: 1 addition & 1 deletion node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
61 changes: 46 additions & 15 deletions wallet/manager.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,56 @@
package wallet

import (
"os"
"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) walletExists(walletName string) bool {
walletPath := util.MakeAbs(filepath.Join(w.walletDirectory, walletName))
if _, err := os.Stat(walletPath); err != nil {
return false
}

return true
}

func (w *Manager) createWalletWithMnemonic(
mnemonic, password, walletName string,
) error {
walletPath := util.MakeAbs(filepath.Join(w.walletDirectory, 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(
Expand All @@ -34,23 +65,22 @@ func (w *Manager) GetValidatorAddress(
}

func (w *Manager) CreateWallet(
mnemonic, language,
password, walletPath string,
) error {
wlt, err := Create(walletPath, mnemonic, language, w.chainType)
password, walletName string,
) (string, error) {
mnemonic, err := GenerateMnemonic(128)
if err != nil {
return err
return "", err
}

if err := wlt.UpdatePassword("", password); err != nil {
return err
if isExists := w.walletExists(walletName); 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(
Expand All @@ -61,6 +91,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
Expand Down
6 changes: 2 additions & 4 deletions www/grpc/proto/wallet.proto
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,6 @@ message RestoreWalletResponse {
message CreateWalletRequest {
// Name of the new wallet.
string wallet_name = 1;
// Mnemonic for wallet recovery.
string mnemonic = 2;
// Language for the mnemonic.
string language = 3;
// Password for securing the wallet.
Expand All @@ -142,8 +140,8 @@ message CreateWalletRequest {

// Response message containing the name of the created wallet.
message CreateWalletResponse {
// Name of the created wallet.
string wallet_name = 1;
// Menomic for wallet recovery.
string mnemonic = 2;
}

// Request message for loading an existing wallet.
Expand Down
2 changes: 1 addition & 1 deletion www/grpc/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
20 changes: 6 additions & 14 deletions www/grpc/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand Down Expand Up @@ -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.Password, req.WalletName,
)
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
}

Expand Down
19 changes: 0 additions & 19 deletions www/grpc/wallet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion www/http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down

0 comments on commit 68a18dd

Please sign in to comment.