use internals::hex::DisplayHex as _;
use tidecoin::key::WPubkeyHash;
use tidecoin::{script, WitnessScriptBuf};
fn main() {
let pk = "b472a266d0bd89c13706a4132ccfb16f7c3b9fcb".parse::<WPubkeyHash>().unwrap();
let script_code = script::p2wpkh_script_code(pk);
let hex = script_code.to_hex_string_prefixed();
let decoded = WitnessScriptBuf::from_hex_prefixed(&hex).unwrap();
assert_eq!(decoded, script_code);
let script_code = script::p2wpkh_script_code(pk);
let hex = script_code.to_hex_string_no_length_prefix();
let decoded = WitnessScriptBuf::from_hex_no_length_prefix(&hex).unwrap();
assert_eq!(decoded, script_code);
println!("human-readable script: {script_code}");
let hex_lower_hex_trait = format!("{script_code:x}");
println!("hex created using `LowerHex`: {hex_lower_hex_trait}");
let bare_bytes = tidecoin::hex::decode_to_vec(&hex_lower_hex_trait).unwrap();
assert!(encoding::decode_from_slice::<WitnessScriptBuf>(&bare_bytes).is_err());
assert!(WitnessScriptBuf::from_hex_prefixed(&hex_lower_hex_trait).is_err());
assert_eq!(
WitnessScriptBuf::from_hex_no_length_prefix(&hex_lower_hex_trait).unwrap(),
script_code
);
let hex_inherent = script_code.to_hex_string_prefixed(); println!("hex created using inherent `to_hex_string_prefixed`: {hex_inherent}");
let decoded = WitnessScriptBuf::from_hex_prefixed(&hex_inherent).unwrap(); assert_eq!(decoded, script_code);
let decoded = encoding::decode_from_slice::<WitnessScriptBuf>(
&tidecoin::hex::decode_to_vec(&hex_inherent).unwrap(),
)
.unwrap();
assert_eq!(decoded, script_code);
let encoded = encoding::encode_to_vec(&script_code).to_lower_hex_string();
println!("hex created using consensus encoding: {encoded}");
let decoded: WitnessScriptBuf =
encoding::decode_from_slice(&tidecoin::hex::decode_to_vec(&encoded).unwrap()).unwrap();
assert_eq!(decoded, script_code);
let encoded = encoding::encode_to_vec(&script_code).to_lower_hex_string();
let decoded = WitnessScriptBuf::from_hex_prefixed(&encoded).unwrap();
assert_eq!(decoded, script_code);
let encoded = encoding::encode_to_vec(&script_code);
assert_eq!(&encoded[1..], script_code.as_bytes()); let decoded: WitnessScriptBuf = encoding::decode_from_slice(&encoded).unwrap();
assert_eq!(decoded, script_code);
let bytes = script_code.to_vec();
let got = WitnessScriptBuf::from_bytes(bytes);
assert_eq!(got, script_code);
}