Enhance hex memo decoding

This commit is contained in:
Rene Vergara 2022-07-07 10:13:33 -05:00
parent 1bf99023b1
commit dbc7568b55
Signed by: pitmutt
GPG key ID: 65122AD495A7F5B2
5 changed files with 30 additions and 19 deletions

View file

@ -48,6 +48,7 @@ library:
- vector - vector
- wai-cors - wai-cors
- warp-tls - warp-tls
- hexstring
executables: executables:
zgo-backend-exe: zgo-backend-exe:

View file

@ -17,6 +17,8 @@ import qualified Data.HashMap.Strict as HM
import Data.Maybe import Data.Maybe
import Data.SecureMem import Data.SecureMem
import qualified Data.Text as T import qualified Data.Text as T
import qualified Data.Text.Encoding as E
import Data.Text.Encoding.Error (lenientDecode)
import qualified Data.Text.Lazy as L import qualified Data.Text.Lazy as L
import Data.Time.Clock import Data.Time.Clock
import Data.Time.Clock.POSIX import Data.Time.Clock.POSIX
@ -122,15 +124,7 @@ instance FromJSON ZcashTx where
conf <- obj .: "confirmations" conf <- obj .: "confirmations"
m <- obj .: "memo" m <- obj .: "memo"
pure $ pure $
ZcashTx ZcashTx t a aZ bh bt c conf (T.filter (/= '\NUL') $ decodeHexText m)
t
a
aZ
bh
bt
c
conf
(T.pack (filter (/= '\NUL') $ decodeHexText m))
instance ToJSON ZcashTx where instance ToJSON ZcashTx where
toJSON (ZcashTx t a aZ bh bt c conf m) = toJSON (ZcashTx t a aZ bh bt c conf m) =
@ -157,13 +151,15 @@ instance Arbitrary ZcashTx where
ZcashTx a aZ t bh bt c cm <$> arbitrary ZcashTx a aZ t bh bt c cm <$> arbitrary
-- | Helper function to turn a hex-encoded memo strings to readable text -- | Helper function to turn a hex-encoded memo strings to readable text
decodeHexText :: String -> String decodeHexText :: String -> T.Text
decodeHexText hexText decodeHexText h = E.decodeUtf8With lenientDecode $ BS.pack $ hexRead h
-- | chunk == "00" = decodeHexText (drop 2 hexText)
| null chunk = ""
| otherwise = chr (read ("0x" <> chunk)) : decodeHexText (drop 2 hexText)
where where
chunk = take 2 hexText hexRead hexText
| null chunk = []
| otherwise =
fromIntegral (read ("0x" <> chunk)) : hexRead (drop 2 hexText)
where
chunk = take 2 hexText
-- | Helper function to turn a string into a hex-encoded string -- | Helper function to turn a string into a hex-encoded string
encodeHexText :: String -> String encodeHexText :: String -> String

View file

@ -41,7 +41,9 @@ packages:
# commit: e7b331f14bcffb8367cd58fbfc8b40ec7642100a # commit: e7b331f14bcffb8367cd58fbfc8b40ec7642100a
# #
# extra-deps: [] # extra-deps: []
extra-deps:
- git: https://github.com/reach-sh/haskell-hexstring.git
commit: 085c16fb21b9f856a435a3faab980e7e0b319341
# Override default flag values for local packages and extra-deps # Override default flag values for local packages and extra-deps
# flags: {} # flags: {}

View file

@ -3,7 +3,18 @@
# For more information, please see the documentation at: # For more information, please see the documentation at:
# https://docs.haskellstack.org/en/stable/lock_files # https://docs.haskellstack.org/en/stable/lock_files
packages: [] packages:
- completed:
name: hexstring
version: 0.11.1
git: https://github.com/reach-sh/haskell-hexstring.git
pantry-tree:
size: 687
sha256: 9ecf67856f59dfb382b283eceb42e4fc1865935d1a7e59111556ed381c6a2ffd
commit: 085c16fb21b9f856a435a3faab980e7e0b319341
original:
git: https://github.com/reach-sh/haskell-hexstring.git
commit: 085c16fb21b9f856a435a3faab980e7e0b319341
snapshots: snapshots:
- completed: - completed:
size: 618683 size: 618683

View file

@ -5,7 +5,7 @@ cabal-version: 1.12
-- see: https://github.com/sol/hpack -- see: https://github.com/sol/hpack
name: zgo-backend name: zgo-backend
version: 0.1.0.1 version: 0.1.0.2
synopsis: Haskell Back-end for the ZGo point-of-sale application synopsis: Haskell Back-end for the ZGo point-of-sale application
description: Please see the README on GitLab at <https://gitlab.com/pitmutt/zgo-backend#readme> description: Please see the README on GitLab at <https://gitlab.com/pitmutt/zgo-backend#readme>
category: Web category: Web
@ -17,7 +17,7 @@ license-file: LICENSE
build-type: Simple build-type: Simple
extra-source-files: extra-source-files:
README.md README.md
ChangeLog.md CHANGELOG.md
zgo.cfg zgo.cfg
source-repository head source-repository head
@ -44,6 +44,7 @@ library
, base >=4.7 && <5 , base >=4.7 && <5
, bson , bson
, bytestring , bytestring
, hexstring
, http-conduit , http-conduit
, http-types , http-types
, mongoDB , mongoDB