Skip to content

Commit 040bd11

Browse files
authored
Merge pull request #65 from bianjieai/sv/GrantAuthorization
add grant authorization
2 parents 31f4a68 + 2ab3899 commit 040bd11

File tree

6 files changed

+348
-16
lines changed

6 files changed

+348
-16
lines changed

src/main/java/irita/sdk/constant/enums/MsgEnum.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import irita.sdk.exception.IritaSDKException;
44
import irita.sdk.exception.UnknownMsgException;
5+
import proto.cosmos.feegrant.v1beta1.Tx;
56

67
public enum MsgEnum {
78
//tibc
@@ -59,6 +60,9 @@ public enum MsgEnum {
5960
// identity
6061
MSG_CREATE_IDENTITY("iritamod.identity.MsgCreateIdentity", proto.identity.Tx.MsgCreateIdentity.class),
6162
MSG_UPDATE_IDENTITY("iritamod.identity.MsgUpdateIdentity", proto.identity.Tx.MsgUpdateIdentity.class),
63+
//feegrant
64+
MSG_GRANT_ALLOWANCE("cosmos.feegrant.v1beta1.MsgGrantAllowance", proto.cosmos.feegrant.v1beta1.Tx.MsgGrantAllowance.class),
65+
MSG_REVOKE_ALLOWANCE("cosmos.feegrant.v1beta1.MsgRevokeAllowance",proto.cosmos.feegrant.v1beta1.Tx.MsgRevokeAllowance.class),
6266
// perm
6367
MSG_ASSIGN_ROLES("iritamod.perm.MsgAssignRoles", proto.perm.Tx.MsgAssignRoles.class),
6468
MSG_UNASSIGN_ROLES("iritamod.perm.MsgUnassignRoles", proto.perm.Tx.MsgUnassignRoles.class),
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package irita.sdk.module.feegrant;
2+
3+
import com.google.protobuf.Any;
4+
import com.google.protobuf.GeneratedMessageV3;
5+
import com.google.protobuf.Timestamp;
6+
import irita.sdk.client.BaseClient;
7+
import irita.sdk.model.Account;
8+
import irita.sdk.model.BaseTx;
9+
import irita.sdk.model.ResultTx;
10+
import org.apache.commons.lang3.StringUtils;
11+
import proto.cosmos.base.v1beta1.CoinOuterClass;
12+
import proto.cosmos.feegrant.v1beta1.Feegrant;
13+
import proto.cosmos.feegrant.v1beta1.Tx;
14+
15+
import java.io.IOException;
16+
import java.util.Collections;
17+
import java.util.List;
18+
19+
public class FeeGrantClient {
20+
21+
private final BaseClient baseClient;
22+
23+
public FeeGrantClient (BaseClient baseClient){
24+
this.baseClient = baseClient;
25+
}
26+
27+
/**
28+
* if set denom&amount grant only onice
29+
* default denom=null&amount=null
30+
* timestamp is expiration time
31+
* @param granter
32+
* @param grantee
33+
* @param denom
34+
* @param amount
35+
* @param timestamp
36+
* @param baseTx
37+
* @return
38+
* @throws IOException
39+
*/
40+
public ResultTx GrantAllowance(String granter, String grantee, String denom, String amount, Timestamp timestamp, BaseTx baseTx) throws IOException {
41+
Account account = baseClient.queryAccount(baseTx);
42+
CoinOuterClass.Coin coinOuterClass = null;
43+
if (StringUtils.isNotEmpty(denom) && StringUtils.isNotEmpty(amount)){
44+
coinOuterClass = CoinOuterClass.Coin.newBuilder()
45+
.setAmount(amount)
46+
.setDenom(denom)
47+
.build();
48+
}
49+
Feegrant.BasicAllowance.Builder basicAllowanceBuilder = Feegrant.BasicAllowance.newBuilder();
50+
if (coinOuterClass != null){
51+
basicAllowanceBuilder.addSpendLimit(coinOuterClass);
52+
}
53+
if (timestamp != null){
54+
basicAllowanceBuilder.setExpiration(timestamp);
55+
}
56+
Feegrant.BasicAllowance f = basicAllowanceBuilder.build();
57+
Any.Builder anyBuilder = Any.newBuilder();
58+
anyBuilder.setTypeUrl("/"+f.getDescriptorForType().getFullName());
59+
if (f.getSpendLimitList().size() >0){
60+
anyBuilder.setValue(f.toByteString());
61+
}
62+
Tx.MsgGrantAllowance msg = Tx.MsgGrantAllowance.newBuilder()
63+
.setGranter(granter)
64+
.setGrantee(grantee)
65+
.setAllowance(anyBuilder.build())
66+
.build();
67+
List<GeneratedMessageV3> msgs = Collections.singletonList(msg);
68+
return baseClient.buildAndSend(msgs, baseTx, account);
69+
}
70+
71+
public ResultTx GrantAllowance(String granter, String grantee, BaseTx baseTx) throws IOException {
72+
Account account = baseClient.queryAccount(baseTx);
73+
Feegrant.BasicAllowance f = Feegrant.BasicAllowance.newBuilder().build();
74+
Any.Builder anyBuilder = Any.newBuilder();
75+
anyBuilder.setTypeUrl("/"+f.getDescriptorForType().getFullName());
76+
Tx.MsgGrantAllowance msg = Tx.MsgGrantAllowance.newBuilder()
77+
.setGranter(granter)
78+
.setGrantee(grantee)
79+
.setAllowance(anyBuilder.build())
80+
.build();
81+
List<GeneratedMessageV3> msgs = Collections.singletonList(msg);
82+
return baseClient.buildAndSend(msgs, baseTx, account);
83+
}
84+
85+
public ResultTx RevokeAllowance(String granter, String grantee, BaseTx baseTx) throws IOException {
86+
Account account = baseClient.queryAccount(baseTx);
87+
Tx.MsgRevokeAllowance msg = Tx.MsgRevokeAllowance.newBuilder()
88+
.setGranter(granter)
89+
.setGrantee(grantee)
90+
.build();
91+
92+
List<GeneratedMessageV3> msgs = Collections.singletonList(msg);
93+
return baseClient.buildAndSend(msgs, baseTx, account);
94+
}
95+
96+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
syntax = "proto3";
2+
package cosmos.feegrant.v1beta1;
3+
4+
import "gogoproto/gogo.proto";
5+
import "google/protobuf/any.proto";
6+
import "cosmos_proto/cosmos.proto";
7+
import "cosmos/base/v1beta1/coin.proto";
8+
import "google/protobuf/timestamp.proto";
9+
import "google/protobuf/duration.proto";
10+
11+
option java_package = "proto.cosmos.feegrant.v1beta1";
12+
13+
// BasicAllowance implements Allowance with a one-time grant of tokens
14+
// that optionally expires. The grantee can use up to SpendLimit to cover fees.
15+
message BasicAllowance {
16+
option (cosmos_proto.implements_interface) = "FeeAllowanceI";
17+
18+
// spend_limit specifies the maximum amount of tokens that can be spent
19+
// by this allowance and will be updated as tokens are spent. If it is
20+
// empty, there is no spend limit and any amount of coins can be spent.
21+
repeated cosmos.base.v1beta1.Coin spend_limit = 1
22+
[(gogoproto.nullable) = false, (gogoproto.castrepeated) = "github.com/irisnet/core-sdk-go/types.Coins"];
23+
24+
// expiration specifies an optional time when this allowance expires
25+
google.protobuf.Timestamp expiration = 2 [(gogoproto.stdtime) = true];
26+
}
27+
28+
// PeriodicAllowance extends Allowance to allow for both a maximum cap,
29+
// as well as a limit per time period.
30+
message PeriodicAllowance {
31+
option (cosmos_proto.implements_interface) = "FeeAllowanceI";
32+
33+
// basic specifies a struct of `BasicAllowance`
34+
BasicAllowance basic = 1 [(gogoproto.nullable) = false];
35+
36+
// period specifies the time duration in which period_spend_limit coins can
37+
// be spent before that allowance is reset
38+
google.protobuf.Duration period = 2 [(gogoproto.stdduration) = true, (gogoproto.nullable) = false];
39+
40+
// period_spend_limit specifies the maximum number of coins that can be spent
41+
// in the period
42+
repeated cosmos.base.v1beta1.Coin period_spend_limit = 3
43+
[(gogoproto.nullable) = false, (gogoproto.castrepeated) = "github.com/irisnet/core-sdk-go/types.Coins"];
44+
45+
// period_can_spend is the number of coins left to be spent before the period_reset time
46+
repeated cosmos.base.v1beta1.Coin period_can_spend = 4
47+
[(gogoproto.nullable) = false, (gogoproto.castrepeated) = "github.com/irisnet/core-sdk-go/types.Coins"];
48+
49+
// period_reset is the time at which this period resets and a new one begins,
50+
// it is calculated from the start time of the first transaction after the
51+
// last period ended
52+
google.protobuf.Timestamp period_reset = 5 [(gogoproto.stdtime) = true, (gogoproto.nullable) = false];
53+
}
54+
55+
// AllowedMsgAllowance creates allowance only for specified message types.
56+
message AllowedMsgAllowance {
57+
option (gogoproto.goproto_getters) = false;
58+
option (cosmos_proto.implements_interface) = "FeeAllowanceI";
59+
60+
// allowance can be any of basic and filtered fee allowance.
61+
google.protobuf.Any allowance = 1 [(cosmos_proto.accepts_interface) = "FeeAllowanceI"];
62+
63+
// allowed_messages are the messages for which the grantee has the access.
64+
repeated string allowed_messages = 2;
65+
}
66+
67+
// Grant is stored in the KVStore to record a grant with full context
68+
message Grant {
69+
// granter is the address of the user granting an allowance of their funds.
70+
string granter = 1;
71+
72+
// grantee is the address of the user being granted an allowance of another user's funds.
73+
string grantee = 2;
74+
75+
// allowance can be any of basic and filtered fee allowance.
76+
google.protobuf.Any allowance = 3 [(cosmos_proto.accepts_interface) = "FeeAllowanceI"];
77+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
syntax = "proto3";
2+
package cosmos.feegrant.v1beta1;
3+
4+
import "cosmos/feegrant/v1beta1/feegrant.proto";
5+
import "cosmos/base/query/v1beta1/pagination.proto";
6+
import "google/api/annotations.proto";
7+
8+
option java_package = "proto.cosmos.feegrant.v1beta1";
9+
10+
// Query defines the gRPC querier service.
11+
service Query {
12+
13+
// Allowance returns fee granted to the grantee by the granter.
14+
rpc Allowance(QueryAllowanceRequest) returns (QueryAllowanceResponse) {
15+
option (google.api.http).get = "/cosmos/feegrant/v1beta1/allowance/{granter}/{grantee}";
16+
}
17+
18+
// Allowances returns all the grants for address.
19+
rpc Allowances(QueryAllowancesRequest) returns (QueryAllowancesResponse) {
20+
option (google.api.http).get = "/cosmos/feegrant/v1beta1/allowances/{grantee}";
21+
}
22+
}
23+
24+
// QueryAllowanceRequest is the request type for the Query/Allowance RPC method.
25+
message QueryAllowanceRequest {
26+
// granter is the address of the user granting an allowance of their funds.
27+
string granter = 1;
28+
29+
// grantee is the address of the user being granted an allowance of another user's funds.
30+
string grantee = 2;
31+
}
32+
33+
// QueryAllowanceResponse is the response type for the Query/Allowance RPC method.
34+
message QueryAllowanceResponse {
35+
// allowance is a allowance granted for grantee by granter.
36+
cosmos.feegrant.v1beta1.Grant allowance = 1;
37+
}
38+
39+
// QueryAllowancesRequest is the request type for the Query/Allowances RPC method.
40+
message QueryAllowancesRequest {
41+
string grantee = 1;
42+
43+
// pagination defines an pagination for the request.
44+
cosmos.base.query.v1beta1.PageRequest pagination = 2;
45+
}
46+
47+
// QueryAllowancesResponse is the response type for the Query/Allowances RPC method.
48+
message QueryAllowancesResponse {
49+
// allowances are allowance's granted for grantee by granter.
50+
repeated cosmos.feegrant.v1beta1.Grant allowances = 1;
51+
52+
// pagination defines an pagination for the response.
53+
cosmos.base.query.v1beta1.PageResponse pagination = 2;
54+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
syntax = "proto3";
2+
package cosmos.feegrant.v1beta1;
3+
4+
import "gogoproto/gogo.proto";
5+
import "google/protobuf/any.proto";
6+
import "cosmos_proto/cosmos.proto";
7+
8+
option java_package = "proto.cosmos.feegrant.v1beta1";
9+
10+
// Msg defines the feegrant msg service.
11+
service Msg {
12+
13+
// GrantAllowance grants fee allowance to the grantee on the granter's
14+
// account with the provided expiration time.
15+
rpc GrantAllowance(MsgGrantAllowance) returns (MsgGrantAllowanceResponse);
16+
17+
// RevokeAllowance revokes any fee allowance of granter's account that
18+
// has been granted to the grantee.
19+
rpc RevokeAllowance(MsgRevokeAllowance) returns (MsgRevokeAllowanceResponse);
20+
}
21+
22+
// MsgGrantAllowance adds permission for Grantee to spend up to Allowance
23+
// of fees from the account of Granter.
24+
message MsgGrantAllowance {
25+
// granter is the address of the user granting an allowance of their funds.
26+
string granter = 1;
27+
28+
// grantee is the address of the user being granted an allowance of another user's funds.
29+
string grantee = 2;
30+
31+
// allowance can be any of basic and filtered fee allowance.
32+
google.protobuf.Any allowance = 3 [(cosmos_proto.accepts_interface) = "FeeAllowanceI"];
33+
}
34+
35+
// MsgGrantAllowanceResponse defines the Msg/GrantAllowanceResponse response type.
36+
message MsgGrantAllowanceResponse {}
37+
38+
// MsgRevokeAllowance removes any existing Allowance from Granter to Grantee.
39+
message MsgRevokeAllowance {
40+
// granter is the address of the user granting an allowance of their funds.
41+
string granter = 1;
42+
43+
// grantee is the address of the user being granted an allowance of another user's funds.
44+
string grantee = 2;
45+
}
46+
47+
// MsgRevokeAllowanceResponse defines the Msg/RevokeAllowanceResponse response type.
48+
message MsgRevokeAllowanceResponse {}

0 commit comments

Comments
 (0)