|
| 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