/
transaction_request.go
138 lines (124 loc) · 5.57 KB
/
transaction_request.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Copyright [2021] - [2022], AssetMantle Pte. Ltd. and the code contributors
// SPDX-License-Identifier: Apache-2.0
package deputize
import (
"encoding/json"
codecUtilities "github.com/AssetMantle/schema/go/codec/utilities"
"github.com/AssetMantle/schema/go/ids"
baseIDs "github.com/AssetMantle/schema/go/ids/base"
"github.com/AssetMantle/schema/go/lists/base"
"github.com/asaskevich/govalidator"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
sdkTypes "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/AssetMantle/modules/helpers"
"github.com/AssetMantle/modules/helpers/constants"
)
type transactionRequest struct {
BaseReq rest.BaseReq `json:"baseReq"`
FromID string `json:"fromID" valid:"required~required field fromID missing, matches(^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$)~invalid field fromID"`
ToID string `json:"toID" valid:"required~required field toID missing, matches(^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$)~invalid field toID"`
ClassificationID string `json:"classificationID" valid:"required~required field classificationID missing, matches(^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$)~invalid field classificationID"`
MaintainedProperties string `json:"maintainedProperties" valid:"required~required field maintainedProperties missing, matches(^.*$)~invalid field maintainedProperties"`
CanIssueIdentity bool `json:"canIssueIdentity"`
CanQuashIdentity bool `json:"canQuashIdentity"`
CanAddMaintainer bool `json:"canAddMaintainer"`
CanRemoveMaintainer bool `json:"canRemoveMaintainer"`
CanMutateMaintainer bool `json:"canMutateMaintainer"`
}
var _ helpers.TransactionRequest = (*transactionRequest)(nil)
// Validate godoc
// @Summary Deputize an identity
// @Description A transaction to deputize a maintainer for an identity classification.
// @Accept text/plain
// @Produce json
// @Tags Identities
// @Param body transactionRequest true "Request body to deputize identity"
// @Success 200 {object} transactionResponse "Message for a successful response."
// @Failure default {object} transactionResponse "Message for an unexpected error response."
// @Router /identities/deputize [post]
func (transactionRequest transactionRequest) Validate() error {
_, err := govalidator.ValidateStruct(transactionRequest)
if err != nil {
return err
}
return nil
}
func (transactionRequest transactionRequest) FromCLI(cliCommand helpers.CLICommand, context client.Context) (helpers.TransactionRequest, error) {
return newTransactionRequest(
cliCommand.ReadBaseReq(context),
cliCommand.ReadString(constants.FromIdentityID),
cliCommand.ReadString(constants.ToIdentityID),
cliCommand.ReadString(constants.ClassificationID),
cliCommand.ReadString(constants.MaintainedProperties),
cliCommand.ReadBool(constants.CanIssueIdentity),
cliCommand.ReadBool(constants.CanQuashIdentity),
cliCommand.ReadBool(constants.CanAddMaintainer),
cliCommand.ReadBool(constants.CanRemoveMaintainer),
cliCommand.ReadBool(constants.CanMutateMaintainer),
), nil
}
func (transactionRequest transactionRequest) FromJSON(rawMessage json.RawMessage) (helpers.TransactionRequest, error) {
if err := json.Unmarshal(rawMessage, &transactionRequest); err != nil {
return nil, err
}
return transactionRequest, nil
}
func (transactionRequest transactionRequest) GetBaseReq() rest.BaseReq {
return transactionRequest.BaseReq
}
func (transactionRequest transactionRequest) MakeMsg() (sdkTypes.Msg, error) {
from, err := sdkTypes.AccAddressFromBech32(transactionRequest.GetBaseReq().From)
if err != nil {
return nil, err
}
maintainedProperties, err := base.PrototypePropertyList().FromMetaPropertiesString(transactionRequest.MaintainedProperties)
if err != nil {
return nil, err
}
fromID, err := baseIDs.PrototypeIdentityID().FromString(transactionRequest.FromID)
if err != nil {
return nil, err
}
toID, err := baseIDs.PrototypeIdentityID().FromString(transactionRequest.ToID)
if err != nil {
return nil, err
}
classificationID, err := baseIDs.PrototypeClassificationID().FromString(transactionRequest.ClassificationID)
if err != nil {
return nil, err
}
return NewMessage(
from,
fromID.(ids.IdentityID),
toID.(ids.IdentityID),
classificationID.(ids.ClassificationID),
maintainedProperties,
transactionRequest.CanIssueIdentity,
transactionRequest.CanQuashIdentity,
transactionRequest.CanAddMaintainer,
transactionRequest.CanRemoveMaintainer,
transactionRequest.CanMutateMaintainer,
), nil
}
func (transactionRequest) RegisterLegacyAminoCodec(legacyAmino *codec.LegacyAmino) {
codecUtilities.RegisterModuleConcrete(legacyAmino, transactionRequest{})
}
func requestPrototype() helpers.TransactionRequest {
return transactionRequest{}
}
func newTransactionRequest(baseReq rest.BaseReq, fromID string, toID string, classificationID string, maintainedProperties string, canIssueIdentity bool, canQuashIdentity bool, canAddMaintainer bool, canRemoveMaintainer bool, canMutateMaintainer bool) helpers.TransactionRequest {
return transactionRequest{
BaseReq: baseReq,
FromID: fromID,
ToID: toID,
ClassificationID: classificationID,
MaintainedProperties: maintainedProperties,
CanIssueIdentity: canIssueIdentity,
CanQuashIdentity: canQuashIdentity,
CanAddMaintainer: canAddMaintainer,
CanRemoveMaintainer: canRemoveMaintainer,
CanMutateMaintainer: canMutateMaintainer,
}
}