Skip to content

Commit

Permalink
Remove dependency on the hex crate
Browse files Browse the repository at this point in the history
The same functionality is provided by bitcoin_hashes.

Refs #61
  • Loading branch information
shesek committed Nov 9, 2020
1 parent 9db2c34 commit b47bb9b
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 18 deletions.
7 changes: 0 additions & 7 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ bitcoin_hashes = { version = "0.9.0", features = [ "serde" ] }
bitcoincore-rpc = "0.12.0"
miniscript = { version = "3.0.0", features = [ "serde" ] }
chrono = { version = "0.4.19", default-features = false }
hex = "0.4.2"
serde = { version = "1.0.117", features = [ "derive" ] }
serde_json = "1.0.59"
lazy_static = "1.4.0"
Expand Down
3 changes: 2 additions & 1 deletion src/electrum/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::sync::{Arc, Mutex};
use std::thread;

use bitcoin::Txid;
use bitcoin_hashes::hex::ToHex;
use serde_json::{from_str, from_value, Value};

use crate::electrum::{electrum_height, QueryExt};
Expand Down Expand Up @@ -255,7 +256,7 @@ impl Connection {
json!(self.query.get_tx_json(&txid)?)
} else {
let raw = self.query.get_tx_raw(&txid)?;
json!(hex::encode(&raw))
json!(raw.to_hex())
})
}

Expand Down
6 changes: 3 additions & 3 deletions src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use warp::sse::ServerSentEvent;
use warp::{self, reply, Filter, Reply};

use bitcoin::{Address, BlockHash, OutPoint, Txid};
use bitcoin_hashes::hex::FromHex;
use bitcoin_hashes::hex::{FromHex, ToHex};

use crate::error::{fmt_error_chain, BwtError, Error, OptionExt};
use crate::types::{BlockId, ScriptHash};
Expand Down Expand Up @@ -225,7 +225,7 @@ fn setup(
.and(query.clone())
.map(|txid: Txid, query: Arc<Query>| {
let tx_raw = query.get_tx_raw(&txid)?;
Ok(hex::encode(tx_raw))
Ok(tx_raw.to_hex())
})
.map(handle_error);

Expand All @@ -236,7 +236,7 @@ fn setup(
.and(query.clone())
.map(|txid: Txid, query: Arc<Query>| {
let proof = query.get_tx_proof(&txid)?;
Ok(hex::encode(proof))
Ok(proof.to_hex())
})
.map(handle_error);

Expand Down
11 changes: 5 additions & 6 deletions src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use serde::Serialize;
use serde_json::Value;

use bitcoin::{BlockHash, BlockHeader, Network, OutPoint, Transaction, Txid};
use bitcoin_hashes::hex::FromHex;
use bitcoincore_rpc::{json as rpcjson, Client as RpcClient, RpcApi};

use crate::error::{BwtError, Context, OptionExt, Result};
Expand Down Expand Up @@ -184,7 +185,7 @@ impl Query {
// and is incompatible with pruning, but works for non-wallet transactions too.
else {
let tx_hex = self.rpc.get_raw_transaction_hex(txid, None)?;
Ok(hex::decode(tx_hex)?)
Ok(Vec::from_hex(&tx_hex)?)
}
}

Expand All @@ -204,14 +205,12 @@ impl Query {

pub fn broadcast(&self, tx_hex: &str) -> Result<Txid> {
if let Some(broadcast_cmd) = &self.config.broadcast_cmd {
// need to deserialize to ensure validity (preventing code injection) and to determine the txid
let tx: Transaction = bitcoin::consensus::deserialize(&hex::decode(tx_hex)?)?;
// deserialize the tx to ensure validity (preventing potential code injection) and to determine the txid
let tx: Transaction = bitcoin::consensus::deserialize(&Vec::from_hex(tx_hex)?)?;
let cmd = broadcast_cmd.replacen("{tx_hex}", tx_hex, 1);
debug!("broadcasting tx with cmd {}", broadcast_cmd);
let status = Command::new("sh").arg("-c").arg(cmd).status()?;
if !status.success() {
bail!(BwtError::BroadcastCmdFailed(status))
}
ensure!(status.success(), BwtError::BroadcastCmdFailed(status));
Ok(tx.txid())
} else {
Ok(self.rpc.send_raw_transaction(tx_hex)?)
Expand Down

0 comments on commit b47bb9b

Please sign in to comment.