Skip to content

Commit

Permalink
extend integration scenario about coin-selection with some assertions…
Browse files Browse the repository at this point in the history
… about the derivation path.
  • Loading branch information
KtorZ committed Oct 9, 2020
1 parent a0c52cb commit 2755db9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import Cardano.Wallet.Api.Types
, ApiAddress
, ApiByronWallet
, ApiCoinSelection
, ApiCoinSelectionInput (derivationPath)
, ApiNetworkInformation
, ApiT (..)
, ApiTransaction
Expand All @@ -34,23 +35,30 @@ import Cardano.Wallet.Api.Types
, WalletStyle (..)
)
import Cardano.Wallet.Primitive.AddressDerivation
( PassphraseMaxLength (..), PassphraseMinLength (..), PaymentAddress )
( DerivationType (..)
, Index (..)
, PassphraseMaxLength (..)
, PassphraseMinLength (..)
, PaymentAddress
)
import Cardano.Wallet.Primitive.AddressDerivation.Byron
( ByronKey )
import Cardano.Wallet.Primitive.AddressDerivation.Icarus
( IcarusKey )
import Cardano.Wallet.Primitive.AddressDerivation.Shelley
( ShelleyKey )
import Cardano.Wallet.Primitive.AddressDiscovery.Sequential
( AddressPoolGap (..) )
( AddressPoolGap (..), coinTypeAda, purposeCIP1852 )
import Cardano.Wallet.Primitive.SyncProgress
( SyncProgress (..) )
import Cardano.Wallet.Primitive.Types
( walletNameMaxLength, walletNameMinLength )
( DerivationIndex (..), walletNameMaxLength, walletNameMinLength )
import Control.Monad
( forM_ )
import Data.Generics.Internal.VL.Lens
( view, (^.) )
import Data.List
( isPrefixOf )
import Data.List.NonEmpty
( NonEmpty ((:|)) )
import Data.Proxy
Expand Down Expand Up @@ -915,9 +923,18 @@ spec = describe "SHELLEY_WALLETS" $ do
targetAddress : _ <- fmap (view #id) <$> listAddresses @n ctx target
let amount = Quantity minUTxOValue
let payment = AddressAmount targetAddress amount
let hasValidDerivationPath input =
( length (derivationPath input) == 5 )
&&
( [ ApiT $ DerivationIndex $ getIndex purposeCIP1852
, ApiT $ DerivationIndex $ getIndex coinTypeAda
, ApiT $ DerivationIndex $ getIndex @'Hardened minBound
] `isPrefixOf` NE.toList (derivationPath input)
)
selectCoins @_ @'Shelley ctx source (payment :| []) >>= flip verify
[ expectResponseCode HTTP.status200
, expectField #inputs (`shouldSatisfy` (not . null))
, expectField #inputs (`shouldSatisfy` all hasValidDerivationPath)
, expectField #outputs (`shouldSatisfy` ((> 1) . length))
, expectField #outputs (`shouldSatisfy` (payment `elem`))
]
Expand Down
17 changes: 16 additions & 1 deletion lib/core/src/Cardano/Wallet/Primitive/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1166,7 +1166,22 @@ instance Buildable AddressState where

instance NFData AddressState

-- | A thin wrapper around derivation indexes.
-- | A thin wrapper around derivation indexes. This can be used to represent
-- derivation path as homogeneous lists of 'DerivationIndex'. This is slightly
-- more convenient than having to carry heterogeneous lists of 'Index depth type'
-- and works fine because:
--
-- 1. The 'depth' matters not because what the depth captures is actually the
-- position of the index in that list. It makes sense to carry at the type
-- level when manipulating standalone indexes to avoid mistakes, but when
-- treating them as a part of a list it is redundant.
--
-- 2. The derivationType is captured by representing indexes as plain Word32.
-- The Soft / Hardened notation is for easing human-readability but in the
-- end, a soft index is simply a value < 2^31, whereas a "hardened" index is
-- simply a value >= 2^31. Therefore, instead of representing indexes as
-- derivationType + relative index within 0 and 2^31, we can represent them
-- as just an index between 0 and 2^32, which is what DerivationIndex does.
newtype DerivationIndex
= DerivationIndex Word32
deriving (Show, Eq, Ord, Generic)
Expand Down

0 comments on commit 2755db9

Please sign in to comment.