forked from coinbase/mesh-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
construction.go
312 lines (257 loc) · 7.95 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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
// 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 (
"fmt"
"github.com/coinbase/rosetta-sdk-go/types"
)
// ConstructionPreprocessResponse returns an error if
// the request public keys are not valid AccountIdentifiers.
func ConstructionPreprocessResponse(
response *types.ConstructionPreprocessResponse,
) error {
if response == nil {
return ErrConstructionPreprocessResponseIsNil
}
for _, accountIdentifier := range response.RequiredPublicKeys {
if err := AccountIdentifier(accountIdentifier); err != nil {
return err
}
}
return nil
}
// ConstructionMetadataResponse returns an error if
// the metadata is not a JSON object.
func ConstructionMetadataResponse(
response *types.ConstructionMetadataResponse,
) error {
if response == nil {
return ErrConstructionMetadataResponseIsNil
}
if response.Metadata == nil {
return ErrConstructionMetadataResponseMetadataMissing
}
if err := AssertUniqueAmounts(response.SuggestedFee); err != nil {
return fmt.Errorf("%w: duplicate suggested fee currency found", err)
}
return nil
}
// TransactionIdentifierResponse returns an error if
// the types.TransactionIdentifier in the response is not
// valid.
func TransactionIdentifierResponse(
response *types.TransactionIdentifierResponse,
) error {
if response == nil {
return ErrTxIdentifierResponseIsNil
}
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 ErrConstructionCombineResponseIsNil
}
if len(response.SignedTransaction) == 0 {
return ErrSignedTxEmpty
}
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 ErrConstructionDeriveResponseIsNil
}
if err := AccountIdentifier(response.AccountIdentifier); err != nil {
return fmt.Errorf("%w: %s", ErrConstructionDeriveResponseAddrEmpty, err.Error())
}
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 ErrConstructionParseResponseIsNil
}
if len(response.Operations) == 0 {
return ErrConstructionParseResponseOperationsEmpty
}
if err := a.Operations(response.Operations, true); err != nil {
return fmt.Errorf("%w unable to parse operations", err)
}
if signed && len(response.AccountIdentifierSigners) == 0 {
return ErrConstructionParseResponseSignersEmptyOnSignedTx
}
if !signed && len(response.AccountIdentifierSigners) > 0 {
return ErrConstructionParseResponseSignersNonEmptyOnUnsignedTx
}
for i, signer := range response.AccountIdentifierSigners {
if err := AccountIdentifier(signer); err != nil {
return fmt.Errorf("%w: at index %d", ErrConstructionParseResponseSignerEmpty, i)
}
}
if len(response.AccountIdentifierSigners) > 0 {
if err := AccountArray("signers", response.AccountIdentifierSigners); err != nil {
return fmt.Errorf("%w: %s", ErrConstructionParseResponseDuplicateSigner, err.Error())
}
}
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 ErrConstructionPayloadsResponseIsNil
}
if len(response.UnsignedTransaction) == 0 {
return ErrConstructionPayloadsResponseUnsignedTxEmpty
}
if len(response.Payloads) == 0 {
return ErrConstructionPayloadsResponsePayloadsEmpty
}
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 ErrPublicKeyIsNil
}
if len(publicKey.Bytes) == 0 {
return ErrPublicKeyBytesEmpty
}
if BytesArrayZero(publicKey.Bytes) {
return ErrPublicKeyBytesZero
}
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.Secp256r1, types.Edwards25519, types.Tweedle, types.Pallas:
return nil
default:
return fmt.Errorf("%w: %s", ErrCurveTypeNotSupported, 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 ErrSigningPayloadIsNil
}
if err := AccountIdentifier(signingPayload.AccountIdentifier); err != nil {
return fmt.Errorf("%w: %s", ErrSigningPayloadAddrEmpty, err)
}
if len(signingPayload.Bytes) == 0 {
return ErrSigningPayloadBytesEmpty
}
if BytesArrayZero(signingPayload.Bytes) {
return ErrSigningPayloadBytesZero
}
// 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 ErrSignaturesEmpty
}
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
// signature type in the returned signature.
if len(signature.SigningPayload.SignatureType) > 0 &&
signature.SigningPayload.SignatureType != signature.SignatureType {
return ErrSignaturesReturnedSigMismatch
}
if len(signature.Bytes) == 0 {
return fmt.Errorf("%w: signature %d has 0 bytes", ErrSignatureBytesEmpty, i)
}
if BytesArrayZero(signature.Bytes) {
return ErrSignatureBytesZero
}
}
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, types.Schnorr1, types.SchnorrPoseidon:
return nil
default:
return fmt.Errorf("%w: %s", ErrSignatureTypeNotSupported, signature)
}
}