Skip to content

Commit

Permalink
feat(grpc): add RestoreWallet API
Browse files Browse the repository at this point in the history
  • Loading branch information
ambersun1234 committed Apr 30, 2024
1 parent 68a18dd commit 2ff2ece
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 9 deletions.
16 changes: 11 additions & 5 deletions wallet/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (w *Manager) walletExists(walletName string) bool {
}

func (w *Manager) createWalletWithMnemonic(
mnemonic, password, walletName string,
walletName, mnemonic, password string,
) error {
walletPath := util.MakeAbs(filepath.Join(w.walletDirectory, walletName))
wlt, err := Create(walletPath, mnemonic, password, w.chainType)
Expand Down Expand Up @@ -76,16 +76,22 @@ func (w *Manager) CreateWallet(
return "", status.Errorf(codes.AlreadyExists, "wallet already exists")

Check warning on line 76 in wallet/manager.go

View check run for this annotation

Codecov / codecov/patch

wallet/manager.go#L75-L76

Added lines #L75 - L76 were not covered by tests
}

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

Check warning on line 80 in wallet/manager.go

View check run for this annotation

Codecov / codecov/patch

wallet/manager.go#L79-L80

Added lines #L79 - L80 were not covered by tests
}

return mnemonic, nil

Check warning on line 83 in wallet/manager.go

View check run for this annotation

Codecov / codecov/patch

wallet/manager.go#L83

Added line #L83 was not covered by tests
}

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

Check warning on line 88 in wallet/manager.go

View check run for this annotation

Codecov / codecov/patch

wallet/manager.go#L87-L88

Added lines #L87 - L88 were not covered by tests
}

return w.createWalletWithMnemonic(walletName, mnemonic, password)

Check warning on line 91 in wallet/manager.go

View check run for this annotation

Codecov / codecov/patch

wallet/manager.go#L91

Added line #L91 was not covered by tests
}

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
3 changes: 3 additions & 0 deletions www/grpc/buf/grpc-gateway.config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ http:
- selector: pactus.Wallet.CreateWallet
get: "/pactus/wallet/create_wallet"

- selector: pactus.Wallet.RestoreWallet
get: "/pactus/wallet/restore_wallet"

- selector: pactus.Wallet.LoadWallet
get: "/pactus/wallet/load_wallet"

Expand Down
6 changes: 2 additions & 4 deletions www/grpc/statik/statik.go

Large diffs are not rendered by default.

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

Check warning on line 89 in www/grpc/wallet.go

View check run for this annotation

Codecov / codecov/patch

www/grpc/wallet.go#L89

Added line #L89 was not covered by tests
}

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 2ff2ece

Please sign in to comment.