-
Notifications
You must be signed in to change notification settings - Fork 211
/
CompatibilitySpec.hs
855 lines (767 loc) · 30.3 KB
/
CompatibilitySpec.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
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Cardano.Wallet.Shelley.CompatibilitySpec
( spec
) where
import Prelude
import Cardano.Address.Derivation
( XPrv, XPub )
import Cardano.Address.Script
( KeyHash
, KeyRole (..)
, Script (..)
, ScriptHash (..)
, keyHashFromBytes
, serializeScript
, toScriptHash
)
import Cardano.Crypto.Hash.Class
( digest )
import Cardano.Ledger.Crypto
( Crypto (..) )
import Cardano.Mnemonic
( ConsistentEntropy
, EntropySize
, Mnemonic
, SomeMnemonic (..)
, entropyToMnemonic
)
import Cardano.Wallet.Api.Types
( DecodeAddress (..), DecodeStakeAddress (..), EncodeStakeAddress (..) )
import Cardano.Wallet.Byron.Compatibility
( maryTokenBundleMaxSize )
import Cardano.Wallet.Primitive.AddressDerivation
( Depth (..)
, Index (getIndex)
, NetworkDiscriminant (..)
, PaymentAddress (..)
, WalletKey
, delegationAddress
, publicKey
)
import Cardano.Wallet.Primitive.AddressDerivation.Byron
( ByronKey (..) )
import Cardano.Wallet.Primitive.AddressDerivation.Shelley
( ShelleyKey (..) )
import Cardano.Wallet.Primitive.Types
( SlotId (..), TokenBundleMaxSize (..), getDecentralizationLevel )
import Cardano.Wallet.Primitive.Types.Address
( Address (..) )
import Cardano.Wallet.Primitive.Types.Coin
( Coin (..) )
import Cardano.Wallet.Primitive.Types.Hash
( Hash (..) )
import Cardano.Wallet.Primitive.Types.RewardAccount
( RewardAccount (..) )
import Cardano.Wallet.Primitive.Types.TokenBundle
( TokenBundle )
import Cardano.Wallet.Primitive.Types.TokenBundle.Gen
( genTokenBundle, genTokenBundleSmallRange, shrinkTokenBundleSmallRange )
import Cardano.Wallet.Primitive.Types.Tx.Constraints
( TokenBundleSizeAssessment (..)
, TokenBundleSizeAssessor (..)
, TxSize (..)
, txOutMaxCoin
, txOutMinCoin
)
import Cardano.Wallet.Primitive.Types.Tx.TxOut.Gen
( genTxOutTokenBundle )
import Cardano.Wallet.Shelley.Compatibility
( CardanoBlock
, StandardCrypto
, computeTokenBundleSerializedLengthBytes
, decentralizationLevelFromPParams
, fromCardanoValue
, fromTip
, inspectAddress
, interval0
, interval1
, invertUnitInterval
, toCardanoHash
, toCardanoValue
, toTip
, tokenBundleSizeAssessor
)
import Cardano.Wallet.Unsafe
( unsafeIntToWord, unsafeMkEntropy )
import Cardano.Wallet.Util
( tryInternalError )
import Codec.Binary.Bech32.TH
( humanReadablePart )
import Codec.Binary.Encoding
( fromBase16 )
import Control.Monad
( forM_ )
import Data.ByteString
( ByteString )
import Data.ByteString.Base58
( bitcoinAlphabet, encodeBase58 )
import Data.Either
( isLeft, isRight )
import Data.Function
( (&) )
import Data.Maybe
( fromMaybe )
import Data.Proxy
( Proxy (..) )
import Data.Ratio
( Ratio, (%) )
import Data.Text
( Text )
import Data.Text.Class
( toText )
import Data.Word
( Word16, Word32, Word64 )
import GHC.TypeLits
( natVal )
import Ouroboros.Network.Block
( BlockNo (..), SlotNo (..), Tip (..) )
import Test.Hspec
( Spec, describe, it, shouldBe, shouldSatisfy )
import Test.Hspec.Core.Spec
( SpecWith )
import Test.Hspec.QuickCheck
( prop )
import Test.QuickCheck
( Arbitrary (..)
, Blind (..)
, Gen
, NonNegative (..)
, Property
, Small (..)
, checkCoverage
, choose
, chooseInt
, conjoin
, counterexample
, cover
, frequency
, oneof
, property
, resize
, vector
, withMaxSuccess
, (===)
, (==>)
)
import Test.QuickCheck.Monadic
( assert, monadicIO, monitor, run )
import qualified Cardano.Api as Cardano
import qualified Cardano.Ledger.Address as SL
import qualified Cardano.Ledger.BaseTypes as SL
import qualified Cardano.Ledger.Shelley as SL
import qualified Cardano.Ledger.Shelley as SLAPI
import qualified Cardano.Ledger.Shelley.PParams as SL
import qualified Cardano.Wallet.Primitive.AddressDerivation as AddressDerivation
import qualified Cardano.Wallet.Primitive.AddressDerivation.Byron as Byron
import qualified Cardano.Wallet.Primitive.AddressDerivation.Shelley as Shelley
import qualified Cardano.Wallet.Primitive.Types as W
import qualified Cardano.Wallet.Primitive.Types.TokenBundle as TokenBundle
import qualified Codec.Binary.Bech32 as Bech32
import qualified Data.ByteString as BS
import qualified Data.Text.Encoding as T
spec :: Spec
spec = do
describe "Conversions" $ do
it "toTip' . fromTip' == id" $ property $ \gh tip -> do
let fromTip' = fromTip gh
let toTip' = toTip gh :: W.BlockHeader -> Tip (CardanoBlock StandardCrypto)
toTip' (fromTip' tip) === tip
it "unsafeIntToWord" $
property prop_unsafeIntToWord
describe "Shelley StakeAddress" $ do
prop "roundtrip / Mainnet" $ \x ->
(decodeStakeAddress @'Mainnet . encodeStakeAddress @'Mainnet) x
===
Right x
prop "roundtrip / Testnet" $ \x ->
(decodeStakeAddress @('Testnet 0) . encodeStakeAddress @('Testnet 0)) x
===
Right x
describe "Shelley Addresses" $ do
prop "(Mainnet) can be deserialised by shelley ledger spec" $ \k -> do
let Address addr = paymentAddress @'Mainnet @ShelleyKey @'CredFromKeyK k
case SL.deserialiseAddr @StandardCrypto addr of
Just _ -> property True
Nothing -> property False
prop "Shelley addresses from bech32" $ \k ->
let addr@(Address bytes) = paymentAddress @'Mainnet @ShelleyKey @'CredFromKeyK k
in decodeAddress @'Mainnet (bech32 bytes) === Right addr
& counterexample (show $ bech32 bytes)
prop "Shelley addresses with delegation from bech32" $ \k1 k2 ->
let addr@(Address bytes) = delegationAddress @'Mainnet @ShelleyKey @'CredFromKeyK k1 k2
in decodeAddress @'Mainnet (bech32 bytes) === Right addr
& counterexample (show $ bech32 bytes)
prop "Shelley addresses from bech32 - testnet" $ \k ->
let addr@(Address raw) = paymentAddress @('Testnet 0) @ShelleyKey @'CredFromKeyK k
in decodeAddress @('Testnet 0) (bech32testnet raw) === Right addr
& counterexample (show $ bech32testnet raw)
prop "Byron addresses from base58" $ \k ->
let addr@(Address bytes) = paymentAddress @'Mainnet @ByronKey @'CredFromKeyK k
in decodeAddress @'Mainnet (base58 bytes) === Right addr
& counterexample (show $ base58 bytes)
describe "decentralizationLevelFromPParams" $ do
let mkDecentralizationParam
:: SL.UnitInterval
-> SLAPI.ShelleyPParams (SL.ShelleyEra StandardCrypto)
mkDecentralizationParam i = SL.emptyPParams { SL._d = i }
let testCases :: [(Ratio Word64, Text)]
testCases =
[ (10 % 10, "0.00%")
, ( 9 % 10, "10.00%")
, ( 5 % 10, "50.00%")
, ( 1 % 10, "90.00%")
, ( 0 % 10, "100.00%")
]
forM_ testCases $ \(input, expectedOutput) -> do
let title = show input <> " -> " <> show expectedOutput
let output = input
& toRational
& unsafeBoundRational
& mkDecentralizationParam
& decentralizationLevelFromPParams
& getDecentralizationLevel
& toText
it title $ output `shouldBe` expectedOutput
describe "Cardano.Api.Value-TokenBundle conversion" $ do
it "roundtrips" $ checkCoverage $ property $ \tb ->
cover 20 (TokenBundle.getCoin tb /= Coin 0) "has ada" $
cover 2 (TokenBundle.getCoin tb == Coin 0) "has no ada" $
cover 10 (length (snd $ TokenBundle.toFlatList tb) > 3)
"has some assets" $
fromCardanoValue (toCardanoValue tb) === tb
describe "Assessing the sizes of token bundles" $ do
it "prop_assessTokenBundleSize_enlarge" $
property prop_assessTokenBundleSize_enlarge
it "prop_assessTokenBundleSize_shrink" $
property prop_assessTokenBundleSize_shrink
it "unit_assessTokenBundleSize_fixedSizeBundle_32" $
property unit_assessTokenBundleSize_fixedSizeBundle_32
it "unit_assessTokenBundleSize_fixedSizeBundle_48" $
property unit_assessTokenBundleSize_fixedSizeBundle_48
it "unit_assessTokenBundleSize_fixedSizeBundle_64" $
property unit_assessTokenBundleSize_fixedSizeBundle_64
it "unit_assessTokenBundleSize_fixedSizeBundle_128" $
property unit_assessTokenBundleSize_fixedSizeBundle_128
describe "Utilities" $ do
describe "UnitInterval" $ do
it "coverage adequate" $
checkCoverage $ property $ \i ->
let half = unsafeBoundRational (1 % 2) in
cover 10 (i == half) "i = 0.5" $
cover 10 (i == interval0) "i = 0" $
cover 10 (i == interval1) "i = 1" $
cover 10 (i > interval0 && i < half) "0 < i < 0.5" $
cover 10 (half < i && i < interval1) "0.5 < i < 1"
True
it "invertUnitInterval . invertUnitInterval == id" $
property $ \i ->
invertUnitInterval (invertUnitInterval i) `shouldBe` i
it "intervalValue i + intervalValue (invertUnitInterval i) == 1" $
property $ \i ->
SL.unboundRational i + SL.unboundRational (invertUnitInterval i)
`shouldBe` 1
it "invertUnitInterval interval0 == interval1" $
invertUnitInterval interval0 `shouldBe` interval1
it "invertUnitInterval interval1 == interval0" $
invertUnitInterval interval1 `shouldBe` interval0
it "invertUnitInterval half == half" $
let half = unsafeBoundRational (1 % 2) in
invertUnitInterval half `shouldBe` half
describe "InspectAddr" $ do
-- Cases below are taken straight from cardano-addresses. We don't go in
-- depth with testing here because this is already tested on
-- cardano-addresses.
let matrix =
[ ( "Byron (1)"
, "37btjrVyb4KEgoGCHJ7XFaJRLBRiVuvcrQWPpp4HeaxdTxhKwQjXHNKL43\
\NhXaQNa862BmxSFXZFKqPqbxRc3kCUeTRMwjJevFeCKokBG7A7num5Wh"
, isRight
)
, ( "Byron (2)"
, "DdzFFzCqrht5csm2GKhnVrjzKpVHHQFNXUDhAFDyLWVY5w8ZsJRP2uhwZ\
\q2CEAVzDZXYXa4GvggqYEegQsdKAKikFfrrCoHheLH2Jskr"
, isRight
)
, ( "Icarus"
, "Ae2tdPwUPEYz6ExfbWubiXPB6daUuhJxikMEb4eXRp5oKZBKZwrbJ2k7EZe"
, isRight
)
, ( "Shelley (base)"
, "addr1vpu5vlrf4xkxv2qpwngf6cjhtw542ayty80v8dyr49rf5eg0yu80w"
, isRight
)
, ( "Shelley (stake by value)"
, "addr1qdu5vlrf4xkxv2qpwngf6cjhtw542ayty80v8dyr49rf5ew\
\vxwdrt70qlcpeeagscasafhffqsxy36t90ldv06wqrk2q5ggg4z"
, isRight
)
, ( "Shelley (stake by pointer)"
, "addr1gw2fxv2umyhttkxyxp8x0dlpdt3k6cwng5pxj3jhsydzer5ph3wczvf2x4v58t"
, isRight
)
, ( "Shelley (reward by key)"
, "stake1upshvetj09hxjcm9v9jxgunjv4ehxmr0d3hkcmmvdakx7mqcjv83c"
, isRight
)
, ( "Shelley (reward by script)"
, "stake17pshvetj09hxjcm9v9jxgunjv4ehxmr0d3hkcmmvdakx7mq36s8xc"
, isRight
)
, ( "Shelley (testnet 1)"
, "addr_test1qpwr8l57ceql23ylyprl6qgct239lxph8clwxy5w8r4qdz8ct9uut5a\
\hmxqkgwy9ecn5carsv39frsgsq09u70wmqwhqjqcjqs"
, isRight
)
, ( "Shelley (testnet 2)"
, "stake_test1uru9j7w96wmanqty8zzuuf6vw3cxgj53cygq8j708hds8tsntl0j7"
, isRight
)
, ( "Malformed (1)"
, "💩"
, isLeft
)
, ( "Malformed (2)"
, "79467c69a9ac66280174d09d62575ba955748b21dec3b483a9469a65"
, isLeft
)
]
forM_ matrix $ \(title, addr, predicate) ->
it title $ inspectAddress addr `shouldSatisfy` predicate
describe "golden tests for script hashes" $ do
testScriptsAllLangs
testScriptsTimelockLang
describe "golden tests for script preimages" $ do
testScriptPreimages
testTimelockScriptImagesLang
--------------------------------------------------------------------------------
-- Conversions
--------------------------------------------------------------------------------
prop_unsafeIntToWord :: TrickyInt Integer Word16 -> Property
prop_unsafeIntToWord (TrickyInt n wrong) = monadicIO $ do
res <- run $ tryInternalError $ unsafeIntToWord @Integer @Word16 n
monitor (counterexample ("res = " ++ show res))
assert $ case res of
Right correct -> fromIntegral correct == n
Left _ -> fromIntegral wrong /= n
data TrickyInt n w = TrickyInt n w deriving (Show, Eq)
instance (Arbitrary n, Integral n, Num w) => Arbitrary (TrickyInt n w) where
arbitrary = do
d <- arbitrary
x <- getSmall . getNonNegative <$> arbitrary :: Gen Int
s <- frequency [(20, pure 1), (5, pure (-1)), (1, pure 0)]
let n = s * ((2 ^ x) + d)
pure $ TrickyInt n (fromIntegral n)
--------------------------------------------------------------------------------
-- Assessing the sizes of token bundles
--------------------------------------------------------------------------------
-- Enlarging a token bundle that is over the size limit should yield a token
-- bundle that is still over the size limit.
--
prop_assessTokenBundleSize_enlarge
:: Blind (VariableSize1024 TokenBundle)
-> Blind (VariableSize16 TokenBundle)
-> Property
prop_assessTokenBundleSize_enlarge b1' b2' =
assess b1 == TokenBundleSizeExceedsLimit ==> conjoin
[ assess (b1 `TokenBundle.add` b2)
=== TokenBundleSizeExceedsLimit
, assess (b1 `TokenBundle.setCoin` txOutMaxCoin)
=== TokenBundleSizeExceedsLimit
]
where
assess = assessTokenBundleSize
$ tokenBundleSizeAssessor maryTokenBundleMaxSize
b1 = unVariableSize1024 $ getBlind b1'
b2 = unVariableSize16 $ getBlind b2'
-- Shrinking a token bundle that is within the size limit should yield a token
-- bundle that is still within the size limit.
--
prop_assessTokenBundleSize_shrink
:: Blind (VariableSize1024 TokenBundle)
-> Blind (VariableSize16 TokenBundle)
-> TokenBundleMaxSize
-> Property
prop_assessTokenBundleSize_shrink b1' b2' maxSize =
assess b1 == TokenBundleSizeWithinLimit ==> conjoin
[ assess (b1 `TokenBundle.difference` b2)
=== TokenBundleSizeWithinLimit
, assess (b1 `TokenBundle.setCoin` txOutMinCoin)
=== TokenBundleSizeWithinLimit
]
where
assess = assessTokenBundleSize (tokenBundleSizeAssessor maxSize)
b1 = unVariableSize1024 $ getBlind b1'
b2 = unVariableSize16 $ getBlind b2'
-- | Creates a test to assess the size of a token bundle with a fixed number of
-- assets, where the expected result is a constant.
--
-- Policy identifiers, asset names, token quantities are all allowed to vary.
--
unit_assessTokenBundleSize_fixedSizeBundle
:: TokenBundle
-- ^ Fixed size bundle
-> TokenBundleSizeAssessment
-- ^ Expected size assessment
-> TokenBundleMaxSize
-- ^ TokenBundle assessor function
-> TxSize
-- ^ Expected min length (bytes)
-> TxSize
-- ^ Expected max length (bytes)
-> Property
unit_assessTokenBundleSize_fixedSizeBundle
bundle
expectedAssessment
maxSize
expectedMinLengthBytes
expectedMaxLengthBytes =
withMaxSuccess 100 $
counterexample counterexampleText $
conjoin . fmap property $
[ actualAssessment == expectedAssessment
, actualLengthBytes >= expectedMinLengthBytes
, actualLengthBytes <= expectedMaxLengthBytes
]
where
actualAssessment = assessTokenBundleSize
(tokenBundleSizeAssessor maxSize)
bundle
actualLengthBytes = computeTokenBundleSerializedLengthBytes bundle
counterexampleText = unlines
[ "Expected min length bytes:"
, show expectedMinLengthBytes
, "Expected max length bytes:"
, show expectedMaxLengthBytes
, "Actual length bytes:"
, show actualLengthBytes
, "Expected assessment:"
, show expectedAssessment
, "Actual assessment:"
, show actualAssessment
]
unit_assessTokenBundleSize_fixedSizeBundle_32
:: Blind (FixedSize32 TokenBundle) -> Property
unit_assessTokenBundleSize_fixedSizeBundle_32 (Blind (FixedSize32 b)) =
unit_assessTokenBundleSize_fixedSizeBundle b
TokenBundleSizeWithinLimit
maryTokenBundleMaxSize
(TxSize 2116) (TxSize 2380)
unit_assessTokenBundleSize_fixedSizeBundle_48
:: Blind (FixedSize48 TokenBundle) -> Property
unit_assessTokenBundleSize_fixedSizeBundle_48 (Blind (FixedSize48 b)) =
unit_assessTokenBundleSize_fixedSizeBundle b
TokenBundleSizeWithinLimit
maryTokenBundleMaxSize
(TxSize 3172) (TxSize 3564)
unit_assessTokenBundleSize_fixedSizeBundle_64
:: Blind (FixedSize64 TokenBundle) -> Property
unit_assessTokenBundleSize_fixedSizeBundle_64 (Blind (FixedSize64 b)) =
unit_assessTokenBundleSize_fixedSizeBundle b
TokenBundleSizeExceedsLimit
maryTokenBundleMaxSize
(TxSize 4228) (TxSize 4748)
unit_assessTokenBundleSize_fixedSizeBundle_128
:: Blind (FixedSize128 TokenBundle) -> Property
unit_assessTokenBundleSize_fixedSizeBundle_128 (Blind (FixedSize128 b)) =
unit_assessTokenBundleSize_fixedSizeBundle b
TokenBundleSizeExceedsLimit
maryTokenBundleMaxSize
(TxSize 8452) (TxSize 9484)
toKeyHash :: Text -> Script KeyHash
toKeyHash txt = case fromBase16 (T.encodeUtf8 txt) of
Right bs -> case keyHashFromBytes (Payment, bs) of
Just kh -> RequireSignatureOf kh
Nothing -> error "Hash key not valid"
Left _ -> error "Hash key not valid"
toPaymentHash :: Text -> Cardano.SimpleScript
toPaymentHash txt =
case Cardano.deserialiseFromRawBytesHex (Cardano.AsHash Cardano.AsPaymentKey) (T.encodeUtf8 txt) of
Right payKeyHash -> Cardano.RequireSignature payKeyHash
Left err -> error $ "toPaymentHash: " <> show err
checkScriptHashes
:: String
-> Script KeyHash
-> Cardano.Script lang
-> SpecWith ()
checkScriptHashes title adrestiaScript nodeScript = it title $
unScriptHash (toScriptHash adrestiaScript) `shouldBe`
Cardano.serialiseToRawBytes (Cardano.hashScript nodeScript)
checkScriptPreimage
:: Cardano.SerialiseAsCBOR (Cardano.Script lang)
=> String
-> Script KeyHash
-> Cardano.Script lang
-> SpecWith ()
checkScriptPreimage title adrestiaScript nodeScript = it title $
(serializeScript adrestiaScript) `shouldBe`
BS.append "\00" (Cardano.serialiseToCBOR nodeScript)
scriptMatrix
:: [(String, Script KeyHash, Cardano.Script Cardano.SimpleScript')]
scriptMatrix =
[ ( "RequireSignatureOf"
, toKeyHash hashKeyTxt1
, toSimpleScript $ toPaymentHash hashKeyTxt1
)
, ( "RequireSignatureOf"
, toKeyHash hashKeyTxt2
, toSimpleScript $ toPaymentHash hashKeyTxt2
)
, ( "RequireSignatureOf"
, toKeyHash hashKeyTxt3
, toSimpleScript $ toPaymentHash hashKeyTxt3
)
, ( "RequireSignatureOf"
, toKeyHash hashKeyTxt4
, toSimpleScript $ toPaymentHash hashKeyTxt4
)
, ( "RequireAllOf"
, RequireAllOf [toKeyHash hashKeyTxt1, toKeyHash hashKeyTxt2]
, toSimpleScript $
Cardano.RequireAllOf [toPaymentHash hashKeyTxt1, toPaymentHash hashKeyTxt2]
)
, ( "RequireAllOf"
, RequireAllOf [toKeyHash hashKeyTxt1, toKeyHash hashKeyTxt2, toKeyHash hashKeyTxt3]
, toSimpleScript $
Cardano.RequireAllOf [toPaymentHash hashKeyTxt1, toPaymentHash hashKeyTxt2, toPaymentHash hashKeyTxt3]
)
, ( "RequireAnyOf"
, RequireAnyOf [toKeyHash hashKeyTxt1, toKeyHash hashKeyTxt2]
, toSimpleScript $
Cardano.RequireAnyOf [toPaymentHash hashKeyTxt1, toPaymentHash hashKeyTxt2]
)
, ( "RequireAnyOf"
, RequireAnyOf [toKeyHash hashKeyTxt1, toKeyHash hashKeyTxt2, toKeyHash hashKeyTxt3]
, toSimpleScript $
Cardano.RequireAnyOf [toPaymentHash hashKeyTxt1, toPaymentHash hashKeyTxt2, toPaymentHash hashKeyTxt3]
)
, ( "RequireSomeOf"
, RequireSomeOf 2 [toKeyHash hashKeyTxt1, toKeyHash hashKeyTxt2, toKeyHash hashKeyTxt3]
, toSimpleScript $
Cardano.RequireMOf 2 [toPaymentHash hashKeyTxt1, toPaymentHash hashKeyTxt2, toPaymentHash hashKeyTxt3]
)
, ( "RequireSomeOf"
, RequireSomeOf 2 [toKeyHash hashKeyTxt1, toKeyHash hashKeyTxt2, toKeyHash hashKeyTxt3, toKeyHash hashKeyTxt4]
, toSimpleScript $
Cardano.RequireMOf 2 [toPaymentHash hashKeyTxt1, toPaymentHash hashKeyTxt2, toPaymentHash hashKeyTxt3, toPaymentHash hashKeyTxt4]
)
, ( "nested 1"
, RequireSomeOf 2 [ toKeyHash hashKeyTxt1, toKeyHash hashKeyTxt2
, RequireAllOf [toKeyHash hashKeyTxt3, toKeyHash hashKeyTxt4]
]
, toSimpleScript $
Cardano.RequireMOf 2 [ toPaymentHash hashKeyTxt1, toPaymentHash hashKeyTxt2
, Cardano.RequireAllOf [toPaymentHash hashKeyTxt3, toPaymentHash hashKeyTxt4]
]
)
, ( "nested 2"
, RequireAllOf [ toKeyHash hashKeyTxt1
, RequireAnyOf [toKeyHash hashKeyTxt2, toKeyHash hashKeyTxt3, toKeyHash hashKeyTxt4]
]
, toSimpleScript $
Cardano.RequireAllOf [ toPaymentHash hashKeyTxt1
, Cardano.RequireAnyOf [toPaymentHash hashKeyTxt2, toPaymentHash hashKeyTxt3, toPaymentHash hashKeyTxt4]
]
)
, ( "nested 3"
, RequireSomeOf 1 [ toKeyHash hashKeyTxt1
, RequireAllOf [ toKeyHash hashKeyTxt2
, RequireAnyOf [toKeyHash hashKeyTxt3, toKeyHash hashKeyTxt4 ]
]
]
, toSimpleScript $
Cardano.RequireMOf 1 [ toPaymentHash hashKeyTxt1
, Cardano.RequireAllOf [ toPaymentHash hashKeyTxt2
, Cardano.RequireAnyOf [toPaymentHash hashKeyTxt3, toPaymentHash hashKeyTxt4]
]
]
)
]
where
toSimpleScript = Cardano.SimpleScript
hashKeyTxt1 = "deeae4e895d8d57378125ed4fd540f9bf245d59f7936a504379cfc1e"
hashKeyTxt2 = "60a3bf69aa748f9934b64357d9f1ca202f1a768aaf57263aedca8d5f"
hashKeyTxt3 = "ffcbb72393215007d9a0aa02b7430080409cd8c053fd4f5b4d905053"
hashKeyTxt4 = "96834025cdca063ce9c32dfae6bc6a3e47f8da07ee4fb8e1a3901559"
testScriptsAllLangs
:: Spec
testScriptsAllLangs = do
forM_ scriptMatrix $ \(title, adrestiaScript, nodeScript) ->
checkScriptHashes title adrestiaScript nodeScript
testScriptPreimages
:: Spec
testScriptPreimages = do
forM_ scriptMatrix $ \(title, adrestiaScript, nodeScript) ->
checkScriptPreimage title adrestiaScript nodeScript
timelockScriptMatrix
:: [(String, Script KeyHash, Cardano.Script Cardano.SimpleScript')]
timelockScriptMatrix =
[ ( "SimpleScript ActiveFromSlot"
, RequireAllOf [toKeyHash hashKeyTxt1, ActiveFromSlot 120]
, toSimpleScript $
Cardano.RequireAllOf
[toPaymentHash hashKeyTxt1, Cardano.RequireTimeAfter (SlotNo 120)]
)
, ( "SimpleScript ActiveUntilSlot"
, RequireAllOf [toKeyHash hashKeyTxt1, ActiveUntilSlot 120]
, toSimpleScript $
Cardano.RequireAllOf
[toPaymentHash hashKeyTxt1, Cardano.RequireTimeBefore (SlotNo 120)]
)
, ( "SimpleScript ActiveFromSlot and ActiveUntilSlot"
, RequireAllOf
[ ActiveFromSlot 120
, ActiveUntilSlot 150
, RequireAnyOf [toKeyHash hashKeyTxt1, toKeyHash hashKeyTxt2]
]
, toSimpleScript $
Cardano.RequireAllOf
[ Cardano.RequireTimeAfter (SlotNo 120)
, Cardano.RequireTimeBefore (SlotNo 150)
, Cardano.RequireAnyOf
[toPaymentHash hashKeyTxt1, toPaymentHash hashKeyTxt2 ]
]
)
]
where
hashKeyTxt1 = "deeae4e895d8d57378125ed4fd540f9bf245d59f7936a504379cfc1e"
hashKeyTxt2 = "60a3bf69aa748f9934b64357d9f1ca202f1a768aaf57263aedca8d5f"
toSimpleScript = Cardano.SimpleScript
testScriptsTimelockLang :: Spec
testScriptsTimelockLang =
forM_ timelockScriptMatrix $ \(title, adrestiaScript, nodeScript) ->
checkScriptHashes title adrestiaScript nodeScript
testTimelockScriptImagesLang :: Spec
testTimelockScriptImagesLang =
forM_ timelockScriptMatrix $ \(title, adrestiaScript, nodeScript) ->
checkScriptPreimage title adrestiaScript nodeScript
instance Arbitrary (Hash "Genesis") where
arbitrary = Hash . BS.pack <$> vector 32
instance Arbitrary (Hash "BlockHeader") where
arbitrary = Hash . BS.pack <$> vector 32
instance Arbitrary RewardAccount where
arbitrary = RewardAccount . BS.pack <$> vector 28
instance Arbitrary (Tip (CardanoBlock StandardCrypto)) where
arbitrary = frequency
[ (10, return TipGenesis)
, (90, arbitraryTip)
]
where
arbitraryTip = do
n <- choose (0, 100)
hash <- toCardanoHash
. Hash
. digest (Proxy @(HASH StandardCrypto))
. BS.pack <$> vector 5
return $ Tip (SlotNo n) hash (BlockNo n)
instance Arbitrary SL.UnitInterval where
arbitrary = oneof
[ pure interval0
, pure interval1
, pure $ unsafeBoundRational (1 % 2)
, unsafeBoundRational . (% 1000) <$> choose (0, 1000)
]
shrink = map unsafeBoundRational . shrink . SL.unboundRational
instance Arbitrary SlotId where
arbitrary = SlotId
<$> (W.EpochNo . fromIntegral <$> choose (0, 10 :: Word32))
<*> (W.SlotInEpoch <$> choose (0, 10))
instance Arbitrary (ShelleyKey 'CredFromKeyK XPrv) where
shrink _ = []
arbitrary = do
mnemonic <- arbitrary
return $ Shelley.unsafeGenerateKeyFromSeed mnemonic mempty
instance Arbitrary (ByronKey 'CredFromKeyK XPrv) where
shrink _ = []
arbitrary = do
mnemonic <- arbitrary
acctIx <- arbitrary
addrIx <- arbitrary
pure $ Byron.unsafeGenerateKeyFromSeed (acctIx, addrIx) mnemonic mempty
instance
( Enum (AddressDerivation.Index derivationType depth)
, Bounded (AddressDerivation.Index derivationType depth)
) =>
Arbitrary (AddressDerivation.Index derivationType depth) where
arbitrary = toEnum <$> chooseInt (0, maxIndex)
where
maxIndex = fromIntegral . getIndex $
maxBound @(AddressDerivation.Index derivationType depth)
instance (WalletKey k, Arbitrary (k 'CredFromKeyK XPrv)) =>
Arbitrary (k 'CredFromKeyK XPub)
where
shrink _ = []
arbitrary = publicKey <$> arbitrary
instance Arbitrary SomeMnemonic where
arbitrary = SomeMnemonic <$> genMnemonic @12
genMnemonic
:: forall mw ent csz.
( ConsistentEntropy ent mw csz
, EntropySize mw ~ ent
)
=> Gen (Mnemonic mw)
genMnemonic = do
let n = fromIntegral (natVal $ Proxy @(EntropySize mw)) `div` 8
bytes <- BS.pack <$> vector n
let ent = unsafeMkEntropy @(EntropySize mw) bytes
return $ entropyToMnemonic ent
instance Show XPrv where
show _ = "<xprv>"
instance Arbitrary TokenBundle.TokenBundle where
arbitrary = genTokenBundleSmallRange
shrink = shrinkTokenBundleSmallRange
newtype FixedSize32 a = FixedSize32 { unFixedSize32 :: a }
deriving (Eq, Show)
newtype FixedSize48 a = FixedSize48 { unFixedSize48 :: a }
deriving (Eq, Show)
newtype FixedSize64 a = FixedSize64 { unFixedSize64 :: a }
deriving (Eq, Show)
newtype FixedSize128 a = FixedSize128 { unFixedSize128 :: a }
deriving (Eq, Show)
newtype VariableSize16 a = VariableSize16 { unVariableSize16 :: a}
deriving (Eq, Show)
newtype VariableSize1024 a = VariableSize1024 { unVariableSize1024 :: a}
deriving (Eq, Show)
instance Arbitrary (FixedSize32 TokenBundle) where
arbitrary = FixedSize32 <$> genTxOutTokenBundle 32
-- No shrinking
instance Arbitrary (FixedSize48 TokenBundle) where
arbitrary = FixedSize48 <$> genTxOutTokenBundle 48
-- No shrinking
instance Arbitrary (FixedSize64 TokenBundle) where
arbitrary = FixedSize64 <$> genTxOutTokenBundle 64
-- No shrinking
instance Arbitrary (FixedSize128 TokenBundle) where
arbitrary = FixedSize128 <$> genTxOutTokenBundle 128
-- No shrinking
instance Arbitrary (VariableSize16 TokenBundle) where
arbitrary = VariableSize16 <$> resize 16 genTokenBundle
-- No shrinking
instance Arbitrary (VariableSize1024 TokenBundle) where
arbitrary = VariableSize1024 <$> resize 1024 genTokenBundle
-- No shrinking
--
-- Helpers
--
--
bech32 :: ByteString -> Text
bech32 = Bech32.encodeLenient hrp . Bech32.dataPartFromBytes
where hrp = [humanReadablePart|addr|]
-- Expected bech32 encoding for testnets
-- https://github.com/cardano-foundation/CIPs/tree/master/CIP5
bech32testnet :: ByteString -> Text
bech32testnet = Bech32.encodeLenient hrp . Bech32.dataPartFromBytes
where hrp = [humanReadablePart|addr_test|]
base58 :: ByteString -> Text
base58 = T.decodeUtf8 . encodeBase58 bitcoinAlphabet
unsafeBoundRational :: Rational -> SL.UnitInterval
unsafeBoundRational =
fromMaybe (error "unsafeBoundRational: the impossible happened")
. SL.boundRational