/
authz.go
91 lines (71 loc) · 2.81 KB
/
authz.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package types
import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/authz"
iidtypes "github.com/furya-official/fury-blockchain/x/iid/types"
)
var (
_ authz.Authorization = &MintAuthorization{}
)
// NewMintAuthorization creates a new MintAuthorization object.
func NewMintAuthorization(minterDid iidtypes.DIDFragment, cw20Limit, cw721Limit, fury1155Limit int64) *MintAuthorization {
return &MintAuthorization{
MinterDid: minterDid,
// MintLimit: &MintLimit{
// Cw20: string(cw20Limit),
// Cw721: string(cw721Limit),
// Fury1155: string(fury1155Limit),
// },
}
}
// MsgTypeURL implements Authorization.MsgTypeURL.
func (a MintAuthorization) MsgTypeURL() string {
return sdk.MsgTypeURL(&MsgMint{})
}
// Accept implements Authorization.Accept.
func (a MintAuthorization) Accept(ctx sdk.Context, msg sdk.Msg) (authz.AcceptResponse, error) {
mMint, ok := msg.(*MsgMint)
if !ok {
return authz.AcceptResponse{Accept: false}, sdkerrors.ErrInvalidType.Wrap("type mismatch")
}
if a.MinterDid.Did() != mMint.MinterDid.Did() {
return authz.AcceptResponse{Accept: false}, sdkerrors.ErrInvalidType.Wrapf("authorized minter (%s) did not match the minter in the msg %s", a.MinterDid.Did(), mMint.MinterDid.Did())
}
// var foundContractInAuthorization bool = false
// var allLimitsAreZero bool = true
// for _, constraints := range a.Constraints {
// }
updatedConstraints := []*MintConstraints{}
var matched bool
for _, constraints := range a.Constraints {
if constraints.ContractAddress != mMint.ContractAddress {
updatedConstraints = append(updatedConstraints, constraints)
continue
}
// state that a match was found becuase if this is full the result will deny, as this must match at least on contract.
matched = true
if constraints.Limit == 0 {
return authz.AcceptResponse{Accept: false, Delete: true, Updated: &a}, sdkerrors.ErrInvalidType.Wrap("contract mint limit reached")
}
constraints.Limit = constraints.Limit - 1
updatedConstraints = append(updatedConstraints, constraints)
}
if !matched {
return authz.AcceptResponse{Accept: false, Delete: false, Updated: &a}, sdkerrors.ErrInvalidType.Wrap("no contract matched the constraints specified in the grant.")
}
a.Constraints = updatedConstraints
// if a.MintLimit.Cw20 == 0 && a.MintLimit.Cw721 == 0 && a.MintLimit.Fury1155 == 0 {
// return authz.AcceptResponse{Accept: true, Delete: true}, nil
// }
return authz.AcceptResponse{Accept: true, Delete: false, Updated: &a}, nil
}
// ValidateBasic implements Authorization.ValidateBasic.
func (a MintAuthorization) ValidateBasic() error {
for _, constraints := range a.Constraints {
if constraints.Limit <= 0 {
return sdkerrors.ErrInvalidCoins.Wrap("spend limit cannot be nil")
}
}
return nil
}