Implement HexString #4

Merged
pitmutt merged 1 commit from fix080 into dev040 2024-02-06 19:33:03 +00:00
7 changed files with 68 additions and 88 deletions
Showing only changes of commit c001fb5343 - Show all commits

View file

@ -145,16 +145,21 @@ impl HshieldedOutput {
} }
} }
#[derive(BorshSerialize, BorshDeserialize)]
pub struct Hhex {
bytes: Vec<u8>
}
#[derive(BorshSerialize, BorshDeserialize)] #[derive(BorshSerialize, BorshDeserialize)]
pub struct Haction { pub struct Haction {
nf: Vec<u8>, nf: Hhex,
rk: Vec<u8>, rk: Hhex,
cmx: Vec<u8>, cmx: Hhex,
eph_key: Vec<u8>, eph_key: Hhex,
enc_txt: Vec<u8>, enc_txt: Hhex,
out_txt: Vec<u8>, out_txt: Hhex,
cv: Vec<u8>, cv: Hhex,
auth: Vec<u8> auth: Hhex
} }
impl<RW> FromHaskell<RW> for Haction { impl<RW> FromHaskell<RW> for Haction {
@ -498,12 +503,12 @@ pub extern "C" fn rust_wrapper_orchard_note_decrypt(
let fvk_input: Vec<u8> = marshall_from_haskell_var(key, key_len, RW); let fvk_input: Vec<u8> = marshall_from_haskell_var(key, key_len, RW);
let note_input: Haction = marshall_from_haskell_var(note, note_len, RW); let note_input: Haction = marshall_from_haskell_var(note, note_len, RW);
let action: Action<Signature<SpendAuth>> = Action::from_parts( let action: Action<Signature<SpendAuth>> = Action::from_parts(
Nullifier::from_bytes(&to_array(note_input.nf)).unwrap(), Nullifier::from_bytes(&to_array(note_input.nf.bytes)).unwrap(),
VerificationKey::try_from(to_array(note_input.rk)).unwrap(), VerificationKey::try_from(to_array(note_input.rk.bytes)).unwrap(),
ExtractedNoteCommitment::from_bytes(&to_array(note_input.cmx)).unwrap(), ExtractedNoteCommitment::from_bytes(&to_array(note_input.cmx.bytes)).unwrap(),
TransmittedNoteCiphertext {epk_bytes: to_array(note_input.eph_key), enc_ciphertext: to_array(note_input.enc_txt), out_ciphertext: to_array(note_input.out_txt)}, TransmittedNoteCiphertext {epk_bytes: to_array(note_input.eph_key.bytes), enc_ciphertext: to_array(note_input.enc_txt.bytes), out_ciphertext: to_array(note_input.out_txt.bytes)},
ValueCommitment::from_bytes(&to_array(note_input.cv)).unwrap(), ValueCommitment::from_bytes(&to_array(note_input.cv.bytes)).unwrap(),
Signature::from(to_array(note_input.auth))); Signature::from(to_array(note_input.auth.bytes)));
let fvk_array = to_array(fvk_input); let fvk_array = to_array(fvk_input);
let domain = OrchardDomain::for_nullifier(*action.nullifier()); let domain = OrchardDomain::for_nullifier(*action.nullifier());
let dec_fvk = FullViewingKey::from_bytes(&fvk_array); let dec_fvk = FullViewingKey::from_bytes(&fvk_array);

View file

@ -36,6 +36,7 @@ library:
- base58-bytestring - base58-bytestring
- cryptonite - cryptonite
- memory - memory
- hexstring
pkg-config-dependencies: pkg-config-dependencies:
- rustzcash_wrapper-uninstalled - rustzcash_wrapper-uninstalled
@ -54,3 +55,4 @@ tests:
- text - text
- aeson - aeson
- haskoin-core - haskoin-core
- hexstring

View file

@ -26,6 +26,7 @@ import C.Zcash
) )
import Data.Aeson import Data.Aeson
import qualified Data.ByteString as BS import qualified Data.ByteString as BS
import Data.HexString (HexString(..), toBytes)
import Foreign.Rust.Marshall.Variable (withPureBorshVarBuffer) import Foreign.Rust.Marshall.Variable (withPureBorshVarBuffer)
import ZcashHaskell.Types import ZcashHaskell.Types
( DecodedNote(..) ( DecodedNote(..)
@ -40,8 +41,8 @@ import ZcashHaskell.Utils (decodeBech32)
isValidShieldedAddress :: BS.ByteString -> Bool isValidShieldedAddress :: BS.ByteString -> Bool
isValidShieldedAddress = rustWrapperIsShielded isValidShieldedAddress = rustWrapperIsShielded
getShieldedOutputs :: BS.ByteString -> [BS.ByteString] getShieldedOutputs :: HexString -> [BS.ByteString]
getShieldedOutputs t = withPureBorshVarBuffer $ rustWrapperTxParse t getShieldedOutputs t = withPureBorshVarBuffer $ rustWrapperTxParse $ toBytes t
-- | Check if given bytestring is a valid Sapling viewing key -- | Check if given bytestring is a valid Sapling viewing key
isValidSaplingViewingKey :: BS.ByteString -> Bool isValidSaplingViewingKey :: BS.ByteString -> Bool
@ -76,24 +77,7 @@ instance FromJSON RawTxResponse where
c <- obj .: "confirmations" c <- obj .: "confirmations"
b <- obj .: "blocktime" b <- obj .: "blocktime"
case o of case o of
Nothing -> Nothing -> pure $ RawTxResponse i h (getShieldedOutputs h) [] ht c b
pure $
RawTxResponse
i
(decodeHexText h)
(getShieldedOutputs (decodeHexText h))
[]
ht
c
b
Just o' -> do Just o' -> do
a <- o' .: "actions" a <- o' .: "actions"
pure $ pure $ RawTxResponse i h (getShieldedOutputs h) a ht c b
RawTxResponse
i
(decodeHexText h)
(getShieldedOutputs (decodeHexText h))
a
ht
c
b

View file

@ -28,6 +28,7 @@ import Data.Aeson
import qualified Data.ByteArray as BA import qualified Data.ByteArray as BA
import qualified Data.ByteString as BS import qualified Data.ByteString as BS
import qualified Data.ByteString.Char8 as C import qualified Data.ByteString.Char8 as C
import Data.HexString
import Data.Int import Data.Int
import Data.Structured import Data.Structured
import qualified Data.Text as T import qualified Data.Text as T
@ -44,7 +45,6 @@ type Seed = C.ByteString
-- | A mnemonic phrase used to derive seeds -- | A mnemonic phrase used to derive seeds
type Phrase = BS.ByteString type Phrase = BS.ByteString
--
-- | Type to represent data after Bech32 decoding -- | Type to represent data after Bech32 decoding
data RawData = RawData data RawData = RawData
{ hrp :: BS.ByteString -- ^ Human-readable part of the Bech32 encoding { hrp :: BS.ByteString -- ^ Human-readable part of the Bech32 encoding
@ -116,8 +116,8 @@ instance FromJSON BlockResponse where
-- | Type to represent response from the `zcashd` RPC `getrawtransaction` -- | Type to represent response from the `zcashd` RPC `getrawtransaction`
data RawTxResponse = RawTxResponse data RawTxResponse = RawTxResponse
{ rt_id :: T.Text { rt_id :: !HexString
, rt_hex :: BS.ByteString , rt_hex :: !HexString
, rt_shieldedOutputs :: [BS.ByteString] , rt_shieldedOutputs :: [BS.ByteString]
, rt_orchardActions :: [OrchardAction] , rt_orchardActions :: [OrchardAction]
, rt_blockheight :: Integer , rt_blockheight :: Integer
@ -148,12 +148,12 @@ data TransparentAddress = TransparentAddress
-- * Sapling -- * Sapling
-- | Type to represent a Sapling Shielded Output as provided by the @getrawtransaction@ RPC method of @zcashd@. -- | Type to represent a Sapling Shielded Output as provided by the @getrawtransaction@ RPC method of @zcashd@.
data ShieldedOutput = ShieldedOutput data ShieldedOutput = ShieldedOutput
{ s_cv :: BS.ByteString -- ^ Value commitment to the input note { s_cv :: HexString -- ^ Value commitment to the input note
, s_cmu :: BS.ByteString -- ^ The u-coordinate of the note commitment for the output note , s_cmu :: HexString -- ^ The u-coordinate of the note commitment for the output note
, s_ephKey :: BS.ByteString -- ^ Ephemeral Jubjub public key , s_ephKey :: HexString -- ^ Ephemeral Jubjub public key
, s_encCipherText :: BS.ByteString -- ^ The output note encrypted to the recipient , s_encCipherText :: HexString -- ^ The output note encrypted to the recipient
, s_outCipherText :: BS.ByteString -- ^ A ciphertext enabling the sender to recover the output note , s_outCipherText :: HexString -- ^ A ciphertext enabling the sender to recover the output note
, s_proof :: BS.ByteString -- ^ Zero-knowledge proof using the Sapling Output circuit , s_proof :: HexString -- ^ Zero-knowledge proof using the Sapling Output circuit
} 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)
@ -168,14 +168,7 @@ instance FromJSON ShieldedOutput where
encText <- obj .: "encCiphertext" encText <- obj .: "encCiphertext"
outText <- obj .: "outCiphertext" outText <- obj .: "outCiphertext"
p <- obj .: "proof" p <- obj .: "proof"
pure $ pure $ ShieldedOutput cv cmu ephKey encText outText p
ShieldedOutput
(decodeHexText cv)
(decodeHexText cmu)
(decodeHexText ephKey)
(decodeHexText encText)
(decodeHexText outText)
(decodeHexText p)
-- * Orchard -- * Orchard
-- | Type to represent a Unified Address -- | Type to represent a Unified Address
@ -211,14 +204,14 @@ data UnifiedFullViewingKey = UnifiedFullViewingKey
-- | Type to represent an Orchard Action as provided by the @getrawtransaction@ RPC method of @zcashd@, and defined in the [Zcash Protocol](https://zips.z.cash/protocol/protocol.pdf) -- | Type to represent an Orchard Action as provided by the @getrawtransaction@ RPC method of @zcashd@, and defined in the [Zcash Protocol](https://zips.z.cash/protocol/protocol.pdf)
data OrchardAction = OrchardAction data OrchardAction = OrchardAction
{ nf :: BS.ByteString -- ^ The nullifier of the input note { nf :: HexString -- ^ The nullifier of the input note
, rk :: BS.ByteString -- ^ The randomized validating key for @auth@ , rk :: HexString -- ^ The randomized validating key for @auth@
, cmx :: BS.ByteString -- ^ The x-coordinate of the note commitment for the output note , cmx :: HexString -- ^ The x-coordinate of the note commitment for the output note
, eph_key :: BS.ByteString -- ^ An encoding of an ephemeral Pallas public key , eph_key :: HexString -- ^ An encoding of an ephemeral Pallas public key
, enc_ciphertext :: BS.ByteString -- ^ The output note encrypted to the recipient , enc_ciphertext :: HexString -- ^ The output note encrypted to the recipient
, out_ciphertext :: BS.ByteString -- ^ A ciphertext enabling the sender to recover the output note , out_ciphertext :: HexString -- ^ A ciphertext enabling the sender to recover the output note
, cv :: BS.ByteString -- ^ A value commitment to the net value of the input note minus the output note , cv :: HexString -- ^ A value commitment to the net value of the input note minus the output note
, auth :: BS.ByteString -- ^ A signature authorizing the spend in this Action , auth :: HexString -- ^ A signature authorizing the spend in this Action
} 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)
@ -235,16 +228,7 @@ instance FromJSON OrchardAction where
outText <- obj .: "outCiphertext" outText <- obj .: "outCiphertext"
cval <- obj .: "cv" cval <- obj .: "cv"
a <- obj .: "spendAuthSig" a <- obj .: "spendAuthSig"
pure $ pure $ OrchardAction n r c ephKey encText outText cval a
OrchardAction
(decodeHexText n)
(decodeHexText r)
(decodeHexText c)
(decodeHexText ephKey)
(decodeHexText encText)
(decodeHexText outText)
(decodeHexText cval)
(decodeHexText a)
-- | Type to represent a decoded note -- | Type to represent a decoded note
data DecodedNote = DecodedNote data DecodedNote = DecodedNote

View file

@ -40,6 +40,8 @@ extra-deps:
commit: 787c2e813eb3a5d16c375d4b37dfefbd2adcdf05 commit: 787c2e813eb3a5d16c375d4b37dfefbd2adcdf05
- git: https://github.com/well-typed/borsh.git - git: https://github.com/well-typed/borsh.git
commit: d2fcfa159e0a844b1ec5e8ed3e232d4b380fa831 commit: d2fcfa159e0a844b1ec5e8ed3e232d4b380fa831
- git: https://git.vergara.tech/Vergara_Tech/haskell-hexstring.git
commit: fe2df6f7d63272ac147911c1573550bed1d38a37
- vector-0.13.0.0@sha256:fa5cac81a17a5af388716792e8b99c24b3b66770086756d0d8b23f8272a0244c,9112 - vector-0.13.0.0@sha256:fa5cac81a17a5af388716792e8b99c24b3b66770086756d0d8b23f8272a0244c,9112
- aeson-2.1.2.1@sha256:f10f3c661bd5cf57aee46b94420e47736240b8e209ac15f4bfc1a4e4d55831fa,6344 - aeson-2.1.2.1@sha256:f10f3c661bd5cf57aee46b94420e47736240b8e209ac15f4bfc1a4e4d55831fa,6344
- generically-0.1.1 - generically-0.1.1

File diff suppressed because one or more lines are too long

View file

@ -48,6 +48,7 @@ library
, cryptonite , cryptonite
, foreign-rust , foreign-rust
, generics-sop , generics-sop
, hexstring
, http-conduit , http-conduit
, memory , memory
, text , text
@ -66,6 +67,7 @@ test-suite zcash-haskell-test
, base >=4.7 && <5 , base >=4.7 && <5
, bytestring , bytestring
, haskoin-core , haskoin-core
, hexstring
, hspec , hspec
, text , text
, zcash-haskell , zcash-haskell