Skip to content

Commit 332c4eb

Browse files
authored
Merge pull request #28 from f-o-a-m/even-hexstrings
Standardize HexString
2 parents 0655cd4 + e9cd6bd commit 332c4eb

File tree

3 files changed

+23
-36
lines changed

3 files changed

+23
-36
lines changed

src/Network/Ethereum/Core/HexString.purs

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ module Network.Ethereum.Core.HexString
55
, asSigned
66
, mkHexString
77
, unHex
8-
, hexLength
9-
, dropHex
10-
, takeHex
8+
, numberOfBytes
9+
, dropBytes
10+
, takeBytes
1111
, nullWord
1212
, getPadLength
1313
, padLeftSigned
@@ -45,7 +45,6 @@ import Network.Ethereum.Core.BigNumber (BigNumber, toString, hexadecimal)
4545
import Node.Encoding (Encoding(Hex, UTF8, ASCII))
4646
import Partial.Unsafe (unsafePartial)
4747
import Simple.JSON (class ReadForeign, class WriteForeign)
48-
import Text.Parsing.Parser.String (class StringLike)
4948

5049
--------------------------------------------------------------------------------
5150
-- * Signed Values
@@ -58,13 +57,6 @@ derive instance eqSign :: Eq Sign
5857
-- | Represents values that can be either positive or negative.
5958
data Signed a = Signed Sign a
6059

61-
instance showSigned :: Show a => Show (Signed a) where
62-
show (Signed s a) = s' <> show a
63-
where
64-
s' = case s of
65-
Pos -> ""
66-
Neg -> "-"
67-
6860
instance eqSigned :: Eq a => Eq (Signed a) where
6961
eq (Signed s a) (Signed s' a') = (s `eq` s') && (a `eq` a')
7062

@@ -83,17 +75,13 @@ asSigned a = Signed Pos a
8375
newtype HexString = HexString String
8476

8577
instance showHexString :: Show HexString where
86-
show (HexString hx) = "0x" <> hx
87-
88-
instance hexStringEq :: Eq HexString where
89-
eq (HexString h1) (HexString h2) = S.toLower h1 == S.toLower h2
78+
show (HexString s) = "0x" <> s
9079

80+
derive newtype instance hexStringEq :: Eq HexString
9181
derive newtype instance hexStringOrd :: Ord HexString
9282
derive newtype instance semigpStringEq :: Semigroup HexString
9383
derive newtype instance monoidStringEq :: Monoid HexString
9484

95-
derive newtype instance stringLikeHexString :: StringLike HexString
96-
9785
_encode :: HexString -> String
9886
_encode = append "0x" <<< unHex
9987

@@ -147,15 +135,14 @@ mkHexString str | S.length str `mod` 2 /= 0 = Nothing
147135
then go tail
148136
else Nothing
149137

150-
-- | Compute the length of the hex string, which is twice the number of bytes it represents
151-
hexLength :: HexString -> Int
152-
hexLength (HexString hx) = S.length hx
138+
numberOfBytes :: HexString -> Int
139+
numberOfBytes (HexString hx) = S.length hx `div` 2
153140

154-
takeHex :: Int -> HexString -> HexString
155-
takeHex n (HexString hx) = HexString $ S.take n hx
141+
takeBytes :: Int -> HexString -> HexString
142+
takeBytes n (HexString hx) = HexString $ S.take (2 * n) hx
156143

157-
dropHex :: Int -> HexString -> HexString
158-
dropHex n (HexString hx) = HexString $ S.drop n hx
144+
dropBytes :: Int -> HexString -> HexString
145+
dropBytes n (HexString hx) = HexString $ S.drop (2 * n) hx
159146

160147
nullWord :: HexString
161148
nullWord = HexString "0000000000000000000000000000000000000000000000000000000000000000"
@@ -168,23 +155,23 @@ nullWord = HexString "0000000000000000000000000000000000000000000000000000000000
168155
-- | Computes the number of 0s needed to pad a bytestring of the input length
169156
getPadLength :: Int -> Int
170157
getPadLength len =
171-
let n = len `mod` 64
172-
in if n == 0 then 0 else 64 - n
158+
let n = len `mod` 32
159+
in if n == 0 then 0 else 32 - n
173160

174161
-- | Pad a `Signed HexString` on the left until it has length == 0 mod 64.
175162
padLeftSigned :: Signed HexString -> HexString
176163
padLeftSigned (Signed s hx) =
177-
let padLength = getPadLength $ hexLength hx
164+
let padLength = getPadLength $ numberOfBytes hx
178165
sgn = if s `eq` Pos then '0' else 'f'
179-
padding = unsafePartial fromJust <<< mkHexString <<< fromCharArray <<< replicate padLength $ sgn
166+
padding = unsafePartial fromJust <<< mkHexString <<< fromCharArray <<< replicate (2 * padLength) $ sgn
180167
in padding <> hx
181168

182169
-- | Pad a `Signed HexString` on the right until it has length 0 mod 64.
183170
padRightSigned :: Signed HexString -> HexString
184171
padRightSigned (Signed s hx) =
185-
let padLength = getPadLength $ hexLength hx
172+
let padLength = getPadLength $ numberOfBytes hx
186173
sgn = if s `eq` Pos then '0' else 'f'
187-
padding = unsafePartial fromJust <<< mkHexString <<< fromCharArray <<< replicate padLength $ sgn
174+
padding = unsafePartial fromJust <<< mkHexString <<< fromCharArray <<< replicate (2 * padLength) $ sgn
188175
in hx <> padding
189176

190177
-- | Pad a `HexString` on the left with '0's until it has length == 0 mod 64.

src/Network/Ethereum/Core/Keccak256.purs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import Prelude
88

99
import Data.ByteString (ByteString, fromString)
1010
import Data.Maybe (fromJust)
11-
import Network.Ethereum.Core.HexString (HexString, fromByteString, takeHex, toByteString)
11+
import Network.Ethereum.Core.HexString (HexString, fromByteString, takeBytes, toByteString)
1212
import Node.Encoding (Encoding(UTF8))
1313
import Partial.Unsafe (unsafePartial)
1414

@@ -32,4 +32,4 @@ instance keccak256HexString :: Keccak256 HexString where
3232

3333
-- | convert a string representing a type signature into a selector
3434
toSelector :: String -> HexString
35-
toSelector = takeHex 8 <<< fromByteString <<< keccak256
35+
toSelector = takeBytes 4 <<< fromByteString <<< keccak256

src/Network/Ethereum/Core/Signatures.purs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import Data.Maybe (Maybe(..), fromJust)
3535
import Effect (Effect)
3636
import Foreign (ForeignError(..), fail)
3737
import Foreign.Class (class Decode, class Encode, decode, encode)
38-
import Network.Ethereum.Core.HexString (HexString, takeHex, nullWord, dropHex, hexLength, toByteString, fromByteString)
38+
import Network.Ethereum.Core.HexString (HexString, takeBytes, nullWord, dropBytes, numberOfBytes, toByteString, fromByteString)
3939
import Network.Ethereum.Core.Keccak256 (keccak256)
4040
import Partial.Unsafe (unsafePartial)
4141
import Simple.JSON (class ReadForeign, class WriteForeign)
@@ -140,10 +140,10 @@ unAddress :: Address -> HexString
140140
unAddress (Address a) = a
141141

142142
mkAddress :: HexString -> Maybe Address
143-
mkAddress hx = if hexLength hx == 40 then Just <<< Address $ hx else Nothing
143+
mkAddress hx = if numberOfBytes hx == 20 then Just <<< Address $ hx else Nothing
144144

145145
nullAddress :: Address
146-
nullAddress = Address $ takeHex 40 nullWord
146+
nullAddress = Address $ takeBytes 20 nullWord
147147

148148
-- | Produce the `Address` corresponding to the `PrivateKey`.
149149
privateToAddress
@@ -157,7 +157,7 @@ publicToAddress
157157
-> Address
158158
publicToAddress (PublicKey publicKey) =
159159
let addrHex = fromByteString $ keccak256 publicKey
160-
in unsafePartial fromJust <<< mkAddress $ dropHex 24 addrHex
160+
in unsafePartial fromJust <<< mkAddress $ dropBytes 12 addrHex
161161

162162
newtype Signature =
163163
Signature { r :: HexString

0 commit comments

Comments
 (0)