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
// Copyright (c) 2022-2023 The MobileCoin Foundation

//! Handle for connected ledger devices
//!
//! This provides methods for interacting with the device
//! and is generic over [ledger_lib::Device]

use std::{sync::Arc, time::Duration};

use async_trait::async_trait;
use ed25519_dalek::VerifyingKey;
use ledger_lib::Device;
use ledger_proto::{ApduBase, ApduReq};
use log::debug;
use rand_core::OsRng;
use tokio::sync::Mutex;

use ledger_mob_apdu::{
    app_info::AppFlags,
    ident::{IdentGetReq, IdentResp, IdentSignReq},
    key_image::{KeyImageReq, KeyImageResp},
    prelude::{AppInfoReq, AppInfoResp},
    state::TxState,
    subaddress_keys::{SubaddressKeyReq, SubaddressKeyResp},
    tx::{TxInfo, TxInfoReq},
    wallet_keys::{WalletKeyReq, WalletKeyResp},
};

use mc_core::{
    account::{ViewAccount, ViewSubaddress},
    keys::TxOutPublic,
};
use mc_crypto_keys::RistrettoPublic;
use mc_crypto_ring_signature::KeyImage;
use mc_transaction_core::{ring_ct::InputRing, tx::Tx};
use mc_transaction_extra::UnsignedTx;
use mc_transaction_signer::types::TxoSynced;

use crate::{
    account::AccountHandle,
    tx::{TransactionHandle, TxConfig},
    Error,
};

/// MobileCoin handle for a connected ledger [Device].
///
/// This is generic over [Device] types to support different
/// underlying transports / providers
#[derive(Clone)]
pub struct DeviceHandle<T: Device> {
    /// Device handle for communication
    t: Arc<Mutex<T>>,
    /// Timeout for user acknowledgements
    user_timeout_s: usize,
    /// Timeout for APDU requests
    request_timeout_s: usize,
}

/// Create a [DeviceHandle] wrapper from a type implementing [Device]
impl<T: Device> From<T> for DeviceHandle<T> {
    fn from(t: T) -> Self {
        Self {
            t: Arc::new(Mutex::new(t)),
            user_timeout_s: 10,
            request_timeout_s: 2,
        }
    }
}

unsafe impl<T: Device> Send for DeviceHandle<T> {}

#[derive(Clone, Debug, PartialEq)]
pub struct MobAppInfo {
    pub app_name: String,
    pub app_version: String,
    pub protocol_version: u8,
    pub flags: AppFlags,
}

impl<T: Device + Send> DeviceHandle<T> {
    /// Helper to fetch user interaction timeout
    fn user_timeout(&self) -> Duration {
        Duration::from_secs(self.user_timeout_s as u64)
    }

    /// Helper to fetch APDU request timeout
    fn request_timeout(&self) -> Duration {
        Duration::from_secs(self.request_timeout_s as u64)
    }

    /// Fetch ledger application info
    pub async fn app_info(&mut self) -> Result<MobAppInfo, Error> {
        let mut buff = [0u8; 256];

        debug!("Requesting app info");

        let resp = self
            .request::<AppInfoResp>(AppInfoReq {}, &mut buff, self.request_timeout())
            .await?;

        Ok(MobAppInfo {
            app_name: resp.name.to_string(),
            app_version: resp.version.to_string(),
            protocol_version: resp.proto,
            flags: resp.flags,
        })
    }

    /// Fetch root keys for the provided account index
    pub async fn account_keys(&mut self, account_index: u32) -> Result<ViewAccount, Error> {
        let (mut buff_a, mut buff_b) = ([0u8; 256], [0u8; 256]);

        debug!("Requesting root keys for account: {}", account_index);

        let req = WalletKeyReq::new(account_index);
        let resp = self
            .retry::<WalletKeyResp>(req, &mut buff_a, &mut buff_b)
            .await?;

        Ok(ViewAccount::new(resp.view_private, resp.spend_public))
    }

    /// Fetch subaddress keys for the provided account and subaddress index
    pub async fn subaddress_keys(
        &mut self,
        account_index: u32,
        subaddress_index: u64,
    ) -> Result<ViewSubaddress, Error> {
        let (mut buff_a, mut buff_b) = ([0u8; 256], [0u8; 256]);

        debug!(
            "Requesting subaddress keys for account: {}, subaddress: {}",
            account_index, subaddress_index
        );

        let req = SubaddressKeyReq::new(account_index, subaddress_index);
        let resp = self
            .retry::<SubaddressKeyResp>(req, &mut buff_a, &mut buff_b)
            .await?;

        Ok(ViewSubaddress {
            view_private: resp.view_private,
            spend_public: resp.spend_public,
        })
    }

    /// Resolve a key image for a given tx_out
    pub async fn key_image(
        &mut self,
        account_index: u32,
        subaddress_index: u64,
        tx_public_key: RistrettoPublic,
    ) -> Result<KeyImage, Error> {
        let (mut buff_a, mut buff_b) = ([0u8; 256], [0u8; 256]);

        debug!(
            "Resolving key image for account: {}, subaddress: {}, tx_public_key: {}",
            account_index, subaddress_index, tx_public_key
        );

        let req = KeyImageReq::new(account_index, subaddress_index, tx_public_key.into());
        let resp = self
            .retry::<KeyImageResp>(req, &mut buff_a, &mut buff_b)
            .await?;

        Ok(resp.key_image)
    }

    /// Helper to retry for requests requiring user approval
    // TODO: fix apdu lifetimes so we don't need multiple buffers here / can return immediate errors
    async fn retry<'a, ANS: ApduBase<'a>>(
        &mut self,
        req: impl ApduReq<'_> + Clone + Send,
        buff_a: &'a mut [u8],
        buff_b: &'a mut [u8],
    ) -> Result<ANS, Error> {
        // First request, may succeed or require approval
        if let Ok(v) = self
            .request::<ANS>(req.clone(), buff_a, self.request_timeout())
            .await
        {
            return Ok(v);
        };

        // Poll on app unlock state
        for i in 0..self.user_timeout_s {
            let info = self.app_info().await?;
            match info.flags.contains(AppFlags::UNLOCKED) {
                true => break,
                false if i == self.user_timeout_s - 1 => return Err(Error::UserTimeout),
                false => {
                    debug!("Waiting for user approval: {}s", i);
                    tokio::time::sleep(std::time::Duration::from_secs(1)).await;
                }
            }
        }

        // Re-issue request
        let resp = self
            .request::<ANS>(req.clone(), buff_b, self.request_timeout())
            .await?;

        Ok(resp)
    }

    /// Fetch a handle to a specific on-device account by SLIP-0010 index
    pub async fn account(&mut self, account_index: u32) -> AccountHandle<T> {
        // TODO: prompt device to generate / cache account keys for re-use

        // Return wrapped handle
        AccountHandle {
            account_index,
            user_timeout: self.user_timeout(),
            t: self.t.clone(),
        }
    }

    /// Sign an unsigned transaction object using the device
    pub async fn transaction(
        &mut self,
        account_index: u32,
        approval_timeout_s: u32,
        unsigned: UnsignedTx,
    ) -> Result<(Tx, Vec<TxoSynced>), Error> {
        // Start device transaction
        debug!("Starting transaction");
        let mut signer = TransactionHandle::new(
            TxConfig {
                account_index,
                num_memos: 0,
                num_rings: unsigned.rings.len(),
                request_timeout: self.request_timeout(),
                user_timeout: Duration::from_secs(approval_timeout_s as u64),
            },
            self.t.clone(),
        )
        .await?;

        // TODO: sign memos (this requires a restructure of UnsignedTx)

        // Build the digest for ring signing
        debug!("Building TX digest");
        let (signing_data, summary, unblinding, digest) =
            unsigned.get_signing_data(&mut OsRng {}).unwrap();

        debug!(
            "Using extended digest: {:02x?}",
            signing_data.mlsag_signing_digest
        );

        // Load transaction summary
        debug!("Loading tx summary");
        signer
            .set_tx_summary(unsigned.block_version, &digest.0, &summary, &unblinding)
            .await?;

        // Await transaction approval
        signer.await_approval(approval_timeout_s).await?;

        // Sign rings
        debug!("Executing signing operation");
        let signature = signing_data.sign(&unsigned.rings, &signer, &mut OsRng {})?;

        debug!("Signing complete");

        // Signal completion to app
        signer.complete().await?;

        // Map key images to real inputs via public key
        let mut txos = vec![];
        for (i, r) in unsigned.rings.iter().enumerate() {
            let tx_out_public_key = match r {
                InputRing::Signable(r) => r.members[r.real_input_index].public_key,
                InputRing::Presigned(_) => panic!("Pre-signed rings unsupported"),
            };

            txos.push(TxoSynced {
                tx_out_public_key: TxOutPublic::from(
                    RistrettoPublic::try_from(&tx_out_public_key).unwrap(),
                ),
                key_image: signature.ring_signatures[i].key_image,
            });
        }

        // Buld transaction object
        let tx = Tx {
            prefix: unsigned.tx_prefix.clone(),
            signature,
            // TODO: where should this come from?
            fee_map_digest: vec![],
        };

        Ok((tx, txos))
    }

    /// Execute and identity challenge and response
    pub async fn identity(
        &mut self,
        index: u32,
        uri: &str,
        challenge: &[u8],
    ) -> Result<(VerifyingKey, [u8; 64]), Error> {
        let mut buff = [0u8; 256];

        debug!("Executing identity challenge");

        // Issue signing request
        let req = IdentSignReq::new(index, uri, challenge);
        let resp = self
            .request::<TxInfo>(req, &mut buff, self.user_timeout())
            .await?;

        if resp.state != TxState::IdentPending {
            return Err(Error::InvalidState(resp.state, TxState::IdentPending));
        }

        // Await user approval
        let n = self.user_timeout_s;
        for i in 0..n {
            let resp = self
                .request::<TxInfo>(TxInfoReq, &mut buff, self.user_timeout())
                .await?;

            match resp.state {
                TxState::IdentApproved => break,
                TxState::IdentPending if i + 1 < n => {
                    tokio::time::sleep(std::time::Duration::from_secs(1)).await;
                }
                TxState::IdentPending => return Err(Error::UserTimeout),
                _ => return Err(Error::UserDenied),
            }
        }

        // Fetch identity response
        let resp = self
            .request::<IdentResp>(IdentGetReq, &mut buff, self.user_timeout())
            .await?;

        let public_key =
            VerifyingKey::from_bytes(&resp.public_key).map_err(|_| Error::InvalidKey)?;

        Ok((public_key, resp.signature))
    }
}

/// Re-export [Device] trait for MobileCoin [DeviceHandle]
#[async_trait]
impl<T: Device + Send> Device for DeviceHandle<T> {
    async fn request<'a, 'b, RESP: ApduBase<'b>>(
        &mut self,
        request: impl ApduReq<'a> + Send,
        buff: &'b mut [u8],
        timeout: Duration,
    ) -> Result<RESP, ledger_lib::Error> {
        self.t.lock().await.request(request, buff, timeout).await
    }
}