Skip to content

Commit e113b2e

Browse files
authored
Merge pull request #1015 from IntersectMBO/certificates-in-tx
Add support for including certificates in transactions in `cardano-wasm`
2 parents 0ca3d5d + 0dd7ea5 commit e113b2e

File tree

5 files changed

+48
-1
lines changed

5 files changed

+48
-1
lines changed

cardano-wasm/js-test/basic-test.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ test('test output matches', async ({ page }) => {
88
// Wait for the test to finish running (we signal this by creating a tag with id "finish-tag" and text "Finished test!")
99
await expect(page.locator('#finish-tag')).toHaveText("Finished test!");
1010
// 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.
11-
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\"");
11+
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\"");
1212
});

cardano-wasm/lib-wrapper/unsigned-tx.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ declare interface UnsignedTx {
2727
*/
2828
addSimpleTxOut(destAddr: string, lovelaceAmount: bigint): UnsignedTx;
2929

30+
/**
31+
* Appends a certificate (in CBOR hex string format) to the transaction.
32+
* @param certCbor The certificate in CBOR hex string format.
33+
* @returns The `UnsignedTx` object with the added certificate.
34+
*/
35+
appendCertificateToTx(certCbor: string): UnsignedTx;
36+
3037
/**
3138
* Sets the fee for the transaction.
3239
* @param lovelaceAmount The fee amount in lovelaces.

cardano-wasm/src-lib/Cardano/Wasm/Api/Info.hs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,16 @@ apiInfo =
302302
, methodReturnType = Fluent
303303
, methodReturnDoc = "The `UnsignedTx` object with the added output."
304304
}
305+
, MethodInfoEntry $
306+
MethodInfo
307+
{ methodName = "appendCertificateToTx"
308+
, methodDoc = "Appends a certificate (in CBOR hex string format) to the transaction."
309+
, methodParams =
310+
[ ParamInfo "certCbor" TSString "The certificate in CBOR hex string format."
311+
]
312+
, methodReturnType = Fluent
313+
, methodReturnDoc = "The `UnsignedTx` object with the added certificate."
314+
}
305315
, MethodInfoEntry $
306316
MethodInfo
307317
{ methodName = "setFee"

cardano-wasm/src-lib/Cardano/Wasm/Api/Tx.hs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
{-# LANGUAGE FlexibleContexts #-}
44
{-# LANGUAGE InstanceSigs #-}
55
{-# LANGUAGE OverloadedStrings #-}
6+
{-# LANGUAGE PolyKinds #-}
67
{-# LANGUAGE ScopedTypeVariables #-}
78
{-# LANGUAGE StandaloneDeriving #-}
89
{-# LANGUAGE TypeFamilies #-}
@@ -15,6 +16,7 @@ module Cardano.Wasm.Api.Tx
1516
, newConwayTxImpl
1617
, addTxInputImpl
1718
, addSimpleTxOutImpl
19+
, appendCertificateToTxImpl
1820
, estimateMinFeeImpl
1921
, setFeeImpl
2022
, signWithPaymentKeyImpl
@@ -125,6 +127,21 @@ addSimpleTxOutImpl (UnsignedTxObject era (Exp.UnsignedTx tx)) destAddr lovelaceA
125127
(Api.AsAddressInEra Api.asType)
126128
(Text.pack destAddrStr)
127129

130+
-- | Append a certificate (in CBOR hex string format) to an unsigned transaction object.
131+
appendCertificateToTxImpl
132+
:: (HasCallStack, MonadThrow m) => UnsignedTxObject -> String -> m UnsignedTxObject
133+
appendCertificateToTxImpl (UnsignedTxObject era (Exp.UnsignedTx tx)) certCbor = do
134+
Exp.Certificate cert <- deserialiseCertificate era certCbor
135+
let tx' = tx & Ledger.bodyTxL . Ledger.certsTxBodyL %~ (<> StrictSeq.fromList [cert])
136+
return $ UnsignedTxObject era $ Exp.UnsignedTx tx'
137+
where
138+
deserialiseCertificate
139+
:: (HasCallStack, MonadThrow m) => Exp.Era era -> String -> m (Exp.Certificate (Exp.LedgerEra era))
140+
deserialiseCertificate era' certCbor' =
141+
obtainCommonConstraints era' $
142+
rightOrError $
143+
Api.deserialiseFromCBOR Exp.AsCertificate (Text.encodeUtf8 $ Text.pack certCbor')
144+
128145
-- | Set the fee for an unsigned transaction object.
129146
setFeeImpl :: UnsignedTxObject -> Ledger.Coin -> UnsignedTxObject
130147
setFeeImpl (UnsignedTxObject era (Exp.UnsignedTx tx)) fee =

cardano-wasm/src-wasm/Cardano/Wasm/Internal/JavaScript/Bridge.hs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,9 @@ foreign export javascript "addTxInput"
353353
foreign export javascript "addSimpleTxOut"
354354
addSimpleTxOut :: JSUnsignedTx -> JSString -> JSCoin -> IO JSUnsignedTx
355355

356+
foreign export javascript "appendCertificateToTx"
357+
appendCertificateToTx :: JSUnsignedTx -> JSString -> IO JSUnsignedTx
358+
356359
foreign export javascript "setFee"
357360
setFee :: JSUnsignedTx -> JSCoin -> IO JSUnsignedTx
358361

@@ -394,6 +397,16 @@ addSimpleTxOut jsUnsignedTx jsDestAddr jsCoin =
394397
<*> fromJSVal jsCoin
395398
)
396399

400+
-- | Append a certificate (in CBOR hex string format) to an unsigned transaction.
401+
appendCertificateToTx :: HasCallStack => JSUnsignedTx -> JSString -> IO JSUnsignedTx
402+
appendCertificateToTx jsUnsignedTx jsCertCbor =
403+
toJSVal
404+
=<< join
405+
( Wasm.appendCertificateToTxImpl
406+
<$> fromJSVal jsUnsignedTx
407+
<*> fromJSVal jsCertCbor
408+
)
409+
397410
-- | Set the transaction fee for an unsigned transaction.
398411
setFee :: HasCallStack => JSUnsignedTx -> JSCoin -> IO JSUnsignedTx
399412
setFee jsUnsignedTx jsCoin =

0 commit comments

Comments
 (0)