Skip to main content

smplx_sdk/
utils.rs

1use bitcoin_hashes::HashEngine;
2use sha2::{Digest, Sha256};
3
4use bip39::Mnemonic;
5
6use simplicityhl::elements::{AssetId, ContractHash, OutPoint, Script};
7use simplicityhl::simplicity::bitcoin;
8use simplicityhl::simplicity::bitcoin::secp256k1;
9use simplicityhl::simplicity::hashes::{Hash, sha256};
10
11pub fn random_mnemonic() -> String {
12    let mnemonic = Mnemonic::generate(12).expect("word count should be valid");
13
14    format!("{}", mnemonic)
15}
16
17pub fn tr_unspendable_key() -> secp256k1::XOnlyPublicKey {
18    secp256k1::XOnlyPublicKey::from_slice(&[
19        0x50, 0x92, 0x9b, 0x74, 0xc1, 0xa0, 0x49, 0x54, 0xb7, 0x8b, 0x4b, 0x60, 0x35, 0xe9, 0x7a, 0x5e, 0x07, 0x8a,
20        0x5a, 0x0f, 0x28, 0xec, 0x96, 0xd5, 0x47, 0xbf, 0xee, 0x9a, 0xce, 0x80, 0x3a, 0xc0,
21    ])
22    .expect("key should be valid")
23}
24
25pub fn asset_entropy(outpoint: &OutPoint, entropy: [u8; 32]) -> sha256::Midstate {
26    let contract_hash = ContractHash::from_byte_array(entropy);
27    AssetId::generate_asset_entropy(*outpoint, contract_hash)
28}
29
30pub fn tap_data_hash(data: &[u8]) -> sha256::Hash {
31    let tag = sha256::Hash::hash(b"TapData");
32
33    let mut eng = sha256::Hash::engine();
34    eng.input(tag.as_byte_array());
35    eng.input(tag.as_byte_array());
36    eng.input(data);
37
38    sha256::Hash::from_engine(eng)
39}
40
41pub fn hash_script(script: &Script) -> [u8; 32] {
42    let mut hasher = Sha256::new();
43
44    sha2::digest::Update::update(&mut hasher, script.as_bytes());
45    hasher.finalize().into()
46}
47
48pub fn sat2btc(sat: u64) -> f64 {
49    bitcoin::Amount::from_sat(sat).to_btc()
50}
51
52pub fn btc2sat(btc: u64) -> u64 {
53    bitcoin::Amount::from_int_btc(btc).to_sat()
54}
55
56#[cfg(test)]
57mod tests {
58    use super::*;
59
60    #[test]
61    fn generates_mnemonic() {
62        random_mnemonic();
63    }
64}