module Network.Haskoin.Test.Keys where
import Data.Bits (clearBit)
import Data.List (foldl')
import Data.Word (Word32)
import Network.Haskoin.Crypto
import Network.Haskoin.Keys.Common
import Network.Haskoin.Keys.Extended
import Network.Haskoin.Test.Crypto
import Test.QuickCheck
arbitrarySecKeyI :: Gen SecKeyI
arbitrarySecKeyI = wrapSecKey <$> arbitrary <*> arbitrary
arbitraryKeyPair :: Gen (SecKeyI, PubKeyI)
arbitraryKeyPair = do
k <- arbitrarySecKeyI
return (k, derivePubKeyI k)
arbitraryXPrvKey :: Gen XPrvKey
arbitraryXPrvKey =
XPrvKey <$> arbitrary
<*> arbitrary
<*> arbitrary
<*> arbitraryHash256
<*> arbitrary
arbitraryXPubKey :: Gen (XPrvKey, XPubKey)
arbitraryXPubKey = (\k -> (k, deriveXPubKey k)) <$> arbitraryXPrvKey
genIndex :: Gen Word32
genIndex = (`clearBit` 31) <$> arbitrary
arbitraryBip32PathIndex :: Gen Bip32PathIndex
arbitraryBip32PathIndex =
oneof [ Bip32SoftIndex <$> genIndex
, Bip32HardIndex <$> genIndex
]
arbitraryHardPath :: Gen HardPath
arbitraryHardPath = foldl' (:|) Deriv <$> listOf genIndex
arbitrarySoftPath :: Gen SoftPath
arbitrarySoftPath = foldl' (:/) Deriv <$> listOf genIndex
arbitraryDerivPath :: Gen DerivPath
arbitraryDerivPath = concatBip32Segments <$> listOf arbitraryBip32PathIndex
arbitraryParsedPath :: Gen ParsedPath
arbitraryParsedPath =
oneof [ ParsedPrv <$> arbitraryDerivPath
, ParsedPub <$> arbitraryDerivPath
, ParsedEmpty <$> arbitraryDerivPath
]
arbitrarySignature :: Gen (Hash256, SecKey, Sig)
arbitrarySignature = do
m <- arbitraryHash256
key <- arbitrary
let sig = signHash key m
return (m, key, sig)