Skip to content

Commit

Permalink
try hex crate instead of hex-conservative
Browse files Browse the repository at this point in the history
  • Loading branch information
RCasatta committed Oct 11, 2024
1 parent 311b075 commit 1be663c
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 10 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ lru = "0.12.0"
prometheus = { version = "0.13.4", features = ["process"] }
lazy_static = "1.5.0"
async_zmq = "0.4.0"
hex = "0.4.3"

[dev-dependencies]
bitcoind = { version = "0.31.0" }
Expand Down
3 changes: 3 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ pub enum Error {
#[error(transparent)]
HexArray(#[from] bitcoin::hashes::hex::HexToArrayError),

#[error(transparent)]
Hex2(#[from] hex::FromHexError),

#[error(transparent)]
ParseInt(#[from] std::num::ParseIntError),

Expand Down
5 changes: 2 additions & 3 deletions src/req.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use crate::threads::index_addresses::Height;
use crate::NetworkExt;
use crate::{error::Error, route::ResponseType};
use bitcoin::address::NetworkUnchecked;
use bitcoin::hashes::hex::FromHex;
use bitcoin::hashes::{sha256d, Hash};
use bitcoin::{consensus::deserialize, Address, BlockHash, Transaction, Txid};
use bitcoin::{OutPoint, Psbt};
Expand Down Expand Up @@ -90,7 +89,7 @@ pub async fn parse(req: &Request<Body>) -> Result<ParsedRequest, Error> {
Err(_) => match Address::from_str(val) {
Ok(address) => Resource::SearchAddress(address.assume_checked()),
Err(_) => {
match Vec::<u8>::from_hex(val)
match hex::decode(val)
.map(|bytes| deserialize::<Transaction>(&bytes))
{
Ok(Ok(tx)) => Resource::SearchFullTx(tx),
Expand Down Expand Up @@ -156,7 +155,7 @@ pub async fn parse(req: &Request<Body>) -> Result<ParsedRequest, Error> {
Resource::TxToT(txid)
}
(&Method::GET, None, Some(&"txhex"), Some(hex), None) => {
let bytes = Vec::<u8>::from_hex(hex)?;
let bytes = hex::decode(hex)?;
let tx: Transaction = deserialize(&bytes)?;
Resource::FullTx(tx)
}
Expand Down
6 changes: 1 addition & 5 deletions src/rpc/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use crate::error::Error;
use crate::state::SerTx;
use crate::NODE_REST_COUNTER;
use bitcoin::consensus::serialize;
use bitcoin::hashes::hex::FromHex;
use bitcoin::{blockdata::constants::genesis_block, BlockHash, Network, Txid};
use hyper::body::Buf;
use once_cell::sync::Lazy;
Expand Down Expand Up @@ -37,10 +36,7 @@ pub async fn call_parse_json(
network: Network,
) -> Result<(Option<BlockHash>, SerTx), Error> {
Ok(match call_json(txid).await {
Ok(tx_json) => (
tx_json.block_hash,
SerTx(Vec::<u8>::from_hex(&tx_json.hex)?),
),
Ok(tx_json) => (tx_json.block_hash, SerTx(hex::decode(&tx_json.hex)?)),
Err(Error::GenesisTx) => {
let mut block = genesis_block(network);
(
Expand Down
3 changes: 1 addition & 2 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,14 +406,13 @@ pub fn tx_output(

#[cfg(test)]
mod test {
use bitcoin::hashes::hex::FromHex;

use crate::state::outpoints_and_sum;

#[test]
fn test_prevouts() {
const SOME_TX: &str = "0100000001a15d57094aa7a21a28cb20b59aab8fc7d1149a3bdbcddba9c622e4f5f6a99ece010000006c493046022100f93bb0e7d8db7bd46e40132d1f8242026e045f03a0efe71bbb8e3f475e970d790221009337cd7f1f929f00cc6ff01f03729b069a7c21b59b1736ddfee5db5946c5da8c0121033b9b137ee87d5a812d6f506efdd37f0affa7ffc310711c06c7f3e097c9447c52ffffffff0100e1f505000000001976a9140389035a9225b3839e2bbf32d826a1e222031fd888ac00000000";
let bytes = Vec::<u8>::from_hex(SOME_TX).unwrap();
let bytes = hex::decode(SOME_TX).unwrap();
let res = outpoints_and_sum(&bytes[..]).unwrap();
assert_eq!(res.sum, 100000000);
assert_eq!(res.prevouts.len(), 1);
Expand Down

0 comments on commit 1be663c

Please sign in to comment.