zcash-haskell/src/ZcashHaskell/Utils.hs

105 lines
3.2 KiB
Haskell
Raw Normal View History

2024-01-18 18:55:23 +00:00
-- Copyright 2022-2024 Vergara Technologies LLC
--
-- This file is part of Zcash-Haskell.
--
2023-08-17 15:02:32 +00:00
-- |
-- Module : ZcashHaskell.Utils
2024-01-18 18:55:23 +00:00
-- Copyright : 2022-2024 Vergara Technologies LLC
-- License : MIT
2023-08-17 15:02:32 +00:00
--
2024-01-18 18:55:23 +00:00
-- Maintainer : pitmutt@vergara.tech
2023-08-17 15:02:32 +00:00
-- Stability : experimental
-- Portability : unknown
--
-- A set of functions to assist in the handling of elements of the Zcash protocol, allowing for decoding of memos, addresses and viewing keys.
--
2023-09-26 20:24:18 +00:00
{-# LANGUAGE OverloadedStrings #-}
2023-06-14 15:53:29 +00:00
module ZcashHaskell.Utils where
import C.Zcash
( rustWrapperBech32Decode
2024-03-04 17:59:07 +00:00
, rustWrapperBech32Encode
, rustWrapperF4Jumble
, rustWrapperF4UnJumble
)
import Control.Exception (try)
2023-09-26 20:24:18 +00:00
import Control.Monad.IO.Class
import Data.Aeson
import qualified Data.ByteString as BS
2023-09-26 20:24:18 +00:00
import qualified Data.Text as T
2024-02-14 14:19:34 +00:00
import qualified Data.Text.Encoding as E
import Foreign.Rust.Marshall.Variable
import Network.HTTP.Client (HttpException(..))
2023-09-26 20:24:18 +00:00
import Network.HTTP.Simple
2023-06-14 15:53:29 +00:00
import ZcashHaskell.Types
2024-03-03 21:19:06 +00:00
import Foreign.C.Types
import Foreign.Marshal.Array (allocaArray, peekArray)
import Foreign.Ptr (Ptr)
import Data.Word
2024-03-10 12:47:26 +00:00
-- |
-- | Decode the given bytestring using Bech32
decodeBech32 :: BS.ByteString -> RawData
decodeBech32 = withPureBorshVarBuffer . rustWrapperBech32Decode
2024-03-04 17:59:07 +00:00
-- | Encode the given Human Readable Part and bytestring as a Bech32m string
encodeBech32m :: BS.ByteString -> BS.ByteString -> T.Text
encodeBech32m h d = withPureBorshVarBuffer $ rustWrapperBech32Encode h d
-- | 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
2023-09-26 20:24:18 +00:00
-- | Make a Zcash RPC call
makeZcashCall ::
(MonadIO m, FromJSON a)
=> BS.ByteString
-> BS.ByteString
-> T.Text
-> [Data.Aeson.Value]
-> m (Response a)
makeZcashCall username password m p = do
let payload = RpcCall "1.0" "test" m p
let myRequest =
setRequestBodyJSON payload $
setRequestPort 8232 $
setRequestBasicAuth username password $
setRequestMethod "POST" defaultRequest
httpJSON myRequest
2024-02-12 17:55:42 +00:00
-- | Make a Zebra RPC call
makeZebraCall ::
FromJSON a
2024-02-14 14:19:34 +00:00
=> T.Text -- ^ Hostname for `zebrad`
-> Int -- ^ Port for `zebrad`
2024-02-12 17:55:42 +00:00
-> T.Text -- ^ RPC method to call
-> [Data.Aeson.Value] -- ^ List of parameters
-> IO (Either String a)
2024-02-14 14:19:34 +00:00
makeZebraCall host port m params = do
2024-02-12 17:55:42 +00:00
let payload = RpcCall "2.0" "zh" m params
let myRequest =
setRequestBodyJSON payload $
2024-02-14 14:19:34 +00:00
setRequestPort port $
setRequestHost (E.encodeUtf8 host) $
setRequestMethod "POST" defaultRequest
r <-
try $ httpJSON myRequest :: FromJSON a1 =>
IO (Either HttpException (Response (RpcResponse a1)))
case r of
Left ex -> return $ Left $ show ex
Right res -> do
let zebraResp = getResponseBody res
case err zebraResp of
Just zErr -> return $ Left $ T.unpack $ emessage zErr
Nothing ->
case result zebraResp of
Nothing -> return $ Left "Empty response from Zebra"
Just zR -> return $ Right zR