use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Balance {
pub confirmed: u64,
pub unconfirmed: i64,
}
impl Balance {
#[inline]
pub fn total(&self) -> i64 {
self.confirmed as i64 + self.unconfirmed
}
#[inline]
pub fn has_balance(&self) -> bool {
self.confirmed > 0 || self.unconfirmed != 0
}
#[inline]
pub fn has_unconfirmed(&self) -> bool {
self.unconfirmed != 0
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Utxo {
#[serde(rename = "tx_hash")]
pub txid: String,
#[serde(rename = "tx_pos")]
pub vout: u32,
pub value: u64,
pub height: u64,
}
impl Utxo {
#[inline]
pub fn is_confirmed(&self) -> bool {
self.height > 0
}
pub fn outpoint(&self) -> String {
format!("{}:{}", self.txid, self.vout)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct TxHistory {
#[serde(rename = "tx_hash")]
pub txid: String,
pub height: i64,
#[serde(default)]
pub fee: Option<u64>,
}
impl TxHistory {
#[inline]
pub fn is_confirmed(&self) -> bool {
self.height > 0
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ServerVersion {
pub server_software: String,
pub protocol_version: String,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ServerFeatures {
#[serde(default)]
pub server_version: String,
#[serde(default)]
pub protocol_max: String,
#[serde(default)]
pub genesis_hash: String,
#[serde(default)]
pub hash_function: String,
}
#[derive(Debug, Clone)]
pub struct ClientConfig {
pub server: String,
pub port: u16,
pub use_tls: bool,
pub timeout: std::time::Duration,
pub retry_count: u32,
pub retry_delay: std::time::Duration,
pub skip_tls_verify: bool,
}
impl ClientConfig {
pub fn tcp(server: impl Into<String>) -> Self {
Self {
server: server.into(),
port: 50001,
use_tls: false,
timeout: std::time::Duration::from_secs(30),
retry_count: 3,
retry_delay: std::time::Duration::from_secs(1),
skip_tls_verify: false,
}
}
pub fn ssl(server: impl Into<String>) -> Self {
Self {
server: server.into(),
port: 50002,
use_tls: true,
timeout: std::time::Duration::from_secs(30),
retry_count: 3,
retry_delay: std::time::Duration::from_secs(1),
skip_tls_verify: false,
}
}
pub fn with_port(mut self, port: u16) -> Self {
self.port = port;
self
}
pub fn with_timeout(mut self, timeout: std::time::Duration) -> Self {
self.timeout = timeout;
self
}
pub fn with_retry(mut self, count: u32, delay: std::time::Duration) -> Self {
self.retry_count = count;
self.retry_delay = delay;
self
}
pub fn with_skip_tls_verify(mut self) -> Self {
self.skip_tls_verify = true;
self
}
pub fn address(&self) -> String {
format!("{}:{}", self.server, self.port)
}
}
impl Default for ClientConfig {
fn default() -> Self {
Self::ssl("electrum.blockstream.info")
}
}
pub const DEFAULT_SERVERS: &[(&str, u16, bool)] = &[
("electrum.blockstream.info", 50002, true),
("electrum.blockstream.info", 50001, false),
("electrum1.bluewallet.io", 443, true),
("electrum2.bluewallet.io", 443, true),
("bitcoin.aranguren.org", 50002, true),
("electrum.bitaroo.net", 50002, true),
];