Skip to content

Commit

Permalink
implement foreign wallet functionality in submitTransaction
Browse files Browse the repository at this point in the history
  • Loading branch information
paweljakubas committed Jan 17, 2022
1 parent 7c1e21d commit 03d51ca
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
Expand Up @@ -101,6 +101,7 @@ module Test.Integration.Framework.TestData
, errMsg403TemplateInvalidDuplicateXPub
, errMsg403TemplateInvalidScript
, errMsg403InvalidConstructTx
, errMsg403ForeignTransaction
) where

import Prelude
Expand Down Expand Up @@ -637,6 +638,13 @@ errMsg400ScriptNotUniformRoles :: String
errMsg400ScriptNotUniformRoles =
"All keys of a script must have the same role: either payment or delegation."

errMsg403ForeignTransaction :: String
errMsg403ForeignTransaction = mconcat
[ "The transaction to be submitted is foreign to the current wallet "
, "and cannot be sent. Submit a transaction that has either input, output "
, "or withdrawal belonging to the wallet."
]

--------------------------------------------------------------------------------
-- Transaction metadata
--------------------------------------------------------------------------------
Expand Down
Expand Up @@ -174,6 +174,7 @@ import Test.Integration.Framework.DSL
import Test.Integration.Framework.TestData
( errMsg403Collateral
, errMsg403Fee
, errMsg403ForeignTransaction
, errMsg403InvalidConstructTx
, errMsg403MinUTxOValue
, errMsg403NotDelegating
Expand Down Expand Up @@ -2155,7 +2156,8 @@ spec = describe "NEW_SHELLEY_TRANSACTIONS" $ do
]
forM_ scenarios $ \(title, foreignWallet) -> it title $ \ctx -> runResourceT $ do
wa <- fixtureWallet ctx
wb <- foreignWallet ctx
wb <- fixtureWallet ctx
wF <- foreignWallet ctx

-- Construct tx
payload <- mkTxPayload ctx wb $ minUTxOValue (_mainEra ctx)
Expand All @@ -2172,12 +2174,11 @@ spec = describe "NEW_SHELLEY_TRANSACTIONS" $ do
signedTx <- getFromResponse Prelude.id <$>
request @ApiSerialisedTransaction ctx signEndpoint Default toSign

-- Submit tx (from wb)
submittedTx <- submitTxWithWid ctx wb signedTx
-- Submit tx (from wF)
submittedTx <- submitTxWithWid ctx wF signedTx
verify submittedTx
[ expectResponseCode HTTP.status403
, expectErrorMessage "Transaction cannot be submitted as it is foreign to\
\ this wallet. Please submit it from the wallet it belongs to."
, expectErrorMessage errMsg403ForeignTransaction
]

describe "TRANS_NEW_SUBMIT_02 - Submitting on foreign Byron wallet is forbidden" $ do
Expand Down
17 changes: 16 additions & 1 deletion lib/core/src/Cardano/Wallet/Api/Server.hs
Expand Up @@ -2349,6 +2349,8 @@ submitTransaction
submitTransaction ctx apiw@(ApiT wid) apitx@(ApiSerialisedTransaction (ApiT sealedTx)) = do
ttl <- liftIO $ W.getTxExpiry ti Nothing
apiDecoded <- decodeTransaction @_ @s @k @n ctx apiw apitx
when (isForeign apiDecoded) $
liftHandler $ throwE ErrSubmitTransactionForeignWallet
let ourOuts = getOurOuts apiDecoded
let ourInps = getOurInps apiDecoded

Expand All @@ -2370,7 +2372,6 @@ submitTransaction ctx apiw@(ApiT wid) apitx@(ApiSerialisedTransaction (ApiT seal
nl = ctx ^. networkLayer
ti = timeInterpreter nl


isOutOurs (WalletOutput _) = True
isOutOurs _ = False
toTxOut (WalletOutput (ApiWalletOutput (ApiT addr, _) (Quantity amt) (ApiT tmap) _)) =
Expand Down Expand Up @@ -2399,6 +2400,20 @@ submitTransaction ctx apiw@(ApiT wid) apitx@(ApiSerialisedTransaction (ApiT seal
let generalInps = apiDecodedTx ^. #inputs
in map toTxInp $ filter isInpOurs generalInps

isForeign apiDecodedTx =
let generalInps = apiDecodedTx ^. #inputs
generalWdrls = apiDecodedTx ^. #withdrawals
generalOuts = apiDecodedTx ^. #outputs
isInpForeign (WalletInput _) = False
isInpForeign _ = True
isOutForeign (WalletOutput _) = False
isOutForeign _ = True
isWdrlForeign (ApiWithdrawalGeneral _ _ context) = context == External
in
all isInpForeign generalInps &&
all isOutForeign generalOuts &&
all isWdrlForeign generalWdrls

joinStakePool
:: forall ctx s n k.
( ctx ~ ApiLayer s k
Expand Down

0 comments on commit 03d51ca

Please sign in to comment.