-
Notifications
You must be signed in to change notification settings - Fork 211
/
ScriptsSpec.hs
346 lines (312 loc) · 12.3 KB
/
ScriptsSpec.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
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Cardano.Wallet.Primitive.ScriptsSpec
( spec
) where
import Prelude
import Cardano.Address.Derivation
( XPub )
import Cardano.Address.Script
( KeyHash (..), Script (..), toScriptHash )
import Cardano.Mnemonic
( SomeMnemonic (..) )
import Cardano.Wallet.Gen
( genMnemonic )
import Cardano.Wallet.Primitive.AddressDerivation
( Depth (..)
, DerivationType (..)
, HardDerivation (..)
, Index (..)
, NetworkDiscriminant (..)
, Passphrase (..)
, PassphraseMaxLength (..)
, PassphraseMinLength (..)
, Role (..)
, SoftDerivation (..)
, WalletKey (..)
, preparePassphrase
)
import Cardano.Wallet.Primitive.AddressDerivation.Shelley
( ShelleyKey (..), generateKeyFromSeed )
import Cardano.Wallet.Primitive.AddressDiscovery.Sequential
( DerivationPrefix (..)
, SeqState (..)
, VerificationKeyPool (..)
, coinTypeAda
, defaultAddressPoolGap
, emptyPendingIxs
, getAddressPoolGap
, mkAddressPool
, mkUnboundedAddressPoolGap
, mkVerificationKeyPool
, purposeCIP1852
, purposeCIP1852
, toVerKeyHash
)
import Cardano.Wallet.Primitive.Scripts
( isShared, retrieveAllVerKeyHashes )
import Cardano.Wallet.Primitive.Types
( PassphraseScheme (..) )
import Cardano.Wallet.Primitive.Types.Address
( AddressState (..) )
import Cardano.Wallet.Unsafe
( unsafeXPub )
import Control.Monad
( replicateM )
import Data.Proxy
( Proxy (..) )
import Data.Word
( Word32 )
import Test.Hspec
( Spec, describe, it )
import Test.QuickCheck
( Arbitrary (..)
, Gen
, Positive (..)
, Property
, arbitraryPrintableChar
, choose
, elements
, property
, scale
, sized
, vectorOf
, (.&&.)
, (===)
)
import qualified Data.ByteArray as BA
import qualified Data.ByteString.Char8 as B8
import qualified Data.List as L
import qualified Data.Map.Strict as Map
import qualified Data.Set as Set
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
spec :: Spec
spec = do
describe "isShared" $ do
it "script composed with all of our verification keys should discover them all"
(property prop_scriptFromOurVerKeys)
it "script composed with not our verification keys should not be discovered"
(property prop_scriptFromNotOurVerKeys)
it "the same script discovered twice should have the same knownScripts imprint"
(property prop_scriptDiscoveredTwice)
it "knownScripts of the sequential state is populated properly"
(property prop_scriptUpdatesStateProperly)
it "scripts with our verification keys are discovered properly"
(property prop_scriptsDiscovered)
it "discovering our verification keys make them mark Used"
(property prop_markingDiscoveredVerKeys)
it "discovering works after pool extension"
(property prop_poolExtension)
it "our verification keys beyond pool gap are not discovered"
(property prop_scriptsOutsideNotDiscovered)
prop_scriptFromOurVerKeys
:: AccountXPubWithScripts
-> Property
prop_scriptFromOurVerKeys (AccountXPubWithScripts accXPub' scripts') = do
let (script:_) = scripts'
let sciptKeyHashes = retrieveAllVerKeyHashes script
let seqState = initializeState accXPub'
let (ourSharedKeys, _) = isShared script seqState
L.sort (L.nub $ map toVerKeyHash ourSharedKeys) === L.sort (L.nub sciptKeyHashes)
prop_scriptFromNotOurVerKeys
:: ShelleyKey 'AccountK XPub
-> AccountXPubWithScripts
-> Property
prop_scriptFromNotOurVerKeys accXPub' (AccountXPubWithScripts _accXPub scripts') = do
let (script:_) = scripts'
let seqState = initializeState accXPub'
let (ourSharedKeys, _) = isShared script seqState
ourSharedKeys === []
prop_scriptUpdatesStateProperly
:: AccountXPubWithScripts
-> Property
prop_scriptUpdatesStateProperly (AccountXPubWithScripts accXPub' scripts') = do
let (script:_) = scripts'
let sciptKeyHashes = retrieveAllVerKeyHashes script
let seqState = initializeState accXPub'
let (_, seqState') = isShared script seqState
let scriptKeyHashesInMap =
Set.fromList . map (toVerKeyHash . projectKey) <$>
Map.lookup (toScriptHash script) (knownScripts seqState')
scriptKeyHashesInMap === Just (Set.fromList (L.nub sciptKeyHashes))
prop_scriptDiscoveredTwice
:: AccountXPubWithScripts
-> Property
prop_scriptDiscoveredTwice (AccountXPubWithScripts accXPub' scripts') = do
let (script:_) = scripts'
let seqState = initializeState accXPub'
let (_, seqState') = isShared script seqState
let (_, seqState'') = isShared script seqState'
knownScripts seqState' === knownScripts seqState''
prop_scriptsDiscovered
:: AccountXPubWithScripts
-> Property
prop_scriptsDiscovered (AccountXPubWithScripts accXPub' scripts') = do
let seqState0 = initializeState accXPub'
let seqState = foldr (\script s -> snd $ isShared script s) seqState0 scripts'
let scriptHashes = Set.fromList $ Map.keys $ knownScripts seqState
scriptHashes === Set.fromList (map toScriptHash scripts')
prop_markingDiscoveredVerKeys
:: AccountXPubWithScripts
-> Property
prop_markingDiscoveredVerKeys (AccountXPubWithScripts accXPub' scripts') = do
let (script:_) = scripts'
let sciptKeyHashes = retrieveAllVerKeyHashes script
let seqState = initializeState accXPub'
let (_, SeqState _ _ _ _ _ _ (VerificationKeyPool _ _ allOurVerKeys)) =
isShared script seqState
let discoveredKeyMap = Map.filterWithKey (\k _ -> k `elem` sciptKeyHashes) allOurVerKeys
let addressStatesToCheck =
map (\(_, (_, isUsed)) -> isUsed) $ Map.toList discoveredKeyMap
L.all (== Used) addressStatesToCheck === True
prop_poolExtension
:: AccountXPubWithScriptsExtension
-> Property
prop_poolExtension (AccountXPubWithScriptsExtension accXPub' scripts') = do
g' == mkUnboundedAddressPoolGap
(getAddressPoolGap defaultAddressPoolGap + getAddressPoolGap g)
.&&.
scriptHashes == Set.fromList (map toScriptHash scripts')
where
(SeqState s1 s2 ixs rpk prefix scr (VerificationKeyPool _ g allOurVerKeys)) =
initializeState accXPub'
--make artificially all except last index Used
isLastIndex ix =
if ix == Index (minBound + getAddressPoolGap defaultAddressPoolGap - 1) then
Unused
else
Used
allOurVerKeys' =
Map.mapWithKey (\_ (ix, _) -> (ix, isLastIndex ix)) allOurVerKeys
seqState =
SeqState s1 s2 ixs rpk prefix scr (VerificationKeyPool accXPub' g allOurVerKeys')
(script:restScripts) = scripts'
(_, seqState0@(SeqState _ _ _ _ _ _ (VerificationKeyPool _ g' _)) ) =
isShared script seqState
seqState' = foldr (\script' s -> snd $ isShared script' s) seqState0 restScripts
scriptHashes = Set.fromList $ Map.keys $ knownScripts seqState'
prop_scriptsOutsideNotDiscovered
:: AccountXPubWithScriptsOutside
-> Property
prop_scriptsOutsideNotDiscovered (AccountXPubWithScriptsOutside accXPub' scripts') = do
let seqState0 = initializeState accXPub'
let seqState = foldr (\script s -> snd $ isShared script s) seqState0 scripts'
let scriptHashes = Map.keys $ knownScripts seqState
null scriptHashes === True
data AccountXPubWithScripts = AccountXPubWithScripts
{ accXPub :: ShelleyKey 'AccountK XPub
, scripts :: [Script]
} deriving (Eq, Show)
data AccountXPubWithScriptsExtension = AccountXPubWithScriptsExtension
{ accXPub :: ShelleyKey 'AccountK XPub
, scripts :: [Script]
} deriving (Eq, Show)
data AccountXPubWithScriptsOutside = AccountXPubWithScriptsOutside
{ accXPub :: ShelleyKey 'AccountK XPub
, scripts :: [Script]
} deriving (Eq, Show)
defaultPrefix :: DerivationPrefix
defaultPrefix = DerivationPrefix
( purposeCIP1852
, coinTypeAda
, minBound
)
dummyRewardAccount :: ShelleyKey 'AddressK XPub
dummyRewardAccount = ShelleyKey $ unsafeXPub $ B8.replicate 64 '0'
projectKey :: ShelleyKey 'ScriptK XPub -> ShelleyKey 'AddressK XPub
projectKey (ShelleyKey k) = ShelleyKey k
initializeState
:: ShelleyKey 'AccountK XPub
-> SeqState 'Mainnet ShelleyKey
initializeState accXPub' =
let intPool = mkAddressPool accXPub' defaultAddressPoolGap []
extPool = mkAddressPool accXPub' defaultAddressPoolGap []
multiPool = mkVerificationKeyPool accXPub' defaultAddressPoolGap Map.empty
in SeqState intPool extPool emptyPendingIxs dummyRewardAccount defaultPrefix Map.empty multiPool
{-------------------------------------------------------------------------------
Arbitrary Instances
-------------------------------------------------------------------------------}
genScript :: [KeyHash] -> Gen Script
genScript keyHashes =
scale (`div` 3) $ sized scriptTree
where
scriptTree 0 = do
keyH <- elements keyHashes
pure $ RequireSignatureOf keyH
scriptTree n = do
Positive m <- arbitrary
let n' = n `div` (m + 1)
scripts' <- vectorOf m (scriptTree n')
atLeast <- choose (1, fromIntegral (m + 1))
elements
[ RequireAllOf scripts'
, RequireAnyOf scripts'
, RequireSomeOf atLeast scripts'
]
prepareVerKeys
:: ShelleyKey 'AccountK XPub
-> [Word32]
-> [ShelleyKey 'AddressK XPub]
prepareVerKeys accXPub' =
let toVerKey = deriveAddressPublicKey accXPub' MultisigScript
minIndex = getIndex @'Soft minBound
in map (\ix -> toVerKey (toEnum (fromInteger $ toInteger $ minIndex + ix)))
instance Arbitrary (ShelleyKey 'AccountK XPub) where
arbitrary = do
mnemonics <- SomeMnemonic <$> genMnemonic @12
encPwd <- arbitrary
let rootXPrv = generateKeyFromSeed (mnemonics, Nothing) encPwd
pure $ publicKey $ deriveAccountPrivateKey encPwd rootXPrv minBound
instance Arbitrary AccountXPubWithScripts where
arbitrary = do
accXPub' <- arbitrary
kNum <- choose (2,8)
let verKeyHashes = map toVerKeyHash (prepareVerKeys accXPub' [0 .. kNum])
scriptsNum <- choose (1,10)
AccountXPubWithScripts accXPub' <$> vectorOf scriptsNum (genScript verKeyHashes)
instance Arbitrary AccountXPubWithScriptsExtension where
arbitrary = do
accXPub' <- arbitrary
-- the first script is expected to trigger extension to multisigScript
let g = getAddressPoolGap defaultAddressPoolGap
scriptTipping <-
genScript (toVerKeyHash <$> (prepareVerKeys accXPub' [g - 1]))
-- the next scripts are using extended indices that were not possible to be discovered
kNum <- choose (2,8)
scriptsNum <- choose (1,10)
let verKeysNext = prepareVerKeys accXPub' [g .. g + kNum]
scriptsNext <-
vectorOf scriptsNum (genScript (toVerKeyHash <$> verKeysNext))
pure $ AccountXPubWithScriptsExtension accXPub' (scriptTipping : scriptsNext )
instance Arbitrary AccountXPubWithScriptsOutside where
arbitrary = do
accXPub' <- arbitrary
kNum <- choose (2,8)
let g = getAddressPoolGap defaultAddressPoolGap
let verKeyHashes = map toVerKeyHash (prepareVerKeys accXPub' [g .. g + kNum])
scriptsNum <- choose (1,10)
AccountXPubWithScriptsOutside accXPub' <$> vectorOf scriptsNum (genScript verKeyHashes)
instance Arbitrary (Passphrase "raw") where
arbitrary = do
n <- choose (passphraseMinLength p, passphraseMaxLength p)
bytes <- T.encodeUtf8 . T.pack <$> replicateM n arbitraryPrintableChar
return $ Passphrase $ BA.convert bytes
where p = Proxy :: Proxy "raw"
shrink (Passphrase bytes)
| BA.length bytes <= passphraseMinLength p = []
| otherwise =
[ Passphrase
$ BA.convert
$ B8.take (passphraseMinLength p)
$ BA.convert bytes
]
where p = Proxy :: Proxy "raw"
instance Arbitrary (Passphrase "encryption") where
arbitrary = preparePassphrase EncryptWithPBKDF2
<$> arbitrary @(Passphrase "raw")