Skip to content

Commit

Permalink
Updates
Browse files Browse the repository at this point in the history
  • Loading branch information
rvl committed May 11, 2021
1 parent 78da2db commit 46491fc
Show file tree
Hide file tree
Showing 10 changed files with 188 additions and 51 deletions.
7 changes: 4 additions & 3 deletions lib/cli/src/Cardano/CLI.hs
Expand Up @@ -149,7 +149,6 @@ import Cardano.Wallet.Api.Types
, ApiByronWallet
, ApiMnemonicT (..)
, ApiPostRandomAddressData (..)
, ApiSerialisedTransaction (..)
, ApiT (..)
, ApiTxId (ApiTxId)
, ApiTxMetadata (..)
Expand Down Expand Up @@ -191,6 +190,8 @@ import Cardano.Wallet.Primitive.Types.Coin
( Coin (..) )
import Cardano.Wallet.Primitive.Types.Hash
( Hash (..) )
import Cardano.Wallet.Primitive.Types.Tx
( SerialisedTx (..) )
import Cardano.Wallet.Version
( gitRevision, showFullVersion, version )
import Control.Applicative
Expand Down Expand Up @@ -860,7 +861,7 @@ cmdTransactionList mkTxClient =
-- | Arguments for 'transaction submit' command
data TransactionSubmitArgs = TransactionSubmitArgs
{ _port :: Port "Wallet"
, _payload :: ApiSerialisedTransaction
, _payload :: ApiT SerialisedTx
}

cmdTransactionSubmit
Expand Down Expand Up @@ -1431,7 +1432,7 @@ accPubKeyArgument = argumentT $ mempty
<> help "64-byte (128-character) hex-encoded public account key."

-- | <payload=BINARY_BLOB>
transactionSubmitPayloadArgument :: Parser ApiSerialisedTransaction
transactionSubmitPayloadArgument :: Parser (ApiT SerialisedTx)
transactionSubmitPayloadArgument = argumentT $ mempty
<> metavar "BINARY_BLOB"
<> help "hex-encoded binary blob of externally-signed transaction."
Expand Down
24 changes: 21 additions & 3 deletions lib/core/src/Cardano/Wallet.hs
Expand Up @@ -111,8 +111,9 @@ module Cardano.Wallet
, selectAssetsNoOutputs
, assignChangeAddresses
, selectionToUnsignedTx
, signTransaction
, buildAndSignTransaction
, signTransaction
, joinSerialisedTxParts
, ErrSelectAssets(..)
, ErrSignPayment (..)
, ErrNotASequentialWallet (..)
Expand Down Expand Up @@ -334,6 +335,7 @@ import Cardano.Wallet.Primitive.Types.Tx
( Direction (..)
, LocalTxSubmissionStatus
, SealedTx (..)
, SerialisedTxParts (..)
, TransactionInfo (..)
, Tx
, TxChange (..)
Expand Down Expand Up @@ -1441,7 +1443,7 @@ signTransaction
-- ^ Reward account derived from the root key (or somewhere else).
-> Passphrase "raw"
-> ByteString
-> ExceptT ErrSignPayment IO SealedTx
-> ExceptT ErrSignPayment IO SerialisedTxParts
signTransaction ctx wid mkRwdAcct pwd txBody = db & \DBLayer{..} -> do
era <- liftIO $ currentNodeEra nl
let _decoded = decodeSignedTx tl era txBody
Expand All @@ -1456,13 +1458,29 @@ signTransaction ctx wid mkRwdAcct pwd txBody = db & \DBLayer{..} -> do
let _rewardAcnt = mkRwdAcct (xprv, pwdP)
-- withExceptT ErrSignPaymentMkTx $ ExceptT $ pure $
-- witnessTransaction tl rewardAcnt keyFrom txBody
pure $ SealedTx txBody
pure $ SerialisedTxParts txBody []

where
db = ctx ^. dbLayer @IO @s @k
tl = ctx ^. transactionLayer @k
nl = ctx ^. networkLayer

joinSerialisedTxParts
:: forall ctx k.
( HasTransactionLayer k ctx
, HasNetworkLayer IO ctx
)
=> ctx
-> SerialisedTxParts
-> IO ByteString
joinSerialisedTxParts ctx (SerialisedTxParts txBody _wits) = do
_era <- currentNodeEra nl
-- TODO: ADP-919 encode full tx
pure txBody
where
nl = ctx ^. networkLayer
_tl = ctx ^. transactionLayer @k

-- | Produce witnesses and construct a transaction from a given
-- selection. Requires the encryption passphrase in order to decrypt
-- the root private key. Note that this doesn't broadcast the
Expand Down
11 changes: 7 additions & 4 deletions lib/core/src/Cardano/Wallet/Api.hs
Expand Up @@ -163,7 +163,6 @@ import Cardano.Wallet.Api.Types
, ApiPostRandomAddressData
, ApiPutAddressesDataT
, ApiSelectCoinsDataT
, ApiSerialisedTransaction
, ApiSharedWallet
, ApiSharedWalletPatchData
, ApiSharedWalletPostData
Expand Down Expand Up @@ -211,6 +210,8 @@ import Cardano.Wallet.Primitive.Types.Coin
( Coin (..) )
import Cardano.Wallet.Primitive.Types.TokenPolicy
( TokenName, TokenPolicyId )
import Cardano.Wallet.Primitive.Types.Tx
( SerialisedTx, SerialisedTxParts )
import Cardano.Wallet.Registry
( HasWorkerCtx (..), WorkerLog, WorkerRegistry )
import Cardano.Wallet.TokenMetadata
Expand Down Expand Up @@ -468,7 +469,8 @@ type SignTransaction n = "wallets"
:> "transactions"
:> "sign"
:> ReqBody '[JSON] PostSignTransactionData
:> PostAccepted '[JSON, OctetStream] ApiSerialisedTransaction
:> (PostAccepted '[JSON, OctetStream] (ApiT SerialisedTx) :<|>
PostAccepted '[JSON] (ApiT SerialisedTxParts))

-- | https://input-output-hk.github.io/cardano-wallet/api/#operation/postTransaction
type CreateTransactionOld n = "wallets"
Expand Down Expand Up @@ -763,7 +765,8 @@ type SignByronTransaction n = "byron-wallets"
:> "transactions"
:> "sign"
:> ReqBody '[JSON] PostSignTransactionData
:> PostAccepted '[JSON, OctetStream] ApiSerialisedTransaction
:> (PostAccepted '[JSON, OctetStream] (ApiT SerialisedTx) :<|>
PostAccepted '[JSON] (ApiT SerialisedTxParts))

-- | https://input-output-hk.github.io/cardano-wallet/api/#operation/postByronTransaction
type CreateByronTransactionOld n = "byron-wallets"
Expand Down Expand Up @@ -917,7 +920,7 @@ type Proxy_ =
-- | https://input-output-hk.github.io/cardano-wallet/api/#operation/postExternalTransaction
type PostExternalTransaction = "proxy"
:> "transactions"
:> ReqBody '[OctetStream] ApiSerialisedTransaction
:> ReqBody '[OctetStream] (ApiT SerialisedTx)
:> PostAccepted '[JSON] ApiTxId

{-------------------------------------------------------------------------------
Expand Down
27 changes: 22 additions & 5 deletions lib/core/src/Cardano/Wallet/Api/Client.hs
Expand Up @@ -71,7 +71,6 @@ import Cardano.Wallet.Api.Types
, ApiPostRandomAddressData
, ApiPutAddressesDataT
, ApiSelectCoinsDataT
, ApiSerialisedTransaction
, ApiT (..)
, ApiTransactionT
, ApiTxId (..)
Expand All @@ -92,6 +91,8 @@ import Cardano.Wallet.Primitive.Types.Address
( AddressState )
import Cardano.Wallet.Primitive.Types.Coin
( Coin (..) )
import Cardano.Wallet.Primitive.Types.Tx
( SerialisedTx, SerialisedTxParts )
import Control.Monad
( void )
import Data.Coerce
Expand Down Expand Up @@ -154,7 +155,11 @@ data TransactionClient = TransactionClient
, postSignTransaction
:: ApiT WalletId
-> PostSignTransactionData
-> ClientM ApiSerialisedTransaction
-> ClientM (ApiT SerialisedTx)
, postSignTransactionParts
:: ApiT WalletId
-> PostSignTransactionData
-> ClientM (ApiT SerialisedTxParts)
, postTransaction
:: ApiT WalletId
-> PostTransactionOldDataT Aeson.Value
Expand All @@ -164,7 +169,7 @@ data TransactionClient = TransactionClient
-> PostTransactionFeeOldDataT Aeson.Value
-> ClientM ApiFee
, postExternalTransaction
:: ApiSerialisedTransaction
:: ApiT SerialisedTx
-> ClientM ApiTxId
, deleteTransaction
:: ApiT WalletId
Expand Down Expand Up @@ -278,20 +283,26 @@ transactionClient
:: TransactionClient
transactionClient =
let
_postSignTransaction
_postSignTransactions
:<|> _postTransaction
:<|> _listTransactions
:<|> _postTransactionFee
:<|> _deleteTransaction
:<|> _getTransaction
= client (Proxy @("v2" :> (Transactions Aeson.Value)))

_postSignTransaction wid p = ep
where ep :<|> _ = _postSignTransactions wid p
_postSignTransactionParts wid p = ep
where _ :<|> ep = _postSignTransactions wid p

_postExternalTransaction
= client (Proxy @("v2" :> Proxy_))
in
TransactionClient
{ listTransactions = (`_listTransactions` Nothing)
, postSignTransaction = _postSignTransaction
, postSignTransactionParts = _postSignTransactionParts
, postTransaction = _postTransaction
, postTransactionFee = _postTransactionFee
, postExternalTransaction = _postExternalTransaction
Expand All @@ -304,20 +315,26 @@ byronTransactionClient
:: TransactionClient
byronTransactionClient =
let
_postSignTransaction
_postSignTransactions
:<|> _postTransaction
:<|> _listTransactions
:<|> _postTransactionFee
:<|> _deleteTransaction
:<|> _getTransaction
= client (Proxy @("v2" :> (ByronTransactions Aeson.Value)))

_postSignTransaction wid p = ep
where ep :<|> _ = _postSignTransactions wid p
_postSignTransactionParts wid p = ep
where _ :<|> ep = _postSignTransactions wid p

_postExternalTransaction
= client (Proxy @("v2" :> Proxy_))

in TransactionClient
{ listTransactions = _listTransactions
, postSignTransaction = _postSignTransaction
, postSignTransactionParts = _postSignTransactionParts
, postTransaction = _postTransaction
, postTransactionFee = _postTransactionFee
, postExternalTransaction = _postExternalTransaction
Expand Down
60 changes: 49 additions & 11 deletions lib/core/src/Cardano/Wallet/Api/Server.hs
Expand Up @@ -65,6 +65,7 @@ module Cardano.Wallet.Api.Server
, postRandomWallet
, postRandomWalletFromXPrv
, postSignTransaction
, postSignTransactionParts
, postTransactionOld
, postTransactionFeeOld
, postTrezorWallet
Expand Down Expand Up @@ -374,7 +375,9 @@ import Cardano.Wallet.Primitive.Types.TokenMap
import Cardano.Wallet.Primitive.Types.TokenPolicy
( TokenName (..), TokenPolicyId (..), nullTokenName )
import Cardano.Wallet.Primitive.Types.Tx
( TransactionInfo (TransactionInfo)
( SerialisedTx (..)
, SerialisedTxParts (..)
, TransactionInfo (TransactionInfo)
, Tx (..)
, TxChange (..)
, TxIn (..)
Expand Down Expand Up @@ -420,6 +423,8 @@ import Control.Tracer
( Tracer, contramap )
import Data.Aeson
( (.=) )
import Data.Bifunctor
( first )
import Data.ByteString
( ByteString )
import Data.Coerce
Expand Down Expand Up @@ -1701,16 +1706,49 @@ postSignTransaction
=> ctx
-> ApiT WalletId
-> PostSignTransactionData
-> Handler ApiSerialisedTransaction
postSignTransaction ctx (ApiT wid) body = do
-> Handler (ApiT SerialisedTx)
postSignTransaction ctx wid body = fmap (ApiT . SerialisedTx) $
postSignTransactionBase @_ @s @k @n ctx wid body
>>= liftIO . W.joinSerialisedTxParts @_ @k ctx

postSignTransactionParts
:: forall ctx s k (n :: NetworkDiscriminant).
( ctx ~ ApiLayer s k
, Bounded (Index (AddressIndexDerivationType k) 'AddressK)
, HardDerivation k
, IsOwned s k
, Typeable n
, Typeable s
, WalletKey k
)
=> ctx
-> ApiT WalletId
-> PostSignTransactionData
-> Handler (ApiT SerialisedTxParts)
postSignTransactionParts ctx wid = fmap ApiT .
postSignTransactionBase @_ @s @k @n ctx wid

postSignTransactionBase
:: forall ctx s k (n :: NetworkDiscriminant).
( ctx ~ ApiLayer s k
, IsOwned s k
, WalletKey k
)
=> ctx
-> ApiT WalletId
-> PostSignTransactionData
-> Handler SerialisedTxParts
postSignTransactionBase ctx (ApiT wid) body = do
let pwd = coerce $ body ^. #passphrase . #getApiT
let txBody = body ^. #txBody . #payload
let txBody = case body ^. #transaction of
ApiSerialisedTransaction (ApiT (SerialisedTx bytes)) -> bytes
ApiSerialisedTransactionParts (ApiT (SerialisedTxParts bytes _wits)) -> bytes

(_, mkRwdAcct) <- mkRewardAccountBuilder @_ @s @_ @n ctx wid Nothing
-- (_, mkRwdAcct) <- mkRewardAccountBuilder @_ @s @_ @n ctx wid Nothing
let stubRwdAcct = first getRawKey

W.SealedTx sealedTx <- withWorkerCtx ctx wid liftE liftE $ \wrk ->
liftHandler $ W.signTransaction @_ @s @k wrk wid mkRwdAcct pwd txBody
pure $ ApiSerialisedTransaction sealedTx
withWorkerCtx ctx wid liftE liftE $ \wrk ->
liftHandler $ W.signTransaction @_ @s @k wrk wid stubRwdAcct pwd txBody

postTransactionOld
:: forall ctx s k n.
Expand Down Expand Up @@ -2135,10 +2173,10 @@ postExternalTransaction
( ctx ~ ApiLayer s k
)
=> ctx
-> ApiSerialisedTransaction
-> ApiT SerialisedTx
-> Handler ApiTxId
postExternalTransaction ctx (ApiSerialisedTransaction load) = do
tx <- liftHandler $ W.submitExternalTx @ctx @k ctx load
postExternalTransaction ctx (ApiT (SerialisedTx bytes)) = do
tx <- liftHandler $ W.submitExternalTx @ctx @k ctx bytes
return $ ApiTxId (ApiT (txId tx))

signMetadata
Expand Down

0 comments on commit 46491fc

Please sign in to comment.