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: implement cmd to sign validator ownership #1004

Merged
merged 2 commits into from
Jan 22, 2024
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
2 changes: 2 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import (
"github.com/bnb-chain/node/plugins/dex/list"
"github.com/bnb-chain/node/plugins/dex/order"
dextypes "github.com/bnb-chain/node/plugins/dex/types"
migrate "github.com/bnb-chain/node/plugins/migrate"
tokenRecover "github.com/bnb-chain/node/plugins/recover"
"github.com/bnb-chain/node/plugins/tokens"
"github.com/bnb-chain/node/plugins/tokens/issue"
Expand Down Expand Up @@ -1178,6 +1179,7 @@ func MakeCodec() *wire.Codec {
oracle.RegisterWire(cdc)
ibc.RegisterWire(cdc)
tokenRecover.RegisterWire(cdc)
migrate.RegisterWire(cdc)
return cdc
}

Expand Down
2 changes: 2 additions & 0 deletions cmd/bnbcli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
apiserv "github.com/bnb-chain/node/plugins/api"
bridgecmd "github.com/bnb-chain/node/plugins/bridge/client/cli"
dexcmd "github.com/bnb-chain/node/plugins/dex/client/cli"
migratecmd "github.com/bnb-chain/node/plugins/migrate/client/cli"
recovercmd "github.com/bnb-chain/node/plugins/recover/client/cli"
tokencmd "github.com/bnb-chain/node/plugins/tokens/client/cli"
"github.com/bnb-chain/node/version"
Expand Down Expand Up @@ -101,6 +102,7 @@ func main() {
bridgecmd.AddCommands(rootCmd, cdc)
sidecmd.AddCommands(rootCmd, cdc)
recovercmd.AddCommands(rootCmd, cdc)
migratecmd.AddCommands(rootCmd, cdc)

// prepare and add flags
executor := cli.PrepareMainCmd(rootCmd, "BC", app.DefaultCLIHome)
Expand Down
22 changes: 22 additions & 0 deletions plugins/migrate/client/cli/commands.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package cli

import (
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/spf13/cobra"
)

func AddCommands(cmd *cobra.Command, cdc *codec.Codec) {
ownerShipCmd := &cobra.Command{
Use: "validator-ownership",
Short: "validator-ownership commands",
}

ownerShipCmd.AddCommand(
client.PostCommands(
SignValidatorOwnerShipCmd(cdc),
)...,
)

cmd.AddCommand(ownerShipCmd)
}
94 changes: 94 additions & 0 deletions plugins/migrate/client/cli/tx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package cli

import (
"encoding/hex"
"fmt"

"github.com/ethereum/go-ethereum/common"
"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/keys"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli"
authtxb "github.com/cosmos/cosmos-sdk/x/auth/client/txbuilder"

"github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/crypto/secp256k1"

"github.com/bnb-chain/node/plugins/migrate"
)

const (
flagBSCOperatorAddress = "bsc-operator-address"
)

func SignValidatorOwnerShipCmd(cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "sign-validator-ownership",
Short: "get validator ownership sign data",
RunE: func(cmd *cobra.Command, args []string) error {
txBldr := authtxb.NewTxBuilderFromCLI().WithCodec(cdc)
cliCtx := context.NewCLIContext().
WithCodec(cdc).
WithAccountDecoder(authcmd.GetAccountDecoder(cdc))
bscOperator := viper.GetString(flagBSCOperatorAddress)

return SignAndPrint(cliCtx, txBldr, common.HexToAddress(bscOperator))
},
}

cmd.Flags().String(flagBSCOperatorAddress, "", "bsc operator address")

return cmd
}

func SignAndPrint(ctx context.CLIContext, builder authtxb.TxBuilder, bscOperator common.Address) error {
name, err := ctx.GetFromName()
if err != nil {
return err
}

passphrase, err := keys.GetPassphrase(name)
if err != nil {
return err
}

msg := migrate.NewValidatorOwnerShipMsg(bscOperator)

// build and sign the transaction
stdMsg, err := builder.Build([]sdk.Msg{msg})
if err != nil {
return err
}
txBytes, err := builder.Sign(name, passphrase, stdMsg)
if err != nil {
return err
}

var tx auth.StdTx
err = builder.Codec.UnmarshalBinaryLengthPrefixed(txBytes, &tx)
if err != nil {
return err
}
json, err := builder.Codec.MarshalJSON(tx)
if err != nil {
return err
}

fmt.Printf("TX JSON: %s\n", json)
fmt.Println("Sign Message: ", string(stdMsg.Bytes()))
fmt.Println("Sign Message Hash: ", "0x"+hex.EncodeToString(crypto.Sha256(stdMsg.Bytes())))
sig := tx.GetSignatures()[0]
fmt.Printf("Signature: %s\n", "0x"+hex.EncodeToString(sig.Signature))
var originPubKey secp256k1.PubKeySecp256k1
err = builder.Codec.UnmarshalBinaryBare(sig.PubKey.Bytes(), &originPubKey)
if err != nil {
return err
}
fmt.Printf("PubKey: %s\n", "0x"+hex.EncodeToString(originPubKey))
return nil
}
75 changes: 75 additions & 0 deletions plugins/migrate/msg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package migrate

import (
"encoding/json"
"strings"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/common"
)

const (
Route = "recover"
MsgType = "validator_ownership"
)

var _ sdk.Msg = ValidatorOwnerShip{}

func NewValidatorOwnerShipMsg(
bscOperatorAddress common.Address,
) ValidatorOwnerShip {
return ValidatorOwnerShip{
BSCOperatorAddress: bscOperatorAddress,
}
}

func newValidatorOwnerShipSignData(
bscOperatorAddress common.Address) ValidatorOwnerShipSignData {
return ValidatorOwnerShipSignData{
BSCOperatorAddress: strings.ToLower(bscOperatorAddress.Hex()),
}
}

type ValidatorOwnerShipSignData struct {
BSCOperatorAddress string `json:"bsc_operator_address"`
}

type ValidatorOwnerShip struct {
BSCOperatorAddress common.Address `json:"bsc_operator_address"`
}

// GetInvolvedAddresses implements types.Msg.
func (msg ValidatorOwnerShip) GetInvolvedAddresses() []sdk.AccAddress {
return msg.GetSigners()
}

// GetSignBytes implements types.Msg.
func (msg ValidatorOwnerShip) GetSignBytes() []byte {
b, err := json.Marshal(newValidatorOwnerShipSignData(msg.BSCOperatorAddress))
if err != nil {
panic(err)
}
return b
}

// GetSigners implements types.Msg.
func (m ValidatorOwnerShip) GetSigners() []sdk.AccAddress {
// This is not a real on-chain transaction
// We can get signer from the public key.
return []sdk.AccAddress{}
}

// Route implements types.Msg.
func (ValidatorOwnerShip) Route() string {
return Route
}

// Type implements types.Msg.
func (ValidatorOwnerShip) Type() string {
return MsgType
}

// ValidateBasic implements types.Msg.
func (msg ValidatorOwnerShip) ValidateBasic() sdk.Error {
return nil
}
10 changes: 10 additions & 0 deletions plugins/migrate/wire.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package migrate

import (
"github.com/bnb-chain/node/wire"
)

// Register concrete types on wire codec
func RegisterWire(cdc *wire.Codec) {
cdc.RegisterConcrete(ValidatorOwnerShip{}, "migrate/ValidatorOwnerShip", nil)
}
6 changes: 3 additions & 3 deletions plugins/recover/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,14 @@ func SignAndPrint(ctx context.CLIContext, builder authtxb.TxBuilder, msg sdk.Msg

fmt.Printf("TX JSON: %s\n", json)
fmt.Println("Sign Message: ", string(stdMsg.Bytes()))
fmt.Println("Sign Message Hash: ", hex.EncodeToString(crypto.Sha256(stdMsg.Bytes())))
fmt.Println("Sign Message Hash: ", "0x"+hex.EncodeToString(crypto.Sha256(stdMsg.Bytes())))
sig := tx.GetSignatures()[0]
fmt.Printf("Signature: %s\n", hex.EncodeToString(sig.Signature))
fmt.Printf("Signature: %s\n", "0x"+hex.EncodeToString(sig.Signature))
var originPubKey secp256k1.PubKeySecp256k1
err = builder.Codec.UnmarshalBinaryBare(sig.PubKey.Bytes(), &originPubKey)
if err != nil {
return err
}
fmt.Printf("PubKey: %s\n", hex.EncodeToString(originPubKey))
fmt.Printf("PubKey: %s\n", "0x"+hex.EncodeToString(originPubKey))
return nil
}