Skip to content

Commit

Permalink
Use MalFixTxid instad of Txid
Browse files Browse the repository at this point in the history
  • Loading branch information
rantan committed Jul 11, 2024
1 parent 7adeadb commit 3771e61
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 57 deletions.
10 changes: 5 additions & 5 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
pub use tapyrus::consensus::{deserialize, serialize};
pub use tapyrus::hex::FromHex;
use tapyrus::Weight;
use tapyrus::{MalFixTxid, Weight};
pub use tapyrus::{
transaction, Amount, BlockHash, OutPoint, ScriptBuf, Transaction, TxIn, TxOut, Txid, Witness,
};
Expand All @@ -19,7 +19,7 @@ pub struct PrevOut {

#[derive(Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct Vin {
pub txid: Txid,
pub txid: MalFixTxid,
pub vout: u32,
// None if coinbase
pub prevout: Option<PrevOut>,
Expand Down Expand Up @@ -47,14 +47,14 @@ pub struct TxStatus {
#[derive(Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct MerkleProof {
pub block_height: u32,
pub merkle: Vec<Txid>,
pub merkle: Vec<MalFixTxid>,
pub pos: usize,
}

#[derive(Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct OutputStatus {
pub spent: bool,
pub txid: Option<Txid>,
pub txid: Option<MalFixTxid>,
pub vin: Option<u64>,
pub status: Option<TxStatus>,
}
Expand All @@ -68,7 +68,7 @@ pub struct BlockStatus {

#[derive(Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct Tx {
pub txid: Txid,
pub txid: MalFixTxid,
pub version: i32,
pub locktime: u32,
pub vin: Vec<Vin>,
Expand Down
40 changes: 19 additions & 21 deletions src/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ use std::str::FromStr;
use tapyrus::consensus::{deserialize, serialize};
use tapyrus::hashes::{sha256, Hash};
use tapyrus::hex::{DisplayHex, FromHex};
use tapyrus::{
block::Header as BlockHeader, Block, BlockHash, MerkleBlock, Script, Transaction, Txid,
};
use tapyrus::{block::Header as BlockHeader, Block, BlockHash, MerkleBlock, Script, Transaction, MalFixTxid};

#[allow(unused_imports)]
use log::{debug, error, info, trace};
Expand Down Expand Up @@ -69,8 +67,8 @@ impl AsyncClient {
AsyncClient { url, client }
}

/// Get a [`Transaction`] option given its [`Txid`]
pub async fn get_tx(&self, txid: &Txid) -> Result<Option<Transaction>, Error> {
/// Get a [`Transaction`] option given its [`MalFixTxid`]
pub async fn get_tx(&self, txid: &MalFixTxid) -> Result<Option<Transaction>, Error> {
let resp = self
.client
.get(&format!("{}/tx/{}/raw", self.url, txid))
Expand All @@ -91,21 +89,21 @@ impl AsyncClient {
}
}

/// Get a [`Transaction`] given its [`Txid`].
pub async fn get_tx_no_opt(&self, txid: &Txid) -> Result<Transaction, Error> {
/// Get a [`Transaction`] given its [`MalFixTxid`].
pub async fn get_tx_no_opt(&self, txid: &MalFixTxid) -> Result<Transaction, Error> {
match self.get_tx(txid).await {
Ok(Some(tx)) => Ok(tx),
Ok(None) => Err(Error::TransactionNotFound(*txid)),
Err(e) => Err(e),
}
}

/// Get a [`Txid`] of a transaction given its index in a block with a given hash.
/// Get a [`MalFixTxid`] of a transaction given its index in a block with a given hash.
pub async fn get_txid_at_block_index(
&self,
block_hash: &BlockHash,
index: usize,
) -> Result<Option<Txid>, Error> {
) -> Result<Option<MalFixTxid>, Error> {
let resp = self
.client
.get(&format!("{}/block/{}/txid/{}", self.url, block_hash, index))
Expand All @@ -122,12 +120,12 @@ impl AsyncClient {
message: resp.text().await?,
})
} else {
Ok(Some(Txid::from_str(&resp.text().await?)?))
Ok(Some(MalFixTxid::from_str(&resp.text().await?)?))
}
}

/// Get the status of a [`Transaction`] given its [`Txid`].
pub async fn get_tx_status(&self, txid: &Txid) -> Result<TxStatus, Error> {
/// Get the status of a [`Transaction`] given its [`MalFixTxid`].
pub async fn get_tx_status(&self, txid: &MalFixTxid) -> Result<TxStatus, Error> {
let resp = self
.client
.get(&format!("{}/tx/{}/status", self.url, txid))
Expand All @@ -143,8 +141,8 @@ impl AsyncClient {
}
}

/// Get transaction info given it's [`Txid`].
pub async fn get_tx_info(&self, txid: &Txid) -> Result<Option<Tx>, Error> {
/// Get transaction info given it's [`MalFixTxid`].
pub async fn get_tx_info(&self, txid: &MalFixTxid) -> Result<Option<Tx>, Error> {
let resp = self
.client
.get(&format!("{}/tx/{}", self.url, txid))
Expand Down Expand Up @@ -222,8 +220,8 @@ impl AsyncClient {
}
}

/// Get a merkle inclusion proof for a [`Transaction`] with the given [`Txid`].
pub async fn get_merkle_proof(&self, tx_hash: &Txid) -> Result<Option<MerkleProof>, Error> {
/// Get a merkle inclusion proof for a [`Transaction`] with the given [`MalFixTxid`].
pub async fn get_merkle_proof(&self, tx_hash: &MalFixTxid) -> Result<Option<MerkleProof>, Error> {
let resp = self
.client
.get(&format!("{}/tx/{}/merkle-proof", self.url, tx_hash))
Expand All @@ -244,8 +242,8 @@ impl AsyncClient {
}
}

/// Get a [`MerkleBlock`] inclusion proof for a [`Transaction`] with the given [`Txid`].
pub async fn get_merkle_block(&self, tx_hash: &Txid) -> Result<Option<MerkleBlock>, Error> {
/// Get a [`MerkleBlock`] inclusion proof for a [`Transaction`] with the given [`MalFixTxid`].
pub async fn get_merkle_block(&self, tx_hash: &MalFixTxid) -> Result<Option<MerkleBlock>, Error> {
let resp = self
.client
.get(&format!("{}/tx/{}/merkleblock-proof", self.url, tx_hash))
Expand All @@ -267,10 +265,10 @@ impl AsyncClient {
}
}

/// Get the spending status of an output given a [`Txid`] and the output index.
/// Get the spending status of an output given a [`MalFixTxid`] and the output index.
pub async fn get_output_status(
&self,
txid: &Txid,
txid: &MalFixTxid,
index: u64,
) -> Result<Option<OutputStatus>, Error> {
let resp = self
Expand Down Expand Up @@ -376,7 +374,7 @@ impl AsyncClient {
pub async fn scripthash_txs(
&self,
script: &Script,
last_seen: Option<Txid>,
last_seen: Option<MalFixTxid>,
) -> Result<Vec<Tx>, Error> {
let script_hash = sha256::Hash::hash(script.as_bytes());
let url = match last_seen {
Expand Down
42 changes: 20 additions & 22 deletions src/blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ use minreq::{Proxy, Request};
use tapyrus::consensus::{deserialize, serialize, Decodable};
use tapyrus::hashes::{sha256, Hash};
use tapyrus::hex::{DisplayHex, FromHex};
use tapyrus::{
block::Header as BlockHeader, Block, BlockHash, MerkleBlock, Script, Transaction, Txid,
};
use tapyrus::{block::Header as BlockHeader, Block, BlockHash, MerkleBlock, Script, Transaction, MalFixTxid};

use crate::{BlockStatus, BlockSummary, Builder, Error, MerkleProof, OutputStatus, Tx, TxStatus};

Expand Down Expand Up @@ -91,7 +89,7 @@ impl BlockingClient {
}
}

fn get_opt_response_txid(&self, path: &str) -> Result<Option<Txid>, Error> {
fn get_opt_response_txid(&self, path: &str) -> Result<Option<MalFixTxid>, Error> {
match self.get_request(path)?.send() {
Ok(resp) if is_status_not_found(resp.status_code) => Ok(None),
Ok(resp) if !is_status_ok(resp.status_code) => {
Expand All @@ -100,7 +98,7 @@ impl BlockingClient {
Err(Error::HttpResponse { status, message })
}
Ok(resp) => Ok(Some(
Txid::from_str(resp.as_str().map_err(Error::Minreq)?).map_err(Error::HexToArray)?,
MalFixTxid::from_str(resp.as_str().map_err(Error::Minreq)?).map_err(Error::HexToArray)?,
)),
Err(e) => Err(Error::Minreq(e)),
}
Expand Down Expand Up @@ -185,36 +183,36 @@ impl BlockingClient {
}
}

/// Get a [`Transaction`] option given its [`Txid`]
pub fn get_tx(&self, txid: &Txid) -> Result<Option<Transaction>, Error> {
/// Get a [`Transaction`] option given its [`MalFixTxid`]
pub fn get_tx(&self, txid: &MalFixTxid) -> Result<Option<Transaction>, Error> {
self.get_opt_response(&format!("/tx/{}/raw", txid))
}

/// Get a [`Transaction`] given its [`Txid`].
pub fn get_tx_no_opt(&self, txid: &Txid) -> Result<Transaction, Error> {
/// Get a [`Transaction`] given its [`MalFixTxid`].
pub fn get_tx_no_opt(&self, txid: &MalFixTxid) -> Result<Transaction, Error> {
match self.get_tx(txid) {
Ok(Some(tx)) => Ok(tx),
Ok(None) => Err(Error::TransactionNotFound(*txid)),
Err(e) => Err(e),
}
}

/// Get a [`Txid`] of a transaction given its index in a block with a given hash.
/// Get a [`MalFixTxid`] of a transaction given its index in a block with a given hash.
pub fn get_txid_at_block_index(
&self,
block_hash: &BlockHash,
index: usize,
) -> Result<Option<Txid>, Error> {
) -> Result<Option<MalFixTxid>, Error> {
self.get_opt_response_txid(&format!("/block/{}/txid/{}", block_hash, index))
}

/// Get the status of a [`Transaction`] given its [`Txid`].
pub fn get_tx_status(&self, txid: &Txid) -> Result<TxStatus, Error> {
/// Get the status of a [`Transaction`] given its [`MalFixTxid`].
pub fn get_tx_status(&self, txid: &MalFixTxid) -> Result<TxStatus, Error> {
self.get_response_json(&format!("/tx/{}/status", txid))
}

/// Get transaction info given it's [`Txid`].
pub fn get_tx_info(&self, txid: &Txid) -> Result<Option<Tx>, Error> {
/// Get transaction info given it's [`MalFixTxid`].
pub fn get_tx_info(&self, txid: &MalFixTxid) -> Result<Option<Tx>, Error> {
self.get_opt_response_json(&format!("/tx/{}", txid))
}

Expand All @@ -233,20 +231,20 @@ impl BlockingClient {
self.get_opt_response(&format!("/block/{}/raw", block_hash))
}

/// Get a merkle inclusion proof for a [`Transaction`] with the given [`Txid`].
pub fn get_merkle_proof(&self, txid: &Txid) -> Result<Option<MerkleProof>, Error> {
/// Get a merkle inclusion proof for a [`Transaction`] with the given [`MalFixTxid`].
pub fn get_merkle_proof(&self, txid: &MalFixTxid) -> Result<Option<MerkleProof>, Error> {
self.get_opt_response_json(&format!("/tx/{}/merkle-proof", txid))
}

/// Get a [`MerkleBlock`] inclusion proof for a [`Transaction`] with the given [`Txid`].
pub fn get_merkle_block(&self, txid: &Txid) -> Result<Option<MerkleBlock>, Error> {
/// Get a [`MerkleBlock`] inclusion proof for a [`Transaction`] with the given [`MalFixTxid`].
pub fn get_merkle_block(&self, txid: &MalFixTxid) -> Result<Option<MerkleBlock>, Error> {
self.get_opt_response_hex(&format!("/tx/{}/merkleblock-proof", txid))
}

/// Get the spending status of an output given a [`Txid`] and the output index.
/// Get the spending status of an output given a [`MalFixTxid`] and the output index.
pub fn get_output_status(
&self,
txid: &Txid,
txid: &MalFixTxid,
index: u64,
) -> Result<Option<OutputStatus>, Error> {
self.get_opt_response_json(&format!("/tx/{}/outspend/{}", txid, index))
Expand Down Expand Up @@ -311,7 +309,7 @@ impl BlockingClient {
pub fn scripthash_txs(
&self,
script: &Script,
last_seen: Option<Txid>,
last_seen: Option<MalFixTxid>,
) -> Result<Vec<Tx>, Error> {
let script_hash = sha256::Hash::hash(script.as_bytes());
let path = match last_seen {
Expand Down
17 changes: 8 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ use std::collections::HashMap;
use std::fmt;
use std::num::TryFromIntError;

use tapyrus::consensus;
use tapyrus::{consensus, MalFixTxid};

pub mod api;

Expand Down Expand Up @@ -185,7 +185,7 @@ pub enum Error {
/// Invalid hex data returned (attempting to create a vector)
HexToBytes(tapyrus::hex::HexToBytesError),
/// Transaction not found
TransactionNotFound(Txid),
TransactionNotFound(MalFixTxid),
/// Header height not found
HeaderHeightNotFound(u32),
/// Header hash not found
Expand Down Expand Up @@ -235,8 +235,7 @@ mod test {
#[cfg(all(feature = "blocking", feature = "async"))]
use {
electrsd::{
electrum_client::ElectrumApi, tapyrusd::tapyruscore_rpc::json::AddressType,
tapyrusd::tapyruscore_rpc::RpcApi,
electrum_client::ElectrumApi, tapyrusd::tapyruscore_rpc::RpcApi,
},
std::time::Duration,
tapyrus::hashes::Hash,
Expand Down Expand Up @@ -493,7 +492,7 @@ mod test {
assert!(tx_status.confirmed);

// Bogus txid returns a TxStatus with false, None, None, None
let txid = Txid::hash(b"ayyyy lmao");
let txid = MalFixTxid::hash(b"ayyyy lmao");
let tx_status = blocking_client.get_tx_status(&txid).unwrap();
let tx_status_async = async_client.get_tx_status(&txid).await.unwrap();
assert_eq!(tx_status, tx_status_async);
Expand Down Expand Up @@ -554,7 +553,7 @@ mod test {
assert_eq!(tx_info.status.block_hash, tx_res.info.blockhash);
assert_eq!(tx_info.status.block_time, tx_res.info.blocktime);

let txid = Txid::hash(b"not exist");
let txid = MalFixTxid::hash(b"not exist");
assert_eq!(blocking_client.get_tx_info(&txid).unwrap(), None);
assert_eq!(async_client.get_tx_info(&txid).await.unwrap(), None);
}
Expand Down Expand Up @@ -744,7 +743,7 @@ mod test {
let merkle_block_async = async_client.get_merkle_block(&txid).await.unwrap().unwrap();
assert_eq!(merkle_block, merkle_block_async);

let mut matches = vec![txid];
let mut matches = vec![Txid::from_byte_array(txid.to_byte_array())];
let mut indexes = vec![];
let root = merkle_block
.txn
Expand Down Expand Up @@ -887,13 +886,13 @@ mod test {
.transaction()
.unwrap();
let script = &expected_tx.output[0].script_pubkey;
let scripthash_txs_txids: Vec<Txid> = blocking_client
let scripthash_txs_txids: Vec<MalFixTxid> = blocking_client
.scripthash_txs(script, None)
.unwrap()
.iter()
.map(|tx| tx.txid)
.collect();
let scripthash_txs_txids_async: Vec<Txid> = async_client
let scripthash_txs_txids_async: Vec<MalFixTxid> = async_client
.scripthash_txs(script, None)
.await
.unwrap()
Expand Down

0 comments on commit 3771e61

Please sign in to comment.