Skip to content

Commit

Permalink
Change the export format for unsigned txs in the deposit UI
Browse files Browse the repository at this point in the history
  • Loading branch information
paolino committed Nov 14, 2024
1 parent 2829753 commit 5e4340b
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/ui/cardano-wallet-ui.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ library
, aeson
, aeson-pretty
, base
, base58-bytestring
, base16-bytestring
, bech32
, bech32-th
, bytestring
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ import Cardano.Wallet.UI.Deposit.Types.Payments
import Control.Monad
( (<=<)
)
import Data.Aeson
( FromJSON (parseJSON)
, KeyValue ((.=))
, ToJSON (toJSON)
, object
, withObject
, (.:)
)
import Data.Text
( Text
)
Expand All @@ -54,12 +62,12 @@ import Servant
, Handler
)

import qualified Data.ByteString.Base58 as Base58
import qualified Data.ByteString.Base16 as B16
import qualified Data.ByteString.Lazy as BL
import qualified Data.Text.Encoding as T

data PaymentHandlerResponse
= ResponseSuccess Text InspectTx
= ResponseSuccess TransactionExport InspectTx
| ResponseExceptionPayments ErrCreatePayment
| ResponseNoReceivers
deriving (Eq, Show)
Expand Down Expand Up @@ -88,6 +96,37 @@ createPaymentHandler layer alert render receivers = do
Receiver{address, amount} <- receivers
pure (address, ValueC (CoinC $ fromIntegral amount) mempty)

data TransactionExport
= TransactionExport
{ dataType :: Text
, description :: Text
, cborHex :: Text
}
deriving (Eq, Show)

instance ToJSON TransactionExport where
toJSON TransactionExport{dataType, description, cborHex} =
object
[ "type" .= dataType
, "description" .= description
, "cborHex" .= cborHex
]

instance FromJSON TransactionExport where
parseJSON = withObject "TransactionExport" $ \o -> do
dataType <- o .: "type"
description <- o .: "description"
cborHex <- o .: "cborHex"
pure TransactionExport{dataType, description, cborHex}

conwayEraTransactionExport :: Text -> TransactionExport
conwayEraTransactionExport cborHex =
TransactionExport
{ dataType = "Unwitnessed Tx ConwayEra"
, description = "Ledger Cddl Format"
, cborHex
}

respondCreatePayment
:: (PaymentHandlerResponse -> html)
-> Either ErrCreatePayment CurrentEraResolvedTx
Expand All @@ -100,8 +139,9 @@ respondCreatePayment render' r = do
itx <- inspectTx tx
let
mt =
T.decodeUtf8
$ Base58.encodeBase58 Base58.bitcoinAlphabet
conwayEraTransactionExport
$ T.decodeUtf8
$ B16.encode
$ BL.toStrict
$ serializeTx
$ resolvedTx tx
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ import Cardano.Wallet.UI.Deposit.Handlers.Payments.Transaction
( AddressValidationResponse (..)
, AmountValidationResponse (..)
, PaymentHandlerResponse (..)
, TransactionExport
)
import Cardano.Wallet.UI.Deposit.Html.Common
( lovelaceH
Expand Down Expand Up @@ -111,6 +112,8 @@ import Servant
, ToHttpApiData (toUrlPiece)
)

import qualified Data.Aeson as Aeson

paymentsH :: Link -> WHtml ()
paymentsH paymentsLink = do
sseH paymentsLink "payments-page" ["payments"]
Expand Down Expand Up @@ -289,10 +292,10 @@ unsignedTransactionH = \case
tdEnd $ addressH WithCopy addr
tdEnd $ lovelaceH $ fromIntegral amount

transactionCBORH :: Text -> Html ()
transactionCBORH :: TransactionExport -> Html ()
transactionCBORH cbor =
truncatableText WithCopy "unsigned-transaction-copy"
$ toHtml cbor
$ toHtml $ Aeson.encode cbor

updateReceiversH
:: [Receiver] -> Coin -> PaymentHandlerResponse -> Html ()
Expand Down

0 comments on commit 5e4340b

Please sign in to comment.