From 6b72a1f13cfd3484ff086e936406acfb074a69c4 Mon Sep 17 00:00:00 2001 From: fluidvanadium Date: Fri, 12 Apr 2024 15:34:24 +0000 Subject: [PATCH 1/2] added dep from workspace --- zingolib/Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/zingolib/Cargo.toml b/zingolib/Cargo.toml index 080cfdeda9..f02582091c 100644 --- a/zingolib/Cargo.toml +++ b/zingolib/Cargo.toml @@ -37,6 +37,7 @@ zcash_note_encryption = { workspace = true } zcash_primitives = { workspace = true } zcash_proofs = { workspace = true } hyper-rustls = { workspace = true } +zip32.workspace = true append-only-vec = { git = "https://github.com/zancas/append-only-vec.git", branch = "add_debug_impl" } From e3f01c53389dd4a9df4b0a2ffd61adfbda3db484 Mon Sep 17 00:00:00 2001 From: Oscar Pepper Date: Fri, 12 Apr 2024 18:58:57 +0100 Subject: [PATCH 2/2] added new utils mod and conversion function from txid hex string to TxId struct --- integration-tests/tests/integrations.rs | 10 +++----- zingolib/CHANGELOG.md | 5 ++-- zingolib/src/lib.rs | 1 + zingolib/src/utils.rs | 33 +++++++++++++++++++++++++ zingolib/src/wallet/utils.rs | 2 +- 5 files changed, 41 insertions(+), 10 deletions(-) create mode 100644 zingolib/src/utils.rs diff --git a/integration-tests/tests/integrations.rs b/integration-tests/tests/integrations.rs index 43e9f60856..e1a2eff0f4 100644 --- a/integration-tests/tests/integrations.rs +++ b/integration-tests/tests/integrations.rs @@ -12,7 +12,7 @@ use zcash_primitives::{ consensus::{BlockHeight, Parameters}, memo::Memo, memo::MemoBytes, - transaction::{fees::zip317::MINIMUM_FEE, TxId}, + transaction::fees::zip317::MINIMUM_FEE, }; use zingo_testutils::{ self, build_fvk_client, check_transaction_equality, increase_height_and_wait_for_client, @@ -28,6 +28,7 @@ use zingolib::{ seeds::{CHIMNEY_BETTER_SEED, HOSPITAL_MUSEUM_SEED}, BASE_HEIGHT, }, + utils, wallet::{ data::{COMMITMENT_TREE_LEVELS, MAX_SHARD_LEVEL}, keys::{ @@ -729,12 +730,7 @@ mod slow { async fn witness_clearing() { let (regtest_manager, _cph, faucet, recipient, txid) = scenarios::faucet_funded_recipient_default(100_000).await; - dbg!(&txid); - let mut txid_bytes = <[u8; 32]>::try_from(hex::decode(txid).unwrap()).unwrap(); - // TxId byte order is displayed in the reverse order from how it's encoded, for some reason - txid_bytes.reverse(); - let txid = TxId::from_bytes(txid_bytes); - dbg!(&txid); + let txid = utils::txid_from_hex_encoded_str(&txid).unwrap(); // 3. Send z-to-z transaction to external z address with a memo let sent_value = 2000; diff --git a/zingolib/CHANGELOG.md b/zingolib/CHANGELOG.md index c76c683993..7e5a6dfcc7 100644 --- a/zingolib/CHANGELOG.md +++ b/zingolib/CHANGELOG.md @@ -17,9 +17,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - LightClient pub fn export_save_buffer_runtime - LightClient pub fn get_wallet_file_location - LightClient pub fn get_wallet_dir_location -- `wallet::keys`: - - `is_transparent_address` +- `wallet::keys::is_transparent_address` - pub struct crate::wallet::notes::NoteRecordIdentifier +- `utils` mod +- `utils::txid_from_hex_encoded_str` fn ### Changed diff --git a/zingolib/src/lib.rs b/zingolib/src/lib.rs index 923d291603..bc600ddf19 100644 --- a/zingolib/src/lib.rs +++ b/zingolib/src/lib.rs @@ -8,6 +8,7 @@ pub mod data; pub mod error; pub mod grpc_connector; pub mod lightclient; +pub mod utils; pub mod wallet; #[cfg(feature = "test")] pub use zingo_testvectors as testvectors; diff --git a/zingolib/src/utils.rs b/zingolib/src/utils.rs new file mode 100644 index 0000000000..6310401064 --- /dev/null +++ b/zingolib/src/utils.rs @@ -0,0 +1,33 @@ +//! General library utilities such as parsing and conversions. + +use std::fmt; + +use zcash_primitives::transaction::TxId; + +/// Converts txid from hex-encoded `&str` to `zcash_primitives::transaction::TxId`. +/// +/// TxId byte order is displayed in the reverse order to how it's encoded. +pub fn txid_from_hex_encoded_str(txid: &str) -> Result { + let txid_bytes = hex::decode(txid).unwrap(); + let mut txid_bytes = <[u8; 32]>::try_from(txid_bytes).unwrap(); + txid_bytes.reverse(); + Ok(TxId::from_bytes(txid_bytes)) +} + +/// The error type for conversion errors. +#[derive(Debug, Clone, Copy, PartialEq)] +pub enum ConversionError { + DecodeHexFailed(hex::FromHexError), + InvalidStringLength, +} + +impl std::error::Error for ConversionError {} + +impl fmt::Display for ConversionError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match *self { + ConversionError::DecodeHexFailed(e) => write!(f, "failed to decode hex. {}", e), + ConversionError::InvalidStringLength => write!(f, "invalid string length"), + } + } +} diff --git a/zingolib/src/wallet/utils.rs b/zingolib/src/wallet/utils.rs index cf376521f2..fc204b6b83 100644 --- a/zingolib/src/wallet/utils.rs +++ b/zingolib/src/wallet/utils.rs @@ -22,7 +22,7 @@ pub fn write_string(mut writer: W, s: &String) -> io::Result<()> { writer.write_all(s.as_bytes()) } -// Interpret a string or hex-encoded memo, and return a Memo object +/// Interpret a string or hex-encoded memo, and return a Memo object pub fn interpret_memo_string(memo_str: String) -> Result { // If the string starts with an "0x", and contains only hex chars ([a-f0-9]+) then // interpret it as a hex