Improve type safety for Orchard

This commit is contained in:
Rene Vergara 2024-03-14 12:35:13 -05:00
parent d2619123c0
commit 4cc4c37960
Signed by: pitmutt
GPG key ID: 65122AD495A7F5B2
4 changed files with 35 additions and 16 deletions

View file

@ -41,7 +41,7 @@ genOrchardSpendingKey ::
genOrchardSpendingKey s coinType accountId =
if BS.length k /= 32
then Nothing
else Just k
else Just $ OrchardSpendingKey k
where
k =
withPureBorshVarBuffer $
@ -56,11 +56,14 @@ genOrchardReceiver ::
genOrchardReceiver i scope osk =
if BS.length k /= 43
then Nothing
else Just k
else Just $ OrchardReceiver k
where
k =
withPureBorshVarBuffer $
rustWrapperGenOrchardReceiver osk (fromIntegral i) (scope == External)
rustWrapperGenOrchardReceiver
(getBytes osk)
(fromIntegral i)
(scope == External)
-- | Checks if given bytestring is a valid encoded unified address
isValidUnifiedAddress :: BS.ByteString -> Maybe UnifiedAddress
@ -79,10 +82,10 @@ isValidUnifiedAddress str =
UnifiedAddress
whichNet
(if BS.length (raw_o x) == 43
then Just (raw_o x)
then Just $ OrchardReceiver (raw_o x)
else Nothing)
(if BS.length (raw_s x) == 43
then Just (SaplingReceiver $ raw_s x)
then Just $ SaplingReceiver (raw_s x)
else Nothing)
(if not (BS.null (raw_t x))
then Just $ TransparentAddress P2PKH (fromRawBytes $ raw_t x)
@ -107,7 +110,7 @@ encodeUnifiedAddress ua = encodeBech32m (E.encodeUtf8 hr) b
P2SH -> packReceiver 0x01 $ Just $ toBytes $ ta_bytes t
P2PKH -> packReceiver 0x00 $ Just $ toBytes $ ta_bytes t
sReceiver = packReceiver 0x02 $ getBytes <$> s_rec ua
oReceiver = packReceiver 0x03 $ o_rec ua
oReceiver = packReceiver 0x03 $ getBytes <$> o_rec ua
padding = E.encodeUtf8 $ T.justifyLeft 16 '\NUL' hr
packReceiver :: Word8 -> Maybe BS.ByteString -> BS.ByteString
packReceiver typeCode receiver' =

View file

@ -40,7 +40,6 @@ import ZcashHaskell.Types
, DecodedNote(..)
, RawData(..)
, RawTxResponse(..)
, SaplingInternalReceiver
, SaplingReceiver(..)
, SaplingSpendingKey(..)
, Seed(..)
@ -124,5 +123,5 @@ genSaplingPaymentAddress i extspk =
(fromIntegral (i * 111)))
-- | Generate an internal Sapling address
genSaplingInternalAddress :: SaplingSpendingKey -> Maybe SaplingInternalReceiver
genSaplingInternalAddress :: SaplingSpendingKey -> Maybe SaplingReceiver
genSaplingInternalAddress sk = undefined

View file

@ -279,9 +279,6 @@ newtype SaplingReceiver =
instance ToBytes SaplingReceiver where
getBytes (SaplingReceiver s) = s
-- | A Sapling internal receiver
type SaplingInternalReceiver = BS.ByteString
-- | Type to represent a Sapling Shielded Output as provided by the @getrawtransaction@ RPC method of @zcashd@.
data ShieldedOutput = ShieldedOutput
{ s_cv :: !HexString -- ^ Value commitment to the input note
@ -308,10 +305,20 @@ instance FromJSON ShieldedOutput where
-- * Orchard
-- | A spending key for Orchard
type OrchardSpendingKey = BS.ByteString
newtype OrchardSpendingKey =
OrchardSpendingKey BS.ByteString
deriving stock (Eq, Prelude.Show, Read)
instance ToBytes OrchardSpendingKey where
getBytes (OrchardSpendingKey o) = o
-- | An Orchard receiver
type OrchardReceiver = BS.ByteString
newtype OrchardReceiver =
OrchardReceiver BS.ByteString
deriving stock (Eq, Prelude.Show, Read)
instance ToBytes OrchardReceiver where
getBytes (OrchardReceiver o) = o
-- | Type to represent a Unified Address
data UnifiedAddress = UnifiedAddress

View file

@ -57,6 +57,7 @@ import ZcashHaskell.Types
, CoinType
, DecodedNote(..)
, OrchardAction(..)
, OrchardSpendingKey(..)
, Phrase(..)
, RawData(..)
, RawTxResponse(..)
@ -565,7 +566,10 @@ prop_OrchardSpendingKey s c (NonNegative i) =
prop_OrchardReceiver ::
Seed -> CoinType -> NonNegative Int -> NonNegative Int -> Scope -> Property
prop_OrchardReceiver s c (NonNegative i) (NonNegative j) scope =
genOrchardReceiver j scope (fromMaybe "" $ genOrchardSpendingKey s c i) =/=
genOrchardReceiver
j
scope
(fromMaybe (OrchardSpendingKey "") $ genOrchardSpendingKey s c i) =/=
Nothing
prop_SaplingSpendingKey :: Seed -> CoinType -> NonNegative Int -> Property
@ -592,8 +596,14 @@ prop_SaplingRecRepeated s c (NonNegative i) =
prop_OrchardRecRepeated ::
Seed -> CoinType -> NonNegative Int -> NonNegative Int -> Scope -> Property
prop_OrchardRecRepeated s c (NonNegative i) (NonNegative j) scope =
genOrchardReceiver j scope (fromMaybe "" $ genOrchardSpendingKey s c i) =/=
genOrchardReceiver (j + 1) scope (fromMaybe "" $ genOrchardSpendingKey s c i)
genOrchardReceiver
j
scope
(fromMaybe (OrchardSpendingKey "") $ genOrchardSpendingKey s c i) =/=
genOrchardReceiver
(j + 1)
scope
(fromMaybe (OrchardSpendingKey "") $ genOrchardSpendingKey s c i)
prop_TransparentSpendingKey :: Seed -> NonNegative Int -> Property
prop_TransparentSpendingKey s (NonNegative i) =