Browse Source

Verify addresses are on the correct network

(cherry picked from commit a03724e6aaa4881577ccba8473b1431859148d2f)
liquid_e
Nadav Ivgi 6 years ago
committed by Lawrence Nahum
parent
commit
e3eed32606
  1. 2
      src/daemon.rs
  2. 12
      src/rest.rs
  3. 8
      src/util.rs

2
src/daemon.rs

@ -18,7 +18,7 @@ use util::HeaderList;
use errors::*;
#[derive(Debug, Copy, Clone)]
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum Network {
Bitcoin,
Testnet,

12
src/rest.rs

@ -20,7 +20,7 @@ use std::str::FromStr;
use std::thread;
use std::sync::Arc;
use daemon::Network;
use util::{FullHash, BlockHeaderMeta, TransactionStatus, script_to_address};
use util::{FullHash, BlockHeaderMeta, TransactionStatus, script_to_address, from_bitcoin_network};
use index::compute_script_hash;
const TX_LIMIT: usize = 50;
@ -336,7 +336,7 @@ fn handle_request(req: Request<Body>, query: &Arc<Query>, network: &Network) ->
json_response(txs)
},
(&Method::GET, Some(&"address"), Some(address), None, None) => {
let script_hash = address_to_scripthash(address)?;
let script_hash = address_to_scripthash(address, &network)?;
let status = query.status(&script_hash[..])?;
// @TODO create new AddressStatsValue struct?
json_response(json!({ "address": address, "tx_count": status.history().len(), }))
@ -346,7 +346,7 @@ fn handle_request(req: Request<Body>, query: &Arc<Query>, network: &Network) ->
.map_or(0u32, |el| el.parse().unwrap_or(0))
.max(0u32) as usize;
let script_hash = address_to_scripthash(address)?;
let script_hash = address_to_scripthash(address, &network)?;
let status = query.status(&script_hash[..])?;
let txs = status.history_txs();
@ -427,8 +427,12 @@ fn blocks(query: &Arc<Query>, start_height: Option<usize>)
json_response(values)
}
fn address_to_scripthash(addr: &str) -> Result<FullHash, StringError> {
fn address_to_scripthash(addr: &str, network: &Network) -> Result<FullHash, StringError> {
let addr = Address::from_str(addr)?;
let addr_network = from_bitcoin_network(&addr.network);
if addr_network != *network && !(addr_network == Network::Testnet && *network == Network::LiquidRegtest) {
return Err(StringError("Address on invalid network".to_string()))
}
Ok(compute_script_hash(&addr.script_pubkey().into_bytes()))
}

8
src/util.rs

@ -366,3 +366,11 @@ fn to_bech_network (network: &Network) -> B32Network {
}
}
// @FIXME
pub fn from_bitcoin_network (network: &BNetwork) -> Network {
match network {
BNetwork::Bitcoin => Network::Liquid,
BNetwork::Regtest => Network::LiquidRegtest,
BNetwork::Testnet => Network::Testnet,
}
}

Loading…
Cancel
Save