forked from stellar/go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
helpers_test.go
411 lines (350 loc) · 12.6 KB
/
helpers_test.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
package txnbuild
import (
"fmt"
"testing"
"github.com/stellar/go/keypair"
"github.com/stellar/go/support/errors"
"github.com/stellar/go/xdr"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func newKeypair0() *keypair.Full {
// Address: GDQNY3PBOJOKYZSRMK2S7LHHGWZIUISD4QORETLMXEWXBI7KFZZMKTL3
return newKeypair("SBPQUZ6G4FZNWFHKUWC5BEYWF6R52E3SEP7R3GWYSM2XTKGF5LNTWW4R")
}
func newKeypair1() *keypair.Full {
// Address: GAS4V4O2B7DW5T7IQRPEEVCRXMDZESKISR7DVIGKZQYYV3OSQ5SH5LVP
return newKeypair("SBMSVD4KKELKGZXHBUQTIROWUAPQASDX7KEJITARP4VMZ6KLUHOGPTYW")
}
func newKeypair2() *keypair.Full {
// Address: GB7BDSZU2Y27LYNLALKKALB52WS2IZWYBDGY6EQBLEED3TJOCVMZRH7H
return newKeypair("SBZVMB74Z76QZ3ZOY7UTDFYKMEGKW5XFJEB6PFKBF4UYSSWHG4EDH7PY")
}
func newKeypair(seed string) *keypair.Full {
myKeypair, _ := keypair.Parse(seed)
return myKeypair.(*keypair.Full)
}
func check(err error) {
if err != nil {
panic(err)
}
}
func checkChallengeTx(txeBase64, anchorName string) (bool, error) {
var txXDR xdr.TransactionEnvelope
err := xdr.SafeUnmarshalBase64(txeBase64, &txXDR)
if err != nil {
return false, err
}
op := txXDR.Operations()[0]
if (xdr.OperationTypeManageData == op.Body.Type) && (op.Body.ManageDataOp.DataName == xdr.String64(anchorName+" auth")) {
return true, nil
}
return false, errors.New("invalid challenge tx")
}
func unmarshalBase64(txeB64 string) (xdr.TransactionEnvelope, error) {
var xdrEnv xdr.TransactionEnvelope
err := xdr.SafeUnmarshalBase64(txeB64, &xdrEnv)
return xdrEnv, err
}
func newSignedTransaction(
params TransactionParams,
network string,
keypairs ...*keypair.Full,
) (string, error) {
tx, err := NewTransaction(params)
if err != nil {
return "", errors.Wrap(err, "couldn't create transaction")
}
tx, err = tx.Sign(network, keypairs...)
if err != nil {
return "", errors.Wrap(err, "couldn't sign transaction")
}
txeBase64, err := tx.Base64()
if err != nil {
return "", errors.Wrap(err, "couldn't encode transaction")
}
return txeBase64, err
}
func newSignedFeeBumpTransaction(
params FeeBumpTransactionParams,
network string,
keypairs ...*keypair.Full,
) (string, error) {
tx, err := NewFeeBumpTransaction(params)
if err != nil {
return "", errors.Wrap(err, "couldn't create transaction")
}
tx, err = tx.Sign(network, keypairs...)
if err != nil {
return "", errors.Wrap(err, "couldn't sign transaction")
}
txeBase64, err := tx.Base64()
if err != nil {
return "", errors.Wrap(err, "couldn't encode transaction")
}
return txeBase64, err
}
func convertToV0(tx *Transaction) {
signatures := tx.Signatures()
tx.envelope.V0 = &xdr.TransactionV0Envelope{
Tx: xdr.TransactionV0{
SourceAccountEd25519: *tx.envelope.SourceAccount().Ed25519,
Fee: xdr.Uint32(tx.envelope.Fee()),
SeqNum: xdr.SequenceNumber(tx.envelope.SeqNum()),
TimeBounds: tx.envelope.TimeBounds(),
Memo: tx.envelope.Memo(),
Operations: tx.envelope.Operations(),
},
Signatures: signatures,
}
tx.envelope.V1 = nil
tx.envelope.Type = xdr.EnvelopeTypeEnvelopeTypeTxV0
}
func TestValidateStellarPublicKey(t *testing.T) {
validKey := "GDWZCOEQRODFCH6ISYQPWY67L3ULLWS5ISXYYL5GH43W7YFMTLB65PYM"
err := validateStellarPublicKey(validKey)
assert.NoError(t, err, "public key should be valid")
invalidKey := "GDWZCOEQRODFCH6ISYQPWY67L3ULLWS5ISXYYL5GH43W7Y"
err = validateStellarPublicKey(invalidKey)
expectedErrMsg := "GDWZCOEQRODFCH6ISYQPWY67L3ULLWS5ISXYYL5GH43W7Y is not a valid stellar public key"
require.EqualError(t, err, expectedErrMsg, "public key should be invalid")
invalidKey = ""
err = validateStellarPublicKey(invalidKey)
expectedErrMsg = "public key is undefined"
require.EqualError(t, err, expectedErrMsg, "public key should be invalid")
invalidKey = "SBCVMMCBEDB64TVJZFYJOJAERZC4YVVUOE6SYR2Y76CBTENGUSGWRRVO"
err = validateStellarPublicKey(invalidKey)
expectedErrMsg = "SBCVMMCBEDB64TVJZFYJOJAERZC4YVVUOE6SYR2Y76CBTENGUSGWRRVO is not a valid stellar public key"
require.EqualError(t, err, expectedErrMsg, "public key should be invalid")
}
func TestValidateStellarAssetWithValidAsset(t *testing.T) {
nativeAsset := NativeAsset{}
err := validateStellarAsset(nativeAsset)
assert.NoError(t, err)
kp0 := newKeypair0()
creditAsset := CreditAsset{"XYZ", kp0.Address()}
err = validateStellarAsset(creditAsset)
assert.NoError(t, err)
}
func TestValidateStellarAssetWithInValidAsset(t *testing.T) {
err := validateStellarAsset(nil)
assert.Error(t, err)
expectedErrMsg := "asset is undefined"
require.EqualError(t, err, expectedErrMsg, "An asset is required")
kp0 := newKeypair0()
creditAssetNoCode := CreditAsset{Code: "", Issuer: kp0.Address()}
err = validateStellarAsset(creditAssetNoCode)
assert.Error(t, err)
expectedErrMsg = "asset code length must be between 1 and 12 characters"
require.EqualError(t, err, expectedErrMsg, "An asset code is required")
creditAssetNoIssuer := CreditAsset{Code: "ABC", Issuer: ""}
err = validateStellarAsset(creditAssetNoIssuer)
assert.Error(t, err)
expectedErrMsg = "asset issuer: public key is undefined"
require.EqualError(t, err, expectedErrMsg, "An asset issuer is required")
}
func TestValidateAmount(t *testing.T) {
err := validateAmount(int64(10))
assert.NoError(t, err)
err = validateAmount("10")
assert.NoError(t, err)
err = validateAmount(int64(0))
assert.NoError(t, err)
err = validateAmount("0")
assert.NoError(t, err)
}
func TestValidateAmountInvalidValue(t *testing.T) {
err := validateAmount(int64(-10))
assert.Error(t, err)
expectedErrMsg := "amount can not be negative"
require.EqualError(t, err, expectedErrMsg, "should be a valid stellar amount")
err = validateAmount("-10")
assert.Error(t, err)
expectedErrMsg = "amount can not be negative"
require.EqualError(t, err, expectedErrMsg, "should be a valid stellar amount")
err = validateAmount(10)
assert.Error(t, err)
expectedErrMsg = "could not parse expected numeric value 10"
require.EqualError(t, err, expectedErrMsg, "should be a valid stellar amount")
err = validateAmount("abc")
assert.Error(t, err)
expectedErrMsg = "invalid amount format: abc"
require.EqualError(t, err, expectedErrMsg, "should be a valid stellar amount")
}
func TestValidateAllowTrustAsset(t *testing.T) {
err := validateAssetCode(nil)
assert.Error(t, err)
expectedErrMsg := "asset is undefined"
require.EqualError(t, err, expectedErrMsg, "An asset is required")
err = validateAssetCode(NativeAsset{})
assert.Error(t, err)
expectedErrMsg = "native (XLM) asset type is not allowed"
require.EqualError(t, err, expectedErrMsg, "An asset is required")
// allow trust asset does not require asset issuer
atAsset := CreditAsset{Code: "ABCD"}
err = validateAssetCode(atAsset)
assert.NoError(t, err)
}
func TestValidateChangeTrustAsset(t *testing.T) {
err := validateChangeTrustAsset(nil)
assert.Error(t, err)
expectedErrMsg := "asset is undefined"
require.EqualError(t, err, expectedErrMsg, "An asset is required")
err = validateChangeTrustAsset(NativeAsset{}.MustToChangeTrustAsset())
assert.Error(t, err)
expectedErrMsg = "native (XLM) asset type is not allowed"
require.EqualError(t, err, expectedErrMsg, "A custom asset is required")
kp0 := newKeypair0()
ctAsset0 := CreditAsset{Issuer: kp0.Address()}
err = validateChangeTrustAsset(ctAsset0.MustToChangeTrustAsset())
assert.Error(t, err)
expectedErrMsg = "asset code length must be between 1 and 12 characters"
require.EqualError(t, err, expectedErrMsg, "asset code is required")
ctAsset1 := CreditAsset{Code: "ABCD"}
err = validateChangeTrustAsset(ctAsset1.MustToChangeTrustAsset())
assert.Error(t, err)
expectedErrMsg = "asset issuer: public key is undefined"
require.EqualError(t, err, expectedErrMsg, "asset issuer is required")
ctAsset2 := CreditAsset{Code: "ABCD", Issuer: kp0.Address()}
err = validateChangeTrustAsset(ctAsset2.MustToChangeTrustAsset())
assert.NoError(t, err)
}
func TestValidatePassiveOfferZeroValues(t *testing.T) {
cpo := CreatePassiveSellOffer{}
err := validatePassiveOffer(cpo.Buying, cpo.Selling, cpo.Amount, cpo.Price)
assert.Error(t, err)
expectedErrMsg := "Field: Buying, Error: asset is undefined"
require.EqualError(t, err, expectedErrMsg, "Buying asset is required")
}
func TestValidatePassiveOfferInvalidAmount(t *testing.T) {
kp0 := newKeypair0()
buying := CreditAsset{Code: "ABCD", Issuer: kp0.Address()}
selling := NativeAsset{}
cpo := CreatePassiveSellOffer{
Buying: buying,
Selling: selling,
Price: xdr.Price{1, 1},
Amount: "-1",
}
err := validatePassiveOffer(cpo.Buying, cpo.Selling, cpo.Amount, cpo.Price)
assert.Error(t, err)
expectedErrMsg := "Field: Amount, Error: amount can not be negative"
require.EqualError(t, err, expectedErrMsg, "valid amount is required")
}
func TestValidatePassiveOfferInvalidPrice(t *testing.T) {
kp0 := newKeypair0()
buying := CreditAsset{Code: "ABCD", Issuer: kp0.Address()}
selling := NativeAsset{}
cpo := CreatePassiveSellOffer{
Buying: buying,
Selling: selling,
Price: xdr.Price{-1, 1},
Amount: "10",
}
err := validatePassiveOffer(cpo.Buying, cpo.Selling, cpo.Amount, cpo.Price)
assert.Error(t, err)
expectedErrMsg := "Field: Price, Error: price cannot be negative: -1/1"
require.EqualError(t, err, expectedErrMsg, "valid price is required")
}
func TestValidatePassiveOfferInvalidAsset(t *testing.T) {
buying := NativeAsset{}
selling := CreditAsset{Code: "ABCD"}
cpo := CreatePassiveSellOffer{
Buying: buying,
Selling: selling,
Price: xdr.Price{1, 1},
Amount: "10",
}
err := validatePassiveOffer(cpo.Buying, cpo.Selling, cpo.Amount, cpo.Price)
assert.Error(t, err)
expectedErrMsg := "Field: Selling, Error: asset issuer: public key is undefined"
require.EqualError(t, err, expectedErrMsg, "Selling asset is required")
kp0 := newKeypair0()
buying1 := CreditAsset{Issuer: kp0.Address()}
selling1 := NativeAsset{}
cpo1 := CreatePassiveSellOffer{
Buying: buying1,
Selling: selling1,
Price: xdr.Price{1, 1},
Amount: "10",
}
err = validatePassiveOffer(cpo1.Buying, cpo1.Selling, cpo1.Amount, cpo1.Price)
assert.Error(t, err)
expectedErrMsg = "Field: Buying, Error: asset code length must be between 1 and 12 characters"
require.EqualError(t, err, expectedErrMsg, "Selling asset is required")
}
func TestValidateOfferManageBuyOffer(t *testing.T) {
kp0 := newKeypair0()
buying := CreditAsset{Code: "ABCD", Issuer: kp0.Address()}
selling := NativeAsset{}
mbo := ManageBuyOffer{
Buying: buying,
Selling: selling,
Price: xdr.Price{1, 1},
Amount: "10",
OfferID: -1,
}
err := validateOffer(mbo.Buying, mbo.Selling, mbo.Amount, mbo.Price, mbo.OfferID)
assert.Error(t, err)
expectedErrMsg := "Field: OfferID, Error: amount can not be negative"
require.EqualError(t, err, expectedErrMsg, "valid offerID is required")
}
func TestValidateOfferManageSellOffer(t *testing.T) {
kp0 := newKeypair0()
buying := CreditAsset{Code: "ABCD", Issuer: kp0.Address()}
selling := NativeAsset{}
mso := ManageSellOffer{
Buying: buying,
Selling: selling,
Price: xdr.Price{1, 1},
Amount: "10",
OfferID: -1,
}
err := validateOffer(mso.Buying, mso.Selling, mso.Amount, mso.Price, mso.OfferID)
assert.Error(t, err)
expectedErrMsg := "Field: OfferID, Error: amount can not be negative"
require.EqualError(t, err, expectedErrMsg, "valid offerID is required")
}
func TestAssetStringParsing(t *testing.T) {
kp0 := newKeypair0()
cred4 := CreditAsset{Code: "ABCD", Issuer: kp0.Address()}
xdr, err := cred4.ToXDR()
assert.NoError(t, err)
cred4String := xdr.StringCanonical()
kp1 := newKeypair1()
cred12 := CreditAsset{Code: "ABCD1234EFGH", Issuer: kp1.Address()}
xdr, err = cred12.ToXDR()
assert.NoError(t, err)
cred12String := xdr.StringCanonical()
native := NativeAsset{}
xdr, err = native.ToXDR()
assert.NoError(t, err)
nativeString := xdr.StringCanonical()
assets := make([]Asset, 3)
for i, input := range []string{nativeString, cred4String, cred12String} {
actual, innerErr := ParseAssetString(input)
assert.NoError(t, innerErr)
assets[i] = actual
}
compareAssets := func(expected Asset, actual Asset) bool {
expXdr, innerErr := expected.ToXDR()
if innerErr != nil {
return false
}
actXdr, innerErr := actual.ToXDR()
if innerErr != nil {
return false
}
return expXdr.Equals(actXdr)
}
assert.True(t, compareAssets(native, assets[0]))
assert.True(t, compareAssets(cred4, assets[1]))
assert.True(t, compareAssets(cred12, assets[2]))
// Now sanity-check some basic error cases
result, err := ParseAssetString("erroneous:maximus")
assert.Error(t, err)
assert.Equal(t, nil, result)
result, err = ParseAssetString(fmt.Sprintf("ABCD:%s,EFGH:%s", kp0.Address(), kp1.Address()))
assert.Error(t, err)
assert.Equal(t, nil, result)
}