Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encrypt metadata spec and types #4176

Merged
merged 9 commits into from
Nov 16, 2023
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
5 changes: 3 additions & 2 deletions lib/wallet/api/http/Cardano/Wallet/Api.hs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ import Cardano.Wallet.Api.Types
, ApiCoinSelectionT
, ApiConstructTransactionDataT
, ApiConstructTransactionT
, ApiDecodeTransactionPostData
, ApiDecodedTransactionT
, ApiFee
, ApiHealthCheck
Expand Down Expand Up @@ -674,7 +675,7 @@ type BalanceTransaction n = "wallets"
type DecodeTransaction n = "wallets"
:> Capture "walletId" (ApiT WalletId)
:> "transactions-decode"
:> ReqBody '[JSON] ApiSerialisedTransaction
:> ReqBody '[JSON] ApiDecodeTransactionPostData
:> PostAccepted '[JSON] (ApiDecodedTransactionT n)

-- | https://cardano-foundation.github.io/cardano-wallet/api/#operation/submitTransaction
Expand Down Expand Up @@ -1196,7 +1197,7 @@ type SignSharedTransaction n = "shared-wallets"
type DecodeSharedTransaction n = "shared-wallets"
:> Capture "walletId" (ApiT WalletId)
:> "transactions-decode"
:> ReqBody '[JSON] ApiSerialisedTransaction
:> ReqBody '[JSON] ApiDecodeTransactionPostData
:> PostAccepted '[JSON] (ApiDecodedTransactionT n)

-- | https://cardano-foundation.github.io/cardano-wallet/api/#operation/submitSharedTransaction
Expand Down
3 changes: 2 additions & 1 deletion lib/wallet/api/http/Cardano/Wallet/Api/Client.hs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ import Cardano.Wallet.Api.Types
, ApiCoinSelectionT
, ApiConstructTransactionDataT
, ApiConstructTransactionT
, ApiDecodeTransactionPostData
, ApiDecodedTransactionT
, ApiFee
, ApiNetworkClock
Expand Down Expand Up @@ -230,7 +231,7 @@ data TransactionClient = TransactionClient
-> ClientM ApiSerialisedTransaction
, decodeTransaction
:: ApiT WalletId
-> ApiSerialisedTransaction
-> ApiDecodeTransactionPostData
-> ClientM (ApiDecodedTransactionT Aeson.Value)
, submitTransaction
:: ApiT WalletId
Expand Down
35 changes: 26 additions & 9 deletions lib/wallet/api/http/Cardano/Wallet/Api/Http/Shelley/Server.hs
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ import Cardano.Wallet.Api.Types
, ApiCoinSelectionWithdrawal (..)
, ApiConstructTransaction (..)
, ApiConstructTransactionData (..)
, ApiDecodeTransactionPostData (..)
, ApiDecodedTransaction (..)
, ApiExternalInput (..)
, ApiFee (..)
Expand Down Expand Up @@ -446,6 +447,7 @@ import Cardano.Wallet.Api.Types
, XPubOrSelf (..)
, getApiMnemonicT
, toApiAsset
, toApiDecodeTransactionPostData
, toApiEra
, toApiNetworkParameters
, toApiUtxoStatistics
Expand Down Expand Up @@ -2619,19 +2621,26 @@ constructTransaction
-> ApiConstructTransactionData n
-> Handler (ApiConstructTransaction n)
constructTransaction api argGenChange knownPools poolStatus apiWalletId body = do
body & \(ApiConstructTransactionData _ _ _ _ _ _ _ _) ->
body & \(ApiConstructTransactionData _ _ _ _ _ _ _ _ _) ->
-- Above is the way to get a compiler error when number of fields changes,
-- in order not to forget to update the pattern below:
case body of
ApiConstructTransactionData
{ payments = Nothing
, withdrawal = Nothing
, metadata = Nothing
, encryptMetadata = Nothing
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏻

, mintBurn = Nothing
, delegations = Nothing
} -> liftHandler $ throwE ErrConstructTxWrongPayload
_ -> pure ()

when (isJust (body ^. #encryptMetadata) && isNothing (body ^. #metadata) ) $
liftHandler $ throwE ErrConstructTxWrongPayload

when (isJust (body ^. #encryptMetadata)) $
liftHandler $ throwE ErrConstructTxNotImplemented

validityInterval <-
liftHandler $ parseValidityInterval ti $ body ^. #validityInterval

Expand Down Expand Up @@ -2801,7 +2810,8 @@ constructTransaction api argGenChange knownPools poolStatus apiWalletId body = d
, encoding = body ^. #encoding
}

apiDecoded <- decodeTransaction @_ @n api apiWalletId balancedTx
apiDecoded <- decodeTransaction @_ @n api apiWalletId
(toApiDecodeTransactionPostData balancedTx)

(_, _, rewardPath) <- handler $ W.readRewardAccount @s db

Expand Down Expand Up @@ -3221,7 +3231,8 @@ constructSharedTransaction
, encoding = body ^. #encoding
}

apiDecoded <- decodeSharedTransaction api (ApiT wid) balancedTx
apiDecoded <- decodeSharedTransaction api (ApiT wid)
(toApiDecodeTransactionPostData balancedTx)
let deposits = case optionalDelegationAction of
Just (JoinRegisteringKey _poolId) ->
[W.getStakeKeyDeposit pp]
Expand Down Expand Up @@ -3277,9 +3288,11 @@ decodeSharedTransaction
:: forall n . HasSNetworkId n
=> ApiLayer (SharedState n SharedKey)
-> ApiT WalletId
-> ApiSerialisedTransaction
-> ApiDecodeTransactionPostData
-> Handler (ApiDecodedTransaction n)
decodeSharedTransaction ctx (ApiT wid) (ApiSerialisedTransaction (ApiT sealed) _) = do
decodeSharedTransaction ctx (ApiT wid) postData = do
let ApiDecodeTransactionPostData (ApiT sealed) decryptMetadata = postData
when (isJust decryptMetadata) $ error "not implemented"
era <- liftIO $ NW.currentNodeEra nl
(txinsOutsPaths, collateralInsOutsPaths, outsPath, pp, certs, txId, fee
, metadata, scriptValidity, interval, witsCount, withdrawals, rewardAcctM)
Expand Down Expand Up @@ -3439,10 +3452,12 @@ decodeTransaction
)
=> ApiLayer s
-> ApiT WalletId
-> ApiSerialisedTransaction
-> ApiDecodeTransactionPostData
-> Handler (ApiDecodedTransaction n)
decodeTransaction
ctx@ApiLayer{..} (ApiT wid) (ApiSerialisedTransaction (ApiT sealed) _) = do
ctx@ApiLayer{..} (ApiT wid) postData = do
let ApiDecodeTransactionPostData (ApiT sealed) decryptMetadata = postData
when (isJust decryptMetadata) $ error "not implemented"
era <- liftIO $ NW.currentNodeEra netLayer
withWorkerCtx ctx wid liftE liftE $ \wrk -> do
(k, _) <- liftHandler $ W.readPolicyPublicKey wrk
Expand Down Expand Up @@ -3575,7 +3590,8 @@ submitTransaction ctx apiw@(ApiT wid) apitx = do

let sealedTx = getApiT . (view #serialisedTxSealed) $ apitx

apiDecoded <- decodeTransaction @s @n ctx apiw apitx
apiDecoded <- decodeTransaction @s @n ctx apiw
(toApiDecodeTransactionPostData apitx)
when (isForeign apiDecoded) $
liftHandler $ throwE ErrSubmitTransactionForeignWallet
let ourOuts = getOurOuts apiDecoded
Expand Down Expand Up @@ -3709,7 +3725,8 @@ submitSharedTransaction ctx apiw@(ApiT wid) apitx = do

let sealedTx = getApiT . (view #serialisedTxSealed) $ apitx

apiDecoded <- decodeSharedTransaction @n ctx apiw apitx
apiDecoded <- decodeSharedTransaction @n ctx apiw
(toApiDecodeTransactionPostData apitx)
when (isForeign apiDecoded) $
liftHandler $ throwE ErrSubmitTransactionForeignWallet
let ourOuts = getOurOuts apiDecoded
Expand Down
34 changes: 34 additions & 0 deletions lib/wallet/api/http/Cardano/Wallet/Api/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ module Cardano.Wallet.Api.Types
, ApiCoinSelectionCollateral (..)
, ApiCoinSelectionOutput (..)
, ApiCoinSelectionWithdrawal (..)
, ApiEncryptMetadata (..)
, ApiConstructTransaction (..)
, ApiConstructTransactionData (..)
, ApiCosignerIndex (..)
Expand Down Expand Up @@ -188,6 +189,9 @@ module Cardano.Wallet.Api.Types
, WalletPutPassphraseMnemonicData (..)
, WalletPutPassphraseOldPassphraseData (..)
, XPubOrSelf (..)
, ApiDecodeTransactionPostData (..)
, fromApiDecodeTransactionPostData
, toApiDecodeTransactionPostData

-- * API Types (Byron)
, ApiByronWallet (..)
Expand Down Expand Up @@ -1198,12 +1202,19 @@ data ApiMultiDelegationAction
deriving (Eq, Generic, Show)
deriving anyclass NFData

newtype ApiEncryptMetadata = ApiEncryptMetadata
{ passphrase :: ApiT (Passphrase "lenient") }
deriving (Eq, Generic, Show)
deriving (FromJSON, ToJSON) via DefaultRecord ApiEncryptMetadata
deriving anyclass NFData

-- | Input parameters for transaction construction.
data ApiConstructTransactionData (n :: NetworkDiscriminant) =
ApiConstructTransactionData
{ payments :: !(Maybe (ApiPaymentDestination n))
, withdrawal :: !(Maybe ApiSelfWithdrawalPostData)
, metadata :: !(Maybe TxMetadataWithSchema)
, encryptMetadata :: !(Maybe ApiEncryptMetadata)
, mintBurn :: !(Maybe (NonEmpty (ApiMintBurnData n)))
, delegations :: !(Maybe (NonEmpty ApiMultiDelegationAction))
, validityInterval :: !(Maybe ApiValidityInterval)
Expand Down Expand Up @@ -1243,6 +1254,24 @@ data ApiValidityBound
deriving (Eq, Generic, Show)
deriving anyclass NFData

data ApiDecodeTransactionPostData = ApiDecodeTransactionPostData
{ transaction :: !(ApiT SealedTx)
, decrypt_metadata :: !(Maybe ApiEncryptMetadata)
}
deriving (Eq, Generic, Show)

fromApiDecodeTransactionPostData
:: ApiDecodeTransactionPostData
-> ApiSerialisedTransaction
fromApiDecodeTransactionPostData (ApiDecodeTransactionPostData sealedtx _) =
ApiSerialisedTransaction sealedtx HexEncoded

toApiDecodeTransactionPostData
:: ApiSerialisedTransaction
-> ApiDecodeTransactionPostData
toApiDecodeTransactionPostData (ApiSerialisedTransaction sealedTx _) =
ApiDecodeTransactionPostData sealedTx Nothing

data ApiSignTransactionPostData = ApiSignTransactionPostData
{ transaction :: !(ApiT SealedTx)
, passphrase :: !(ApiT (Passphrase "lenient"))
Expand Down Expand Up @@ -2569,6 +2598,11 @@ instance ToJSON ApiSerialisedTransaction where
sealedTxBytesValue @'Base64 . getApiT $ tx
]

instance FromJSON ApiDecodeTransactionPostData where
parseJSON = genericParseJSON strictRecordTypeOptions
instance ToJSON ApiDecodeTransactionPostData where
toJSON = genericToJSON strictRecordTypeOptions

instance FromJSON ApiSignTransactionPostData where
parseJSON = genericParseJSON strictRecordTypeOptions
instance ToJSON ApiSignTransactionPostData where
Expand Down
Loading
Loading