From 2f48be7d0bd090915b59a35f3a6bb39cdfb1c3f5 Mon Sep 17 00:00:00 2001 From: Roman Zeyde Date: Sun, 5 Aug 2018 09:47:01 +0300 Subject: [PATCH] Support "blockchain.address.get_balance" Electrum API --- src/rpc.rs | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/src/rpc.rs b/src/rpc.rs index ee41873..66d7398 100644 --- a/src/rpc.rs +++ b/src/rpc.rs @@ -51,6 +51,14 @@ fn unspent_from_status(status: &Status) -> Value { )) } +fn address_from_value(val: Option<&Value>) -> Result
{ + let addr = val + .chain_err(|| "no address")? + .as_str() + .chain_err(|| "non-string address")?; + Address::from_str(addr).chain_err(|| format!("invalid address {}", addr)) +} + fn jsonify_header(entry: &HeaderEntry) -> Value { let header = entry.header(); json!({ @@ -173,6 +181,15 @@ impl Connection { ) } + fn blockchain_address_get_balance(&self, params: &[Value]) -> Result { + let addr = address_from_value(params.get(0)).chain_err(|| "bad address")?; + let script_hash = compute_script_hash(&addr.script_pubkey().into_vec()); + let status = self.query.status(&script_hash[..])?; + Ok( + json!({ "confirmed": status.confirmed_balance(), "unconfirmed": status.mempool_balance() }), + ) + } + fn blockchain_scripthash_get_history(&self, params: &[Value]) -> Result { let script_hash = hash_from_value(params.get(0)).chain_err(|| "bad script_hash")?; let status = self.query.status(&script_hash[..])?; @@ -191,12 +208,7 @@ impl Connection { } fn blockchain_address_listunspent(&self, params: &[Value]) -> Result { - let address = params - .get(0) - .chain_err(|| "no address")? - .as_str() - .chain_err(|| "non-string address")?; - let addr = Address::from_str(address).chain_err(|| format!("invalid address {}", address))?; + let addr = address_from_value(params.get(0)).chain_err(|| "bad address")?; let script_hash = compute_script_hash(&addr.script_pubkey().into_vec()); Ok(unspent_from_status(&self.query.status(&script_hash[..])?)) } @@ -251,6 +263,7 @@ impl Connection { "blockchain.block.get_header" => self.blockchain_block_get_header(¶ms), "blockchain.estimatefee" => self.blockchain_estimatefee(¶ms), "blockchain.relayfee" => self.blockchain_relayfee(), + "blockchain.address.get_balance" => self.blockchain_address_get_balance(¶ms), "blockchain.address.listunspent" => self.blockchain_address_listunspent(¶ms), "blockchain.scripthash.subscribe" => self.blockchain_scripthash_subscribe(¶ms), "blockchain.scripthash.get_balance" => self.blockchain_scripthash_get_balance(¶ms),