-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathBytes.purs
56 lines (47 loc) · 1.99 KB
/
Bytes.purs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
module Network.Ethereum.Web3.Solidity.Bytes
( BytesN
, unBytesN
, proxyBytesN
, update
, fromBuffer
, generator
) where
import Prelude
import Control.Monad.Gen (class MonadGen)
import Node.Buffer.Immutable (ImmutableBuffer)
import Node.Buffer.Immutable as B
import Node.Encoding (Encoding(Hex))
import Data.Maybe (Maybe(..), fromJust)
import Data.Reflectable (class Reflectable, reflectType)
import Network.Ethereum.Core.HexString as Hex
import Network.Ethereum.Types (mkHexString)
import Partial.Unsafe (unsafePartial)
import Type.Proxy (Proxy(..))
--------------------------------------------------------------------------------
-- * Statically sized byte array
--------------------------------------------------------------------------------
-- Represents a statically sized bytestring of size `n` bytes.
-- | See module [Network.Ethereum.Web3.Solidity.Sizes](/Network.Ethereum.Web3.Solidity.Sizes) for some predefined sizes.
newtype BytesN (n :: Int) = BytesN ImmutableBuffer
derive newtype instance eqBytesN :: Eq (BytesN n)
instance showBytesN :: Show (BytesN n) where
show (BytesN bs) = show <<< unsafePartial fromJust <<< mkHexString $ B.toString Hex bs
generator :: forall n m. Reflectable n Int => MonadGen m => Proxy n -> m (BytesN n)
generator p = do
bs <- Hex.generator (reflectType p)
pure $ BytesN $ Hex.toBuffer bs
-- | Access the underlying raw bytestring
unBytesN :: forall n. BytesN n -> ImmutableBuffer
unBytesN (BytesN bs) = bs
proxyBytesN :: forall n. BytesN n
proxyBytesN = BytesN $ B.fromArray []
update :: forall n. BytesN n -> ImmutableBuffer -> BytesN n
update _ = BytesN
-- | Attempt to coerce a bytestring into one of the appropriate size.
-- | See module [Network.Ethereum.Web3.Solidity.Sizes](/Network.Ethereum.Web3.Solidity.Sizes) for some predefined sizes.
fromBuffer :: forall proxy n. Reflectable n Int => proxy n -> ImmutableBuffer -> Maybe (BytesN n)
fromBuffer _ bs =
if not $ B.size bs <= reflectType (Proxy :: Proxy n) then
Nothing
else
Just $ BytesN bs