/
Wallets.hs
805 lines (733 loc) · 34.4 KB
/
Wallets.hs
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE OverloadedLabels #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
module Test.Integration.Scenario.CLI.Shelley.Wallets
( spec
, walletNames
, walletNamesInvalid
) where
import Prelude
import Cardano.CLI
( Port )
import Cardano.Wallet.Api.Types
( ApiTransaction
, ApiUtxoStatistics
, ApiWallet
, DecodeAddress (..)
, DecodeStakeAddress (..)
, EncodeAddress (..)
, getApiT
)
import Cardano.Wallet.Primitive.AddressDerivation
( PassphraseMaxLength (..), PassphraseMinLength (..) )
import Cardano.Wallet.Primitive.AddressDiscovery.Sequential
( AddressPoolGap (..) )
import Cardano.Wallet.Primitive.SyncProgress
( SyncProgress (..) )
import Cardano.Wallet.Primitive.Types
( walletNameMaxLength, walletNameMinLength )
import Control.Monad
( forM_ )
import Data.Generics.Internal.VL.Lens
( view, (^.) )
import Data.Generics.Product.Typed
( typed )
import Data.Proxy
( Proxy (..) )
import Data.Quantity
( Quantity (..) )
import Data.Text
( Text )
import Data.Word
( Word32, Word64 )
import System.Command
( Exit (..), Stderr (..), Stdout (..) )
import System.Exit
( ExitCode (..) )
import Test.Hspec
( SpecWith, describe )
import Test.Hspec.Expectations.Lifted
( shouldBe, shouldContain, shouldNotBe, shouldNotContain )
import Test.Hspec.Extra
( it )
import Test.Integration.Framework.DSL
( Context (..)
, KnownCommand
, cardanoWalletCLI
, createWalletViaCLI
, deleteWalletViaCLI
, emptyRandomWallet
, emptyWallet
, emptyWalletWith
, eventually
, expectCliField
, expectCliListField
, expectValidJSON
, expectWalletUTxO
, fixtureWallet
, generateMnemonicsViaCLI
, getWalletUtxoStatisticsViaCLI
, getWalletViaCLI
, listAddresses
, listWalletsViaCLI
, minUTxOValue
, notDelegating
, postTransactionViaCLI
, updateWalletNameViaCLI
, updateWalletPassphraseViaCLI
, verify
, walletId
)
import Test.Integration.Framework.TestData
( arabicWalletName
, cmdOk
, errMsg400WalletIdEncoding
, errMsg403WrongPass
, errMsg404NoWallet
, falseWalletIds
, russianWalletName
, wildcardsWalletName
)
import qualified Data.Text as T
spec :: forall n t.
( KnownCommand t
, DecodeAddress n
, DecodeStakeAddress n
, EncodeAddress n
) => SpecWith (Context t)
spec = describe "SHELLEY_CLI_WALLETS" $ do
it "BYRON_GET_03 - Shelley CLI does not show Byron wallet" $ \ctx -> do
wid <- emptyRandomWallet' ctx
(Exit c, Stdout out, Stderr err) <- getWalletViaCLI @t ctx wid
out `shouldBe` ""
c `shouldBe` ExitFailure 1
err `shouldContain` errMsg404NoWallet (T.pack wid)
it "BYRON_LIST_03 - Shelley CLI does not list Byron wallet" $ \ctx -> do
_ <- emptyRandomWallet' ctx
wid <- emptyWallet' ctx
(Exit c, Stdout out, Stderr err) <- listWalletsViaCLI @t ctx
c `shouldBe` ExitSuccess
err `shouldBe` cmdOk
j <- expectValidJSON (Proxy @[ApiWallet]) out
length j `shouldBe` 1
expectCliListField 0 walletId (`shouldBe` T.pack wid) j
it "BYRON_DELETE_03 - Shelley CLI does not delete Byron wallet" $ \ctx -> do
wid <- emptyRandomWallet' ctx
(Exit c, Stdout out, Stderr err) <- deleteWalletViaCLI @t ctx wid
out `shouldBe` ""
c `shouldBe` ExitFailure 1
err `shouldContain` errMsg404NoWallet (T.pack wid)
it "BYRON_WALLETS_UTXO -\
\ Cannot show Byron wal utxo with shelley CLI" $ \ctx -> do
wid <- emptyRandomWallet' ctx
(Exit c, Stdout o, Stderr e) <- getWalletUtxoStatisticsViaCLI @t ctx wid
c `shouldBe` ExitFailure 1
e `shouldContain` errMsg404NoWallet (T.pack wid)
o `shouldBe` mempty
it "BYRON_WALLETS_UPDATE_PASS -\
\ Cannot update Byron wal with shelley CLI" $ \ctx -> do
wid <- emptyRandomWallet' ctx
let port = T.pack $ show $ ctx ^. typed @(Port "wallet")
let args = T.unpack <$>
[ "wallet", "update", "passphrase"
, "--port", port, T.pack wid ]
(Exit c, Stdout out, Stderr err) <- cardanoWalletCLI @t args
out `shouldBe` ""
c `shouldBe` ExitFailure 1
err `shouldContain` errMsg404NoWallet (T.pack wid)
it "BYRON_WALLETS_UPDATE -\
\ Cannot update name Byron wal with shelley CLI" $ \ctx -> do
wid <- emptyRandomWallet' ctx
let port = T.pack $ show $ ctx ^. typed @(Port "wallet")
let args = T.unpack <$>
[ "wallet", "update", "name"
, "--port", port, T.pack wid, "name" ]
(Exit c, Stdout out, Stderr err) <- cardanoWalletCLI @t args
out `shouldBe` ""
c `shouldBe` ExitFailure 1
err `shouldContain` errMsg404NoWallet (T.pack wid)
it "WALLETS_CREATE_01,08 - Can create a wallet" $ \ctx -> do
Stdout m <- generateMnemonicsViaCLI @t []
(c, out, err) <- createWalletViaCLI @t ctx ["n"] m "\n" "secure-passphrase"
c `shouldBe` ExitSuccess
T.unpack err `shouldContain` cmdOk
j <- expectValidJSON (Proxy @ApiWallet) out
verify j
[ expectCliField (#name . #getApiT . #getWalletName) (`shouldBe` "n")
, expectCliField
(#addressPoolGap . #getApiT . #getAddressPoolGap) (`shouldBe` 20)
, expectCliField (#balance . #getApiT . #available) (`shouldBe` Quantity 0)
, expectCliField (#balance . #getApiT . #total) (`shouldBe` Quantity 0)
, expectCliField (#balance . #getApiT . #reward) (`shouldBe` Quantity 0)
, expectCliField #delegation (`shouldBe` notDelegating [])
, expectCliField #passphrase (`shouldNotBe` Nothing)
]
eventually "Wallet state = Ready" $ do
Stdout og <- getWalletViaCLI @t ctx $ T.unpack (j ^. walletId)
jg <- expectValidJSON (Proxy @ApiWallet) og
expectCliField (#state . #getApiT) (`shouldBe` Ready) jg
it "WALLETS_CREATE_02 - Restored wallet preserves funds" $ \ctx -> do
wSrc <- fixtureWallet ctx
-- create a wallet
Stdout m <- generateMnemonicsViaCLI @t []
(c1, o1, e1) <- createWalletViaCLI @t ctx ["n"] m "\n" "secure-passphrase"
c1 `shouldBe` ExitSuccess
T.unpack e1 `shouldContain` cmdOk
wDest <- expectValidJSON (Proxy @ApiWallet) o1
verify wDest
[ expectCliField
(#balance . #getApiT . #available) (`shouldBe` Quantity 0)
, expectCliField
(#balance . #getApiT . #total) (`shouldBe` Quantity 0)
]
--send transaction to the wallet
let amount = minUTxOValue
addrs:_ <- listAddresses @n ctx wDest
let addr = encodeAddress @n (getApiT $ fst $ addrs ^. #id)
let args = T.unpack <$>
[ wSrc ^. walletId
, "--payment", T.pack (show amount) <> "@" <> addr
]
(cp, op, ep) <- postTransactionViaCLI @t ctx "cardano-wallet" args
T.unpack ep `shouldContain` cmdOk
_ <- expectValidJSON (Proxy @(ApiTransaction n)) op
cp `shouldBe` ExitSuccess
eventually "Wallet balance is as expected" $ do
Stdout og <- getWalletViaCLI @t ctx $ T.unpack (wDest ^. walletId)
jg <- expectValidJSON (Proxy @ApiWallet) og
expectCliField (#balance . #getApiT . #available)
(`shouldBe` Quantity amount) jg
expectCliField (#balance . #getApiT . #total)
(`shouldBe` Quantity amount) jg
-- delete wallet
Exit cd <- deleteWalletViaCLI @t ctx $ T.unpack (wDest ^. walletId)
cd `shouldBe` ExitSuccess
-- restore wallet
(c2, o2, e2) <- createWalletViaCLI @t ctx ["n"] m "\n" "secure-passphraseX"
c2 `shouldBe` ExitSuccess
T.unpack e2 `shouldContain` cmdOk
wRestored <- expectValidJSON (Proxy @ApiWallet) o2
expectCliField walletId (`shouldBe` wDest ^. walletId) wRestored
eventually "Wallet is fully restored" $ do
Stdout og2 <- getWalletViaCLI @t ctx $ T.unpack (wDest ^. walletId)
jg2 <- expectValidJSON (Proxy @ApiWallet) og2
expectCliField (#state . #getApiT) (`shouldBe` Ready) jg2
-- make sure funds are there
Stdout o3 <- getWalletViaCLI @t ctx $ T.unpack (wRestored ^. walletId)
justRestored <- expectValidJSON (Proxy @ApiWallet) o3
verify justRestored
[ expectCliField
(#balance . #getApiT . #available) (`shouldBe` Quantity amount)
, expectCliField
(#balance . #getApiT . #total) (`shouldBe` Quantity amount)
, expectCliField walletId (`shouldBe` wDest ^. walletId)
]
it "WALLETS_CREATE_03 - Cannot create wallet that exists" $ \ctx -> do
Stdout m1 <- generateMnemonicsViaCLI @t ["--size", "24"]
Stdout m2 <- generateMnemonicsViaCLI @t ["--size", "12"]
(c1, o1, e1) <- createWalletViaCLI @t ctx ["n1"] m1 m2 "secure-passphrase"
c1 `shouldBe` ExitSuccess
T.unpack e1 `shouldContain` cmdOk
j <- expectValidJSON (Proxy @ApiWallet) o1
expectCliField (#name . #getApiT . #getWalletName) (`shouldBe` "n1") j
(c2, o2, e2) <- createWalletViaCLI @t ctx ["n2"] m1 m2 "Xsecure-passphraseX"
c2 `shouldBe` ExitFailure 1
o2 `shouldBe` ""
T.unpack e2 `shouldContain` "This operation would yield a wallet with \
\the following id: " ++ T.unpack (j ^. walletId) ++ " \
\However, I already know of a wallet with this id."
describe "WALLETS_CREATE_04 - Wallet names" $ do
forM_ walletNames $ \(title, n) -> it title $ \ctx -> do
Stdout m <- generateMnemonicsViaCLI @t ["--size", "18"]
(c, o, e) <- createWalletViaCLI @t ctx [n] m "\n" "secure-passphrase"
c `shouldBe` ExitSuccess
T.unpack e `shouldContain` cmdOk
j <- expectValidJSON (Proxy @ApiWallet) o
expectCliField
(#name . #getApiT . #getWalletName) (`shouldBe` T.pack n) j
describe "WALLETS_CREATE_04 - Wallet names invalid" $ do
forM_ walletNamesInvalid $ \(name, expects) -> it expects $ \ctx -> do
Stdout m <- generateMnemonicsViaCLI @t ["--size", "18"]
(c, o, e) <- createWalletViaCLI @t ctx [name] m "\n" "secure-passphrase"
c `shouldBe` ExitFailure 1
T.unpack e `shouldContain` expects
o `shouldBe` ""
describe "WALLETS_CREATE_05 - Can create wallet with different mnemonic sizes" $ do
forM_ ["15", "18", "21", "24"] $ \(size) -> it size $ \ctx -> do
let name = "Wallet created via CLI "
Stdout mnemonics <- generateMnemonicsViaCLI @t ["--size", size]
let pwd = "Secure passphrase"
(c, out, err) <-
createWalletViaCLI @t ctx [name] mnemonics "\n" pwd
c `shouldBe` ExitSuccess
T.unpack err `shouldContain` cmdOk
j <- expectValidJSON (Proxy @ApiWallet) out
expectCliField
(#name . #getApiT . #getWalletName) (`shouldBe` T.pack name) j
describe "WALLETS_CREATE_05 - Can't create wallet with wrong size of mnemonic" $ do
forM_ ["9", "12"] $ \(size) -> it size $ \ctx -> do
let name = "Wallet created via CLI"
Stdout m1 <- generateMnemonicsViaCLI @t ["--size", size]
Stdout m2 <- generateMnemonicsViaCLI @t ["--size", size]
let pwd = "Secure passphrase"
(c, out, err) <-
createWalletViaCLI @t ctx [name] m1 m2 pwd
c `shouldBe` (ExitFailure 1)
out `shouldBe` ""
T.unpack err `shouldContain` "Invalid number of words: 15, 18, 21\
\ or 24 words are expected."
(Stdout o, Stderr e) <- listWalletsViaCLI @t ctx
o `shouldBe` "[]\n"
e `shouldBe` cmdOk
describe "WALLETS_CREATE_06 - Can create wallet with different mnemonic snd factor sizes" $ do
forM_ ["9", "12"] $ \(size) -> it size $ \ctx -> do
let name = "Wallet created via CLI"
Stdout m1 <- generateMnemonicsViaCLI @t []
Stdout m2 <- generateMnemonicsViaCLI @t ["--size", size]
let pwd = "Secure passphrase"
(c, out, err) <- createWalletViaCLI @t ctx [name] m1 m2 pwd
c `shouldBe` ExitSuccess
T.unpack err `shouldContain` cmdOk
j <- expectValidJSON (Proxy @ApiWallet) out
expectCliField
(#name . #getApiT . #getWalletName) (`shouldBe` T.pack name) j
describe "WALLETS_CREATE_06 - Can't create wallet with wrong size of mnemonic snd factor" $ do
forM_ ["15", "18", "21", "24"] $ \(size) -> it size $ \ctx -> do
let name = "Wallet created via CLI"
Stdout m1 <- generateMnemonicsViaCLI @t ["--size", size]
Stdout m2 <- generateMnemonicsViaCLI @t ["--size", size]
let pwd = "Secure passphrase"
(c, out, err) <- createWalletViaCLI @t ctx [name] m1 m2 pwd
c `shouldBe` (ExitFailure 1)
out `shouldBe` ""
T.unpack err `shouldContain` "Invalid number of words: 9 or 12\
\ words are expected."
(Stdout o, Stderr e) <- listWalletsViaCLI @t ctx
o `shouldBe` "[]\n"
e `shouldBe` cmdOk
describe "WALLETS_CREATE_07 - Passphrase is valid" $ do
let proxy_ = Proxy @"raw"
let passphraseMax = replicate (passphraseMaxLength proxy_) 'ą'
let passBelowMax = replicate (passphraseMaxLength proxy_ - 1) 'ć'
let passphraseMin = replicate (passphraseMinLength proxy_) 'ń'
let passAboveMin = replicate (passphraseMinLength proxy_ + 1) 'ę'
let matrix =
[ ( "Pass min", passphraseMin )
, ( "Pass max", passphraseMax )
, ( "Pass max - 1", passBelowMax )
, ( "Pass min + 1", passAboveMin )
, ( "Russian", "АаБбВвГгДдЕеЁёЖжЗз")
, ( "Polish", "aąbcćdeęfghijklłmnoóp" )
, ( "Kanji", "亜哀挨愛曖悪握圧扱宛嵐")
]
forM_ matrix $ \(title, pass) -> it title $ \ctx -> do
Stdout m <- generateMnemonicsViaCLI @t []
(c, o, e) <- createWalletViaCLI @t ctx ["Wallet name"] m "\n" pass
c `shouldBe` ExitSuccess
T.unpack e `shouldContain` cmdOk
j <- expectValidJSON (Proxy @ApiWallet) o
expectCliField #passphrase (`shouldNotBe` Nothing) j
describe "WALLETS_CREATE_07 - When passphrase is invalid" $ do
let proxy_ = Proxy @"raw"
let passAboveMax = replicate (passphraseMaxLength proxy_ + 1) 'ą'
let passBelowMin = replicate (passphraseMinLength proxy_ - 1) 'ń'
let passMinWarn = "passphrase is too short: expected at \
\least " ++ show (passphraseMinLength proxy_) ++ " characters"
let passMaxWarn = "passphrase is too long: expected at \
\most " ++ show (passphraseMaxLength proxy_) ++ " characters"
let matrix =
[ ( "Pass below min length", passBelowMin, passMinWarn )
, ( "Pass above max length", passAboveMax, passMaxWarn )
]
forM_ matrix $ \(title, pass, warn) -> it title $ \ctx -> do
Stdout m <- generateMnemonicsViaCLI @t []
(c, o, e) <- createWalletViaCLI @t ctx ["Wallet name"] m "\n" pass
c `shouldBe` ExitFailure 1
o `shouldBe` ""
T.unpack e `shouldContain` warn
describe "WALLETS_CREATE_08 - --address-pool-gap values" $ do
let expectsOk c o e gap = do
c `shouldBe` ExitSuccess
T.unpack e `shouldContain` cmdOk
j <- expectValidJSON (Proxy @ApiWallet) o
expectCliField
(#addressPoolGap . #getApiT . #getAddressPoolGap)
(`shouldBe` (read gap :: Word32))
j
let addrPoolMin = fromIntegral @_ @Int $ getAddressPoolGap minBound
let addrPoolMax = fromIntegral @_ @Int $ getAddressPoolGap maxBound
let expectsErr c o e gap = do
c `shouldBe` ExitFailure 1
o `shouldNotContain` gap
T.unpack e `shouldContain`
"An address pool gap must be a natural number between "
++ show addrPoolMin ++ " and "
++ show addrPoolMax ++ "."
let matrix =
[ ( "Gap min", show addrPoolMin, expectsOk )
, ( "Gap min + 1", show (addrPoolMin + 1), expectsOk )
, ( "Gap max + 1 -> fail", show (addrPoolMax + 1), expectsErr )
, ( "Gap min - 1 -> fail", show (addrPoolMin - 1), expectsErr )
, ( "-1000 -> fail", "-1000", expectsErr )
, ( "0 -> fail", "0", expectsErr )
, ( "10.5 -> fail", "10.5", expectsErr )
, ( "arbitraty string -> fail", "string", expectsErr )
]
forM_ matrix $ \(title, gap, expects) -> it title $ \ctx -> do
Stdout m <- generateMnemonicsViaCLI @t []
(c, o, e) <- createWalletViaCLI @t ctx ["n", "--address-pool-gap", gap]
m "\n" "secure-passphraze"
expects c o e gap
it "WALLETS_GET_01 - Can get a wallet" $ \ctx -> do
walId <- emptyWallet' ctx
(Exit c, Stdout out, Stderr err) <- getWalletViaCLI @t ctx walId
c `shouldBe` ExitSuccess
err `shouldBe` cmdOk
j <- expectValidJSON (Proxy @ApiWallet) out
verify j
[ expectCliField
(#name . #getApiT . #getWalletName) (`shouldBe` "Empty Wallet")
, expectCliField
(#addressPoolGap . #getApiT . #getAddressPoolGap) (`shouldBe` 20)
, expectCliField
(#balance . #getApiT . #available) (`shouldBe` Quantity 0)
, expectCliField
(#balance . #getApiT . #total) (`shouldBe` Quantity 0)
, expectCliField
(#balance . #getApiT . #reward) (`shouldBe` Quantity 0)
, expectCliField #delegation (`shouldBe` notDelegating [])
, expectCliField #passphrase (`shouldNotBe` Nothing)
]
eventually "Wallet state = Ready" $ do
Stdout og <- getWalletViaCLI @t ctx walId
jg <- expectValidJSON (Proxy @ApiWallet) og
expectCliField (#state . #getApiT) (`shouldBe` Ready) jg
describe "WALLETS_GET_03,04 - Cannot get wallets with false ids" $ do
forM_ falseWalletIds $ \(title, walId) -> it title $ \ctx -> do
(Exit c, Stdout out, Stderr err) <- getWalletViaCLI @t ctx walId
out `shouldBe` ""
c `shouldBe` ExitFailure 1
if (title == "40 chars hex") then
err `shouldContain` errMsg404NoWallet (T.pack $ replicate 40 '1')
else
err `shouldContain` errMsg400WalletIdEncoding
it "WALLETS_LIST_01 - Can list wallets" $ \ctx -> do
let name = "Wallet to be listed"
w1 <- emptyWalletWith' ctx (name, "secure-passphrase", 21)
_ <- emptyWallet' ctx
(Exit c, Stdout out, Stderr err) <- listWalletsViaCLI @t ctx
c `shouldBe` ExitSuccess
err `shouldBe` cmdOk
j <- expectValidJSON (Proxy @[ApiWallet]) out
length j `shouldBe` 2
verify j
[ expectCliListField 0
(#name . #getApiT . #getWalletName) (`shouldBe` name)
, expectCliListField 0
(#addressPoolGap . #getApiT . #getAddressPoolGap) (`shouldBe` 21)
, expectCliListField 0
(#balance . #getApiT . #available) (`shouldBe` Quantity 0)
, expectCliListField 0
(#balance . #getApiT . #total) (`shouldBe` Quantity 0)
, expectCliListField 0
(#balance . #getApiT . #reward) (`shouldBe` Quantity 0)
, expectCliListField 0 #delegation (`shouldBe` notDelegating [])
, expectCliListField 0 walletId (`shouldBe` T.pack w1)
]
it "WALLETS_LIST_01 - Wallets are listed from oldest to newest" $ \ctx -> do
w1 <- emptyWalletWith' ctx ("1", "secure-passphrase", 20)
w2 <- emptyWalletWith' ctx ("2", "secure-passphrase", 20)
w3 <- emptyWalletWith' ctx ("3", "secure-passphrase", 20)
(Exit c, Stdout out, Stderr err) <- listWalletsViaCLI @t ctx
c `shouldBe` ExitSuccess
err `shouldBe` cmdOk
j <- expectValidJSON (Proxy @[ApiWallet]) out
length j `shouldBe` 3
verify j
[ expectCliListField 0 walletId (`shouldBe` T.pack w1)
, expectCliListField 1 walletId (`shouldBe` T.pack w2)
, expectCliListField 2 walletId (`shouldBe` T.pack w3)
]
describe "WALLETS_UPDATE_01,02 - Can update wallet name" $ do
forM_ walletNames $ \(title, n) -> it title $ \ctx -> do
wid <- emptyWallet' ctx
let args = [wid, n]
(Exit c, Stdout out, Stderr err) <-
updateWalletNameViaCLI @t ctx args
c `shouldBe` ExitSuccess
err `shouldBe` cmdOk
j <- expectValidJSON (Proxy @ApiWallet) out
expectCliField
(#name . #getApiT . #getWalletName) (`shouldBe` T.pack n) j
it "WALLETS_UPDATE_PASS_01 - Can update passphrase normally"
$ \ctx -> do
let name = "name"
let ppOld = "old secure passphrase"
let ppNew = "new secure passphrase"
let addrPoolMin = fromIntegral @_ @Int $ getAddressPoolGap minBound
w <- emptyWalletWith ctx (name, T.pack ppOld, addrPoolMin)
let initPassUpdateTime = w ^. #passphrase
let wid = T.unpack $ w ^. walletId
--update pass
(exitCode, out, err) <-
updateWalletPassphraseViaCLI @t ctx wid ppOld ppNew ppNew
out `shouldBe` "\n"
T.unpack err `shouldContain` cmdOk
exitCode `shouldBe` ExitSuccess
--verify passphraseLastUpdate was updated
Stdout o <- getWalletViaCLI @t ctx wid
j <- expectValidJSON (Proxy @ApiWallet) o
expectCliField #passphrase (`shouldNotBe` initPassUpdateTime) j
describe "WALLETS_UPDATE_PASS_02 - New passphrase values" $ do
let minLength = passphraseMinLength (Proxy @"raw")
let maxLength = passphraseMaxLength (Proxy @"raw")
let matrix =
[ ( show minLength ++ " char long"
, replicate minLength 'ź'
, expect (ExitSuccess, "\n", cmdOk)
)
, ( show (minLength - 1) ++ " char long"
, replicate (minLength - 1) 'ź'
, expect (ExitFailure 1, mempty, "passphrase is too short")
)
, ( show maxLength ++ " char long"
, replicate minLength 'ź'
, expect (ExitSuccess, "\n", cmdOk)
)
, ( show (maxLength + 1) ++ " char long"
, replicate (maxLength + 1) 'ź'
, expect (ExitFailure 1, mempty, "passphrase is too long")
)
, ( "Empty passphrase"
, ""
, expect (ExitFailure 1, mempty, "passphrase is too short")
)
, ( "Russian passphrase", T.unpack russianWalletName
, expect (ExitSuccess, "\n", cmdOk)
)
, ( "Arabic passphrase", T.unpack arabicWalletName
, expect (ExitSuccess, "\n", cmdOk)
)
, ( "Wildcards passphrase", T.unpack wildcardsWalletName
, expect (ExitSuccess, "\n", cmdOk)
)
]
forM_ matrix $ \(title, ppNew, expectations) -> it title $ \ctx -> do
let name = "name"
let ppOld = "old secure passphrase"
let addrPoolMin = fromIntegral @_ @Int $ getAddressPoolGap minBound
wid <- emptyWalletWith' ctx (name, T.pack ppOld, addrPoolMin)
(exitCode, out, err) <-
updateWalletPassphraseViaCLI @t ctx wid ppOld ppNew ppNew
expectations (exitCode, out, err)
it "WALLETS_UPDATE_PASS_02 - \
\Cannot update passphrase if new passphrase is not confirmed correctly"
$ \ctx -> do
let name = "name"
let ppOld = "old secure passphrase"
let ppNew1 = "new secure passphrase 1"
let ppNew2 = "new secure passphrase 2"
let addrPoolMin = fromIntegral @_ @Int $ getAddressPoolGap minBound
wid <- emptyWalletWith' ctx (name, T.pack ppOld, addrPoolMin)
(exitCode, out, err) <-
updateWalletPassphraseViaCLI @t ctx wid ppOld ppNew1 ppNew2
out `shouldBe` mempty
T.unpack err `shouldContain` "Passphrases don't match"
exitCode `shouldBe` ExitFailure 1
describe "WALLETS_UPDATE_PASS_03 - Old passphrase values" $ do
let minLength = passphraseMinLength (Proxy @"raw")
let maxLength = passphraseMaxLength (Proxy @"raw")
let matrix =
[ ( show (minLength - 1) ++ " char long"
, replicate (minLength - 1) 'ź'
, expect (ExitFailure 1, mempty, "passphrase is too short")
)
, ( show (maxLength + 1) ++ " char long"
, replicate (maxLength + 1) 'ź'
, expect (ExitFailure 1, mempty, "passphrase is too long")
)
, ( "Empty passphrase"
, ""
, expect (ExitFailure 1, mempty, "passphrase is too short")
)
, ( "Incorrect old passphrase"
, "wrong secure passphrase"
, expect (ExitFailure 1, mempty, errMsg403WrongPass)
)
]
forM_ matrix $ \(title, ppOldWrong, expectations) -> it title $ \ctx -> do
let name = "name"
let ppOldRight = "right secure passphrase"
let ppNew = "new secure passphrase"
let addrPoolMin = fromIntegral @_ @Int $ getAddressPoolGap minBound
wid <- emptyWalletWith' ctx
(name, T.pack ppOldRight, addrPoolMin)
(exitCode, out, err) <-
updateWalletPassphraseViaCLI @t ctx wid ppOldWrong ppNew ppNew
expectations (exitCode, out, err)
describe "WALLETS_UPDATE_PASS_03 - \
\Can update pass from pass that's boundary value" $ do
let minLength = passphraseMinLength (Proxy @"raw")
let maxLength = passphraseMaxLength (Proxy @"raw")
let matrix =
[ ( show minLength ++ " char long"
, replicate minLength 'ź'
, expect (ExitSuccess, "\n", cmdOk)
)
, ( show maxLength ++ " char long"
, replicate maxLength 'ź'
, expect (ExitSuccess, "\n", cmdOk)
)
]
forM_ matrix $ \(title, ppOldRight, expectations) -> it title $ \ctx -> do
let name = "name"
let ppNew = replicate maxLength 'ź'
let addrPoolMin = fromIntegral @_ @Int $ getAddressPoolGap minBound
wid <- emptyWalletWith' ctx
(name, T.pack ppOldRight, addrPoolMin)
(exitCode, out, err) <-
updateWalletPassphraseViaCLI @t ctx wid ppOldRight ppNew ppNew
expectations (exitCode, out, err)
describe "WALLETS_UPDATE_PASS_04 - Cannot update pass of wallets with false ids" $ do
forM_ falseWalletIds $ \(title, wid) -> it title $ \ctx -> do
let ppOld = "right secure passphrase"
let ppNew = "new secure passphrase"
(c, out, err) <-
updateWalletPassphraseViaCLI @t ctx wid ppOld ppNew ppNew
out `shouldBe` ""
c `shouldBe` ExitFailure 1
if (title == "40 chars hex") then
T.unpack err `shouldContain`
errMsg404NoWallet (T.pack $ replicate 40 '1')
else
T.unpack err `shouldContain` errMsg400WalletIdEncoding
describe "WALLETS_UPDATE_PASS_05,06 - \
\Transaction after updating passphrase can only be made with new pass" $ do
let oldPass = "cardano-wallet"
let newPass = "cardano-wallet2"
let expectTxOK (ec, out, err) = do
ec `shouldBe` ExitSuccess
_ <- expectValidJSON (Proxy @(ApiTransaction n)) out
T.unpack err `shouldContain` cmdOk
let matrix =
[ ("Old passphrase -> fail", oldPass
, expect (ExitFailure 1, mempty, errMsg403WrongPass)
)
, ("New passphrase -> OK", newPass
, expectTxOK
)
]
forM_ matrix $ \(title, pass, expectations) -> it title $ \ctx -> do
wSrc <- fixtureWallet ctx
wDest <- emptyWallet ctx
addr:_ <- listAddresses @n ctx wDest
let wid = T.unpack $ wSrc ^. walletId
(c, out, err) <-
updateWalletPassphraseViaCLI @t ctx wid oldPass newPass newPass
expect (ExitSuccess, "\n", cmdOk) (c, out, err)
let addrStr = encodeAddress @n (getApiT $ fst $ addr ^. #id)
let args = T.unpack <$>
[ wSrc ^. walletId
, "--payment", "1@" <> addrStr
]
(cTx, outTx, errTx) <- postTransactionViaCLI @t ctx pass args
expectations (cTx, outTx, errTx)
it "WALLETS_DELETE_01, WALLETS_LIST_02 - Can delete wallet" $ \ctx -> do
walId <- emptyWallet' ctx
(Exit c, Stdout out, Stderr err) <- deleteWalletViaCLI @t ctx walId
err `shouldBe` cmdOk
c `shouldBe` ExitSuccess
out `shouldBe` "\n"
(Stdout o, Stderr e) <- listWalletsViaCLI @t ctx
o `shouldBe` "[]\n"
e `shouldBe` cmdOk
it "WALLETS_UTXO_01 - Wallet's inactivity is reflected in utxo" $ \ctx -> do
wid <- emptyWallet' ctx
(Exit c, Stdout o, Stderr e) <- getWalletUtxoStatisticsViaCLI @t ctx wid
c `shouldBe` ExitSuccess
e `shouldBe` cmdOk
utxoStats <- expectValidJSON (Proxy @ApiUtxoStatistics) o
expectWalletUTxO [] (Right utxoStats)
it "WALLETS_UTXO_02 - Utxo statistics works properly" $ \ctx -> do
wSrc <- fixtureWallet ctx
wDest <- emptyWallet ctx
--send transactions to the wallet
let coins = [13, 43, 66, 101, 1339] :: [Word64]
addrs:_ <- listAddresses @n ctx wDest
let addr = encodeAddress @n (getApiT $ fst $ addrs ^. #id)
let payments = flip map coins $ \c ->
["--payment", show c <> "@" <> T.unpack addr ]
let args = T.unpack (wSrc ^. walletId) : mconcat payments
(cp, op, ep) <- postTransactionViaCLI @t ctx "cardano-wallet" args
T.unpack ep `shouldContain` cmdOk
_ <- expectValidJSON (Proxy @(ApiTransaction n)) op
cp `shouldBe` ExitSuccess
eventually "Wallet balance is as expected" $ do
Stdout og <- getWalletViaCLI @t ctx $ T.unpack (wDest ^. walletId)
jg <- expectValidJSON (Proxy @ApiWallet) og
expectCliField (#balance . #getApiT . #available)
(`shouldBe` Quantity (fromIntegral $ sum coins)) jg
expectCliField (#balance . #getApiT . #total)
(`shouldBe` Quantity (fromIntegral $ sum coins)) jg
--verify utxo
(Exit c, Stdout o, Stderr e) <-
getWalletUtxoStatisticsViaCLI @t ctx $ T.unpack (wDest ^. walletId)
c `shouldBe` ExitSuccess
e `shouldBe` cmdOk
utxoStats1 <- expectValidJSON (Proxy @ApiUtxoStatistics) o
expectWalletUTxO coins (Right utxoStats1)
describe "WALLETS_UTXO_03 - non-existing wallets" $ do
forM_ falseWalletIds $ \(title, walId) -> it title $ \ctx -> do
(Exit c, Stdout out, Stderr err) <- getWalletUtxoStatisticsViaCLI @t ctx walId
out `shouldBe` mempty
c `shouldBe` ExitFailure 1
if (title == "40 chars hex") then
err `shouldContain` errMsg404NoWallet (T.pack $ replicate 40 '1')
else
err `shouldContain` errMsg400WalletIdEncoding
it "WALLETS_UTXO_03 - Deleted wallet is not available for utxo" $ \ctx -> do
wid <- emptyWallet' ctx
Exit cd <- deleteWalletViaCLI @t ctx wid
cd `shouldBe` ExitSuccess
(Exit c, Stdout o, Stderr e) <- getWalletUtxoStatisticsViaCLI @t ctx wid
c `shouldBe` ExitFailure 1
e `shouldContain` errMsg404NoWallet (T.pack wid)
o `shouldBe` mempty
it "WALLETS_UTXO_03 - 'almost' valid walletId" $ \ctx -> do
wid <- emptyWallet' ctx
(Exit c, Stdout o, Stderr e)
<- getWalletUtxoStatisticsViaCLI @t ctx (wid ++ "1")
c `shouldBe` ExitFailure 1
e `shouldContain` errMsg400WalletIdEncoding
o `shouldBe` mempty
where
expect (expEc, expOut, expErr) (ec, out, err) = do
ec `shouldBe` expEc
out `shouldBe` expOut
T.unpack err `shouldContain` expErr
emptyRandomWallet' :: Context t -> IO String
emptyRandomWallet' = fmap (T.unpack . view walletId) . emptyRandomWallet
emptyWallet' :: Context t -> IO String
emptyWallet' = fmap (T.unpack . view walletId) . emptyWallet
emptyWalletWith' :: Context t -> (Text, Text, Int) -> IO String
emptyWalletWith' ctx (name, pass, pg) =
fmap (T.unpack . view walletId) (emptyWalletWith ctx (name, pass, pg))
walletNames :: [(String, String)]
walletNames =
[ ( "Name min", replicate walletNameMinLength 'ź' )
, ( "Name max", replicate walletNameMaxLength 'ą' )
, ( "Name max - 1", replicate ( walletNameMaxLength - 1 ) 'ą' )
, ( "Name min + 1", replicate ( walletNameMinLength + 1 ) 'ź' )
, ( "Single space", " " )
, ( "Russian", "АаБбВвГгДдЕеЁёЖжЗз")
, ( "Polish", "aąbcćdeęfghijklłmnoóp" )
, ( "Kanji", "亜哀挨愛曖悪握圧扱宛嵐")
]
walletNamesInvalid :: [(String, String)]
walletNamesInvalid =
[ ("", "name is too short: expected at least 1 character")
, (replicate (walletNameMaxLength + 1) 'ą', "name is too long: expected at most 255 characters")
]