Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display serialised tx endpoint #2977

Merged
merged 31 commits into from
Nov 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
7bda41c
add first version of endpoint in swagger
paweljakubas Oct 14, 2021
9728b09
add basic types
paweljakubas Oct 14, 2021
c007ecc
change inputs to be more general in ApiDecodedTransaction
paweljakubas Oct 15, 2021
c88c524
deal with colls, inps and outs
paweljakubas Oct 15, 2021
d6b999c
deal with withdrawals
paweljakubas Oct 15, 2021
c9f4b34
deal with validity interval
paweljakubas Oct 15, 2021
df4f127
add lookupTxIns in Cardano.Wallet
paweljakubas Oct 15, 2021
bd277de
use lookupTxIns for inps and collaterals in decodeTransaction
paweljakubas Oct 15, 2021
2d46976
improve handling inputs
paweljakubas Oct 18, 2021
ab51465
generalize withdrawals in swagger
paweljakubas Oct 18, 2021
fd34bde
deal with withdrawals
paweljakubas Oct 18, 2021
16d8bac
first integration test for decode tx
paweljakubas Oct 19, 2021
802639a
rm unneeded errors from ErrBalanceTx
paweljakubas Oct 19, 2021
ef01d0f
unpend single output tx test
paweljakubas Oct 19, 2021
06bc139
fix address comparing in lookupTxIns
paweljakubas Oct 20, 2021
12b1125
finish integration test for inputs
paweljakubas Oct 20, 2021
4a9b08c
amend ExternalInput definition - we do not know amount from cbor here
paweljakubas Oct 20, 2021
6b1dc15
generalize outputs in swagger
paweljakubas Oct 20, 2021
2d5d30e
add generalized output type
paweljakubas Oct 20, 2021
6b895e7
first impl of txout enricher
paweljakubas Oct 21, 2021
ef329b1
add more tests for outputs of wallet in integration testing
paweljakubas Oct 21, 2021
c23c889
add change addr detection
paweljakubas Oct 22, 2021
428e9ba
use pool gap to determine number of candidate change addresses
paweljakubas Oct 25, 2021
92eb97c
add integration test for metadata
paweljakubas Oct 25, 2021
815761a
add integration testing for withdrawals
paweljakubas Oct 25, 2021
0ae8f4f
adjust core unit tests
paweljakubas Oct 25, 2021
5aa90dd
fixing schema/json things
paweljakubas Oct 27, 2021
1746cac
use normaliseDelegationAddress
paweljakubas Oct 28, 2021
124c0ab
Simplify lookupTx{Ins,Outs} using isOurs
Anviking Oct 29, 2021
582fade
Remove distinction between amount and amount_incoming
Anviking Nov 1, 2021
f7977b8
Tweak integration expectations
Anviking Nov 2, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ module Test.Integration.Framework.TestData
, errMsg403TemplateInvalidDuplicateXPub
, errMsg403TemplateInvalidScript
, errMsg403InvalidConstructTx
, errMsg403transactionAlreadyBalanced
) where

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

errMsg403transactionAlreadyBalanced :: String
errMsg403transactionAlreadyBalanced =
"The transaction is already balanced. Please send a transaction that requires more inputs/outputs to be picked to be balanced."

--------------------------------------------------------------------------------
-- Transaction metadata
--------------------------------------------------------------------------------
Expand Down

Large diffs are not rendered by default.

61 changes: 56 additions & 5 deletions lib/core/src/Cardano/Wallet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,12 @@ module Cardano.Wallet
, importRandomAddresses
, listAddresses
, normalizeDelegationAddress
, lookupTxIns
, lookupTxOuts
, ErrCreateRandomAddress(..)
, ErrImportRandomAddress(..)
, ErrImportAddress(..)
, ErrDecodeTx (..)

-- ** Payment
, getTxExpiry
Expand Down Expand Up @@ -457,7 +460,7 @@ import Control.Monad.Trans.Except
import Control.Monad.Trans.Maybe
( MaybeT (..), maybeToExceptT )
import Control.Monad.Trans.State
( runState, state )
( evalState, runState, state )
import Control.Tracer
( Tracer, contramap, traceWith )
import Crypto.Hash
Expand Down Expand Up @@ -559,7 +562,6 @@ import qualified Data.Set as Set
import qualified Data.Text as T
import qualified Data.Vector as V


-- $Development
-- __Naming Conventions__
--
Expand Down Expand Up @@ -1169,6 +1171,52 @@ manageRewardBalance _ ctx wid = db & \DBLayer{..} -> do
Address
-------------------------------------------------------------------------------}

lookupTxIns
:: forall ctx s k .
( HasDBLayer IO s k ctx
, IsOurs s Address
)
=> ctx
-> WalletId
-> [TxIn]
-> ExceptT ErrDecodeTx IO [(TxIn, Maybe (TxOut, NonEmpty DerivationIndex))]
lookupTxIns ctx wid txins = db & \DBLayer{..} -> do
cp <- mapExceptT atomically
$ withExceptT ErrDecodeTxNoSuchWallet
$ withNoSuchWallet wid
$ readCheckpoint wid
pure $ map (\i -> (i, lookupTxIn cp i)) txins
where
db = ctx ^. dbLayer @IO @s @k
lookupTxIn :: Wallet s -> TxIn -> Maybe (TxOut, NonEmpty DerivationIndex)
lookupTxIn cp txIn = do
out@(TxOut addr _) <- UTxO.lookup txIn (totalUTxO mempty cp)
path <- fst $ isOurs addr (getState cp)
return (out, path)

lookupTxOuts
:: forall ctx s k .
( HasDBLayer IO s k ctx
, IsOurs s Address
)
=> ctx
-> WalletId
-> [TxOut]
-> ExceptT ErrDecodeTx IO [(TxOut, Maybe (NonEmpty DerivationIndex))]
lookupTxOuts ctx wid txouts = db & \DBLayer{..} -> do
cp <- mapExceptT atomically
$ withExceptT ErrDecodeTxNoSuchWallet
$ withNoSuchWallet wid
$ readCheckpoint wid
-- NOTE: We evolve the state (in practice an address pool) as we loop
-- through the outputs, but we don't consider pending transactions.
-- /Theoretically/ the outputs might only be discoverable after discovering
-- outputs other pending transactions.
pure $ flip evalState (getState cp) $ forM txouts $ \out@(TxOut addr _) -> do
(out,) <$> state (isOurs addr)
where
db = ctx ^. dbLayer @IO @s @k

-- | List all addresses of a wallet with their metadata. Addresses
-- are ordered from the most-recently-discovered to the oldest known.
listAddresses
Expand Down Expand Up @@ -2794,11 +2842,9 @@ data ErrSignPayment

-- | Errors that can occur when balancing transaction.
data ErrBalanceTx
= ErrBalanceTxTxAlreadyBalanced
| ErrBalanceTxUpdateError ErrUpdateSealedTx
= ErrBalanceTxUpdateError ErrUpdateSealedTx
| ErrBalanceTxSelectAssets ErrSelectAssets
| ErrBalanceTxAssignRedeemers ErrAssignRedeemers
| ErrBalanceTxNotImplemented
deriving (Show, Eq)

-- | Errors that can occur when constructing an unsigned transaction.
Expand All @@ -2825,6 +2871,11 @@ data ErrWitnessTx
| ErrWitnessTxIncorrectTTL PastHorizonException
deriving (Show, Eq)

-- | Errors that can occur when decoding a transaction.
newtype ErrDecodeTx
= ErrDecodeTxNoSuchWallet ErrNoSuchWallet
deriving (Show, Eq)

-- | Errors that can occur when submitting a signed transaction to the network.
data ErrSubmitTx
= ErrSubmitTxNetwork ErrPostTx
Expand Down
10 changes: 10 additions & 0 deletions lib/core/src/Cardano/Wallet/Api.hs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ module Cardano.Wallet.Api
, CreateTransactionOld
, PostTransactionFeeOld
, BalanceTransaction
, DecodeTransaction

, StakePools
, ListStakePools
Expand Down Expand Up @@ -177,6 +178,7 @@ import Cardano.Wallet.Api.Types
, ApiCoinSelectionT
, ApiConstructTransactionDataT
, ApiConstructTransactionT
, ApiDecodedTransactionT
, ApiFee
, ApiHealthCheck
, ApiMaintenanceAction
Expand Down Expand Up @@ -528,6 +530,7 @@ type ShelleyTransactions n =
:<|> CreateTransactionOld n
:<|> PostTransactionFeeOld n
:<|> BalanceTransaction n
:<|> DecodeTransaction n

-- | https://input-output-hk.github.io/cardano-wallet/api/#operation/constructTransaction
type ConstructTransaction n = "wallets"
Expand Down Expand Up @@ -588,6 +591,13 @@ type BalanceTransaction n = "wallets"
:> ReqBody '[JSON] (ApiBalanceTransactionPostDataT n)
:> PostAccepted '[JSON] ApiSerialisedTransaction

-- | https://input-output-hk.github.io/cardano-wallet/api/#operation/decodeTransaction
type DecodeTransaction n = "wallets"
:> Capture "walletId" (ApiT WalletId)
:> "transactions-decode"
:> ReqBody '[JSON] ApiSerialisedTransaction
:> PostAccepted '[JSON] (ApiDecodedTransactionT n)

{-------------------------------------------------------------------------------
Shelley Migrations

Expand Down
9 changes: 9 additions & 0 deletions lib/core/src/Cardano/Wallet/Api/Client.hs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ import Cardano.Wallet.Api.Types
, ApiCoinSelectionT
, ApiConstructTransactionDataT
, ApiConstructTransactionT
, ApiDecodedTransactionT
, ApiFee
, ApiNetworkClock
, ApiNetworkInformation (..)
Expand Down Expand Up @@ -194,6 +195,10 @@ data TransactionClient = TransactionClient
:: ApiT WalletId
-> ApiBalanceTransactionPostDataT Aeson.Value
-> ClientM ApiSerialisedTransaction
, decodeTransaction
:: ApiT WalletId
-> ApiSerialisedTransaction
-> ClientM (ApiDecodedTransactionT Aeson.Value)
}

data AddressClient = AddressClient
Expand Down Expand Up @@ -310,6 +315,7 @@ transactionClient =
:<|> _postTransaction
:<|> _postTransactionFee
:<|> _balanceTransaction
:<|> _decodeTransaction
= client (Proxy @("v2" :> (ShelleyTransactions Aeson.Value)))

_postExternalTransaction
Expand All @@ -325,6 +331,7 @@ transactionClient =
, getTransaction = _getTransaction
, constructTransaction = _constructTransaction
, balanceTransaction = _balanceTransaction
, decodeTransaction = _decodeTransaction
}

fromSerialisedTx :: ApiBytesT base SerialisedTx -> ApiT SealedTx
Expand Down Expand Up @@ -357,6 +364,7 @@ byronTransactionClient =
, getTransaction = _getTransaction
, constructTransaction = _constructTransaction
, balanceTransaction = error "balance transaction endpoint not supported for byron"
, decodeTransaction = error "decode transaction endpoint not supported for byron"
}

-- | Produces an 'AddressClient n' working against the /wallets API
Expand Down Expand Up @@ -458,3 +466,4 @@ type instance PostTransactionOldDataT Aeson.Value = Aeson.Value
type instance PostTransactionFeeOldDataT Aeson.Value = Aeson.Value
type instance ApiPutAddressesDataT Aeson.Value = Aeson.Value
type instance ApiBalanceTransactionPostDataT Aeson.Value = Aeson.Value
type instance ApiDecodedTransactionT Aeson.Value = Aeson.Value
16 changes: 16 additions & 0 deletions lib/core/src/Cardano/Wallet/Api/Link.hs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ module Cardano.Wallet.Api.Link
, createUnsignedTransaction
, signTransaction
, balanceTransaction
, decodeTransaction

-- * StakePools
, listStakePools
Expand Down Expand Up @@ -681,6 +682,21 @@ balanceTransaction w = discriminate @style
where
wid = w ^. typed @(ApiT WalletId)

decodeTransaction
:: forall style w.
( HasCallStack
, HasType (ApiT WalletId) w
, Discriminate style
)
=> w
-> (Method, Text)
decodeTransaction w = discriminate @style
(endpoint @(Api.DecodeTransaction Net) (wid &))
(notSupported "Byron")
(notSupported "Shared")
where
wid = w ^. typed @(ApiT WalletId)

--
-- Stake Pools
--
Expand Down
Loading