|
| 1 | +package grant |
| 2 | + |
| 3 | +import ( |
| 4 | + "time" |
| 5 | + |
| 6 | + "github.com/cosmos/cosmos-sdk/client" |
| 7 | + "github.com/cosmos/cosmos-sdk/client/tx" |
| 8 | + sdk "github.com/cosmos/cosmos-sdk/types" |
| 9 | + "github.com/cosmos/cosmos-sdk/x/authz" |
| 10 | + "github.com/spf13/cobra" |
| 11 | +) |
| 12 | + |
| 13 | +// AddGranteeCmd returns a command to add a grantee to a granter. |
| 14 | +func AddGranteeCmd(msgGrants []string, flagExpiration string) func(cmd *cobra.Command, args []string) error { |
| 15 | + return func(cmd *cobra.Command, args []string) error { |
| 16 | + clientCtx, err := client.GetClientTxContext(cmd) |
| 17 | + if err != nil { |
| 18 | + return err |
| 19 | + } |
| 20 | + |
| 21 | + exp, err := cmd.Flags().GetInt64(flagExpiration) |
| 22 | + if err != nil { |
| 23 | + return err |
| 24 | + } |
| 25 | + expTime := time.Unix(exp, 0) |
| 26 | + |
| 27 | + granter := clientCtx.GetFromAddress() |
| 28 | + msgs := []sdk.Msg{} |
| 29 | + |
| 30 | + for _, arg := range args { |
| 31 | + grantee, err := sdk.AccAddressFromBech32(arg) |
| 32 | + if err != nil { |
| 33 | + return err |
| 34 | + } |
| 35 | + |
| 36 | + gMsgs, err := combineGrantMsgs(granter, grantee, msgGrants, &expTime) |
| 37 | + if err != nil { |
| 38 | + return err |
| 39 | + } |
| 40 | + msgs = append(msgs, gMsgs...) |
| 41 | + } |
| 42 | + |
| 43 | + return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msgs...) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +// RemoveGranteeCmd returns a command to remove a grantee from a granter. |
| 48 | +func RemoveGranteeCmd(msgRevokes []string) func(cmd *cobra.Command, args []string) error { |
| 49 | + return func(cmd *cobra.Command, args []string) error { |
| 50 | + clientCtx, err := client.GetClientTxContext(cmd) |
| 51 | + if err != nil { |
| 52 | + return err |
| 53 | + } |
| 54 | + |
| 55 | + granter := clientCtx.GetFromAddress() |
| 56 | + msgs := []sdk.Msg{} |
| 57 | + |
| 58 | + for _, arg := range args { |
| 59 | + grantee, err := sdk.AccAddressFromBech32(arg) |
| 60 | + if err != nil { |
| 61 | + return err |
| 62 | + } |
| 63 | + |
| 64 | + rMsgs, err := combineRevokeMsgs(granter, grantee, msgRevokes) |
| 65 | + if err != nil { |
| 66 | + return err |
| 67 | + } |
| 68 | + msgs = append(msgs, rMsgs...) |
| 69 | + } |
| 70 | + |
| 71 | + return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msgs...) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +// combineGrantMsgs combines multiple grant messages into a single slice of messages. |
| 76 | +func combineGrantMsgs( |
| 77 | + granter sdk.AccAddress, |
| 78 | + grantee sdk.AccAddress, |
| 79 | + msgGrants []string, |
| 80 | + expiration *time.Time, |
| 81 | +) ([]sdk.Msg, error) { |
| 82 | + msgs := []sdk.Msg{} |
| 83 | + |
| 84 | + for _, msgGrant := range msgGrants { |
| 85 | + msg, err := authz.NewMsgGrant( |
| 86 | + granter, |
| 87 | + grantee, |
| 88 | + authz.NewGenericAuthorization(msgGrant), |
| 89 | + expiration, |
| 90 | + ) |
| 91 | + if err != nil { |
| 92 | + return nil, err |
| 93 | + } |
| 94 | + |
| 95 | + if err = msg.ValidateBasic(); err != nil { |
| 96 | + return nil, err |
| 97 | + } |
| 98 | + |
| 99 | + msgs = append(msgs, msg) |
| 100 | + } |
| 101 | + |
| 102 | + return msgs, nil |
| 103 | +} |
| 104 | + |
| 105 | +// combineRevokeMsgs combines multiple revoke messages into a single slice of messages. |
| 106 | +func combineRevokeMsgs(granter sdk.AccAddress, grantee sdk.AccAddress, msgRevokes []string) ([]sdk.Msg, error) { |
| 107 | + msgs := []sdk.Msg{} |
| 108 | + |
| 109 | + for _, msgRevoke := range msgRevokes { |
| 110 | + msg := authz.NewMsgRevoke( |
| 111 | + granter, |
| 112 | + grantee, |
| 113 | + msgRevoke, |
| 114 | + ) |
| 115 | + |
| 116 | + if err := msg.ValidateBasic(); err != nil { |
| 117 | + return nil, err |
| 118 | + } |
| 119 | + |
| 120 | + msgs = append(msgs, &msg) |
| 121 | + } |
| 122 | + |
| 123 | + return msgs, nil |
| 124 | +} |
0 commit comments