Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions action/candidate_register.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,27 @@ func NewCandidateRegister(
return cr, nil
}

// NewCandidateRegisterWithBLS creates a CandidateRegister instance with BLS public key
func NewCandidateRegisterWithBLS(
name, operatorAddrStr, rewardAddrStr, ownerAddrStr, amountStr string,
duration uint32,
autoStake bool,
payload []byte,
pubKey []byte,
) (*CandidateRegister, error) {
cr, err := NewCandidateRegister(name, operatorAddrStr, rewardAddrStr, ownerAddrStr, amountStr, duration, autoStake, payload)
if err != nil {
return nil, err
}
_, err = crypto.BLS12381PublicKeyFromBytes(pubKey)
if err != nil {
return nil, errors.Wrap(err, "failed to parse BLS public key")
}
cr.pubKey = make([]byte, len(pubKey))
copy(cr.pubKey, pubKey)
return cr, nil
}

// LegacyAmount returns the legacy amount
func (cr *CandidateRegister) LegacyAmount() *big.Int {
return cr.amount
Expand Down
31 changes: 31 additions & 0 deletions action/candidate_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,22 @@ type CandidateUpdate struct {
pubKey []byte
}

// CandidateUpdateOption defines the method to customize CandidateUpdate
type CandidateUpdateOption func(*CandidateUpdate) error

// WithCandidateUpdatePubKey sets the BLS public key for CandidateUpdate
func WithCandidateUpdatePubKey(pubKey []byte) CandidateUpdateOption {
return func(cu *CandidateUpdate) error {
_, err := crypto.BLS12381PublicKeyFromBytes(pubKey)
if err != nil {
return errors.Wrap(err, "failed to parse BLS public key")
}
cu.pubKey = make([]byte, len(pubKey))
copy(cu.pubKey, pubKey)
return nil
}
}

func init() {
var ok bool
_candidateUpdateMethod, ok = NativeStakingContractABI().Methods["candidateUpdate"]
Expand Down Expand Up @@ -82,6 +98,21 @@ func NewCandidateUpdate(name, operatorAddrStr, rewardAddrStr string) (*Candidate
return cu, nil
}

// NewCandidateUpdateWithBLS creates a CandidateUpdate instance with BLS public key
func NewCandidateUpdateWithBLS(name, operatorAddrStr, rewardAddrStr string, pubkey []byte) (*CandidateUpdate, error) {
cu, err := NewCandidateUpdate(name, operatorAddrStr, rewardAddrStr)
if err != nil {
return nil, err
}
_, err = crypto.BLS12381PublicKeyFromBytes(pubkey)
if err != nil {
return nil, errors.Wrap(err, "failed to parse BLS public key")
}
cu.pubKey = make([]byte, len(pubkey))
copy(cu.pubKey, pubkey)
return cu, nil
}

// Name returns candidate name to update
func (cu *CandidateUpdate) Name() string { return cu.name }

Expand Down
28 changes: 20 additions & 8 deletions ioctl/cmd/action/stake2register.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (

"github.com/spf13/cobra"

"github.com/iotexproject/go-pkgs/crypto"

"github.com/iotexproject/iotex-core/v2/action"
"github.com/iotexproject/iotex-core/v2/ioctl/config"
"github.com/iotexproject/iotex-core/v2/ioctl/output"
Expand All @@ -19,8 +21,8 @@ import (
// Multi-language support
var (
_registerCmdUses = map[config.Language]string{
config.English: "register NAME (ALIAS|OPERATOR_ADDRESS) (ALIAS|REWARD_ADDRESS) (ALIAS|OWNER_ADDRESS) AMOUNT_IOTX STAKE_DURATION [DATA] [--auto-stake] [-s SIGNER] [-n NONCE] [-l GAS_LIMIT] [-p GAS_PRICE] [-P PASSWORD] [-y]",
config.Chinese: "register 名字 (别名|操作者地址)(别名|奖励地址)(别名|所有者地址)IOTX数量 质押持续时间 [数据] [--auto-stake] [-s 签署人] [-n NONCE] [-l GAS限制] [-p GAS价格] [-P 密码] [-y]",
config.English: "register NAME (ALIAS|OPERATOR_ADDRESS) (ALIAS|REWARD_ADDRESS) (ALIAS|OWNER_ADDRESS) BLS_PUBKEY AMOUNT_IOTX STAKE_DURATION [DATA] [--auto-stake] [-s SIGNER] [-n NONCE] [-l GAS_LIMIT] [-p GAS_PRICE] [-P PASSWORD] [-y]",
config.Chinese: "register 名字 (别名|操作者地址)(别名|奖励地址)(别名|所有者地址)BLS公钥 IOTX数量 质押持续时间 [数据] [--auto-stake] [-s 签署人] [-n NONCE] [-l GAS限制] [-p GAS价格] [-P 密码] [-y]",
}

_registerCmdShorts = map[config.Language]string{
Expand All @@ -33,7 +35,7 @@ var (
var _stake2RegisterCmd = &cobra.Command{
Use: config.TranslateInLang(_registerCmdUses, config.UILanguage),
Short: config.TranslateInLang(_registerCmdShorts, config.UILanguage),
Args: cobra.RangeArgs(6, 7),
Args: cobra.RangeArgs(7, 8),
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
err := register(args)
Expand Down Expand Up @@ -65,20 +67,30 @@ func register(args []string) error {
return output.NewError(output.AddressError, "failed to get owner address", err)
}

amountInRau, err := util.StringToRau(args[4], util.IotxDecimalNum)
// Validate and parse BLS public key
blsPubKeyStr := args[4]
blsPubKeyBytes, err := hex.DecodeString(blsPubKeyStr)
Comment thread
envestcc marked this conversation as resolved.
if err != nil {
return output.NewError(output.ConvertError, "failed to decode BLS public key", err)
}
if _, err = crypto.BLS12381PublicKeyFromBytes(blsPubKeyBytes); err != nil {
return output.NewError(output.ValidationError, "invalid BLS public key", err)
}

amountInRau, err := util.StringToRau(args[5], util.IotxDecimalNum)
if err != nil {
return output.NewError(output.ConvertError, "invalid amount", err)
}

stakeDuration, err := parseStakeDuration(args[5])
stakeDuration, err := parseStakeDuration(args[6])
if err != nil {
return output.NewError(0, "", err)
}
duration := uint32(stakeDuration.Uint64())

var payload []byte
if len(args) == 7 {
payload, err = hex.DecodeString(args[6])
if len(args) == 8 {
payload, err = hex.DecodeString(args[7])
if err != nil {
return output.NewError(output.ConvertError, "failed to decode data", err)
}
Expand All @@ -103,7 +115,7 @@ func register(args []string) error {
if err != nil {
return output.NewError(0, "failed to get nonce ", err)
}
cr, err := action.NewCandidateRegister(name, operatorAddrStr, rewardAddrStr, ownerAddrStr, amountInRau.String(), duration, _stake2AutoStake, payload)
cr, err := action.NewCandidateRegisterWithBLS(name, operatorAddrStr, rewardAddrStr, ownerAddrStr, amountInRau.String(), duration, _stake2AutoStake, payload, blsPubKeyBytes)

if err != nil {
return output.NewError(output.InstantiationError, "failed to make a candidateRegister instance", err)
Expand Down
22 changes: 18 additions & 4 deletions ioctl/cmd/action/stake2update.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@
package action

import (
"encoding/hex"

"github.com/spf13/cobra"

"github.com/iotexproject/go-pkgs/crypto"

"github.com/iotexproject/iotex-core/v2/action"
"github.com/iotexproject/iotex-core/v2/ioctl/config"
"github.com/iotexproject/iotex-core/v2/ioctl/output"
Expand All @@ -17,9 +21,9 @@ import (
// Multi-language support
var (
_stake2UpdateCmdUses = map[config.Language]string{
config.English: "update NAME (ALIAS|OPERATOR_ADDRESS) (ALIAS|REWARD_ADDRESS)" +
config.English: "update NAME (ALIAS|OPERATOR_ADDRESS) (ALIAS|REWARD_ADDRESS) BLS_PUBKEY" +
" [-s SIGNER] [-n NONCE] [-l GAS_LIMIT] [-p GAS_PRICE] [-P PASSWORD] [-y]",
config.Chinese: "update 名字 (别名|操作者地址) (别名|奖励地址)" +
config.Chinese: "update 名字 (别名|操作者地址) (别名|奖励地址) BLS公钥" +
" [-s 签署人] [-n NONCE] [-l GAS限制] [-p GAS价格] [-P 密码] [-y]",
}
_stake2UpdateCmdShorts = map[config.Language]string{
Expand All @@ -31,7 +35,7 @@ var (
var _stake2UpdateCmd = &cobra.Command{
Use: config.TranslateInLang(_stake2UpdateCmdUses, config.UILanguage),
Short: config.TranslateInLang(_stake2UpdateCmdShorts, config.UILanguage),
Args: cobra.ExactArgs(3),
Args: cobra.ExactArgs(4),
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
err := stake2Update(args)
Expand All @@ -58,6 +62,16 @@ func stake2Update(args []string) error {
return output.NewError(output.AddressError, "failed to get reward address", err)
}

// Validate and parse BLS public key
blsPubKeyStr := args[3]
blsPubKeyBytes, err := hex.DecodeString(blsPubKeyStr)
Comment thread
envestcc marked this conversation as resolved.
if err != nil {
return output.NewError(output.ConvertError, "failed to decode BLS public key", err)
}
if _, err = crypto.BLS12381PublicKeyFromBytes(blsPubKeyBytes); err != nil {
return output.NewError(output.ValidationError, "invalid BLS public key", err)
}

sender, err := Signer()
if err != nil {
return output.NewError(output.AddressError, "failed to get signed address", err)
Expand All @@ -77,7 +91,7 @@ func stake2Update(args []string) error {
return output.NewError(0, "failed to get nonce ", err)
}

s2u, err := action.NewCandidateUpdate(name, operatorAddrStr, rewardAddrStr)
s2u, err := action.NewCandidateUpdateWithBLS(name, operatorAddrStr, rewardAddrStr, blsPubKeyBytes)
if err != nil {
return output.NewError(output.InstantiationError, "failed to make a candidateUpdate instance", err)
}
Expand Down