From 1c59a2d430503be9d164844ac7311a84ac5d0ca0 Mon Sep 17 00:00:00 2001 From: Jemilu Mohammed Date: Thu, 19 Oct 2023 22:56:41 +0000 Subject: [PATCH 1/9] feat: introduce address_type flag to support creating both validator and bls_account address types via the cli --- cmd/wallet/address.go | 23 ++++++++++++++++++++++- cmd/wallet/main.go | 2 ++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/cmd/wallet/address.go b/cmd/wallet/address.go index a9568bcad..e1d5ae58b 100644 --- a/cmd/wallet/address.go +++ b/cmd/wallet/address.go @@ -76,12 +76,33 @@ func buildNewAddressCmd(parentCmd *cobra.Command) { } parentCmd.AddCommand(newAddressCmd) + blsAddressOption := "bls_account" + validatorAddressOption := "validator" + + addressType := newAddressCmd.Flags().String("address_type", + blsAddressOption, "Address type to create") + newAddressCmd.Run = func(_ *cobra.Command, _ []string) { + var addr string + var err error + label := cmd.PromptInput("Label") wallet, err := openWallet() cmd.FatalErrorCheck(err) - addr, err := wallet.NewBLSAccountAddress(label) + if *addressType != blsAddressOption || *addressType != validatorAddressOption { + cmd.PrintErrorMsgf("Invalid address type. Supported address types are 'validator' and 'bls_account'") + return + } + + if *addressType == blsAddressOption { + addr, err = wallet.NewBLSAccountAddress(label) + } + + if *addressType == validatorAddressOption { + addr, err = wallet.NewValidatorAddress(label) + } + cmd.FatalErrorCheck(err) err = wallet.Save() diff --git a/cmd/wallet/main.go b/cmd/wallet/main.go index fb63fa783..a518ffe0a 100644 --- a/cmd/wallet/main.go +++ b/cmd/wallet/main.go @@ -12,6 +12,7 @@ var ( pathOpt *string offlineOpt *bool serverAddrOpt *string + walletAddrOpt *string ) func addPasswordOption(c *cobra.Command) *string { @@ -53,6 +54,7 @@ func main() { pathOpt = rootCmd.PersistentFlags().String("path", "default_wallet", "the path to the wallet file") offlineOpt = rootCmd.PersistentFlags().Bool("offline", false, "offline mode") serverAddrOpt = rootCmd.PersistentFlags().String("server", "", "server gRPC address") + walletAddrOpt = rootCmd.PersistentFlags().String("address_type", "", "type of address") buildCreateCmd(rootCmd) buildRecoverCmd(rootCmd) From 07b913018ff30647ddde7544daeb5a4b36bc2284 Mon Sep 17 00:00:00 2001 From: Jemilu Mohammed Date: Fri, 20 Oct 2023 01:30:55 +0000 Subject: [PATCH 2/9] feat: add new button to create validator address on pactos GUI --- cmd/gtk/assets/ui/widget_wallet.ui | 15 +++++++++++++++ cmd/gtk/model_wallet.go | 12 ++++++++++-- cmd/gtk/widget_wallet.go | 16 ++++++++++++---- cmd/wallet/address.go | 17 +++++++++-------- wallet/client.go | 11 +++++++++++ 5 files changed, 57 insertions(+), 14 deletions(-) diff --git a/cmd/gtk/assets/ui/widget_wallet.ui b/cmd/gtk/assets/ui/widget_wallet.ui index fb8143dd9..3eb89b4f3 100644 --- a/cmd/gtk/assets/ui/widget_wallet.ui +++ b/cmd/gtk/assets/ui/widget_wallet.ui @@ -253,6 +253,21 @@ True + + + True + False + Create new validator address + True + _New Validator Address + True + + + + False + True + + True diff --git a/cmd/gtk/model_wallet.go b/cmd/gtk/model_wallet.go index d87f4b653..3783d90dc 100644 --- a/cmd/gtk/model_wallet.go +++ b/cmd/gtk/model_wallet.go @@ -85,8 +85,16 @@ func (model *walletModel) rebuildModel() { }() } -func (model *walletModel) createAddress() error { - address, err := model.wallet.NewBLSAccountAddress("") +func (model *walletModel) createAddress(accountType wallet.AddressType) error { + var address string + var err error + + if accountType == wallet.AddressTypeBLSAccount { + address, err = model.wallet.NewBLSAccountAddress("") + } else { + address, err = model.wallet.NewValidatorAddress("") + } + if err != nil { return err } diff --git a/cmd/gtk/widget_wallet.go b/cmd/gtk/widget_wallet.go index 6bb07efaa..78e19079a 100644 --- a/cmd/gtk/widget_wallet.go +++ b/cmd/gtk/widget_wallet.go @@ -8,6 +8,7 @@ import ( "github.com/gotk3/gotk3/gdk" "github.com/gotk3/gotk3/glib" "github.com/gotk3/gotk3/gtk" + "github.com/pactus-project/pactus/wallet" ) // IDs to access the tree view columns. @@ -55,6 +56,7 @@ func buildWidgetWallet(model *walletModel) (*widgetWallet, error) { labelEncrypted := getLabelObj(builder, "id_label_wallet_encrypted") getToolButtonObj(builder, "id_button_new_address").SetIconWidget(AddIcon()) + getToolButtonObj(builder, "id_button_new_validator_address").SetIconWidget(AddIcon()) getToolButtonObj(builder, "id_button_change_password").SetIconWidget(PasswordIcon()) getToolButtonObj(builder, "id_button_show_seed").SetIconWidget(SeedIcon()) @@ -136,9 +138,10 @@ func buildWidgetWallet(model *walletModel) (*widgetWallet, error) { }) signals := map[string]interface{}{ - "on_new_address": w.onNewAddress, - "on_change_password": w.onChangePassword, - "on_show_seed": w.onShowSeed, + "on_new_address": w.onNewAddress, + "on_new_validator_address": w.onNewValidatorAddress, + "on_change_password": w.onChangePassword, + "on_show_seed": w.onShowSeed, } builder.ConnectSignals(signals) @@ -148,7 +151,12 @@ func buildWidgetWallet(model *walletModel) (*widgetWallet, error) { } func (ww *widgetWallet) onNewAddress() { - err := ww.model.createAddress() + err := ww.model.createAddress(wallet.AddressTypeBLSAccount) + errorCheck(err) +} + +func (ww *widgetWallet) onNewValidatorAddress() { + err := ww.model.createAddress(wallet.AddressTypeValidator) errorCheck(err) } diff --git a/cmd/wallet/address.go b/cmd/wallet/address.go index e1d5ae58b..cd85e2c13 100644 --- a/cmd/wallet/address.go +++ b/cmd/wallet/address.go @@ -6,6 +6,7 @@ import ( "github.com/pactus-project/pactus/cmd" "github.com/pactus-project/pactus/crypto/bls" "github.com/pactus-project/pactus/util" + w "github.com/pactus-project/pactus/wallet" "github.com/spf13/cobra" ) @@ -76,11 +77,8 @@ func buildNewAddressCmd(parentCmd *cobra.Command) { } parentCmd.AddCommand(newAddressCmd) - blsAddressOption := "bls_account" - validatorAddressOption := "validator" - addressType := newAddressCmd.Flags().String("address_type", - blsAddressOption, "Address type to create") + w.AddressTypeBLSAccount.String(), "Address type to create") newAddressCmd.Run = func(_ *cobra.Command, _ []string) { var addr string @@ -90,16 +88,19 @@ func buildNewAddressCmd(parentCmd *cobra.Command) { wallet, err := openWallet() cmd.FatalErrorCheck(err) - if *addressType != blsAddressOption || *addressType != validatorAddressOption { - cmd.PrintErrorMsgf("Invalid address type. Supported address types are 'validator' and 'bls_account'") + isForBLSAccount := *addressType == w.AddressTypeBLSAccount.String() + isForValidatorAccount := *addressType == w.AddressTypeValidator.String() + + if !isForBLSAccount || !isForValidatorAccount { + cmd.PrintErrorMsgf("Invalid address type. Supported address types are '%s' and '%s'", w.AddressTypeBLSAccount, w.AddressTypeValidator) return } - if *addressType == blsAddressOption { + if isForBLSAccount { addr, err = wallet.NewBLSAccountAddress(label) } - if *addressType == validatorAddressOption { + if isForValidatorAccount { addr, err = wallet.NewValidatorAddress(label) } diff --git a/wallet/client.go b/wallet/client.go index e7e6c8480..3ca2c955c 100644 --- a/wallet/client.go +++ b/wallet/client.go @@ -12,6 +12,17 @@ import ( "google.golang.org/grpc/credentials/insecure" ) +type AddressType string + +var ( + AddressTypeBLSAccount AddressType = "bls_account" + AddressTypeValidator AddressType = "validator" +) + +func (a AddressType) String() string { + return string(a) +} + type grpcClient struct { blockchainClient pactus.BlockchainClient transactionClient pactus.TransactionClient From 5505d4427d872f4ef50456d2319784377b6cbd86 Mon Sep 17 00:00:00 2001 From: Jemilu Mohammed Date: Fri, 20 Oct 2023 02:08:48 +0000 Subject: [PATCH 3/9] fix: fix bug in cli command to create address --- cmd/wallet/address.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/wallet/address.go b/cmd/wallet/address.go index cd85e2c13..99a0c5320 100644 --- a/cmd/wallet/address.go +++ b/cmd/wallet/address.go @@ -91,8 +91,8 @@ func buildNewAddressCmd(parentCmd *cobra.Command) { isForBLSAccount := *addressType == w.AddressTypeBLSAccount.String() isForValidatorAccount := *addressType == w.AddressTypeValidator.String() - if !isForBLSAccount || !isForValidatorAccount { - cmd.PrintErrorMsgf("Invalid address type. Supported address types are '%s' and '%s'", w.AddressTypeBLSAccount, w.AddressTypeValidator) + if !isForBLSAccount && !isForValidatorAccount { + cmd.PrintErrorMsgf("Invalid address type '%s'. Supported address types are '%s' and '%s'", *addressType, w.AddressTypeBLSAccount, w.AddressTypeValidator) return } From 55599065b8a1c4f2678e39dbebd151bce27e9389 Mon Sep 17 00:00:00 2001 From: Jemilu Mohammed Date: Fri, 20 Oct 2023 12:18:03 +0000 Subject: [PATCH 4/9] fis: make change requests - introduce modal for new address creation that takes an optional account label and a required account type - change type flagname to --type for address creation command - do requested refactoring TODO: update list of addresses after address modal creates a new address --- .../assets/ui/dialog_wallet_create_address.ui | 136 ++++++++++++++++++ cmd/gtk/assets/ui/widget_wallet.ui | 15 -- cmd/gtk/dialog_wallet_create_address.go | 79 ++++++++++ cmd/gtk/model_wallet.go | 2 +- cmd/gtk/widget_wallet.go | 23 +-- cmd/wallet/address.go | 21 +-- cmd/wallet/main.go | 2 - wallet/client.go | 12 +- 8 files changed, 233 insertions(+), 57 deletions(-) create mode 100644 cmd/gtk/assets/ui/dialog_wallet_create_address.ui create mode 100644 cmd/gtk/dialog_wallet_create_address.go diff --git a/cmd/gtk/assets/ui/dialog_wallet_create_address.ui b/cmd/gtk/assets/ui/dialog_wallet_create_address.ui new file mode 100644 index 000000000..b13f19d84 --- /dev/null +++ b/cmd/gtk/assets/ui/dialog_wallet_create_address.ui @@ -0,0 +1,136 @@ + + + + + + False + Change password + 320 + dialog + + + False + 8 + 8 + 4 + 4 + vertical + 2 + + + False + end + + + _Cancel + True + True + False + False + True + True + + + + + True + True + 0 + + + + + _Ok + True + True + False + True + True + True + True + True + + + + + True + True + 1 + + + + + False + False + 0 + + + + + + True + False + 8 + 8 + + + True + False + start + Label: + + + 0 + 0 + + + + + True + True + True + 24 + + + 1 + 0 + + + + + True + False + start + AccountType: + + + 0 + 1 + + + + + True + False + + + 1 + 1 + + + + + True + True + 8 + 1 + + + + + + id_button_cancel + id_button_ok + + + diff --git a/cmd/gtk/assets/ui/widget_wallet.ui b/cmd/gtk/assets/ui/widget_wallet.ui index 3eb89b4f3..fb8143dd9 100644 --- a/cmd/gtk/assets/ui/widget_wallet.ui +++ b/cmd/gtk/assets/ui/widget_wallet.ui @@ -253,21 +253,6 @@ True - - - True - False - Create new validator address - True - _New Validator Address - True - - - - False - True - - True diff --git a/cmd/gtk/dialog_wallet_create_address.go b/cmd/gtk/dialog_wallet_create_address.go new file mode 100644 index 000000000..097feb0a5 --- /dev/null +++ b/cmd/gtk/dialog_wallet_create_address.go @@ -0,0 +1,79 @@ +//go:build gtk + +package main + +import ( + _ "embed" + "fmt" + + "github.com/gotk3/gotk3/gtk" + + w "github.com/pactus-project/pactus/wallet" +) + +//go:embed assets/ui/dialog_wallet_create_address.ui +var uiWalletCreateAddressDialog []byte + +func createAddress(wallet *w.Wallet) { + builder, err := gtk.BuilderNewFromString(string(uiWalletCreateAddressDialog)) + fatalErrorCheck(err) + + dlg := getDialogObj(builder, "id_dialog_wallet_create_address") + addressLabel := getEntryObj(builder, "id_entry_account_label") + oldPasswordLabel := getLabelObj(builder, "id_label_account_label") + + accountTypeCombo := getComboBoxTextObj(builder, "id_combo_account_type") + accountTypeCombo.Append("bls_account", "Account") + accountTypeCombo.Append("validator", "Validator") + + accountTypeCombo.SetActive(0) + + getButtonObj(builder, "id_button_ok").SetImage(OkIcon()) + getButtonObj(builder, "id_button_cancel").SetImage(CancelIcon()) + + if !wallet.IsEncrypted() { + addressLabel.SetVisible(false) + oldPasswordLabel.SetVisible(false) + } + + onOk := func() { + walletAddressLabel, err := addressLabel.GetText() + fatalErrorCheck(err) + + walletAccountType := accountTypeCombo.GetActiveID() + fatalErrorCheck(err) + + if walletAccountType == w.AddressTypeBLSAccount { + _, err = wallet.NewBLSAccountAddress(walletAddressLabel) + } else if walletAccountType == w.AddressTypeValidator { + _, err = wallet.NewValidatorAddress(walletAddressLabel) + } else { + errorMsg := fmt.Sprintf("Invalid address type '%s'. Supported address types are '%s' and '%s'", walletAccountType, w.AddressTypeBLSAccount, w.AddressTypeValidator) + showWarningDialog(dlg, errorMsg) + return + } + + errorCheck(err) + + err = wallet.Save() + errorCheck(err) + + dlg.Close() + } + + onCancel := func() { + dlg.Close() + } + + // Map the handlers to callback functions, and connect the signals + // to the Builder. + signals := map[string]interface{}{ + "on_ok": onOk, + "on_cancel": onCancel, + } + builder.ConnectSignals(signals) + + dlg.SetModal(true) + + dlg.Run() +} diff --git a/cmd/gtk/model_wallet.go b/cmd/gtk/model_wallet.go index 3783d90dc..acde9f6b4 100644 --- a/cmd/gtk/model_wallet.go +++ b/cmd/gtk/model_wallet.go @@ -85,7 +85,7 @@ func (model *walletModel) rebuildModel() { }() } -func (model *walletModel) createAddress(accountType wallet.AddressType) error { +func (model *walletModel) createAddress(accountType string) error { var address string var err error diff --git a/cmd/gtk/widget_wallet.go b/cmd/gtk/widget_wallet.go index 78e19079a..4c2f05c08 100644 --- a/cmd/gtk/widget_wallet.go +++ b/cmd/gtk/widget_wallet.go @@ -8,7 +8,6 @@ import ( "github.com/gotk3/gotk3/gdk" "github.com/gotk3/gotk3/glib" "github.com/gotk3/gotk3/gtk" - "github.com/pactus-project/pactus/wallet" ) // IDs to access the tree view columns. @@ -56,7 +55,6 @@ func buildWidgetWallet(model *walletModel) (*widgetWallet, error) { labelEncrypted := getLabelObj(builder, "id_label_wallet_encrypted") getToolButtonObj(builder, "id_button_new_address").SetIconWidget(AddIcon()) - getToolButtonObj(builder, "id_button_new_validator_address").SetIconWidget(AddIcon()) getToolButtonObj(builder, "id_button_change_password").SetIconWidget(PasswordIcon()) getToolButtonObj(builder, "id_button_show_seed").SetIconWidget(SeedIcon()) @@ -138,10 +136,9 @@ func buildWidgetWallet(model *walletModel) (*widgetWallet, error) { }) signals := map[string]interface{}{ - "on_new_address": w.onNewAddress, - "on_new_validator_address": w.onNewValidatorAddress, - "on_change_password": w.onChangePassword, - "on_show_seed": w.onShowSeed, + "on_new_address": w.onNewAddress, + "on_change_password": w.onChangePassword, + "on_show_seed": w.onShowSeed, } builder.ConnectSignals(signals) @@ -150,20 +147,14 @@ func buildWidgetWallet(model *walletModel) (*widgetWallet, error) { return w, nil } -func (ww *widgetWallet) onNewAddress() { - err := ww.model.createAddress(wallet.AddressTypeBLSAccount) - errorCheck(err) -} - -func (ww *widgetWallet) onNewValidatorAddress() { - err := ww.model.createAddress(wallet.AddressTypeValidator) - errorCheck(err) -} - func (ww *widgetWallet) onChangePassword() { changePassword(ww.model.wallet) } +func (ww *widgetWallet) onNewAddress() { + createAddress(ww.model.wallet) +} + func (ww *widgetWallet) onShowSeed() { password, ok := getWalletPassword(ww.model.wallet) if !ok { diff --git a/cmd/wallet/address.go b/cmd/wallet/address.go index 99a0c5320..7e1590ac8 100644 --- a/cmd/wallet/address.go +++ b/cmd/wallet/address.go @@ -77,8 +77,8 @@ func buildNewAddressCmd(parentCmd *cobra.Command) { } parentCmd.AddCommand(newAddressCmd) - addressType := newAddressCmd.Flags().String("address_type", - w.AddressTypeBLSAccount.String(), "Address type to create") + addressType := newAddressCmd.Flags().String("type", + w.AddressTypeBLSAccount, "the type of address: bls_account or validator") newAddressCmd.Run = func(_ *cobra.Command, _ []string) { var addr string @@ -88,20 +88,13 @@ func buildNewAddressCmd(parentCmd *cobra.Command) { wallet, err := openWallet() cmd.FatalErrorCheck(err) - isForBLSAccount := *addressType == w.AddressTypeBLSAccount.String() - isForValidatorAccount := *addressType == w.AddressTypeValidator.String() - - if !isForBLSAccount && !isForValidatorAccount { - cmd.PrintErrorMsgf("Invalid address type '%s'. Supported address types are '%s' and '%s'", *addressType, w.AddressTypeBLSAccount, w.AddressTypeValidator) - return - } - - if isForBLSAccount { + if *addressType == w.AddressTypeBLSAccount { addr, err = wallet.NewBLSAccountAddress(label) - } - - if isForValidatorAccount { + } else if *addressType == w.AddressTypeValidator { addr, err = wallet.NewValidatorAddress(label) + } else { + cmd.PrintErrorMsgf("Invalid address type '%s'. Supported address types are '%s' and '%s'", *addressType, w.AddressTypeBLSAccount, w.AddressTypeValidator) + return } cmd.FatalErrorCheck(err) diff --git a/cmd/wallet/main.go b/cmd/wallet/main.go index a518ffe0a..fb63fa783 100644 --- a/cmd/wallet/main.go +++ b/cmd/wallet/main.go @@ -12,7 +12,6 @@ var ( pathOpt *string offlineOpt *bool serverAddrOpt *string - walletAddrOpt *string ) func addPasswordOption(c *cobra.Command) *string { @@ -54,7 +53,6 @@ func main() { pathOpt = rootCmd.PersistentFlags().String("path", "default_wallet", "the path to the wallet file") offlineOpt = rootCmd.PersistentFlags().Bool("offline", false, "offline mode") serverAddrOpt = rootCmd.PersistentFlags().String("server", "", "server gRPC address") - walletAddrOpt = rootCmd.PersistentFlags().String("address_type", "", "type of address") buildCreateCmd(rootCmd) buildRecoverCmd(rootCmd) diff --git a/wallet/client.go b/wallet/client.go index 3ca2c955c..886f3858d 100644 --- a/wallet/client.go +++ b/wallet/client.go @@ -12,17 +12,11 @@ import ( "google.golang.org/grpc/credentials/insecure" ) -type AddressType string - -var ( - AddressTypeBLSAccount AddressType = "bls_account" - AddressTypeValidator AddressType = "validator" +const ( + AddressTypeBLSAccount string = "bls_account" + AddressTypeValidator string = "validator" ) -func (a AddressType) String() string { - return string(a) -} - type grpcClient struct { blockchainClient pactus.BlockchainClient transactionClient pactus.TransactionClient From 233644591b8195829dbcbbffd6c0daf611f26c1e Mon Sep 17 00:00:00 2001 From: Jemilu Mohammed Date: Sat, 21 Oct 2023 00:14:12 +0000 Subject: [PATCH 5/9] fix lint error --- cmd/wallet/address.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cmd/wallet/address.go b/cmd/wallet/address.go index 7e1590ac8..6acd36e72 100644 --- a/cmd/wallet/address.go +++ b/cmd/wallet/address.go @@ -93,7 +93,8 @@ func buildNewAddressCmd(parentCmd *cobra.Command) { } else if *addressType == w.AddressTypeValidator { addr, err = wallet.NewValidatorAddress(label) } else { - cmd.PrintErrorMsgf("Invalid address type '%s'. Supported address types are '%s' and '%s'", *addressType, w.AddressTypeBLSAccount, w.AddressTypeValidator) + formatString := "Invalid address type '%s'. Supported address types are '%s' and '%s'" + cmd.PrintErrorMsgf(formatString, *addressType, w.AddressTypeBLSAccount, w.AddressTypeValidator) return } From 773499a574f03df27d0b157841fdb18a77556835 Mon Sep 17 00:00:00 2001 From: Jemilu Mohammed Date: Sat, 21 Oct 2023 00:36:20 +0000 Subject: [PATCH 6/9] fix: introduce callback to add newly created address --- cmd/gtk/dialog_wallet_create_address.go | 8 +++++--- cmd/gtk/widget_wallet.go | 25 ++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/cmd/gtk/dialog_wallet_create_address.go b/cmd/gtk/dialog_wallet_create_address.go index 097feb0a5..c6ef982dd 100644 --- a/cmd/gtk/dialog_wallet_create_address.go +++ b/cmd/gtk/dialog_wallet_create_address.go @@ -14,7 +14,7 @@ import ( //go:embed assets/ui/dialog_wallet_create_address.ui var uiWalletCreateAddressDialog []byte -func createAddress(wallet *w.Wallet) { +func createAddress(wallet *w.Wallet, addCreatedAddess func(address string, label string)) { builder, err := gtk.BuilderNewFromString(string(uiWalletCreateAddressDialog)) fatalErrorCheck(err) @@ -43,10 +43,11 @@ func createAddress(wallet *w.Wallet) { walletAccountType := accountTypeCombo.GetActiveID() fatalErrorCheck(err) + var address string if walletAccountType == w.AddressTypeBLSAccount { - _, err = wallet.NewBLSAccountAddress(walletAddressLabel) + address, err = wallet.NewBLSAccountAddress(walletAddressLabel) } else if walletAccountType == w.AddressTypeValidator { - _, err = wallet.NewValidatorAddress(walletAddressLabel) + address, err = wallet.NewValidatorAddress(walletAddressLabel) } else { errorMsg := fmt.Sprintf("Invalid address type '%s'. Supported address types are '%s' and '%s'", walletAccountType, w.AddressTypeBLSAccount, w.AddressTypeValidator) showWarningDialog(dlg, errorMsg) @@ -54,6 +55,7 @@ func createAddress(wallet *w.Wallet) { } errorCheck(err) + addCreatedAddess(address, walletAddressLabel) err = wallet.Save() errorCheck(err) diff --git a/cmd/gtk/widget_wallet.go b/cmd/gtk/widget_wallet.go index 4c2f05c08..75d5f88b7 100644 --- a/cmd/gtk/widget_wallet.go +++ b/cmd/gtk/widget_wallet.go @@ -4,6 +4,7 @@ package main import ( _ "embed" + "fmt" "github.com/gotk3/gotk3/gdk" "github.com/gotk3/gotk3/glib" @@ -147,12 +148,34 @@ func buildWidgetWallet(model *walletModel) (*widgetWallet, error) { return w, nil } +func (ww *widgetWallet) addAddress(address string, label string) { + iter := ww.model.listStore.Append() + _ = ww.model.listStore.Set(iter, + []int{ + IDAddressesColumnNo, + IDAddressesColumnAddress, + IDAddressesColumnLabel, + IDAddressesColumnBalance, + IDAddressesColumnStake, + }, + []interface{}{ + fmt.Sprintf("%v", ww.model.wallet.AddressCount()+1), + address, + label, + "0", + "0", + }) +} + func (ww *widgetWallet) onChangePassword() { changePassword(ww.model.wallet) } func (ww *widgetWallet) onNewAddress() { - createAddress(ww.model.wallet) + var addNewAddressCallback = func(address string, label string) { + ww.addAddress(address, label) + } + createAddress(ww.model.wallet, addNewAddressCallback) } func (ww *widgetWallet) onShowSeed() { From 744ce70c1ae0d6cde078415bba0a667a1b5c71c4 Mon Sep 17 00:00:00 2001 From: Jemilu Mohammed Date: Sat, 21 Oct 2023 00:44:32 +0000 Subject: [PATCH 7/9] refactor: remove dead code for adding address prior to using modal. rename receiver on *widgetWallet --- cmd/gtk/model_wallet.go | 42 ---------------------------------------- cmd/gtk/widget_wallet.go | 4 ++-- 2 files changed, 2 insertions(+), 44 deletions(-) diff --git a/cmd/gtk/model_wallet.go b/cmd/gtk/model_wallet.go index acde9f6b4..db44c8cf1 100644 --- a/cmd/gtk/model_wallet.go +++ b/cmd/gtk/model_wallet.go @@ -84,45 +84,3 @@ func (model *walletModel) rebuildModel() { }) }() } - -func (model *walletModel) createAddress(accountType string) error { - var address string - var err error - - if accountType == wallet.AddressTypeBLSAccount { - address, err = model.wallet.NewBLSAccountAddress("") - } else { - address, err = model.wallet.NewValidatorAddress("") - } - - if err != nil { - return err - } - - iter := model.listStore.Append() - err = model.listStore.Set(iter, - []int{ - IDAddressesColumnNo, - IDAddressesColumnAddress, - IDAddressesColumnLabel, - IDAddressesColumnBalance, - IDAddressesColumnStake, - }, - []interface{}{ - fmt.Sprintf("%v", model.wallet.AddressCount()+1), - address, - "", - "0", - "0", - }) - if err != nil { - return err - } - - err = model.wallet.Save() - if err != nil { - return err - } - - return nil -} diff --git a/cmd/gtk/widget_wallet.go b/cmd/gtk/widget_wallet.go index 75d5f88b7..e8c6d099c 100644 --- a/cmd/gtk/widget_wallet.go +++ b/cmd/gtk/widget_wallet.go @@ -148,7 +148,7 @@ func buildWidgetWallet(model *walletModel) (*widgetWallet, error) { return w, nil } -func (ww *widgetWallet) addAddress(address string, label string) { +func (ww *widgetWallet) addNewAddressToListStore(address string, label string) { iter := ww.model.listStore.Append() _ = ww.model.listStore.Set(iter, []int{ @@ -173,7 +173,7 @@ func (ww *widgetWallet) onChangePassword() { func (ww *widgetWallet) onNewAddress() { var addNewAddressCallback = func(address string, label string) { - ww.addAddress(address, label) + ww.addNewAddressToListStore(address, label) } createAddress(ww.model.wallet, addNewAddressCallback) } From 185d7ae0ec1ea6ef44c9410acb67b9b010504a06 Mon Sep 17 00:00:00 2001 From: Jemilu Mohammed Date: Sat, 21 Oct 2023 00:58:41 +0000 Subject: [PATCH 8/9] refactor: remove hacky callback and send widgetWallet down to modal creator --- cmd/gtk/dialog_wallet_create_address.go | 21 +++++++++------------ cmd/gtk/widget_wallet.go | 9 +++------ 2 files changed, 12 insertions(+), 18 deletions(-) diff --git a/cmd/gtk/dialog_wallet_create_address.go b/cmd/gtk/dialog_wallet_create_address.go index c6ef982dd..4129e5c00 100644 --- a/cmd/gtk/dialog_wallet_create_address.go +++ b/cmd/gtk/dialog_wallet_create_address.go @@ -14,13 +14,12 @@ import ( //go:embed assets/ui/dialog_wallet_create_address.ui var uiWalletCreateAddressDialog []byte -func createAddress(wallet *w.Wallet, addCreatedAddess func(address string, label string)) { +func createAddress(ww *widgetWallet) { builder, err := gtk.BuilderNewFromString(string(uiWalletCreateAddressDialog)) fatalErrorCheck(err) dlg := getDialogObj(builder, "id_dialog_wallet_create_address") addressLabel := getEntryObj(builder, "id_entry_account_label") - oldPasswordLabel := getLabelObj(builder, "id_label_account_label") accountTypeCombo := getComboBoxTextObj(builder, "id_combo_account_type") accountTypeCombo.Append("bls_account", "Account") @@ -31,11 +30,6 @@ func createAddress(wallet *w.Wallet, addCreatedAddess func(address string, label getButtonObj(builder, "id_button_ok").SetImage(OkIcon()) getButtonObj(builder, "id_button_cancel").SetImage(CancelIcon()) - if !wallet.IsEncrypted() { - addressLabel.SetVisible(false) - oldPasswordLabel.SetVisible(false) - } - onOk := func() { walletAddressLabel, err := addressLabel.GetText() fatalErrorCheck(err) @@ -45,19 +39,22 @@ func createAddress(wallet *w.Wallet, addCreatedAddess func(address string, label var address string if walletAccountType == w.AddressTypeBLSAccount { - address, err = wallet.NewBLSAccountAddress(walletAddressLabel) + address, err = ww.model.wallet.NewBLSAccountAddress(walletAddressLabel) } else if walletAccountType == w.AddressTypeValidator { - address, err = wallet.NewValidatorAddress(walletAddressLabel) + address, err = ww.model.wallet.NewValidatorAddress(walletAddressLabel) } else { - errorMsg := fmt.Sprintf("Invalid address type '%s'. Supported address types are '%s' and '%s'", walletAccountType, w.AddressTypeBLSAccount, w.AddressTypeValidator) + formatString := "Invalid address type '%s'. Supported address types are '%s' and '%s'" + errorMsg := fmt.Sprintf(formatString, walletAccountType, w.AddressTypeBLSAccount, w.AddressTypeValidator) showWarningDialog(dlg, errorMsg) return } errorCheck(err) - addCreatedAddess(address, walletAddressLabel) - err = wallet.Save() + err = ww.addNewAddressToListStore(address, walletAddressLabel) + errorCheck(err) + + err = ww.model.wallet.Save() errorCheck(err) dlg.Close() diff --git a/cmd/gtk/widget_wallet.go b/cmd/gtk/widget_wallet.go index e8c6d099c..5b2ab0671 100644 --- a/cmd/gtk/widget_wallet.go +++ b/cmd/gtk/widget_wallet.go @@ -148,9 +148,9 @@ func buildWidgetWallet(model *walletModel) (*widgetWallet, error) { return w, nil } -func (ww *widgetWallet) addNewAddressToListStore(address string, label string) { +func (ww *widgetWallet) addNewAddressToListStore(address string, label string) error { iter := ww.model.listStore.Append() - _ = ww.model.listStore.Set(iter, + return ww.model.listStore.Set(iter, []int{ IDAddressesColumnNo, IDAddressesColumnAddress, @@ -172,10 +172,7 @@ func (ww *widgetWallet) onChangePassword() { } func (ww *widgetWallet) onNewAddress() { - var addNewAddressCallback = func(address string, label string) { - ww.addNewAddressToListStore(address, label) - } - createAddress(ww.model.wallet, addNewAddressCallback) + createAddress(ww) } func (ww *widgetWallet) onShowSeed() { From da2a61adfddbe894adaee66171e37a437151e7ae Mon Sep 17 00:00:00 2001 From: mantre Date: Sun, 22 Oct 2023 20:01:02 +0800 Subject: [PATCH 9/9] fix: some improvements --- .../assets/ui/dialog_wallet_create_address.ui | 6 +- cmd/gtk/dialog_wallet_create_address.go | 31 ++++------ cmd/gtk/widget_wallet.go | 20 ------ cmd/wallet/address.go | 61 +++++++++---------- cmd/wallet/create.go | 8 +-- cmd/wallet/history.go | 10 +-- cmd/wallet/recover.go | 6 +- 7 files changed, 57 insertions(+), 85 deletions(-) diff --git a/cmd/gtk/assets/ui/dialog_wallet_create_address.ui b/cmd/gtk/assets/ui/dialog_wallet_create_address.ui index b13f19d84..1964b2bad 100644 --- a/cmd/gtk/assets/ui/dialog_wallet_create_address.ui +++ b/cmd/gtk/assets/ui/dialog_wallet_create_address.ui @@ -4,7 +4,7 @@ False - Change password + New address 320 dialog @@ -101,7 +101,7 @@ True False start - AccountType: + Type: 0 @@ -109,7 +109,7 @@ - + True False diff --git a/cmd/gtk/dialog_wallet_create_address.go b/cmd/gtk/dialog_wallet_create_address.go index 4129e5c00..f708b93f3 100644 --- a/cmd/gtk/dialog_wallet_create_address.go +++ b/cmd/gtk/dialog_wallet_create_address.go @@ -8,7 +8,7 @@ import ( "github.com/gotk3/gotk3/gtk" - w "github.com/pactus-project/pactus/wallet" + "github.com/pactus-project/pactus/wallet" ) //go:embed assets/ui/dialog_wallet_create_address.ui @@ -21,11 +21,11 @@ func createAddress(ww *widgetWallet) { dlg := getDialogObj(builder, "id_dialog_wallet_create_address") addressLabel := getEntryObj(builder, "id_entry_account_label") - accountTypeCombo := getComboBoxTextObj(builder, "id_combo_account_type") - accountTypeCombo.Append("bls_account", "Account") - accountTypeCombo.Append("validator", "Validator") + addressTypeCombo := getComboBoxTextObj(builder, "id_combo_address_type") + addressTypeCombo.Append(wallet.AddressTypeBLSAccount, "Account") + addressTypeCombo.Append(wallet.AddressTypeValidator, "Validator") - accountTypeCombo.SetActive(0) + addressTypeCombo.SetActive(0) getButtonObj(builder, "id_button_ok").SetImage(OkIcon()) getButtonObj(builder, "id_button_cancel").SetImage(CancelIcon()) @@ -34,29 +34,24 @@ func createAddress(ww *widgetWallet) { walletAddressLabel, err := addressLabel.GetText() fatalErrorCheck(err) - walletAccountType := accountTypeCombo.GetActiveID() + walletAddressType := addressTypeCombo.GetActiveID() fatalErrorCheck(err) - var address string - if walletAccountType == w.AddressTypeBLSAccount { - address, err = ww.model.wallet.NewBLSAccountAddress(walletAddressLabel) - } else if walletAccountType == w.AddressTypeValidator { - address, err = ww.model.wallet.NewValidatorAddress(walletAddressLabel) + if walletAddressType == wallet.AddressTypeBLSAccount { + _, err = ww.model.wallet.NewBLSAccountAddress(walletAddressLabel) + } else if walletAddressType == wallet.AddressTypeValidator { + _, err = ww.model.wallet.NewValidatorAddress(walletAddressLabel) } else { - formatString := "Invalid address type '%s'. Supported address types are '%s' and '%s'" - errorMsg := fmt.Sprintf(formatString, walletAccountType, w.AddressTypeBLSAccount, w.AddressTypeValidator) - showWarningDialog(dlg, errorMsg) - return + err = fmt.Errorf("invalid address type '%s'", walletAddressType) } errorCheck(err) - err = ww.addNewAddressToListStore(address, walletAddressLabel) - errorCheck(err) - err = ww.model.wallet.Save() errorCheck(err) + ww.model.rebuildModel() + dlg.Close() } diff --git a/cmd/gtk/widget_wallet.go b/cmd/gtk/widget_wallet.go index 5b2ab0671..655ca17e2 100644 --- a/cmd/gtk/widget_wallet.go +++ b/cmd/gtk/widget_wallet.go @@ -4,7 +4,6 @@ package main import ( _ "embed" - "fmt" "github.com/gotk3/gotk3/gdk" "github.com/gotk3/gotk3/glib" @@ -148,25 +147,6 @@ func buildWidgetWallet(model *walletModel) (*widgetWallet, error) { return w, nil } -func (ww *widgetWallet) addNewAddressToListStore(address string, label string) error { - iter := ww.model.listStore.Append() - return ww.model.listStore.Set(iter, - []int{ - IDAddressesColumnNo, - IDAddressesColumnAddress, - IDAddressesColumnLabel, - IDAddressesColumnBalance, - IDAddressesColumnStake, - }, - []interface{}{ - fmt.Sprintf("%v", ww.model.wallet.AddressCount()+1), - address, - label, - "0", - "0", - }) -} - func (ww *widgetWallet) onChangePassword() { changePassword(ww.model.wallet) } diff --git a/cmd/wallet/address.go b/cmd/wallet/address.go index 6acd36e72..c93e211f1 100644 --- a/cmd/wallet/address.go +++ b/cmd/wallet/address.go @@ -6,7 +6,7 @@ import ( "github.com/pactus-project/pactus/cmd" "github.com/pactus-project/pactus/crypto/bls" "github.com/pactus-project/pactus/util" - w "github.com/pactus-project/pactus/wallet" + "github.com/pactus-project/pactus/wallet" "github.com/spf13/cobra" ) @@ -42,20 +42,20 @@ func buildAllAddressesCmd(parentCmd *cobra.Command) { false, "Display the validator stake for each address") allAddressCmd.Run = func(_ *cobra.Command, _ []string) { - wallet, err := openWallet() + wlt, err := openWallet() cmd.FatalErrorCheck(err) cmd.PrintLine() - for i, info := range wallet.AddressInfos() { + for i, info := range wlt.AddressInfos() { line := fmt.Sprintf("%v- %s\t", i+1, info.Address) if *balanceOpt { - balance, _ := wallet.Balance(info.Address) + balance, _ := wlt.Balance(info.Address) line += fmt.Sprintf("%v\t", util.ChangeToCoin(balance)) } if *stakeOpt { - stake, _ := wallet.Stake(info.Address) + stake, _ := wlt.Stake(info.Address) line += fmt.Sprintf("%v\t", util.ChangeToCoin(stake)) } @@ -78,29 +78,26 @@ func buildNewAddressCmd(parentCmd *cobra.Command) { parentCmd.AddCommand(newAddressCmd) addressType := newAddressCmd.Flags().String("type", - w.AddressTypeBLSAccount, "the type of address: bls_account or validator") + wallet.AddressTypeBLSAccount, "the type of address: bls_account or validator") newAddressCmd.Run = func(_ *cobra.Command, _ []string) { var addr string var err error label := cmd.PromptInput("Label") - wallet, err := openWallet() + wlt, err := openWallet() cmd.FatalErrorCheck(err) - if *addressType == w.AddressTypeBLSAccount { - addr, err = wallet.NewBLSAccountAddress(label) - } else if *addressType == w.AddressTypeValidator { - addr, err = wallet.NewValidatorAddress(label) + if *addressType == wallet.AddressTypeBLSAccount { + addr, err = wlt.NewBLSAccountAddress(label) + } else if *addressType == wallet.AddressTypeValidator { + addr, err = wlt.NewValidatorAddress(label) } else { - formatString := "Invalid address type '%s'. Supported address types are '%s' and '%s'" - cmd.PrintErrorMsgf(formatString, *addressType, w.AddressTypeBLSAccount, w.AddressTypeValidator) - return + err = fmt.Errorf("invalid address type '%s'", *addressType) } - cmd.FatalErrorCheck(err) - err = wallet.Save() + err = wlt.Save() cmd.FatalErrorCheck(err) cmd.PrintLine() @@ -120,14 +117,14 @@ func buildBalanceCmd(parentCmd *cobra.Command) { balanceCmd.Run = func(_ *cobra.Command, args []string) { addr := args[0] - wallet, err := openWallet() + wlt, err := openWallet() cmd.FatalErrorCheck(err) cmd.PrintLine() - balance, err := wallet.Balance(addr) + balance, err := wlt.Balance(addr) cmd.FatalErrorCheck(err) - stake, err := wallet.Stake(addr) + stake, err := wlt.Stake(addr) cmd.FatalErrorCheck(err) cmd.PrintInfoMsgf("balance: %v\tstake: %v", @@ -149,11 +146,11 @@ func buildPrivateKeyCmd(parentCmd *cobra.Command) { privateKeyCmd.Run = func(_ *cobra.Command, args []string) { addr := args[0] - wallet, err := openWallet() + wlt, err := openWallet() cmd.FatalErrorCheck(err) - password := getPassword(wallet, *passOpt) - prv, err := wallet.PrivateKey(password, addr) + password := getPassword(wlt, *passOpt) + prv, err := wlt.PrivateKey(password, addr) cmd.FatalErrorCheck(err) cmd.PrintLine() @@ -173,10 +170,10 @@ func buildPublicKeyCmd(parentCmd *cobra.Command) { publicKeyCmd.Run = func(_ *cobra.Command, args []string) { addr := args[0] - wallet, err := openWallet() + wlt, err := openWallet() cmd.FatalErrorCheck(err) - info := wallet.AddressInfo(addr) + info := wlt.AddressInfo(addr) if info == nil { cmd.PrintErrorMsgf("Address not found") return @@ -203,17 +200,17 @@ func buildImportPrivateKeyCmd(parentCmd *cobra.Command) { importPrivateKeyCmd.Run = func(_ *cobra.Command, _ []string) { prvStr := cmd.PromptInput("Private Key") - wallet, err := openWallet() + wlt, err := openWallet() cmd.FatalErrorCheck(err) prv, err := bls.PrivateKeyFromString(prvStr) cmd.FatalErrorCheck(err) - password := getPassword(wallet, *passOpt) - err = wallet.ImportPrivateKey(password, prv) + password := getPassword(wlt, *passOpt) + err = wlt.ImportPrivateKey(password, prv) cmd.FatalErrorCheck(err) - err = wallet.Save() + err = wlt.Save() cmd.FatalErrorCheck(err) cmd.PrintLine() @@ -233,16 +230,16 @@ func buildSetLabelCmd(parentCmd *cobra.Command) { setLabelCmd.Run = func(c *cobra.Command, args []string) { addr := args[0] - wallet, err := openWallet() + wlt, err := openWallet() cmd.FatalErrorCheck(err) - oldLabel := wallet.Label(addr) + oldLabel := wlt.Label(addr) newLabel := cmd.PromptInputWithSuggestion("Label", oldLabel) - err = wallet.SetLabel(addr, newLabel) + err = wlt.SetLabel(addr, newLabel) cmd.FatalErrorCheck(err) - err = wallet.Save() + err = wlt.Save() cmd.FatalErrorCheck(err) cmd.PrintLine() diff --git a/cmd/wallet/create.go b/cmd/wallet/create.go index f5b740cff..8ce6b6c18 100644 --- a/cmd/wallet/create.go +++ b/cmd/wallet/create.go @@ -53,16 +53,16 @@ func buildChangePasswordCmd(parentCmd *cobra.Command) { passOpt := addPasswordOption(changePasswordCmd) changePasswordCmd.Run = func(_ *cobra.Command, _ []string) { - wallet, err := openWallet() + wlt, err := openWallet() cmd.FatalErrorCheck(err) - oldPassword := getPassword(wallet, *passOpt) + oldPassword := getPassword(wlt, *passOpt) newPassword := cmd.PromptPassword("New Password", true) - err = wallet.UpdatePassword(oldPassword, newPassword) + err = wlt.UpdatePassword(oldPassword, newPassword) cmd.FatalErrorCheck(err) - err = wallet.Save() + err = wlt.Save() cmd.FatalErrorCheck(err) cmd.PrintLine() diff --git a/cmd/wallet/history.go b/cmd/wallet/history.go index 78662f3da..aaa27b961 100644 --- a/cmd/wallet/history.go +++ b/cmd/wallet/history.go @@ -31,16 +31,16 @@ func buildAddToHistoryCmd(parentCmd *cobra.Command) { addToHistoryCmd.Run = func(_ *cobra.Command, args []string) { txID := args[0] - wallet, err := openWallet() + wlt, err := openWallet() cmd.FatalErrorCheck(err) id, err := hash.FromString(txID) cmd.FatalErrorCheck(err) - err = wallet.AddTransaction(id) + err = wlt.AddTransaction(id) cmd.FatalErrorCheck(err) - err = wallet.Save() + err = wlt.Save() cmd.FatalErrorCheck(err) cmd.PrintInfoMsgf("Transaction successfully added to the wallet.") @@ -59,10 +59,10 @@ func buildShowHistoryCmd(parentCmd *cobra.Command) { showHistoryCmd.Run = func(_ *cobra.Command, args []string) { addr := args[0] - wallet, err := openWallet() + wlt, err := openWallet() cmd.FatalErrorCheck(err) - history := wallet.GetHistory(addr) + history := wlt.GetHistory(addr) for i, h := range history { if h.Time != nil { cmd.PrintInfoMsgf("%d %v %v %v %s\t%v", diff --git a/cmd/wallet/recover.go b/cmd/wallet/recover.go index 0c2cf31a5..98359ad1e 100644 --- a/cmd/wallet/recover.go +++ b/cmd/wallet/recover.go @@ -51,11 +51,11 @@ func buildGetSeedCmd(parentCmd *cobra.Command) { passOpt := addPasswordOption(getSeedCmd) getSeedCmd.Run = func(_ *cobra.Command, _ []string) { - wallet, err := openWallet() + wlt, err := openWallet() cmd.FatalErrorCheck(err) - password := getPassword(wallet, *passOpt) - mnemonic, err := wallet.Mnemonic(password) + password := getPassword(wlt, *passOpt) + mnemonic, err := wlt.Mnemonic(password) cmd.FatalErrorCheck(err) cmd.PrintLine()