pub mod error;
#[cfg(feature = "encryption")]
pub mod encryption;
pub mod from;
mod label;
mod serde_util;
use bitcoin::{address::NetworkUnchecked, Address};
use serde::{Deserialize, Serialize};
use std::fmt::Display;
#[derive(Clone, Debug, Serialize, Deserialize, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct Labels(Vec<Label>);
#[derive(Clone, Debug, Serialize, Deserialize, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[serde(tag = "type")]
pub enum Label {
#[serde(rename = "tx")]
Transaction(TransactionRecord),
#[serde(rename = "addr")]
Address(AddressRecord),
#[serde(rename = "pubkey")]
PublicKey(PublicKeyRecord),
#[serde(rename = "input")]
Input(InputRecord),
#[serde(rename = "output")]
Output(OutputRecord),
#[serde(rename = "xpub")]
ExtendedPublicKey(ExtendedPublicKeyRecord),
}
#[derive(Clone, Debug, Serialize, Deserialize, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub enum LabelRef {
Txid(bitcoin::Txid),
Address(bitcoin::Address<NetworkUnchecked>),
PublicKey(String),
Input(bitcoin::OutPoint),
Output(bitcoin::OutPoint),
Xpub(String),
}
impl Display for LabelRef {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match &self {
LabelRef::Txid(txid) => write!(f, "{txid}"),
LabelRef::Address(address) => write!(f, "{}", address.clone().assume_checked()),
LabelRef::PublicKey(pk) => write!(f, "{}", pk),
LabelRef::Input(outpoint) => write!(f, "{}", outpoint),
LabelRef::Output(outpoint) => write!(f, "{}", outpoint),
LabelRef::Xpub(xpub) => write!(f, "{}", xpub),
}
}
}
#[derive(Clone, Debug, Serialize, Deserialize, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct TransactionRecord {
#[serde(rename = "ref")]
pub ref_: bitcoin::Txid,
#[serde(skip_serializing_if = "Option::is_none")]
pub label: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub origin: Option<String>,
}
#[derive(Clone, Debug, Serialize, Deserialize, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct AddressRecord {
#[serde(rename = "ref")]
pub ref_: Address<NetworkUnchecked>,
#[serde(skip_serializing_if = "Option::is_none")]
pub label: Option<String>,
}
#[derive(Clone, Debug, Serialize, Deserialize, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct PublicKeyRecord {
#[serde(rename = "ref")]
pub ref_: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub label: Option<String>,
}
#[derive(Clone, Debug, Serialize, Deserialize, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct InputRecord {
#[serde(rename = "ref")]
pub ref_: bitcoin::OutPoint,
pub label: Option<String>,
}
#[derive(Clone, Debug, Serialize, Deserialize, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct OutputRecord {
#[serde(rename = "ref")]
pub ref_: bitcoin::OutPoint,
pub label: Option<String>,
#[serde(
default = "default_true",
deserialize_with = "serde_util::deserialize_string_or_bool"
)]
pub spendable: bool,
}
#[derive(Clone, Debug, Serialize, Deserialize, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct ExtendedPublicKeyRecord {
#[serde(rename = "ref")]
pub ref_: String,
pub label: Option<String>,
}
impl OutputRecord {
pub fn spendable(&self) -> bool {
self.spendable
}
}
fn default_true() -> bool {
true
}