2023-06-14 15:53:29 +00:00
|
|
|
module ZcashHaskell.Utils where
|
2023-05-04 20:26:49 +00:00
|
|
|
|
|
|
|
import C.Zcash
|
|
|
|
( rustWrapperBech32Decode
|
|
|
|
, rustWrapperF4Jumble
|
|
|
|
, rustWrapperF4UnJumble
|
|
|
|
)
|
|
|
|
|
|
|
|
import qualified Data.ByteString as BS
|
|
|
|
import Foreign.Rust.Marshall.Variable
|
2023-06-14 15:53:29 +00:00
|
|
|
import ZcashHaskell.Types
|
2023-05-04 20:26:49 +00:00
|
|
|
|
|
|
|
-- | Helper function to turn a hex-encoded strings to bytestring
|
|
|
|
decodeHexText :: String -> BS.ByteString
|
|
|
|
decodeHexText h = BS.pack $ hexRead h
|
|
|
|
where
|
|
|
|
hexRead hexText
|
|
|
|
| null chunk = []
|
|
|
|
| otherwise =
|
|
|
|
fromIntegral (read ("0x" <> chunk)) : hexRead (drop 2 hexText)
|
|
|
|
where
|
|
|
|
chunk = take 2 hexText
|
|
|
|
|
|
|
|
-- | Decode the given bytestring using Bech32
|
|
|
|
decodeBech32 :: BS.ByteString -> RawData
|
|
|
|
decodeBech32 = withPureBorshVarBuffer . rustWrapperBech32Decode
|
|
|
|
|
|
|
|
-- | Apply the F4Jumble transformation to the given bytestring
|
|
|
|
f4Jumble :: BS.ByteString -> BS.ByteString
|
|
|
|
f4Jumble = withPureBorshVarBuffer . rustWrapperF4Jumble
|
|
|
|
|
|
|
|
-- | Apply the inverse F4Jumble transformation to the given bytestring
|
|
|
|
f4UnJumble :: BS.ByteString -> BS.ByteString
|
|
|
|
f4UnJumble = withPureBorshVarBuffer . rustWrapperF4UnJumble
|