@@ -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)
4545import Node.Encoding (Encoding (Hex, UTF8, ASCII))
4646import Partial.Unsafe (unsafePartial )
4747import 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.
5958data 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-
6860instance 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
8375newtype HexString = HexString String
8476
8577instance 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
9181derive newtype instance hexStringOrd :: Ord HexString
9282derive newtype instance semigpStringEq :: Semigroup HexString
9383derive 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
160147nullWord :: HexString
161148nullWord = HexString " 0000000000000000000000000000000000000000000000000000000000000000"
@@ -168,23 +155,23 @@ nullWord = HexString "0000000000000000000000000000000000000000000000000000000000
168155-- | Computes the number of 0s needed to pad a bytestring of the input length
169156getPadLength :: Int -> Int
170157getPadLength 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.
175162padLeftSigned :: Signed HexString -> HexString
176163padLeftSigned (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.
183170padRightSigned :: Signed HexString -> HexString
184171padRightSigned (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.
0 commit comments