From f3860ef9ff7f53f48c5f0a4870dc531f2544e676 Mon Sep 17 00:00:00 2001 From: Pawel Jakubas Date: Tue, 7 Nov 2023 16:08:53 +0100 Subject: [PATCH] use fromMetadataEncrypted in decodeTransaction --- .../Cardano/Wallet/Api/Http/Server/Error.hs | 22 +++++++++++++++++++ .../Cardano/Wallet/Api/Http/Shelley/Server.hs | 20 ++++++++++++----- .../http/Cardano/Wallet/Api/Types/Error.hs | 1 + lib/wallet/src/Cardano/Wallet.hs | 1 + 4 files changed, 38 insertions(+), 6 deletions(-) diff --git a/lib/wallet/api/http/Cardano/Wallet/Api/Http/Server/Error.hs b/lib/wallet/api/http/Cardano/Wallet/Api/Http/Server/Error.hs index 3d042a74c82..ac90951ec84 100644 --- a/lib/wallet/api/http/Cardano/Wallet/Api/Http/Server/Error.hs +++ b/lib/wallet/api/http/Cardano/Wallet/Api/Http/Server/Error.hs @@ -40,6 +40,7 @@ import Cardano.Wallet , ErrConstructTx (..) , ErrCreateMigrationPlan (..) , ErrCreateRandomAddress (..) + , ErrDecodeTx (..) , ErrDerivePublicKey (..) , ErrFetchRewards (..) , ErrGetPolicyId (..) @@ -285,6 +286,7 @@ instance IsServerError WalletException where ExceptionWritePolicyPublicKey e -> toServerError e ExceptionSoftDerivationIndex e -> toServerError e ExceptionHardenedDerivationIndex e -> toServerError e + ExceptionDecodeTx e -> toServerError e instance IsServerError ErrNoSuchWallet where toServerError = \case @@ -418,6 +420,26 @@ instance IsServerError ErrMkTransaction where ErrMkTransactionQuitStakePool e -> toServerError e ErrMkTransactionIncorrectTTL e -> toServerError e +instance IsServerError ErrDecodeTx where + toServerError = \case + ErrDecodeTxIncorrectEncryptedMetadata -> + apiError err403 InvalidMetadataDecryption $ mconcat + [ "It looks like the encrypted metadata has wrong structure. " + , "It should be a map of at most 64-byte blocks." + ] + ErrDecodeTxDecryptMetadata cryptoError -> + apiError err403 InvalidMetadataDecryption $ mconcat + [ "It looks like the encrypted metadata cannot be decrypted. " + , "The exact error is: " + , T.pack (show cryptoError) + ] + ErrDecodeTxDecryptedPayload err -> + apiError err403 InvalidMetadataDecryption $ mconcat + [ "It looks like the decrypted metadata cannot be decoded. " + , "The exact error is: " + , err + ] + instance IsServerError ErrConstructTx where toServerError = \case ErrConstructTxWrongPayload -> diff --git a/lib/wallet/api/http/Cardano/Wallet/Api/Http/Shelley/Server.hs b/lib/wallet/api/http/Cardano/Wallet/Api/Http/Shelley/Server.hs index 4e81f4ab1a9..bb9850d0002 100644 --- a/lib/wallet/api/http/Cardano/Wallet/Api/Http/Shelley/Server.hs +++ b/lib/wallet/api/http/Cardano/Wallet/Api/Http/Shelley/Server.hs @@ -3550,15 +3550,13 @@ decodeTransaction -> Handler (ApiDecodedTransaction n) decodeTransaction 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 let keyhash = KeyHash Policy (xpubToBytes k) - let (decodedTx, toMint, toBurn, allCerts, interval, witsCount) = + (decodedTx, toMint, toBurn, allCerts, interval, witsCount) = decodeTx tl era (ShelleyWalletCtx keyhash) sealed - let Tx { txId + Tx { txId , fee , resolvedInputs , resolvedCollateralInputs @@ -3567,7 +3565,17 @@ decodeTransaction , metadata , scriptValidity } = decodedTx - let db = wrk ^. dbLayer + (ApiDecodeTransactionPostData (ApiT sealed) decryptMetadata ) = + postData + db = wrk ^. dbLayer + metadata' <- case (decryptMetadata, metadata) of + (Just apiDecrypt, Just meta) -> + case fromMetadataEncrypted apiDecrypt meta of + Left err -> + liftHandler $ throwE err + Right (TxMetadataWithSchema _ txmetadata) -> + pure . Just . ApiT $ txmetadata + _ -> pure $ ApiT <$> metadata (acct, _, acctPath) <- liftHandler $ W.shelleyOnlyReadRewardAccount @s db inputPaths <- @@ -3600,7 +3608,7 @@ decodeTransaction , depositsReturned = (Quantity . fromIntegral . unCoin . W.stakeKeyDeposit $ pp) <$ filter ourRewardAccountDeregistration certs - , metadata = ApiTxMetadata $ ApiT <$> metadata + , metadata = ApiTxMetadata metadata' , scriptValidity = ApiT <$> scriptValidity , validityInterval = ApiValidityIntervalExplicit <$> interval , witnessCount = mkApiWitnessCount witsCount diff --git a/lib/wallet/api/http/Cardano/Wallet/Api/Types/Error.hs b/lib/wallet/api/http/Cardano/Wallet/Api/Types/Error.hs index d3981f7f603..bfcbedb909d 100644 --- a/lib/wallet/api/http/Cardano/Wallet/Api/Types/Error.hs +++ b/lib/wallet/api/http/Cardano/Wallet/Api/Types/Error.hs @@ -128,6 +128,7 @@ data ApiErrorInfo | InputsDepleted | InsufficientCollateral | InvalidCoinSelection + | InvalidMetadataDecryption | InvalidValidityBounds | InvalidWalletType | KeyNotFoundForAddress diff --git a/lib/wallet/src/Cardano/Wallet.hs b/lib/wallet/src/Cardano/Wallet.hs index cfc1168a6c5..efb9b7f7449 100644 --- a/lib/wallet/src/Cardano/Wallet.hs +++ b/lib/wallet/src/Cardano/Wallet.hs @@ -3643,6 +3643,7 @@ data WalletException | ExceptionBalanceTxInternalError ErrBalanceTxInternalError | ExceptionSubmitTransaction ErrSubmitTransaction | ExceptionConstructTx ErrConstructTx + | ExceptionDecodeTx ErrDecodeTx | ExceptionGetPolicyId ErrGetPolicyId | ExceptionWitnessTx ErrWitnessTx | ExceptionSubmitTx ErrSubmitTx