Skip to content

Commit

Permalink
fix: fixing wallet-cli issues (#686)
Browse files Browse the repository at this point in the history
  • Loading branch information
themantre authored Sep 10, 2023
1 parent 673e5cc commit af851a5
Show file tree
Hide file tree
Showing 15 changed files with 223 additions and 184 deletions.
14 changes: 12 additions & 2 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,13 +360,23 @@ func StartNode(workingDir string, passwordFetcher func(*wallet.Wallet) (string,

// Now, attempt to restore the config file with the number of validators from the old config.
switch gen.ChainType() {
case genesis.Mainnet:
panic("not yet implemented!")

case genesis.Testnet:
err = config.SaveTestnetConfig(confPath, confBack.Node.NumValidators)
if err != nil {
return nil, nil, err
}
case genesis.Mainnet:
panic("not yet implemented!")

case genesis.Localnet:
err = config.SaveLocalnetConfig(confPath, confBack.Node.NumValidators)
if err != nil {
return nil, nil, err
}

default:
return nil, nil, fmt.Errorf("invalid chain type")
}

PrintSuccessMsgf("Config restored to the default values")
Expand Down
4 changes: 2 additions & 2 deletions cmd/daemon/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import (
"github.com/spf13/cobra"
)

// BuildInitCmd builds the init command for the Pactus blockchain.
// buildInitCmd builds a sub-command to initialized the Pactus blockchain node.
func buildInitCmd(parentCmd *cobra.Command) {
initCmd := &cobra.Command{
Use: "init",
Short: "Initialize the Pactus blockchain",
Short: "Initialize the Pactus blockchain node",
}
parentCmd.AddCommand(initCmd)
workingDirOpt := initCmd.Flags().StringP("working-dir", "w",
Expand Down
17 changes: 9 additions & 8 deletions cmd/daemon/main.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
package main

import (
"fmt"

"github.com/pactus-project/pactus/cmd"
"github.com/spf13/cobra"
)

func main() {
rootCmd := &cobra.Command{
Use: "pactus-daemon",
Short: "Pactus daemon",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("use --help")
},
Use: "pactus-daemon",
Short: "Pactus daemon",
CompletionOptions: cobra.CompletionOptions{HiddenDefaultCmd: true},
}

// Hide the "help" sub-command
rootCmd.SetHelpCommand(&cobra.Command{Hidden: true})

buildVersionCmd(rootCmd)
buildInitCmd(rootCmd)
buildStartCmd(rootCmd)

err := rootCmd.Execute()
if err != nil {
panic(err)
cmd.PrintErrorMsgf("%s", err)
}
}
4 changes: 2 additions & 2 deletions cmd/daemon/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import (
"github.com/spf13/cobra"
)

// Start starts the pactus node.
// buildStartCmd builds a sub-command to starts the Pactus blockchain node.
func buildStartCmd(parentCmd *cobra.Command) {
startCmd := &cobra.Command{
Use: "start",
Short: "Start the Pactus blockchain",
Short: "Start the Pactus blockchain node",
}

parentCmd.AddCommand(startCmd)
Expand Down
83 changes: 42 additions & 41 deletions cmd/wallet/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import (
"github.com/spf13/cobra"
)

// buildAllAddrCmd builds all sub-commands related to addresses.
func buildAllAddrCmd(parentCmd *cobra.Command) {
addrCmd := &cobra.Command{
Use: "address",
Short: "Manage address book",
Short: "Manage the address book",
}

parentCmd.AddCommand(addrCmd)
Expand All @@ -25,19 +26,19 @@ func buildAllAddrCmd(parentCmd *cobra.Command) {
buildSetLabelCmd(addrCmd)
}

// AllAddresses lists all the wallet addresses.
// buildAllAddressesCmd builds a command to list all addresses from the wallet.
func buildAllAddressesCmd(parentCmd *cobra.Command) {
allAddressCmd := &cobra.Command{
Use: "all",
Short: "Show all addresses",
Short: "Display all stored addresses",
}
parentCmd.AddCommand(allAddressCmd)

balanceOpt := allAddressCmd.Flags().Bool("balance",
false, "show account balance")
false, "Display the account balance for each address")

stakeOpt := allAddressCmd.Flags().Bool("stake",
false, "show validator stake")
false, "Display the validator stake for each address")

allAddressCmd.Run = func(_ *cobra.Command, _ []string) {
wallet, err := openWallet()
Expand Down Expand Up @@ -67,11 +68,11 @@ func buildAllAddressesCmd(parentCmd *cobra.Command) {
}
}

// NewAddress creates a new address.
// buildNewAddressCmd builds a command for creating a new wallet address.
func buildNewAddressCmd(parentCmd *cobra.Command) {
newAddressCmd := &cobra.Command{
Use: "new",
Short: "Creating a new address",
Short: "Create a new address",
}
parentCmd.AddCommand(newAddressCmd)

Expand All @@ -91,71 +92,75 @@ func buildNewAddressCmd(parentCmd *cobra.Command) {
}
}

// Balance shows the balance of an address.
// buildBalanceCmd builds a command to display the balance of a given address.
func buildBalanceCmd(parentCmd *cobra.Command) {
balanceCmd := &cobra.Command{
Use: "balance",
Short: "Show the balance of an address",
Use: "balance [flags] <ADDRESS>",
Short: "Display the balance of an address",
Args: cobra.ExactArgs(1),
}
parentCmd.AddCommand(balanceCmd)

addrArg := addAddressArg(parentCmd)
balanceCmd.Run = func(_ *cobra.Command, args []string) {
addr := args[0]

balanceCmd.Run = func(_ *cobra.Command, _ []string) {
wallet, err := openWallet()
cmd.FatalErrorCheck(err)

cmd.PrintLine()
balance, err := wallet.Balance(*addrArg)
balance, err := wallet.Balance(addr)
cmd.FatalErrorCheck(err)

stake, err := wallet.Stake(*addrArg)
stake, err := wallet.Stake(addr)
cmd.FatalErrorCheck(err)

cmd.PrintInfoMsgf("balance: %v\tstake: %v",
util.ChangeToCoin(balance), util.ChangeToCoin(stake))
}
}

// PrivateKey returns the private key of an address.
// buildPrivateKeyCmd builds a command to show the private key of a given address.
func buildPrivateKeyCmd(parentCmd *cobra.Command) {
privateKeyCmd := &cobra.Command{
Use: "priv",
Short: "Show the private key of an address",
Use: "priv [flags] <ADDRESS>",
Short: "Display the private key for a specified address",
Args: cobra.ExactArgs(1),
}
parentCmd.AddCommand(privateKeyCmd)

addrArg := addAddressArg(parentCmd)
passOpt := addPasswordOption(parentCmd)
passOpt := addPasswordOption(privateKeyCmd)

privateKeyCmd.Run = func(_ *cobra.Command, args []string) {
addr := args[0]

privateKeyCmd.Run = func(_ *cobra.Command, _ []string) {
wallet, err := openWallet()
cmd.FatalErrorCheck(err)

password := getPassword(wallet, *passOpt)
prv, err := wallet.PrivateKey(password, *addrArg)
prv, err := wallet.PrivateKey(password, addr)
cmd.FatalErrorCheck(err)

cmd.PrintLine()
cmd.PrintWarnMsgf("Private Key: %v", prv)
}
}

// PublicKey returns the public key of an address.
// buildPublicKeyCmd builds a command to show the public key of a given address.
func buildPublicKeyCmd(parentCmd *cobra.Command) {
publicKeyCmd := &cobra.Command{
Use: "pub",
Short: "Show the public key of an address",
Use: "pub [flags] <ADDRESS>",
Short: "Display the public key for a specified address",
Args: cobra.ExactArgs(1),
}
parentCmd.AddCommand(publicKeyCmd)

addrArg := addAddressArg(parentCmd)
publicKeyCmd.Run = func(_ *cobra.Command, args []string) {
addr := args[0]

publicKeyCmd.Run = func(_ *cobra.Command, _ []string) {
wallet, err := openWallet()
cmd.FatalErrorCheck(err)

info := wallet.AddressInfo(*addrArg)
info := wallet.AddressInfo(addr)
if info == nil {
cmd.PrintErrorMsgf("Address not found")
return
Expand All @@ -169,15 +174,15 @@ func buildPublicKeyCmd(parentCmd *cobra.Command) {
}
}

// ImportPrivateKey imports a private key into the wallet.
// buildImportPrivateKeyCmd build a command to import a private key into the wallet.
func buildImportPrivateKeyCmd(parentCmd *cobra.Command) {
importPrivateKeyCmd := &cobra.Command{
Use: "import",
Short: "Import a private key into wallet",
}
parentCmd.AddCommand(importPrivateKeyCmd)

passOpt := addPasswordOption(parentCmd)
passOpt := addPasswordOption(importPrivateKeyCmd)

importPrivateKeyCmd.Run = func(_ *cobra.Command, _ []string) {
prvStr := cmd.PromptInput("Private Key")
Expand All @@ -201,24 +206,25 @@ func buildImportPrivateKeyCmd(parentCmd *cobra.Command) {
}
}

// SetLabel set label for the address.
// buildSetLabelCmd build a command to set or update the label for an address.
func buildSetLabelCmd(parentCmd *cobra.Command) {
setLabelCmd := &cobra.Command{
Use: "label",
Short: "Set label for the an address",
Use: "label [flags] <ADDRESS>",
Short: "Assign or update a label for a specific address",
Args: cobra.ExactArgs(1),
}
parentCmd.AddCommand(setLabelCmd)

addrArg := addAddressArg(parentCmd)
setLabelCmd.Run = func(c *cobra.Command, args []string) {
addr := args[0]

setLabelCmd.Run = func(_ *cobra.Command, _ []string) {
wallet, err := openWallet()
cmd.FatalErrorCheck(err)

oldLabel := wallet.Label(*addrArg)
oldLabel := wallet.Label(addr)
newLabel := cmd.PromptInputWithSuggestion("Label", oldLabel)

err = wallet.SetLabel(*addrArg, newLabel)
err = wallet.SetLabel(addr, newLabel)
cmd.FatalErrorCheck(err)

err = wallet.Save()
Expand All @@ -228,8 +234,3 @@ func buildSetLabelCmd(parentCmd *cobra.Command) {
cmd.PrintSuccessMsgf("Label set successfully")
}
}

func addAddressArg(c *cobra.Command) *string {
return c.Flags().String("ADDRESS",
"", "address string")
}
24 changes: 13 additions & 11 deletions cmd/wallet/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@ import (
"github.com/spf13/cobra"
)

// Generate creates a new wallet.
func buildGenerateCmd(parentCmd *cobra.Command) {
// buildCreateCmd builds a command to create a new wallet.
func buildCreateCmd(parentCmd *cobra.Command) {
generateCmd := &cobra.Command{
Use: "create",
Short: "Create a new wallet",
}
parentCmd.AddCommand(generateCmd)

testnetOpt := generateCmd.Flags().Bool("testnet", false, "creating wallet for testnet")
entropyOpt := generateCmd.Flags().Int("entropy", 128, "Entropy bit length")
testnetOpt := generateCmd.Flags().Bool("testnet", false,
"Create a wallet for the testnet environment")
entropyOpt := generateCmd.Flags().Int("entropy", 128,
"Specify the entropy bit length")

generateCmd.Run = func(_ *cobra.Command, _ []string) {
password := cmd.PromptPassword("Password", true)
Expand All @@ -26,28 +28,28 @@ func buildGenerateCmd(parentCmd *cobra.Command) {
if *testnetOpt {
network = genesis.Testnet
}
wallet, err := wallet.Create(*pathArg, mnemonic, password, network)
wallet, err := wallet.Create(*pathOpt, mnemonic, password, network)
cmd.FatalErrorCheck(err)

err = wallet.Save()
cmd.FatalErrorCheck(err)

cmd.PrintLine()
cmd.PrintSuccessMsgf("Wallet created successfully at: %s", wallet.Path())
cmd.PrintInfoMsgf("Seed: \"%v\"", mnemonic)
cmd.PrintSuccessMsgf("Your wallet was successfully created at: %s", wallet.Path())
cmd.PrintInfoMsgf("Seed phrase: \"%v\"", mnemonic)
cmd.PrintWarnMsgf("Please keep your seed in a safe place; " +
"if you lose it, you will not be able to restore your wallet.")
}
}

// ChangePassword updates the wallet password.
// buildChangePasswordCmd builds a command to update the wallet's password.
func buildChangePasswordCmd(parentCmd *cobra.Command) {
changePasswordCmd := &cobra.Command{
Use: "password",
Short: "Change wallet password",
Short: "Change the wallet's password",
}
parentCmd.AddCommand(changePasswordCmd)
passOpt := addPasswordOption(parentCmd)
passOpt := addPasswordOption(changePasswordCmd)

changePasswordCmd.Run = func(_ *cobra.Command, _ []string) {
wallet, err := openWallet()
Expand All @@ -63,6 +65,6 @@ func buildChangePasswordCmd(parentCmd *cobra.Command) {
cmd.FatalErrorCheck(err)

cmd.PrintLine()
cmd.PrintWarnMsgf("Wallet password updated")
cmd.PrintWarnMsgf("Your wallet password successfully updated.")
}
}
Loading

0 comments on commit af851a5

Please sign in to comment.