sapio_ctv_emulator_trait/
emulator.rs1use bitcoin::hashes::sha256;
9use bitcoin::util::psbt::PartiallySignedTransaction;
10pub use sapio_base::Clause;
11use std::fmt;
12use std::sync::Arc;
13#[derive(Debug)]
15pub enum EmulatorError {
16 NetworkIssue(std::io::Error),
19 BIP32Error(bitcoin::util::bip32::Error),
21}
22impl fmt::Display for EmulatorError {
23 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
24 write!(f, "{:?}", self)
25 }
26}
27impl std::error::Error for EmulatorError {}
28
29impl From<std::io::Error> for EmulatorError {
30 fn from(e: std::io::Error) -> EmulatorError {
31 EmulatorError::NetworkIssue(e)
32 }
33}
34
35impl From<bitcoin::util::bip32::Error> for EmulatorError {
36 fn from(e: bitcoin::util::bip32::Error) -> EmulatorError {
37 EmulatorError::BIP32Error(e)
38 }
39}
40
41pub trait CTVEmulator: Sync + Send {
44 fn get_signer_for(&self, h: sha256::Hash) -> Result<Clause, EmulatorError>;
47 fn sign(
49 &self,
50 b: PartiallySignedTransaction,
51 ) -> Result<PartiallySignedTransaction, EmulatorError>;
52}
53
54pub type NullEmulator = Arc<dyn CTVEmulator>;
57
58pub struct CTVAvailable;
60impl CTVEmulator for CTVAvailable {
61 fn get_signer_for(&self, h: sha256::Hash) -> Result<Clause, EmulatorError> {
62 Ok(Clause::TxTemplate(h))
63 }
64 fn sign(
65 &self,
66 b: PartiallySignedTransaction,
67 ) -> Result<PartiallySignedTransaction, EmulatorError> {
68 Ok(b)
69 }
70}