Skip to content

Commit

Permalink
Merge pull request #23 from f-o-a-m/add-toethsignedmessage
Browse files Browse the repository at this point in the history
Add toEthSignedMessage function
  • Loading branch information
martyall authored Oct 14, 2020
2 parents 450ab3f + 82ba1cf commit 5fd685a
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Network/Ethereum/Core/Signatures.purs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module Network.Ethereum.Core.Signatures
, publicToAddress
, Signature(..)
, signMessage
, toEthSignedMessage
, recoverSender
, ChainId(..)
, addChainIdOffset
Expand All @@ -38,6 +39,7 @@ import Network.Ethereum.Core.HexString (HexString, takeHex, nullWord, dropHex, h
import Network.Ethereum.Core.Keccak256 (keccak256)
import Partial.Unsafe (unsafePartial)
import Simple.JSON (class ReadForeign, class WriteForeign)
import Type.Quotient (mkQuotient)

-- | Opaque PrivateKey type
newtype PrivateKey = PrivateKey BS.ByteString
Expand Down Expand Up @@ -183,6 +185,14 @@ signMessage privateKey message =
, v
}

-- | Prefix a message with the "Ethereum Signed Message" prefix
toEthSignedMessage :: BS.ByteString -> Maybe BS.ByteString
toEthSignedMessage bs = do
let x19 = BS.singleton (mkQuotient 25) -- 0x19 == 25 dec
pfx <- BS.fromString "Ethereum Signed Message:\n" BS.UTF8
lenStr <- BS.fromString (show $ BS.length bs) BS.UTF8
pure $ x19 <> pfx <> lenStr <> bs

foreign import ecRecover :: Fn3 BS.ByteString BS.ByteString Int PublicKey

-- | Recover the sender of the message from the `Signature`.
Expand Down

0 comments on commit 5fd685a

Please sign in to comment.