/
transaction_request.go
95 lines (84 loc) · 3.25 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
// Copyright [2021] - [2022], AssetMantle Pte. Ltd. and the code contributors
// SPDX-License-Identifier: Apache-2.0
package unprovision
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/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"`
To string `json:"to" valid:"required~required field to missing, matches(^[a-z0-9]+$)~invalid field to"`
IdentityID string `json:"identityID" valid:"required~required field identityID missing, matches(^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$)~invalid field identityID"`
}
var _ helpers.TransactionRequest = (*transactionRequest)(nil)
// Validate godoc
// @Summary Unprovision identity transaction
// @Description Unprovision identity transaction
// @Accept text/plain
// @Produce json
// @Tags Identities
// @Param body body transactionRequest true "Request body to unprovision identity"
// @Success 200 {object} transactionResponse "Message for a successful response."
// @Failure default {object} transactionResponse "Message for an unexpected error response."
// @Router /identities/unprovision [post]
func (transactionRequest transactionRequest) Validate() error {
_, err := govalidator.ValidateStruct(transactionRequest)
return err
}
func (transactionRequest transactionRequest) FromCLI(cliCommand helpers.CLICommand, context client.Context) (helpers.TransactionRequest, error) {
return newTransactionRequest(
cliCommand.ReadBaseReq(context),
cliCommand.ReadString(constants.To),
cliCommand.ReadString(constants.IdentityID),
), 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
}
to, err := sdkTypes.AccAddressFromBech32(transactionRequest.To)
if err != nil {
return nil, err
}
identityID, err := baseIDs.PrototypeIdentityID().FromString(transactionRequest.IdentityID)
if err != nil {
return nil, err
}
return NewMessage(
from,
to,
identityID.(ids.IdentityID),
), nil
}
func (transactionRequest) RegisterLegacyAminoCodec(legacyAmino *codec.LegacyAmino) {
codecUtilities.RegisterModuleConcrete(legacyAmino, transactionRequest{})
}
func requestPrototype() helpers.TransactionRequest {
return transactionRequest{}
}
func newTransactionRequest(baseReq rest.BaseReq, to string, identityID string) helpers.TransactionRequest {
return transactionRequest{
BaseReq: baseReq,
To: to,
IdentityID: identityID,
}
}