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

//! Wallet key APDUs, for fetching root account keys

use encdec::{Decode, Encode};

use mc_core::keys::{RootSpendPublic, RootViewPrivate};

use super::{ApduError, ApduStatic, Instruction, MOB_APDU_CLA};
use crate::helpers::*;

/// Wallet key request APDU.
///
/// Requests root / account keys for SLIP-0010 derived account.
///
/// ## Encoding:
/// ```text
///  0                   1                   2                   3
///  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
/// |                         WALLET_INDEX                          |
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
/// ```
#[derive(Copy, Clone, PartialEq, Debug, Encode, Decode)]
#[encdec(error = "ApduError")]
pub struct WalletKeyReq {
    /// SLIP-0010 account index
    pub account_index: u32,
}

impl WalletKeyReq {
    /// Create a new [WalletKeyReq] APDU
    pub fn new(account_index: u32) -> Self {
        Self { account_index }
    }
}

impl ApduStatic for WalletKeyReq {
    const CLA: u8 = MOB_APDU_CLA;
    const INS: u8 = Instruction::GetWalletKeys as u8;
}

/// Wallet key response APDU
///
/// Contains root view private and spend public keys for application use.
///
/// ## Encoding:
/// ```text
///  0                   1                   2                   3
///  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
/// |                         WALLET_INDEX                          |
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
/// |                       SUBADDRESS_INDEX                        |
/// |                         (8-byte u64)                          |
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
/// |                                                               |
/// /                      ROOT_VIEW_PRIVATE_KEY                    /
/// /                (32-byte Ristretto Private Key)                /
/// |                                                               |
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
/// |                                                               |
/// /                      ROOT_SPEND_PUBLIC_KEY                    /
/// /                 (32-byte Ristretto Public Key)                /
/// |                                                               |
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
/// ```
#[derive(Clone, PartialEq, Debug, Encode, Decode)]
#[encdec(error = "ApduError")]
pub struct WalletKeyResp {
    /// SLIP-0010 account index
    pub account_index: u32,
    /// View Private Key
    #[encdec(with = "pri_key")]
    pub view_private: RootViewPrivate,
    /// Spend public key
    #[encdec(with = "pub_key")]
    pub spend_public: RootSpendPublic,
}

impl WalletKeyResp {
    /// Create a new [`WalletKeyResp`] APDU
    pub fn new(
        account_index: u32,
        view_private: RootViewPrivate,
        spend_public: RootSpendPublic,
    ) -> Self {
        Self {
            account_index,
            view_private,
            spend_public,
        }
    }
}

#[cfg(test)]
mod test {

    use mc_crypto_keys::{RistrettoPrivate, RistrettoPublic};

    use mc_util_from_random::FromRandom;

    use rand::random;
    use rand_core::OsRng;

    use super::*;
    use crate::test::encode_decode_apdu;

    #[test]
    fn wallet_keys_get_apdu() {
        let apdu = WalletKeyReq::new(random());

        let mut buff = [0u8; 128];
        encode_decode_apdu(&mut buff, &apdu);
    }

    #[test]
    fn wallet_keys_ans_apdu() {
        let (view_private, spend_private) = (
            RistrettoPrivate::from_random(&mut OsRng {}),
            RistrettoPrivate::from_random(&mut OsRng {}),
        );

        let apdu = WalletKeyResp::new(
            random(),
            view_private.into(),
            RistrettoPublic::from(&spend_private).into(),
        );

        let mut buff = [0u8; 256];
        encode_decode_apdu(&mut buff, &apdu);
    }
}