-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathuphold_test.go
379 lines (317 loc) · 9.28 KB
/
uphold_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
package uphold
import (
"context"
"crypto/tls"
"encoding/hex"
"errors"
"net/http"
"net/url"
"os"
"testing"
"time"
"github.com/brave-intl/bat-go/libs/altcurrency"
errorutils "github.com/brave-intl/bat-go/libs/errors"
"github.com/brave-intl/bat-go/libs/httpsignature"
"github.com/brave-intl/bat-go/libs/pindialer"
"github.com/brave-intl/bat-go/libs/wallet"
uuid "github.com/satori/go.uuid"
"github.com/shopspring/decimal"
"golang.org/x/crypto/ed25519"
"gotest.tools/assert"
)
func TestGetCardDetails(t *testing.T) {
ctx := context.Background()
if os.Getenv("UPHOLD_ACCESS_TOKEN") == "" {
t.Skip("skipping test; UPHOLD_ACCESS_TOKEN not set")
}
var info wallet.Info
info.Provider = "uphold"
info.ProviderID = "6654ecb0-6079-4f6c-ba58-791cc890a561"
{
tmp := altcurrency.BAT
info.AltCurrency = &tmp
}
wallet, err := FromWalletInfo(ctx, info)
if err != nil {
t.Error(err)
}
_, err = wallet.GetBalance(ctx, true)
if err != nil {
t.Error(err)
}
}
func TestRegister(t *testing.T) {
ctx := context.Background()
if os.Getenv("UPHOLD_ACCESS_TOKEN") == "" {
t.Skip("skipping test; UPHOLD_ACCESS_TOKEN not set")
}
var info wallet.Info
info.Provider = "uphold"
info.ProviderID = ""
{
tmp := altcurrency.BAT
info.AltCurrency = &tmp
}
publicKey, privateKey, err := httpsignature.GenerateEd25519Key(nil)
if err != nil {
t.Fatal(err)
}
destWallet := &Wallet{Info: info, PrivKey: privateKey, PubKey: publicKey}
err = destWallet.Register(ctx, "bat-go test card")
if err != nil {
t.Error(err)
}
}
func TestDecodeTransaction(t *testing.T) {
var info wallet.Info
info.Provider = "uphold"
info.ProviderID = uuid.NewV4().String()
{
tmp := altcurrency.BAT
info.AltCurrency = &tmp
}
wallet, err := FromWalletInfo(context.Background(), info)
if err != nil {
t.Error(err)
}
var pk httpsignature.Ed25519PubKey
pk, err = hex.DecodeString("424073b208e97af51cab7a389bcfe6942a3b7c7520fe9dab84f311f7846f5fcf")
if err != nil {
t.Error(err)
}
wallet.PubKey = pk
txnB64 := "eyJoZWFkZXJzIjp7ImRpZ2VzdCI6IlNIQS0yNTY9WFg0YzgvM0J4ejJkZWNkakhpY0xWaXJ5dTgxbWdGNkNZTTNONFRHc0xoTT0iLCJzaWduYXR1cmUiOiJrZXlJZD1cInByaW1hcnlcIixhbGdvcml0aG09XCJlZDI1NTE5XCIsaGVhZGVycz1cImRpZ2VzdFwiLHNpZ25hdHVyZT1cIjI4TitabzNodlRRWmR2K2trbGFwUE5IY29OMEpLdWRiSU5GVnlOSm0rWDBzdDhzbXdzYVlHaTJQVHFRbjJIVWdacUp4Q2NycEpTMWpxZHdyK21RNEN3PT1cIiJ9LCJvY3RldHMiOiJ7XCJkZW5vbWluYXRpb25cIjp7XCJhbW91bnRcIjpcIjI1XCIsXCJjdXJyZW5jeVwiOlwiQkFUXCJ9LFwiZGVzdGluYXRpb25cIjpcImZvb0BiYXIuY29tXCJ9In0="
txnReq, err := wallet.decodeTransaction(txnB64)
if err != nil {
t.Error(err)
}
var expected transactionRequest
expected.Denomination.Amount, err = decimal.NewFromString("25.0")
if err != nil {
t.Error(err)
}
{
tmp := altcurrency.BAT
expected.Denomination.Currency = &tmp
}
if txnReq.Destination != expected.Destination {
t.Error("Decoded transaction does not match expected value")
}
if !txnReq.Denomination.Amount.Equal(expected.Denomination.Amount) {
t.Error("Decoded transaction does not match expected value")
}
if *txnReq.Denomination.Currency != *expected.Denomination.Currency {
t.Error("Decoded transaction does not match expected value")
}
}
func TestReMarshall(t *testing.T) {
// FIXME
//{"denomination":{"amount":"50.000000000000000000","currency":"BAT"},"destination":"99f7ee1c-bce7-4b11-bb91-825412f4764b"}}
}
func TestVerifyTransaction(t *testing.T) {
// FIXME test malicious signature cases
}
func TestTransactions(t *testing.T) {
ctx := context.Background()
if os.Getenv("UPHOLD_ACCESS_TOKEN") == "" {
t.Skip("skipping test; UPHOLD_ACCESS_TOKEN not set")
}
if os.Getenv("DONOR_WALLET_PUBLIC_KEY") == "" {
t.Skip("skipping test; DONOR_WALLET_PUBLIC_KEY not set")
}
if os.Getenv("DONOR_WALLET_PRIVATE_KEY") == "" {
t.Skip("skipping test; DONOR_WALLET_PRIVATE_KEY not set")
}
if os.Getenv("DONOR_WALLET_CARD_ID") == "" {
t.Skip("skipping test; DONOR_WALLET_CARD_ID not set")
}
var donorInfo wallet.Info
donorInfo.Provider = "uphold"
donorInfo.ProviderID = os.Getenv("DONOR_WALLET_CARD_ID")
{
tmp := altcurrency.BAT
donorInfo.AltCurrency = &tmp
}
donorWalletPublicKeyHex := os.Getenv("DONOR_WALLET_PUBLIC_KEY")
donorWalletPrivateKeyHex := os.Getenv("DONOR_WALLET_PRIVATE_KEY")
var donorPublicKey httpsignature.Ed25519PubKey
var donorPrivateKey ed25519.PrivateKey
donorPublicKey, err := hex.DecodeString(donorWalletPublicKeyHex)
if err != nil {
t.Fatal(err)
}
donorPrivateKey, err = hex.DecodeString(donorWalletPrivateKeyHex)
if err != nil {
t.Fatal(err)
}
donorWallet := &Wallet{Info: donorInfo, PrivKey: donorPrivateKey, PubKey: donorPublicKey}
var info wallet.Info
info.Provider = "uphold"
info.ProviderID = ""
{
tmp := altcurrency.BAT
info.AltCurrency = &tmp
}
publicKey, privateKey, err := httpsignature.GenerateEd25519Key(nil)
if err != nil {
t.Fatal(err)
}
destWallet := &Wallet{Info: info, PrivKey: privateKey, PubKey: publicKey}
err = destWallet.Register(ctx, "bat-go test transaction card")
if err != nil {
t.Error(err)
}
value, err := decimal.NewFromString("10")
if err != nil {
t.Error(err)
}
tx, err := donorWallet.PrepareTransaction(
altcurrency.BAT,
altcurrency.BAT.ToProbi(value),
destWallet.Info.ProviderID,
"bat-go:uphold.TestTransactions",
"",
nil,
)
if err != nil {
t.Error(err)
}
submitInfo, err := donorWallet.SubmitTransaction(ctx, tx, false)
if err != nil {
t.Error(err)
}
balance, err := destWallet.GetBalance(ctx, true)
if err != nil {
t.Error(err)
}
if balance.TotalProbi.GreaterThan(decimal.Zero) {
t.Error("Submit without confirm should not result in a balance.")
}
// Submitted but unconfirmed transactions cannot be retrieved via GetTransaction
_, err = donorWallet.GetTransaction(ctx, submitInfo.ID)
if err == nil {
t.Error("Expected error retrieving unconfirmed transaction")
}
if !errorutils.IsErrNotFound(err) {
t.Error("Expected \"missing\" transaction as error cause")
}
commitInfo, err := donorWallet.ConfirmTransaction(ctx, submitInfo.ID)
if err != nil {
t.Error(err)
}
if commitInfo.ID != submitInfo.ID {
t.Error("Transaction id mismatch!")
}
if commitInfo.Destination != destWallet.ProviderID {
t.Error("Transaction destination mismatch!")
}
if !commitInfo.Probi.Equals(submitInfo.Probi) {
t.Error("Transaction probi mismatch!")
}
getInfo, err := donorWallet.GetTransaction(ctx, submitInfo.ID)
if err != nil {
t.Error(err)
}
if getInfo.ID != submitInfo.ID {
t.Error("Transaction id mismatch!")
}
if getInfo.Destination != destWallet.ProviderID {
t.Error("Transaction destination mismatch!")
}
if !getInfo.Probi.Equals(submitInfo.Probi) {
t.Error("Transaction probi mismatch!")
}
balance, err = destWallet.GetBalance(ctx, true)
if err != nil {
t.Error(err)
}
if balance.TotalProbi.Equals(decimal.Zero) {
t.Error("Submit with confirm should result in a balance.")
}
// wait for funds to be available
<-time.After(2 * time.Second)
txInfo, err := destWallet.Transfer(ctx, altcurrency.BAT, submitInfo.Probi, donorWallet.ProviderID)
if err != nil {
t.Log(err)
t.Log(errors.Unwrap(err))
t.FailNow()
}
if txInfo == nil {
t.Fatalf("no tx information from transfer!")
}
balance, err = destWallet.GetBalance(ctx, true)
if err != nil {
t.Error(err)
}
if !balance.TotalProbi.Equals(decimal.Zero) {
t.Error("Transfer should move balance back to donorWallet.")
}
if !submitInfo.Probi.Equals(txInfo.Probi) {
t.Error("Transaction amount should match")
}
if len(txInfo.ID) == 0 {
t.Error("Transaction should have identifier")
}
}
func TestFingerprintCheck(t *testing.T) {
var proxy func(*http.Request) (*url.URL, error)
wrongFingerprint := "IYSLsapSKlkofKfi6M2hmS4gzXbQKGIX/DHBWIgstw3="
w := requireDonorWallet(t)
req, err := w.signRegistration("randomlabel")
if err != nil {
t.Error(err)
}
// Check fingerprint error case
client := &http.Client{
Timeout: time.Second * 60,
// remove middleware calls
Transport: &http.Transport{
Proxy: proxy,
TLSClientConfig: pindialer.GetTLSConfig(wrongFingerprint),
},
}
_, err = client.Do(req)
assert.ErrorContains(t, err, "the server certificate was not valid")
// Check the fingerprint success case.
tlsConfig := pindialer.GetTLSConfig(upholdCertFingerprint)
// VerifyConnection callback is only called after
// tlsConfig.VerifyPeerCertificate returns success.
verifyConnectionCalled := false
if tlsConfig.VerifyConnection != nil {
t.Fatalf("tlsConfig.VerifyConnection must be unset")
}
tlsConfig.VerifyConnection = func(tls.ConnectionState) error {
if verifyConnectionCalled {
t.Fatalf("Unexpected extra call to VerifyConnection")
}
verifyConnectionCalled = true
return nil
}
client = &http.Client{
Timeout: time.Second * 60,
Transport: &http.Transport{
Proxy: proxy,
TLSClientConfig: tlsConfig,
},
}
_, _ = client.Do(req)
assert.Equal(t, true, verifyConnectionCalled)
}
func requireDonorWallet(t *testing.T) *Wallet {
if os.Getenv("UPHOLD_ACCESS_TOKEN") == "" {
t.Skip("skipping test; UPHOLD_ACCESS_TOKEN not set")
}
var info wallet.Info
info.Provider = "uphold"
info.ProviderID = ""
{
tmp := altcurrency.BAT
info.AltCurrency = &tmp
}
publicKey, privateKey, err := httpsignature.GenerateEd25519Key(nil)
if err != nil {
t.Fatal(err)
}
return &Wallet{Info: info, PrivKey: privateKey, PubKey: publicKey}
}