Skip to content

Commit

Permalink
feat(grpc): refactor CreateWallet and add RestoreWallet API endpoint (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ambersun1234 authored May 5, 2024
1 parent 6b14f5a commit a96b7ac
Show file tree
Hide file tree
Showing 36 changed files with 3,692 additions and 938 deletions.
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

0 comments on commit a96b7ac

Please sign in to comment.