Skip to content

Commit

Permalink
use fromMetadataEncrypted in decodeTransaction
Browse files Browse the repository at this point in the history
  • Loading branch information
paweljakubas committed Nov 8, 2023
1 parent 7719239 commit f3860ef
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 6 deletions.
22 changes: 22 additions & 0 deletions lib/wallet/api/http/Cardano/Wallet/Api/Http/Server/Error.hs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import Cardano.Wallet
, ErrConstructTx (..)
, ErrCreateMigrationPlan (..)
, ErrCreateRandomAddress (..)
, ErrDecodeTx (..)
, ErrDerivePublicKey (..)
, ErrFetchRewards (..)
, ErrGetPolicyId (..)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 ->
Expand Down
20 changes: 14 additions & 6 deletions lib/wallet/api/http/Cardano/Wallet/Api/Http/Shelley/Server.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 <-
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/wallet/api/http/Cardano/Wallet/Api/Types/Error.hs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ data ApiErrorInfo
| InputsDepleted
| InsufficientCollateral
| InvalidCoinSelection
| InvalidMetadataDecryption
| InvalidValidityBounds
| InvalidWalletType
| KeyNotFoundForAddress
Expand Down
1 change: 1 addition & 0 deletions lib/wallet/src/Cardano/Wallet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3643,6 +3643,7 @@ data WalletException
| ExceptionBalanceTxInternalError ErrBalanceTxInternalError
| ExceptionSubmitTransaction ErrSubmitTransaction
| ExceptionConstructTx ErrConstructTx
| ExceptionDecodeTx ErrDecodeTx
| ExceptionGetPolicyId ErrGetPolicyId
| ExceptionWitnessTx ErrWitnessTx
| ExceptionSubmitTx ErrSubmitTx
Expand Down

0 comments on commit f3860ef

Please sign in to comment.