Skip to content

Commit

Permalink
feat(grpc): add RestoreWallet implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
ambersun1234 committed May 5, 2024
1 parent bd459b6 commit c3f47d1
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 10 deletions.
2 changes: 1 addition & 1 deletion cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ func StartNode(workingDir string, passwordFetcher func(*wallet.Wallet) (string,
return nil, nil, err
}

nodeInstance, err := node.NewNode(gen, conf, valKeys, rewardAddrs)
nodeInstance, err := node.NewNode(walletsDir, gen, conf, valKeys, rewardAddrs)
if err != nil {
return nil, nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type Node struct {
nanomsg *nanomsg.Server
}

func NewNode(genDoc *genesis.Genesis, conf *config.Config,
func NewNode(walletDir string, genDoc *genesis.Genesis, conf *config.Config,
valKeys []*bls.ValidatorKey, rewardAddrs []crypto.Address,
) (*Node, error) {
// Initialize the logger
Expand Down 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, conf.GRPC.WalletsDir)
walletMgr := wallet.NewWalletManager(chainType, walletDir)

syn, err := sync.NewSynchronizer(conf.Sync, valKeys, st, consMgr, net, messageCh)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion node/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestRunningNode(t *testing.T) {

valKeys := []*bls.ValidatorKey{ts.RandValKey(), ts.RandValKey()}
rewardAddrs := []crypto.Address{ts.RandAccAddress(), ts.RandAccAddress()}
n, err := NewNode(gen, conf, valKeys, rewardAddrs)
n, err := NewNode(util.TempDirPath(), gen, conf, valKeys, rewardAddrs)

require.NoError(t, err)
assert.Equal(t, n.state.LastBlockHash(), hash.UndefHash)
Expand Down
4 changes: 3 additions & 1 deletion tests/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,9 @@ func TestMain(m *testing.M) {
tGenDoc = genesis.MakeGenesis(util.Now(), accs, vals, params)

for i := 0; i < tTotalNodes; i++ {
tNodes[i], _ = node.NewNode(tGenDoc, tConfigs[i],
tNodes[i], _ = node.NewNode(
util.TempDirPath(),
tGenDoc, tConfigs[i],
tValKeys[i],
[]crypto.Address{
tValKeys[i][0].PublicKey().AccountAddress(),
Expand Down
17 changes: 12 additions & 5 deletions wallet/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (w *Manager) getWalletPath(walletName string) string {
}

func (w *Manager) createWalletWithMnemonic(
mnemonic, password, walletName string,
walletName, mnemonic, password string,
) error {
walletPath := w.getWalletPath(walletName)
wlt, err := Create(walletPath, mnemonic, password, w.chainType)
Expand Down Expand Up @@ -71,16 +71,23 @@ func (w *Manager) CreateWallet(
return "", status.Errorf(codes.AlreadyExists, "wallet already exists")
}

if err := w.createWalletWithMnemonic(mnemonic, password, walletName); err != nil {
if err := w.createWalletWithMnemonic(walletName, mnemonic, password); err != nil {
return "", err
}

return mnemonic, nil
}

func (w *Manager) LoadWallet(
walletName, walletPath string,
) error {
func (w *Manager) RestoreWallet(walletName, mnemonic, password string) error {
walletPath := w.getWalletPath(walletName)
if isExists := util.PathExists(walletPath); isExists {
return status.Errorf(codes.AlreadyExists, "wallet already exists")
}

return w.createWalletWithMnemonic(walletName, mnemonic, password)
}

func (w *Manager) LoadWallet(walletName string) error {
if _, ok := w.wallets[walletName]; ok {
// TODO: define special codes for errors
return status.Errorf(codes.AlreadyExists, "wallet already loaded")
Expand Down
21 changes: 21 additions & 0 deletions www/grpc/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,27 @@ func (s *walletServer) CreateWallet(_ context.Context,
}, nil
}

func (s *walletServer) RestoreWallet(_ context.Context,
req *pactus.RestoreWalletRequest,
) (*pactus.RestoreWalletResponse, error) {
if req.WalletName == "" {
return nil, fmt.Errorf("wallet name is required")
}
if req.Mnemonic == "" {
return nil, fmt.Errorf("mnemonic is required")
}

if err := s.walletManager.RestoreWallet(
req.WalletName, req.Mnemonic, req.Password,
); err != nil {
return nil, err
}

return &pactus.RestoreWalletResponse{
WalletName: req.WalletName,
}, nil
}

func (s *walletServer) LoadWallet(_ context.Context,
req *pactus.LoadWalletRequest,
) (*pactus.LoadWalletResponse, error) {
Expand Down
41 changes: 41 additions & 0 deletions www/grpc/wallet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ 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 @@ -31,6 +32,46 @@ func TestDisableWallet(t *testing.T) {
td.StopServer()
}

func TestRestoreWallet(t *testing.T) {
config := testConfig()
config.EnableWallet = true

td := setup(t, config)
conn, client := td.walletClient(t)

t.Run("should return error if no wallet name provided", func(t *testing.T) {
res, err := client.RestoreWallet(context.Background(),
&pactus.RestoreWalletRequest{})
assert.Error(t, err)
assert.Nil(t, res)
})

t.Run("should return error if no mnemonic provided", func(t *testing.T) {
res, err := client.RestoreWallet(context.Background(),
&pactus.RestoreWalletRequest{
WalletName: "test",
})
assert.Error(t, err)
assert.Nil(t, res)
})

t.Run("should restore wallet", func(t *testing.T) {
mnemonic, err := wallet.GenerateMnemonic(128)
assert.NoError(t, err)

res, err := client.RestoreWallet(context.Background(),
&pactus.RestoreWalletRequest{
WalletName: "test",
Mnemonic: mnemonic,
})
assert.NoError(t, err)
assert.NotNil(t, res)
})

assert.Nil(t, conn.Close(), "Error closing connection")
td.StopServer()
}

func TestCreateWallet(t *testing.T) {
conf := testConfig()
conf.EnableWallet = true
Expand Down

0 comments on commit c3f47d1

Please sign in to comment.