-
Notifications
You must be signed in to change notification settings - Fork 211
/
SharedSpec.hs
382 lines (346 loc) · 14.9 KB
/
SharedSpec.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 AllowAmbiguousTypes #-}
{-# LANGUAGE BinaryLiterals #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Cardano.Wallet.Address.Discovery.SharedSpec
( spec
) where
import Prelude
import Cardano.Address.Derivation
( XPrv, XPub )
import Cardano.Address.Script
( Cosigner (..)
, Script (..)
, ScriptTemplate (..)
, ValidationLevel (..)
, validateScriptTemplate
)
import Cardano.Wallet.Address.Derivation
( Depth (..)
, DerivationType (..)
, HardDerivation (..)
, Index (..)
, KeyFingerprint
, Role (..)
, WalletKey (..)
)
import Cardano.Wallet.Address.Derivation.Shared
( unsafeGenerateKeyFromSeed )
import Cardano.Wallet.Address.Derivation.SharedKey
( SharedKey (..), constructAddressFromIx )
import Cardano.Wallet.Address.Discovery
( IsOurs (..), KnownAddresses (..) )
import Cardano.Wallet.Address.Discovery.Sequential
( AddressPoolGap (..), defaultAddressPoolGap, mkUnboundedAddressPoolGap )
import Cardano.Wallet.Address.Discovery.Shared
( Readiness (..)
, SharedAddressPool (..)
, SharedAddressPools (..)
, SharedState (..)
, isShared
, liftPaymentAddress
)
import Cardano.Wallet.Address.Keys.Shared
( mkSharedStateFromAccountXPub, mkSharedStateFromRootXPrv )
import Cardano.Wallet.Flavor
( KeyFlavorS (SharedKeyS) )
import Cardano.Wallet.Gen
( genNatural, genScript, genScriptTemplate )
import Cardano.Wallet.Primitive.Passphrase.Gen
( genEncryptionPassphrase )
import Cardano.Wallet.Primitive.Types.Address
( Address (..), AddressState (..) )
import Cardano.Wallet.Read.NetworkId
( HasSNetworkId, NetworkDiscriminant (..) )
import Cardano.Wallet.Unsafe
( someDummyMnemonic )
import Control.Arrow
( first )
import Data.Either
( isRight )
import Data.Maybe
( isJust )
import Data.Proxy
( Proxy (..) )
import Data.Word
( Word32, Word8 )
import Test.Hspec
( Spec, describe, it )
import Test.QuickCheck
( Arbitrary (..)
, Gen
, Property
, arbitraryBoundedEnum
, choose
, elements
, property
, suchThat
, (.&&.)
, (===)
, (==>)
)
import qualified Cardano.Wallet.Address.Pool as AddressPool
import qualified Data.ByteString as BS
import qualified Data.List as L
import qualified Data.Map.Strict as Map
spec :: Spec
spec = do
describe "isShared for Catalyst" $ do
it "address composed with our verification key should be discoverable if within pool gap"
(property (prop_addressWithScriptFromOurVerKeyIxIn @'Mainnet))
it "address composed with our verification key must not be discoverable if beyond pool gap"
(property (prop_addressWithScriptFromOurVerKeyIxBeyond @'Mainnet))
it "first discovery enlarges ourAddresses and marks the address Used"
(property (prop_addressDiscoveryMakesAddressUsed @'Mainnet))
it "multiple discovery of the same address is idempotent for state"
(property (prop_addressDoubleDiscovery @'Mainnet))
it "address composed with our verification key must not be discoverable with other account public key for the same key index"
(property (prop_addressDiscoveryImpossibleFromOtherAccXPub @'Mainnet))
it "address composed with our verification key must not be discoverable within the same mnemonic when other of its account is used"
(property (prop_addressDiscoveryImpossibleFromOtherAccountOfTheSameRootXPrv @'Mainnet))
it "address composed with our verification key must not be discoverable within proper account is script changes structure"
(property (prop_addressDiscoveryImpossibleWithinAccountButDifferentScript @'Mainnet))
it "upon address discovery there is exact and consecutive number of Unused indices that amounts to the address pool gap number"
(property (prop_addressDiscoveryDoesNotChangeGapInvariance @'Mainnet))
describe "KnownAddresses" $ do
it "addresses with wrong prefixes and our credentials are discovered via isOurs" $ do
(property prop_oursUnexpectedPrefix)
-- specialize to SharedKey
mkSharedStateFromAccountXPub'
:: HasSNetworkId n
=> SharedKey 'AccountK XPub
-> Index 'Hardened 'AccountK
-> AddressPoolGap
-> ScriptTemplate
-> Maybe ScriptTemplate
-> SharedState n SharedKey
mkSharedStateFromAccountXPub' = mkSharedStateFromAccountXPub SharedKeyS
prop_addressWithScriptFromOurVerKeyIxIn
:: forall n. HasSNetworkId n
=> CatalystSharedState
-> Index 'Soft 'CredFromScriptK
-> Property
prop_addressWithScriptFromOurVerKeyIxIn (CatalystSharedState accXPub' accIx' pTemplate' dTemplate' g) keyIx =
preconditions keyIx g dTemplate' ==>
keyIx' === keyIx
where
addr = constructAddressFromIx @n UtxoExternal pTemplate' dTemplate' keyIx
sharedState = mkSharedStateFromAccountXPub' @n accXPub' accIx' g pTemplate' dTemplate'
(Just (keyIx', _), _) = isShared @n addr sharedState
prop_addressWithScriptFromOurVerKeyIxBeyond
:: forall n. HasSNetworkId n
=> CatalystSharedState
-> Index 'Soft 'CredFromScriptK
-> Property
prop_addressWithScriptFromOurVerKeyIxBeyond (CatalystSharedState accXPub' accIx' pTemplate' dTemplate' g) keyIx =
fromIntegral (fromEnum keyIx) >= threshold g ==>
fst (isShared @n addr sharedState) === Nothing .&&.
snd (isShared @n addr sharedState) === sharedState
where
addr = constructAddressFromIx @n UtxoExternal pTemplate' dTemplate' keyIx
sharedState = mkSharedStateFromAccountXPub' @n accXPub' accIx' g pTemplate' dTemplate'
getAddrPool
:: SharedState n k
-> AddressPool.Pool (KeyFingerprint "payment" k) (Index 'Soft 'CredFromScriptK)
getAddrPool st = case ready st of
Active (SharedAddressPools (SharedAddressPool pool) _ _) -> pool
Pending -> error "expected active state"
prop_addressDiscoveryMakesAddressUsed
:: forall n. HasSNetworkId n
=> CatalystSharedState
-> Index 'Soft 'CredFromScriptK
-> Property
prop_addressDiscoveryMakesAddressUsed (CatalystSharedState accXPub' accIx' pTemplate' dTemplate' g) keyIx =
preconditions keyIx g dTemplate' ==>
(snd <$> Map.lookup addr ourAddrs) === Just Used .&&.
fromIntegral (Map.size ourAddrs) === (fromIntegral (fromEnum ix + 1) + getAddressPoolGap g)
where
sharedState = mkSharedStateFromAccountXPub' @n accXPub' accIx' g pTemplate' dTemplate'
addr = AddressPool.addressFromIx (getAddrPool sharedState) keyIx
(Just (ix, _), sharedState') = isShared @n (liftPaymentAddress @n addr) sharedState
ourAddrs = AddressPool.addresses (getAddrPool sharedState')
prop_addressDoubleDiscovery
:: forall n. HasSNetworkId n
=> CatalystSharedState
-> Index 'Soft 'CredFromScriptK
-> Property
prop_addressDoubleDiscovery (CatalystSharedState accXPub' accIx' pTemplate' dTemplate' g) keyIx =
preconditions keyIx g dTemplate' ==>
isJust (fst sharedState') === True .&&.
snd sharedState' === snd sharedState''
where
addr = constructAddressFromIx @n UtxoExternal pTemplate' dTemplate' keyIx
sharedState = mkSharedStateFromAccountXPub' @n accXPub' accIx' g pTemplate' dTemplate'
sharedState' = isShared @n addr sharedState
sharedState'' = isShared @n addr (snd sharedState')
prop_addressDiscoveryImpossibleFromOtherAccXPub
:: forall n. HasSNetworkId n
=> CatalystSharedState
-> Index 'Soft 'CredFromScriptK
-> SharedKey 'AccountK XPub
-> Property
prop_addressDiscoveryImpossibleFromOtherAccXPub (CatalystSharedState _ accIx' pTemplate' dTemplate' g) keyIx accXPub' =
preconditions keyIx g dTemplate' ==>
fst (isShared addr sharedState) === Nothing .&&.
snd (isShared addr sharedState) === sharedState
where
addr = constructAddressFromIx @n UtxoExternal pTemplate' dTemplate' keyIx
(ScriptTemplate _ script') = pTemplate'
pTemplate'' = ScriptTemplate (Map.fromList [(Cosigner 0, getRawKey accXPub')]) script'
sharedState = mkSharedStateFromAccountXPub' @n accXPub' accIx' g pTemplate'' dTemplate'
prop_addressDiscoveryImpossibleFromOtherAccountOfTheSameRootXPrv
:: forall n. HasSNetworkId n
=> CatalystSharedState
-> Index 'Soft 'CredFromScriptK
-> (SharedKey 'RootK XPrv, Index 'Hardened 'AccountK, Index 'Hardened 'AccountK)
-> Property
prop_addressDiscoveryImpossibleFromOtherAccountOfTheSameRootXPrv (CatalystSharedState _ _ pTemplate' dTemplate' g) keyIx (rootXPrv, accIx', accIx'') =
preconditions keyIx g dTemplate' ==>
fst (isShared addr sharedState) === Nothing .&&.
snd (isShared addr sharedState) === sharedState
where
accXPub' = publicKey $ deriveAccountPrivateKey mempty rootXPrv accIx'
accXPub'' = publicKey $ deriveAccountPrivateKey mempty rootXPrv accIx''
(ScriptTemplate _ script') = pTemplate'
pTemplate'' = ScriptTemplate (Map.fromList [(Cosigner 0, getRawKey accXPub')]) script'
pTemplate''' = ScriptTemplate (Map.fromList [(Cosigner 0, getRawKey accXPub'')]) script'
sharedState = mkSharedStateFromAccountXPub' @n accXPub'' accIx'' g pTemplate'' dTemplate'
addr = constructAddressFromIx @n UtxoExternal pTemplate''' dTemplate' keyIx
prop_addressDiscoveryImpossibleWithinAccountButDifferentScript
:: forall n. HasSNetworkId n
=> CatalystSharedState
-> Index 'Soft 'CredFromScriptK
-> OneCosignerScript
-> Property
prop_addressDiscoveryImpossibleWithinAccountButDifferentScript (CatalystSharedState accXPub' accIx' pTemplate' dTemplate' g) keyIx (OneCosignerScript script') =
preconditions keyIx g dTemplate' ==>
fst (isShared addr sharedState) === Nothing .&&.
snd (isShared addr sharedState) === sharedState
where
(ScriptTemplate cosignerXpubs _) = pTemplate'
pTemplate'' = ScriptTemplate cosignerXpubs script'
sharedState = mkSharedStateFromAccountXPub' @n accXPub' accIx' g pTemplate' dTemplate'
addr = constructAddressFromIx @n UtxoExternal pTemplate'' dTemplate' keyIx
prop_addressDiscoveryDoesNotChangeGapInvariance
:: forall n. HasSNetworkId n
=> CatalystSharedState
-> Index 'Soft 'CredFromScriptK
-> Property
prop_addressDiscoveryDoesNotChangeGapInvariance (CatalystSharedState accXPub' accIx' pTemplate' dTemplate' g) keyIx =
preconditions keyIx g dTemplate' ==>
fromIntegral (L.length mapOfConsecutiveUnused) === getAddressPoolGap g
where
sharedState = mkSharedStateFromAccountXPub' @n accXPub' accIx' g pTemplate' dTemplate'
addr = AddressPool.addressFromIx (getAddrPool sharedState) keyIx
(_, sharedState') = isShared @n (liftPaymentAddress @n addr) sharedState
mapOfConsecutiveUnused
= L.tail
. L.dropWhile (== Unused)
. L.map snd
. L.sortOn fst
. Map.elems . AddressPool.addresses
$ getAddrPool sharedState'
preconditions
:: Index 'Soft 'CredFromScriptK
-> AddressPoolGap
-> Maybe ScriptTemplate
-> Bool
preconditions keyIx g dTemplate' =
fromIntegral (fromEnum keyIx) < threshold g && templateValidated
where
templateValidated = case dTemplate' of
Nothing -> True
Just t -> isRight (validateScriptTemplate RecommendedValidation t)
threshold :: AddressPoolGap -> Word32
threshold g =
fromIntegral (fromEnum (minBound @(Index 'Soft 'CredFromScriptK))) +
getAddressPoolGap g
data CatalystSharedState = CatalystSharedState
{ accXPub :: SharedKey 'AccountK XPub
, accIx :: Index 'Hardened 'AccountK
, pTemplate :: ScriptTemplate
, dTemplate :: Maybe ScriptTemplate
, addrPoolGap :: AddressPoolGap
} deriving (Eq, Show)
prop_oursUnexpectedPrefix
:: SharedState 'Mainnet SharedKey
-> UnexpectedPrefix
-> Property
prop_oursUnexpectedPrefix s prefix =
let
(Address addr, _, _) = head $ knownAddresses s
addr' = BS.cons (unWord8 prefix) (BS.tail addr)
in
first isJust (isOurs (Address addr') s) === (False, s)
{-------------------------------------------------------------------------------
Arbitrary Instances
-------------------------------------------------------------------------------}
instance Arbitrary CatalystSharedState where
arbitrary = do
accIx' <- arbitrary
accXPub' <- snd <$> genKeys accIx'
slotUntil <- genNatural
slotAfter <- genNatural `suchThat` (> slotUntil)
let script' = RequireAllOf
[ RequireSignatureOf (Cosigner 0)
, RequireAnyOf [ ActiveUntilSlot slotUntil, ActiveFromSlot slotAfter] ]
let pTemplate' =
ScriptTemplate (Map.fromList [(Cosigner 0, getRawKey accXPub')]) script'
(OneCosignerScript otherScript) <- arbitrary
let _otherTemplate = ScriptTemplate (Map.fromList [(Cosigner 0, getRawKey accXPub')]) otherScript
--dTemplate' <- elements [Nothing, Just pTemplate', Just otherTemplate]
dTemplate' <- elements [Nothing, Just pTemplate']
CatalystSharedState accXPub' accIx' pTemplate' dTemplate' <$> arbitrary
instance Arbitrary (SharedKey 'AccountK XPub) where
arbitrary = do
accIx' <- arbitrary
snd <$> genKeys accIx'
newtype OneCosignerScript = OneCosignerScript
{ unScript :: Script Cosigner } deriving (Show, Eq)
instance Arbitrary OneCosignerScript where
arbitrary = OneCosignerScript <$> genScript [Cosigner 0]
instance Arbitrary (SharedKey 'RootK XPrv) where
arbitrary = do
accIx' <- arbitrary
fst <$> genKeys accIx'
instance Show XPrv where
show = const "XPrv"
genKeys
:: Index 'Hardened 'AccountK
-> Gen (SharedKey 'RootK XPrv, SharedKey 'AccountK XPub)
genKeys accIx' = do
let mw = someDummyMnemonic (Proxy @12)
let rootXPrv = unsafeGenerateKeyFromSeed (mw, Nothing) mempty
pure (rootXPrv, publicKey $ deriveAccountPrivateKey mempty rootXPrv accIx')
instance Arbitrary AddressPoolGap where
shrink _ = []
arbitrary = mkUnboundedAddressPoolGap <$> choose (10, 20)
instance Arbitrary (Index 'Hardened depth) where
shrink _ = []
arbitrary = arbitraryBoundedEnum
instance Arbitrary (Index 'Soft depth) where
shrink _ = []
arbitrary = toEnum <$> choose (0, 100)
newtype UnexpectedPrefix = UnexpectedPrefix {unWord8 :: Word8}
deriving (Eq, Show)
instance Arbitrary UnexpectedPrefix where
arbitrary = do
let baseAddr = 0b00110001 -- scripthash; scripthash, mainnet
enterpriseAddr = 0b01110001 -- scripthash, mainnet
rewardAcct = 0b11110001 -- scripthash, mainnet
validPrefixesMainnet = [baseAddr, enterpriseAddr, rewardAcct]
UnexpectedPrefix <$> arbitrary `suchThat` (`notElem` validPrefixesMainnet)
instance Arbitrary (SharedState 'Mainnet SharedKey) where
arbitrary = do
rootXPrv <- arbitrary
pwd <- genEncryptionPassphrase
accIx' <- arbitrary
scriptTemplate <- genScriptTemplate
pure $ mkSharedStateFromRootXPrv SharedKeyS (rootXPrv, pwd) accIx'
defaultAddressPoolGap scriptTemplate Nothing