Generalize parsing of addresses

This commit is contained in:
Rene Vergara 2024-07-08 13:06:25 -05:00
parent 06c58f62df
commit 75dc71459f
Signed by: pitmutt
GPG key ID: 65122AD495A7F5B2
2 changed files with 60 additions and 50 deletions

View file

@ -82,8 +82,7 @@ import ZcashHaskell.Keys (generateWalletSeedPhrase, getWalletSeed)
import ZcashHaskell.Orchard (getSaplingFromUA, isValidUnifiedAddress) import ZcashHaskell.Orchard (getSaplingFromUA, isValidUnifiedAddress)
import ZcashHaskell.Sapling (decodeSaplingAddress, isValidShieldedAddress) import ZcashHaskell.Sapling (decodeSaplingAddress, isValidShieldedAddress)
import ZcashHaskell.Transparent import ZcashHaskell.Transparent
( decodeExchangeAddress ( decodeTransparentAddress
, decodeTransparentAddress
, encodeTransparentReceiver , encodeTransparentReceiver
) )
import ZcashHaskell.Types import ZcashHaskell.Types
@ -100,7 +99,9 @@ import Zenith.Types
import Zenith.Utils import Zenith.Utils
( displayTaz ( displayTaz
, displayZec , displayZec
, isRecipientValid
, jsonNumber , jsonNumber
, parseAddress
, showAddress , showAddress
, validBarValue , validBarValue
) )
@ -508,19 +509,6 @@ mkSendForm bal =
label s w = label s w =
padBottom (Pad 1) $ vLimit 1 (hLimit 15 $ str s <+> fill ' ') <+> w padBottom (Pad 1) $ vLimit 1 (hLimit 15 $ str s <+> fill ' ') <+> w
isRecipientValid :: T.Text -> Bool
isRecipientValid a =
case isValidUnifiedAddress (E.encodeUtf8 a) of
Just _a1 -> True
Nothing ->
isValidShieldedAddress (E.encodeUtf8 a) ||
(case decodeTransparentAddress (E.encodeUtf8 a) of
Just _a3 -> True
Nothing ->
case decodeExchangeAddress a of
Just _a4 -> True
Nothing -> False)
listDrawElement :: (Show a) => Bool -> a -> Widget Name listDrawElement :: (Show a) => Bool -> a -> Widget Name
listDrawElement sel a = listDrawElement sel a =
let selStr s = let selStr s =
@ -1288,36 +1276,22 @@ sendTransaction ::
-> IO () -> IO ()
sendTransaction pool chan zHost zPort znet accId bl amt ua memo = do sendTransaction pool chan zHost zPort znet accId bl amt ua memo = do
BC.writeBChan chan $ TickMsg "Preparing transaction..." BC.writeBChan chan $ TickMsg "Preparing transaction..."
outUA <- parseAddress ua case parseAddress ua znet of
res <- Nothing -> BC.writeBChan chan $ TickMsg "Incorrect address"
runFileLoggingT "zenith.log" $ Just outUA -> do
prepareTx pool zHost zPort znet accId bl amt outUA memo res <-
BC.writeBChan chan $ TickMsg "Transaction ready, sending to Zebra..." runFileLoggingT "zenith.log" $
case res of prepareTx pool zHost zPort znet accId bl amt outUA memo
Left e -> BC.writeBChan chan $ TickMsg $ show e BC.writeBChan chan $ TickMsg "Transaction ready, sending to Zebra..."
Right rawTx -> do case res of
resp <- Left e -> BC.writeBChan chan $ TickMsg $ show e
makeZebraCall Right rawTx -> do
zHost resp <-
zPort makeZebraCall
"sendrawtransaction" zHost
[Data.Aeson.String $ toText rawTx] zPort
case resp of "sendrawtransaction"
Left e1 -> BC.writeBChan chan $ TickMsg $ "Zebra error: " ++ show e1 [Data.Aeson.String $ toText rawTx]
Right txId -> BC.writeBChan chan $ TickTx txId case resp of
where Left e1 -> BC.writeBChan chan $ TickMsg $ "Zebra error: " ++ show e1
parseAddress :: T.Text -> IO UnifiedAddress Right txId -> BC.writeBChan chan $ TickTx txId
parseAddress a =
case isValidUnifiedAddress (E.encodeUtf8 a) of
Just a1 -> return a1
Nothing ->
case decodeSaplingAddress (E.encodeUtf8 a) of
Just a2 ->
return $
UnifiedAddress znet Nothing (Just $ sa_receiver a2) Nothing
Nothing ->
case decodeTransparentAddress (E.encodeUtf8 a) of
Just a3 ->
return $
UnifiedAddress znet Nothing Nothing (Just $ ta_receiver a3)
Nothing -> throwIO $ userError "Incorrect address"

View file

@ -12,8 +12,17 @@ import qualified Data.Text.Encoding as E
import System.Process (createProcess_, shell) import System.Process (createProcess_, shell)
import Text.Regex.Posix import Text.Regex.Posix
import ZcashHaskell.Orchard (encodeUnifiedAddress, isValidUnifiedAddress) import ZcashHaskell.Orchard (encodeUnifiedAddress, isValidUnifiedAddress)
import ZcashHaskell.Sapling (isValidShieldedAddress) import ZcashHaskell.Sapling (decodeSaplingAddress, isValidShieldedAddress)
import ZcashHaskell.Types (ZcashNet(..)) import ZcashHaskell.Transparent
( decodeExchangeAddress
, decodeTransparentAddress
)
import ZcashHaskell.Types
( SaplingAddress(..)
, TransparentAddress(..)
, UnifiedAddress(..)
, ZcashNet(..)
)
import Zenith.Types import Zenith.Types
( AddressGroup(..) ( AddressGroup(..)
, UnifiedAddressDB(..) , UnifiedAddressDB(..)
@ -84,3 +93,30 @@ copyAddress a =
-- | Bound a value to the 0..1 range, used for progress reporting on UIs -- | Bound a value to the 0..1 range, used for progress reporting on UIs
validBarValue :: Float -> Float validBarValue :: Float -> Float
validBarValue = clamp (0, 1) validBarValue = clamp (0, 1)
isRecipientValid :: T.Text -> Bool
isRecipientValid a =
case isValidUnifiedAddress (E.encodeUtf8 a) of
Just _a1 -> True
Nothing ->
isValidShieldedAddress (E.encodeUtf8 a) ||
(case decodeTransparentAddress (E.encodeUtf8 a) of
Just _a3 -> True
Nothing ->
case decodeExchangeAddress a of
Just _a4 -> True
Nothing -> False)
parseAddress :: T.Text -> ZcashNet -> Maybe UnifiedAddress
parseAddress a znet =
case isValidUnifiedAddress (E.encodeUtf8 a) of
Just a1 -> Just a1
Nothing ->
case decodeSaplingAddress (E.encodeUtf8 a) of
Just a2 ->
Just $ UnifiedAddress znet Nothing (Just $ sa_receiver a2) Nothing
Nothing ->
case decodeTransparentAddress (E.encodeUtf8 a) of
Just a3 ->
Just $ UnifiedAddress znet Nothing Nothing (Just $ ta_receiver a3)
Nothing -> Nothing