From 88482d2b3c61c642949072d440420a6dfb6b3449 Mon Sep 17 00:00:00 2001 From: maxipaz Date: Mon, 2 Sep 2024 14:05:07 +0200 Subject: [PATCH] feat(cmd): pactus-wallet add info commands --- cmd/wallet/history.go | 2 +- cmd/wallet/info.go | 28 ++++++++++++++++++++++++++++ cmd/wallet/main.go | 1 + wallet/history_test.go | 4 ++-- wallet/manager.go | 2 +- wallet/wallet.go | 14 +++++++++++++- 6 files changed, 46 insertions(+), 5 deletions(-) create mode 100644 cmd/wallet/info.go diff --git a/cmd/wallet/history.go b/cmd/wallet/history.go index 96115940a..4fd8306c7 100644 --- a/cmd/wallet/history.go +++ b/cmd/wallet/history.go @@ -63,7 +63,7 @@ func buildShowHistoryCmd(parentCmd *cobra.Command) { wlt, err := openWallet() cmd.FatalErrorCheck(err) - history := wlt.GetHistory(addr) + history := wlt.History(addr) for i, h := range history { if h.Time != nil { cmd.PrintInfoMsgf("%d %v %v %v %s\t%v", diff --git a/cmd/wallet/info.go b/cmd/wallet/info.go new file mode 100644 index 000000000..1a659396d --- /dev/null +++ b/cmd/wallet/info.go @@ -0,0 +1,28 @@ +package main + +import ( + "time" + + "github.com/pactus-project/pactus/cmd" + "github.com/spf13/cobra" +) + +// buildInfoCmd builds all sub-commands related to the wallet information. +func buildInfoCmd(parentCmd *cobra.Command) { + infoCmd := &cobra.Command{ + Use: "info", + Short: "retrieving the wallet information.", + } + + parentCmd.AddCommand(infoCmd) + + infoCmd.Run = func(_ *cobra.Command, _ []string) { + wlt, err := openWallet() + cmd.FatalErrorCheck(err) + + cmd.PrintInfoMsgf("version: %d", wlt.Version()) + cmd.PrintInfoMsgf("created at: %s", wlt.CreationTime().Format(time.RFC3339)) + cmd.PrintInfoMsgf("is encrtypted: %t", wlt.IsEncrypted()) + cmd.PrintInfoMsgf("network: %s", wlt.Network().String()) + } +} diff --git a/cmd/wallet/main.go b/cmd/wallet/main.go index 6da00a9fe..9187e4132 100644 --- a/cmd/wallet/main.go +++ b/cmd/wallet/main.go @@ -63,6 +63,7 @@ func main() { buildAllTransactionCmd(rootCmd) buildAllAddrCmd(rootCmd) buildAllHistoryCmd(rootCmd) + buildInfoCmd(rootCmd) err := rootCmd.Execute() if err != nil { diff --git a/wallet/history_test.go b/wallet/history_test.go index b82ec334d..c314cb01d 100644 --- a/wallet/history_test.go +++ b/wallet/history_test.go @@ -10,7 +10,7 @@ func TestGetHistory(t *testing.T) { td := setup(t) defer td.Close() - history := td.wallet.GetHistory(td.RandAccAddress().String()) + history := td.wallet.History(td.RandAccAddress().String()) assert.Empty(t, history) } @@ -23,6 +23,6 @@ func TestAddDuplicatedTrx(t *testing.T) { assert.NoError(t, err) assert.Equal(t, trx.ID().String(), id) - history := td.wallet.GetHistory(trx.Payload().Signer().String()) + history := td.wallet.History(trx.Payload().Signer().String()) assert.Equal(t, id, history[0].TxID) } diff --git a/wallet/manager.go b/wallet/manager.go index 2361d9ff8..37d2c4b79 100644 --- a/wallet/manager.go +++ b/wallet/manager.go @@ -200,7 +200,7 @@ func (wm *Manager) AddressHistory( return nil, status.Errorf(codes.NotFound, "wallet is not loaded") } - return wlt.GetHistory(address), nil + return wlt.History(address), nil } func (wm *Manager) SignMessage(walletName, password, addr, msg string) (string, error) { diff --git a/wallet/wallet.go b/wallet/wallet.go index 0f27792be..bafa18e9a 100644 --- a/wallet/wallet.go +++ b/wallet/wallet.go @@ -490,7 +490,7 @@ func (w *Wallet) AddTransaction(id tx.ID) error { return nil } -func (w *Wallet) GetHistory(addr string) []HistoryInfo { +func (w *Wallet) History(addr string) []HistoryInfo { return w.store.History.getAddrHistory(addr) } @@ -502,3 +502,15 @@ func (w *Wallet) SignMessage(password, addr, msg string) (string, error) { return prv.Sign([]byte(msg)).String(), nil } + +func (w *Wallet) Version() int { + return w.store.Version +} + +func (w *Wallet) CreationTime() time.Time { + return w.store.CreatedAt +} + +func (w *Wallet) Network() genesis.ChainType { + return w.store.Network +}