lwk_ledger 0.17.0

Liquid Wallet Kit - Interact with Ledger Hardware Wallet
Documentation
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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
mod apdu;
mod client;
pub mod command;
mod error;
mod interpreter;
mod merkle;
mod psbt;
mod transport_tcp;
mod wallet;

#[cfg(feature = "serial")]
mod transport_hid;

#[cfg(feature = "test_emulator")]
mod ledger_emulator;

#[cfg(feature = "asyncr")]
pub mod asyncr;

use std::io::Cursor;

use byteorder::{BigEndian, ReadBytesExt};
#[cfg(feature = "test_emulator")]
pub use ledger_emulator::TestLedgerEmulator;

pub use ledger_apdu::APDUAnswer;

// Adapted from
// https://github.com/LedgerHQ/app-bitcoin-new/tree/master/bitcoin_client_rs
pub use client::LiquidClient;
use client::Transport;
pub use psbt::PartialSignature;
pub use transport_tcp::TransportTcp;
pub use wallet::{AddressType, Version, WalletPolicy, WalletPubKey};

pub use apdu::{APDUCmdVec, StatusWord};

use elements_miniscript::confidential::slip77;
use elements_miniscript::elements::bitcoin::bip32::{
    ChildNumber, DerivationPath, Fingerprint, Xpub,
};
use elements_miniscript::elements::pset::PartiallySignedTransaction;
use elements_miniscript::elements::{
    bitcoin::key::PublicKey,
    bitcoin::sign_message::MessageSignature,
    opcodes::{
        all::{OP_CHECKMULTISIG, OP_PUSHNUM_1, OP_PUSHNUM_16},
        All,
    },
    script::Instruction,
    Script,
};

use lwk_common::Signer;

#[derive(Debug)]
pub struct Ledger<T: Transport> {
    /// Ledger Liquid Client
    pub client: LiquidClient<T>,
}

impl Ledger<TransportTcp> {
    pub fn new(port: u16) -> Self {
        let client = LiquidClient::new(TransportTcp::new(port).expect("TODO"));
        Self { client }
    }
}

#[cfg(feature = "serial")]
impl Ledger<transport_hid::TransportHID> {
    pub fn new_hid() -> Self {
        let h = ledger_transport_hid::hidapi::HidApi::new().expect("unable to get HIDAPI");
        let hid = ledger_transport_hid::TransportNativeHID::new(&h).unwrap();
        let client = LiquidClient::new(transport_hid::TransportHID::new(hid));
        Self { client }
    }
}

pub type Error = error::LiquidClientError<TransportTcp>;

impl<T: Transport> Signer for &Ledger<T> {
    type Error = crate::Error;

    fn sign(&self, pset: &mut PartiallySignedTransaction) -> std::result::Result<u32, Self::Error> {
        // Set the default values some fields that Ledger requires
        if pset.global.tx_data.fallback_locktime.is_none() {
            pset.global.tx_data.fallback_locktime =
                Some(elements_miniscript::elements::LockTime::ZERO);
        }
        for input in pset.inputs_mut() {
            if input.sequence.is_none() {
                input.sequence = Some(elements_miniscript::elements::Sequence::default());
            }
        }

        // Use a map to avoid inserting a wallet twice
        let mut wallets = std::collections::HashMap::<String, WalletPolicy>::new();
        let mut n_sigs = 0;
        let master_fp = self.fingerprint()?;

        // Figure out which wallets are signing
        'outer: for input in pset.inputs() {
            let is_p2wpkh = input
                .witness_utxo
                .as_ref()
                .map(|u| u.script_pubkey.is_v0_p2wpkh())
                .unwrap_or(false);
            let is_p2sh = input
                .witness_utxo
                .as_ref()
                .map(|u| u.script_pubkey.is_p2sh())
                .unwrap_or(false);
            let is_p2shwpkh = is_p2sh
                && input
                    .redeem_script
                    .as_ref()
                    .map(|x| x.is_v0_p2wpkh())
                    .unwrap_or(false);
            // Singlesig
            if is_p2wpkh || is_p2shwpkh {
                // We expect exactly one element
                if let Some((fp, path)) = input.bip32_derivation.values().next() {
                    if fp == &master_fp {
                        // TODO: check path
                        // path has len 3
                        // path has all hardened
                        // path has purpose matching address type
                        // path has correct coin type
                        let mut v: Vec<ChildNumber> = path.clone().into();
                        v.truncate(3);
                        let path: DerivationPath = v.into();

                        // Do we care about the descriptor blinding key here?
                        let name = "".to_string();
                        let version = Version::V2;
                        // TODO: cache xpubs
                        let xpub = self
                            .client
                            .get_extended_pubkey(&path, false)
                            .map_err(to_dbg)?;
                        let key = WalletPubKey::from(((*fp, path.clone()), xpub));
                        let keys = vec![key];
                        let desc = if is_p2wpkh {
                            "wpkh(@0/**)"
                        } else {
                            "sh(wpkh(@0/**))"
                        };
                        let wallet_policy =
                            WalletPolicy::new(name, version, desc.to_string(), keys);
                        let is_change = false;
                        if let Ok(d) = wallet_policy.get_descriptor(is_change) {
                            wallets.insert(d, wallet_policy);
                        }
                    }
                }
            } else {
                let is_p2wsh = input
                    .witness_utxo
                    .as_ref()
                    .map(|u| u.script_pubkey.is_v0_p2wsh())
                    .unwrap_or(false);
                let details = input.witness_script.as_ref().and_then(parse_multisig);
                // Multisig
                if is_p2wsh {
                    if let Some((threshold, pubkeys)) = details {
                        let mut keys: Vec<WalletPubKey> = vec![];
                        for pubkey in pubkeys {
                            if let Some((fp, path)) = input.bip32_derivation.get(&pubkey) {
                                let mut v: Vec<ChildNumber> = path.clone().into();
                                v.truncate(3);
                                let path: DerivationPath = v.into();
                                let keysource = (*fp, path);
                                if let Some(xpub) = pset.global.xpub.iter().find_map(|(x, ks)| {
                                    if ks == &keysource {
                                        Some(x)
                                    } else {
                                        None
                                    }
                                }) {
                                    let mut key = WalletPubKey::from((keysource, *xpub));
                                    key.multipath = Some("/**".to_string());
                                    keys.push(key);
                                } else {
                                    // Global xpub not available, cannot reconstruct the script
                                    continue 'outer;
                                }
                            } else {
                                // No keysource for pubkey in script
                                // Either the script is not ours or data is missing
                                continue 'outer;
                            }
                        }
                        let sorted = false;
                        let wallet_policy = WalletPolicy::new_multisig(
                            "todo".to_string(),
                            Version::V1,
                            AddressType::NativeSegwit,
                            threshold as usize,
                            keys,
                            sorted,
                            None,
                        )
                        .map_err(to_dbg)?;
                        let is_change = false;
                        if let Ok(d) = wallet_policy.get_descriptor(is_change) {
                            wallets.insert(d, wallet_policy);
                        }
                    }
                }
            }
        }

        // For each wallet, sign
        for wallet_policy in wallets.values() {
            let hmac = if wallet_policy.threshold.is_some() {
                // Register multisig wallets
                let (_id, hmac) = self.client.register_wallet(wallet_policy).map_err(to_dbg)?;
                Some(hmac)
            } else {
                None
            };
            let partial_sigs = self
                .client
                .sign_psbt(pset, wallet_policy, hmac.as_ref())
                .map_err(to_dbg)?;
            n_sigs += partial_sigs.len();

            // Add sigs to pset
            for (input_idx, sig) in partial_sigs {
                let input = &mut pset.inputs_mut()[input_idx];
                for (public_key, (fp, _origin)) in &input.bip32_derivation {
                    if fp == &master_fp {
                        // TODO: user the pubkey from PartialSignature to insert in partial_sigs
                        let sig_vec = match sig {
                            PartialSignature::Sig(_, sig) => sig.to_vec(),
                            _ => panic!("FIXME: support taproot sig or raise error"),
                        };
                        input.partial_sigs.insert(*public_key, sig_vec);
                        // FIXME: handle cases where we have multiple pubkeys with master fingerprint
                        break;
                    }
                }
            }
        }

        Ok(n_sigs as u32)
    }

    fn derive_xpub(&self, path: &DerivationPath) -> std::result::Result<Xpub, Self::Error> {
        self.client.get_extended_pubkey(path, false).map_err(to_dbg)
    }

    fn slip77_master_blinding_key(
        &self,
    ) -> std::result::Result<slip77::MasterBlindingKey, Self::Error> {
        self.client.get_master_blinding_key().map_err(to_dbg)
    }

    fn fingerprint(&self) -> std::result::Result<Fingerprint, Self::Error> {
        self.client.get_master_fingerprint().map_err(to_dbg)
    }

    fn sign_message(
        &self,
        _message: &str,
        _path: &DerivationPath,
    ) -> Result<MessageSignature, Self::Error> {
        todo!(); // TODO: use internal methods to implement
    }
}

fn to_dbg(e: impl std::fmt::Debug) -> Error {
    Error::ClientError(format!("{e:?}"))
}

impl<T: Transport> Signer for Ledger<T> {
    type Error = crate::Error;

    fn sign(&self, pset: &mut PartiallySignedTransaction) -> std::result::Result<u32, Self::Error> {
        Signer::sign(&self, pset)
    }

    fn derive_xpub(&self, path: &DerivationPath) -> std::result::Result<Xpub, Self::Error> {
        Signer::derive_xpub(&self, path)
    }

    fn slip77_master_blinding_key(
        &self,
    ) -> std::result::Result<slip77::MasterBlindingKey, Self::Error> {
        Signer::slip77_master_blinding_key(&self)
    }

    fn fingerprint(&self) -> std::result::Result<Fingerprint, Self::Error> {
        Signer::fingerprint(&self)
    }

    fn sign_message(
        &self,
        message: &str,
        path: &DerivationPath,
    ) -> Result<MessageSignature, Self::Error> {
        Signer::sign_message(&self, message, path)
    }
}

// "duplicated" from Jade
// taken and adapted from:
// https://github.com/rust-bitcoin/rust-bitcoin/blob/37daf4620c71dc9332c3e08885cf9de696204bca/bitcoin/src/blockdata/script/borrowed.rs#L266
#[allow(unused)]
pub fn parse_multisig(script: &Script) -> Option<(u32, Vec<PublicKey>)> {
    fn decode_pushnum(op: All) -> Option<u8> {
        let start: u8 = OP_PUSHNUM_1.into_u8();
        let end: u8 = OP_PUSHNUM_16.into_u8();
        if start < op.into_u8() && end >= op.into_u8() {
            Some(op.into_u8() - start + 1)
        } else {
            None
        }
    }

    let required_sigs;

    let mut instructions = script.instructions();
    if let Some(Ok(Instruction::Op(op))) = instructions.next() {
        if let Some(pushnum) = decode_pushnum(op) {
            required_sigs = pushnum;
        } else {
            return None;
        }
    } else {
        return None;
    }

    let mut num_pubkeys: u8 = 0;
    let mut pubkeys = vec![];
    while let Some(Ok(instruction)) = instructions.next() {
        match instruction {
            Instruction::PushBytes(pubkey) => {
                let pk = PublicKey::from_slice(pubkey).expect("FIXME");
                pubkeys.push(pk);
                num_pubkeys += 1;
            }
            Instruction::Op(op) => {
                if let Some(pushnum) = decode_pushnum(op) {
                    if pushnum != num_pubkeys {
                        return None;
                    }
                }
                break;
            }
        }
    }

    if required_sigs > num_pubkeys {
        return None;
    }

    if let Some(Ok(Instruction::Op(op))) = instructions.next() {
        if op != OP_CHECKMULTISIG {
            return None;
        }
    } else {
        return None;
    }

    if instructions.next().is_none() {
        Some((required_sigs.into(), pubkeys))
    } else {
        None
    }
}

const LEDGER_CHANNEL: u16 = 0x0101;

pub fn read_multi_apdu(apdu_answers: Vec<Vec<u8>>) -> Result<Vec<u8>, Error> {
    let mut result = vec![];
    let mut expected_apdu_len = 0usize;

    for (sequence_idx, el) in apdu_answers.into_iter().enumerate() {
        let res = el.len();

        if (sequence_idx == 0 && res < 7) || res < 5 {
            return Err(Error::ClientError(
                "Read error. Incomplete header".to_string(),
            ));
        }

        let mut rdr = Cursor::new(&el);

        let rcv_channel = rdr
            .read_u16::<BigEndian>()
            .map_err(|_| Error::ClientError("Invalid channel".to_string()))?;
        let rcv_tag = rdr
            .read_u8()
            .map_err(|_| Error::ClientError("Invalid tag".to_string()))?;
        let rcv_seq_idx = rdr
            .read_u16::<BigEndian>()
            .map_err(|_| Error::ClientError("Invalid sequence idx".to_string()))?;

        if rcv_channel != LEDGER_CHANNEL {
            return Err(Error::ClientError("Invalid channel".to_string()));
        }
        if rcv_tag != 0x05u8 {
            return Err(Error::ClientError("Invalid tag".to_string()));
        }

        if rcv_seq_idx != sequence_idx as u16 {
            return Err(Error::ClientError("Invalid sequence idx".to_string()));
        }

        if rcv_seq_idx == 0 {
            expected_apdu_len = rdr
                .read_u16::<BigEndian>()
                .map_err(|_| Error::ClientError("Invalid expected apdu len".to_string()))?
                as usize;
        }

        let needs_more = expected_apdu_len > (result.len() + el.len()); // TODO check off by one

        let start = rdr.position() as usize;
        let end = if needs_more {
            el.len()
        } else {
            expected_apdu_len - result.len() + start
        };

        let new_chunk = &el[start..end];

        result.extend_from_slice(new_chunk);

        if result.len() >= expected_apdu_len {
            return Ok(result);
        }
    }
    Err(Error::ClientError("Incomplete APDU".to_string()))
}

const LEDGER_PACKET_WRITE_SIZE: u8 = 64;

// based on https://github.com/Zondax/ledger-rs/blob/master/ledger-transport-hid/src/lib.rs
// with the notable difference we don't use the prefix 0x00
pub fn write_apdu(apdu_command: &APDUCmdVec) -> Vec<[u8; LEDGER_PACKET_WRITE_SIZE as usize]> {
    let channel = LEDGER_CHANNEL;
    let apdu_command = apdu_command.serialize();
    let mut results = vec![];
    let command_length = apdu_command.len();
    let mut in_data = Vec::with_capacity(command_length + 2);
    in_data.push(((command_length >> 8) & 0xFF) as u8);
    in_data.push((command_length & 0xFF) as u8);
    in_data.extend_from_slice(&apdu_command);

    let mut buffer = vec![0u8; LEDGER_PACKET_WRITE_SIZE as usize];
    buffer[0] = ((channel >> 8) & 0xFF) as u8; // channel big endian
    buffer[1] = (channel & 0xFF) as u8; // channel big endian
    buffer[2] = 0x05u8;

    for (sequence_idx, chunk) in in_data
        .chunks((LEDGER_PACKET_WRITE_SIZE - 5) as usize)
        .enumerate()
    {
        buffer[3] = ((sequence_idx >> 8) & 0xFF) as u8; // sequence_idx big endian
        buffer[4] = (sequence_idx & 0xFF) as u8; // sequence_idx big endian
        buffer[5..5 + chunk.len()].copy_from_slice(chunk);

        results.push(buffer.clone().try_into().unwrap());
    }
    results
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_read_apdu() {
        // messages taken from xpub requests in the browser
        let message1 = [
            1, 1, 5, 0, 0, 0, 113, 116, 112, 117, 98, 68, 67, 119, 89, 106, 112, 68, 104, 85, 100,
            80, 71, 80, 53, 114, 83, 51, 119, 103, 78, 103, 49, 51, 109, 84, 114, 114, 106, 66,
            117, 71, 56, 86, 57, 86, 112, 87, 98, 121, 112, 116, 88, 54, 84, 82, 80, 98, 78, 111,
            90, 86, 88, 115,
        ]
        .to_vec();
        let message2 = [
            1, 1, 5, 0, 1, 111, 86, 85, 83, 107, 67, 106, 109, 81, 56, 106, 74, 121, 99, 106, 117,
            68, 75, 66, 98, 57, 101, 97, 116, 97, 83, 121, 109, 88, 97, 107, 84, 84, 97, 71, 105,
            102, 120, 82, 54, 107, 109, 86, 115, 102, 70, 101, 104, 72, 49, 90, 103, 74, 84, 144,
            0, 0, 0, 0,
        ]
        .to_vec();
        let result = read_multi_apdu(vec![message1, message2]).unwrap();
        assert_eq!(
            result,
            vec![
                116, 112, 117, 98, 68, 67, 119, 89, 106, 112, 68, 104, 85, 100, 80, 71, 80, 53,
                114, 83, 51, 119, 103, 78, 103, 49, 51, 109, 84, 114, 114, 106, 66, 117, 71, 56,
                86, 57, 86, 112, 87, 98, 121, 112, 116, 88, 54, 84, 82, 80, 98, 78, 111, 90, 86,
                88, 115, 111, 86, 85, 83, 107, 67, 106, 109, 81, 56, 106, 74, 121, 99, 106, 117,
                68, 75, 66, 98, 57, 101, 97, 116, 97, 83, 121, 109, 88, 97, 107, 84, 84, 97, 71,
                105, 102, 120, 82, 54, 107, 109, 86, 115, 102, 70, 101, 104, 72, 49, 90, 103, 74,
                84, 144, 0
            ]
        );
        let answer = APDUAnswer::from_answer(result).unwrap();
        let status = StatusWord::try_from(answer.retcode()).unwrap_or(StatusWord::Unknown);
        // let vec = answer.data().to_vec();
        assert_eq!(status, StatusWord::OK);
    }

    #[test]
    fn test_read_apdu_single() {
        let get_version_test_vector_array = [
            1, 14, 76, 105, 113, 117, 105, 100, 32, 82, 101, 103, 116, 101, 115, 116, 5, 50, 46,
            50, 46, 51, 1, 2, 144, 0,
        ];

        let received_apdu_ledger = [
            1, 1, 5, 0, 0, 0, 26, 1, 14, 76, 105, 113, 117, 105, 100, 32, 82, 101, 103, 116, 101,
            115, 116, 5, 50, 46, 50, 46, 51, 1, 2, 144, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        ];
        let result = read_multi_apdu(vec![received_apdu_ledger.to_vec()]).unwrap();
        assert_eq!(result, get_version_test_vector_array.to_vec());
    }

    #[test]
    fn test_fix_return_error() {
        let d = [[
            1, 1, 5, 0, 0, 0, 67, 66, 246, 203, 175, 41, 59, 76, 239, 143, 8, 205, 206, 188, 195,
            151, 107, 194, 119, 208, 91, 66, 183, 226, 62, 33, 83, 168, 81, 140, 125, 100, 200,
            140, 146, 242, 46, 52, 250, 248, 37, 179, 244, 196, 225, 203, 90, 152, 201, 177, 38,
            128, 184, 233, 230, 21, 233, 229,
        ]
        .to_vec()]
        .to_vec();

        let result = read_multi_apdu(d).unwrap_err();
        assert_eq!(result.to_string(), "Client Error: Incomplete APDU");
    }

    #[test]
    fn test_write_apdu() {
        let command = crate::command::get_version();
        assert_eq!(&command.serialize(), &[176u8, 1, 0, 0, 0]);
        let results = write_apdu(&command);
        assert_eq!(
            results,
            vec![[
                1, 1, 5, 0, 0, 0, 5, 176, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0
            ]]
        );
    }
}