Skip to content

Commit 927da7a

Browse files
chessaiEvgenii Akentev
authored andcommitted
change ChainId to Word16, reorder TokenMessage fields
1 parent 130f826 commit 927da7a

File tree

1 file changed

+19
-32
lines changed

1 file changed

+19
-32
lines changed

src/Crypto/Hash/HyperlaneNatives.hs

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
{-# LANGUAGE RecordWildCards #-}
99
{-# LANGUAGE ScopedTypeVariables #-}
1010
{-# LANGUAGE TypeApplications #-}
11+
{-# LANGUAGE TypeOperators #-}
1112

1213
-- | Implementation of Hyperlane natives.
1314
module Crypto.Hash.HyperlaneNatives
@@ -48,7 +49,7 @@ import Data.Text qualified as Text
4849
import Data.Text.Encoding qualified as Text
4950
import Data.Text.Read qualified as Text
5051
import Data.WideWord.Word256 (Word256(..))
51-
import Data.Word (Word8, Word32)
52+
import Data.Word (Word8, Word16, Word32)
5253
import Ethereum.Misc (keccak256, _getKeccak256Hash, _getBytesN)
5354
import Pact.JSON.Decode qualified as J
5455
import Pact.Types.Exp (Literal(..))
@@ -158,9 +159,9 @@ data HyperlaneMessage = HyperlaneMessage
158159
deriving stock (Eq, Show)
159160

160161
data TokenMessageERC20 = TokenMessageERC20
161-
{ tmRecipient :: ByteString -- variable
162-
, tmAmount :: Word256 -- uint256
163-
, tmChainId :: Word256 -- uint256
162+
{ tmAmount :: Word256 -- uint256
163+
, tmChainId :: Word16 -- uint16
164+
, tmRecipient :: ByteString -- variable
164165
}
165166
deriving stock (Eq, Show)
166167

@@ -170,7 +171,7 @@ data TokenMessageERC20 = TokenMessageERC20
170171

171172
packHyperlaneMessage :: HyperlaneMessage -> Builder
172173
packHyperlaneMessage (HyperlaneMessage{..}) =
173-
BB.word8 hmVersion
174+
BB.word8 hmVersion
174175
<> BB.word32BE hmNonce
175176
<> BB.word32BE hmOriginDomain
176177
<> BB.byteString hmSender
@@ -217,31 +218,15 @@ packHyperlaneMessage (HyperlaneMessage{..}) =
217218
-- 5D7D
218219
packTokenMessageERC20 :: TokenMessageERC20 -> Builder
219220
packTokenMessageERC20 t =
220-
word256BE 96
221-
<> word256BE (tmAmount t)
222-
<> word256BE (tmChainId t)
223-
<> word256BE recipientSize
224-
<> BB.byteString recipient
225-
where
226-
(recipient, recipientSize) = padRight (tmRecipient t)
221+
word256BE (tmAmount t)
222+
<> BB.word16BE (tmChainId t)
223+
<> BB.byteString (tmRecipient t)
227224

228225
unpackTokenMessageERC20 :: Get TokenMessageERC20
229226
unpackTokenMessageERC20 = do
230-
firstOffset <- getWord256BE
231-
unless (firstOffset == 96) $ do
232-
fail $ "TokenMessage firstOffset expected 96, found " ++ show firstOffset
233-
234227
tmAmount <- getWord256BE
235-
tmChainId <- getWord256BE
236-
237-
recipientSize <- getWord256BE
238-
tmRecipient <- do
239-
let size = fromIntegral @Word256 @Int recipientSize
240-
recipient <- BS.take size
241-
<$> Bin.getByteString (fromIntegral @Word256 @Int (recipientSize + restSize recipientSize))
242-
if BS.length recipient < size
243-
then fail "TokenMessage recipient was smaller than expected"
244-
else pure recipient
228+
tmChainId <- Bin.getWord16be
229+
tmRecipient <- BL.toStrict <$> Bin.getRemainingLazyByteString
245230

246231
pure $ TokenMessageERC20 {..}
247232

@@ -274,11 +259,13 @@ decodeBase64AndValidate key expected s = do
274259

275260
return decoded
276261

277-
parseChainId :: Text -> Either HyperlaneError Word256
262+
parseChainId :: forall a. (a ~ Word16) => Text -> Either HyperlaneError a
278263
parseChainId s = do
279-
cid <- first (HyperlaneErrorInvalidChainId . Text.pack) $ Text.decimal s
280-
unless (fst cid >= 0) $ throwError $ HyperlaneErrorInvalidChainId "can't be negative"
281-
return $ fst cid
264+
(cid, _) <- first (HyperlaneErrorInvalidChainId . Text.pack) $ Text.decimal s
265+
unless (cid >= minBound && cid <= maxBound) $ do
266+
throwError $ HyperlaneErrorInvalidChainId $ Text.pack $
267+
"ChainId must be in [" <> show @a minBound <> ", " <> show @a maxBound <> "]"
268+
pure cid
282269

283270
------------------------------------------------------
284271
-- Hyperlane Message Pact Object Decoding --
@@ -370,8 +357,8 @@ getWord256BE = do
370357
--
371358
-- > padRight "hello world"
372359
-- ("hello world\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL",11)
373-
padRight :: ByteString -> (ByteString, Word256)
374-
padRight s =
360+
_padRight :: ByteString -> (ByteString, Word256)
361+
_padRight s =
375362
let
376363
size = BS.length s
377364
missingZeroes = restSize size

0 commit comments

Comments
 (0)