Skip to content

Commit

Permalink
feat: create new validator address (CLI and GUI) (#757)
Browse files Browse the repository at this point in the history
  • Loading branch information
jemiluv8 authored Oct 24, 2023
1 parent 0f628c1 commit 1804fd3
Show file tree
Hide file tree
Showing 9 changed files with 266 additions and 74 deletions.
136 changes: 136 additions & 0 deletions cmd/gtk/assets/ui/dialog_wallet_create_address.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.38.2 -->
<interface>
<requires lib="gtk+" version="3.24"/>
<object class="GtkDialog" id="id_dialog_wallet_create_address">
<property name="can-focus">False</property>
<property name="title" translatable="yes">New address</property>
<property name="default-width">320</property>
<property name="type-hint">dialog</property>
<child internal-child="vbox">
<object class="GtkBox">
<property name="can-focus">False</property>
<property name="margin-start">8</property>
<property name="margin-end">8</property>
<property name="margin-top">4</property>
<property name="margin-bottom">4</property>
<property name="orientation">vertical</property>
<property name="spacing">2</property>
<child internal-child="action_area">
<object class="GtkButtonBox">
<property name="can-focus">False</property>
<property name="layout-style">end</property>
<child>
<object class="GtkButton" id="id_button_cancel">
<property name="label">_Cancel</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="focus-on-click">False</property>
<property name="receives-default">False</property>
<property name="use-underline">True</property>
<property name="always-show-image">True</property>
<signal name="activate" handler="on_cancel" swapped="no"/>
<signal name="clicked" handler="on_cancel" swapped="no"/>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkButton" id="id_button_ok">
<property name="label">_Ok</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="focus-on-click">False</property>
<property name="can-default">True</property>
<property name="has-default">True</property>
<property name="receives-default">True</property>
<property name="use-underline">True</property>
<property name="always-show-image">True</property>
<signal name="activate" handler="on_ok" swapped="no"/>
<signal name="clicked" handler="on_ok" swapped="no"/>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">0</property>
</packing>
</child>
<child>
<!-- n-columns=2 n-rows=3 -->
<object class="GtkGrid">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="row-spacing">8</property>
<property name="column-spacing">8</property>
<child>
<object class="GtkLabel" id="id_label_account_label">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="halign">start</property>
<property name="label" translatable="yes">Label:</property>
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">0</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="id_entry_account_label">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="activates-default">True</property>
<property name="width-chars">24</property>
</object>
<packing>
<property name="left-attach">1</property>
<property name="top-attach">0</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="halign">start</property>
<property name="label" translatable="yes">Type:</property>
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">1</property>
</packing>
</child>
<child>
<object class="GtkComboBoxText" id="id_combo_address_type">
<property name="visible">True</property>
<property name="can-focus">False</property>
</object>
<packing>
<property name="left-attach">1</property>
<property name="top-attach">1</property>
</packing>
</child>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="padding">8</property>
<property name="position">1</property>
</packing>
</child>
</object>
</child>
<action-widgets>
<action-widget response="-2">id_button_cancel</action-widget>
<action-widget response="-3">id_button_ok</action-widget>
</action-widgets>
</object>
</interface>
73 changes: 73 additions & 0 deletions cmd/gtk/dialog_wallet_create_address.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//go:build gtk

package main

import (
_ "embed"
"fmt"

"github.com/gotk3/gotk3/gtk"

"github.com/pactus-project/pactus/wallet"
)

//go:embed assets/ui/dialog_wallet_create_address.ui
var uiWalletCreateAddressDialog []byte

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")

addressTypeCombo := getComboBoxTextObj(builder, "id_combo_address_type")
addressTypeCombo.Append(wallet.AddressTypeBLSAccount, "Account")
addressTypeCombo.Append(wallet.AddressTypeValidator, "Validator")

addressTypeCombo.SetActive(0)

getButtonObj(builder, "id_button_ok").SetImage(OkIcon())
getButtonObj(builder, "id_button_cancel").SetImage(CancelIcon())

onOk := func() {
walletAddressLabel, err := addressLabel.GetText()
fatalErrorCheck(err)

walletAddressType := addressTypeCombo.GetActiveID()
fatalErrorCheck(err)

if walletAddressType == wallet.AddressTypeBLSAccount {
_, err = ww.model.wallet.NewBLSAccountAddress(walletAddressLabel)
} else if walletAddressType == wallet.AddressTypeValidator {
_, err = ww.model.wallet.NewValidatorAddress(walletAddressLabel)
} else {
err = fmt.Errorf("invalid address type '%s'", walletAddressType)
}

errorCheck(err)

err = ww.model.wallet.Save()
errorCheck(err)

ww.model.rebuildModel()

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()
}
34 changes: 0 additions & 34 deletions cmd/gtk/model_wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,37 +84,3 @@ func (model *walletModel) rebuildModel() {
})
}()
}

func (model *walletModel) createAddress() error {
address, err := model.wallet.NewBLSAccountAddress("")
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
}
9 changes: 4 additions & 5 deletions cmd/gtk/widget_wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,14 @@ func buildWidgetWallet(model *walletModel) (*widgetWallet, error) {
return w, nil
}

func (ww *widgetWallet) onNewAddress() {
err := ww.model.createAddress()
errorCheck(err)
}

func (ww *widgetWallet) onChangePassword() {
changePassword(ww.model.wallet)
}

func (ww *widgetWallet) onNewAddress() {
createAddress(ww)
}

func (ww *widgetWallet) onShowSeed() {
password, ok := getWalletPassword(ww.model.wallet)
if !ok {
Expand Down
Loading

0 comments on commit 1804fd3

Please sign in to comment.