Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(grpc): refactor CreateWallet and add RestoreWallet API endpoint #1256

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,9 @@ func MakeConfig(genDoc *genesis.Genesis, confPath, walletsDir string) (*config.C
conf.GRPC.DefaultWalletName = DefaultWalletName
conf.GRPC.WalletsDir = walletsDir

conf.WalletManager.ChainType = chainType
conf.WalletManager.WalletsDir = walletsDir

return conf, nil
}

Expand Down
47 changes: 25 additions & 22 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/pactus-project/pactus/txpool"
"github.com/pactus-project/pactus/util"
"github.com/pactus-project/pactus/util/logger"
"github.com/pactus-project/pactus/wallet"
"github.com/pactus-project/pactus/www/grpc"
"github.com/pactus-project/pactus/www/http"
"github.com/pactus-project/pactus/www/jsonrpc"
Expand All @@ -34,17 +35,18 @@ var (
)

type Config struct {
Node *NodeConfig `toml:"node"`
Store *store.Config `toml:"store"`
Network *network.Config `toml:"network"`
Sync *sync.Config `toml:"sync"`
TxPool *txpool.Config `toml:"tx_pool"`
Consensus *consensus.Config `toml:"-"`
Logger *logger.Config `toml:"logger"`
GRPC *grpc.Config `toml:"grpc"`
JSONRPC *jsonrpc.Config `toml:"jsonrpc"`
HTTP *http.Config `toml:"http"`
Nanomsg *nanomsg.Config `toml:"nanomsg"`
Node *NodeConfig `toml:"node"`
Store *store.Config `toml:"store"`
Network *network.Config `toml:"network"`
Sync *sync.Config `toml:"sync"`
TxPool *txpool.Config `toml:"tx_pool"`
Consensus *consensus.Config `toml:"-"`
Logger *logger.Config `toml:"logger"`
GRPC *grpc.Config `toml:"grpc"`
JSONRPC *jsonrpc.Config `toml:"jsonrpc"`
HTTP *http.Config `toml:"http"`
WalletManager *wallet.Config `toml:"-"`
Nanomsg *nanomsg.Config `toml:"nanomsg"`
}

type BootstrapInfo struct {
Expand Down Expand Up @@ -86,17 +88,18 @@ func (conf *NodeConfig) BasicCheck() error {

func defaultConfig() *Config {
conf := &Config{
Node: DefaultNodeConfig(),
Store: store.DefaultConfig(),
Network: network.DefaultConfig(),
Sync: sync.DefaultConfig(),
TxPool: txpool.DefaultConfig(),
Consensus: consensus.DefaultConfig(),
Logger: logger.DefaultConfig(),
GRPC: grpc.DefaultConfig(),
JSONRPC: jsonrpc.DefaultConfig(),
HTTP: http.DefaultConfig(),
Nanomsg: nanomsg.DefaultConfig(),
Node: DefaultNodeConfig(),
Store: store.DefaultConfig(),
Network: network.DefaultConfig(),
Sync: sync.DefaultConfig(),
TxPool: txpool.DefaultConfig(),
Consensus: consensus.DefaultConfig(),
Logger: logger.DefaultConfig(),
GRPC: grpc.DefaultConfig(),
JSONRPC: jsonrpc.DefaultConfig(),
HTTP: http.DefaultConfig(),
Nanomsg: nanomsg.DefaultConfig(),
WalletManager: wallet.DefaultConfig(),
}

return conf
Expand Down
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(conf.WalletManager)

syn, err := sync.NewSynchronizer(conf.Sync, valKeys, st, consMgr, net, messageCh)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion tests/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ 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(
tGenDoc, tConfigs[i],
tValKeys[i],
[]crypto.Address{
tValKeys[i][0].PublicKey().AccountAddress(),
Expand Down
15 changes: 15 additions & 0 deletions wallet/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package wallet

import (
"github.com/pactus-project/pactus/genesis"
)

type Config struct {
// private config
WalletsDir string `toml:"-"`
ChainType genesis.ChainType `toml:"-"`
}

func DefaultConfig() *Config {
return &Config{}
}
69 changes: 51 additions & 18 deletions wallet/manager.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,52 @@
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(conf *Config) *Manager {
return &Manager{
wallets: make(map[string]*Wallet),
chainType: chainType,
wallets: make(map[string]*Wallet),
chainType: conf.ChainType,
walletDirectory: conf.WalletsDir,
}
}

func (w *Manager) getWalletPath(walletName string) string {
return util.MakeAbs(filepath.Join(w.walletDirectory, walletName))
}

func (w *Manager) createWalletWithMnemonic(
walletName, mnemonic, password 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(
publicKey string,
) (string, error) {
Expand All @@ -34,33 +59,41 @@ 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(walletName, mnemonic, password); err != nil {
return "", err
}

return nil
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")
}

walletPath := util.MakeAbs(filepath.Join(w.walletDirectory, walletName))
wlt, err := Open(walletPath, true)
if err != nil {
return err
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
Loading
Loading