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
2 changes: 1 addition & 1 deletion cardano-wasm/js-test/basic-test.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ test('test output matches', async ({ page }) => {
// Wait for the test to finish running (we signal this by creating a tag with id "finish-tag" and text "Finished test!")
await expect(page.locator('#finish-tag')).toHaveText("Finished test!");
// Check the output of the test (from the example folder), which is displayed in the code element with id "test-output". The output contains information about the various objects and results of trying some of the functions.
await expect(page.locator('#test-output')).toHaveText("> \"Api object:\"> [object] { objectType: cardano-api tx: [object Object] newGrpcConnection: async function (...args) wallet: [object Object] }> \"Bech32 of address:\"> \"addr_test1vp93p9em3regvgylxuvet6fgr3e9sn259pcejgrk4ykystcs7v8j6\"> \"UnsignedTx object:\"> [object] { objectType: UnsignedTx addTxInput: function (txId,txIx) addSimpleTxOut: function (destAddr,lovelaceAmount) setFee: function (lovelaceAmount) estimateMinFee: function (protocolParams,numKeyWitnesses,numByronKeyWitnesses,totalRefScriptSize) signWithPaymentKey: function (signingKey) }> \"Estimated fee:\"> 164005n> \"SignedTx object:\"> [object] { objectType: SignedTx alsoSignWithPaymentKey: function (signingKey) txToCbor: function () }> \"Tx CBOR:\"> \"84a300d9010281825820be6efd42a3d7b9a00d09d77a5d41e55ceaf0bd093a8aa8a893ce70d9caafd97800018182581d6082935e44937e8b530f32ce672b5d600d0a286b4e8a52c6555f659b871a00989680021a000280a5a100d9010281825820adfc1c30385916da87db1ba3328f0690a57ebb2a6ac9f6f86b2d97f943adae005840a49259b5977aea523b46f01261fbff93e0899e8700319e11f5ab96b67eb628fca1a233ce2d50ee3227b591b84f27237d920d63974d65728362382f751c4d9400f5f6\"");
await expect(page.locator('#test-output')).toHaveText("> \"Api object:\"> [object] { objectType: cardano-api tx: [object Object] newGrpcConnection: async function (...args) wallet: [object Object] }> \"Bech32 of address:\"> \"addr_test1vp93p9em3regvgylxuvet6fgr3e9sn259pcejgrk4ykystcs7v8j6\"> \"UnsignedTx object:\"> [object] { objectType: UnsignedTx addTxInput: function (txId,txIx) addSimpleTxOut: function (destAddr,lovelaceAmount) appendCertificateToTx: function (certCbor) setFee: function (lovelaceAmount) estimateMinFee: function (protocolParams,numKeyWitnesses,numByronKeyWitnesses,totalRefScriptSize) signWithPaymentKey: function (signingKey) }> \"Estimated fee:\"> 164005n> \"SignedTx object:\"> [object] { objectType: SignedTx alsoSignWithPaymentKey: function (signingKey) txToCbor: function () }> \"Tx CBOR:\"> \"84a300d9010281825820be6efd42a3d7b9a00d09d77a5d41e55ceaf0bd093a8aa8a893ce70d9caafd97800018182581d6082935e44937e8b530f32ce672b5d600d0a286b4e8a52c6555f659b871a00989680021a000280a5a100d9010281825820adfc1c30385916da87db1ba3328f0690a57ebb2a6ac9f6f86b2d97f943adae005840a49259b5977aea523b46f01261fbff93e0899e8700319e11f5ab96b67eb628fca1a233ce2d50ee3227b591b84f27237d920d63974d65728362382f751c4d9400f5f6\"");
});
7 changes: 7 additions & 0 deletions cardano-wasm/lib-wrapper/unsigned-tx.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ declare interface UnsignedTx {
*/
addSimpleTxOut(destAddr: string, lovelaceAmount: bigint): UnsignedTx;

/**
* Appends a certificate (in CBOR hex string format) to the transaction.
* @param certCbor The certificate in CBOR hex string format.
* @returns The `UnsignedTx` object with the added certificate.
*/
appendCertificateToTx(certCbor: string): UnsignedTx;

/**
* Sets the fee for the transaction.
* @param lovelaceAmount The fee amount in lovelaces.
Expand Down
10 changes: 10 additions & 0 deletions cardano-wasm/src-lib/Cardano/Wasm/Api/Info.hs
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,16 @@ apiInfo =
, methodReturnType = Fluent
, methodReturnDoc = "The `UnsignedTx` object with the added output."
}
, MethodInfoEntry $
MethodInfo
{ methodName = "appendCertificateToTx"
, methodDoc = "Appends a certificate (in CBOR hex string format) to the transaction."
, methodParams =
[ ParamInfo "certCbor" TSString "The certificate in CBOR hex string format."
]
, methodReturnType = Fluent
, methodReturnDoc = "The `UnsignedTx` object with the added certificate."
}
, MethodInfoEntry $
MethodInfo
{ methodName = "setFee"
Expand Down
17 changes: 17 additions & 0 deletions cardano-wasm/src-lib/Cardano/Wasm/Api/Tx.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeFamilies #-}
Expand All @@ -15,6 +16,7 @@ module Cardano.Wasm.Api.Tx
, newConwayTxImpl
, addTxInputImpl
, addSimpleTxOutImpl
, appendCertificateToTxImpl
, estimateMinFeeImpl
, setFeeImpl
, signWithPaymentKeyImpl
Expand Down Expand Up @@ -125,6 +127,21 @@ addSimpleTxOutImpl (UnsignedTxObject era (Exp.UnsignedTx tx)) destAddr lovelaceA
(Api.AsAddressInEra Api.asType)
(Text.pack destAddrStr)

-- | Append a certificate (in CBOR hex string format) to an unsigned transaction object.
appendCertificateToTxImpl
:: (HasCallStack, MonadThrow m) => UnsignedTxObject -> String -> m UnsignedTxObject
appendCertificateToTxImpl (UnsignedTxObject era (Exp.UnsignedTx tx)) certCbor = do
Exp.Certificate cert <- deserialiseCertificate era certCbor
let tx' = tx & Ledger.bodyTxL . Ledger.certsTxBodyL %~ (<> StrictSeq.fromList [cert])
return $ UnsignedTxObject era $ Exp.UnsignedTx tx'
where
deserialiseCertificate
:: (HasCallStack, MonadThrow m) => Exp.Era era -> String -> m (Exp.Certificate (Exp.LedgerEra era))
deserialiseCertificate era' certCbor' =
obtainCommonConstraints era' $
rightOrError $
Api.deserialiseFromCBOR Exp.AsCertificate (Text.encodeUtf8 $ Text.pack certCbor')

-- | Set the fee for an unsigned transaction object.
setFeeImpl :: UnsignedTxObject -> Ledger.Coin -> UnsignedTxObject
setFeeImpl (UnsignedTxObject era (Exp.UnsignedTx tx)) fee =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,9 @@ foreign export javascript "addTxInput"
foreign export javascript "addSimpleTxOut"
addSimpleTxOut :: JSUnsignedTx -> JSString -> JSCoin -> IO JSUnsignedTx

foreign export javascript "appendCertificateToTx"
appendCertificateToTx :: JSUnsignedTx -> JSString -> IO JSUnsignedTx

foreign export javascript "setFee"
setFee :: JSUnsignedTx -> JSCoin -> IO JSUnsignedTx

Expand Down Expand Up @@ -394,6 +397,16 @@ addSimpleTxOut jsUnsignedTx jsDestAddr jsCoin =
<*> fromJSVal jsCoin
)

-- | Append a certificate (in CBOR hex string format) to an unsigned transaction.
appendCertificateToTx :: HasCallStack => JSUnsignedTx -> JSString -> IO JSUnsignedTx
appendCertificateToTx jsUnsignedTx jsCertCbor =
toJSVal
=<< join
( Wasm.appendCertificateToTxImpl
<$> fromJSVal jsUnsignedTx
<*> fromJSVal jsCertCbor
)

-- | Set the transaction fee for an unsigned transaction.
setFee :: HasCallStack => JSUnsignedTx -> JSCoin -> IO JSUnsignedTx
setFee jsUnsignedTx jsCoin =
Expand Down
Loading