rvv040 - Sapling address encoding to human readable format

Change Bech32m to Bech32  encoding for Sapling address
This commit is contained in:
Rene V. Vergara A. 2024-04-10 09:55:41 -04:00
parent 1f8a4cbd2b
commit 3f918f734a
5 changed files with 40 additions and 8 deletions

View file

@ -105,6 +105,7 @@ use orchard::{
use bech32::{ use bech32::{
Hrp, Hrp,
Bech32,
Bech32m Bech32m
}; };
@ -585,7 +586,7 @@ pub extern "C" fn rust_wrapper_bech32decode(
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn rust_wrapper_bech32_encode( pub extern "C" fn rust_wrapper_bech32m_encode(
hr: *const u8, hr: *const u8,
hr_len: usize, hr_len: usize,
b: *const u8, b: *const u8,
@ -1088,3 +1089,20 @@ pub extern "C" fn rust_wrapper_derive_orchard_receiver(
marshall_to_haskell_var(&o_rec.to_raw_address_bytes().to_vec(), out, out_len, RW); marshall_to_haskell_var(&o_rec.to_raw_address_bytes().to_vec(), out, out_len, RW);
} }
#[no_mangle]
pub extern "C" fn rust_wrapper_bech32_encode(
hr: *const u8,
hr_len: usize,
b: *const u8,
b_len: usize,
out: *mut u8,
out_len: &mut usize
) {
let hr: String = marshall_from_haskell_var(hr, hr_len, RW);
let hrp = Hrp::parse(&hr).unwrap();
let b: Vec<u8> = marshall_from_haskell_var(b, b_len, RW);
let string = bech32::encode::<Bech32>(hrp, &b).unwrap();
marshall_to_haskell_var(&string, out, out_len, RW);
}

View file

@ -38,7 +38,7 @@ import ZcashHaskell.Types
-> `()' -> `()'
#} #}
{# fun unsafe rust_wrapper_bech32_encode as rustWrapperBech32Encode {# fun unsafe rust_wrapper_bech32m_encode as rustWrapperBech32mEncode
{ toBorshVar* `BS.ByteString'& { toBorshVar* `BS.ByteString'&
, toBorshVar* `BS.ByteString'& , toBorshVar* `BS.ByteString'&
, getVarBuffer `Buffer (T.Text)'& , getVarBuffer `Buffer (T.Text)'&
@ -192,3 +192,12 @@ import ZcashHaskell.Types
} }
-> `()' -> `()'
#} #}
{# fun unsafe rust_wrapper_bech32_encode as rustWrapperBech32Encode
{ toBorshVar* `BS.ByteString'&
, toBorshVar* `BS.ByteString'&
, getVarBuffer `Buffer (T.Text)'&
}
-> `()'
#}

View file

@ -33,7 +33,7 @@ import qualified Data.Text.Encoding as E
import Data.Word import Data.Word
import Foreign.Rust.Marshall.Variable import Foreign.Rust.Marshall.Variable
import ZcashHaskell.Types import ZcashHaskell.Types
import ZcashHaskell.Utils (encodeBech32m, f4Jumble) import ZcashHaskell.Utils (encodeBech32m, encodeBech32, f4Jumble)
-- | Derives an Orchard spending key for the given seed and account ID -- | Derives an Orchard spending key for the given seed and account ID
genOrchardSpendingKey :: genOrchardSpendingKey ::
@ -169,5 +169,5 @@ getSaplingFromUA uadd = do
Just sraw -> do Just sraw -> do
let net = ua_net a let net = ua_net a
case net of case net of
MainNet -> encodeBech32m (chrToByteString sapPaymentAddressHrp) ( getBytes sraw ) MainNet -> encodeBech32 (chrToByteString sapPaymentAddressHrp) ( getBytes sraw )
TestNet -> encodeBech32m (chrToByteString sapTestPaymentAddressHrp) ( getBytes sraw ) TestNet -> encodeBech32 (chrToByteString sapTestPaymentAddressHrp) ( getBytes sraw )

View file

@ -19,6 +19,7 @@ module ZcashHaskell.Utils where
import C.Zcash import C.Zcash
( rustWrapperBech32Decode ( rustWrapperBech32Decode
, rustWrapperBech32mEncode
, rustWrapperBech32Encode , rustWrapperBech32Encode
, rustWrapperF4Jumble , rustWrapperF4Jumble
, rustWrapperF4UnJumble , rustWrapperF4UnJumble
@ -45,7 +46,11 @@ decodeBech32 = withPureBorshVarBuffer . rustWrapperBech32Decode
-- | Encode the given Human Readable Part and bytestring as a Bech32m string -- | Encode the given Human Readable Part and bytestring as a Bech32m string
encodeBech32m :: BS.ByteString -> BS.ByteString -> T.Text encodeBech32m :: BS.ByteString -> BS.ByteString -> T.Text
encodeBech32m h d = withPureBorshVarBuffer $ rustWrapperBech32Encode h d encodeBech32m h d = withPureBorshVarBuffer $ rustWrapperBech32mEncode h d
-- | Encode the given Human Readable Part and bytestring as a Bech32 string
encodeBech32 :: BS.ByteString -> BS.ByteString -> T.Text
encodeBech32 h d = withPureBorshVarBuffer $ rustWrapperBech32Encode h d
-- | Apply the F4Jumble transformation to the given bytestring -- | Apply the F4Jumble transformation to the given bytestring
f4Jumble :: BS.ByteString -> BS.ByteString f4Jumble :: BS.ByteString -> BS.ByteString

View file

@ -845,10 +845,10 @@ main = do
print tb print tb
show tb `shouldNotBe` "" show tb `shouldNotBe` ""
describe "Extract Sapling Address" $ do describe "Extract Sapling Address" $ do
let sr = getSaplingFromUA "u1y3224rl7f5tz262apkmj33z6pe5rn3z4y6pqtces45valg9qsvuer688crr8xvvqq4u7umhrvt0qaj5jv05ec7jd66qedq97dvrhhzryq69lrm8mc25ql0zt6yk2yks9zyp0qv7tgtfqw5zgl7q3p660v37f0ra5aj2g8y75mrzkgrgr8fl5cmur07tcqcrvzf96t4ennh45vnxemp8" let sr = getSaplingFromUA "u14a5c4ufn9feqvxssnvscep29j5cse4gjpg0w3w5vjhafn74hg9k73xgnxqv6m255n23weggr6j97c8kdwvn4pkz7rz6my52z8248gjmr7knlw536tcurs5km7knqnzez4cywudt3q6shr553hurduvljfeqvfzgegenfjashslkz3y4ykhxel6mrjp9gsm9xk7k6kdxn9y84kccmv8l"
it "Extract sapling address" $ do it "Extract sapling address" $ do
print sr print sr
sr `shouldBe` "zs1algtrzkczkl5708yaw2sr58jkutm8wcx2tj0a9ex3ns8nu8l3wtlusemfcpllnfmhyhw7vk9m0h" sr `shouldBe` "zs1waxrpde36rlrjdwfhnvw030sn29lzwmvmeupd8x2uuqgypaafx7mqcy0ep8yf2xtg30n5424t60"
-- | Properties -- | Properties