Skip to content

Commit f9683f0

Browse files
committed
Normalized address storage.
All addresses are stored in lowercase form. If you want to see it as a checksummed address, use Eth.Utils.addressToChecksumString. Eth.Utils.addressFromString still checks for EIP-55 compliance if input is mixed case. Signed-off-by: Coury Ditch <[email protected]>
1 parent d10a16f commit f9683f0

File tree

3 files changed

+23
-19
lines changed

3 files changed

+23
-19
lines changed

src/Eth/Sentry/Event.elm

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,10 @@ init tagger nodePath =
7676
)
7777

7878

79-
{-|
79+
{-| Returns the first log found.
8080
81-
Returns the first log found.
82-
83-
If a block range is defined in the LogFilter,
84-
this will only return the first log found within that given block range.
81+
If a block range is defined in the LogFilter,
82+
this will only return the first log found within that given block range.
8583
8684
-}
8785
watchOnce : (Log -> msg) -> EventSentry msg -> LogFilter -> ( EventSentry msg, Cmd msg )
@@ -90,15 +88,13 @@ watchOnce onReceive eventSentry logFilter =
9088
|> (\( eventSentry_, cmd, _ ) -> ( eventSentry_, cmd ))
9189

9290

93-
{-|
94-
95-
Continuously polls for logs in newly mined blocks.
91+
{-| Continuously polls for logs in newly mined blocks.
9692
97-
If the range within the LogFilter includes past blocks,
98-
then all events within the given block range are returned,
99-
along with events in the latest block.
93+
If the range within the LogFilter includes past blocks,
94+
then all events within the given block range are returned,
95+
along with events in the latest block.
10096
101-
Polling continues until `stopWatching` is called.
97+
Polling continues until `stopWatching` is called.
10298
10399
-}
104100
watch : (Log -> msg) -> EventSentry msg -> LogFilter -> ( EventSentry msg, Cmd msg, Ref )

src/Eth/Utils.elm

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,17 @@ toAddress str =
9999

100100
emptyZerosInBytes32 =
101101
String.left 24 noZeroX
102+
103+
normalize =
104+
String.toLower >> Internal.Address >> Ok
102105
in
103106
-- Address is always stored without "0x"
104107
if String.length noZeroX == 64 && String.all ((==) '0') emptyZerosInBytes32 then
105108
if isUpperCaseAddress bytes32Address || isLowerCaseAddress bytes32Address then
106-
Ok <| Internal.Address bytes32Address
109+
normalize bytes32Address
107110

108111
else if isChecksumAddress bytes32Address then
109-
Ok <| Internal.Address bytes32Address
112+
normalize bytes32Address
110113

111114
else
112115
Err <| "Given address " ++ quote str ++ " failed the EIP-55 checksum test."
@@ -118,10 +121,10 @@ toAddress str =
118121
Err <| "Given address " ++ quote str ++ " contains invalid hex characters."
119122

120123
else if isUpperCaseAddress noZeroX || isLowerCaseAddress noZeroX then
121-
Ok <| Internal.Address noZeroX
124+
normalize noZeroX
122125

123126
else if isChecksumAddress noZeroX then
124-
Ok <| Internal.Address noZeroX
127+
normalize noZeroX
125128

126129
else
127130
Err <| "Given address " ++ quote str ++ " failed the EIP-55 checksum test."

tests/Address.elm

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,25 @@ toAddressTests =
1919
, test "from uppercase address with 0x" <|
2020
\_ ->
2121
Eth.toAddress "0XF85FEEA2FDD81D51177F6B8F35F0E6734CE45F5F"
22-
|> Expect.equal (Ok <| Internal.Address "F85FEEA2FDD81D51177F6B8F35F0E6734CE45F5F")
22+
|> Expect.equal (Ok <| Internal.Address "f85feea2fdd81d51177f6b8f35f0e6734ce45f5f")
2323
, test "from evm" <|
2424
\_ ->
2525
Eth.toAddress "000000000000000000000000f85feea2fdd81d51177f6b8f35f0e6734ce45f5f"
2626
|> Expect.equal (Ok <| Internal.Address "f85feea2fdd81d51177f6b8f35f0e6734ce45f5f")
2727
, test "from already checksummed" <|
2828
\_ ->
2929
Eth.toAddress "0xe4219dc25D6a05b060c2a39e3960A94a214aAeca"
30-
|> Expect.equal (Ok <| Internal.Address "e4219dc25D6a05b060c2a39e3960A94a214aAeca")
30+
|> Expect.equal (Ok <| Internal.Address "e4219dc25d6a05b060c2a39e3960a94a214aaeca")
3131
, test "addressToString" <|
3232
\_ ->
3333
Eth.toAddress "0XF85FEEA2FDD81D51177F6B8F35F0E6734CE45F5F"
3434
|> Result.map Eth.addressToString
35-
|> Expect.equal (Ok "0xF85FEEA2FDD81D51177F6B8F35F0E6734CE45F5F")
35+
|> Expect.equal (Ok "0xf85feea2fdd81d51177f6b8f35f0e6734ce45f5f")
36+
, test "addressToChecksumString" <|
37+
\_ ->
38+
Eth.toAddress "0xe4219dc25d6a05b060c2a39e3960a94a214aaeca"
39+
|> Result.map Eth.addressToChecksumString
40+
|> Expect.equal (Ok "0xe4219dc25D6a05b060c2a39e3960A94a214aAeca")
3641
]
3742
, describe "toAddress fails"
3843
[ test "from short address with 0x" <|

0 commit comments

Comments
 (0)