Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chessai authored and Evgenii Akentev committed Jun 6, 2024
1 parent 927da7a commit b81ecfa
Showing 1 changed file with 51 additions and 65 deletions.
116 changes: 51 additions & 65 deletions tests/HyperlaneSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,23 @@
module HyperlaneSpec (spec) where

import Control.Lens ((^?), at, _Just, _1)
import Crypto.Hash.HyperlaneNatives (hyperlaneMessageId)
import Crypto.Hash.HyperlaneNatives (HyperlaneMessage(..), TokenMessageERC20(..), hyperlaneMessageId, decodeHyperlaneMessageObject, packTokenMessageERC20, unpackTokenMessageERC20)
import Data.Binary.Get qualified as Bin
import Data.Default (def)
import Data.ByteString.Builder qualified as BB
import Data.ByteString.Lazy qualified as BL
import Data.Either (fromRight)
import Data.Map (Map)
import Data.Map.Strict qualified as Map
import Data.Text (Text)
import Data.Text.Encoding qualified as Text
import Pact.Types.Runtime (FieldKey, Object(..), ObjectMap(..), Term, Literal(..), tLit, tStr, asString, toTObject, Type(..), _TObject, Name)
import Pact.Types.Util (encodeBase64UrlUnpadded)
import Pact.Types.Util (decodeBase64UrlUnpadded, encodeBase64UrlUnpadded)
import Test.Hspec

data Reference = Reference
{ object :: Object Name
, tokenMessageText :: Text
, messageId :: Text
}

Expand All @@ -35,46 +40,64 @@ testRefs refs = describe "hyperlane" $ mapM_ (uncurry testRef) (zip [0..] refs)
| Just message <- unwrapObject ref.object ^? at "message" . _Just . _TObject . _1 = message
| otherwise = error "Extracting HyperlaneMessage Object failed"

let
hyperlaneMessage :: HyperlaneMessage
hyperlaneMessage = fromRight (error "Decoding reference hyperlane message failed") $ do
decodeHyperlaneMessageObject hyperlaneMessageObject

let
tokenMessage :: TokenMessageERC20
tokenMessage = Bin.runGet unpackTokenMessageERC20 (BL.fromStrict (hmMessageBody hyperlaneMessage))

it "Computes the correct message id" $ do
hyperlaneMessageId hyperlaneMessageObject `shouldBe` Right ref.messageId

it "TokenMessage encoding matches reference" $ do
let hexMessage = Text.decodeUtf8 (encodeBase64UrlUnpadded (BL.toStrict (BB.toLazyByteString (packTokenMessageERC20 tokenMessage))))
hexMessage `shouldBe` ref.tokenMessageText

-- TODO: This only applies on ETH -> KDA (when TokenMessage recipient is a guard)
--it "TokenMessage decodes properly into a Pact Term" $ do
-- hyperlaneDecodeTokenMessage ref.tokenMessageText `shouldBe` tokenMessageToTerm tokenMessage

-- Recipient info
-- ETH -> KDA = Guard
-- KDA -> ETH = ETH Address

spec :: Spec
spec = testRefs
[ let
tokenMessageText = Text.decodeUtf8 $ encodeBase64UrlUnpadded $ mconcat
[ "0000000000000000000000000000000000000000000000000000000000000060" -- offset (decimal 96)
, "000000000000000000000000000000000000000000000001158e460913d00000" -- amount
, "0000000000000000000000000000000000000000000000000000000000000000" -- chainId
, "000000000000000000000000000000000000000000000000000000000000005f" -- recipient length
, "7b2270726564223a226b6579732d616c6c222c226b657973223a5b2265356462" -- recipient
, "3335393733663534343634326362386231353339636238626466303339636665" -- |
, "31316535663765313132376131343662643261366431336432386334225d7d00" -- V
]
in Reference
{ object = mkObject
tokenMsgText = Text.decodeUtf8 $ encodeBase64UrlUnpadded $ BL.toStrict $ BB.toLazyByteString $
packTokenMessageERC20 $ TokenMessageERC20
{ tmAmount = 10
, tmChainId = 0
, tmRecipient = Text.encodeUtf8 "{\"pred\":\"keys-all\",\"keys\":[\"e5db35973f544642cb8b1539cb8bdf039cfe11e5f7e1127a146bd2a6d13d28c4\"]}"
}
in
Reference
{ object = mkObject
[ ("message",) $ obj
[ ("version", tLit $ LInteger 3)
, ("nonce", tLit $ LInteger 0)
, ("originDomain", tLit $ LInteger 31_337)
, ("sender", tStr $ asString ("AAAAAAAAAAAAAAAAf6k4W-ECrD6sKXSD3WIz1is-FJY" :: Text))
, ("destinationDomain", tLit $ LInteger 626)
, ("recipient", tStr $ asString ("AAAAAAAAAADpgrOqkM0BOY-FQnNzkDXuYlsVcf50GRU" :: Text))
, ("messageBody", tStr tokenMessageText)
, ("messageBody", tStr tokenMsgText)
]
]
, messageId = "ip_5uS6XKg_ptmgG5FZNfDh5qXrE4-tMjbNNO4XY5K0"
, messageId = "E9W6As6Nqv0tW66uxgQjzKekSS23utRfWzwsIN7HEqw"
, tokenMessageText = tokenMsgText
}
, let
tokenMessageText = Text.decodeUtf8 $ encodeBase64UrlUnpadded $ mconcat
[ "0000000000000000000000000000000000000000000000000000000000000060" -- offset (decimal 96)
, "00000000000000000000000000000000000000000000000006f05b59d3b20000" -- amount
, "0000000000000000000000000000000000000000000000000000000000000000" -- chainId
, "000000000000000000000000000000000000000000000000000000000000005f" -- recipient length
, "7b2270726564223a226b6579732d616c6c222c226b657973223a5b2239346333" -- recipient
, "3561623162643730323433656336373034393530373766373834363337336234" -- |
, "64633565393737396437613637333262356365623666646530353963225d7d00" -- V
]
in Reference
tokenMsgText = Text.decodeUtf8 $ encodeBase64UrlUnpadded $ BL.toStrict $ BB.toLazyByteString $
packTokenMessageERC20 $ TokenMessageERC20
{ tmAmount = 10
, tmChainId = 0
, tmRecipient = fromRight (error "failed to decode TokenMessage recipient") $ decodeBase64UrlUnpadded "cSOeAK6UKzlLOpGrIp5SZK2Db28"
}
in
Reference
{ object = mkObject
[ ("message",) $ obj
[ ("version", tLit $ LInteger 3)
Expand All @@ -83,10 +106,11 @@ spec = testRefs
, ("sender", tStr $ asString ("AAAAAAAAAAAAAAAAf6k4W-ECrD6sKXSD3WIz1is-FJY" :: Text))
, ("destinationDomain", tLit $ LInteger 626)
, ("recipient", tStr $ asString ("AAAAAAAAAADpgrOqkM0BOY-FQnNzkDXuYlsVcf50GRU" :: Text))
, ("messageBody", tStr tokenMessageText)
, ("messageBody", tStr tokenMsgText)
]
]
, messageId = "v2Ou0yuW17zbxzopxOjQj5uL2xza78YAqUsEBLal36M"
, messageId = "kJGRc6EIp8d5c2jWYE87_X64P-MQGP8zrCLJ0GyjNE4"
, tokenMessageText = tokenMsgText
}
]

Expand All @@ -98,41 +122,3 @@ obj = toTObject TyAny def

unwrapObject :: Object n -> Map FieldKey (Term n)
unwrapObject o = _objectMap (_oObject o)

{-
"version": 3,
"nonce": 0,
"originDomain": 31337,
"sender": "0x0000000000000000000000006171479a003d1d89915dd9e71657620313870283",
"destinationDomain": 626,
"recipient": "0x676a5f45557a44534f6e54497a4d72676c6e725f77584b56494454467a773465",
"tokenMessage": {
"amount": 0.5,
"chainId": 0,
"recipient": "{\"keys\":[\"94c35ab1bd70243ec670495077f7846373b4dc5e9779d7a6732b5ceb6fde059c\"],\"pred\":\"keys-all\"}"
}
-}

{-
expected:
0000000000000000000000000000000000000000000000000000000000000060 -- | tag (96)
00000000000000000000000000000000000000000000000006f05b59d3b20000 -- | amount
0000000000000000000000000000000000000000000000000000000000000000 -- | chainId
000000000000000000000000000000000000000000000000000000000000005f -- | recipient length
7b2270726564223a226b6579732d616c6c222c226b657973223a5b2239346333 -- | recipient
3561623162643730323433656336373034393530373766373834363337336234 |
64633565393737396437613637333262356365623666646530353963225d7d00 |
but got:
0000000000000000000000000000000000000000000000000000000000000060 -- | tag (96)
00000000000000000000000000000000000000000000000006f05b59d3b20000 -- | amount
0000000000000000000000000000000000000000000000000000000000000000 -- | chainId
000000000000000000000000000000000000000000000000000000000000005f -- | recipient length
7b226b657973223a5b2239346333356162316264373032343365633637303439 -- | recipient
3530373766373834363337336234646335653937373964376136373332623563 |
65623666646530353963225d2c2270726564223a226b6579732d616c6c227d00 |
-}

0 comments on commit b81ecfa

Please sign in to comment.