rvv040 - decodeSaplingAddress Function added

SaplingAddress  data type added
This commit is contained in:
Rene V. Vergara A. 2024-04-16 14:39:56 -04:00
parent 0e52adbea4
commit 382284a8ca
6 changed files with 114 additions and 1 deletions

View file

@ -5,6 +5,21 @@ 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.5.1]
### Added
- Added unction to decode a Sappling Address in Human Readable Format
`decodeSaplingAddress` returns 43 byte array containing
- Added a new Datatype `SaplingAddress`
- Added a new FFI function `rust_wrapper_decode_sapling_address` to haskell-rust interface
### Changed
- `TransparentAddress` type refactored
- `TransparentReceiver` added to replace old `TransparentAddress`
- `sha256` Function moved outside of `encodeTransparentReceiver`
## [0.5.5.0] ## [0.5.5.0]
### Added ### Added
@ -16,7 +31,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `TransparentAddress` type refactored - `TransparentAddress` type refactored
- `TransparentReceiver` added to replace old `TransparentAddress` - `TransparentReceiver` added to replace old `TransparentAddress`
- `sha256` Function moved outside of `encodeTransparentReceiver` - `sha256` Function moved outside of `encodeTransparentReceiver`
## [0.5.4.1] ## [0.5.4.1]
### Added ### Added

View file

@ -90,6 +90,7 @@ use zcash_address::{
ZcashAddress ZcashAddress
}; };
use zcash_client_backend::encoding::decode_payment_address;
use zcash_client_backend::keys::sapling::{ use zcash_client_backend::keys::sapling::{
spending_key, spending_key,
ExtendedFullViewingKey, ExtendedFullViewingKey,
@ -1242,3 +1243,50 @@ pub extern "C" fn rust_wrapper_read_sapling_position(
} }
} }
} }
#[no_mangle]
pub extern "C" fn rust_wrapper_decode_sapling_address(
sapling: *const u8,
sapling_len: usize,
out: *mut u8,
out_len: &mut usize){
let sapling_address_from_haskell : Vec<u8> = marshall_from_haskell_var(sapling, sapling_len, RW);
let sapling_address = std::str::from_utf8(&sapling_address_from_haskell).unwrap();
let mut netid = 0;
match sapling_address.find("1") {
Some(ix) => {
let netstr = &sapling_address[0..ix];
if netstr == "zs" {
netid = 1
} else {
if netstr == "ztestsapling" {
netid = 2
}
}
match decode_payment_address(netstr, sapling_address) {
Ok( t )=> {
let address_to_bytes = t.to_bytes();
let mut out_bytes_temp : [u8;44] = [0;44];
out_bytes_temp[0] = netid;
let mut iy = 1;
for ix in 0..43 {
out_bytes_temp[iy] = address_to_bytes[ix];
iy += 1;
}
let out_bytes: Vec<u8> = out_bytes_temp.to_vec();
marshall_to_haskell_var(&out_bytes, out, out_len, RW);
}
Err(e) => {
let h = vec![0];
marshall_to_haskell_var(&h, out, out_len, RW);
}
}
}
None => {
let h = vec![0];
marshall_to_haskell_var(&h, out, out_len, RW);
}
}
}

View file

@ -232,3 +232,10 @@ import ZcashHaskell.Types
-> `()' -> `()'
#} #}
{# fun unsafe rust_wrapper_decode_sapling_address as rustWrapperDecodeSaplingAddress
{ toBorshVar* `BS.ByteString'&
, getVarBuffer `Buffer (BS.ByteString)'&
}
-> `()'
#}

View file

@ -22,6 +22,7 @@ import C.Zcash
, rustWrapperReadSaplingCommitmentTree , rustWrapperReadSaplingCommitmentTree
, rustWrapperReadSaplingPosition , rustWrapperReadSaplingPosition
, rustWrapperReadSaplingWitness , rustWrapperReadSaplingWitness
, rustWrapperDecodeSaplingAddress
, rustWrapperSaplingCheck , rustWrapperSaplingCheck
, rustWrapperSaplingChgPaymentAddress , rustWrapperSaplingChgPaymentAddress
, rustWrapperSaplingDecodeEsk , rustWrapperSaplingDecodeEsk
@ -45,6 +46,7 @@ import ZcashHaskell.Types
, DecodedNote(..) , DecodedNote(..)
, RawData(..) , RawData(..)
, RawTxResponse(..) , RawTxResponse(..)
, SaplingAddress(..)
, SaplingCommitmentTree(..) , SaplingCommitmentTree(..)
, SaplingReceiver(..) , SaplingReceiver(..)
, SaplingSpendingKey(..) , SaplingSpendingKey(..)
@ -205,3 +207,26 @@ getSaplingWitness tree =
getSaplingNotePosition :: SaplingWitness -> Integer getSaplingNotePosition :: SaplingWitness -> Integer
getSaplingNotePosition = getSaplingNotePosition =
fromIntegral . rustWrapperReadSaplingPosition . hexBytes . sapWit fromIntegral . rustWrapperReadSaplingPosition . hexBytes . sapWit
getNetId:: [Word8] -> ZcashNet
getNetId [x] = do
case x of
1 -> MainNet
2 -> TestNet
-- | decode a Sapling address
decodeSaplingAddress :: BS.ByteString -> Maybe SaplingAddress
decodeSaplingAddress sapling_address = do
if BS.length sa > 1
then do
let sa0 = BS.unpack sa
Just $ SaplingAddress (getNetId (take 1 sa0)) (BS.pack (drop 1 sa0))
else Nothing
where
sa =
withPureBorshVarBuffer $
rustWrapperDecodeSaplingAddress sapling_address

View file

@ -477,6 +477,12 @@ newtype SaplingReceiver =
instance ToBytes SaplingReceiver where instance ToBytes SaplingReceiver where
getBytes (SaplingReceiver s) = s getBytes (SaplingReceiver s) = s
data SaplingAddress = SaplingAddress
{
net_type :: !ZcashNet
, address :: !BS.ByteString
} deriving (Eq, Prelude.Show, Read)
-- | Type to represent a Sapling Shielded Spend as provided by the @getrawtransaction@ RPC method -- | Type to represent a Sapling Shielded Spend as provided by the @getrawtransaction@ RPC method
data ShieldedSpend = ShieldedSpend data ShieldedSpend = ShieldedSpend
{ sp_cv :: !HexString { sp_cv :: !HexString

View file

@ -57,6 +57,7 @@ import ZcashHaskell.Sapling
, isValidShieldedAddress , isValidShieldedAddress
, matchSaplingAddress , matchSaplingAddress
, updateSaplingCommitmentTree , updateSaplingCommitmentTree
, decodeSaplingAddress
) )
import ZcashHaskell.Transparent import ZcashHaskell.Transparent
import ZcashHaskell.Types import ZcashHaskell.Types
@ -94,6 +95,7 @@ import ZcashHaskell.Types
, fromRawSBundle , fromRawSBundle
, fromRawTBundle , fromRawTBundle
, getValue , getValue
, SaplingAddress(..)
) )
import ZcashHaskell.Utils import ZcashHaskell.Utils
@ -911,6 +913,17 @@ main = do
t_rec u t_rec u
(ta_receiver <$> decodeTransparentAddress (E.encodeUtf8 tAdd)) `shouldBe` (ta_receiver <$> decodeTransparentAddress (E.encodeUtf8 tAdd)) `shouldBe`
t_rec u t_rec u
describe "Decode a Sapling Address (MainNet)" $ do
let sa = decodeSaplingAddress "zs1waxrpde36rlrjdwfhnvw030sn29lzwmvmeupd8x2uuqgypaafx7mqcy0ep8yf2xtg30n5424t60"
it "Try to decode a valid MainNet Sapling Address" $ do
print sa
sa `shouldNotBe` Nothing
describe "Decode a Sapling Address (TestNet)" $ do
let sa = decodeSaplingAddress "ztestsapling188csdsvhdny25am8ume03qr2026hdy03zpg5pq7jmmfxtxtct0e93p0rg80yfxvynqd4gwlwft5"
it "Try to decode a valid TestNet Sapling Address " $ do
print sa
sa `shouldBe` Nothing
-- | Properties -- | Properties
prop_PhraseLength :: Property prop_PhraseLength :: Property