Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/byron/cardano-wallet-byron.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ test-suite cardano-node-integration
, http-client
, http-types
, iohk-monitoring
, memory
, temporary
, text
build-tools:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,20 @@ import Cardano.Wallet.Api.Types
, WalletStyle (..)
)
import Cardano.Wallet.Primitive.AddressDerivation
( NetworkDiscriminant (..), PaymentAddress (..) )
( FromMnemonic (..)
, NetworkDiscriminant (..)
, Passphrase (..)
, PassphraseScheme (..)
, PaymentAddress (..)
, hex
, preparePassphrase
)
import Cardano.Wallet.Primitive.AddressDerivation.Byron
( ByronKey )
( ByronKey (..), generateKeyFromSeed )
import Cardano.Wallet.Primitive.AddressDerivation.Icarus
( IcarusKey )
import Cardano.Wallet.Primitive.Mnemonic
( entropyToMnemonic, genEntropy )
( entropyToMnemonic, genEntropy, mnemonicToText )
import Cardano.Wallet.Primitive.Types
( Address, Direction (..), TxStatus (..) )
import Control.Monad
Expand All @@ -46,12 +53,15 @@ import Numeric.Natural
( Natural )
import Test.Hspec
( SpecWith, describe, it, shouldBe )
import Test.Integration.Faucet
( nextWallet )
import Test.Integration.Framework.DSL
( Context
( Context (..)
, Headers (..)
, Payload (..)
, TxDescription (..)
, between
, emptyByronWalletFromXPrvWith
, emptyIcarusWallet
, emptyRandomWallet
, eventually
Expand All @@ -63,8 +73,10 @@ import Test.Integration.Framework.DSL
, fixtureIcarusWalletAddrs
, fixtureIcarusWalletWith
, fixturePassphrase
, fixturePassphraseEncrypted
, fixtureRandomWallet
, fixtureRandomWalletAddrs
, fixtureRandomWalletMws
, fixtureRandomWalletWith
, icarusAddresses
, json
Expand All @@ -86,6 +98,9 @@ import Test.Integration.Framework.TestData

import qualified Cardano.Wallet.Api.Link as Link
import qualified Data.Aeson as Aeson
import qualified Data.ByteArray as BA
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import qualified Network.HTTP.Types.Status as HTTP

spec
Expand All @@ -96,54 +111,59 @@ spec
, DecodeAddress n
)
=> SpecWith (Context t)
spec = describe "BYRON_TXS" $ do
-- Random → Random
scenario_TRANS_CREATE_01_02 @n fixtureRandomWallet
[ fixtureRandomWalletAddrs @n
]
spec = do
describe "BYRON_TXS" $ do
-- Random → Random
scenario_TRANS_CREATE_01_02 @n fixtureRandomWallet
[ fixtureRandomWalletAddrs @n
]

-- Random → [Random, Icarus]
scenario_TRANS_CREATE_01_02 @n fixtureRandomWallet
[ fixtureRandomWalletAddrs @n
, fixtureIcarusWalletAddrs @n
]
-- Random → [Random, Icarus]
scenario_TRANS_CREATE_01_02 @n fixtureRandomWallet
[ fixtureRandomWalletAddrs @n
, fixtureIcarusWalletAddrs @n
]

-- Icarus → Icarus
scenario_TRANS_CREATE_01_02 @n fixtureIcarusWallet
[ fixtureIcarusWalletAddrs @n
]
-- Icarus → Icarus
scenario_TRANS_CREATE_01_02 @n fixtureIcarusWallet
[ fixtureIcarusWalletAddrs @n
]

-- Icarus → [Icarus, Random]
scenario_TRANS_CREATE_01_02 @n fixtureRandomWallet
[ fixtureIcarusWalletAddrs @n
, fixtureRandomWalletAddrs @n
]
-- Icarus → [Icarus, Random]
scenario_TRANS_CREATE_01_02 @n fixtureRandomWallet
[ fixtureIcarusWalletAddrs @n
, fixtureRandomWalletAddrs @n
]

scenario_TRANS_CREATE_02x @n
scenario_TRANS_CREATE_02x @n

-- TRANS_CREATE_03 requires actually being able to compute exact fees, which
-- is not really possible w/ cardano-node. So, skipping.
-- TRANS_CREATE_03 requires actually being able to compute exact fees, which
-- is not really possible w/ cardano-node. So, skipping.

scenario_TRANS_CREATE_04a @n
scenario_TRANS_CREATE_04b @n
scenario_TRANS_CREATE_04c @n
scenario_TRANS_CREATE_04d @n
scenario_TRANS_CREATE_04a @n
scenario_TRANS_CREATE_04b @n
scenario_TRANS_CREATE_04c @n
scenario_TRANS_CREATE_04d @n

scenario_TRANS_CREATE_07 @n
scenario_TRANS_CREATE_07 @n

scenario_TRANS_ESTIMATE_01_02 @n fixtureRandomWallet
[ randomAddresses @n . entropyToMnemonic <$> genEntropy
]
scenario_TRANS_ESTIMATE_01_02 @n fixtureRandomWallet
[ randomAddresses @n . entropyToMnemonic <$> genEntropy
]

scenario_TRANS_ESTIMATE_01_02 @n fixtureIcarusWallet
[ icarusAddresses @n . entropyToMnemonic <$> genEntropy
, icarusAddresses @n . entropyToMnemonic <$> genEntropy
]
scenario_TRANS_ESTIMATE_01_02 @n fixtureIcarusWallet
[ icarusAddresses @n . entropyToMnemonic <$> genEntropy
, icarusAddresses @n . entropyToMnemonic <$> genEntropy
]

scenario_TRANS_ESTIMATE_04a @n
scenario_TRANS_ESTIMATE_04b @n
scenario_TRANS_ESTIMATE_04c @n
scenario_TRANS_ESTIMATE_04a @n
scenario_TRANS_ESTIMATE_04b @n
scenario_TRANS_ESTIMATE_04c @n

describe "BYRON_RESTORATION" $ do
scenario_RESTORE_01 @n fixtureRandomWallet
scenario_RESTORE_02 @n (fixtureRandomWalletAddrs @n)
scenario_RESTORE_03 @n (fixtureRandomWalletAddrs @n)
--
-- Scenarios
--
Expand Down Expand Up @@ -440,6 +460,192 @@ scenario_TRANS_CREATE_07 = it title $ \ctx -> do
where
title = "TRANS_CREATE_07 - Deleted wallet"

scenario_RESTORE_01
:: forall (n :: NetworkDiscriminant) t.
( DecodeAddress n
, EncodeAddress n
, PaymentAddress n ByronKey
)
=> (Context t -> IO ApiByronWallet)
-> SpecWith (Context t)
scenario_RESTORE_01 fixtureSource = it title $ \ctx -> do
-- SETUP
let amnt = 100_000 :: Natural
wSrc <- fixtureSource ctx
(wDest, payment, mnemonics) <- do
(wDest, mnemonics) <- fixtureRandomWalletMws ctx
let addrs = randomAddresses @n mnemonics
pure (wDest, mkPayment @n (head addrs) amnt, mnemonics)

-- ACTION
r <- postByronTransaction @n ctx wSrc [payment] fixturePassphrase

-- ASSERTIONS
let (feeMin, feeMax) = ctx ^. #_feeEstimator $ PaymentDescription
{ nInputs = 1
, nOutputs = 1
, nChanges = 1
}
verify r
[ expectResponseCode HTTP.status202
, expectField #amount $ between
( Quantity (feeMin + amnt)
, Quantity (feeMax + amnt)
)
, expectField #direction (`shouldBe` ApiT Outgoing)
, expectField #status (`shouldBe` ApiT Pending)
]

eventually "source balance decreases" $ do
rSrc <- request @ApiByronWallet ctx
(Link.getWallet @'Byron wSrc) Default Empty
verify rSrc
[ expectField (#balance . #available) $ between
( Quantity (faucetAmt - amnt - feeMax)
, Quantity (faucetAmt - amnt - feeMin)
)
]

eventually "destination balance increases" $ do
rDest <- request @ApiByronWallet ctx
(Link.getWallet @'Byron wDest) Default Empty
verify rDest
[ expectField (#balance . #available)
(`shouldBe` Quantity (faucetAmt + amnt))
]

-- ACTION
rd1 <- request
@ApiByronWallet ctx (Link.deleteWallet @'Byron wDest) Default Empty
expectResponseCode @IO HTTP.status204 rd1

-- MORE SETUP
let (Right seed) = fromMnemonic @'[12] (mnemonicToText mnemonics)
let rawPassd = Passphrase $ BA.convert $ T.encodeUtf8 fixturePassphrase
let rootXPrv = T.decodeUtf8 $ hex $ getKey $
generateKeyFromSeed seed
(preparePassphrase EncryptWithScrypt rawPassd)

-- ACTION
wDestRestored <- emptyByronWalletFromXPrvWith ctx "random"
("Byron Wallet Restored", rootXPrv, fixturePassphraseEncrypted)

-- ASSERTIONS
eventually "destination balance increases" $ do
rDest <- request @ApiByronWallet ctx
(Link.getWallet @'Byron wDestRestored) Default Empty
verify rDest
[ expectField (#balance . #available)
(`shouldBe` Quantity (faucetAmt + amnt))
]
where
title = "BYRON_RESTORE_01 - can restore recipient wallet from xprv"

scenario_RESTORE_02
:: forall (n :: NetworkDiscriminant) t.
( DecodeAddress n
, EncodeAddress n
)
=> (Context t -> IO (ApiByronWallet, [Address]))
-> SpecWith (Context t)
scenario_RESTORE_02 fixtureTarget = it title $ \ctx -> do
-- SETUP
let amnt = 100_000 :: Natural
mnemonics <- mnemonicToText <$> nextWallet @"random" (_faucet ctx)
let (Right seed) = fromMnemonic @'[12] mnemonics
let rawPassd = Passphrase $ BA.convert $ T.encodeUtf8 fixturePassphrase
let rootXPrv = T.decodeUtf8 $ hex $ getKey $
generateKeyFromSeed seed
(preparePassphrase EncryptWithScrypt rawPassd)
wSrc <- emptyByronWalletFromXPrvWith ctx "random"
("Byron Wallet Restored", rootXPrv, fixturePassphraseEncrypted)
(wDest, payment) <- do
(wDest, addrs) <- fixtureTarget ctx
pure (wDest, mkPayment @n (head addrs) amnt)

-- ACTION
r <- postByronTransaction @n ctx wSrc [payment] fixturePassphrase

-- ASSERTIONS
let (feeMin, feeMax) = ctx ^. #_feeEstimator $ PaymentDescription
{ nInputs = 1
, nOutputs = 1
, nChanges = 1
}
verify r
[ expectResponseCode HTTP.status202
, expectField #amount $ between
( Quantity (feeMin + amnt)
, Quantity (feeMax + amnt)
)
, expectField #direction (`shouldBe` ApiT Outgoing)
, expectField #status (`shouldBe` ApiT Pending)
]

eventually "source balance decreases" $ do
rSrc <- request @ApiByronWallet ctx
(Link.getWallet @'Byron wSrc) Default Empty
verify rSrc
[ expectField (#balance . #available) $ between
( Quantity (faucetAmt - amnt - feeMax)
, Quantity (faucetAmt - amnt - feeMin)
)
]

eventually "destination balance increases" $ do
rDest <- request @ApiByronWallet ctx
(Link.getWallet @'Byron wDest) Default Empty
verify rDest
[ expectField (#balance . #available)
(`shouldBe` Quantity (faucetAmt + amnt))
]
where
title = "BYRON_RESTORE_02 - can send tx from restored wallet from xprv"

scenario_RESTORE_03
:: forall (n :: NetworkDiscriminant) t.
( DecodeAddress n
, EncodeAddress n
)
=> (Context t -> IO (ApiByronWallet, [Address]))
-> SpecWith (Context t)
scenario_RESTORE_03 fixtureTarget = it title $ \ctx -> do
-- SETUP
mnemonics <- mnemonicToText <$> nextWallet @"random" (_faucet ctx)
let (Right seed) = fromMnemonic @'[12] mnemonics
let rawPassd = Passphrase $ BA.convert $ T.encodeUtf8 fixturePassphrase
let rootXPrv = T.decodeUtf8 $ hex $ getKey $
generateKeyFromSeed seed
(preparePassphrase EncryptWithScrypt rawPassd)
let passHashCorrupted = T.replicate 100 "0"
let amnt = 100_000 :: Natural
(_, payment) <- do
(wDest, addrs) <- fixtureTarget ctx
pure (wDest, mkPayment @n (head addrs) amnt)

-- ACTION
wSrc <- emptyByronWalletFromXPrvWith ctx "random"
("Byron Wallet Restored", rootXPrv, passHashCorrupted)
rSrc <- request @ApiByronWallet ctx
(Link.getWallet @'Byron wSrc) Default Empty
-- ASSERTIONS
verify rSrc
[ expectField (#balance . #available)
(`shouldBe` Quantity faucetAmt)
]

-- ACTION
r <- postByronTransaction @n ctx wSrc [payment] fixturePassphrase
-- ASSERTIONS
verify r
[ expectResponseCode HTTP.status403
, expectErrorMessage errMsg403WrongPass
]

where
title = "BYRON_RESTORE_03 - restoring wallet from corrupted hash gives\
\ proper balance but sending tx fails"


--
-- More Elaborated Fixtures
Expand Down
1 change: 0 additions & 1 deletion lib/core-integration/cardano-wallet-core-integration.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ library
, http-api-data
, http-client
, http-types
, memory
, OddWord
, process
, retry
Expand Down
Loading