Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: create new validator address (CLI and GUI) #757

15 changes: 15 additions & 0 deletions cmd/gtk/assets/ui/widget_wallet.ui
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,21 @@
<property name="homogeneous">True</property>
</packing>
</child>
<child>
<object class="GtkToolButton" id="id_button_new_validator_address">
themantre marked this conversation as resolved.
Show resolved Hide resolved
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="tooltip-text" translatable="yes">Create new validator address</property>
<property name="is-important">True</property>
<property name="label" translatable="yes">_New Validator Address</property>
<property name="use-underline">True</property>
<signal name="clicked" handler="on_new_validator_address" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
<property name="homogeneous">True</property>
</packing>
</child>
<child>
<object class="GtkToolButton" id="id_button_change_password">
<property name="visible">True</property>
Expand Down
12 changes: 10 additions & 2 deletions cmd/gtk/model_wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
themantre marked this conversation as resolved.
Show resolved Hide resolved
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
}
Expand Down
16 changes: 12 additions & 4 deletions cmd/gtk/widget_wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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())
themantre marked this conversation as resolved.
Show resolved Hide resolved
getToolButtonObj(builder, "id_button_change_password").SetIconWidget(PasswordIcon())
getToolButtonObj(builder, "id_button_show_seed").SetIconWidget(SeedIcon())

Expand Down Expand Up @@ -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,
themantre marked this conversation as resolved.
Show resolved Hide resolved
"on_change_password": w.onChangePassword,
"on_show_seed": w.onShowSeed,
}
builder.ConnectSignals(signals)

Expand All @@ -148,7 +151,12 @@ func buildWidgetWallet(model *walletModel) (*widgetWallet, error) {
}

func (ww *widgetWallet) onNewAddress() {
err := ww.model.createAddress()
err := ww.model.createAddress(wallet.AddressTypeBLSAccount)
themantre marked this conversation as resolved.
Show resolved Hide resolved
errorCheck(err)
}

func (ww *widgetWallet) onNewValidatorAddress() {
err := ww.model.createAddress(wallet.AddressTypeValidator)
errorCheck(err)
}

Expand Down
24 changes: 23 additions & 1 deletion cmd/wallet/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -76,12 +77,33 @@ func buildNewAddressCmd(parentCmd *cobra.Command) {
}
parentCmd.AddCommand(newAddressCmd)

addressType := newAddressCmd.Flags().String("address_type",
themantre marked this conversation as resolved.
Show resolved Hide resolved
w.AddressTypeBLSAccount.String(), "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)
isForBLSAccount := *addressType == w.AddressTypeBLSAccount.String()
themantre marked this conversation as resolved.
Show resolved Hide resolved
isForValidatorAccount := *addressType == w.AddressTypeValidator.String()

if !isForBLSAccount || !isForValidatorAccount {
themantre marked this conversation as resolved.
Show resolved Hide resolved
cmd.PrintErrorMsgf("Invalid address type. Supported address types are '%s' and '%s'", w.AddressTypeBLSAccount, w.AddressTypeValidator)
return
}

if isForBLSAccount {
addr, err = wallet.NewBLSAccountAddress(label)
}

if isForValidatorAccount {
addr, err = wallet.NewValidatorAddress(label)
}

cmd.FatalErrorCheck(err)

err = wallet.Save()
Expand Down
2 changes: 2 additions & 0 deletions cmd/wallet/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
pathOpt *string
offlineOpt *bool
serverAddrOpt *string
walletAddrOpt *string

Check failure on line 15 in cmd/wallet/main.go

View workflow job for this annotation

GitHub Actions / linting

var `walletAddrOpt` is unused (unused)

Check failure on line 15 in cmd/wallet/main.go

View workflow job for this annotation

GitHub Actions / build-linux

var `walletAddrOpt` is unused (unused)
themantre marked this conversation as resolved.
Show resolved Hide resolved
)

func addPasswordOption(c *cobra.Command) *string {
Expand Down Expand Up @@ -53,6 +54,7 @@
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)
Expand Down
11 changes: 11 additions & 0 deletions wallet/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@
"google.golang.org/grpc/credentials/insecure"
)

type AddressType string
themantre marked this conversation as resolved.
Show resolved Hide resolved

var (
AddressTypeBLSAccount AddressType = "bls_account"
themantre marked this conversation as resolved.
Show resolved Hide resolved
AddressTypeValidator AddressType = "validator"
)

func (a AddressType) String() string {
return string(a)

Check warning on line 23 in wallet/client.go

View check run for this annotation

Codecov / codecov/patch

wallet/client.go#L23

Added line #L23 was not covered by tests
}

type grpcClient struct {
blockchainClient pactus.BlockchainClient
transactionClient pactus.TransactionClient
Expand Down
Loading