Skip to content

Commit

Permalink
Merge pull request #230 from InjectiveLabs/feat/permissions_module_me…
Browse files Browse the repository at this point in the history
…ssages

Feat/permissions module messages
  • Loading branch information
aarmoa committed Jul 26, 2024
2 parents fb8a8bf + e985b0f commit 3bc09db
Show file tree
Hide file tree
Showing 15 changed files with 930 additions and 98 deletions.
66 changes: 66 additions & 0 deletions client/chain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"sync/atomic"
"time"

permissionstypes "github.com/InjectiveLabs/sdk-go/chain/permissions/types"

sdkmath "cosmossdk.io/math"

"github.com/cosmos/cosmos-sdk/client/grpc/cmtservice"
Expand Down Expand Up @@ -286,6 +288,13 @@ type ChainClient interface {
FetchIBCConnectionConsensusState(ctx context.Context, connectionId string, revisionNumber uint64, revisionHeight uint64) (*ibcconnectiontypes.QueryConnectionConsensusStateResponse, error)
FetchIBCConnectionParams(ctx context.Context) (*ibcconnectiontypes.QueryConnectionParamsResponse, error)

// Permissions module
FetchAllNamespaces(ctx context.Context) (*permissionstypes.QueryAllNamespacesResponse, error)
FetchNamespaceByDenom(ctx context.Context, denom string, includeRoles bool) (*permissionstypes.QueryNamespaceByDenomResponse, error)
FetchAddressRoles(ctx context.Context, denom, address string) (*permissionstypes.QueryAddressRolesResponse, error)
FetchAddressesByRole(ctx context.Context, denom, role string) (*permissionstypes.QueryAddressesByRoleResponse, error)
FetchVouchersForAddress(ctx context.Context, address string) (*permissionstypes.QueryVouchersForAddressResponse, error)

Close()
}

Expand Down Expand Up @@ -322,6 +331,7 @@ type chainClient struct {
ibcClientQueryClient ibcclienttypes.QueryClient
ibcConnectionQueryClient ibcconnectiontypes.QueryClient
ibcTransferQueryClient ibctransfertypes.QueryClient
permissionsQueryClient permissionstypes.QueryClient
tendermintQueryClient cmtservice.ServiceClient
tokenfactoryQueryClient tokenfactorytypes.QueryClient
txClient txtypes.ServiceClient
Expand Down Expand Up @@ -422,6 +432,7 @@ func NewChainClient(
ibcClientQueryClient: ibcclienttypes.NewQueryClient(conn),
ibcConnectionQueryClient: ibcconnectiontypes.NewQueryClient(conn),
ibcTransferQueryClient: ibctransfertypes.NewQueryClient(conn),
permissionsQueryClient: permissionstypes.NewQueryClient(conn),
tendermintQueryClient: cmtservice.NewServiceClient(conn),
tokenfactoryQueryClient: tokenfactorytypes.NewQueryClient(conn),
txClient: txtypes.NewServiceClient(conn),
Expand Down Expand Up @@ -763,6 +774,9 @@ func (c *chainClient) BuildSignedTx(clientCtx client.Context, accNum, accSeq, in
}

func (c *chainClient) buildSignedTx(clientCtx client.Context, txf tx.Factory, msgs ...sdk.Msg) ([]byte, error) {
if c.network.ChainId != "injective-777" {
panic("This versions of Go SDK should only be used with Devnet environment")
}
ctx := context.Background()
if clientCtx.Simulate {
simTxBytes, err := txf.BuildSimTx(msgs...)
Expand Down Expand Up @@ -874,6 +888,10 @@ func (c *chainClient) broadcastTx(
await bool,
msgs ...sdk.Msg,
) (*txtypes.BroadcastTxResponse, error) {
if c.network.ChainId != "injective-777" {
panic("This versions of Go SDK should only be used with Devnet environment")
}

txBytes, err := c.buildSignedTx(clientCtx, txf, msgs...)
if err != nil {
err = errors.Wrap(err, "failed to build signed Tx")
Expand Down Expand Up @@ -2592,3 +2610,51 @@ func (c *chainClient) FetchIBCConnectionParams(ctx context.Context) (*ibcconnect

return res, err
}

// Permissions module

func (c *chainClient) FetchAllNamespaces(ctx context.Context) (*permissionstypes.QueryAllNamespacesResponse, error) {
req := &permissionstypes.QueryAllNamespacesRequest{}
res, err := common.ExecuteCall(ctx, c.network.ChainCookieAssistant, c.permissionsQueryClient.AllNamespaces, req)

return res, err
}

func (c *chainClient) FetchNamespaceByDenom(ctx context.Context, denom string, includeRoles bool) (*permissionstypes.QueryNamespaceByDenomResponse, error) {
req := &permissionstypes.QueryNamespaceByDenomRequest{
Denom: denom,
IncludeRoles: includeRoles,
}
res, err := common.ExecuteCall(ctx, c.network.ChainCookieAssistant, c.permissionsQueryClient.NamespaceByDenom, req)

return res, err
}

func (c *chainClient) FetchAddressRoles(ctx context.Context, denom, address string) (*permissionstypes.QueryAddressRolesResponse, error) {
req := &permissionstypes.QueryAddressRolesRequest{
Denom: denom,
Address: address,
}
res, err := common.ExecuteCall(ctx, c.network.ChainCookieAssistant, c.permissionsQueryClient.AddressRoles, req)

return res, err
}

func (c *chainClient) FetchAddressesByRole(ctx context.Context, denom, role string) (*permissionstypes.QueryAddressesByRoleResponse, error) {
req := &permissionstypes.QueryAddressesByRoleRequest{
Denom: denom,
Role: role,
}
res, err := common.ExecuteCall(ctx, c.network.ChainCookieAssistant, c.permissionsQueryClient.AddressesByRole, req)

return res, err
}

func (c *chainClient) FetchVouchersForAddress(ctx context.Context, address string) (*permissionstypes.QueryVouchersForAddressResponse, error) {
req := &permissionstypes.QueryVouchersForAddressRequest{
Address: address,
}
res, err := common.ExecuteCall(ctx, c.network.ChainCookieAssistant, c.permissionsQueryClient.VouchersForAddress, req)

return res, err
}
4 changes: 2 additions & 2 deletions client/chain/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func createClient(senderAddress cosmtypes.AccAddress, cosmosKeyring keyring.Keyr
}

func TestDefaultSubaccount(t *testing.T) {
network := common.LoadNetwork("testnet", "lb")
network := common.LoadNetwork("devnet", "lb")
senderAddress, cosmosKeyring, err := accountForTests()

if err != nil {
Expand All @@ -75,7 +75,7 @@ func TestDefaultSubaccount(t *testing.T) {
}

func TestGetSubaccountWithIndex(t *testing.T) {
network := common.LoadNetwork("testnet", "lb")
network := common.LoadNetwork("devnet", "lb")
senderAddress, cosmosKeyring, err := accountForTests()

if err != nil {
Expand Down
24 changes: 24 additions & 0 deletions client/chain/chain_test_support.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"errors"
"time"

permissionstypes "github.com/InjectiveLabs/sdk-go/chain/permissions/types"

sdkmath "cosmossdk.io/math"

"github.com/cosmos/cosmos-sdk/client/grpc/cmtservice"
Expand Down Expand Up @@ -717,3 +719,25 @@ func (c *MockChainClient) FetchIBCConnectionConsensusState(ctx context.Context,
func (c *MockChainClient) FetchIBCConnectionParams(ctx context.Context) (*ibcconnectiontypes.QueryConnectionParamsResponse, error) {
return &ibcconnectiontypes.QueryConnectionParamsResponse{}, nil
}

// Permissions module

func (c *MockChainClient) FetchAllNamespaces(ctx context.Context) (*permissionstypes.QueryAllNamespacesResponse, error) {
return &permissionstypes.QueryAllNamespacesResponse{}, nil
}

func (c *MockChainClient) FetchNamespaceByDenom(ctx context.Context, denom string, includeRoles bool) (*permissionstypes.QueryNamespaceByDenomResponse, error) {
return &permissionstypes.QueryNamespaceByDenomResponse{}, nil
}

func (c *MockChainClient) FetchAddressRoles(ctx context.Context, denom, address string) (*permissionstypes.QueryAddressRolesResponse, error) {
return &permissionstypes.QueryAddressRolesResponse{}, nil
}

func (c *MockChainClient) FetchAddressesByRole(ctx context.Context, denom, role string) (*permissionstypes.QueryAddressesByRoleResponse, error) {
return &permissionstypes.QueryAddressesByRoleResponse{}, nil
}

func (c *MockChainClient) FetchVouchersForAddress(ctx context.Context, address string) (*permissionstypes.QueryVouchersForAddressResponse, error) {
return &permissionstypes.QueryVouchersForAddressResponse{}, nil
}
96 changes: 0 additions & 96 deletions client/common/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package common

import (
"context"
"crypto/tls"
"fmt"
"net"
"net/http"
Expand Down Expand Up @@ -256,101 +255,6 @@ func LoadNetwork(name, node string) Network {
ExplorerCookieAssistant: &DisabledCookieAssistant{},
OfficialTokensListURL: DevnetTokensListURL,
}
case "testnet":
validNodes := []string{"lb", "sentry"}
if !contains(validNodes, node) {
panic(fmt.Sprintf("invalid node %s for %s", node, name))
}

var lcdEndpoint, tmEndpoint, chainGrpcEndpoint, chainStreamGrpcEndpoint, exchangeGrpcEndpoint, explorerGrpcEndpoint string
var chainTLSCert, exchangeTLSCert, explorerTLSCert credentials.TransportCredentials
var chainCookieAssistant, exchangeCookieAssistant, explorerCookieAssistant CookieAssistant
if node == "lb" {
lcdEndpoint = "https://testnet.sentry.lcd.injective.network:443"
tmEndpoint = "https://testnet.sentry.tm.injective.network:443"
chainGrpcEndpoint = "testnet.sentry.chain.grpc.injective.network:443"
chainStreamGrpcEndpoint = "testnet.sentry.chain.stream.injective.network:443"
exchangeGrpcEndpoint = "testnet.sentry.exchange.grpc.injective.network:443"
explorerGrpcEndpoint = "testnet.sentry.explorer.grpc.injective.network:443"
chainTLSCert = credentials.NewServerTLSFromCert(&tls.Certificate{})
exchangeTLSCert = credentials.NewServerTLSFromCert(&tls.Certificate{})
explorerTLSCert = credentials.NewServerTLSFromCert(&tls.Certificate{})
chainCookieAssistant = &BareMetalLoadBalancedCookieAssistant{}
exchangeCookieAssistant = &BareMetalLoadBalancedCookieAssistant{}
explorerCookieAssistant = &BareMetalLoadBalancedCookieAssistant{}
} else if node == "sentry" {
lcdEndpoint = "https://testnet.lcd.injective.network:443"
tmEndpoint = "https://testnet.tm.injective.network:443"
chainGrpcEndpoint = "testnet.chain.grpc.injective.network:443"
chainStreamGrpcEndpoint = "testnet.chain.stream.injective.network:443"
exchangeGrpcEndpoint = "testnet.exchange.grpc.injective.network:443"
explorerGrpcEndpoint = "testnet.explorer.grpc.injective.network:443"
chainTLSCert = credentials.NewServerTLSFromCert(&tls.Certificate{})
exchangeTLSCert = credentials.NewServerTLSFromCert(&tls.Certificate{})
explorerTLSCert = credentials.NewServerTLSFromCert(&tls.Certificate{})
chainCookieAssistant = &DisabledCookieAssistant{}
exchangeCookieAssistant = &DisabledCookieAssistant{}
explorerCookieAssistant = &DisabledCookieAssistant{}
}

return Network{
LcdEndpoint: lcdEndpoint,
TmEndpoint: tmEndpoint,
ChainGrpcEndpoint: chainGrpcEndpoint,
ChainStreamGrpcEndpoint: chainStreamGrpcEndpoint,
ChainTLSCert: chainTLSCert,
ExchangeGrpcEndpoint: exchangeGrpcEndpoint,
ExchangeTLSCert: exchangeTLSCert,
ExplorerGrpcEndpoint: explorerGrpcEndpoint,
ExplorerTLSCert: explorerTLSCert,
ChainId: "injective-888",
FeeDenom: "inj",
Name: "testnet",
ChainCookieAssistant: chainCookieAssistant,
ExchangeCookieAssistant: exchangeCookieAssistant,
ExplorerCookieAssistant: explorerCookieAssistant,
OfficialTokensListURL: TestnetTokensListURL,
}
case "mainnet":
validNodes := []string{"lb"}
if !contains(validNodes, node) {
panic(fmt.Sprintf("invalid node %s for %s", node, name))
}
var lcdEndpoint, tmEndpoint, chainGrpcEndpoint, chainStreamGrpcEndpoint, exchangeGrpcEndpoint, explorerGrpcEndpoint string
var chainTLSCert, exchangeTLSCert, explorerTLSCert credentials.TransportCredentials
var chainCookieAssistant, exchangeCookieAssistant, explorerCookieAssistant CookieAssistant

lcdEndpoint = "https://sentry.lcd.injective.network"
tmEndpoint = "https://sentry.tm.injective.network:443"
chainGrpcEndpoint = "sentry.chain.grpc.injective.network:443"
chainStreamGrpcEndpoint = "sentry.chain.stream.injective.network:443"
exchangeGrpcEndpoint = "sentry.exchange.grpc.injective.network:443"
explorerGrpcEndpoint = "sentry.explorer.grpc.injective.network:443"
chainTLSCert = credentials.NewServerTLSFromCert(&tls.Certificate{})
exchangeTLSCert = credentials.NewServerTLSFromCert(&tls.Certificate{})
explorerTLSCert = credentials.NewServerTLSFromCert(&tls.Certificate{})
chainCookieAssistant = &BareMetalLoadBalancedCookieAssistant{}
exchangeCookieAssistant = &BareMetalLoadBalancedCookieAssistant{}
explorerCookieAssistant = &BareMetalLoadBalancedCookieAssistant{}

return Network{
LcdEndpoint: lcdEndpoint,
TmEndpoint: tmEndpoint,
ChainGrpcEndpoint: chainGrpcEndpoint,
ChainStreamGrpcEndpoint: chainStreamGrpcEndpoint,
ChainTLSCert: chainTLSCert,
ExchangeGrpcEndpoint: exchangeGrpcEndpoint,
ExchangeTLSCert: exchangeTLSCert,
ExplorerGrpcEndpoint: explorerGrpcEndpoint,
ExplorerTLSCert: explorerTLSCert,
ChainId: "injective-1",
FeeDenom: "inj",
Name: "mainnet",
ChainCookieAssistant: chainCookieAssistant,
ExchangeCookieAssistant: exchangeCookieAssistant,
ExplorerCookieAssistant: explorerCookieAssistant,
OfficialTokensListURL: MainnetTokensListURL,
}

default:
panic(fmt.Sprintf("invalid network %s", name))
Expand Down
93 changes: 93 additions & 0 deletions examples/chain/permissions/1_CreateNamespace/example.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package main

import (
"encoding/json"
"fmt"
"os"

permissionstypes "github.com/InjectiveLabs/sdk-go/chain/permissions/types"
"github.com/InjectiveLabs/sdk-go/client"
chainclient "github.com/InjectiveLabs/sdk-go/client/chain"
"github.com/InjectiveLabs/sdk-go/client/common"
rpchttp "github.com/cometbft/cometbft/rpc/client/http"
)

func main() {
network := common.LoadNetwork("devnet", "lb")
tmClient, err := rpchttp.New(network.TmEndpoint, "/websocket")
if err != nil {
panic(err)
}

senderAddress, cosmosKeyring, err := chainclient.InitCosmosKeyring(
os.Getenv("HOME")+"/.injectived",
"injectived",
"file",
"inj-user",
"12345678",
"f9db9bf330e23cb7839039e944adef6e9df447b90b503d5b4464c90bea9022f3", // keyring will be used if pk not provided
false,
)

if err != nil {
panic(err)
}

clientCtx, err := chainclient.NewClientContext(
network.ChainId,
senderAddress.String(),
cosmosKeyring,
)

if err != nil {
panic(err)
}

clientCtx = clientCtx.WithNodeURI(network.TmEndpoint).WithClient(tmClient)

chainClient, err := chainclient.NewChainClient(
clientCtx,
network,
common.OptionGasPrices(client.DefaultGasPriceWithDenom),
)

if err != nil {
panic(err)
}

blockedAddress := "inj1hkhdaj2a2clmq5jq6mspsggqs32vynpk228q3r"
namespace := permissionstypes.Namespace{
Denom: "factory/inj1hkhdaj2a2clmq5jq6mspsggqs32vynpk228q3r/inj_test",
RolePermissions: []*permissionstypes.Role{
{
Role: permissionstypes.EVERYONE,
Permissions: uint32(permissionstypes.Action_MINT | permissionstypes.Action_RECEIVE | permissionstypes.Action_BURN),
},
{
Role: "blacklisted",
Permissions: 0,
},
},
AddressRoles: []*permissionstypes.AddressRoles{
{
Address: blockedAddress,
Roles: []string{"blacklisted"},
},
},
}

msg := &permissionstypes.MsgCreateNamespace{
Sender: senderAddress.String(),
Namespace: namespace,
}

//AsyncBroadcastMsg, SyncBroadcastMsg, QueueBroadcastMsg
response, err := chainClient.SyncBroadcastMsg(msg)

if err != nil {
panic(err)
}

str, _ := json.MarshalIndent(response, "", " ")
fmt.Print(string(str))
}
Loading

0 comments on commit 3bc09db

Please sign in to comment.