bsv/script/templates/mod.rs
1//! Script template system: traits and implementations for standard Bitcoin scripts.
2//!
3//! Provides ScriptTemplateLock and ScriptTemplateUnlock traits, plus implementations
4//! for P2PKH, PushDrop, and RPuzzle templates. Translates the TS SDK ScriptTemplate.ts
5//! and related template classes.
6
7pub mod p2pkh;
8pub mod push_drop;
9pub mod r_puzzle;
10
11pub use p2pkh::P2PKH;
12pub use push_drop::PushDrop;
13pub use r_puzzle::RPuzzle;
14
15use crate::script::error::ScriptError;
16use crate::script::{LockingScript, UnlockingScript};
17
18/// Trait for creating locking scripts (analogous to TS SDK ScriptTemplate).
19///
20/// Implementors produce a LockingScript from configuration stored in the struct.
21pub trait ScriptTemplateLock {
22 /// Create a locking script from the template's parameters.
23 fn lock(&self) -> Result<LockingScript, ScriptError>;
24}
25
26/// Trait for creating unlocking scripts (analogous to TS SDK ScriptTemplateUnlock).
27///
28/// Implementors produce an UnlockingScript and can estimate its byte length
29/// for fee calculation purposes.
30pub trait ScriptTemplateUnlock {
31 /// Sign a transaction input and produce an unlocking script.
32 ///
33 /// The `preimage` is the sighash preimage bytes that the caller computes
34 /// from the transaction context. The template signs this directly.
35 fn sign(&self, preimage: &[u8]) -> Result<UnlockingScript, ScriptError>;
36
37 /// Estimate the byte length of the unlocking script (for fee calculation).
38 fn estimate_length(&self) -> Result<usize, ScriptError>;
39}