1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
use std::sync::Arc;

use crate::model::{Signer, SignerError};
use bip39::Mnemonic;
use boltz_client::PublicKey;
use lwk_common::Signer as LwkSigner;
use lwk_wollet::bitcoin::bip32::Xpriv;
use lwk_wollet::bitcoin::Network;
use lwk_wollet::elements_miniscript;
use lwk_wollet::elements_miniscript::{
    bitcoin::{self, bip32::DerivationPath},
    elements::{
        bitcoin::bip32::{self, Fingerprint, Xpub},
        hashes::Hash,
        pset::PartiallySignedTransaction,
        secp256k1_zkp::{All, Secp256k1},
        sighash::SighashCache,
    },
    elementssig_to_rawsig,
    psbt::PsbtExt,
    slip77::MasterBlindingKey,
};
use lwk_wollet::hashes::{sha256, HashEngine, Hmac, HmacEngine};
use lwk_wollet::secp256k1::ecdsa::Signature;
use lwk_wollet::secp256k1::Message;

#[derive(thiserror::Error, Debug)]
pub enum SignError {
    #[error(transparent)]
    Pset(#[from] elements_miniscript::elements::pset::Error),

    #[error(transparent)]
    ElementsEncode(#[from] elements_miniscript::elements::encode::Error),

    #[error(transparent)]
    Sighash(#[from] elements_miniscript::psbt::SighashError),

    #[error(transparent)]
    PsetParse(#[from] elements_miniscript::elements::pset::ParseError),

    #[error(transparent)]
    Bip32(#[from] bip32::Error),

    #[error(transparent)]
    Generic(#[from] anyhow::Error),

    #[error(transparent)]
    UserSignerError(#[from] crate::model::SignerError),
}

/// Possible errors when creating a new software signer [`SwSigner`]
#[derive(thiserror::Error, Debug)]
pub enum NewError {
    #[error(transparent)]
    Bip39(#[from] bip39::Error),

    #[error(transparent)]
    Bip32(#[from] bip32::Error),
}

/// A software signer
pub struct SdkLwkSigner {
    sdk_signer: Arc<Box<dyn Signer>>,
}

impl SdkLwkSigner {
    /// Creates a new software signer from the given mnemonic.
    ///
    /// Takes also a flag if the network is mainnet so that generated extended keys are in the
    /// correct form xpub/tpub (there is no need to discriminate between regtest and testnet)
    pub fn new(sdk_signer: Arc<Box<dyn Signer>>) -> Result<Self, NewError> {
        Ok(Self { sdk_signer })
    }

    pub fn xpub(&self) -> Result<Xpub, SignError> {
        let xpub = self.sdk_signer.xpub()?;
        Ok(Xpub::decode(&xpub)?)
    }

    pub fn fingerprint(&self) -> Result<Fingerprint, SignError> {
        let f: Fingerprint = self.xpub()?.identifier()[0..4]
            .try_into()
            .map_err(|_| SignError::Generic(anyhow::anyhow!("Wrong fingerprint length")))?;
        Ok(f)
    }

    pub fn sign_ecdsa_recoverable(&self, msg: &Message) -> Result<Vec<u8>, SignError> {
        let sig_bytes = self
            .sdk_signer
            .sign_ecdsa_recoverable(msg.as_ref().to_vec())?;
        Ok(sig_bytes)
    }
}

impl LwkSigner for SdkLwkSigner {
    type Error = SignError;

    fn sign(&self, pset: &mut PartiallySignedTransaction) -> Result<u32, Self::Error> {
        let tx = pset.extract_tx()?;
        let mut sighash_cache = SighashCache::new(&tx);
        let mut signature_added = 0;

        // genesis hash is not used at all for sighash calculation
        let genesis_hash = elements_miniscript::elements::BlockHash::all_zeros();
        let mut messages = vec![];
        for i in 0..pset.inputs().len() {
            // computing all the messages to sign, it is not necessary if we are not going to sign
            // some input, but since the pset is borrowed, we can't do this action in a inputs_mut() for loop
            let msg = pset
                .sighash_msg(i, &mut sighash_cache, None, genesis_hash)?
                .to_secp_msg();
            messages.push(msg);
        }

        // Fixme: Take a parameter
        let hash_ty = elements_miniscript::elements::EcdsaSighashType::All;

        let signer_fingerprint = self.fingerprint()?;
        for (input, msg) in pset.inputs_mut().iter_mut().zip(messages) {
            for (want_public_key, (fingerprint, derivation_path)) in input.bip32_derivation.iter() {
                if &signer_fingerprint == fingerprint {
                    let xpub = self.derive_xpub(derivation_path)?;
                    let public_key: PublicKey = xpub.public_key.into();
                    if want_public_key == &public_key {
                        // fixme: for taproot use schnorr
                        let sig_bytes = self
                            .sdk_signer
                            .sign_ecdsa(msg.as_ref().to_vec(), derivation_path.to_string())?;
                        let sig = Signature::from_der(&sig_bytes).map_err(|_| {
                            SignError::Generic(anyhow::anyhow!("Invalid esda signature"))
                        })?;
                        let sig = elementssig_to_rawsig(&(sig, hash_ty));

                        let inserted = input.partial_sigs.insert(public_key, sig);
                        if inserted.is_none() {
                            signature_added += 1;
                        }
                    }
                }
            }
        }

        Ok(signature_added)
    }

    fn slip77_master_blinding_key(&self) -> Result<MasterBlindingKey, Self::Error> {
        let bytes: [u8; 32] = self
            .sdk_signer
            .slip77_master_blinding_key()?
            .try_into()
            .map_err(|_| {
                SignError::Generic(anyhow::anyhow!("Wrong slip77 master blinding key length"))
            })?;
        Ok(bytes.into())
    }

    fn derive_xpub(&self, path: &DerivationPath) -> Result<Xpub, Self::Error> {
        let pubkey_bytes = self.sdk_signer.derive_xpub(path.to_string())?;
        let xpub = Xpub::decode(pubkey_bytes.as_slice())?;
        Ok(xpub)
    }
}

pub struct SdkSigner {
    xprv: Xpriv,
    secp: Secp256k1<All>, // could be sign only, but it is likely the caller already has the All context.
    mnemonic: Mnemonic,
    network: Network,
}

impl SdkSigner {
    pub fn new(mnemonic: &str, is_mainnet: bool) -> Result<Self, NewError> {
        let secp = Secp256k1::new();
        let mnemonic: Mnemonic = mnemonic.parse()?;
        let seed = mnemonic.to_seed("");

        let network = if is_mainnet {
            bitcoin::Network::Bitcoin
        } else {
            bitcoin::Network::Testnet
        };

        let xprv = Xpriv::new_master(network, &seed)?;

        Ok(Self {
            xprv,
            secp,
            mnemonic,
            network,
        })
    }

    fn seed(&self) -> [u8; 64] {
        self.mnemonic.to_seed("")
    }
}

impl Signer for SdkSigner {
    fn xpub(&self) -> Result<Vec<u8>, SignerError> {
        Ok(Xpub::from_priv(&self.secp, &self.xprv).encode().to_vec())
    }

    fn derive_xpub(&self, derivation_path: String) -> Result<Vec<u8>, SignerError> {
        let der: DerivationPath = derivation_path.parse()?;
        let derived = self.xprv.derive_priv(&self.secp, &der)?;
        Ok(Xpub::from_priv(&self.secp, &derived).encode().to_vec())
    }

    fn sign_ecdsa(&self, msg: Vec<u8>, derivation_path: String) -> Result<Vec<u8>, SignerError> {
        let der: DerivationPath = derivation_path.parse()?;
        let ext_derived = self.xprv.derive_priv(&self.secp, &der)?;
        let sig = self.secp.sign_ecdsa_low_r(
            &Message::from_digest(
                msg.try_into()
                    .map_err(|_| anyhow::anyhow!("failed to sign"))?,
            ),
            &ext_derived.private_key,
        );
        Ok(sig.serialize_der().to_vec())
    }

    fn slip77_master_blinding_key(&self) -> Result<Vec<u8>, SignerError> {
        let seed = self.seed();
        let master_blinding_key = MasterBlindingKey::from_seed(&seed[..]);
        Ok(master_blinding_key.as_bytes().to_vec())
    }

    fn sign_ecdsa_recoverable(&self, msg: Vec<u8>) -> Result<Vec<u8>, SignerError> {
        let seed = self.seed();
        let secp = Secp256k1::new();
        let keypair = Xpriv::new_master(self.network, &seed)
            .map_err(|e| anyhow::anyhow!("Could not get signer keypair: {e}"))?
            .to_keypair(&secp);
        let s = msg.as_slice();

        let msg: Message = Message::from_digest_slice(s)
            .map_err(|e| SignerError::Generic { err: e.to_string() })?;
        // Get message signature and encode to zbase32
        let recoverable_sig = secp.sign_ecdsa_recoverable(&msg, &keypair.secret_key());
        let (recovery_id, sig) = recoverable_sig.serialize_compact();
        let mut complete_signature = vec![31 + recovery_id.to_i32() as u8];
        complete_signature.extend_from_slice(&sig);
        Ok(complete_signature)
    }

    fn hmac_sha256(&self, msg: Vec<u8>, derivation_path: String) -> Result<Vec<u8>, SignerError> {
        let der: DerivationPath = derivation_path.parse()?;
        let priv_key = self.xprv.derive_priv(&self.secp, &der)?;
        let mut engine = HmacEngine::<sha256::Hash>::new(priv_key.to_priv().to_bytes().as_slice());

        engine.input(msg.as_slice());
        Ok(Hmac::<sha256::Hash>::from_engine(engine)
            .as_byte_array()
            .to_vec())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use bip32::KeySource;
    use bitcoin::PublicKey;
    use elements::{
        pset::{Input, Output, PartiallySignedTransaction},
        AssetId, TxOut, Txid,
    };
    use lwk_common::{singlesig_desc, Singlesig};
    use lwk_signer::SwSigner;
    use lwk_wollet::{
        elements::{self, Script},
        ElementsNetwork, NoPersist, Wollet, WolletDescriptor,
    };
    use std::collections::BTreeMap;

    fn get_descriptor<S: LwkSigner>(
        signer: &S,
        is_mainnet: bool,
    ) -> Result<WolletDescriptor, anyhow::Error> {
        let descriptor_str = singlesig_desc(
            signer,
            Singlesig::Wpkh,
            lwk_common::DescriptorBlindingKey::Slip77,
            is_mainnet,
        )
        .map_err(|e| anyhow::anyhow!("Invalid descriptor: {e}"))?;
        Ok(descriptor_str.parse()?)
    }

    fn create_signers(mnemonic: &str) -> (SwSigner, SdkLwkSigner) {
        let sw_signer = SwSigner::new(mnemonic, false).unwrap();
        let sdk_signer: Box<dyn Signer> = Box::new(SdkSigner::new(mnemonic, false).unwrap());
        let sdk_signer = SdkLwkSigner::new(Arc::new(sdk_signer)).unwrap();
        (sw_signer, sdk_signer)
    }

    fn create_pset<S: LwkSigner>(signer: &S) -> PartiallySignedTransaction {
        // Create a PartiallySignedTransaction
        let mut pset = PartiallySignedTransaction::new_v2();

        // Add a dummy input
        let prev_txid = Txid::from_slice(&[0; 32]).unwrap();
        let prev_vout = 0;

        let derivation_path: DerivationPath = "m/84'/0'/0'/0/0".parse().unwrap();
        let xpub = signer.derive_xpub(&derivation_path).unwrap();
        let mut bip32_derivation_map: BTreeMap<PublicKey, KeySource> = BTreeMap::new();
        bip32_derivation_map.insert(
            xpub.public_key.into(),
            (signer.fingerprint().unwrap(), derivation_path),
        );
        let input = Input {
            non_witness_utxo: None,
            witness_utxo: Some(TxOut::new_fee(
                100_000_000,
                AssetId::from_slice(&[1; 32]).unwrap(),
            )),
            previous_txid: prev_txid,
            previous_output_index: prev_vout,
            bip32_derivation: bip32_derivation_map,
            ..Default::default()
        };

        pset.add_input(input);

        // Add a dummy output using new_explicit
        let output_script = Script::new();
        let output_amount = 99_000_000;
        let output_asset = AssetId::from_slice(&[1; 32]).unwrap();
        let output = Output::new_explicit(
            output_script,
            output_amount,
            output_asset,
            None, // No blinding key for this example
        );
        pset.add_output(output);
        pset
    }

    #[test]
    fn test_sign() {
        let mnemonic = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about";
        let (sw_signer, sdk_signer) = create_signers(mnemonic);

        // Clone the PSET for each signer
        let mut pset_sw = create_pset(&sw_signer);
        let mut pset_sdk = create_pset(&sdk_signer);

        // Sign with SwSigner
        let sw_sig_count = sw_signer.sign(&mut pset_sw).unwrap();
        assert_eq!(sw_sig_count, 1);

        // Sign with SdkLwkSigner
        let sdk_sig_count = sdk_signer.sign(&mut pset_sdk).unwrap();
        assert_eq!(sdk_sig_count, 1);

        // Compare the sign results
        assert_eq!(pset_sw, pset_sdk);

        // Extract and compare the final transactions
        let tx_sw = pset_sw.extract_tx().unwrap();
        let tx_sdk = pset_sdk.extract_tx().unwrap();
        assert_eq!(tx_sw, tx_sdk);
    }

    #[test]
    fn test_slip77_master_blinding_key() {
        let mnemonic = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about";
        let (sw_signer, sdk_signer) = create_signers(mnemonic);

        let sw_key = sw_signer.slip77_master_blinding_key().unwrap();
        let sdk_key = sdk_signer.slip77_master_blinding_key().unwrap();

        assert_eq!(
            sw_key, sdk_key,
            "SLIP77 master blinding keys should be identical"
        );
    }

    #[test]
    fn test_derive_xpub() {
        let mnemonic = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about";
        let (sw_signer, sdk_signer) = create_signers(mnemonic);

        let path = "m/84'/0'/0'/0/0".parse().unwrap();
        let sw_xpub = sw_signer.derive_xpub(&path).unwrap();
        let sdk_xpub = sdk_signer.derive_xpub(&path).unwrap();

        assert_eq!(sw_xpub, sdk_xpub, "Derived xpubs should be identical");
    }

    #[test]
    fn test_identifier() {
        let mnemonic = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about";
        let (sw_signer, sdk_signer) = create_signers(mnemonic);

        let sw_identifier = sw_signer.xpub().identifier();
        let sdk_identifier = sdk_signer.xpub().unwrap().identifier();

        assert_eq!(
            sw_identifier, sdk_identifier,
            "Identifiers should be identical"
        );
    }

    #[test]
    fn test_fingerprint() {
        let mnemonic = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about";
        let (sw_signer, sdk_signer) = create_signers(mnemonic);

        let sw_fingerprint = sw_signer.fingerprint();
        let sdk_fingerprint = sdk_signer.fingerprint().unwrap();
        let manual_finger_print = sdk_signer.xpub().unwrap().identifier()[0..4]
            .try_into()
            .unwrap();
        assert_eq!(
            sw_fingerprint, sdk_fingerprint,
            "Fingerprints should be identical"
        );

        assert_eq!(
            sw_fingerprint, manual_finger_print,
            "Fingerprints should be identical"
        );
    }

    #[test]
    fn test_sdk_signer_vs_sw_signer() {
        // Use a test mnemonic (don't use this in production!)
        let mnemonic = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about";
        let network = ElementsNetwork::LiquidTestnet;

        // 1. Create a wallet using SwSigner
        let sw_signer = SwSigner::new(mnemonic, false).unwrap();
        let sw_wallet = Wollet::new(
            network,
            NoPersist::new(),
            get_descriptor(&sw_signer, false).unwrap(),
        )
        .unwrap();

        // 2. Create a wallet using SdkLwkSigner
        let sdk_signer: Box<dyn Signer> = Box::new(SdkSigner::new(mnemonic, false).unwrap());
        let sdk_signer = SdkLwkSigner::new(Arc::new(sdk_signer)).unwrap();
        let sdk_wallet = Wollet::new(
            network,
            NoPersist::new(),
            get_descriptor(&sdk_signer, false).unwrap(),
        )
        .unwrap();

        // Generate new addresses and compare
        let sw_address = sw_wallet.address(None).unwrap();
        let sdk_address = sdk_wallet.address(None).unwrap();

        assert_eq!(
            sw_address.address().to_string(),
            sdk_address.address().to_string(),
            "Addresses should be identical"
        );
    }
}