mirror of
https://github.com/romanz/electrs.git
synced 2024-11-19 01:43:29 +01:00
Support transaction broadcast RPC
This commit is contained in:
parent
31abb3e538
commit
4a8f7b5c0b
@ -2,7 +2,7 @@ use base64;
|
||||
use bitcoin::blockdata::block::{Block, BlockHeader};
|
||||
use bitcoin::blockdata::transaction::Transaction;
|
||||
use bitcoin::network::serialize::BitcoinHash;
|
||||
use bitcoin::network::serialize::deserialize;
|
||||
use bitcoin::network::serialize::{deserialize, serialize};
|
||||
use bitcoin::util::hash::Sha256dHash;
|
||||
use hex;
|
||||
use serde_json::{from_str, Value};
|
||||
@ -233,6 +233,15 @@ impl Daemon {
|
||||
Ok(MempoolEntry::new(fee, vsize))
|
||||
}
|
||||
|
||||
pub fn broadcast(&self, tx: &Transaction) -> Result<Sha256dHash> {
|
||||
let tx = hex::encode(serialize(tx).unwrap());
|
||||
let txid = self.request("sendrawtransaction", json!([tx]))?;
|
||||
Ok(
|
||||
Sha256dHash::from_hex(txid.as_str().chain_err(|| "non-string txid")?)
|
||||
.chain_err(|| "failed to parse txid")?,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn get_all_headers(&self) -> Result<HeaderMap> {
|
||||
let info: Value = self.request("getblockchaininfo", json!([]))?;
|
||||
let max_height = info.get("blocks")
|
||||
|
@ -87,6 +87,10 @@ impl<'a> Query<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn daemon(&self) -> &Daemon {
|
||||
self.daemon
|
||||
}
|
||||
|
||||
fn load_txns(&self, prefixes: Vec<HashPrefix>) -> Vec<TxnHeight> {
|
||||
let mut txns = Vec::new();
|
||||
for txid_prefix in prefixes {
|
||||
|
16
src/rpc.rs
16
src/rpc.rs
@ -1,5 +1,6 @@
|
||||
use bitcoin::blockdata::block::BlockHeader;
|
||||
use bitcoin::network::serialize::serialize;
|
||||
use bitcoin::blockdata::transaction::Transaction;
|
||||
use bitcoin::network::serialize::{deserialize, serialize};
|
||||
use bitcoin::util::hash::Sha256dHash;
|
||||
use crossbeam;
|
||||
use crypto::digest::Digest;
|
||||
@ -165,6 +166,18 @@ impl<'a> Handler<'a> {
|
||||
)))
|
||||
}
|
||||
|
||||
fn blockchain_transaction_broadcast(&self, params: &[Value]) -> Result<Value> {
|
||||
let tx = params.get(0).chain_err(|| "missing tx")?;
|
||||
let tx = tx.as_str().chain_err(|| "non-string tx")?;
|
||||
let tx = hex::decode(&tx).chain_err(|| "non-hex tx")?;
|
||||
let tx: Transaction = deserialize(&tx).chain_err(|| "failed to parse tx")?;
|
||||
let txid = self.query
|
||||
.daemon()
|
||||
.broadcast(&tx)
|
||||
.chain_err(|| "broadcast failed")?;
|
||||
Ok(json!(txid.be_hex_string()))
|
||||
}
|
||||
|
||||
fn blockchain_transaction_get(&self, params: &[Value]) -> Result<Value> {
|
||||
// TODO: handle 'verbose' param
|
||||
let tx_hash = hash_from_value(params.get(0)).chain_err(|| "bad tx_hash")?;
|
||||
@ -204,6 +217,7 @@ impl<'a> Handler<'a> {
|
||||
"blockchain.scripthash.subscribe" => self.blockchain_scripthash_subscribe(¶ms),
|
||||
"blockchain.scripthash.get_balance" => self.blockchain_scripthash_get_balance(¶ms),
|
||||
"blockchain.scripthash.get_history" => self.blockchain_scripthash_get_history(¶ms),
|
||||
"blockchain.transaction.broadcast" => self.blockchain_transaction_broadcast(¶ms),
|
||||
"blockchain.transaction.get" => self.blockchain_transaction_get(¶ms),
|
||||
"blockchain.transaction.get_merkle" => self.blockchain_transaction_get_merkle(¶ms),
|
||||
&_ => bail!("unknown method {} {:?}", method, params),
|
||||
|
Loading…
Reference in New Issue
Block a user