Skip to content

Commit

Permalink
Encrypt metadata spec and types (#4176)
Browse files Browse the repository at this point in the history
<!--
Detail in a few bullet points the work accomplished in this PR.

Before you submit, don't forget to:

* Make sure the GitHub PR fields are correct:
   ✓ Set a good Title for your PR.
   ✓ Assign yourself to the PR.
   ✓ Assign one or more reviewer(s).
   ✓ Link to a Jira issue, and/or other GitHub issues or PRs.
   ✓ In the PR description delete any empty sections
     and all text commented in <!--, so that this text does not appear
     in merge commit messages.

* Don't waste reviewers' time:
   ✓ If it's a draft, select the Create Draft PR option.
✓ Self-review your changes to make sure nothing unexpected slipped
through.

* Try to make your intent clear:
   ✓ Write a good Description that explains what this PR is meant to do.
   ✓ Jira will detect and link to this PR once created, but you can also
     link this PR in the description of the corresponding Jira ticket.
   ✓ Highlight what Testing you have done.
   ✓ Acknowledge any changes required to the Documentation.
-->

- [x] Adding spec and change swagger accordingly
- [x] Add field `encrypt_metadata` to `ApiConstructTransactionPostData`
- [x] Introduce ApiDecodeTransactionPostData`
- [x] deal with all code adjustment
- [x] adjust unit tests

### Issue Number

ADP-322
  • Loading branch information
paweljakubas authored Nov 16, 2023
2 parents bf42709 + c42044d commit fd9eb91
Show file tree
Hide file tree
Showing 10 changed files with 3,871 additions and 2,658 deletions.
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 @@ -359,6 +359,7 @@ import Cardano.Wallet.Api.Types
, ApiCoinSelectionWithdrawal (..)
, ApiConstructTransaction (..)
, ApiConstructTransactionData (..)
, ApiDecodeTransactionPostData (..)
, ApiDecodedTransaction (..)
, ApiExternalInput (..)
, ApiFee (..)
Expand Down Expand Up @@ -452,6 +453,7 @@ import Cardano.Wallet.Api.Types
, XPubOrSelf (..)
, getApiMnemonicT
, toApiAsset
, toApiDecodeTransactionPostData
, toApiEra
, toApiNetworkParameters
, toApiUtxoStatistics
Expand Down Expand Up @@ -2625,19 +2627,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
, 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 @@ -2807,7 +2816,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 @@ -3227,7 +3237,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 @@ -3283,9 +3294,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 @@ -3449,10 +3462,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 @@ -3585,7 +3600,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 @@ -3719,7 +3735,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

0 comments on commit fd9eb91

Please sign in to comment.