-
Notifications
You must be signed in to change notification settings - Fork 36
/
specification.go
418 lines (388 loc) · 13.8 KB
/
specification.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
package types
import (
"errors"
"fmt"
"regexp"
"strings"
"gopkg.in/yaml.v2"
sdk "github.com/cosmos/cosmos-sdk/types"
)
const (
// TODO: Move these to params.
// Default max length for Description.Name
maxDescriptionNameLength = 200
// Default max length for Description.Description
maxDescriptionDescriptionLength = 5000
// Default max length for a ContractSpecification.ClassName
maxContractSpecificationClassNameLength = 1000
// Default max length for RecordSpecification.Name
maxRecordSpecificationNameLength = 200
// Default max length for a RecordSpecification.TypeName
maxRecordSpecificationTypeNameLength = 1000
// Default max length for InputSpecification.Name
maxInputSpecificationNameLength = 200
// Default max length for a InputSpecification.TypeName
maxInputSpecificationTypeNameLength = 1000
// Default max url length
maxURLLength = 2048
)
var (
urlProtocolsAllowedRegexps = []*regexp.Regexp{
regexp.MustCompile("https?://"),
regexp.MustCompile("data:.*,"),
// If you alter this, make sure to alter the associated error message.
}
)
// NewScopeSpecification creates a new ScopeSpecification instance.
func NewScopeSpecification(
specificationID MetadataAddress,
description *Description,
ownerAddresses []string,
partiesInvolved []PartyType,
contractSpecIDs []MetadataAddress,
) *ScopeSpecification {
return &ScopeSpecification{
SpecificationId: specificationID,
Description: description,
OwnerAddresses: ownerAddresses,
PartiesInvolved: partiesInvolved,
ContractSpecIds: contractSpecIDs,
}
}
// ValidateBasic performs basic format checking of data in a ScopeSpecification
func (s ScopeSpecification) ValidateBasic() error {
prefix, err := VerifyMetadataAddressFormat(s.SpecificationId)
if err != nil {
return fmt.Errorf("invalid scope specification id: %w", err)
}
if prefix != PrefixScopeSpecification {
return fmt.Errorf("invalid scope specification id prefix (expected: %s, got %s)", PrefixScopeSpecification, prefix)
}
if s.Description != nil {
err = s.Description.ValidateBasic("ScopeSpecification.Description")
if err != nil {
return err
}
}
if len(s.OwnerAddresses) < 1 {
return errors.New("the ScopeSpecification must have at least one owner")
}
for i, owner := range s.OwnerAddresses {
if _, err = sdk.AccAddressFromBech32(owner); err != nil {
return fmt.Errorf("invalid owner address at index %d on ScopeSpecification: %w", i, err)
}
}
if len(s.PartiesInvolved) == 0 {
return errors.New("the ScopeSpecification must have at least one party involved")
}
for i, contractSpecID := range s.ContractSpecIds {
prefix, err = VerifyMetadataAddressFormat(contractSpecID)
if err != nil {
return fmt.Errorf("invalid contract specification id at index %d: %w", i, err)
}
if prefix != PrefixContractSpecification {
return fmt.Errorf("invalid contract specification id prefix at index %d (expected: %s, got %s)",
i, PrefixContractSpecification, prefix)
}
}
return nil
}
// String implements stringer interface
func (s ScopeSpecification) String() string {
out, _ := yaml.Marshal(s)
return string(out)
}
// NewScopeSpecification creates a new ScopeSpecification instance.
func NewContractSpecification(
specificationID MetadataAddress,
description *Description,
ownerAddresses []string,
partiesInvolved []PartyType,
source isContractSpecification_Source,
className string,
) *ContractSpecification {
return &ContractSpecification{
SpecificationId: specificationID,
Description: description,
OwnerAddresses: ownerAddresses,
PartiesInvolved: partiesInvolved,
Source: source,
ClassName: className,
}
}
// NewContractSpecificationSourceResourceID creates a new source (for a ContractSpecification) with a resource id
func NewContractSpecificationSourceResourceID(resourceID sdk.AccAddress) *ContractSpecification_ResourceId {
return &ContractSpecification_ResourceId{ResourceId: resourceID}
}
// NewContractSpecificationSourceHash creates a new source (for a ContractSpecification) with a hash
func NewContractSpecificationSourceHash(hash string) *ContractSpecification_Hash {
return &ContractSpecification_Hash{Hash: hash}
}
// ValidateBasic performs basic format checking of data in a ContractSpecification
func (s ContractSpecification) ValidateBasic() error {
prefix, err := VerifyMetadataAddressFormat(s.SpecificationId)
if err != nil {
return fmt.Errorf("invalid contract specification id: %w", err)
}
if prefix != PrefixContractSpecification {
return fmt.Errorf("invalid contract specification id prefix (expected: %s, got %s)", PrefixContractSpecification, prefix)
}
if s.Description != nil {
err = s.Description.ValidateBasic("ContractSpecification.Description")
if err != nil {
return err
}
}
if len(s.OwnerAddresses) == 0 {
return fmt.Errorf("invalid owner addresses count (expected > 0 got: %d)", len(s.OwnerAddresses))
}
for i, owner := range s.OwnerAddresses {
if _, err = sdk.AccAddressFromBech32(owner); err != nil {
return fmt.Errorf("invalid owner address at index %d: %w", i, err)
}
}
if len(s.PartiesInvolved) == 0 {
return fmt.Errorf("invalid parties involved count (expected > 0 got: %d)", len(s.PartiesInvolved))
}
if s.Source == nil {
return errors.New("a source is required")
}
switch source := s.Source.(type) {
case *ContractSpecification_ResourceId:
_, err = VerifyMetadataAddressFormat(source.ResourceId)
if err != nil {
return fmt.Errorf("invalid source resource id: %w", err)
}
case *ContractSpecification_Hash:
if len(source.Hash) == 0 {
return errors.New("source hash cannot be empty")
}
default:
return errors.New("unknown source type")
}
if len(s.ClassName) == 0 {
return errors.New("class name cannot be empty")
}
if len(s.ClassName) > maxContractSpecificationClassNameLength {
return fmt.Errorf("class name exceeds maximum length (expected <= %d got: %d)",
maxContractSpecificationClassNameLength, len(s.ClassName))
}
return nil
}
// String implements stringer interface
func (s ContractSpecification) String() string {
out, _ := yaml.Marshal(s)
return string(out)
}
// NewRecordSpecification creates a new RecordSpecification instance
func NewRecordSpecification(
specificationID MetadataAddress,
name string,
inputs []*InputSpecification,
typeName string,
resultType DefinitionType,
responsibleParties []PartyType,
) *RecordSpecification {
return &RecordSpecification{
SpecificationId: specificationID,
Name: name,
Inputs: inputs,
TypeName: typeName,
ResultType: resultType,
ResponsibleParties: responsibleParties,
}
}
// ValidateBasic performs basic format checking of data in a RecordSpecification
func (s RecordSpecification) ValidateBasic() error {
prefix, err := VerifyMetadataAddressFormat(s.SpecificationId)
if err != nil {
return fmt.Errorf("invalid record specification id: %w", err)
}
if prefix != PrefixRecordSpecification {
return fmt.Errorf("invalid record specification id prefix (expected: %s, got %s)",
PrefixRecordSpecification, prefix)
}
if len(s.Name) == 0 {
return errors.New("record specification name cannot be empty")
}
if len(s.Name) > maxRecordSpecificationNameLength {
return fmt.Errorf("record specification name exceeds maximum length (expected <= %d got: %d)",
maxRecordSpecificationNameLength, len(s.Name))
}
// Make sure the provided specification id is correct.
contractSpecUUID, _ := s.SpecificationId.ContractSpecUUID()
expectedID := RecordSpecMetadataAddress(contractSpecUUID, s.Name)
if !s.SpecificationId.Equals(expectedID) {
return fmt.Errorf("invalid record specification id value (expected: %s, got %s)",
expectedID, s.SpecificationId)
}
for i, inputSpec := range s.Inputs {
if err := inputSpec.ValidateBasic(); err != nil {
return fmt.Errorf("invalid input specification at index %d: %w", i, err)
}
}
if len(s.TypeName) == 0 {
return errors.New("record specification type name cannot be empty")
}
if len(s.TypeName) > maxRecordSpecificationTypeNameLength {
return fmt.Errorf("record specification type name exceeds maximum length (expected <= %d got: %d)",
maxRecordSpecificationTypeNameLength, len(s.TypeName))
}
if len(s.ResponsibleParties) == 0 {
return fmt.Errorf("invalid responsible parties count (expected > 0 got: %d)", len(s.ResponsibleParties))
}
if s.ResultType == DefinitionType_DEFINITION_TYPE_UNSPECIFIED {
return errors.New("record specification result type cannot be unspecified")
}
return nil
}
// String implements stringer interface
func (s RecordSpecification) String() string {
out, _ := yaml.Marshal(s)
return string(out)
}
// NewInputSpecification creates a new InputSpecification instance
func NewInputSpecification(
name string,
typeName string,
source isInputSpecification_Source,
) *InputSpecification {
return &InputSpecification{
Name: name,
TypeName: typeName,
Source: source,
}
}
// NewInputSpecificationSourceRecordID creates a new source (for an InputSpecification) with a resource id
func NewInputSpecificationSourceRecordID(recordID MetadataAddress) *InputSpecification_RecordId {
return &InputSpecification_RecordId{RecordId: recordID}
}
// NewContractSpecificationSourceHash creates a new source (for a InputSpecification) with a hash
func NewInputSpecificationSourceHash(hash string) *InputSpecification_Hash {
return &InputSpecification_Hash{Hash: hash}
}
// ValidateBasic performs basic format checking of data in a InputSpecification
func (s InputSpecification) ValidateBasic() error {
if len(s.Name) == 0 {
return errors.New("input specification name cannot be empty")
}
if len(s.Name) > maxInputSpecificationNameLength {
return fmt.Errorf("input specification name exceeds maximum length (expected <= %d got: %d)",
maxInputSpecificationNameLength, len(s.Name))
}
if len(s.TypeName) == 0 {
return errors.New("input specification type name cannot be empty")
}
if len(s.TypeName) > maxInputSpecificationTypeNameLength {
return fmt.Errorf("input specification type name exceeds maximum length (expected <= %d got: %d)",
maxInputSpecificationTypeNameLength, len(s.TypeName))
}
if s.Source == nil {
return errors.New("input specification source is required")
}
switch source := s.Source.(type) {
case *InputSpecification_RecordId:
prefix, err := VerifyMetadataAddressFormat(source.RecordId)
if err != nil {
return fmt.Errorf("invalid input specification source record id: %w", err)
}
if prefix != PrefixRecord {
return fmt.Errorf("invalid input specification source record id prefix (expected: %s, got: %s)",
PrefixRecord, prefix)
}
case *InputSpecification_Hash:
if len(source.Hash) == 0 {
return errors.New("input specification source hash cannot be empty")
}
default:
return errors.New("unknown input specification source type")
}
return nil
}
// String implements stringer interface
func (s InputSpecification) String() string {
out, _ := yaml.Marshal(s)
return string(out)
}
// NewDescription creates a new Description instance.
func NewDescription(name, description, websiteURL, iconURL string) *Description {
return &Description{
Name: name,
Description: description,
WebsiteUrl: websiteURL,
IconUrl: iconURL,
}
}
// ValidateBasic performs basic format checking of data in an Description.
// The path parameter is used to provide extra context to any error messages.
// e.g. If the name field is invalid in this description, and the path provided is "ScopeSpecification.Description",
// the error message will contain "ScopeSpecification.Description.Name" and the problem.
// Provide "" if there is no context you wish to provide.
func (d Description) ValidateBasic(path string) error {
if len(d.Name) == 0 {
return fmt.Errorf("description %s cannot be empty", makeFieldString(path, "Name"))
}
if len(d.Name) > maxDescriptionNameLength {
return fmt.Errorf("description %s exceeds maximum length (expected <= %d got: %d)",
makeFieldString(path, "Name"), maxDescriptionNameLength, len(d.Name))
}
if len(d.Description) > maxDescriptionDescriptionLength {
return fmt.Errorf("description %s exceeds maximum length (expected <= %d got: %d)",
makeFieldString(path, "Description"), maxDescriptionDescriptionLength, len(d.Description))
}
err := validateURLBasic(d.WebsiteUrl, false, path, "WebsiteUrl")
if err != nil {
return err
}
err = validateURLBasic(d.IconUrl, false, path, "IconUrl")
if err != nil {
return err
}
return nil
}
// String implements stringer interface
func (d Description) String() string {
out, _ := yaml.Marshal(d)
return string(out)
}
func (x PartyType) IsValid() bool {
_, ok := PartyType_name[int32(x)]
return ok
}
func (x PartyType) SimpleString() string {
return strings.TrimPrefix(x.String(), "PARTY_TYPE_")
}
// validateURLBasic - Helper function to check if a url string is superficially valid.
// The path and fieldName parameters are combined using makeFieldString for error messages.
func validateURLBasic(url string, required bool, path string, fieldName string) error {
if len(url) == 0 {
if required {
return fmt.Errorf("url %s cannot be empty", makeFieldString(path, fieldName))
}
return nil
}
if len(url) > maxURLLength {
return fmt.Errorf("url %s exceeds maximum length (expected <= %d got: %d)",
makeFieldString(path, fieldName), maxURLLength, len(url))
}
isAllowedProtocol := false
for _, r := range urlProtocolsAllowedRegexps {
if r.MatchString(url) {
isAllowedProtocol = true
break
}
}
if !isAllowedProtocol {
return fmt.Errorf("url %s must use the http, https, or data protocol", makeFieldString(path, fieldName))
}
return nil
}
// makeFieldString: Helper function to create a string for a field meant for use in an error message for that field.
// If path is empty, then fieldName is returned unaltered.
// If path is not empty, "(path) fieldName" is returned.
func makeFieldString(path string, fieldName string) string {
if len(path) == 0 {
return fieldName
}
return fmt.Sprintf("(%s) %s", path, fieldName)
}