Skip to content

Commit

Permalink
fix: remove custom types
Browse files Browse the repository at this point in the history
  • Loading branch information
nakul1010 committed Jul 25, 2023
1 parent abd476a commit b63a927
Show file tree
Hide file tree
Showing 37 changed files with 611 additions and 646 deletions.
67 changes: 51 additions & 16 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions bitcoin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ edition = "2018"
default = []
regtest-manual-mining = []
cli = ["clap"]
uses-bitcoind = []
uses-bitcoind = ["regtest-manual-mining"]
light-client = []

[dependencies]
thiserror = "1.0"
bitcoincore-rpc = { git = "https://github.com/rust-bitcoin/rust-bitcoincore-rpc", rev = "bde02d7fbf031df7d3a49946ec0e7f1abde34e58" }
bitcoincore-rpc = { git = "https://github.com/rust-bitcoin/rust-bitcoincore-rpc", rev = "7bd815f1e1ae721404719ee8e6867064b7c68494" }
hex = "0.4.2"
async-trait = "0.1.40"
tokio = { version = "1.0", features = ["full"] }
Expand Down
4 changes: 2 additions & 2 deletions bitcoin/src/electrs/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use bitcoincore_rpc::bitcoin::{
consensus::encode::Error as BitcoinEncodeError, hashes::hex::Error as HexError,
util::address::Error as BitcoinAddressError,
address::Error as BitcoinAddressError, consensus::encode::Error as BitcoinEncodeError,
hashes::hex::Error as HexError,
};
use reqwest::{Error as ReqwestError, StatusCode};
use serde_json::Error as SerdeJsonError;
Expand Down
15 changes: 8 additions & 7 deletions bitcoin/src/electrs/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
mod error;
mod types;

use bitcoincore_rpc::bitcoin::ScriptBuf;
pub use error::Error;
pub use types::*;

use crate::{
deserialize, opcodes, serialize, Address, Block, BlockHash, BlockHeader, Builder as ScriptBuilder, FromHex,
Network, OutPoint, Script, SignedAmount, ToHex, Transaction, Txid, H256,
Network, OutPoint, SignedAmount, Transaction, Txid, H256,
};
use futures::future::{join_all, try_join};
use reqwest::{Client, Url};
Expand Down Expand Up @@ -185,7 +186,7 @@ impl ElectrsClient {
.collect::<Result<Vec<_>, Error>>()
}

pub(crate) async fn get_script_pubkey(&self, outpoint: OutPoint) -> Result<Script, Error> {
pub(crate) async fn get_script_pubkey(&self, outpoint: OutPoint) -> Result<ScriptBuf, Error> {
let tx: TransactionValue = self
.get_and_decode(&format!("/tx/{txid}", txid = outpoint.txid))
.await?;
Expand Down Expand Up @@ -215,7 +216,7 @@ impl ElectrsClient {
let txid = self
.cli
.post(url)
.body(serialize(&tx).to_hex())
.body(hex::encode(serialize(&tx)))
.send()
.await?
.error_for_status()?
Expand All @@ -232,7 +233,7 @@ impl ElectrsClient {
let mut transactions: Vec<TransactionValue> = self
.get_and_decode(&format!(
"/scripthash/{scripthash}/txs/chain/{last_seen_txid}",
scripthash = script_hash.to_hex()
scripthash = hex::encode(&script_hash)
))
.await?;
let page_size = transactions.len();
Expand All @@ -257,7 +258,7 @@ impl ElectrsClient {
) -> Result<Option<Txid>, Error> {
let script = ScriptBuilder::new()
.push_opcode(opcodes::OP_RETURN)
.push_slice(data.as_bytes())
.push_slice(&data.as_fixed_bytes())
.into_script();

let script_hash = {
Expand Down Expand Up @@ -302,11 +303,11 @@ mod tests {
async fn test_electrs(url: &str, script_hex: &str, expected_txid: &str) {
let script_bytes = Vec::from_hex(script_hex).unwrap();
let script_hash = Sha256Hash::hash(&script_bytes);
let expected_txid = Txid::from_hex(expected_txid).unwrap();
let expected_txid = Txid::from_str(expected_txid).unwrap();

let electrs_client = ElectrsClient::new(Some(url.to_owned()), Network::Bitcoin).unwrap();
let txs = electrs_client
.get_txs_by_scripthash(script_hash.to_vec())
.get_txs_by_scripthash(script_hash.to_byte_array().to_vec())
.await
.unwrap();
assert!(txs.iter().any(|tx| tx.txid.eq(&expected_txid)));
Expand Down
7 changes: 4 additions & 3 deletions bitcoin/src/electrs/types.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{BlockHash, Script, Txid};
use crate::{BlockHash, Txid};
use bitcoincore_rpc::bitcoin::ScriptBuf;
use serde::Deserialize;

// https://github.com/Blockstream/electrs/blob/adedee15f1fe460398a7045b292604df2161adc0/src/util/transaction.rs#L17-L26
Expand All @@ -19,7 +20,7 @@ pub struct TxInValue {
pub txid: Txid,
pub vout: u32,
pub prevout: Option<TxOutValue>,
pub scriptsig: Script,
pub scriptsig: ScriptBuf,
pub scriptsig_asm: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub witness: Option<Vec<String>>,
Expand All @@ -34,7 +35,7 @@ pub struct TxInValue {
// https://github.com/Blockstream/electrs/blob/adedee15f1fe460398a7045b292604df2161adc0/src/rest.rs#L239-L270
#[derive(Deserialize)]
pub struct TxOutValue {
pub scriptpubkey: Script,
pub scriptpubkey: ScriptBuf,
pub scriptpubkey_asm: String,
pub scriptpubkey_type: String,
#[serde(skip_serializing_if = "Option::is_none")]
Expand Down
5 changes: 4 additions & 1 deletion bitcoin/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use crate::{BitcoinError, BitcoinLightError, ElectrsError};
use bitcoincore_rpc::{
bitcoin::{
address::Error as AddressError,
consensus::encode::Error as BitcoinEncodeError,
hashes::{hex::Error as HashHexError, Error as HashesError},
key::Error as KeyError,
secp256k1::Error as Secp256k1Error,
util::{address::Error as AddressError, key::Error as KeyError},
},
jsonrpc::{error::RpcError, Error as JsonRpcError},
};
Expand Down Expand Up @@ -74,6 +75,8 @@ pub enum Error {
MissingBitcoinFeeInfo,
#[error("FailedToConstructWalletName")]
FailedToConstructWalletName,
#[error("AddressError: {0}")]
AddressError(#[from] AddressError),
}

impl Error {
Expand Down
15 changes: 8 additions & 7 deletions bitcoin/src/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,8 @@ async fn get_best_block_info(rpc: &DynBitcoinCoreApi) -> Result<(u32, BlockHash)
mod tests {
use super::*;
use crate::*;
use bitcoincore_rpc::bitcoin::PackedLockTime;
pub use bitcoincore_rpc::bitcoin::{Address, Amount, Network, PublicKey, TxMerkleNode};
use bitcoincore_rpc::bitcoin::{absolute::Height, block::Version, locktime::absolute::LockTime, CompactTarget};
pub use bitcoincore_rpc::bitcoin::{Address, Amount, Network, PublicKey};
use sp_core::H256;

mockall::mock! {
Expand All @@ -207,15 +207,16 @@ mod tests {
async fn wait_for_block(&self, height: u32, num_confirmations: u32) -> Result<Block, Error>;
fn get_balance(&self, min_confirmations: Option<u32>) -> Result<Amount, Error>;
fn list_transactions(&self, max_count: Option<usize>) -> Result<Vec<json::ListTransactionResult>, Error>;
fn list_addresses(&self) -> Result<Vec<Address>, Error>;
async fn get_block_count(&self) -> Result<u64, Error>;
async fn get_raw_tx(&self, txid: &Txid, block_hash: &BlockHash) -> Result<Vec<u8>, Error>;
async fn get_transaction(&self, txid: &Txid, block_hash: Option<BlockHash>) -> Result<Transaction, Error>;
async fn get_proof(&self, txid: Txid, block_hash: &BlockHash) -> Result<Vec<u8>, Error>;
async fn get_block_hash(&self, height: u32) -> Result<BlockHash, Error>;
async fn get_new_address(&self) -> Result<Address, Error>;
async fn get_new_public_key(&self) -> Result<PublicKey, Error>;
fn dump_derivation_key(&self, public_key: &PublicKey) -> Result<PrivateKey, Error>;
fn import_derivation_key(&self, private_key: &PrivateKey) -> Result<(), Error>;
fn dump_private_key(&self, address: &Address) -> Result<PrivateKey, Error>;
fn import_private_key(&self, private_key: &PrivateKey, is_derivation_key: bool) -> Result<(), Error>;
async fn add_new_deposit_key(
&self,
public_key: PublicKey,
Expand Down Expand Up @@ -281,7 +282,7 @@ mod tests {
fn dummy_tx(value: i32) -> Transaction {
Transaction {
version: value,
lock_time: PackedLockTime(1),
lock_time: LockTime::Blocks(Height::ZERO),
input: vec![],
output: vec![],
}
Expand All @@ -291,8 +292,8 @@ mod tests {
Block {
txdata: transactions.into_iter().map(dummy_tx).collect(),
header: BlockHeader {
version: 4,
bits: 0,
version: Version::from_consensus(4),
bits: CompactTarget::from_consensus(0),
nonce: 0,
time: 0,
prev_blockhash: next_hash,
Expand Down
Loading

0 comments on commit b63a927

Please sign in to comment.