-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'chessai/add-keccak256-native' into rsoeldner/keccak-fv
- Loading branch information
Showing
11 changed files
with
115 additions
and
86 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 |
---|---|---|
@@ -1,57 +1,54 @@ | ||
{-# LANGUAGE DataKinds #-} | ||
{-# LANGUAGE DeriveAnyClass #-} | ||
{-# LANGUAGE DerivingStrategies #-} | ||
{-# LANGUAGE ImportQualifiedPost #-} | ||
{-# LANGUAGE OverloadedStrings #-} | ||
{-# LANGUAGE RankNTypes #-} | ||
{-# LANGUAGE ScopedTypeVariables #-} | ||
{-# LANGUAGE TypeApplications #-} | ||
|
||
-- | Implementation of the `keccack256` pact native. | ||
-- | Implementation of the `keccak256` pact native. | ||
-- | ||
-- `keccak256` 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.Keccak256Native (keccak256) where | ||
module Crypto.Hash.Keccak256Native (Keccak256Error(..), keccak256) where | ||
|
||
import Control.Exception (Exception(..), SomeException(..), try) | ||
import Control.Lens ((^.)) | ||
import Control.Monad (forM_) | ||
import Control.Monad.Catch (throwM) | ||
import Data.ByteString.Base64 qualified as Base64 | ||
import Data.ByteString.Short qualified as BSS | ||
import Data.Hash.Class.Mutable (initialize, finalize, updateByteString) | ||
import Data.Hash.Internal.OpenSSL (OpenSslException(..)) | ||
import Data.Hash.Keccak (Keccak256(..)) | ||
import Data.Text (Text) | ||
import Data.Text.Encoding qualified as Text | ||
import Data.Vector (Vector) | ||
import Pact.Types.Term (Term, Name, _TLitString) | ||
import Pact.Types.Util (encodeBase64UrlUnpadded, decodeBase64UrlUnpadded) | ||
import System.IO.Unsafe (unsafePerformIO) | ||
|
||
keccak256 :: Vector (Term Name) -> Text | ||
data Keccak256Error | ||
= Keccak256OpenSslException String | ||
| Keccak256Base64Exception String | ||
deriving stock (Eq, Show) | ||
deriving anyclass (Exception) | ||
|
||
keccak256 :: Vector Text -> Either Keccak256Error Text | ||
keccak256 strings = unsafePerformIO $ do | ||
e <- try @SomeException @_ $ do | ||
ctx <- initialize @Keccak256 | ||
forM_ strings $ \string -> do | ||
let s = string ^. _TLitString | ||
case Base64.decode (Text.encodeUtf8 s) of | ||
case decodeBase64UrlUnpadded (Text.encodeUtf8 string) of | ||
Left b64Err -> do | ||
throwM (Base64Exception b64Err) | ||
throwM (Keccak256Base64Exception b64Err) | ||
Right bytes -> do | ||
updateByteString @Keccak256 ctx bytes | ||
Keccak256 hash <- finalize ctx | ||
pure (BSS.fromShort hash) | ||
case e of | ||
Left err | ||
| Just (OpenSslException _) <- fromException err -> error "keccak256 failed" | ||
| Just (Base64Exception _) <- fromException err -> error "keccak256 failed: invalid base64 input" | ||
| Just (OpenSslException msg) <- fromException err -> pure (Left (Keccak256OpenSslException msg)) | ||
| Just (exc :: Keccak256Error) <- fromException err -> pure (Left exc) | ||
| otherwise -> error "keccak256 failed" | ||
Right hash -> pure (Text.decodeUtf8 (Base64.encode hash)) | ||
Right hash -> pure (Right (Text.decodeUtf8 (encodeBase64UrlUnpadded hash))) | ||
{-# noinline keccak256 #-} | ||
|
||
newtype Base64Exception = Base64Exception String | ||
deriving stock (Show) | ||
|
||
instance Exception Base64Exception where | ||
displayException (Base64Exception err) = | ||
"base64 decode exception: " ++ err |
This file contains 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 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 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.