Skip to content

Commit 5fd685a

Browse files
authored
Merge pull request #23 from f-o-a-m/add-toethsignedmessage
Add toEthSignedMessage function
2 parents 450ab3f + 82ba1cf commit 5fd685a

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

src/Network/Ethereum/Core/Signatures.purs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ module Network.Ethereum.Core.Signatures
1515
, publicToAddress
1616
, Signature(..)
1717
, signMessage
18+
, toEthSignedMessage
1819
, recoverSender
1920
, ChainId(..)
2021
, addChainIdOffset
@@ -38,6 +39,7 @@ import Network.Ethereum.Core.HexString (HexString, takeHex, nullWord, dropHex, h
3839
import Network.Ethereum.Core.Keccak256 (keccak256)
3940
import Partial.Unsafe (unsafePartial)
4041
import Simple.JSON (class ReadForeign, class WriteForeign)
42+
import Type.Quotient (mkQuotient)
4143

4244
-- | Opaque PrivateKey type
4345
newtype PrivateKey = PrivateKey BS.ByteString
@@ -183,6 +185,14 @@ signMessage privateKey message =
183185
, v
184186
}
185187

188+
-- | Prefix a message with the "Ethereum Signed Message" prefix
189+
toEthSignedMessage :: BS.ByteString -> Maybe BS.ByteString
190+
toEthSignedMessage bs = do
191+
let x19 = BS.singleton (mkQuotient 25) -- 0x19 == 25 dec
192+
pfx <- BS.fromString "Ethereum Signed Message:\n" BS.UTF8
193+
lenStr <- BS.fromString (show $ BS.length bs) BS.UTF8
194+
pure $ x19 <> pfx <> lenStr <> bs
195+
186196
foreign import ecRecover :: Fn3 BS.ByteString BS.ByteString Int PublicKey
187197

188198
-- | Recover the sender of the message from the `Signature`.

0 commit comments

Comments
 (0)