-
Notifications
You must be signed in to change notification settings - Fork 46
/
models_app_links.go
215 lines (177 loc) · 5.65 KB
/
models_app_links.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
package types
import (
"encoding/hex"
"fmt"
"strings"
"time"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// NewApplicationLink allows to build a new ApplicationLink instance
func NewApplicationLink(
user string, data Data, state ApplicationLinkState, oracleRequest OracleRequest, result *Result, creationTime time.Time,
) ApplicationLink {
return ApplicationLink{
User: user,
Data: data,
State: state,
OracleRequest: oracleRequest,
Result: result,
CreationTime: creationTime,
}
}
// Validate returns an error if the instance does not contain valid data
func (l ApplicationLink) Validate() error {
_, err := sdk.AccAddressFromBech32(l.User)
if err != nil {
return fmt.Errorf("invalid user address: %s", err)
}
err = l.Data.Validate()
if err != nil {
return err
}
err = l.OracleRequest.Validate()
if err != nil {
return err
}
if l.Result != nil {
err = l.Result.Validate()
if err != nil {
return err
}
}
if l.CreationTime.IsZero() {
return fmt.Errorf("invalid creation time: %s", l.CreationTime)
}
return nil
}
// IsVerificationOngoing tells whether the verification for the link is still ongoing
func (l *ApplicationLink) IsVerificationOngoing() bool {
return l.State == ApplicationLinkStateInitialized || l.State == AppLinkStateVerificationStarted
}
// MustMarshalApplicationLink serializes the given application link using the provided BinaryMarshaler
func MustMarshalApplicationLink(cdc codec.BinaryMarshaler, link ApplicationLink) []byte {
return cdc.MustMarshalBinaryBare(&link)
}
// MustUnmarshalApplicationLink deserializes the given byte array as an application link using
// the provided BinaryMarshaler
func MustUnmarshalApplicationLink(cdc codec.BinaryMarshaler, bz []byte) ApplicationLink {
var link ApplicationLink
cdc.MustUnmarshalBinaryBare(bz, &link)
return link
}
// --------------------------------------------------------------------------------------------------------------------
// NewData allows to build a new Data instance
func NewData(application, username string) Data {
return Data{
Application: application,
Username: username,
}
}
// Validate returns an error if the instance does not contain valid data
func (d Data) Validate() error {
if len(strings.TrimSpace(d.Application)) == 0 {
return fmt.Errorf("application name cannot be empty or blank")
}
if len(strings.TrimSpace(d.Username)) == 0 {
return fmt.Errorf("application username cannot be empty or blank")
}
return nil
}
// --------------------------------------------------------------------------------------------------------------------
// NewOracleRequest allows to build a new OracleRequest instance
func NewOracleRequest(id int64, scriptID int64, callData OracleRequest_CallData, clientID string) OracleRequest {
return OracleRequest{
ID: id,
OracleScriptID: scriptID,
CallData: callData,
ClientID: clientID,
}
}
// Validate returns an error if the instance does not contain valid data
func (o OracleRequest) Validate() error {
if o.OracleScriptID <= 0 {
return fmt.Errorf("invalid oracle script id: %d", o.OracleScriptID)
}
err := o.CallData.Validate()
if err != nil {
return err
}
if len(strings.TrimSpace(o.ClientID)) == 0 {
return fmt.Errorf("client id cannot be empty or blank")
}
return nil
}
// NewOracleRequestCallData allows to build a new OracleRequest_CallData instance
func NewOracleRequestCallData(application, callData string) OracleRequest_CallData {
return OracleRequest_CallData{
Application: application,
CallData: callData,
}
}
// Validate returns an error if the instance does not contain valid data
func (c OracleRequest_CallData) Validate() error {
if len(strings.TrimSpace(c.Application)) == 0 {
return fmt.Errorf("application cannot be empty or blank")
}
if len(strings.TrimSpace(c.CallData)) == 0 {
return fmt.Errorf("call data cannot be empty or blank")
}
if _, err := hex.DecodeString(c.CallData); err != nil {
return fmt.Errorf("invalid call data encoding: must be hex")
}
return nil
}
// --------------------------------------------------------------------------------------------------------------------
// Validate returns an error if the instance does not contain valid data
func (r *Result) Validate() error {
var err error
switch result := (r.Sum).(type) {
case *Result_Success_:
err = result.Validate()
case *Result_Failed_:
err = result.Validate()
}
return err
}
// NewErrorResult allows to build a new Result instance representing an error
func NewErrorResult(error string) *Result {
return &Result{
Sum: &Result_Failed_{
Failed: &Result_Failed{
Error: error,
},
},
}
}
// Validate returns an error if the instance does not contain valid data
func (r Result_Failed_) Validate() error {
if len(strings.TrimSpace(r.Failed.Error)) == 0 {
return fmt.Errorf("error message cannot be empty or blank")
}
return nil
}
// NewSuccessResult allows to build a new Result instance representing a success
func NewSuccessResult(value, signature string) *Result {
return &Result{
Sum: &Result_Success_{
Success: &Result_Success{
Value: value,
Signature: signature,
},
},
}
}
// Validate returns an error if the instance does not contain valid data
func (r Result_Success_) Validate() error {
if len(strings.TrimSpace(r.Success.Value)) == 0 {
return fmt.Errorf("value cannot be empty or blank")
}
if len(strings.TrimSpace(r.Success.Signature)) == 0 {
return fmt.Errorf("signature cannot be empty or blank")
}
if _, err := hex.DecodeString(r.Success.Signature); err != nil {
return fmt.Errorf("invalid signature encoding; must be hex")
}
return nil
}