Skip to content

Commit

Permalink
Miscellaneous formatting fixes.
Browse files Browse the repository at this point in the history
This commit makes the following fixes to formatting:

  - Replace hanging vertical alignment with newline characters and
    fixed-width indentation.

  - Wrap lines to fit within the 80-column line length limit.

  - Move closing parentheses to the same column as opening parentheses for
    multi-line parenthetical expressions.
  • Loading branch information
jonathanknowles committed May 7, 2024
1 parent 78798e7 commit c2d8bb9
Show file tree
Hide file tree
Showing 5 changed files with 286 additions and 140 deletions.
25 changes: 15 additions & 10 deletions lib/api/src/Cardano/Wallet/Api/Http/Shelley/Server.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2752,15 +2752,16 @@ constructTransaction api argGenChange knownPools poolStatus apiWalletId body = d
liftHandler $ throwE ErrConstructTxWrongPayload

metadata <- case (body ^. #encryptMetadata, body ^. #metadata) of
(Just apiEncrypt, Just metadataWithSchema) -> do
salt <- liftIO $ genSalt 8
case toMetadataEncrypted apiEncrypt metadataWithSchema (Just salt) of
(Just apiEncrypt, Just metadataWithSchema) -> do
salt <- liftIO $ genSalt 8
toMetadataEncrypted apiEncrypt metadataWithSchema (Just salt)
& \case
Left err ->
liftHandler $ throwE err
Right meta ->
pure $ Just meta
_ ->
pure $ body ^? #metadata . traverse . #txMetadataWithSchema_metadata
_ ->
pure $ body ^? #metadata . traverse . #txMetadataWithSchema_metadata

validityInterval <-
liftHandler $ parseValidityInterval ti $ body ^. #validityInterval
Expand Down Expand Up @@ -3187,11 +3188,13 @@ toMetadataEncrypted apiEncrypt payload saltM = do
keyAndValueCond k v =
k == 674 && isJust (inspectMetaPair v)
findMsgValue =
let (Cardano.TxMetadata themap) = payload ^. #txMetadataWithSchema_metadata
let Cardano.TxMetadata themap =
payload ^. #txMetadataWithSchema_metadata
filteredMap = Map.filterWithKey keyAndValueCond themap
in if Map.size filteredMap >= 1 then
in
if Map.size filteredMap >= 1 then
Right $ Map.toList filteredMap
else
else
Left ErrConstructTxIncorrectRawMetadata
encryptPairIfQualifies pair@(Cardano.TxMetaText metaField, metaValue) =
if metaField == "msg" then
Expand All @@ -3208,7 +3211,8 @@ toMetadataEncrypted apiEncrypt payload saltM = do
[ ( Cardano.TxMetaText metaField
, Cardano.TxMetaList
( map Cardano.TxMetaText $ flip toTextChunks [] $
toBase64Text enc )
toBase64Text enc
)
)
, encMethodEntry
]
Expand All @@ -3227,7 +3231,8 @@ toMetadataEncrypted apiEncrypt payload saltM = do
pure (key, Cardano.TxMetaMap $ concat pairs')
encryptingMsg _ = error "encryptingMsg should have TxMetaMap value"
updateTxMetadata =
let (Cardano.TxMetadata themap) = payload ^. #txMetadataWithSchema_metadata
let Cardano.TxMetadata themap =
payload ^. #txMetadataWithSchema_metadata
in Cardano.TxMetadata . foldr (uncurry Map.insert) themap

metadataPBKDF2Config :: PBKDF2Config SHA256
Expand Down
3 changes: 2 additions & 1 deletion lib/api/src/Cardano/Wallet/Api/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1253,7 +1253,8 @@ instance FromJSON EncryptMetadataMethod where

data ApiEncryptMetadata = ApiEncryptMetadata
{ passphrase :: ApiT (Passphrase "lenient")
, enc :: Maybe EncryptMetadataMethod }
, enc :: Maybe EncryptMetadataMethod
}
deriving (Eq, Generic, Show)
deriving (FromJSON, ToJSON) via DefaultRecord ApiEncryptMetadata
deriving anyclass NFData
Expand Down
4 changes: 1 addition & 3 deletions lib/crypto-primitives/src/Cryptography/Cipher/AES256CBC.hs
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,7 @@ decrypt mode key iv msg = do
WithoutPadding -> Right p
WithPadding -> maybeToEither EmptyPayload (PKCS7.unpad p)

getSaltFromEncrypted
:: ByteString
-> Maybe ByteString
getSaltFromEncrypted :: ByteString -> Maybe ByteString
getSaltFromEncrypted msg = do
when (BS.length msg < 32) Nothing
let (prefix,rest) = BS.splitAt 8 msg
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -556,16 +556,18 @@ spec = describe "NEW_SHELLEY_TRANSACTIONS" $ do
it "TRANS_NEW_CREATE_02c - Incorrect metadata structure to be encrypted" $
\ctx -> runResourceT $ do
let metadataRaw =
TxMetadata (Map.fromList
[ (0,TxMetaText "hello")
, (1,TxMetaMap [(TxMetaText "hello", TxMetaText "world")])
, (50, TxMetaNumber 1_245)
])
TxMetadata $ Map.fromList
[ (0,TxMetaText "hello")
, (1,TxMetaMap [(TxMetaText "hello", TxMetaText "world")])
, (50, TxMetaNumber 1_245)
]
wa <- fixtureWallet ctx
let metadataToBeEncrypted =
TxMetadataWithSchema TxMetadataNoSchema metadataRaw
let encryptMetadata =
ApiEncryptMetadata (ApiT $ Passphrase "metadata-secret") Nothing
ApiEncryptMetadata
(ApiT $ Passphrase "metadata-secret")
Nothing
let payload = Json [json|{
"encrypt_metadata": #{toJSON encryptMetadata},
"metadata": #{toJSON metadataToBeEncrypted}
Expand All @@ -577,32 +579,34 @@ spec = describe "NEW_SHELLEY_TRANSACTIONS" $ do
]
decodeErrorInfo rTx `shouldBe` InvalidMetadataEncryption

it "TRANS_NEW_CREATE_02c - Correct metadata structure to be encrypted - short" $
it "TRANS_NEW_CREATE_02c - \
\Correct metadata structure to be encrypted - short" $
\ctx -> runResourceT $ do
let toBeEncrypted = TxMetaText "world"
let metadataRaw =
TxMetadata (Map.fromList
[ (0, TxMetaText "hello")
, (674, TxMetaMap
[(TxMetaText "msg", toBeEncrypted)])
, (50, TxMetaNumber 1_245)
])
TxMetadata $ Map.fromList
[ (0, TxMetaText "hello")
, (674, TxMetaMap [(TxMetaText "msg", toBeEncrypted)])
, (50, TxMetaNumber 1_245)
]
checkMetadataEncryption ctx toBeEncrypted metadataRaw

it "TRANS_NEW_CREATE_02c - Correct metadata structure to be encrypted - long" $
it "TRANS_NEW_CREATE_02c - \
\Correct metadata structure to be encrypted - long" $
\ctx -> runResourceT $ do
let toBeEncrypted = TxMetaList
let toBeEncrypted =
TxMetaList
[ TxMetaText "Hard times create strong men."
, TxMetaText "Strong men create good times."
, TxMetaText "Good times create weak men."
, TxMetaText "And, weak men create hard times."]
, TxMetaText "And, weak men create hard times."
]
let metadataRaw =
TxMetadata (Map.fromList
[ (0, TxMetaText "hello")
, (674, TxMetaMap
[(TxMetaText "msg", toBeEncrypted)])
, (50, TxMetaNumber 1_245)
])
TxMetadata $ Map.fromList
[ (0, TxMetaText "hello")
, (674, TxMetaMap [(TxMetaText "msg", toBeEncrypted)])
, (50, TxMetaNumber 1_245)
]
checkMetadataEncryption ctx toBeEncrypted metadataRaw

it "TRANS_NEW_CREATE_03a - Withdrawal from self, 0 rewards" $ \ctx -> runResourceT $ do
Expand Down Expand Up @@ -5328,7 +5332,8 @@ spec = describe "NEW_SHELLEY_TRANSACTIONS" $ do
let ApiSerialisedTransaction apiTx _ = getFromResponse #transaction rTx
signedTx <- signTx ctx wa apiTx [ expectResponseCode HTTP.status202 ]
let era = fromApiEra $ _mainEra ctx
let tx = cardanoTxIdeallyNoLaterThan era $ getApiT (signedTx ^. #serialisedTxSealed)
let tx = cardanoTxIdeallyNoLaterThan era $
getApiT (signedTx ^. #serialisedTxSealed)

let extractTxt (Cardano.TxMetaText txt) = txt
extractTxt _ =
Expand All @@ -5343,8 +5348,8 @@ spec = describe "NEW_SHELLEY_TRANSACTIONS" $ do
]) -> foldl T.append T.empty $ extractTxt <$> chunks
Just _ -> error "Tx metadata incorrect"

-- we retriev salt from the encypted msg, then encrypt the value in `msg`
-- field and compare
-- we retriev salt from the encypted msg, then encrypt the value in
-- `msg` field and compare
let (Just salt) = getSaltFromEncrypted $ unsafeFromBase64 encryptedMsg
let pwd = BA.convert $ unPassphrase $ getApiT pwdApiT
let (key, iv) = generateKey metadataPBKDF2Config pwd (Just salt)
Expand Down

0 comments on commit c2d8bb9

Please sign in to comment.