1
2use bitcoin::{TapSighash, Witness, taproot};
3use bitcoin::key::Keypair;
4
5use ark::vtxo::{TapScriptClause, VtxoClause};
6use ark::vtxo::policy::signing::VtxoSigner;
7
8use crate::{SECP, Wallet};
9
10impl Wallet {
11 pub (crate) async fn clause_keypair(&self, clause: &VtxoClause) -> Option<Keypair> {
12 let clause_pubkey = clause.pubkey();
13 self.pubkey_keypair(&clause_pubkey).await.ok()
14 .flatten().map(|(_, keypair)| keypair)
15 }
16}
17
18#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
19#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
20impl VtxoSigner for Wallet {
21 async fn witness(
22 &self,
23 clause: &VtxoClause,
24 control_block: &taproot::ControlBlock,
25 sighash: TapSighash,
26 ) -> Option<Witness> {
27 let signature = match self.clause_keypair(clause).await {
28 Some(keypair) => {
29 SECP.sign_schnorr_with_aux_rand(&sighash.into(), &keypair, &rand::random())
30 },
31 None => return None,
32 };
33
34 match clause {
35 VtxoClause::DelayedSign(c) => Some(c.witness(&signature, control_block)),
36 VtxoClause::DelayedTimelockSign(c) => Some(c.witness(&signature, &control_block)),
37 VtxoClause::TimelockSign(c) => Some(c.witness(&signature, &control_block)),
38 VtxoClause::HashDelaySign(c) => {
39 let receive = self.db.fetch_lightning_receive_by_payment_hash(c.hash.into())
40 .await.ok().flatten();
41
42 receive.map(|r| c.witness(
43 &(signature, r.payment_preimage.to_byte_array()), &control_block,
44 ))
45 },
46 VtxoClause::HashSign(_) => None, }
48 }
49}