Skip to content

Commit 6b352f0

Browse files
committed
Export methods in the JavaScript API
1 parent 351fb95 commit 6b352f0

File tree

4 files changed

+232
-2
lines changed

4 files changed

+232
-2
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) 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\"");
11+
await expect(page.locator('#test-output')).toHaveText("> \"Api object:\"> [object] {    objectType: cardano-api    tx: [object Object]    newGrpcConnection: async function (...args)    certificate: [object Object]    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/cardano-api.d.ts

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ declare interface CardanoApi {
2626
newTx(): Promise<UnsignedTx>;
2727

2828
/**
29-
* Create a new unsigned transaction in the current experimental era (currently unavailable).
29+
* Create a new unsigned transaction in the current experimental era (currently Dijkstra).
3030
* @returns A promise that resolves to a new `UnsignedTx` object.
3131
*/
3232
newExperimentalEraTx(): Promise<UnsignedTx>;
@@ -45,6 +45,69 @@ declare interface CardanoApi {
4545
*/
4646
newGrpcConnection(webGrpcUrl: string): Promise<GrpcConnection>;
4747

48+
/**
49+
* Methods for creating certificates.
50+
*/
51+
certificate: {
52+
/**
53+
* Methods for creating certificates in Conway era.
54+
*/
55+
conway: {
56+
/**
57+
* Make a certificate that delegates a stake address to a stake pool in Conway era.
58+
* @param stakeKeyHash The stake key hash in base16 format.
59+
* @param poolId The pool ID in base16 format.
60+
* @returns A promise that resolves to the CBOR-encoded certificate as a hex string.
61+
*/
62+
makeStakeAddressStakeDelegationCertificate(stakeKeyHash: string, poolId: string): Promise<string>;
63+
64+
/**
65+
* Make a stake address registration certificate in Conway era.
66+
* @param stakeKeyHash The stake key hash in base16 format.
67+
* @param deposit The deposit amount in lovelaces.
68+
* @returns A promise that resolves to the CBOR-encoded certificate as a hex string.
69+
*/
70+
makeStakeAddressRegistrationCertificate(stakeKeyHash: string, deposit: bigint): Promise<string>;
71+
72+
/**
73+
* Make a stake address unregistration certificate in Conway era.
74+
* @param stakeKeyHash The stake key hash in base16 format.
75+
* @param deposit The deposit amount in lovelaces.
76+
* @returns A promise that resolves to the CBOR-encoded certificate as a hex string.
77+
*/
78+
makeStakeAddressUnregistrationCertificate(stakeKeyHash: string, deposit: bigint): Promise<string>;
79+
}
80+
81+
/**
82+
* Methods for creating certificates in the current experimental era.
83+
*/
84+
experimentalEra: {
85+
/**
86+
* Make a certificate that delegates a stake address to a stake pool in the current experimental era.
87+
* @param stakeKeyHash The stake key hash in base16 format.
88+
* @param poolId The pool ID in base16 format.
89+
* @returns A promise that resolves to the CBOR-encoded certificate as a hex string.
90+
*/
91+
makeStakeAddressStakeDelegationCertificateExperimentalEra(stakeKeyHash: string, poolId: string): Promise<string>;
92+
93+
/**
94+
* Make a stake address registration certificate in the current experimental era.
95+
* @param stakeKeyHash The stake key hash in base16 format.
96+
* @param deposit The deposit amount in lovelaces.
97+
* @returns A promise that resolves to the CBOR-encoded certificate as a hex string.
98+
*/
99+
makeStakeAddressRegistrationCertificateExperimentalEra(stakeKeyHash: string, deposit: bigint): Promise<string>;
100+
101+
/**
102+
* Make a stake address unregistration certificate in the current experimental era.
103+
* @param stakeKeyHash The stake key hash in base16 format.
104+
* @param deposit The deposit amount in lovelaces.
105+
* @returns A promise that resolves to the CBOR-encoded certificate as a hex string.
106+
*/
107+
makeStakeAddressUnregistrationCertificateExperimentalEra(stakeKeyHash: string, deposit: bigint): Promise<string>;
108+
}
109+
}
110+
48111
/**
49112
* Methods for generating and restoring wallets.
50113
*/

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

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,94 @@ apiInfo =
476476
, methodReturnType = NewObject (virtualObjectName grpcConnection)
477477
, methodReturnDoc = "A promise that resolves to a new `GrpcConnection`."
478478
}
479+
, MethodGroupEntry $
480+
MethodGroup
481+
{ groupName = "certificate"
482+
, groupDoc = ["Methods for creating certificates."]
483+
, groupMethods =
484+
[ MethodGroupEntry $
485+
MethodGroup
486+
{ groupName = "conway"
487+
, groupDoc = ["Methods for creating certificates in Conway era."]
488+
, groupMethods =
489+
[ MethodInfoEntry $
490+
MethodInfo
491+
{ methodName = "makeStakeAddressStakeDelegationCertificate"
492+
, methodDoc = "Make a certificate that delegates a stake address to a stake pool in Conway era."
493+
, methodParams =
494+
[ ParamInfo "stakeKeyHash" TSString "The stake key hash in base16 format."
495+
, ParamInfo "poolId" TSString "The pool ID in base16 format."
496+
]
497+
, methodReturnType = OtherType TSString
498+
, methodReturnDoc = "A promise that resolves to the CBOR-encoded certificate as a hex string."
499+
}
500+
, MethodInfoEntry $
501+
MethodInfo
502+
{ methodName = "makeStakeAddressRegistrationCertificate"
503+
, methodDoc = "Make a stake address registration certificate in Conway era."
504+
, methodParams =
505+
[ ParamInfo "stakeKeyHash" TSString "The stake key hash in base16 format."
506+
, ParamInfo "deposit" TSBigInt "The deposit amount in lovelaces."
507+
]
508+
, methodReturnType = OtherType TSString
509+
, methodReturnDoc = "A promise that resolves to the CBOR-encoded certificate as a hex string."
510+
}
511+
, MethodInfoEntry $
512+
MethodInfo
513+
{ methodName = "makeStakeAddressUnregistrationCertificate"
514+
, methodDoc = "Make a stake address unregistration certificate in Conway era."
515+
, methodParams =
516+
[ ParamInfo "stakeKeyHash" TSString "The stake key hash in base16 format."
517+
, ParamInfo "deposit" TSBigInt "The deposit amount in lovelaces."
518+
]
519+
, methodReturnType = OtherType TSString
520+
, methodReturnDoc = "A promise that resolves to the CBOR-encoded certificate as a hex string."
521+
}
522+
]
523+
}
524+
, MethodGroupEntry $
525+
MethodGroup
526+
{ groupName = "experimentalEra"
527+
, groupDoc = ["Methods for creating certificates in the current experimental era."]
528+
, groupMethods =
529+
[ MethodInfoEntry $
530+
MethodInfo
531+
{ methodName = "makeStakeAddressStakeDelegationCertificateExperimentalEra"
532+
, methodDoc =
533+
"Make a certificate that delegates a stake address to a stake pool in the current experimental era."
534+
, methodParams =
535+
[ ParamInfo "stakeKeyHash" TSString "The stake key hash in base16 format."
536+
, ParamInfo "poolId" TSString "The pool ID in base16 format."
537+
]
538+
, methodReturnType = OtherType TSString
539+
, methodReturnDoc = "A promise that resolves to the CBOR-encoded certificate as a hex string."
540+
}
541+
, MethodInfoEntry $
542+
MethodInfo
543+
{ methodName = "makeStakeAddressRegistrationCertificateExperimentalEra"
544+
, methodDoc = "Make a stake address registration certificate in the current experimental era."
545+
, methodParams =
546+
[ ParamInfo "stakeKeyHash" TSString "The stake key hash in base16 format."
547+
, ParamInfo "deposit" TSBigInt "The deposit amount in lovelaces."
548+
]
549+
, methodReturnType = OtherType TSString
550+
, methodReturnDoc = "A promise that resolves to the CBOR-encoded certificate as a hex string."
551+
}
552+
, MethodInfoEntry $
553+
MethodInfo
554+
{ methodName = "makeStakeAddressUnregistrationCertificateExperimentalEra"
555+
, methodDoc = "Make a stake address unregistration certificate in the current experimental era."
556+
, methodParams =
557+
[ ParamInfo "stakeKeyHash" TSString "The stake key hash in base16 format."
558+
, ParamInfo "deposit" TSBigInt "The deposit amount in lovelaces."
559+
]
560+
, methodReturnType = OtherType TSString
561+
, methodReturnDoc = "A promise that resolves to the CBOR-encoded certificate as a hex string."
562+
}
563+
]
564+
}
565+
]
566+
}
479567
, MethodGroupEntry $
480568
MethodGroup
481569
{ groupName = "wallet"

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

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ module Cardano.Wasm.Internal.JavaScript.Bridge where
1616
import Cardano.Api qualified as Api
1717
import Cardano.Api.Ledger qualified as Ledger
1818

19+
import Cardano.Wasm.Api.Certificate.StakeCertificate qualified as Wasm
1920
import Cardano.Wasm.Api.GRPC qualified as Wasm
2021
import Cardano.Wasm.Api.Info (apiInfo)
2122
import Cardano.Wasm.Api.Tx qualified as Wasm
@@ -356,6 +357,24 @@ foreign export javascript "addSimpleTxOut"
356357
foreign export javascript "appendCertificateToTx"
357358
appendCertificateToTx :: JSUnsignedTx -> JSString -> IO JSUnsignedTx
358359

360+
foreign export javascript "makeStakeAddressStakeDelegationCertificate"
361+
makeStakeAddressStakeDelegationCertificate :: JSString -> JSString -> IO JSString
362+
363+
foreign export javascript "makeStakeAddressStakeDelegationCertificateExperimentalEra"
364+
makeStakeAddressStakeDelegationCertificateExperimentalEra :: JSString -> JSString -> IO JSString
365+
366+
foreign export javascript "makeStakeAddressRegistrationCertificate"
367+
makeStakeAddressRegistrationCertificate :: JSString -> JSCoin -> IO JSString
368+
369+
foreign export javascript "makeStakeAddressRegistrationCertificateExperimentalEra"
370+
makeStakeAddressRegistrationCertificateExperimentalEra :: JSString -> JSCoin -> IO JSString
371+
372+
foreign export javascript "makeStakeAddressUnregistrationCertificate"
373+
makeStakeAddressUnregistrationCertificate :: JSString -> JSCoin -> IO JSString
374+
375+
foreign export javascript "makeStakeAddressUnregistrationCertificateExperimentalEra"
376+
makeStakeAddressUnregistrationCertificateExperimentalEra :: JSString -> JSCoin -> IO JSString
377+
359378
foreign export javascript "setFee"
360379
setFee :: JSUnsignedTx -> JSCoin -> IO JSUnsignedTx
361380

@@ -407,6 +426,66 @@ appendCertificateToTx jsUnsignedTx jsCertCbor =
407426
<*> fromJSVal jsCertCbor
408427
)
409428

429+
-- | Make a certificate that delegates a stake address to a stake pool in Conway era.
430+
makeStakeAddressStakeDelegationCertificate :: HasCallStack => JSString -> JSString -> IO JSString
431+
makeStakeAddressStakeDelegationCertificate jsStakeKeyHash jsPoolId =
432+
toJSVal
433+
=<< join
434+
( Wasm.makeStakeAddressStakeDelegationCertificateImpl
435+
<$> fromJSVal jsStakeKeyHash
436+
<*> fromJSVal jsPoolId
437+
)
438+
439+
-- | Make a certificate that delegates a stake address to a stake pool in the current experimental era.
440+
makeStakeAddressStakeDelegationCertificateExperimentalEra :: HasCallStack => JSString -> JSString -> IO JSString
441+
makeStakeAddressStakeDelegationCertificateExperimentalEra jsStakeKeyHash jsPoolId =
442+
toJSVal
443+
=<< join
444+
( Wasm.makeStakeAddressStakeDelegationCertificateExperimentalEraImpl
445+
<$> fromJSVal jsStakeKeyHash
446+
<*> fromJSVal jsPoolId
447+
)
448+
449+
-- | Make a stake address registration certificate in Conway era.
450+
makeStakeAddressRegistrationCertificate :: HasCallStack => JSString -> JSCoin -> IO JSString
451+
makeStakeAddressRegistrationCertificate jsStakeKeyHash jsDeposit =
452+
toJSVal
453+
=<< join
454+
( Wasm.makeStakeAddressRegistrationCertificateImpl
455+
<$> fromJSVal jsStakeKeyHash
456+
<*> (fromInteger <$> fromJSBigInt jsDeposit)
457+
)
458+
459+
-- | Make a stake address registration certificate in the current experimental era.
460+
makeStakeAddressRegistrationCertificateExperimentalEra :: HasCallStack => JSString -> JSCoin -> IO JSString
461+
makeStakeAddressRegistrationCertificateExperimentalEra jsStakeKeyHash jsDeposit =
462+
toJSVal
463+
=<< join
464+
( Wasm.makeStakeAddressRegistrationCertificateExperimentalEraImpl
465+
<$> fromJSVal jsStakeKeyHash
466+
<*> (fromInteger <$> fromJSBigInt jsDeposit)
467+
)
468+
469+
-- | Make a stake address unregistration certificate in Conway era.
470+
makeStakeAddressUnregistrationCertificate :: HasCallStack => JSString -> JSCoin -> IO JSString
471+
makeStakeAddressUnregistrationCertificate jsStakeKeyHash jsDeposit =
472+
toJSVal
473+
=<< join
474+
( Wasm.makeStakeAddressUnregistrationCertificateImpl
475+
<$> fromJSVal jsStakeKeyHash
476+
<*> (fromInteger <$> fromJSBigInt jsDeposit)
477+
)
478+
479+
-- | Make a stake address unregistration certificate in the current experimental era.
480+
makeStakeAddressUnregistrationCertificateExperimentalEra :: HasCallStack => JSString -> JSCoin -> IO JSString
481+
makeStakeAddressUnregistrationCertificateExperimentalEra jsStakeKeyHash jsDeposit =
482+
toJSVal
483+
=<< join
484+
( Wasm.makeStakeAddressUnregistrationCertificateExperimentalEraImpl
485+
<$> fromJSVal jsStakeKeyHash
486+
<*> (fromInteger <$> fromJSBigInt jsDeposit)
487+
)
488+
410489
-- | Set the transaction fee for an unsigned transaction.
411490
setFee :: HasCallStack => JSUnsignedTx -> JSCoin -> IO JSUnsignedTx
412491
setFee jsUnsignedTx jsCoin =

0 commit comments

Comments
 (0)