-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.go
133 lines (119 loc) · 5.35 KB
/
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
// Copyright [2021] - [2022], AssetMantle Pte. Ltd. and the code contributors
// SPDX-License-Identifier: Apache-2.0
package define
import (
"encoding/json"
errorConstants "github.com/AssetMantle/modules/schema/errors/constants"
"github.com/AssetMantle/modules/schema/helpers/base"
"github.com/asaskevich/govalidator"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/codec"
sdkTypes "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/AssetMantle/modules/schema/helpers"
"github.com/AssetMantle/modules/schema/helpers/constants"
baseIDs "github.com/AssetMantle/modules/schema/ids/base"
"github.com/AssetMantle/modules/schema/lists/utilities"
codecUtilities "github.com/AssetMantle/modules/utilities/codec"
)
type transactionRequest struct {
BaseReq rest.BaseReq `json:"baseReq"`
FromID string `json:"fromID" valid:"required~required field fromID missing, matches(^[A-Za-z0-9-_=.|]+$)~invalid field fromID"`
ImmutableMetaProperties string `json:"immutableMetaProperties" valid:"required~required field immutableMetaProperties missing, matches(^.*$)~invalid field immutableMetaProperties"`
ImmutableProperties string `json:"immutableProperties" valid:"required~required field immutableProperties missing, matches(^.*$)~invalid field immutableProperties"`
MutableMetaProperties string `json:"mutableMetaProperties" valid:"required~required field mutableMetaProperties missing, matches(^.*$)~invalid field mutableMetaProperties"`
MutableProperties string `json:"mutableProperties" valid:"required~required field mutableProperties missing, matches(^.*$)~invalid field mutableProperties"`
}
var _ helpers.TransactionRequest = (*transactionRequest)(nil)
// Validate godoc
// @Summary Define an asset transaction
// @Description Define asset with mutable immutable properties
// @Accept text/plain
// @Produce json
// @Tags Assets
// @Param body transactionRequest true "A transaction to define the asset."
// @Success 200 {object} transactionResponse "Message for a successful transaction."
// @Failure default {object} transactionResponse "Message for an unexpected error in the transaction."
// @Router /assets/define [post]
func (transactionRequest transactionRequest) Validate() error {
_, err := govalidator.ValidateStruct(transactionRequest)
if err != nil {
return err
}
inputValidator := base.NewInputValidator(constants.PropertyExpression)
if !inputValidator.IsValid(transactionRequest.ImmutableProperties, transactionRequest.ImmutableMetaProperties, transactionRequest.MutableProperties, transactionRequest.MutableMetaProperties) {
return errorConstants.IncorrectFormat
}
return nil
}
func (transactionRequest transactionRequest) FromCLI(cliCommand helpers.CLICommand, cliContext context.CLIContext) (helpers.TransactionRequest, error) {
return newTransactionRequest(
cliCommand.ReadBaseReq(cliContext),
cliCommand.ReadString(constants.FromID),
cliCommand.ReadString(constants.ImmutableMetaProperties),
cliCommand.ReadString(constants.ImmutableProperties),
cliCommand.ReadString(constants.MutableMetaProperties),
cliCommand.ReadString(constants.MutableProperties),
), 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
}
immutableMetaProperties, err := utilities.ReadMetaPropertyList(transactionRequest.ImmutableMetaProperties)
if err != nil {
return nil, err
}
immutableProperties, err := utilities.ReadMetaPropertyList(transactionRequest.ImmutableProperties)
if err != nil {
return nil, err
}
immutableProperties = immutableProperties.ScrubData()
mutableMetaProperties, err := utilities.ReadMetaPropertyList(transactionRequest.MutableMetaProperties)
if err != nil {
return nil, err
}
mutableProperties, err := utilities.ReadMetaPropertyList(transactionRequest.MutableProperties)
if err != nil {
return nil, err
}
mutableProperties = mutableProperties.ScrubData()
fromID, err := baseIDs.ReadIdentityID(transactionRequest.FromID)
if err != nil {
return nil, err
}
return newMessage(
from,
fromID,
immutableMetaProperties,
immutableProperties,
mutableMetaProperties,
mutableProperties,
), nil
}
func (transactionRequest) RegisterCodec(codec *codec.Codec) {
codecUtilities.RegisterModuleConcrete(codec, transactionRequest{})
}
func requestPrototype() helpers.TransactionRequest {
return transactionRequest{}
}
func newTransactionRequest(baseReq rest.BaseReq, fromID string, immutableMetaProperties string, immutableProperties string, mutableMetaProperties string, mutableProperties string) helpers.TransactionRequest {
return transactionRequest{
BaseReq: baseReq,
FromID: fromID,
ImmutableMetaProperties: immutableMetaProperties,
ImmutableProperties: immutableProperties,
MutableMetaProperties: mutableMetaProperties,
MutableProperties: mutableProperties,
}
}