Skip to content

Commit

Permalink
feat(cmd): pactus-wallet add info commands
Browse files Browse the repository at this point in the history
  • Loading branch information
maxipaz committed Sep 2, 2024
1 parent 550d5b5 commit 6437f50
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
90 changes: 90 additions & 0 deletions cmd/wallet/info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package main

import (
"time"

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

// buildAllInfoCmd builds all sub-commands related to the wallet information.
func buildAllInfoCmd(parentCmd *cobra.Command) {
infoCmd := &cobra.Command{
Use: "info",
Short: "retrieving the wallet information.",
}

parentCmd.AddCommand(infoCmd)
buildWalletVersionInfoCmd(infoCmd)
buildCreationTimeInfoCmd(infoCmd)
buildIsEncryptedInfoCmd(infoCmd)
buildNetworkInfoCmd(infoCmd)
}

// buildWalletVersionInfoCmd builds a command for retrieving the current wallet's version.
func buildWalletVersionInfoCmd(parentCmd *cobra.Command) {
walletVersionInfoCmd := &cobra.Command{
Use: "version",
Short: "display the current version of the wallet.",
}

parentCmd.AddCommand(walletVersionInfoCmd)

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

cmd.PrintInfoMsgf("version: %d", wlt.GetVersion())
}
}

// buildCreationTimeInfoCmd builds a command for retrieving the wallet's creation time.
func buildCreationTimeInfoCmd(parentCmd *cobra.Command) {
walletCreationTimeInfoCmd := &cobra.Command{
Use: "created-at",
Short: "display the creation time of the wallet.",
}

parentCmd.AddCommand(walletCreationTimeInfoCmd)

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

cmd.PrintInfoMsgf("created at: %s", wlt.GetCreationTime().Format(time.RFC3339))
}
}

// buildIsEncryptedInfoCmd builds a command for knowing if the wallet is encrypted or not.
func buildIsEncryptedInfoCmd(parentCmd *cobra.Command) {
walletIsEncryptedInfoCmd := &cobra.Command{
Use: "is-encrypted",
Short: "display if the wallet is encrypted or not.",
}

parentCmd.AddCommand(walletIsEncryptedInfoCmd)

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

cmd.PrintInfoMsgf("is encrtypted: %t", wlt.IsEncrypted())
}
}

// buildNetworkInfoCmd builds a command for retrieving the wallet's network.
func buildNetworkInfoCmd(parentCmd *cobra.Command) {
walletNetworkInfoCmd := &cobra.Command{
Use: "network",
Short: "display the network of the wallet.",
}

parentCmd.AddCommand(walletNetworkInfoCmd)

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

cmd.PrintInfoMsgf("network: %s", wlt.GetNetwork().String())
}
}
1 change: 1 addition & 0 deletions cmd/wallet/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func main() {
buildAllTransactionCmd(rootCmd)
buildAllAddrCmd(rootCmd)
buildAllHistoryCmd(rootCmd)
buildAllInfoCmd(rootCmd)

err := rootCmd.Execute()
if err != nil {
Expand Down
12 changes: 12 additions & 0 deletions wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -502,3 +502,15 @@ func (w *Wallet) SignMessage(password, addr, msg string) (string, error) {

return prv.Sign([]byte(msg)).String(), nil
}

func (w *Wallet) GetVersion() int {
return w.store.Version
}

func (w *Wallet) GetCreationTime() time.Time {
return w.store.CreatedAt
}

func (w *Wallet) GetNetwork() genesis.ChainType {
return w.store.Network
}

0 comments on commit 6437f50

Please sign in to comment.