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(x/asset): Add clawback and transfer auth priviledges #176

Open
wants to merge 9 commits into
base: example-priv
Choose a base branch
from
Open
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
26 changes: 26 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@
assetmodulekeeper "github.com/realiotech/realio-network/x/asset/keeper"
assetmoduletypes "github.com/realiotech/realio-network/x/asset/types"

"github.com/realiotech/realio-network/x/asset/priviledges/clawback"

Check failure on line 129 in app/app.go

View workflow job for this annotation

GitHub Actions / lint

could not import github.com/realiotech/realio-network/x/asset/priviledges/clawback (-: # github.com/realiotech/realio-network/x/asset/priviledges/clawback
"github.com/realiotech/realio-network/x/asset/priviledges/freeze"
mintpriviledge "github.com/realiotech/realio-network/x/asset/priviledges/mint"
"github.com/realiotech/realio-network/x/asset/priviledges/transfer_auth"

realionetworktypes "github.com/realiotech/realio-network/types"

// unnamed import of statik for swagger UI support
Expand Down Expand Up @@ -309,6 +314,8 @@
// multi-staking keys
multistakingtypes.StoreKey,
// this line is used by starport scaffolding # stargate/app/storeKey
freeze.StoreKey,
transfer_auth.StoreKey,
)

// Add the EVM transient store key
Expand Down Expand Up @@ -415,6 +422,25 @@
app.AccountKeeper,
)

err := app.AssetKeeper.AddPrivilege(mintpriviledge.NewMintPriviledge(app.BankKeeper))
if err != nil {
panic(err)
}
err = app.AssetKeeper.AddPrivilege(freeze.NewFreezePriviledge(keys[freeze.StoreKey]))
if err != nil {
panic(err)
}
err = app.AssetKeeper.AddPrivilege(transfer_auth.NewTransferAuthPriviledge(keys[transfer_auth.StoreKey]))
if err != nil {
panic(err)
}
err = app.AssetKeeper.AddPrivilege(clawback.NewClawbackPriviledge(app.BankKeeper))
if err != nil {
panic(err)
}

app.BankKeeper.AppendSendRestriction(app.AssetKeeper.AssetSendRestriction)

// IBC Keeper
app.IBCKeeper = ibckeeper.NewKeeper(
appCodec, keys[ibchost.StoreKey], app.GetSubspace(ibchost.ModuleName), app.StakingKeeper, app.UpgradeKeeper, scopedIBCKeeper,
Expand Down
45 changes: 45 additions & 0 deletions app/apptesting/test_suite.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package apptesting

import (
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/suite"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"

bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
govkeeper "github.com/cosmos/cosmos-sdk/x/gov/keeper"
"github.com/realiotech/realio-network/app"
"github.com/realiotech/realio-network/x/asset/keeper"
"github.com/realiotech/realio-network/x/asset/types"
)

type KeeperTestSuite struct {
suite.Suite
app *app.RealioNetwork
ctx sdk.Context

assetKeeper *keeper.Keeper
govkeeper govkeeper.Keeper
msgServer types.MsgServer
bankKeeper bankkeeper.Keeper
}

func (suite *KeeperTestSuite) SetupTest() {
app := app.Setup(false, nil, 3)

suite.app = app
suite.ctx = app.BaseApp.NewContext(false, tmproto.Header{Height: app.LastBlockHeight() + 1})
suite.assetKeeper = keeper.NewKeeper(
app.AppCodec(), app.InterfaceRegistry(), app.GetKey(types.StoreKey),
app.GetMemKey(types.MemStoreKey), app.GetSubspace(types.ModuleName), app.BankKeeper, app.AccountKeeper,
)
suite.govkeeper = app.GovKeeper
suite.bankKeeper = app.BankKeeper
}

func (suite *KeeperTestSuite)

Check failure on line 41 in app/apptesting/test_suite.go

View workflow job for this annotation

GitHub Actions / lint

expected 'IDENT', found newline (typecheck)

func TestKeeperTestSuite(t *testing.T) {

Check failure on line 43 in app/apptesting/test_suite.go

View workflow job for this annotation

GitHub Actions / lint

expected '(', found 'func' (typecheck)
suite.Run(t, new(KeeperTestSuite))
}
11 changes: 11 additions & 0 deletions proto/realionetwork/asset/priviledges/clawback/messages.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
syntax = "proto3";
package realionetwork.asset.v1;

option go_package = "github.com/realiotech/realio-network/x/asset/priviledges/clawback";

import "cosmos_proto/cosmos.proto";

message MsgClawbackToken {
string account = 1 [ (cosmos_proto.scalar) = "cosmos.AddressString" ];
uint64 amount = 2;
}
14 changes: 14 additions & 0 deletions proto/realionetwork/asset/priviledges/freeze/messages.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
syntax = "proto3";
package realionetwork.asset.v1;

option go_package = "github.com/realiotech/realio-network/x/asset/priviledges/freeze";

import "cosmos_proto/cosmos.proto";

message MsgFreezeToken {
repeated string accounts = 1 [ (cosmos_proto.scalar) = "cosmos.AddressString" ];
}

message MsgUnfreezeToken {
repeated string accounts = 1 [ (cosmos_proto.scalar) = "cosmos.AddressString" ];
}
6 changes: 2 additions & 4 deletions proto/realionetwork/asset/priviledges/mint/messages.proto
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import "realionetwork/asset/v1/token.proto";


message MsgMintToken {
string account = 1 [ (cosmos_proto.scalar) = "cosmos.AddressString" ];
string token_id = 2;
string to_account = 3;
uint64 amount = 4;
string to_account = 1;
uint64 amount = 2;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
syntax = "proto3";
package realionetwork.asset.v1;

option go_package = "github.com/realiotech/realio-network/x/asset/priviledges/transfer_auth";

import "cosmos_proto/cosmos.proto";

message MsgUpdateAllowList {
repeated string allowed_addresses = 1 [ (cosmos_proto.scalar) = "cosmos.AddressString" ];
repeated string disallowed_addresses = 2 [ (cosmos_proto.scalar) = "cosmos.AddressString"];
}
22 changes: 22 additions & 0 deletions proto/realionetwork/asset/priviledges/transfer_auth/query.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
syntax = "proto3";
package realionetwork.asset.v1;

option go_package = "github.com/realiotech/realio-network/x/asset/priviledges/transfer_auth";

import "cosmos_proto/cosmos.proto";

message QueryWhitelistedAddressesRequest {
string token_id = 1;
}

message QueryWhitelistedAddressesResponse {
repeated string whitelisted_addrs = 1;
}

message QueryIsAddressWhitelistedRequest {
string address = 1 [ (cosmos_proto.scalar) = "cosmos.AddressString" ];
}

message QueryIsAddressWhitelistedRespones {
bool is_whitelisted = 1;
}
1 change: 1 addition & 0 deletions proto/realionetwork/asset/v1/token.proto
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ message TokenManagement {
string manager = 1 [ (cosmos_proto.scalar) = "cosmos.AddressString" ];
bool add_new_privilege = 2;
repeated string excluded_privileges = 3;
repeated string enabled_privileges = 4;
}

message Balance {
Expand Down
1 change: 1 addition & 0 deletions proto/realionetwork/asset/v1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ message MsgCreateToken {
string description = 6;
repeated string excluded_privileges = 7;
bool add_new_privilege = 8;
repeated string enabled_privileges = 9;
}

message MsgCreateTokenResponse {}
Expand Down
21 changes: 14 additions & 7 deletions x/asset/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ type (
Keeper struct {
cdc codec.BinaryCodec
// registry is used to register privilege interface and implementation.
registry cdctypes.InterfaceRegistry
storeKey storetypes.StoreKey
memKey storetypes.StoreKey
paramstore paramtypes.Subspace
bankKeeper types.BankKeeper
ak types.AccountKeeper
PrivilegeManager map[string]types.PrivilegeI
registry cdctypes.InterfaceRegistry
storeKey storetypes.StoreKey
memKey storetypes.StoreKey
paramstore paramtypes.Subspace
bankKeeper types.BankKeeper
ak types.AccountKeeper
PrivilegeManager map[string]types.PrivilegeI
RestrictionChecker map[string]RestrictionChecker
}
)

Expand Down Expand Up @@ -68,6 +69,12 @@ func (k *Keeper) AddPrivilege(priv types.PrivilegeI) error {
// regiester the privilege's interfaces
priv.RegisterInterfaces(k.registry)

checker, ok := priv.(RestrictionChecker)
// currently we should only support one restriction checker at a time
if ok {
k.RestrictionChecker[priv.Name()] = checker
}

return nil
}

Expand Down
6 changes: 3 additions & 3 deletions x/asset/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ func (m MockPrivilegeI) RegisterInterfaces(registry cdctypes.InterfaceRegistry)
}

func (m MockPrivilegeI) MsgHandler() types.MsgHandler {
return func(context context.Context, privMsg sdk.Msg, tokenID string, privAcc sdk.AccAddress) (proto.Message, error) {
typeUrl := sdk.MsgTypeURL(privMsg)
return func(context context.Context, privMsg proto.Message, tokenID string, privAcc sdk.AccAddress) (proto.Message, error) {
typeUrl := "/" + proto.MessageName(privMsg)
if typeUrl != sdk.MsgTypeURL(&types.MsgMock{}) {
return nil, errors.New("unsupport msg type")
}
Expand All @@ -124,7 +124,7 @@ func (m MockPrivilegeI) MsgHandler() types.MsgHandler {
}

func (m MockPrivilegeI) QueryHandler() types.QueryHandler {
return func(context context.Context, privQuery sdk.Msg) (proto.Message, error) {
return func(context context.Context, privQuery proto.Message, tokenID string) (proto.Message, error) {
return nil, nil
}
}
Expand Down
14 changes: 10 additions & 4 deletions x/asset/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import (
"context"
"fmt"
"slices"

Check failure on line 6 in x/asset/keeper/msg_server.go

View workflow job for this annotation

GitHub Actions / Analyze

package slices is not in GOROOT (/opt/hostedtoolcache/go/1.20.2/x64/src/slices)

Check failure on line 6 in x/asset/keeper/msg_server.go

View workflow job for this annotation

GitHub Actions / test (1.19.x, ubuntu-latest)

package slices is not in GOROOT (/opt/hostedtoolcache/go/1.19.13/x64/src/slices)

Check failure on line 6 in x/asset/keeper/msg_server.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, ubuntu-latest)

package slices is not in GOROOT (/opt/hostedtoolcache/go/1.20.14/x64/src/slices)

Check failure on line 6 in x/asset/keeper/msg_server.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, macos-latest)

package slices is not in GOROOT (/Users/runner/hostedtoolcache/go/1.20.14/arm64/src/slices)
"strings"

errorsmod "cosmossdk.io/errors"
Expand Down Expand Up @@ -47,10 +47,10 @@
k.SetToken(ctx, tokenId, token)
k.bankKeeper.SetDenomMetaData(ctx, bank.Metadata{
Base: tokenId, Symbol: lowerCaseSymbol, Name: lowerCaseName,
DenomUnits: []*bank.DenomUnit{{Denom: lowerCaseSymbol, Exponent: msg.Decimal}, {Denom: tokenId, Exponent: 0}},
DenomUnits: []*bank.DenomUnit{{Denom: tokenId, Exponent: msg.Decimal}},
})

tokenManage := types.NewTokenManagement(msg.Manager, msg.AddNewPrivilege, msg.ExcludedPrivileges)
tokenManage := types.NewTokenManagement(msg.Manager, msg.AddNewPrivilege, msg.ExcludedPrivileges, msg.EnabledPrivileges)
k.SetTokenManagement(ctx, tokenId, tokenManage)

ctx.EventManager().EmitEvent(
Expand Down Expand Up @@ -135,8 +135,13 @@
if tm.Manager != msg.Manager {
return nil, errorsmod.Wrapf(sdkerrors.ErrUnauthorized, "sender is not token manager")
}
if slices.Contains(tm.ExcludedPrivileges, msg.Privilege) {
return nil, errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "privilege %s is excluded", msg.Privilege)
if !slices.Contains(tm.EnabledPrivileges, msg.GetPrivilege()) {
if !tm.AddNewPrivilege {
return nil, errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "can't add new privilege")
} else if slices.Contains(tm.ExcludedPrivileges, msg.Privilege) {
return nil, errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "privilege %s is excluded", msg.Privilege)
}
tm.EnabledPrivileges = append(tm.EnabledPrivileges, msg.Privilege)
}

for _, user := range msg.AssignedTo {
Expand All @@ -145,6 +150,7 @@
return nil, err
}
k.SetTokenPrivilegeAccount(ctx, msg.TokenId, msg.Privilege, userAcc)
k.SetTokenManagement(ctx, msg.TokenId, tm)
}

return &types.MsgAssignPrivilegeResponse{}, nil
Expand Down
16 changes: 8 additions & 8 deletions x/asset/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (s *KeeperTestSuite) TestUpdateToken() {
token := types.NewToken(tokenId, strings.ToLower(name), lowerCaseSymbol, 2, description)
k.SetToken(ctx, tokenId, token)

tokenManage := types.NewTokenManagement(managerAddr.String(), true, []string{})
tokenManage := types.NewTokenManagement(managerAddr.String(), true, []string{}, []string{})
k.SetTokenManagement(ctx, tokenId, tokenManage)

return &types.MsgUpdateToken{
Expand Down Expand Up @@ -162,7 +162,7 @@ func (s *KeeperTestSuite) TestAllocateToken() {
token := types.NewToken(tokenId, strings.ToLower(name), lowerCaseSymbol, 2, description)
k.SetToken(ctx, tokenId, token)

tokenManage := types.NewTokenManagement(managerAddr.String(), true, []string{})
tokenManage := types.NewTokenManagement(managerAddr.String(), true, []string{}, []string{})
k.SetTokenManagement(ctx, tokenId, tokenManage)

return &types.MsgAllocateToken{
Expand Down Expand Up @@ -235,7 +235,7 @@ func (s *KeeperTestSuite) TestAssignPrivilege() {
token := types.NewToken(tokenId, strings.ToLower(name), lowerCaseSymbol, 2, description)
k.SetToken(ctx, tokenId, token)

tokenManage := types.NewTokenManagement(managerAddr.String(), true, []string{})
tokenManage := types.NewTokenManagement(managerAddr.String(), true, []string{}, []string{})
k.SetTokenManagement(ctx, tokenId, tokenManage)

return &types.MsgAssignPrivilege{
Expand Down Expand Up @@ -307,7 +307,7 @@ func (s *KeeperTestSuite) TestUnassignPrivilege() {
token := types.NewToken(tokenId, strings.ToLower(name), lowerCaseSymbol, 2, description)
k.SetToken(ctx, tokenId, token)

tokenManage := types.NewTokenManagement(managerAddr.String(), true, []string{})
tokenManage := types.NewTokenManagement(managerAddr.String(), true, []string{}, []string{})
k.SetTokenManagement(ctx, tokenId, tokenManage)

k.SetTokenPrivilegeAccount(ctx, tokenId, creatorAddr.String(), userAddr1)
Expand Down Expand Up @@ -382,7 +382,7 @@ func (s *KeeperTestSuite) TestDisablePrivilege() {
token := types.NewToken(tokenId, strings.ToLower(name), lowerCaseSymbol, 2, description)
k.SetToken(ctx, tokenId, token)

tokenManage := types.NewTokenManagement(managerAddr.String(), true, []string{})
tokenManage := types.NewTokenManagement(managerAddr.String(), true, []string{}, []string{})
k.SetTokenManagement(ctx, tokenId, tokenManage)

return &types.MsgDisablePrivilege{
Expand Down Expand Up @@ -447,7 +447,7 @@ func (s *KeeperTestSuite) TestExecutePrivilege() {
token := types.NewToken(tokenId, strings.ToLower(name), lowerCaseSymbol, 2, description)
k.SetToken(ctx, tokenId, token)

tokenManage := types.NewTokenManagement(managerAddr.String(), true, []string{})
tokenManage := types.NewTokenManagement(managerAddr.String(), true, []string{}, []string{})
k.SetTokenManagement(ctx, tokenId, tokenManage)

k.SetTokenPrivilegeAccount(ctx, tokenId, "test", userAddr1)
Expand Down Expand Up @@ -483,7 +483,7 @@ func (s *KeeperTestSuite) TestExecutePrivilege() {
token := types.NewToken(tokenId, strings.ToLower(name), lowerCaseSymbol, 2, description)
k.SetToken(ctx, tokenId, token)

tokenManage := types.NewTokenManagement(managerAddr.String(), true, []string{})
tokenManage := types.NewTokenManagement(managerAddr.String(), true, []string{}, []string{})
k.SetTokenManagement(ctx, tokenId, tokenManage)

k.SetTokenPrivilegeAccount(ctx, tokenId, "test", userAddr1)
Expand Down Expand Up @@ -543,7 +543,7 @@ func (s *KeeperTestSuite) TestExecutePrivilege() {
token := types.NewToken(tokenId, strings.ToLower(name), lowerCaseSymbol, 2, description)
k.SetToken(ctx, tokenId, token)

tokenManage := types.NewTokenManagement(managerAddr.String(), true, []string{})
tokenManage := types.NewTokenManagement(managerAddr.String(), true, []string{}, []string{})
k.SetTokenManagement(ctx, tokenId, tokenManage)

k.SetTokenPrivilegeAccount(ctx, tokenId, creatorAddr.String(), userAddr1)
Expand Down
Loading
Loading