Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new zingolib utils mod with txid converter #946

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions integration-tests/tests/integrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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::{
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions zingolib/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions zingolib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
33 changes: 33 additions & 0 deletions zingolib/src/utils.rs
Original file line number Diff line number Diff line change
@@ -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<TxId, ConversionError> {
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"),
}
}
}
2 changes: 1 addition & 1 deletion zingolib/src/wallet/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub fn write_string<W: Write>(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<MemoBytes, String> {
// If the string starts with an "0x", and contains only hex chars ([a-f0-9]+) then
// interpret it as a hex
Expand Down
Loading