Merge pull request 'Implement HexString' (#4) from fix080 into dev040

Reviewed-on: #4
This commit is contained in:
pitmutt 2024-02-06 19:33:02 +00:00 committed by Vergara Technologies LLC
commit 6ea8698ccb
Signed by: Vergara Technologies LLC
GPG key ID: 99DB473BB4715618
7 changed files with 68 additions and 88 deletions

View file

@ -145,16 +145,21 @@ impl HshieldedOutput {
}
}
#[derive(BorshSerialize, BorshDeserialize)]
pub struct Hhex {
bytes: Vec<u8>
}
#[derive(BorshSerialize, BorshDeserialize)]
pub struct Haction {
nf: Vec<u8>,
rk: Vec<u8>,
cmx: Vec<u8>,
eph_key: Vec<u8>,
enc_txt: Vec<u8>,
out_txt: Vec<u8>,
cv: Vec<u8>,
auth: Vec<u8>
nf: Hhex,
rk: Hhex,
cmx: Hhex,
eph_key: Hhex,
enc_txt: Hhex,
out_txt: Hhex,
cv: Hhex,
auth: Hhex
}
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 note_input: Haction = marshall_from_haskell_var(note, note_len, RW);
let action: Action<Signature<SpendAuth>> = Action::from_parts(
Nullifier::from_bytes(&to_array(note_input.nf)).unwrap(),
VerificationKey::try_from(to_array(note_input.rk)).unwrap(),
ExtractedNoteCommitment::from_bytes(&to_array(note_input.cmx)).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)},
ValueCommitment::from_bytes(&to_array(note_input.cv)).unwrap(),
Signature::from(to_array(note_input.auth)));
Nullifier::from_bytes(&to_array(note_input.nf.bytes)).unwrap(),
VerificationKey::try_from(to_array(note_input.rk.bytes)).unwrap(),
ExtractedNoteCommitment::from_bytes(&to_array(note_input.cmx.bytes)).unwrap(),
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.bytes)).unwrap(),
Signature::from(to_array(note_input.auth.bytes)));
let fvk_array = to_array(fvk_input);
let domain = OrchardDomain::for_nullifier(*action.nullifier());
let dec_fvk = FullViewingKey::from_bytes(&fvk_array);

View file

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

View file

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

View file

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

View file

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

File diff suppressed because one or more lines are too long

View file

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