Skip to content

Commit

Permalink
Custody module - #444, #447
Browse files Browse the repository at this point in the history
  • Loading branch information
kobs30 committed May 30, 2023
1 parent a7acde3 commit a6f8aaa
Show file tree
Hide file tree
Showing 13 changed files with 882 additions and 235 deletions.
16 changes: 8 additions & 8 deletions app/ante/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ func (cd CustodyDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool,
switch kiratypes.MsgType(msg) {
case kiratypes.MsgTypeCreateCustody:
{
msg, ok := msg.(*custodytypes.MsgCreteCustodyRecord)
msg, ok := msg.(*custodytypes.MsgCreateCustodyRecord)
if !ok {
return ctx, sdkerrors.Wrap(sdkerrors.ErrInvalidType, "Not a MsgCreteCustodyRecord")
return ctx, sdkerrors.Wrap(sdkerrors.ErrInvalidType, "Not a MsgCreateCustodyRecord")
}

hash := sha256.Sum256([]byte(msg.OldKey))
Expand All @@ -107,7 +107,7 @@ func (cd CustodyDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool,
{
msg, ok := msg.(*custodytypes.MsgAddToCustodyWhiteList)
if !ok {
return ctx, sdkerrors.Wrap(sdkerrors.ErrInvalidType, "Not a MsgCreteCustodyRecord")
return ctx, sdkerrors.Wrap(sdkerrors.ErrInvalidType, "Not a MsgCreateCustodyRecord")
}

hash := sha256.Sum256([]byte(msg.OldKey))
Expand All @@ -125,7 +125,7 @@ func (cd CustodyDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool,
{
msg, ok := msg.(*custodytypes.MsgAddToCustodyCustodians)
if !ok {
return ctx, sdkerrors.Wrap(sdkerrors.ErrInvalidType, "Not a MsgCreteCustodyRecord")
return ctx, sdkerrors.Wrap(sdkerrors.ErrInvalidType, "Not a MsgCreateCustodyRecord")
}

hash := sha256.Sum256([]byte(msg.OldKey))
Expand All @@ -143,7 +143,7 @@ func (cd CustodyDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool,
{
msg, ok := msg.(*custodytypes.MsgRemoveFromCustodyCustodians)
if !ok {
return ctx, sdkerrors.Wrap(sdkerrors.ErrInvalidType, "Not a MsgCreteCustodyRecord")
return ctx, sdkerrors.Wrap(sdkerrors.ErrInvalidType, "Not a MsgCreateCustodyRecord")
}

hash := sha256.Sum256([]byte(msg.OldKey))
Expand All @@ -161,7 +161,7 @@ func (cd CustodyDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool,
{
msg, ok := msg.(*custodytypes.MsgDropCustodyCustodians)
if !ok {
return ctx, sdkerrors.Wrap(sdkerrors.ErrInvalidType, "Not a MsgCreteCustodyRecord")
return ctx, sdkerrors.Wrap(sdkerrors.ErrInvalidType, "Not a MsgCreateCustodyRecord")
}

hash := sha256.Sum256([]byte(msg.OldKey))
Expand All @@ -179,7 +179,7 @@ func (cd CustodyDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool,
{
msg, ok := msg.(*custodytypes.MsgRemoveFromCustodyWhiteList)
if !ok {
return ctx, sdkerrors.Wrap(sdkerrors.ErrInvalidType, "Not a MsgCreteCustodyRecord")
return ctx, sdkerrors.Wrap(sdkerrors.ErrInvalidType, "Not a MsgCreateCustodyRecord")
}

hash := sha256.Sum256([]byte(msg.OldKey))
Expand All @@ -197,7 +197,7 @@ func (cd CustodyDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool,
{
msg, ok := msg.(*custodytypes.MsgDropCustodyWhiteList)
if !ok {
return ctx, sdkerrors.Wrap(sdkerrors.ErrInvalidType, "Not a MsgCreteCustodyRecord")
return ctx, sdkerrors.Wrap(sdkerrors.ErrInvalidType, "Not a MsgCreateCustodyRecord")
}

hash := sha256.Sum256([]byte(msg.OldKey))
Expand Down
24 changes: 21 additions & 3 deletions proto/kira/custody/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ option go_package = "github.com/KiraCore/sekai/x/custody/types";

// Msg defines the custody Msg service.
service Msg {
rpc CreateCustody(MsgCreteCustodyRecord) returns (MsgCreteCustodyRecordResponse);
rpc CreateCustody(MsgCreateCustodyRecord) returns (MsgCreateCustodyRecordResponse);
rpc DisableCustody(MsgDisableCustodyRecord) returns (MsgDisableCustodyRecordResponse);
rpc DropCustody(MsgDropCustodyRecord) returns (MsgDropCustodyRecordResponse);
rpc AddToCustodians(MsgAddToCustodyCustodians) returns (MsgAddToCustodyCustodiansResponse);
rpc RemoveFromCustodians(MsgRemoveFromCustodyCustodians) returns (MsgRemoveFromCustodyCustodiansResponse);
rpc DropCustodians(MsgDropCustodyCustodians) returns (MsgDropCustodyCustodiansResponse);
Expand All @@ -28,7 +29,7 @@ service Msg {
rpc PasswordConfirm(MsgPasswordConfirmTransaction) returns (MsgPasswordConfirmTransactionResponse);
}

message MsgCreteCustodyRecord {
message MsgCreateCustodyRecord {
option (gogoproto.equal) = false;
option (gogoproto.goproto_getters) = false;

Expand All @@ -55,6 +56,22 @@ message MsgDisableCustodyRecord {
(gogoproto.moretags) = "yaml:\"address\""
];

string old_key = 2;
string new_key = 3;

string next_address = 4;
string target_address = 5;
}

message MsgDropCustodyRecord {
option (gogoproto.equal) = false;
option (gogoproto.goproto_getters) = false;

bytes address = 1 [
(gogoproto.casttype) = "github.com/cosmos/cosmos-sdk/types.AccAddress",
(gogoproto.moretags) = "yaml:\"address\""
];

string old_key = 2;
string target_address = 3 ;
}
Expand Down Expand Up @@ -308,8 +325,9 @@ message MsgPasswordConfirmTransaction {
string password = 4;
}

message MsgCreteCustodyRecordResponse {}
message MsgCreateCustodyRecordResponse {}
message MsgDisableCustodyRecordResponse {}
message MsgDropCustodyRecordResponse {}
message MsgAddToCustodyCustodiansResponse {}
message MsgRemoveFromCustodyCustodiansResponse {}
message MsgDropCustodyCustodiansResponse {}
Expand Down
4 changes: 2 additions & 2 deletions scripts/test-local/custody-processes.sh
Original file line number Diff line number Diff line change
Expand Up @@ -272,11 +272,11 @@ sendTokens validator "$ACCOUNT5_ADDRESS" 1000000000000 ukex 100 ukex
# Send tokens with enabled custodians and enabled password protection --->

# TEST 4/1
# Disable custodians --->
# Drop custodians --->
echoInfo "TEST 4/1"
TESTER1_BALANCE_EXPECTED=$(($TESTER1_BALANCE_EXPECTED - 150)) # -150 fee

disableCustody tester1 150 ukex $KEY3
dropCustody tester1 150 ukex $KEY3

CUSTODY_KEY=$(getCustodyKey tester1)
TESTER1_BALANCE_REAL=$(showBalance tester1 ukex)
Expand Down
1 change: 1 addition & 0 deletions types/Msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ const (
// custody module
MsgTypeCreateCustody = "create-custody"
MsgTypeDisableCustody = "disable-custody"
MsgTypeDropCustody = "drop-custody"
MsgTypeAddToCustodyWhiteList = "add-to-custody-whitelist"
MsgTypeAddToCustodyCustodians = "add-to-custody-custodians"
MsgTypeRemoveFromCustodyCustodians = "remove-from-custody-custodians"
Expand Down
59 changes: 59 additions & 0 deletions x/custody/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func NewTxCmd() *cobra.Command {

txCmd.AddCommand(GetTxCreateCustody())
txCmd.AddCommand(GetTxDisableCustody())
txCmd.AddCommand(GetTxDropCustody())
txCmd.AddCommand(NewCustodiansTxCmd())
txCmd.AddCommand(NewWhiteListTxCmd())
txCmd.AddCommand(NewLimitsTxCmd())
Expand Down Expand Up @@ -644,6 +645,48 @@ func GetTxCreateCustody() *cobra.Command {
return cmd
}

func GetTxDropCustody() *cobra.Command {
cmd := &cobra.Command{
Use: "drop",
Short: "Drop custody settings",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

oldKey, err := cmd.Flags().GetString(OldKey)
if err != nil {
return fmt.Errorf("invalid old key: %w", err)
}

targetAddr, err := cmd.Flags().GetString(TargetAddress)
if err != nil {
return fmt.Errorf("invalid target address: %w", err)
}

msg := types.NewMsgDropCustody(
clientCtx.FromAddress,
oldKey,
targetAddr,
)

return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}

cmd.Flags().String(OldKey, "", "Previous hash string.")
cmd.MarkFlagRequired(OldKey)

cmd.Flags().String(TargetAddress, "", "Target of the control request.")

flags.AddTxFlagsToCmd(cmd)
cmd.MarkFlagRequired(flags.FlagFrom)

return cmd
}

func GetTxDisableCustody() *cobra.Command {
cmd := &cobra.Command{
Use: "disable",
Expand All @@ -660,6 +703,16 @@ func GetTxDisableCustody() *cobra.Command {
return fmt.Errorf("invalid old key: %w", err)
}

newKey, err := cmd.Flags().GetString(NewKey)
if err != nil {
return fmt.Errorf("invalid new key: %w", err)
}

nextAddr, err := cmd.Flags().GetString(NextAddress)
if err != nil {
return fmt.Errorf("invalid next address: %w", err)
}

targetAddr, err := cmd.Flags().GetString(TargetAddress)
if err != nil {
return fmt.Errorf("invalid target address: %w", err)
Expand All @@ -668,6 +721,8 @@ func GetTxDisableCustody() *cobra.Command {
msg := types.NewMsgDisableCustody(
clientCtx.FromAddress,
oldKey,
newKey,
nextAddr,
targetAddr,
)

Expand All @@ -678,6 +733,10 @@ func GetTxDisableCustody() *cobra.Command {
cmd.Flags().String(OldKey, "", "Previous hash string.")
cmd.MarkFlagRequired(OldKey)

cmd.Flags().String(NewKey, "", "Next hash string.")
cmd.MarkFlagRequired(NewKey)

cmd.Flags().String(NextAddress, "", "Next address to control the settings.")
cmd.Flags().String(TargetAddress, "", "Target of the control request.")

flags.AddTxFlagsToCmd(cmd)
Expand Down
5 changes: 4 additions & 1 deletion x/custody/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@ func NewHandler(ck keeper.Keeper, cgk types.CustomGovKeeper, bk types.BankKeeper
ctx = ctx.WithEventManager(sdk.NewEventManager())

switch msg := msg.(type) {
case *types.MsgCreteCustodyRecord:
case *types.MsgCreateCustodyRecord:
res, err := msgServer.CreateCustody(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)
case *types.MsgDisableCustodyRecord:
res, err := msgServer.DisableCustody(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)
case *types.MsgDropCustodyRecord:
res, err := msgServer.DropCustody(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)
case *types.MsgAddToCustodyCustodians:
res, err := msgServer.AddToCustodians(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)
Expand Down
11 changes: 10 additions & 1 deletion x/custody/keeper/custody.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,16 @@ func (k Keeper) SetCustodyRecord(ctx sdk.Context, record types.CustodyRecord) {
store.Set(key, k.cdc.MustMarshal(record.CustodySettings))
}

func (k Keeper) DisableCustodyRecord(ctx sdk.Context, address sdk.AccAddress) {
func (k Keeper) DisableCustodyRecord(ctx sdk.Context, record types.CustodyRecord) {
store := ctx.KVStore(k.storeKey)
key := append([]byte(types.PrefixKeyCustodyRecord), record.Address...)

record.CustodySettings.CustodyEnabled = false

store.Set(key, k.cdc.MustMarshal(record.CustodySettings))
}

func (k Keeper) DropCustodyRecord(ctx sdk.Context, address sdk.AccAddress) {
store := ctx.KVStore(k.storeKey)
key := append([]byte(types.PrefixKeyCustodyRecord), address...)

Expand Down
38 changes: 35 additions & 3 deletions x/custody/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func NewMsgServerImpl(keeper Keeper, cgk types.CustomGovKeeper, bk types.BankKee

var _ types.MsgServer = msgServer{}

func (s msgServer) CreateCustody(goCtx context.Context, msg *types.MsgCreteCustodyRecord) (*types.MsgCreteCustodyRecordResponse, error) {
func (s msgServer) CreateCustody(goCtx context.Context, msg *types.MsgCreateCustodyRecord) (*types.MsgCreateCustodyRecordResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
record := types.CustodyRecord{
Address: msg.Address,
Expand All @@ -42,16 +42,48 @@ func (s msgServer) CreateCustody(goCtx context.Context, msg *types.MsgCreteCusto

s.keeper.SetCustodyRecord(ctx, record)

return &types.MsgCreteCustodyRecordResponse{}, nil
return &types.MsgCreateCustodyRecordResponse{}, nil
}

func (s msgServer) DisableCustody(goCtx context.Context, msg *types.MsgDisableCustodyRecord) (*types.MsgDisableCustodyRecordResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
s.keeper.DisableCustodyRecord(ctx, msg.Address)

if msg.TargetAddress != "" {
targetAddr, err := sdk.AccAddressFromBech32(msg.TargetAddress)
if err != nil {
return nil, errors.Wrap(types.ErrWrongTargetAddr, "Can not convert string to address")
}

msg.Address = targetAddr
}

record := types.CustodyRecord{
Address: msg.Address,
CustodySettings: s.keeper.GetCustodyInfoByAddress(ctx, msg.Address),
}

s.keeper.DisableCustodyRecord(ctx, record)

return &types.MsgDisableCustodyRecordResponse{}, nil
}

func (s msgServer) DropCustody(goCtx context.Context, msg *types.MsgDropCustodyRecord) (*types.MsgDropCustodyRecordResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

if msg.TargetAddress != "" {
targetAddr, err := sdk.AccAddressFromBech32(msg.TargetAddress)
if err != nil {
return nil, errors.Wrap(types.ErrWrongTargetAddr, "Can not convert string to address")
}

msg.Address = targetAddr
}

s.keeper.DropCustodyRecord(ctx, msg.Address)

return &types.MsgDropCustodyRecordResponse{}, nil
}

func (s msgServer) AddToCustodians(goCtx context.Context, msg *types.MsgAddToCustodyCustodians) (*types.MsgAddToCustodyCustodiansResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

Expand Down
64 changes: 32 additions & 32 deletions x/custody/types/codec.go
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
package types

import (
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/msgservice"
)

// RegisterCodec register codec and metadata
func RegisterCodec(cdc *codec.LegacyAmino) {
cdc.RegisterConcrete(&MsgCreteCustodyRecord{}, "kiraHub/MsgCreteCustodyRecord", nil)
}

// RegisterInterfaces register Msg and structs
func RegisterInterfaces(registry types.InterfaceRegistry) {
registry.RegisterImplementations((*sdk.Msg)(nil),
&MsgCreteCustodyRecord{},
)

msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc)
}

var (
amino = codec.NewLegacyAmino()
ModuleCdc = codec.NewAminoCodec(amino)
)

func init() {
RegisterCodec(amino)
amino.Seal()
}
package types

import (
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/msgservice"
)

// RegisterCodec register codec and metadata
func RegisterCodec(cdc *codec.LegacyAmino) {
cdc.RegisterConcrete(&MsgCreateCustodyRecord{}, "kiraHub/MsgCreateCustodyRecord", nil)
}

// RegisterInterfaces register Msg and structs
func RegisterInterfaces(registry types.InterfaceRegistry) {
registry.RegisterImplementations((*sdk.Msg)(nil),
&MsgCreateCustodyRecord{},
)

msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc)
}

var (
amino = codec.NewLegacyAmino()
ModuleCdc = codec.NewAminoCodec(amino)
)

func init() {
RegisterCodec(amino)
amino.Seal()
}
Loading

0 comments on commit a6f8aaa

Please sign in to comment.