Skip to content

Commit

Permalink
Compare wallet policy key derivation to cardano-addresses
Browse files Browse the repository at this point in the history
- Add some golden tests comparing wallet policy key derivation to
  cardano-addresses policy key derivation.
  • Loading branch information
sevanspowell committed Jul 27, 2021
1 parent c149e55 commit aaaabb1
Showing 1 changed file with 100 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TypeApplications #-}

{-# OPTIONS_GHC -Wno-orphans #-}

Expand All @@ -10,13 +11,15 @@ module Cardano.Wallet.Primitive.AddressDerivation.MintBurnSpec
import Prelude

import Cardano.Address.Derivation
( XPrv, XPub )
( XPrv, XPub, xprvToBytes )
import Cardano.Address.Script
( KeyHash )
import Cardano.Mnemonic
( Mnemonic, SomeMnemonic (..) )
import Cardano.Wallet.Primitive.AddressDerivation
( Depth (PolicyK, RootK, ScriptK)
, DerivationType (Hardened)
, Index
( Depth (..)
, DerivationType (..)
, Index (..)
, Passphrase
, WalletKey (publicKey)
, getRawKey
Expand All @@ -30,10 +33,15 @@ import Cardano.Wallet.Primitive.AddressDerivation.Shelley
import Cardano.Wallet.Primitive.AddressDerivationSpec
()
import Cardano.Wallet.Unsafe
( unsafeXPrv )
import qualified Data.ByteString as BS
( unsafeBech32Decode, unsafeFromHex, unsafeMkMnemonic, unsafeXPrv )
import Data.Function
( (&) )
import Data.Text
( Text )
import GHC.TypeNats
( KnownNat )
import Test.Hspec
( Spec, describe, it )
( Expectation, Spec, describe, it, shouldBe )
import Test.Hspec.Extra
( parallel )
import Test.QuickCheck
Expand All @@ -42,6 +50,10 @@ import Test.QuickCheck.Arbitrary
( arbitraryBoundedEnum )

import qualified Cardano.Address.Script as CA
import qualified Cardano.Wallet.Primitive.AddressDerivation.Shelley as Shelley
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as BL
import qualified Data.Text.Encoding as T

spec :: Spec
spec = do
Expand All @@ -56,6 +68,23 @@ spec = do
property prop_keyDerivationDiffIndexDiffKey
it "Using derivePolicyKeyAndHash returns same private key as using derivePolicyPrivateKey" $
property prop_keyDerivationRelation
it "Deriving a policy key with cardano-address returns same result as cardano-wallet" $ do
unit_comparePolicyKeys goldenTestMnemonic (Index 0x80000000)
"acct_xsk1tqqqnvtppk994fxm05wtlvp6uuu58srue9st8ew29vr5mxxf89tj2ze05pm8qjkyfetpzl58jkgx6dd3s96szhyxfajpc2gxv22xef0ems2cnvh5d5xjemgghg9m8489v6c9rvnt9za2pruyrtkp5y77l5ujxeug"
-- Child key 1855H/1815H/0H generated by cardano-address CLI
-- from test mnemonic
unit_comparePolicyKeyHashes goldenTestMnemonic (Index 0x80000000)
"6adb501348b99cd38172f355615d7c1d0b9d1e3fc69b565b85a98127"
-- Hash of child key 1855H/1815H/0H generated by cardano-address
-- CLI from test mnemonic
unit_comparePolicyKeys goldenTestMnemonic (Index 0x80000010)
"acct_xsk1rprds8krzhspy8xhkupk2270hzkexmkdga98zv5ns4x9d9kf89t4qp438a26sn62wsfldthkrvnwe6vnetuehgxyxt4hm46zsw03mknuwef0afcljmultpjjwx8zqm0ftnfkqucwvf5qr4gved6h3gnyeqcsur4p"
-- Child key 1855H/1815H/16H generated by cardano-address CLI
-- from test mnemonic
unit_comparePolicyKeyHashes goldenTestMnemonic (Index 0x80000010)
"1f07b91d27ca1cbf911ccfba254315733b3c908575ce2fc29d4c6965"
-- Hash of child key 1855H/1815H/16H generated by
-- cardano-address CLI from test mnemonic

{-------------------------------------------------------------------------------
Properties
Expand Down Expand Up @@ -137,6 +166,70 @@ prop_keyDerivationRelation pwd masterkey policyIx =
key2 :: XPrv
key2 = getRawKey $ fst keyAndHash

unit_comparePolicyKeys
:: KnownNat n
=> Mnemonic n
-> Index 'Hardened 'PolicyK
-> Text
-> Expectation
unit_comparePolicyKeys mnemonic index goldenPolicyKeyBech32 =
let
walletRootKey :: XPrv
walletRootKey =
Shelley.generateKeyFromSeed (SomeMnemonic mnemonic, Nothing) mempty
& getRawKey

walletPolicyKey :: XPrv
walletPolicyKey =
derivePolicyPrivateKey (mempty :: Passphrase pwd) walletRootKey index

walletPolicyKeyBytes :: BS.ByteString
walletPolicyKeyBytes = xprvToBytes walletPolicyKey

goldenPolicyKeyBytes :: BS.ByteString
goldenPolicyKeyBytes =
BL.toStrict $ unsafeBech32Decode goldenPolicyKeyBech32
in
walletPolicyKeyBytes `shouldBe` goldenPolicyKeyBytes

unit_comparePolicyKeyHashes
:: KnownNat n
=> Mnemonic n
-> Index 'Hardened 'PolicyK
-> Text
-> Expectation
unit_comparePolicyKeyHashes mnemonic index goldenPolicyKeyHashHex =
let
walletRootKey :: XPrv
walletRootKey =
Shelley.generateKeyFromSeed (SomeMnemonic mnemonic, Nothing) mempty
& getRawKey

walletPolicyData :: (ShelleyKey 'PolicyK XPrv, KeyHash)
walletPolicyData =
derivePolicyKeyAndHash
(mempty :: Passphrase pwd) (liftRawKey walletRootKey) index

walletPolicyKeyHashBytes :: BS.ByteString
walletPolicyKeyHashBytes = CA.digest $ snd walletPolicyData

goldenPolicyKeyHashBytes :: BS.ByteString
goldenPolicyKeyHashBytes =
unsafeFromHex (T.encodeUtf8 goldenPolicyKeyHashHex)

in
walletPolicyKeyHashBytes `shouldBe` goldenPolicyKeyHashBytes

goldenTestMnemonic :: Mnemonic 24
goldenTestMnemonic = unsafeMkMnemonic @24
[ "history", "stable", "illegal", "holiday"
, "push", "company", "aisle", "fly"
, "check", "dog", "earn", "admit"
, "smart", "rotate", "nation", "goddess"
, "fix", "wheat", "scissors", "across"
, "crazy", "actor", "fence", "baby"
]

instance Arbitrary XPrv where
arbitrary = unsafeXPrv . BS.pack <$> vector 128

Expand Down

0 comments on commit aaaabb1

Please sign in to comment.