This repository was archived by the owner on Jan 9, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 116
add hyperlane-message-id native #1335
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
37acd74
add hyperlane-message-id native
chessai b2ddfe4
remove benchmark code
chessai f1b629c
add unit test
chessai 843d41c
convert milligas to gas in hyperlane-message-id defaultGasTable
chessai ce99a62
factor out repetitive prisms
chessai eec6720
add hyperlane-message-id repl test
chessai 01253f0
add example to hyperlane-message-id Pact Native
chessai 0ea1b30
round hyperlane-message-id gas constant up instead of down
chessai a20e39a
add module-level documentation to HyperlaneMessageId.hs
chessai e563729
move ghc-option for no missed extra shared lib to cabal.project
chessai a51220b
move demon let into where
chessai 9fdb3f1
regenerate docs
chessai 76b6173
move demon let to where
chessai 9383f57
add a gas model golden test for enforce-verifier
chessai a21115a
add hyperlane-message-id behind DisableVerifiers flag
chessai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,170 @@ | ||
| {-# LANGUAGE DataKinds #-} | ||
| {-# LANGUAGE ImportQualifiedPost #-} | ||
| {-# LANGUAGE NumericUnderscores #-} | ||
| {-# LANGUAGE OverloadedStrings #-} | ||
| {-# LANGUAGE RankNTypes #-} | ||
| {-# LANGUAGE RecordWildCards #-} | ||
| {-# LANGUAGE TypeApplications #-} | ||
|
|
||
| -- | Implementation of the `hyperlane-message-id` pact native. | ||
| -- | ||
| -- `hyperlane-message-id` takes as input a Pact object representing a | ||
| -- 'HyperlaneMessage', and returns a base16-encoded hash of the abi-encoding | ||
| -- of the input. | ||
| module Crypto.Hash.HyperlaneMessageId (hyperlaneMessageId) where | ||
|
|
||
| import Control.Error.Util (hush) | ||
| import Control.Lens ((^?), at, _Just, Prism', _1) | ||
| import Data.ByteString (ByteString) | ||
| import Data.ByteString qualified as BS | ||
| import Data.ByteString.Base16 qualified as Base16 | ||
| import Data.ByteString.Builder (Builder) | ||
| import Data.ByteString.Builder qualified as BB | ||
| import Data.ByteString.Lazy qualified as BL | ||
| import Data.ByteString.Short qualified as BSS | ||
| import Data.Decimal (Decimal) | ||
| import Data.Map (Map) | ||
| import Data.Text (Text) | ||
| import Data.Text qualified as Text | ||
| import Data.Text.Encoding qualified as Text | ||
| import Data.WideWord.Word256 (Word256(..)) | ||
| import Data.Word (Word8, Word32) | ||
| import Ethereum.Misc (keccak256, _getKeccak256Hash, _getBytesN) | ||
| import Pact.Types.Runtime (Object(..), ObjectMap(..), FieldKey, Name, Literal, _TLiteral, _TObject, _LDecimal, _LInteger, _LString) | ||
| import Pact.Types.Term (Term) | ||
|
|
||
| ---------------------------------------------- | ||
| -- Primitive -- | ||
| ---------------------------------------------- | ||
|
|
||
| hyperlaneMessageId :: Object Name -> Text | ||
| hyperlaneMessageId o = case decodeHyperlaneMessageObject o of | ||
| Nothing -> error "Couldn't decode HyperlaneMessage" | ||
| Just hm -> getHyperlaneMessageId hm | ||
|
|
||
| ---------------------------------------------- | ||
| -- Hyperlane Message Encoding -- | ||
| ---------------------------------------------- | ||
|
|
||
| data HyperlaneMessage = HyperlaneMessage | ||
| { hmVersion :: Word8 -- uint8 | ||
| , hmNonce :: Word32 -- uint32 | ||
| , hmOriginDomain :: Word32 -- uint32 | ||
| , hmSender :: ByteString -- 32x uint8 | ||
| , hmDestinationDomain :: Word32 -- uint32 | ||
| , hmRecipient :: ByteString -- 32x uint8 | ||
| , hmTokenMessage :: TokenMessageERC20 -- variable | ||
| } | ||
|
|
||
| packHyperlaneMessage :: HyperlaneMessage -> Builder | ||
| packHyperlaneMessage (HyperlaneMessage{..}) = | ||
| BB.word8 hmVersion | ||
| <> BB.word32BE hmNonce | ||
| <> BB.word32BE hmOriginDomain | ||
| <> BB.byteString (padLeft hmSender) | ||
| <> BB.word32BE hmDestinationDomain | ||
| <> BB.byteString (padLeft hmRecipient) | ||
| <> packTokenMessageERC20 hmTokenMessage | ||
|
|
||
| data TokenMessageERC20 = TokenMessageERC20 | ||
| { tmRecipient :: Text -- variable | ||
| , tmAmount :: Word256 -- uint256 | ||
| , tmChainId :: Maybe Word256 -- uint256 | ||
| } | ||
|
|
||
| packTokenMessageERC20 :: TokenMessageERC20 -> Builder | ||
| packTokenMessageERC20 t = | ||
| word256BE 64 | ||
| <> word256BE (tmAmount t) | ||
|
|
||
| <> word256BE recipientSize | ||
| <> BB.byteString recipient | ||
| where | ||
| (recipient, recipientSize) = padRight (Text.encodeUtf8 (tmRecipient t)) | ||
|
|
||
| word256BE :: Word256 -> Builder | ||
| word256BE (Word256 a b c d) = | ||
| BB.word64BE a <> BB.word64BE b <> BB.word64BE c <> BB.word64BE d | ||
|
|
||
| -- | Pad with zeroes on the left to 32 bytes | ||
| -- | ||
| -- > padLeft "hello world" | ||
| -- "\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NULhello world" | ||
| padLeft :: ByteString -> ByteString | ||
| padLeft s = BS.replicate (32 - BS.length s) 0 <> s | ||
|
|
||
| -- | Pad with zeroes on the right, such that the resulting size is a multiple of 32. | ||
| -- | ||
| -- > padRight "hello world" | ||
| -- ("hello world\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL",11) | ||
| padRight :: ByteString -> (ByteString, Word256) | ||
| padRight s = | ||
| let | ||
| size = BS.length s | ||
| missingZeroes = restSize size | ||
| in (s <> BS.replicate missingZeroes 0, fromIntegral size) | ||
|
|
||
| -- | Returns the modular of 32 bytes. | ||
| restSize :: Integral a => a -> a | ||
| restSize size = (32 - size) `mod` 32 | ||
|
|
||
| ---------------------------------------------- | ||
| -- Hyperlane Message Hashing -- | ||
| ---------------------------------------------- | ||
|
|
||
| getHyperlaneMessageId :: HyperlaneMessage -> Text | ||
| getHyperlaneMessageId = | ||
| encodeHex | ||
| . keccak256Hash | ||
| . BL.toStrict | ||
| . BB.toLazyByteString | ||
| . packHyperlaneMessage | ||
|
|
||
| keccak256Hash :: ByteString -> ByteString | ||
| keccak256Hash = BSS.fromShort . _getBytesN . _getKeccak256Hash . keccak256 | ||
|
|
||
| encodeHex :: ByteString -> Text | ||
| encodeHex b = "0x" <> Text.decodeUtf8 (Base16.encode b) | ||
|
|
||
| decodeHex :: Text -> Maybe ByteString | ||
| decodeHex s = do | ||
| h <- Text.stripPrefix "0x" s | ||
| hush (Base16.decode (Text.encodeUtf8 h)) | ||
|
|
||
| ---------------------------------------------- | ||
| -- Hyperlane Pact Object Decoding -- | ||
| ---------------------------------------------- | ||
|
|
||
| decodeHyperlaneMessageObject :: Object Name -> Maybe HyperlaneMessage | ||
| decodeHyperlaneMessageObject o = do | ||
| let om = _objectMap (_oObject o) | ||
|
|
||
| hmVersion <- fromIntegral @Integer @Word8 <$> grabField om "version" _LInteger | ||
| hmNonce <- fromIntegral @Integer @Word32 <$> grabField om "nonce" _LInteger | ||
| hmOriginDomain <- fromIntegral @Integer @Word32 <$> grabField om "originDomain" _LInteger | ||
| hmSender <- Text.encodeUtf8 <$> grabField om "sender" _LString | ||
| hmDestinationDomain <- fromIntegral @Integer @Word32 <$> grabField om "destinationDomain" _LInteger | ||
| hmRecipient <- decodeHex =<< grabField om "recipient" _LString | ||
|
|
||
| let tokenObject = om ^? at "tokenMessage" . _Just . _TObject . _1 | ||
| hmTokenMessage <- case decodeTokenMessageERC20 =<< tokenObject of | ||
| Just t -> pure t | ||
| _ -> error "Couldn't encode TokenMessageERC20" | ||
|
|
||
| pure HyperlaneMessage{..} | ||
|
|
||
| decodeTokenMessageERC20 :: Object Name -> Maybe TokenMessageERC20 | ||
| decodeTokenMessageERC20 o = do | ||
| let om = _objectMap (_oObject o) | ||
| tmRecipient <- grabField om "recipient" _LString | ||
| tmAmount <- decimalToWord <$> grabField om "amount" _LDecimal | ||
| let tmChainId = Nothing | ||
| pure $ TokenMessageERC20{..} | ||
|
|
||
| decimalToWord :: Decimal -> Word256 | ||
| decimalToWord d = | ||
| let ethInWei = 1_000_000_000_000_000_000 -- 1e18 | ||
| in round $ d * ethInWei | ||
|
|
||
| grabField :: Map FieldKey (Term Name) -> FieldKey -> Prism' Literal a -> Maybe a | ||
| grabField m key p = m ^? at key . _Just . _TLiteral . _1 . p |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.