Upgrade Zebra call

This commit is contained in:
Rene Vergara 2024-03-20 11:13:02 -05:00
parent f228eff367
commit e7050f03c0
Signed by: pitmutt
GPG key ID: 65122AD495A7F5B2
5 changed files with 29 additions and 9 deletions

View file

@ -5,8 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [0.5.1.0]
## [Unreleased] ### Changed
- Modified the `makeZebraCall` function to handle errors explicitly
## [0.5.0.1]
### Added ### Added

View file

@ -126,7 +126,7 @@ genSaplingPaymentAddress i extspk =
-- | Generate an internal Sapling address -- | Generate an internal Sapling address
genSaplingInternalAddress :: SaplingSpendingKey -> Maybe SaplingReceiver genSaplingInternalAddress :: SaplingSpendingKey -> Maybe SaplingReceiver
genSaplingInternalAddress sk = genSaplingInternalAddress sk =
if BS.length res > 0 if BS.length res == 43
then Just $ SaplingReceiver res then Just $ SaplingReceiver res
else Nothing else Nothing
where where

View file

@ -44,7 +44,7 @@ import Haskoin.Crypto.Keys.Extended (XPrvKey)
-- --
-- | A seed for generating private keys -- | A seed for generating private keys
newtype Seed = newtype Seed =
Seed C.ByteString Seed BS.ByteString
deriving stock (Eq, Prelude.Show, GHC.Generic) deriving stock (Eq, Prelude.Show, GHC.Generic)
deriving anyclass (SOP.Generic, SOP.HasDatatypeInfo) deriving anyclass (SOP.Generic, SOP.HasDatatypeInfo)
deriving anyclass (Data.Structured.Show) deriving anyclass (Data.Structured.Show)
@ -55,7 +55,7 @@ instance ToBytes Seed where
-- | A mnemonic phrase used to derive seeds -- | A mnemonic phrase used to derive seeds
newtype Phrase = newtype Phrase =
Phrase BS.ByteString Phrase C.ByteString
deriving stock (Eq, Prelude.Show, GHC.Generic, Read) deriving stock (Eq, Prelude.Show, GHC.Generic, Read)
deriving anyclass (SOP.Generic, SOP.HasDatatypeInfo) deriving anyclass (SOP.Generic, SOP.HasDatatypeInfo)
deriving anyclass (Data.Structured.Show) deriving anyclass (Data.Structured.Show)
@ -191,7 +191,7 @@ data BlockResponse = BlockResponse
{ bl_confirmations :: !Integer -- ^ Block confirmations { bl_confirmations :: !Integer -- ^ Block confirmations
, bl_height :: !Integer -- ^ Block height , bl_height :: !Integer -- ^ Block height
, bl_time :: !Integer -- ^ Block time , bl_time :: !Integer -- ^ Block time
, bl_txs :: ![T.Text] -- ^ List of transaction IDs in the block , bl_txs :: ![HexString] -- ^ List of transaction IDs in the block
} deriving (Prelude.Show, Eq) } deriving (Prelude.Show, Eq)
instance FromJSON BlockResponse where instance FromJSON BlockResponse where

View file

@ -23,12 +23,14 @@ import C.Zcash
, rustWrapperF4Jumble , rustWrapperF4Jumble
, rustWrapperF4UnJumble , rustWrapperF4UnJumble
) )
import Control.Exception (try)
import Control.Monad.IO.Class import Control.Monad.IO.Class
import Data.Aeson import Data.Aeson
import qualified Data.ByteString as BS import qualified Data.ByteString as BS
import qualified Data.Text as T import qualified Data.Text as T
import qualified Data.Text.Encoding as E import qualified Data.Text.Encoding as E
import Foreign.Rust.Marshall.Variable import Foreign.Rust.Marshall.Variable
import Network.HTTP.Client (HttpException(..))
import Network.HTTP.Simple import Network.HTTP.Simple
import ZcashHaskell.Types import ZcashHaskell.Types
@ -74,12 +76,12 @@ makeZcashCall username password m p = do
-- | Make a Zebra RPC call -- | Make a Zebra RPC call
makeZebraCall :: makeZebraCall ::
(MonadIO m, FromJSON a) FromJSON a
=> T.Text -- ^ Hostname for `zebrad` => T.Text -- ^ Hostname for `zebrad`
-> Int -- ^ Port for `zebrad` -> Int -- ^ Port for `zebrad`
-> T.Text -- ^ RPC method to call -> T.Text -- ^ RPC method to call
-> [Data.Aeson.Value] -- ^ List of parameters -> [Data.Aeson.Value] -- ^ List of parameters
-> m (Response a) -> IO (Either String a)
makeZebraCall host port m params = do makeZebraCall host port m params = do
let payload = RpcCall "2.0" "zh" m params let payload = RpcCall "2.0" "zh" m params
let myRequest = let myRequest =
@ -87,4 +89,16 @@ makeZebraCall host port m params = do
setRequestPort port $ setRequestPort port $
setRequestHost (E.encodeUtf8 host) $ setRequestHost (E.encodeUtf8 host) $
setRequestMethod "POST" defaultRequest setRequestMethod "POST" defaultRequest
httpJSON myRequest 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

View file

@ -5,7 +5,7 @@ cabal-version: 3.0
-- see: https://github.com/sol/hpack -- see: https://github.com/sol/hpack
name: zcash-haskell name: zcash-haskell
version: 0.5.0.1 version: 0.5.1.0
synopsis: Utilities to interact with the Zcash blockchain synopsis: Utilities to interact with the Zcash blockchain
description: Please see the README on the repo at <https://git.vergara.tech/Vergara_Tech/zcash-haskell#readme> description: Please see the README on the repo at <https://git.vergara.tech/Vergara_Tech/zcash-haskell#readme>
category: Blockchain category: Blockchain
@ -53,6 +53,7 @@ library
, generics-sop , generics-sop
, hexstring >=0.12.1 , hexstring >=0.12.1
, http-conduit , http-conduit
, http-client
, memory , memory
, text , text
, haskoin-core , haskoin-core