forked from coinbase/mesh-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
construction.go
291 lines (241 loc) · 7.39 KB
/
construction.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
// Copyright 2020 Coinbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package asserter
import (
"errors"
"fmt"
"github.com/coinbase/rosetta-sdk-go/types"
)
// ConstructionMetadataResponse returns an error if
// the metadata is not a JSON object.
func ConstructionMetadataResponse(
response *types.ConstructionMetadataResponse,
) error {
if response == nil {
return errors.New("construction metadata response cannot be nil")
}
if response.Metadata == nil {
return errors.New("Metadata is nil")
}
return nil
}
// ConstructionSubmitResponse returns an error if
// the types.TransactionIdentifier in the response is not
// valid or if the Submission.Status is not contained
// within the provided validStatuses slice.
func ConstructionSubmitResponse(
response *types.ConstructionSubmitResponse,
) error {
if response == nil {
return errors.New("construction submit response cannot be nil")
}
if err := TransactionIdentifier(response.TransactionIdentifier); err != nil {
return err
}
return nil
}
// ConstructionCombineResponse returns an error if
// a *types.ConstructionCombineResponse does
// not have a populated SignedTransaction.
func ConstructionCombineResponse(
response *types.ConstructionCombineResponse,
) error {
if response == nil {
return errors.New("construction combine response cannot be nil")
}
if len(response.SignedTransaction) == 0 {
return errors.New("signed transaction cannot be empty")
}
return nil
}
// ConstructionDeriveResponse returns an error if
// a *types.ConstructionDeriveResponse does
// not have a populated Address.
func ConstructionDeriveResponse(
response *types.ConstructionDeriveResponse,
) error {
if response == nil {
return errors.New("construction derive response cannot be nil")
}
if len(response.Address) == 0 {
return errors.New("address cannot be empty")
}
return nil
}
// ConstructionHashResponse returns an error if
// a *types.ConstructionHashResponse does
// not have a populated transaction hash.
func ConstructionHashResponse(
response *types.ConstructionHashResponse,
) error {
if response == nil {
return errors.New("construction hash response cannot be nil")
}
if len(response.TransactionHash) == 0 {
return errors.New("transaction hash cannot be empty")
}
return nil
}
// ConstructionParseResponse returns an error if
// a *types.ConstructionParseResponse does
// not have a valid set of operations or
// if the signers is empty.
func (a *Asserter) ConstructionParseResponse(
response *types.ConstructionParseResponse,
signed bool,
) error {
if a == nil {
return ErrAsserterNotInitialized
}
if response == nil {
return errors.New("construction parse response cannot be nil")
}
if len(response.Operations) == 0 {
return errors.New("operations cannot be empty")
}
if err := a.Operations(response.Operations, true); err != nil {
return fmt.Errorf("%w unable to parse operations", err)
}
if signed && len(response.Signers) == 0 {
return errors.New("signers cannot be empty")
}
if !signed && len(response.Signers) > 0 {
return errors.New("signers should be empty for unsigned txs")
}
for i, signer := range response.Signers {
if len(signer) == 0 {
return fmt.Errorf("signer %d cannot be empty string", i)
}
}
return nil
}
// ConstructionPayloadsResponse returns an error if
// a *types.ConstructionPayloadsResponse does
// not have an UnsignedTransaction or has no
// valid *SigningPaylod.
func ConstructionPayloadsResponse(
response *types.ConstructionPayloadsResponse,
) error {
if response == nil {
return errors.New("construction payloads response cannot be nil")
}
if len(response.UnsignedTransaction) == 0 {
return errors.New("unsigned transaction cannot be empty")
}
if len(response.Payloads) == 0 {
return errors.New("signing payloads cannot be empty")
}
for i, payload := range response.Payloads {
if err := SigningPayload(payload); err != nil {
return fmt.Errorf("%w: signing payload %d is invalid", err, i)
}
}
return nil
}
// PublicKey returns an error if
// the *types.PublicKey is nil, is not
// valid hex, or has an undefined CurveType.
func PublicKey(
publicKey *types.PublicKey,
) error {
if publicKey == nil {
return errors.New("PublicKey cannot be nil")
}
if len(publicKey.Bytes) == 0 {
return errors.New("public key bytes cannot be empty")
}
if err := CurveType(publicKey.CurveType); err != nil {
return fmt.Errorf("%w public key curve type is not supported", err)
}
return nil
}
// CurveType returns an error if
// the curve is not a valid types.CurveType.
func CurveType(
curve types.CurveType,
) error {
switch curve {
case types.Secp256k1, types.Edwards25519:
return nil
default:
return fmt.Errorf("%s is not a supported CurveType", curve)
}
}
// SigningPayload returns an error
// if a *types.SigningPayload is nil,
// has an empty address, has invlaid hex,
// or has an invalid SignatureType (if populated).
func SigningPayload(
signingPayload *types.SigningPayload,
) error {
if signingPayload == nil {
return errors.New("signing payload cannot be nil")
}
if len(signingPayload.Address) == 0 {
return errors.New("signing payload address cannot be empty")
}
if len(signingPayload.Bytes) == 0 {
return errors.New("signing payload bytes cannot be empty")
}
// SignatureType can be optionally populated
if len(signingPayload.SignatureType) == 0 {
return nil
}
if err := SignatureType(signingPayload.SignatureType); err != nil {
return fmt.Errorf("%w signature payload signature type is not valid", err)
}
return nil
}
// Signatures returns an error if any
// *types.Signature is invalid.
func Signatures(
signatures []*types.Signature,
) error {
if len(signatures) == 0 {
return errors.New("signatures cannot be empty")
}
for i, signature := range signatures {
if err := SigningPayload(signature.SigningPayload); err != nil {
return fmt.Errorf("%w: signature %d has invalid signing payload", err, i)
}
if err := PublicKey(signature.PublicKey); err != nil {
return fmt.Errorf("%w: signature %d has invalid public key", err, i)
}
if err := SignatureType(signature.SignatureType); err != nil {
return fmt.Errorf("%w: signature %d has invalid signature type", err, i)
}
// Return an error if the requested signature type does not match the
// signture type in the returned signature.
if len(signature.SigningPayload.SignatureType) > 0 &&
signature.SigningPayload.SignatureType != signature.SignatureType {
return fmt.Errorf("requested signature type does not match returned signature type")
}
if len(signature.Bytes) == 0 {
return fmt.Errorf("signature %d bytes cannot be empty", i)
}
}
return nil
}
// SignatureType returns an error if
// signature is not a valid types.SignatureType.
func SignatureType(
signature types.SignatureType,
) error {
switch signature {
case types.Ecdsa, types.EcdsaRecovery, types.Ed25519:
return nil
default:
return fmt.Errorf("%s is not a supported SignatureType", signature)
}
}