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/QB-1485: Use eth_secp256k1 by default & add legacy withdraw #188

Merged
merged 3 commits into from
Dec 5, 2022
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
14 changes: 7 additions & 7 deletions client/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ The pass backend requires GnuPG: https://gnupg.org/
// support adding Ethereum supported keys
addCmd := keys.AddKeyCommand()

//// update the default signing algorithm value to "eth_secp256k1"
//algoFlag := addCmd.Flag("algo")
//algoFlag.DefValue = string(hd.EthSecp256k1Type)
//err := algoFlag.Value.Set(string(hd.EthSecp256k1Type))
//if err != nil {
// panic(err)
//}
// update the default signing algorithm value to "eth_secp256k1"
algoFlag := addCmd.Flag(flags.FlagKeyAlgorithm)
algoFlag.DefValue = string(stratoshd.EthSecp256k1Type)
err := algoFlag.Value.Set(string(stratoshd.EthSecp256k1Type))
if err != nil {
panic(err)
}

addCmd.RunE = runAddCmd

Expand Down
2 changes: 1 addition & 1 deletion client/keys/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ Example:
f.Uint32(flagCoinType, stratos.GetConfig().GetCoinType(), "coin type number for HD derivation")
f.Uint32(flagAccount, 0, "Account number for HD derivation")
f.Uint32(flagIndex, 0, "Address index number for HD derivation")
f.String(flags.FlagKeyAlgorithm, string(hd.Secp256k1Type), "Key signing algorithm to generate keys for")
f.String(flags.FlagKeyAlgorithm, string(stratoshd.EthSecp256k1Type), "Key signing algorithm to generate keys for")

return cmd
}
Expand Down
27 changes: 24 additions & 3 deletions proto/stratos/pot/v1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ syntax = "proto3";
package stratos.pot.v1;

import "gogoproto/gogo.proto";
//import "google/protobuf/any.proto";
import "google/api/annotations.proto";

//import "cosmos_proto/cosmos.proto";
import "stratos/pot/v1/pot.proto";
import "cosmos/base/v1beta1/coin.proto";

Expand All @@ -20,6 +17,9 @@ service Msg {
rpc HandleMsgWithdraw(MsgWithdraw) returns (MsgWithdrawResponse) {
option (google.api.http).post = "/stratos/pot/v1/withdraw";
};
rpc HandleMsgLegacyWithdraw(MsgLegacyWithdraw) returns (MsgLegacyWithdrawResponse) {
option (google.api.http).post = "/stratos/pot/v1/legacy_withdraw";
};
rpc HandleMsgFoundationDeposit(MsgFoundationDeposit) returns (MsgFoundationDepositResponse) {
option (google.api.http).post = "/stratos/pot/v1/foundation_deposit";
};
Expand Down Expand Up @@ -79,6 +79,27 @@ message MsgWithdraw {
// MsgWithdrawResponse defines the Msg/MsgWithdraw response type.
message MsgWithdrawResponse {}

// MsgLegacyWithdraw encapsulates an legacyWithdraw transaction as an SDK message.
message MsgLegacyWithdraw {
repeated cosmos.base.v1beta1.Coin amount = 1 [
(gogoproto.nullable) = false,
(gogoproto.jsontag) = "amount",
(gogoproto.moretags) = "yaml:\"amount\"",
(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins"
];
string from = 2 [
(gogoproto.jsontag) = "from",
(gogoproto.moretags) = "yaml:\"from\""
];
string target_address = 3 [
(gogoproto.jsontag) = "target_address",
(gogoproto.moretags) = "yaml:\"target_address\""
];
}

// MsgLegacyWithdrawResponse defines the Msg/MsgWithdraw response type.
message MsgLegacyWithdrawResponse {}

// MsgFoundationDeposit - encapsulates an FoundationDeposit transaction as an SDK message
message MsgFoundationDeposit {
repeated cosmos.base.v1beta1.Coin amount = 1 [
Expand Down
78 changes: 65 additions & 13 deletions x/pot/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func NewTxCmd() *cobra.Command {
potTxCmd.AddCommand(
VolumeReportCmd(),
WithdrawCmd(),
LegacyWithdrawCmd(),
FoundationDepositCmd(),
SlashingResourceNodeCmd(),
)
Expand Down Expand Up @@ -61,11 +62,8 @@ func WithdrawCmd() *cobra.Command {
},
}

//cmd.Flags().AddFlagSet(FsAmount)
//cmd.Flags().AddFlagSet(FsTargetAddress)
cmd.Flags().AddFlagSet(flagSetAmount())
cmd.Flags().AddFlagSet(flagSetTargetAddress())

flags.AddTxFlagsToCmd(cmd)

_ = cmd.MarkFlagRequired(FlagAmount)
Expand Down Expand Up @@ -99,21 +97,75 @@ func buildWithdrawMsg(clientCtx client.Context, txf tx.Factory, fs *flag.FlagSet
}
}

//if viper.IsSet(FlagTargetAddress) {
// targetAddressStr := viper.GetString(FlagTargetAddress)
// targetAddress, err = sdk.AccAddressFromBech32(targetAddressStr)
// if err != nil {
// return txf, nil, err
// }
//} else {
// targetAddress = walletAddress
//}

msg := types.NewMsgWithdraw(amount, walletAddress, targetAddress)

return txf, msg, nil
}

func LegacyWithdrawCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "legacy-withdraw",
Short: "withdraw POT reward recorded by legacy wallet address",
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

txf := tx.NewFactoryCLI(clientCtx, cmd.Flags()).
WithTxConfig(clientCtx.TxConfig).WithAccountRetriever(clientCtx.AccountRetriever)

txf, msg, err := buildLegacyWithdrawMsg(clientCtx, txf, cmd.Flags())
if err != nil {
return err
}

return tx.GenerateOrBroadcastTxWithFactory(clientCtx, txf, msg)
},
}

cmd.Flags().AddFlagSet(flagSetAmount())
cmd.Flags().AddFlagSet(flagSetTargetAddress())
flags.AddTxFlagsToCmd(cmd)

_ = cmd.MarkFlagRequired(FlagAmount)
_ = cmd.MarkFlagRequired(flags.FlagFrom)

return cmd
}

// makes a new LegacyWithdrawMsg.
func buildLegacyWithdrawMsg(clientCtx client.Context, txf tx.Factory, fs *flag.FlagSet) (tx.Factory, *types.MsgLegacyWithdraw, error) {
amountStr, err := fs.GetString(FlagAmount)
if err != nil {
return txf, nil, err
}
amount, err := sdk.ParseCoinsNormalized(amountStr)
if err != nil {
return txf, nil, err
}
from := clientCtx.GetFromAddress()

targetAddressStr, err := fs.GetString(FlagTargetAddress)
if err != nil {
return txf, nil, err
}

var targetAddress sdk.AccAddress
if targetAddressStr == "" {
targetAddress = from
} else {
targetAddress, err = sdk.AccAddressFromBech32(targetAddressStr)
if err != nil {
return txf, nil, err
}
}

msg := types.NewMsgLegacyWithdraw(amount, from, targetAddress)

return txf, msg, nil
}

// VolumeReportCmd will report wallets volume and sign it with the given key.
func VolumeReportCmd() *cobra.Command {
cmd := &cobra.Command{
Expand Down
7 changes: 3 additions & 4 deletions x/pot/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,18 @@ func NewHandler(k keeper.Keeper) sdk.Handler {
case *types.MsgVolumeReport:
res, err := msgServer.HandleMsgVolumeReport(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)
//return handleMsgVolumeReport(ctx, k, msg)
case *types.MsgWithdraw:
res, err := msgServer.HandleMsgWithdraw(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)
//return handleMsgWithdraw(ctx, k, msg)
case *types.MsgLegacyWithdraw:
res, err := msgServer.HandleMsgLegacyWithdraw(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)
case *types.MsgFoundationDeposit:
res, err := msgServer.HandleMsgFoundationDeposit(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)
//return handleMsgFoundationDeposit(ctx, k, msg)
case *types.MsgSlashingResourceNode:
res, err := msgServer.HandleMsgSlashingResourceNode(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)
//return handleMsgSlashingResourceNode(ctx, k, msg)
default:
errMsg := fmt.Sprintf("unrecognized %s message type: %T", types.ModuleName, msg)
return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, errMsg)
Expand Down
45 changes: 45 additions & 0 deletions x/pot/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"fmt"
"strconv"

"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/bech32"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
stratos "github.com/stratosnet/stratos-chain/types"
"github.com/stratosnet/stratos-chain/x/pot/types"
Expand Down Expand Up @@ -103,6 +105,49 @@ func (k msgServer) HandleMsgWithdraw(goCtx context.Context, msg *types.MsgWithdr
return &types.MsgWithdrawResponse{}, nil
}

func (k msgServer) HandleMsgLegacyWithdraw(goCtx context.Context, msg *types.MsgLegacyWithdraw) (*types.MsgLegacyWithdrawResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

targetAddress, err := sdk.AccAddressFromBech32(msg.TargetAddress)
if err != nil {
return &types.MsgLegacyWithdrawResponse{}, sdkerrors.Wrap(types.ErrInvalidAddress, err.Error())
}

fromAddress, err := sdk.AccAddressFromBech32(msg.From)
if err != nil {
return &types.MsgLegacyWithdrawResponse{}, sdkerrors.Wrap(types.ErrInvalidAddress, err.Error())
}

fromAcc := k.AccountKeeper.GetAccount(ctx, fromAddress)
pubKey := fromAcc.GetPubKey()
legacyPubKey := secp256k1.PubKey{Key: pubKey.Bytes()}
legacyWalletAddress := sdk.AccAddress(legacyPubKey.Address().Bytes())

legacyWalletAddrStr, err := bech32.ConvertAndEncode(stratos.AccountAddressPrefix, legacyWalletAddress.Bytes())
if err != nil {
return &types.MsgLegacyWithdrawResponse{}, sdkerrors.Wrap(types.ErrLegacyWithdrawFailure, err.Error())
}

err = k.Withdraw(ctx, msg.Amount, legacyWalletAddress, targetAddress)
if err != nil {
return &types.MsgLegacyWithdrawResponse{}, sdkerrors.Wrap(types.ErrLegacyWithdrawFailure, err.Error())
}

ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeLegacyWithdraw,
sdk.NewAttribute(types.AttributeKeyAmount, msg.Amount.String()),
sdk.NewAttribute(types.AttributeKeyLegacyWalletAddress, legacyWalletAddrStr),
),
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(sdk.AttributeKeySender, msg.From),
),
})
return &types.MsgLegacyWithdrawResponse{}, nil
}

func (k msgServer) HandleMsgFoundationDeposit(goCtx context.Context, msg *types.MsgFoundationDeposit) (*types.MsgFoundationDepositResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
from, err := sdk.AccAddressFromBech32(msg.From)
Expand Down
4 changes: 4 additions & 0 deletions x/pot/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ const (
codeErrSlashingResourceNodeFailure
codeErrRewardDistributionNotComplete
codeErrVolumeReport
codeErrLegacyAddressNotMatch
codeErrLegacyWithdrawFailure
)

var (
Expand Down Expand Up @@ -68,4 +70,6 @@ var (
ErrSlashingResourceNodeFailure = sdkerrors.Register(ModuleName, codeErrSlashingResourceNodeFailure, "failure during slashing resource node")
ErrRewardDistributionNotComplete = sdkerrors.Register(ModuleName, codeErrRewardDistributionNotComplete, "Reward distribution not completed")
ErrVolumeReport = sdkerrors.Register(ModuleName, codeErrVolumeReport, "volume report failed")
ErrLegacyAddressNotMatch = sdkerrors.Register(ModuleName, codeErrLegacyAddressNotMatch, "public key does not mathe the legacy wallet address")
ErrLegacyWithdrawFailure = sdkerrors.Register(ModuleName, codeErrLegacyWithdrawFailure, "failure during legacyWithdraw")
)
16 changes: 9 additions & 7 deletions x/pot/types/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@ package types
const (
EventTypeVolumeReport = "volume_report"
EventTypeWithdraw = "withdraw"
EventTypeLegacyWithdraw = "legacy_withdraw"
EventTypeFoundationDeposit = "foundation_deposit"
EventTypeSlashing = "slashing"

AttributeKeyEpoch = "epoch"
AttributeKeyReportReference = "report_reference"
AttributeKeyAmount = "amount"
AttributeKeyWalletAddress = "wallet_address"
AttributeKeyNodeP2PAddress = "p2p_address"
AttributeKeySlashingNodeType = "slashing_type"
AttributeKeyNodeSuspended = "suspend"
AttributeKeyEpoch = "epoch"
AttributeKeyReportReference = "report_reference"
AttributeKeyAmount = "amount"
AttributeKeyWalletAddress = "wallet_address"
AttributeKeyLegacyWalletAddress = "legacy_wallet_address"
AttributeKeyNodeP2PAddress = "p2p_address"
AttributeKeySlashingNodeType = "slashing_type"
AttributeKeyNodeSuspended = "suspend"

AttributeValueCategory = ModuleName
)
47 changes: 47 additions & 0 deletions x/pot/types/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import (
const (
VolumeReportMsgType = "volume_report"
WithdrawMsgType = "withdraw"
LegacyWithdrawMsgType = "legacy_withdraw"
FoundationDepositMsgType = "foundation_deposit"
)

// verify interface at compile time
var (
_ sdk.Msg = &MsgVolumeReport{}
_ sdk.Msg = &MsgWithdraw{}
_ sdk.Msg = &MsgLegacyWithdraw{}
_ sdk.Msg = &MsgFoundationDeposit{}
_ sdk.Msg = &MsgSlashingResourceNode{}
)
Expand Down Expand Up @@ -166,6 +168,51 @@ func (msg MsgWithdraw) ValidateBasic() error {
return nil
}

func NewMsgLegacyWithdraw(amount sdk.Coins, from sdk.AccAddress, targetAddress sdk.AccAddress) *MsgLegacyWithdraw {
return &MsgLegacyWithdraw{
Amount: amount,
From: from.String(),
TargetAddress: targetAddress.String(),
}
}

// Route Implement
func (msg MsgLegacyWithdraw) Route() string { return RouterKey }

// GetSigners Implement
func (msg MsgLegacyWithdraw) GetSigners() []sdk.AccAddress {
var addrs []sdk.AccAddress
from, err := sdk.AccAddressFromBech32(msg.From)
if err != nil {
return addrs
}
addrs = append(addrs, from)
return addrs
}

// Type Implement
func (msg MsgLegacyWithdraw) Type() string { return LegacyWithdrawMsgType }

// GetSignBytes gets the bytes for the message signer to sign on
func (msg MsgLegacyWithdraw) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(msg)
return sdk.MustSortJSON(bz)
}

// ValidateBasic validity check for the AnteHandler
func (msg MsgLegacyWithdraw) ValidateBasic() error {
if !(msg.Amount.IsValid()) {
return ErrWithdrawAmountInvalid
}
if len(msg.From) == 0 {
return ErrEmptyFromAddr
}
if len(msg.TargetAddress) == 0 {
return ErrMissingTargetAddress
}
return nil
}

func NewMsgFoundationDeposit(amount sdk.Coins, from sdk.AccAddress) *MsgFoundationDeposit {
return &MsgFoundationDeposit{
Amount: amount,
Expand Down
Loading