From 0dadc22ac31138f36d6a6d66247394a882a26c9c Mon Sep 17 00:00:00 2001 From: martyall Date: Sun, 21 Apr 2024 07:25:52 -0700 Subject: [PATCH 1/2] remove purescript-bytestring dep --- packages.dhall | 34 +------------ spago.dhall | 2 - src/Network/Ethereum/Core/HexString.purs | 26 +++++----- src/Network/Ethereum/Core/Keccak256.purs | 18 ++++--- src/Network/Ethereum/Core/RLP.purs | 10 ++-- src/Network/Ethereum/Core/Signatures.purs | 58 +++++++++++------------ test/Common.purs | 4 +- test/Spec/Hex.purs | 12 ++--- test/Spec/Keccak256.purs | 18 +++---- test/Spec/RLP.purs | 40 ++++++++-------- 10 files changed, 92 insertions(+), 130 deletions(-) diff --git a/packages.dhall b/packages.dhall index 2db3580..5e949b4 100644 --- a/packages.dhall +++ b/packages.dhall @@ -2,36 +2,4 @@ let upstream = https://github.com/purescript/package-sets/releases/download/psc-0.15.15-20240416/packages.dhall sha256:ca727657c01cc31d0e79c2113b59126b9826f4b56d20a8193be3c725599fb754 - -let additions = - { bytestrings = - { dependencies = - [ "arrays" - , "console" - , "effect" - , "exceptions" - , "foldable-traversable" - , "integers" - , "leibniz" - , "maybe" - , "newtype" - , "node-buffer" - , "partial" - , "prelude" - , "quickcheck" - , "quickcheck-laws" - , "quotient" - , "unsafe-coerce" - ] - , repo = - "https://github.com/rightfold/purescript-bytestrings" - , version = "6733a32fca306015b3428e9985ffac65325a9864" - } - , quotient = - { dependencies = [ "prelude", "quickcheck" ] - , repo = "https://github.com/rightfold/purescript-quotient.git" - , version = "v3.0.0" - } - } - -in upstream // additions +in upstream diff --git a/spago.dhall b/spago.dhall index 929ec41..b3d5e19 100644 --- a/spago.dhall +++ b/spago.dhall @@ -2,7 +2,6 @@ , dependencies = [ "argonaut" , "arrays" - , "bytestrings" , "effect" , "either" , "foldable-traversable" @@ -19,7 +18,6 @@ , "ordered-collections" , "partial" , "prelude" - , "quotient" , "simple-json" , "strings" , "unfoldable" diff --git a/src/Network/Ethereum/Core/HexString.purs b/src/Network/Ethereum/Core/HexString.purs index 72f27de..475d372 100644 --- a/src/Network/Ethereum/Core/HexString.purs +++ b/src/Network/Ethereum/Core/HexString.purs @@ -16,8 +16,8 @@ module Network.Ethereum.Core.HexString , fromUtf8 , toAscii , fromAscii - , toByteString - , fromByteString + , toBuffer + , fromBuffer , generator ) where @@ -27,7 +27,7 @@ import Control.Monad.Gen (class MonadGen, oneOf) import Data.Argonaut as A import Data.Array (fold, fromFoldable, replicate, unsafeIndex) import Data.Array.NonEmpty as NEA -import Data.ByteString as BS +import Node.Buffer.Immutable as B import Data.Either (Either(..), either) import Data.List.Lazy (replicateM) import Data.Maybe (Maybe(..), fromJust) @@ -137,14 +137,14 @@ nullWord = HexString "0000000000000000000000000000000000000000000000000000000000 -- | This breaks at the first null octet, following the web3 function `toUft8`. -- Since 'split' always returns a nonempty list, this index is actually safe. toUtf8 :: HexString -> String -toUtf8 hx = flip BS.toString UTF8 $ bs (unHex hx) +toUtf8 hx = B.toString UTF8 $ bs (unHex hx) where - bs :: String -> BS.ByteString - bs hxstr = unsafePartial fromJust $ BS.fromString hxstr Hex + bs :: String -> B.ImmutableBuffer + bs hxstr = B.fromString hxstr Hex -- | Takes a hex string and produces the corresponding ASCII decoded string. toAscii :: HexString -> String -toAscii hx = flip BS.toString ASCII $ unsafePartial $ fromJust $ BS.fromString (unHex hx) Hex +toAscii hx = B.toString ASCII $ B.fromString (unHex hx) Hex -- | Get the 'HexString' corresponding to the UTF8 encoding. fromUtf8 :: String -> HexString @@ -152,18 +152,18 @@ fromUtf8 s = unsafePartial fromJust $ let s' = unsafePartial $ split (Pattern "\x0000") s `unsafeIndex` 0 in - BS.fromString s' UTF8 >>= (pure <<< flip BS.toString Hex) >>= mkHexString + mkHexString $ B.toString Hex $ B.fromString s' UTF8 -- | Get the 'HexString' corresponding to the ASCII encoding. fromAscii :: String -> HexString fromAscii s = unsafePartial fromJust $ - BS.fromString s ASCII >>= (pure <<< flip BS.toString Hex) >>= mkHexString + mkHexString $ B.toString Hex $ B.fromString s ASCII -toByteString :: HexString -> BS.ByteString -toByteString hx = unsafePartial fromJust (BS.fromString (unHex hx) Hex) +toBuffer :: HexString -> B.ImmutableBuffer +toBuffer hx = B.fromString (unHex hx) Hex -fromByteString :: BS.ByteString -> HexString -fromByteString bs = unsafePartial fromJust $ mkHexString (BS.toString bs Hex) +fromBuffer :: B.ImmutableBuffer -> HexString +fromBuffer bs = unsafePartial $ fromJust $ mkHexString $ B.toString Hex bs -------------------------------------------------------------------------------- diff --git a/src/Network/Ethereum/Core/Keccak256.purs b/src/Network/Ethereum/Core/Keccak256.purs index 5665ed3..6965ff0 100644 --- a/src/Network/Ethereum/Core/Keccak256.purs +++ b/src/Network/Ethereum/Core/Keccak256.purs @@ -6,30 +6,28 @@ module Network.Ethereum.Core.Keccak256 import Prelude -import Data.ByteString (ByteString, fromString) -import Data.Maybe (fromJust) -import Network.Ethereum.Core.HexString (HexString, fromByteString, takeBytes, toByteString) +import Node.Buffer.Immutable (ImmutableBuffer, fromString) +import Network.Ethereum.Core.HexString (HexString, fromBuffer, takeBytes, toBuffer) import Node.Encoding (Encoding(UTF8)) -import Partial.Unsafe (unsafePartial) -------------------------------------------------------------------------------- -- | A class for things which you can hash. Mostly used as a utility for calculating selectors and -- | event topics class Keccak256 a where - keccak256 :: a -> ByteString + keccak256 :: a -> ImmutableBuffer -foreign import _keccak256 :: ByteString -> ByteString +foreign import _keccak256 :: ImmutableBuffer -> ImmutableBuffer -instance Keccak256 ByteString where +instance Keccak256 ImmutableBuffer where keccak256 = _keccak256 instance Keccak256 String where - keccak256 = keccak256 <<< unsafePartial fromJust <<< flip fromString UTF8 + keccak256 s = keccak256 $ fromString s UTF8 instance Keccak256 HexString where - keccak256 = keccak256 <<< toByteString + keccak256 = keccak256 <<< toBuffer -- | convert a string representing a type signature into a selector toSelector :: String -> HexString -toSelector = takeBytes 4 <<< fromByteString <<< keccak256 +toSelector = takeBytes 4 <<< fromBuffer <<< keccak256 diff --git a/src/Network/Ethereum/Core/RLP.purs b/src/Network/Ethereum/Core/RLP.purs index 4509112..5af8237 100644 --- a/src/Network/Ethereum/Core/RLP.purs +++ b/src/Network/Ethereum/Core/RLP.purs @@ -5,14 +5,14 @@ module Network.Ethereum.Core.RLP ) where import Prelude -import Data.ByteString (ByteString) +import Node.Buffer.Immutable (ImmutableBuffer) import Network.Ethereum.Core.BigNumber (BigNumber) import Network.Ethereum.Core.HexString (HexString, unHex) import Network.Ethereum.Core.Signatures (Address, unAddress) import Unsafe.Coerce (unsafeCoerce) class RLPEncode a where - rlpEncode :: a -> ByteString + rlpEncode :: a -> ImmutableBuffer data RLPObject = RLPNull @@ -21,7 +21,7 @@ data RLPObject | RLPAddress Address | RLPInt Int | RLPBigNumber BigNumber - | RLPByteString ByteString + | RLPBuffer ImmutableBuffer | RLPArray (Array RLPObject) data RLPVal = RLPVal @@ -38,13 +38,13 @@ transRLP obj = case obj of RLPHexString hx -> mkRLPVal $ "0x" <> (unHex hx) RLPAddress addr -> transRLP <<< RLPHexString $ unAddress addr RLPBigNumber bn -> mkRLPVal bn - RLPByteString bs -> mkRLPVal bs + RLPBuffer bs -> mkRLPVal bs RLPArray as -> mkRLPVal $ map transRLP as where mkRLPVal :: forall a. a -> RLPVal mkRLPVal = unsafeCoerce -foreign import _rlpEncode :: RLPVal -> ByteString +foreign import _rlpEncode :: RLPVal -> ImmutableBuffer instance RLPEncode RLPObject where rlpEncode = _rlpEncode <<< transRLP diff --git a/src/Network/Ethereum/Core/Signatures.purs b/src/Network/Ethereum/Core/Signatures.purs index ee00d8d..7bf1113 100644 --- a/src/Network/Ethereum/Core/Signatures.purs +++ b/src/Network/Ethereum/Core/Signatures.purs @@ -28,7 +28,7 @@ import Prelude import Control.Monad.Gen (class MonadGen) import Data.Argonaut (JsonDecodeError(..)) import Data.Argonaut as A -import Data.ByteString as BS +import Node.Buffer.Immutable as B import Data.Either (Either(..), either) import Data.Function.Uncurried (Fn2, Fn3, runFn2, runFn3) import Data.Generic.Rep (class Generic) @@ -36,42 +36,42 @@ import Data.Maybe (Maybe(..), fromJust) import Data.Show.Generic (genericShow) import Effect (Effect) import Foreign (ForeignError(..), fail) -import Network.Ethereum.Core.HexString (HexString, dropBytes, fromByteString, nullWord, numberOfBytes, takeBytes, toByteString) +import Network.Ethereum.Core.HexString (HexString, dropBytes, fromBuffer, nullWord, numberOfBytes, takeBytes, toBuffer) import Network.Ethereum.Core.HexString as Hex import Network.Ethereum.Core.Keccak256 (keccak256) import Partial.Unsafe (unsafePartial) import Simple.JSON (class ReadForeign, readImpl, class WriteForeign, writeImpl) -import Type.Quotient (mkQuotient) +import Node.Encoding (Encoding(UTF8)) -- | Opaque PrivateKey type -newtype PrivateKey = PrivateKey BS.ByteString +newtype PrivateKey = PrivateKey B.ImmutableBuffer instance Show PrivateKey where - show (PrivateKey pk) = show $ fromByteString pk + show (PrivateKey pk) = show $ fromBuffer pk derive instance Eq PrivateKey -- | Opaque PublicKey type -newtype PublicKey = PublicKey BS.ByteString +newtype PublicKey = PublicKey B.ImmutableBuffer instance Show PublicKey where - show (PublicKey pk) = show $ fromByteString pk + show (PublicKey pk) = show $ fromBuffer pk derive instance Eq PublicKey foreign import isValidPublic - :: BS.ByteString + :: B.ImmutableBuffer -> Boolean foreign import isValidPrivate - :: BS.ByteString + :: B.ImmutableBuffer -> Boolean -- | Get the underlying `HexString` representation of a PublicKey. unPublicKey :: PublicKey -> HexString -unPublicKey (PublicKey pk) = fromByteString pk +unPublicKey (PublicKey pk) = fromBuffer pk -- | Attempt to construct a PublicKey from a HexString mkPublicKey @@ -79,25 +79,25 @@ mkPublicKey -> Maybe PublicKey mkPublicKey publicKey = let - publicKeyBS = toByteString publicKey + publicKeyB = toBuffer publicKey in - if isValidPublic publicKeyBS then Just $ PublicKey publicKeyBS + if isValidPublic publicKeyB then Just $ PublicKey publicKeyB else Nothing -- | Get the underlying `HexString` representation of a PrivateKey unPrivateKey :: PrivateKey -> HexString -unPrivateKey (PrivateKey pk) = fromByteString pk +unPrivateKey (PrivateKey pk) = fromBuffer pk mkPrivateKey :: HexString -> Maybe PrivateKey mkPrivateKey privateKey = let - privateKeyBS = toByteString privateKey + privateKeyB = toBuffer privateKey in - if isValidPrivate privateKeyBS then Just $ PrivateKey privateKeyBS + if isValidPrivate privateKeyB then Just $ PrivateKey privateKeyB else Nothing -- | Produce the `PublicKey` for the corresponding `PrivateKey`. @@ -160,7 +160,7 @@ publicToAddress -> Address publicToAddress (PublicKey publicKey) = let - addrHex = fromByteString $ keccak256 publicKey + addrHex = fromBuffer $ keccak256 publicKey in unsafePartial fromJust <<< mkAddress $ dropBytes 12 addrHex @@ -177,40 +177,40 @@ derive instance Eq Signature instance Show Signature where show = genericShow -foreign import ecSign :: Fn2 PrivateKey BS.ByteString { r :: BS.ByteString, s :: BS.ByteString, v :: Int } +foreign import ecSign :: Fn2 PrivateKey B.ImmutableBuffer { r :: B.ImmutableBuffer, s :: B.ImmutableBuffer, v :: Int } -- | Sign the message with a `PrivateKey` signMessage :: PrivateKey - -> BS.ByteString + -> B.ImmutableBuffer -> Signature signMessage privateKey message = let { r, s, v } = runFn2 ecSign privateKey message in Signature - { r: fromByteString r - , s: fromByteString s + { r: fromBuffer r + , s: fromBuffer s , 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 +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] -foreign import ecRecover :: Fn3 BS.ByteString BS.ByteString Int PublicKey +foreign import ecRecover :: Fn3 B.ImmutableBuffer B.ImmutableBuffer Int PublicKey -- | Recover the sender of the message from the `Signature`. recoverSender - :: BS.ByteString + :: B.ImmutableBuffer -> Signature -> PublicKey recoverSender messageHash (Signature { v, r, s }) = - runFn3 ecRecover messageHash (toByteString r <> toByteString s) v + runFn3 ecRecover messageHash (B.concat [toBuffer r, toBuffer s]) v -- | Used in Ethereum to prevent replay attacks newtype ChainId = ChainId Int diff --git a/test/Common.purs b/test/Common.purs index cbe5186..b7c0c69 100644 --- a/test/Common.purs +++ b/test/Common.purs @@ -2,7 +2,7 @@ module Common where import Prelude -import Data.ByteString as BS +import Node.Buffer.Immutable as B import Data.Generic.Rep (class Generic) import Data.Maybe (Maybe(..), maybe) import Data.Show.Generic (genericShow) @@ -48,7 +48,7 @@ instance Show RawTransaction where makeTransactionMessage :: Sig.ChainId -> RawTransaction - -> BS.ByteString + -> B.ImmutableBuffer makeTransactionMessage (Sig.ChainId chainId) (RawTransaction tx) = let txWithChainId = diff --git a/test/Spec/Hex.purs b/test/Spec/Hex.purs index 0d997b5..57ac5b3 100644 --- a/test/Spec/Hex.purs +++ b/test/Spec/Hex.purs @@ -5,12 +5,12 @@ import Prelude import Control.Monad.Except (runExcept) import Control.Monad.Gen (chooseInt) import Data.Argonaut as A -import Data.ByteString as BS +import Node.Buffer.Immutable as B import Data.Either (Either(..), hush) -import Data.Maybe (Maybe(Just), fromJust) +import Data.Maybe (fromJust) import Effect.Class (liftEffect) import Foreign (unsafeToForeign) -import Network.Ethereum.Core.HexString (HexString, fromAscii, fromUtf8, mkHexString, numberOfBytes, splitAtByteOffset, toAscii, toByteString, toUtf8, unHex) +import Network.Ethereum.Core.HexString (HexString, fromAscii, fromUtf8, mkHexString, numberOfBytes, splitAtByteOffset, toAscii, toBuffer, toUtf8, unHex) import Network.Ethereum.Core.HexString as Hex import Node.Encoding (Encoding(Hex)) import Partial.Unsafe (unsafePartial) @@ -27,9 +27,9 @@ hexSpec = describe "hex-spec" do it "can convert byteStrings to HexString" do let hx = unsafePartial fromJust $ mkHexString "1234" - bs1 = toByteString $ hx - bs2 = BS.fromString "1234" Hex - Just bs1 `shouldEqual` bs2 + bs1 = toBuffer $ hx + bs2 = B.fromString "1234" Hex + bs1 `shouldEqual` bs2 describe "HexString manipulations" $ do it "can splitAtByteOffset" $ liftEffect do diff --git a/test/Spec/Keccak256.purs b/test/Spec/Keccak256.purs index c9dd3a4..6ce3e0a 100644 --- a/test/Spec/Keccak256.purs +++ b/test/Spec/Keccak256.purs @@ -2,10 +2,10 @@ module CoreSpec.Keccak256 (keccak256Spec) where import Prelude -import Data.ByteString (ByteString) +import Node.Buffer.Immutable (ImmutableBuffer) import Data.Maybe (fromJust) import Network.Ethereum.Core.Keccak256 (keccak256) -import Network.Ethereum.Core.HexString (mkHexString, toByteString) +import Network.Ethereum.Core.HexString (mkHexString, toBuffer) import Partial.Unsafe (unsafePartial) import Test.Spec (Spec, describe, it) import Test.Spec.Assertions (shouldEqual) @@ -14,12 +14,12 @@ keccak256Spec :: Spec Unit keccak256Spec = describe "keccak256-spec" do describe "Keccak256 tests" do it "can hash strings" do - keccak256 "test123" `shouldEqual` mkBS "f81b517a242b218999ec8eec0ea6e2ddbef2a367a14e93f4a32a39e260f686ad" - keccak256 "test(int)" `shouldEqual` mkBS "f4d03772bec1e62fbe8c5691e1a9101e520e8f8b5ca612123694632bf3cb51b1" + keccak256 "test123" `shouldEqual` mkB "f81b517a242b218999ec8eec0ea6e2ddbef2a367a14e93f4a32a39e260f686ad" + keccak256 "test(int)" `shouldEqual` mkB "f4d03772bec1e62fbe8c5691e1a9101e520e8f8b5ca612123694632bf3cb51b1" it "can hash hex strings" do - keccak256 (unsafePartial (fromJust <<< mkHexString) "80") `shouldEqual` mkBS "56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421" - keccak256 "0x80" `shouldEqual` mkBS "6b03a5eef7706e3fb52a61c19ab1122fad7237726601ac665bd4def888f0e4a0" - keccak256 (unsafePartial (fromJust <<< mkHexString) "3c9229289a6125f7fdf1885a77bb12c37a8d3b4962d936f7e3084dece32a3ca1") `shouldEqual` mkBS "82ff40c0a986c6a5cfad4ddf4c3aa6996f1a7837f9c398e17e5de5cbd5a12b28" + keccak256 (unsafePartial (fromJust <<< mkHexString) "80") `shouldEqual` mkB "56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421" + keccak256 "0x80" `shouldEqual` mkB "6b03a5eef7706e3fb52a61c19ab1122fad7237726601ac665bd4def888f0e4a0" + keccak256 (unsafePartial (fromJust <<< mkHexString) "3c9229289a6125f7fdf1885a77bb12c37a8d3b4962d936f7e3084dece32a3ca1") `shouldEqual` mkB "82ff40c0a986c6a5cfad4ddf4c3aa6996f1a7837f9c398e17e5de5cbd5a12b28" -mkBS :: String -> ByteString -mkBS = toByteString <<< unsafePartial fromJust <<< mkHexString +mkB :: String -> ImmutableBuffer +mkB = toBuffer <<< unsafePartial fromJust <<< mkHexString diff --git a/test/Spec/RLP.purs b/test/Spec/RLP.purs index 789090f..fc5bb6e 100644 --- a/test/Spec/RLP.purs +++ b/test/Spec/RLP.purs @@ -3,17 +3,15 @@ module CoreSpec.RLP (rlpSpec) where import Prelude import Common (RawTransaction(..), makeTransactionMessage, mkAddress', mkHexString') -import Data.ByteString as BS -import Data.Maybe (Maybe(..), fromJust) +import Node.Buffer.Immutable as B +import Data.Maybe (Maybe(..)) import Network.Ethereum.Core.BigNumber (fromInt, pow) import Network.Ethereum.Core.Keccak256 (keccak256) import Network.Ethereum.Core.RLP as RLP import Network.Ethereum.Core.Signatures as Sig import Node.Encoding (Encoding(Hex, UTF8)) -import Partial.Unsafe (unsafePartial) import Test.Spec (Spec, it, describe) import Test.Spec.Assertions (shouldEqual) -import Type.Quotient (mkQuotient) rlpSpec :: Spec Unit rlpSpec = do @@ -22,23 +20,23 @@ rlpSpec = do it "should return itself if single byte and less than 0x7f" do let encoded = RLP.rlpEncode $ RLP.RLPHexString (mkHexString' "0a") - BS.length encoded `shouldEqual` 1 + B.size encoded `shouldEqual` 1 it "can match the RLP encoding for ints" $ do let encoded = RLP.rlpEncode $ RLP.RLPInt 1024 - BS.length encoded `shouldEqual` 3 - BS.unsafeIndex encoded 0 `shouldEqual` mkQuotient 130 - BS.unsafeIndex encoded 1 `shouldEqual` mkQuotient 4 - BS.unsafeIndex encoded 2 `shouldEqual` mkQuotient 0 + B.size encoded `shouldEqual` 3 + B.getAtOffset 0 encoded `shouldEqual` Just 130 + B.getAtOffset 1 encoded `shouldEqual` Just 4 + B.getAtOffset 2 encoded `shouldEqual` Just 0 it "can match the RLP encoding for strings" $ do let testString = "This function takes in a data, convert it to buffer if not, and a length for recursion" - testStringBS = unsafePartial fromJust $ BS.fromString testString UTF8 - encoded = RLP.rlpEncode $ RLP.RLPByteString testStringBS + testStringB = B.fromString testString UTF8 + encoded = RLP.rlpEncode $ RLP.RLPBuffer testStringB - BS.unsafeIndex encoded 0 `shouldEqual` mkQuotient 184 - BS.unsafeIndex encoded 1 `shouldEqual` mkQuotient 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 @@ -51,25 +49,25 @@ rlpSpec = do ] ] encoded = RLP.rlpEncode nestedList - testVal = BS.pack $ map mkQuotient [ 199, 192, 193, 192, 195, 192, 193, 192 ] + testVal = B.fromArray [ 199, 192, 193, 192, 195, 192, 193, 192 ] encoded `shouldEqual` testVal it "can match the RLP encoding for transactions" $ do let - testVal = BS.pack $ map mkQuotient [ 193, 128 ] + testVal = B.fromArray [ 193, 128 ] encoded = RLP.rlpEncode $ RLP.RLPArray [ RLP.RLPNull ] encoded `shouldEqual` testVal it "can encode zero" $ do let - encoded = RLP.rlpEncode $ RLP.RLPByteString $ BS.pack [ mkQuotient 0 ] - testVal = BS.pack [ mkQuotient 0 ] + encoded = RLP.rlpEncode $ RLP.RLPBuffer $ B.fromArray [ 0 ] + testVal = B.fromArray [ 0 ] encoded `shouldEqual` testVal it "can encode zero the int" $ do let encoded = RLP.rlpEncode $ RLP.RLPInt 0 - testVal = BS.pack [ mkQuotient 128 ] + testVal = B.fromArray [ 128 ] encoded `shouldEqual` testVal it "EIP155 example" $ do @@ -86,8 +84,8 @@ rlpSpec = do rlpEncodingString = "ec098504a817c800825208943535353535353535353535353535353535353535880de0b6b3a764000080018080" hashedMessageString = "daf5a779ae972f972197303d7b574746c7ef83eadac0f2791ad23db92e4c8e53" txMessage = makeTransactionMessage chainId rawTx - BS.toString txMessage Hex `shouldEqual` rlpEncodingString - BS.toString (keccak256 txMessage) Hex `shouldEqual` hashedMessageString + B.toString Hex txMessage `shouldEqual` rlpEncodingString + B.toString Hex (keccak256 txMessage) `shouldEqual` hashedMessageString it "can match the RLP encoding for transactions" $ do let @@ -102,4 +100,4 @@ rlpSpec = do chainId = Sig.ChainId 4 rlpEncodingString = "f84729843b9aca0082a2c59486b89c0906b111508d5caa38e2e61689a124c86080a4d14e62b80000000000000000000000000000000000000000000000000000000000000001048080" -- hashedMessageString = "f5bbe123594fb9f588c98b069063ce1d224917b7374c700e49ec4fce0f63f999" - BS.toString (makeTransactionMessage chainId rawTx) Hex `shouldEqual` rlpEncodingString + B.toString Hex (makeTransactionMessage chainId rawTx) `shouldEqual` rlpEncodingString From b80e96d966776a3855885cd1e985f24dcae02fc7 Mon Sep 17 00:00:00 2001 From: martyall Date: Sun, 21 Apr 2024 08:08:26 -0700 Subject: [PATCH 2/2] tidy --- src/Network/Ethereum/Core/HexString.purs | 8 +++++--- src/Network/Ethereum/Core/Signatures.purs | 12 +++++++----- test/Spec/RLP.purs | 6 +++--- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/Network/Ethereum/Core/HexString.purs b/src/Network/Ethereum/Core/HexString.purs index 475d372..cdd7c07 100644 --- a/src/Network/Ethereum/Core/HexString.purs +++ b/src/Network/Ethereum/Core/HexString.purs @@ -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 diff --git a/src/Network/Ethereum/Core/Signatures.purs b/src/Network/Ethereum/Core/Signatures.purs index 7bf1113..633511b 100644 --- a/src/Network/Ethereum/Core/Signatures.purs +++ b/src/Network/Ethereum/Core/Signatures.purs @@ -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 @@ -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 diff --git a/test/Spec/RLP.purs b/test/Spec/RLP.purs index fc5bb6e..1089d95 100644 --- a/test/Spec/RLP.purs +++ b/test/Spec/RLP.purs @@ -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 @@ -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