Skip to content

Commit eeb2a3b

Browse files
authored
Merge pull request #995 from IntersectMBO/pure-wasi-lib
Create JS independent WASM library `cardano-wasi`
2 parents efa0697 + 26bdc48 commit eeb2a3b

File tree

8 files changed

+434
-4
lines changed

8 files changed

+434
-4
lines changed

cardano-wasm/app-wasi/Main.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module Main where
2+
3+
main :: IO ()
4+
main = return ()

cardano-wasm/cardano-wasm.cabal

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,21 +69,29 @@ executable cardano-wasi
6969
import: project-config
7070
main-is: Main.hs
7171
hs-source-dirs:
72-
app
72+
app-wasi
7373
src-wasi
7474

7575
default-language: Haskell2010
7676

7777
if arch(wasm32)
7878
ghc-options:
7979
-no-hs-main
80-
-optl-mexec-model=reactor
81-
"-optl-Wl,--strip-all"
80+
"-optl-Wl,--strip-all,--export=hs_init,--export=newTx,--export=newExperimentalEraTx,--export=newConwayTx,--export=addTxInput,--export=addSimpleTxOut,--export=appendCertificateToTx,--export=setFee,--export=estimateMinFee,--export=signWithPaymentKey,--export=alsoSignWithPaymentKey,--export=toCbor,--export=generatePaymentWallet,--export=generateStakeWallet,--export=restorePaymentWalletFromSigningKeyBech32,--export=restoreStakeWalletFromSigningKeyBech32,--export=generateTestnetPaymentWallet,--export=generateTestnetStakeWallet,--export=restoreTestnetPaymentWalletFromSigningKeyBech32,--export=restoreTestnetStakeWalletFromSigningKeyBech32,--export=getAddressBech32,--export=getBech32ForPaymentVerificationKey,--export=getBech32ForPaymentSigningKey,--export=getBech32ForStakeVerificationKey,--export=getBech32ForStakeSigningKey,--export=getBase16ForPaymentVerificationKeyHash,--export=getBase16ForStakeVerificationKeyHash,--export=mallocNBytes,--export=getStrLen,--export=freeMemory"
8281
other-modules:
82+
Cardano.Wasi.Internal.Api.GRPC
83+
Cardano.Wasi.Internal.Api.Memory
84+
Cardano.Wasi.Internal.Api.Tx
85+
Cardano.Wasi.Internal.Api.Wallet
86+
Cardano.Wasi.Internal.Conversion
87+
8388
build-depends:
89+
aeson,
8490
base,
91+
cardano-api,
8592
cardano-wasm:cardano-wasi-lib,
86-
optparse-applicative,
93+
text,
94+
utf8-string,
8795

8896
executable cardano-wasm
8997
import: project-config

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
module Cardano.Wasm.Api.Tx
1212
( UnsignedTxObject (..)
13+
, SignedTxObject (..)
1314
, ProtocolParamsJSON (..)
1415
, newTxImpl
1516
, newExperimentalEraTxImpl
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module Cardano.Wasi.Internal.Api.GRPC (module GRPC) where
2+
3+
import Cardano.Wasm.Api.GRPC as GRPC
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{-# LANGUAGE CPP #-}
2+
3+
module Cardano.Wasi.Internal.Api.Memory
4+
( mallocNBytes
5+
, getStrLen
6+
, freeMemory
7+
)
8+
where
9+
10+
import Foreign.C (CChar (..), CString)
11+
import Foreign.Marshal (free, lengthArray0, mallocBytes)
12+
import Foreign.Ptr (Ptr)
13+
14+
-- * Memory
15+
16+
#if defined(wasm32_HOST_ARCH)
17+
18+
foreign export ccall "mallocNBytes"
19+
mallocBytes :: Int -> IO (Ptr a)
20+
21+
foreign export ccall "getStrLen"
22+
getStrLen :: CString -> IO Int
23+
24+
foreign export ccall "freeMemory"
25+
freeMemory :: Ptr a -> IO ()
26+
27+
#endif
28+
29+
mallocNBytes :: Int -> IO (Ptr a)
30+
mallocNBytes = mallocBytes
31+
32+
getStrLen :: CString -> IO Int
33+
getStrLen = lengthArray0 (CChar 0)
34+
35+
freeMemory :: Ptr a -> IO ()
36+
freeMemory = free
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
{-# LANGUAGE CPP #-}
2+
3+
module Cardano.Wasi.Internal.Api.Tx
4+
( newTx
5+
, newExperimentalEraTx
6+
, newConwayTx
7+
, addTxInput
8+
, addSimpleTxOut
9+
, appendCertificateToTx
10+
, setFee
11+
, estimateMinFee
12+
, signWithPaymentKey
13+
, alsoSignWithPaymentKey
14+
, toCbor
15+
)
16+
where
17+
18+
import Cardano.Api qualified as Api
19+
20+
import Cardano.Wasi.Internal.Conversion
21+
( cstrToInt
22+
, fromCJSON
23+
, intToCStr
24+
, stringToSigningKey
25+
, toCJSON
26+
, txIdToString
27+
)
28+
import Cardano.Wasm.Api.Tx
29+
30+
import Control.Monad (join)
31+
32+
import Foreign.C (CString)
33+
import Foreign.C.String (newCString, peekCString)
34+
35+
-- * UnsignedTxObject
36+
37+
#if defined(wasm32_HOST_ARCH)
38+
39+
foreign export ccall "newTx"
40+
newTx :: IO UnsignedTxObjectJSON
41+
42+
foreign export ccall "newExperimentalEraTx"
43+
newExperimentalEraTx :: IO UnsignedTxObjectJSON
44+
45+
foreign export ccall "newConwayTx"
46+
newConwayTx :: IO UnsignedTxObjectJSON
47+
48+
foreign export ccall "addTxInput"
49+
addTxInput :: UnsignedTxObjectJSON -> CString -> Int -> IO UnsignedTxObjectJSON
50+
51+
foreign export ccall "addSimpleTxOut"
52+
addSimpleTxOut :: UnsignedTxObjectJSON -> CString -> CString -> IO UnsignedTxObjectJSON
53+
54+
foreign export ccall "appendCertificateToTx"
55+
appendCertificateToTx :: UnsignedTxObjectJSON -> CString -> IO UnsignedTxObjectJSON
56+
57+
foreign export ccall "setFee"
58+
setFee :: UnsignedTxObjectJSON -> CString -> IO UnsignedTxObjectJSON
59+
60+
foreign export ccall "estimateMinFee"
61+
estimateMinFee :: UnsignedTxObjectJSON -> CString -> Int -> Int -> Int -> IO CString
62+
63+
foreign export ccall "signWithPaymentKey"
64+
signWithPaymentKey :: UnsignedTxObjectJSON -> CString -> IO SignedTxObjectJSON
65+
66+
#endif
67+
68+
type UnsignedTxObjectJSON = CString
69+
70+
newTx :: IO UnsignedTxObjectJSON
71+
newTx = toCJSON newTxImpl
72+
73+
newExperimentalEraTx :: IO UnsignedTxObjectJSON
74+
newExperimentalEraTx = toCJSON =<< newExperimentalEraTxImpl
75+
76+
newConwayTx :: IO UnsignedTxObjectJSON
77+
newConwayTx = toCJSON newConwayTxImpl
78+
79+
addTxInput :: UnsignedTxObjectJSON -> CString -> Int -> IO UnsignedTxObjectJSON
80+
addTxInput unsignedTxObject txId txIx =
81+
toCJSON
82+
=<< ( addTxInputImpl
83+
<$> fromCJSON True "UnsignedTx" unsignedTxObject
84+
<*> txIdToString txId
85+
<*> pure (Api.TxIx (fromIntegral txIx))
86+
)
87+
88+
addSimpleTxOut :: UnsignedTxObjectJSON -> CString -> CString -> IO UnsignedTxObjectJSON
89+
addSimpleTxOut unsignedTxObject destAddr lovelaceAmountStr =
90+
toCJSON
91+
=<< join
92+
( addSimpleTxOutImpl
93+
<$> fromCJSON True "UnsignedTx" unsignedTxObject
94+
<*> peekCString destAddr
95+
<*> (Api.Coin <$> cstrToInt "Lovelace amount" lovelaceAmountStr)
96+
)
97+
98+
appendCertificateToTx :: UnsignedTxObjectJSON -> CString -> IO UnsignedTxObjectJSON
99+
appendCertificateToTx unsignedTxObject certCborStr =
100+
toCJSON
101+
=<< join
102+
( appendCertificateToTxImpl
103+
<$> fromCJSON True "UnsignedTx" unsignedTxObject
104+
<*> peekCString certCborStr
105+
)
106+
107+
setFee :: UnsignedTxObjectJSON -> CString -> IO UnsignedTxObjectJSON
108+
setFee unsignedTxObject feeStr =
109+
toCJSON
110+
=<< ( setFeeImpl
111+
<$> fromCJSON True "UnsignedTx" unsignedTxObject
112+
<*> (Api.Coin <$> cstrToInt "fee" feeStr)
113+
)
114+
115+
estimateMinFee :: UnsignedTxObjectJSON -> CString -> Int -> Int -> Int -> IO CString
116+
estimateMinFee ptrUnsignedTxObject pparams numInputs numOutputs numShelleyKeyWitnesses = do
117+
(intToCStr . Api.unCoin)
118+
=<< join
119+
( estimateMinFeeImpl
120+
<$> fromCJSON False "UnsignedTx" ptrUnsignedTxObject
121+
<*> (ProtocolParamsJSON <$> fromCJSON False "ProtocolParameters" pparams)
122+
<*> pure (fromIntegral numInputs)
123+
<*> pure (fromIntegral numOutputs)
124+
<*> pure (fromIntegral numShelleyKeyWitnesses)
125+
)
126+
127+
signWithPaymentKey :: UnsignedTxObjectJSON -> CString -> IO SignedTxObjectJSON
128+
signWithPaymentKey unsignedTxObject signingKeyBech32 =
129+
toCJSON
130+
=<< ( signWithPaymentKeyImpl
131+
<$> fromCJSON True "UnsignedTx" unsignedTxObject
132+
<*> stringToSigningKey signingKeyBech32
133+
)
134+
135+
-- * SignedTxObject
136+
137+
#if defined(wasm32_HOST_ARCH)
138+
139+
foreign export ccall "alsoSignWithPaymentKey"
140+
alsoSignWithPaymentKey :: SignedTxObjectJSON -> CString -> IO SignedTxObjectJSON
141+
142+
foreign export ccall "toCbor"
143+
toCbor :: SignedTxObjectJSON -> IO CString
144+
145+
#endif
146+
147+
type SignedTxObjectJSON = CString
148+
149+
alsoSignWithPaymentKey :: SignedTxObjectJSON -> CString -> IO SignedTxObjectJSON
150+
alsoSignWithPaymentKey signedTxObject signingKeyBech32 =
151+
toCJSON
152+
=<< ( alsoSignWithPaymentKeyImpl
153+
<$> fromCJSON True "SignedTx" signedTxObject
154+
<*> stringToSigningKey signingKeyBech32
155+
)
156+
157+
toCbor :: SignedTxObjectJSON -> IO CString
158+
toCbor signedTxObject =
159+
newCString . toCborImpl
160+
=<< fromCJSON False "SignedTx" signedTxObject

0 commit comments

Comments
 (0)