hal_simplicity/
lib.rs

1pub extern crate simplicity;
2
3pub mod actions;
4
5pub mod address;
6pub mod block;
7pub mod hal_simplicity;
8pub mod tx;
9
10pub mod confidential;
11
12pub use elements::bitcoin;
13pub use hal::HexBytes;
14
15use elements::AddressParams;
16use serde::{Deserialize, Serialize};
17
18/// Known Elements networks.
19#[derive(Clone, Copy, PartialEq, Eq, Debug, Deserialize, Serialize)]
20#[serde(rename_all = "lowercase")]
21pub enum Network {
22	ElementsRegtest,
23	Liquid,
24	LiquidTestnet,
25}
26
27impl Network {
28	pub fn from_params(params: &'static AddressParams) -> Option<Network> {
29		if *params == AddressParams::ELEMENTS {
30			Some(Network::ElementsRegtest)
31		} else if *params == AddressParams::LIQUID_TESTNET {
32			Some(Network::LiquidTestnet)
33		} else if *params == AddressParams::LIQUID {
34			Some(Network::Liquid)
35		} else {
36			None
37		}
38	}
39
40	pub fn address_params(self) -> &'static AddressParams {
41		match self {
42			Network::ElementsRegtest => &AddressParams::ELEMENTS,
43			Network::Liquid => &AddressParams::LIQUID,
44			Network::LiquidTestnet => &AddressParams::LIQUID_TESTNET,
45		}
46	}
47}
48
49/// Get JSON-able objects that describe the type.
50pub trait GetInfo<T: ::serde::Serialize> {
51	/// Get a description of this object given the network of interest.
52	fn get_info(&self, network: Network) -> T;
53}
54
55/// Parse a string which may be base64 or hex-encoded.
56///
57/// An even-length string with exclusively lowercase hex characters will be parsed as hex;
58/// failing that, it will be parsed as base64 and return an error accordingly.
59pub fn hex_or_base64(s: &str) -> Result<Vec<u8>, simplicity::base64::DecodeError> {
60	if s.len() % 2 == 0 && s.bytes().all(|b| b.is_ascii_hexdigit() && b.is_ascii_lowercase()) {
61		use simplicity::hex::FromHex as _;
62		Ok(Vec::from_hex(s).expect("charset checked above"))
63	} else {
64		use simplicity::base64::prelude::Engine as _;
65		simplicity::base64::prelude::BASE64_STANDARD.decode(s)
66	}
67}