Skip to content

Commit 91fe936

Browse files
fix(rpc): using dedicated big-endian hex parser
1 parent eabfb62 commit 91fe936

File tree

4 files changed

+16
-28
lines changed

4 files changed

+16
-28
lines changed

zallet/src/components/json_rpc/methods/get_raw_transaction.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use schemars::JsonSchema;
55
use serde::Serialize;
66
use transparent::bundle::{TxIn, TxOut};
77
use zaino_state::{FetchServiceError, FetchServiceSubscriber, LightWalletIndexer, ZcashIndexer};
8+
use zcash_encoding::ReverseHex;
89
use zcash_protocol::{
910
TxId,
1011
consensus::{self, BlockHeight},
@@ -15,7 +16,7 @@ use crate::components::{
1516
database::DbConnection,
1617
json_rpc::{
1718
server::LegacyCode,
18-
utils::{JsonZec, JsonZecBalance, parse_txid, value_from_zat_balance, value_from_zatoshis},
19+
utils::{JsonZec, JsonZecBalance, value_from_zat_balance, value_from_zatoshis},
1920
},
2021
};
2122

@@ -475,7 +476,7 @@ pub(crate) async fn call(
475476
verbose: Option<u64>,
476477
blockhash: Option<String>,
477478
) -> Response {
478-
let _txid = parse_txid(txid_str)?;
479+
let _txid = TxId::from_reverse_hex(txid_str)?;
479480
let verbose = verbose.is_some_and(|v| v != 0);
480481

481482
// TODO: We can't support this via the current Zaino API; wait for `ChainIndex`.
@@ -572,7 +573,7 @@ pub(crate) async fn call(
572573
let (vjoinsplit, join_split_pub_key, join_split_sig) = match tx.sprout_bundle() {
573574
Some(bundle) if !bundle.joinsplits.is_empty() => (
574575
bundle.joinsplits.iter().map(JoinSplit::encode).collect(),
575-
Some(TxId::from_bytes(bundle.joinsplit_pubkey).to_string()),
576+
Some(ReverseHex::decode(bundle.joinsplit_pubkey)),
576577
Some(hex::encode(bundle.joinsplit_sig)),
577578
),
578579
_ => (vec![], None, None),
@@ -614,8 +615,7 @@ pub(crate) async fn call(
614615
in_active_chain: None,
615616
hex: tx_hex,
616617
txid: txid_str.to_ascii_lowercase(),
617-
authdigest: TxId::from_bytes(tx.auth_commitment().as_bytes().try_into().unwrap())
618-
.to_string(),
618+
authdigest: ReverseHex::decode(&tx.auth_commitment().as_bytes().try_into().unwrap()),
619619
size,
620620
overwintered,
621621
version: tx.version().header() & 0x7FFFFFFF,
@@ -709,10 +709,10 @@ impl JoinSplit {
709709
impl SaplingSpend {
710710
fn encode(spend: &SpendDescription<sapling::bundle::Authorized>) -> Self {
711711
Self {
712-
cv: TxId::from_bytes(spend.cv().to_bytes()).to_string(),
713-
anchor: TxId::from_bytes(spend.anchor().to_bytes()).to_string(),
714-
nullifier: TxId::from_bytes(spend.nullifier().0).to_string(),
715-
rk: TxId::from_bytes(<[u8; 32]>::from(*spend.rk())).to_string(),
712+
cv: ReverseHex::decode(&spend.cv().to_bytes()),
713+
anchor: ReverseHex::decode(&spend.anchor().to_bytes()),
714+
nullifier: ReverseHex::decode(&spend.nullifier().0),
715+
rk: ReverseHex::decode(&<[u8; 32]>::from(*spend.rk())),
716716
proof: hex::encode(spend.zkproof()),
717717
spend_auth_sig: hex::encode(<[u8; 64]>::from(*spend.spend_auth_sig())),
718718
}
@@ -722,9 +722,9 @@ impl SaplingSpend {
722722
impl SaplingOutput {
723723
fn encode(output: &OutputDescription<sapling::bundle::GrothProofBytes>) -> Self {
724724
Self {
725-
cv: TxId::from_bytes(output.cv().to_bytes()).to_string(),
726-
cmu: TxId::from_bytes(output.cmu().to_bytes()).to_string(),
727-
ephemeral_key: TxId::from_bytes(output.ephemeral_key().0).to_string(),
725+
cv: ReverseHex::decode(&output.cv().to_bytes()),
726+
cmu: ReverseHex::decode(&output.cmu().to_bytes()),
727+
ephemeral_key: ReverseHex::decode(&output.ephemeral_key().0),
728728
enc_ciphertext: hex::encode(output.enc_ciphertext()),
729729
out_ciphertext: hex::encode(output.out_ciphertext()),
730730
proof: hex::encode(output.zkproof()),

zallet/src/components/json_rpc/methods/view_transaction.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use crate::components::{
3030
database::DbConnection,
3131
json_rpc::{
3232
server::LegacyCode,
33-
utils::{JsonZec, parse_txid, value_from_zatoshis},
33+
utils::{JsonZec, value_from_zatoshis},
3434
},
3535
};
3636

@@ -275,7 +275,7 @@ pub(crate) async fn call(
275275
chain: FetchServiceSubscriber,
276276
txid_str: &str,
277277
) -> Response {
278-
let txid = parse_txid(txid_str)?;
278+
let txid = TxId::from_reverse_hex(txid_str)?;
279279

280280
// Fetch this early so we can detect if the wallet is not ready yet.
281281
// TODO: Replace with Zaino `ChainIndex` so we can operate against a chain snapshot.

zallet/src/components/json_rpc/utils.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,6 @@ pub(super) async fn ensure_wallet_is_unlocked(keystore: &KeyStore) -> RpcResult<
4343
}
4444
}
4545

46-
// TODO: Move this to `zcash_protocol`.
47-
// https://github.com/zcash/librustzcash/issues/1934
48-
pub(crate) fn parse_txid(txid_str: &str) -> RpcResult<TxId> {
49-
let mut bytes = [0; 32];
50-
hex::decode_to_slice(txid_str, &mut bytes)
51-
.map_err(|_| LegacyCode::InvalidParameter.with_static("invalid txid"))?;
52-
bytes.reverse();
53-
Ok(TxId::from_bytes(bytes))
54-
}
55-
5646
/// Parses the `seedfp` parameter present in many wallet RPCs.
5747
#[cfg(zallet_build = "wallet")]
5848
pub(super) fn parse_seedfp_parameter(seedfp: &str) -> RpcResult<SeedFingerprint> {

zallet/src/components/sync.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@ use super::{
3838
chain_view::ChainView,
3939
database::{Database, DbConnection},
4040
};
41-
use crate::{
42-
components::json_rpc::utils::parse_txid, config::ZalletConfig, error::Error, network::Network,
43-
};
41+
use crate::{config::ZalletConfig, error::Error, network::Network};
4442

4543
mod cache;
4644

@@ -649,7 +647,7 @@ async fn data_requests(
649647
};
650648

651649
for txid_str in chain.get_address_tx_ids(request).await? {
652-
let txid = parse_txid(&txid_str)
650+
let txid = TxId::from_reverse_hex(&txid_str)
653651
.expect("TODO: Zaino's API should have caught this error for us");
654652

655653
let tx = match chain.get_raw_transaction(txid_str, Some(1)).await? {

0 commit comments

Comments
 (0)