Skip to content

Commit

Permalink
tidy
Browse files Browse the repository at this point in the history
  • Loading branch information
martyall committed Apr 21, 2024
1 parent 0dadc22 commit b80e96d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
8 changes: 5 additions & 3 deletions src/Network/Ethereum/Core/HexString.purs
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,14 @@ fromUtf8 s = unsafePartial fromJust $
let
s' = unsafePartial $ split (Pattern "\x0000") s `unsafeIndex` 0
in
mkHexString $ B.toString Hex $ B.fromString s' UTF8
mkHexString $ B.toString Hex $ B.fromString s' UTF8

-- | Get the 'HexString' corresponding to the ASCII encoding.
fromAscii :: String -> HexString
fromAscii s = unsafePartial fromJust $
mkHexString $ B.toString Hex $ B.fromString s ASCII
fromAscii s = unsafePartial fromJust
$ mkHexString
$ B.toString Hex
$ B.fromString s ASCII

toBuffer :: HexString -> B.ImmutableBuffer
toBuffer hx = B.fromString (unHex hx) Hex
Expand Down
12 changes: 7 additions & 5 deletions src/Network/Ethereum/Core/Signatures.purs
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,12 @@ signMessage privateKey message =
-- | Prefix a message with the "Ethereum Signed Message" prefix
toEthSignedMessage :: B.ImmutableBuffer -> B.ImmutableBuffer
toEthSignedMessage bs =
let x19 = B.fromArray [25] -- 0x19 == 25 dec
pfx = B.fromString "Ethereum Signed Message:\n" UTF8
lenStr = B.fromString (show $ B.size bs) UTF8
in B.concat [x19, pfx, lenStr, bs]
let
x19 = B.fromArray [ 25 ] -- 0x19 == 25 dec
pfx = B.fromString "Ethereum Signed Message:\n" UTF8
lenStr = B.fromString (show $ B.size bs) UTF8
in
B.concat [ x19, pfx, lenStr, bs ]

foreign import ecRecover :: Fn3 B.ImmutableBuffer B.ImmutableBuffer Int PublicKey

Expand All @@ -210,7 +212,7 @@ recoverSender
-> Signature
-> PublicKey
recoverSender messageHash (Signature { v, r, s }) =
runFn3 ecRecover messageHash (B.concat [toBuffer r, toBuffer s]) v
runFn3 ecRecover messageHash (B.concat [ toBuffer r, toBuffer s ]) v

-- | Used in Ethereum to prevent replay attacks
newtype ChainId = ChainId Int
Expand Down
6 changes: 3 additions & 3 deletions test/Spec/RLP.purs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ rlpSpec = do
testStringB = B.fromString testString UTF8
encoded = RLP.rlpEncode $ RLP.RLPBuffer testStringB

B.getAtOffset 0 encoded `shouldEqual` Just 184
B.getAtOffset 1 encoded `shouldEqual` Just 86
B.getAtOffset 0 encoded `shouldEqual` Just 184
B.getAtOffset 1 encoded `shouldEqual` Just 86

it "can match the RLP encoding for nested arrays" $ do
let
Expand Down Expand Up @@ -100,4 +100,4 @@ rlpSpec = do
chainId = Sig.ChainId 4
rlpEncodingString = "f84729843b9aca0082a2c59486b89c0906b111508d5caa38e2e61689a124c86080a4d14e62b80000000000000000000000000000000000000000000000000000000000000001048080"
-- hashedMessageString = "f5bbe123594fb9f588c98b069063ce1d224917b7374c700e49ec4fce0f63f999"
B.toString Hex (makeTransactionMessage chainId rawTx) `shouldEqual` rlpEncodingString
B.toString Hex (makeTransactionMessage chainId rawTx) `shouldEqual` rlpEncodingString

0 comments on commit b80e96d

Please sign in to comment.