-
Notifications
You must be signed in to change notification settings - Fork 217
/
CompatibilitySpec.hs
382 lines (340 loc) · 12.6 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
{-# 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.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.Primitive.AddressDerivation
( Depth (..)
, NetworkDiscriminant (..)
, PaymentAddress (..)
, WalletKey
, publicKey
)
import Cardano.Wallet.Primitive.AddressDerivation.Byron
( ByronKey (..) )
import Cardano.Wallet.Primitive.AddressDerivation.Shelley
( ShelleyKey (..) )
import Cardano.Wallet.Primitive.Slotting
( fromFlatSlot )
import Cardano.Wallet.Primitive.Types
( Address (..)
, ChimericAccount (..)
, DecentralizationLevel (..)
, EpochLength (..)
, Hash (..)
, SlotId (..)
)
import Cardano.Wallet.Shelley.Compatibility
( CardanoBlock
, StandardCrypto
, decentralizationLevelFromPParams
, fromTip
, inspectAddress
, interval0
, interval1
, invertUnitInterval
, toCardanoHash
, toPoint
)
import Cardano.Wallet.Unsafe
( unsafeMkEntropy )
import Codec.Binary.Bech32.TH
( humanReadablePart )
import Control.Monad
( forM_ )
import Data.ByteArray.Encoding
( Base (..), convertToBase )
import Data.ByteString
( ByteString )
import Data.ByteString.Base58
( bitcoinAlphabet, encodeBase58 )
import Data.Either
( isLeft, isRight )
import Data.Function
( (&) )
import Data.Proxy
( Proxy (..) )
import Data.Ratio
( Ratio, (%) )
import Data.Text
( Text )
import Data.Text.Class
( toText )
import Data.Word
( Word64 )
import GHC.TypeLits
( natVal )
import Ouroboros.Network.Block
( BlockNo (..), Point, SlotNo (..), Tip (..), getTipPoint )
import Test.Hspec
( Spec, describe, it, shouldBe, shouldSatisfy )
import Test.Hspec.QuickCheck
( prop )
import Test.QuickCheck
( Arbitrary (..)
, Gen
, checkCoverage
, choose
, conjoin
, counterexample
, cover
, frequency
, genericShrink
, oneof
, property
, vector
, (===)
)
import qualified Cardano.Ledger.Shelley as SL
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 Codec.Binary.Bech32 as Bech32
import qualified Data.ByteString as BS
import qualified Data.Text.Encoding as T
import qualified Shelley.Spec.Ledger.Address as SL
import qualified Shelley.Spec.Ledger.BaseTypes as SL
import qualified Shelley.Spec.Ledger.PParams as SL
spec :: Spec
spec = do
describe "Conversions" $
it "toPoint' . fromTip' == getTipPoint" $ property $ \gh tip -> do
let fromTip' = fromTip gh
let toPoint' = toPoint gh :: W.BlockHeader -> Point (CardanoBlock StandardCrypto)
toPoint' (fromTip' tip) === (getTipPoint tip)
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 k
case SL.deserialiseAddr @(SL.Shelley StandardCrypto) addr of
Just _ -> property True
Nothing -> property False
prop "Shelley addresses from base16, bech32 and base58" $ \k -> do
let addr@(Address bytes) = paymentAddress @'Mainnet @ShelleyKey k
conjoin
[ decodeAddress @'Mainnet (base16 bytes) === Right addr
& counterexample (show $ base16 bytes)
, decodeAddress @'Mainnet (bech32 bytes) === Right addr
& counterexample (show $ bech32 bytes)
, decodeAddress @'Mainnet (base58 bytes) === Right addr
& counterexample (show $ base58 bytes)
]
prop "Byron addresses from base16, bech32 and base58" $ \k -> do
let addr@(Address bytes) = paymentAddress @'Mainnet @ByronKey k
conjoin
[ decodeAddress @'Mainnet (base16 bytes) === Right addr
& counterexample (show $ base16 bytes)
, decodeAddress @'Mainnet (bech32 bytes) === Right addr
& counterexample (show $ bech32 bytes)
, decodeAddress @'Mainnet (base58 bytes) === Right addr
& counterexample (show $ base58 bytes)
]
describe "decentralizationLevelFromPParams" $ do
let mkDecentralizationParam :: SL.UnitInterval -> SL.PParams (SL.Shelley c)
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
& SL.truncateUnitInterval
& mkDecentralizationParam
& decentralizationLevelFromPParams
& unDecentralizationLevel
& toText
it title $ output `shouldBe` expectedOutput
describe "Utilities" $ do
describe "UnitInterval" $ do
it "coverage adequate" $
checkCoverage $ property $ \i ->
let half = SL.truncateUnitInterval (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.intervalValue i + SL.intervalValue (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 = SL.truncateUnitInterval (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)"
, "stake17pshvetj09hxjcm9v9jxgunjv4ehxmr0d3hkcmmvdakx7mrgdp5xscfm7jc"
, 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
instance Arbitrary (Hash "Genesis") where
arbitrary = Hash . BS.pack <$> vector 32
instance Arbitrary (Hash "BlockHeader") where
arbitrary = Hash . BS.pack <$> vector 32
instance Arbitrary ChimericAccount where
arbitrary = ChimericAccount . 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)
epochLength :: EpochLength
epochLength = EpochLength 10
instance Arbitrary SL.UnitInterval where
arbitrary = oneof
[ pure interval0
, pure interval1
, pure $ SL.truncateUnitInterval (1 % 2)
, SL.truncateUnitInterval . (% 1000) <$> choose (0, 1000)
]
shrink = genericShrink
instance Arbitrary SlotId where
arbitrary = fromFlatSlot epochLength <$> choose (0, 100)
instance Arbitrary (ShelleyKey 'AddressK XPrv) where
shrink _ = []
arbitrary = do
mnemonic <- arbitrary
return $ Shelley.unsafeGenerateKeyFromSeed mnemonic mempty
instance Arbitrary (ByronKey 'AddressK XPrv) where
shrink _ = []
arbitrary = do
mnemonic <- arbitrary
acctIx <- toEnum <$> arbitrary
addrIx <- toEnum <$> arbitrary
return $ Byron.unsafeGenerateKeyFromSeed (acctIx, addrIx) mnemonic mempty
instance (WalletKey k, Arbitrary (k 'AddressK XPrv)) => Arbitrary (k 'AddressK 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>"
--
-- Helpers
--
--
base16 :: ByteString -> Text
base16 = T.decodeUtf8 . convertToBase Base16
bech32 :: ByteString -> Text
bech32 = Bech32.encodeLenient hrp . Bech32.dataPartFromBytes
where hrp = [humanReadablePart|addr|]
base58 :: ByteString -> Text
base58 = T.decodeUtf8 . encodeBase58 bitcoinAlphabet