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 4 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 @@ -415,6 +415,8 @@ func New(
app.AccountKeeper,
)

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
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;
}
11 changes: 11 additions & 0 deletions proto/realionetwork/asset/priviledges/freeze/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/freeze";

import "cosmos_proto/cosmos.proto";

message MsgFreezeToken {
string account = 1 [ (cosmos_proto.scalar) = "cosmos.AddressString" ];
uint64 amount = 2;
}
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"];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
syntax = "proto3";
package realionetwork.asset.v1;

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

message AllowAddrs {
map<string, bool> addrs = 1;
}
25 changes: 25 additions & 0 deletions proto/realionetwork/asset/priviledges/transfer_auth/query.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
syntax = "proto3";
package realionetwork.asset.v1;

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

import "cosmos_proto/cosmos.proto";
import "realionetwork/asset/priviledges/transfer_auth/priv.proto";

message QueryAllowAddressRequest {
string token_id = 1;
}

message QueryAllowAddressRespones {
AllowAddrs allow_addrs = 1;
}

message QueryIsAllowedRequest {
string address = 1 [ (cosmos_proto.scalar) = "cosmos.AddressString" ];
string token_id = 2;

}

message QueryIsAllowedRespones {
bool is_allow = 1;
}
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 []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 && len(k.RestrictionChecker) == 0 {
k.RestrictionChecker = append(k.RestrictionChecker, 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) 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) {

Check failure on line 109 in x/asset/keeper/keeper_test.go

View workflow job for this annotation

GitHub Actions / lint

cannot use (func(context context.Context, privMsg proto.Message, tokenID string, privAcc sdk.AccAddress) (proto.Message, error) literal) (value of type func(context "context".Context, privMsg "github.com/gogo/protobuf/proto".Message, tokenID string, privAcc "github.com/cosmos/cosmos-sdk/types".AccAddress) ("github.com/gogo/protobuf/proto".Message, error)) as "github.com/realiotech/realio-network/x/asset/types".MsgHandler value in return statement (typecheck)
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) 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
54 changes: 54 additions & 0 deletions x/asset/keeper/restriction.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package keeper

import (
errorsmod "cosmossdk.io/errors"
"github.com/realiotech/realio-network/x/asset/types"

sdk "github.com/cosmos/cosmos-sdk/types"
)

type RestrictionChecker interface {
IsAllow(ctx sdk.Context, tokenId string, sender string) (bool, error)
}

func (k Keeper) AssetSendRestriction(ctx sdk.Context, fromAddr, toAddr sdk.AccAddress, amt sdk.Coins) (newToAddr sdk.AccAddress, err error) {
facs95 marked this conversation as resolved.
Show resolved Hide resolved
newToAddr = toAddr
err = nil

// if no checker exist allow all sender
if len(k.RestrictionChecker) == 0 {
return newToAddr, nil
}

checker := k.RestrictionChecker[0]
Copy link
Contributor

@haleyga haleyga Sep 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a comment in x/asset/keeper/keeper.go that we should not support more than 1 RestrictionChecker. However, the code seems to allow more than one anyway (both x/asset/keeper/keeper.go and x/asset/keeper/restriction.go have the conditional check len(k.RestrictionChecker) == 0 instead of something like len(k.RestrictionChecker) != 1 for instance.

This line checker := k.RestrictionChecker[0] will ignore any RestrictionChecker after the first one registered. If multiple exist, how can we be sure the first is the correct one to use? It seems the code should either enforce only one RestrictionChecker, or this code block should iterate all RestrictionCheckers in this list.


for _, coin := range amt {
// Check if the value already exists
// fetch bank metadata to get symbol from denom
symbol := coin.Denom
tokenMetadata, found := k.bankKeeper.GetDenomMetaData(ctx, coin.Denom)
if found {
symbol = tokenMetadata.Symbol
}
_, isFound := k.GetToken(
ctx,
symbol,
)
if !isFound {
continue
}

isAllow, err := checker.IsAllow(ctx, symbol, fromAddr.String())
if err != nil {
return newToAddr, err
}

if isAllow {
continue
} else { //nolint:revive // superfluous else, could fix, but not worth it?
err = errorsmod.Wrapf(types.ErrNotAuthorized, "%s is not authorized to transact with %s", fromAddr, coin.Denom)
return newToAddr, err
}
}
return newToAddr, nil
}
Loading
Loading