rust-cktap 0.1.0

A Rust implementation of the Coinkite Tap Protocol (cktap) for use with SATSCARD, TAPSIGNER, and SATSCHIP products.
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
// Copyright (c) 2025 rust-cktap contributors
// SPDX-License-Identifier: MIT OR Apache-2.0

use crate::apdu::tap_signer::{XpubCommand, XpubResponse};
use crate::apdu::{
    CommandApdu as _, DeriveCommand, DeriveResponse, NewCommand, NewResponse, SignCommand,
    SignResponse, StatusCommand, StatusResponse,
    tap_signer::{BackupCommand, BackupResponse, ChangeCommand, ChangeResponse},
};
use crate::error::{ChangeError, DeriveError, ReadError, SignPsbtError, StatusError, XpubError};
use crate::shared::{Authentication, Certificate, CkTransport, Nfc, Read, Wait, transmit};
use crate::{BIP32_HARDENED_MASK, CkTapError};
use async_trait::async_trait;
use bitcoin::PublicKey;
use bitcoin::bip32::{ChainCode, Xpub};
use bitcoin::hex::DisplayHex;
use bitcoin::secp256k1::{self, All, Message, Secp256k1, ecdsa::Signature};
use bitcoin_hashes::sha256;
use std::sync::Arc;

const BIP84_PATH_LEN: usize = 5;

// BIP84 derivation path structure, m / 84' / 0' / account' / change / address_index

// Derivation sub-path indexes that must be hardened
const BIP84_HARDENED_SUBPATH: [usize; 3] = [0, 1, 2];

pub struct TapSigner {
    pub transport: Arc<dyn CkTransport>,
    pub secp: Secp256k1<All>,
    pub proto: usize,
    pub ver: String,
    pub birth: usize,
    pub path: Option<Vec<usize>>,
    // [(1<<31)+84, (1<<31), (1<<31)], user-defined, will be omitted if not yet setup
    pub num_backups: Option<usize>,
    pub pubkey: PublicKey,
    pub card_nonce: [u8; 16],
    pub auth_delay: Option<usize>,
}

impl Authentication for TapSigner {
    fn secp(&self) -> &Secp256k1<All> {
        &self.secp
    }

    fn ver(&self) -> &str {
        &self.ver
    }

    fn pubkey(&self) -> &PublicKey {
        &self.pubkey
    }

    fn card_nonce(&self) -> &[u8; 16] {
        &self.card_nonce
    }

    fn set_card_nonce(&mut self, new_nonce: [u8; 16]) {
        self.card_nonce = new_nonce;
    }

    fn auth_delay(&self) -> &Option<usize> {
        &self.auth_delay
    }

    fn set_auth_delay(&mut self, auth_delay: Option<usize>) {
        self.auth_delay = auth_delay;
    }

    fn transport(&self) -> Arc<dyn CkTransport> {
        self.transport.clone()
    }
}

/// Function shared between TapSigner and SatsChip
#[async_trait]
pub trait TapSignerShared: Authentication {
    /// Initialize the tap signer or sats chip, can only be done once
    async fn init(&mut self, chain_code: ChainCode, cvc: &str) -> Result<(), CkTapError> {
        let (_, epubkey, xcvc) = self.calc_ekeys_xcvc(cvc, NewCommand::name());
        let new_command = NewCommand::new(Some(0), Some(chain_code), epubkey, xcvc);
        let new_response: NewResponse = transmit(self.transport(), &new_command).await?;
        self.set_card_nonce(new_response.card_nonce);
        Ok(())
    }

    /// Get the status of the tap signer or sats chip, including the current card nonce
    async fn status(&mut self) -> Result<StatusResponse, CkTapError> {
        let cmd = StatusCommand::default();
        let status_response: StatusResponse = transmit(self.transport(), &cmd).await?;
        self.set_card_nonce(status_response.card_nonce);
        Ok(status_response)
    }

    /// Sign a message digest with the tap signer
    async fn sign(
        &mut self,
        digest: [u8; 32],
        sub_path: Vec<u32>,
        cvc: &str,
    ) -> Result<SignResponse, CkTapError> {
        let (eprivkey, epubkey, xcvc) = self.calc_ekeys_xcvc(cvc, SignCommand::name());

        // Use the same session key to encrypt the new CVC
        let session_key = secp256k1::ecdh::SharedSecret::new(&self.pubkey().inner, &eprivkey);

        // encrypt the new cvc by XORing with the session key
        let xdigest_vec: Vec<u8> = session_key
            .as_ref()
            .iter()
            .zip(digest)
            .map(|(session_key_byte, digest_byte)| session_key_byte ^ digest_byte)
            .collect();

        let xdigest: [u8; 32] = xdigest_vec.try_into().expect("input is also 32 bytes");

        let sign_command =
            SignCommand::for_tapsigner(sub_path.clone(), xdigest, epubkey, xcvc.clone());

        let mut sign_response: Result<SignResponse, CkTapError> =
            transmit(self.transport(), &sign_command).await;

        let mut unlucky_number_retries = 0;
        while let Err(CkTapError::Card(crate::CardError::UnluckyNumber)) = sign_response {
            let sign_command =
                SignCommand::for_tapsigner(sub_path.clone(), xdigest, epubkey, xcvc.clone());

            sign_response = transmit(self.transport(), &sign_command).await;
            unlucky_number_retries += 1;

            if unlucky_number_retries > 3 {
                break;
            }
        }

        let sign_response = sign_response?;
        self.set_card_nonce(sign_response.card_nonce);
        Ok(sign_response)
    }

    /// Sign a BIP84, currently only P2WPKH (BIP84) (Native SegWit) PSBT
    /// This function will return a signed but not finalized PSBT. You will need to finalize the
    /// PSBT yourself before it can be broadcast.
    async fn sign_psbt(
        &mut self,
        mut psbt: bitcoin::Psbt,
        cvc: &str,
    ) -> Result<bitcoin::Psbt, SignPsbtError> {
        use bitcoin::{
            secp256k1::ecdsa,
            sighash::{EcdsaSighashType, SighashCache},
        };

        let unsigned_tx = psbt.unsigned_tx.clone();
        let mut sighash_cache = SighashCache::new(&unsigned_tx);

        for (input_index, input) in psbt.inputs.iter_mut().enumerate() {
            // extract previous output data from the PSBT
            let witness_utxo = input
                .witness_utxo
                .as_ref()
                .ok_or(SignPsbtError::MissingUtxo(input_index))?;

            let amount = witness_utxo.value;

            // extract the P2WPKH script from PSBT
            let script_pubkey = &witness_utxo.script_pubkey;
            if !script_pubkey.is_p2wpkh() {
                return Err(SignPsbtError::InvalidScript(input_index));
            }

            // get the public key from the PSBT
            let key_pairs = &input.bip32_derivation;
            let (psbt_pubkey, (_fingerprint, path)) = key_pairs
                .iter()
                .next()
                .ok_or(SignPsbtError::MissingPubkey(input_index))?;

            let path = path.to_u32_vec();

            if path.len() != BIP84_PATH_LEN {
                return Err(SignPsbtError::InvalidPath(input_index));
            }

            let sub_path = BIP84_HARDENED_SUBPATH.map(|i| path[i]);
            if sub_path.iter().any(|p| *p > BIP32_HARDENED_MASK) {
                return Err(SignPsbtError::InvalidPath(input_index));
            }

            // calculate sighash
            let script = script_pubkey.as_script();
            let sighash = sighash_cache
                .p2wpkh_signature_hash(input_index, script, amount, EcdsaSighashType::All)
                .map_err(|e| SignPsbtError::SighashError(e.to_string()))?;

            // the digest is the sighash
            let digest: &[u8; 32] = sighash.as_ref();

            // send digest to TAPSIGNER for signing
            let mut sign_response = self.sign(*digest, sub_path.to_vec(), cvc).await?;
            let mut signature_raw = sign_response.sig;

            // verify that TAPSIGNER used the same public key as the PSBT
            if sign_response.pubkey != psbt_pubkey.serialize() {
                // try deriving the TAPSIGNER and try again
                // take the hardened path and remove the hardened bit, because `sign` hardens it
                let path: Vec<u32> = path
                    .into_iter()
                    .map(|p| p ^ BIP32_HARDENED_MASK)
                    .take(BIP84_HARDENED_SUBPATH.len())
                    .collect();
                let derive_response = self.derive(path, cvc).await;
                if derive_response.is_err() {
                    return Err(SignPsbtError::PubkeyMismatch(input_index));
                }

                // update signature to the new one we just derived
                sign_response = self.sign(*digest, sub_path.to_vec(), cvc).await?;
                signature_raw = sign_response.sig;

                // if still not matching, return error
                if sign_response.pubkey != psbt_pubkey.serialize() {
                    return Err(SignPsbtError::PubkeyMismatch(input_index));
                }
            }

            // update the PSBT input with the signature
            let ecdsa_sig = ecdsa::Signature::from_compact(&signature_raw)
                .map_err(|e| SignPsbtError::SignatureError(e.to_string()))?;

            let final_sig = bitcoin::ecdsa::Signature::sighash_all(ecdsa_sig);
            input.partial_sigs.insert((*psbt_pubkey).into(), final_sig);
        }

        Ok(psbt)
    }

    /// Derive a public key at the given hardened path.
    ///
    /// The derive command on the TAPSIGNER is used to perform hardened BIP-32 key derivation.
    /// Wallets are expected to use it for deriving the BIP-44/48/84 prefix of the path; the value
    /// is captured and stored long term. This is effectively calculating the XPUB to be used on the
    /// mobile wallet.
    ///
    /// Ref: <https://github.com/coinkite/coinkite-tap-proto/blob/master/docs/protocol.md#tapsigner-performs-subkey-derivation>
    async fn derive(&mut self, path: Vec<u32>, cvc: &str) -> Result<PublicKey, DeriveError> {
        // set most significant bit to 1 to represent hardened path steps
        let path = path.iter().map(|p| p ^ (1 << 31)).collect::<Vec<_>>();
        let app_nonce = crate::rand_nonce();
        let (_, epubkey, xcvc) = self.calc_ekeys_xcvc(cvc, DeriveCommand::name());
        let cmd = DeriveCommand::for_tapsigner(app_nonce, path, epubkey, xcvc);
        let derive_response: DeriveResponse = transmit(self.transport(), &cmd).await?;
        self.set_card_nonce(derive_response.card_nonce);

        let master_pubkey = PublicKey::from_slice(&derive_response.master_pubkey)?;
        let pubkey = match &derive_response.pubkey {
            Some(pubkey) => PublicKey::from_slice(pubkey)?,
            None => master_pubkey,
        };

        // TODO FIX currently signature validation only works if no derivation path is used
        if pubkey == master_pubkey {
            let card_nonce = self.card_nonce();
            let sig = &derive_response.sig;

            let mut message_bytes: Vec<u8> = Vec::new();
            message_bytes.extend("OPENDIME".as_bytes());
            message_bytes.extend(card_nonce);
            message_bytes.extend(app_nonce);
            message_bytes.extend(&derive_response.chain_code);

            let message_bytes_hash = sha256::Hash::hash(message_bytes.as_slice());
            let message = Message::from_digest(message_bytes_hash.to_byte_array());

            let signature = Signature::from_compact(sig)?;

            self.secp()
                .verify_ecdsa(&message, &signature, &master_pubkey.inner)?;
        }
        Ok(pubkey)
    }

    /// Change the CVC used for card authentication to a new user provided one
    async fn change(&mut self, new_cvc: &str, cvc: &str) -> Result<(), ChangeError> {
        if new_cvc.len() < 6 {
            return Err(ChangeError::TooShort(new_cvc.len()));
        }

        if new_cvc.len() > 32 {
            return Err(ChangeError::TooLong(new_cvc.len()));
        }

        if new_cvc == cvc {
            return Err(ChangeError::SameAsOld);
        }

        // Create session key and encrypt current CVC
        let (eprivkey, epubkey, xcvc) = self.calc_ekeys_xcvc(cvc, ChangeCommand::name());

        // Use the same session key to encrypt the new CVC
        let session_key = secp256k1::ecdh::SharedSecret::new(&self.pubkey().inner, &eprivkey);

        // encrypt the new cvc by XORing with the session key
        let xnew_cvc: Vec<u8> = session_key
            .as_ref()
            .iter()
            .zip(new_cvc.as_bytes().iter())
            .map(|(session_key_byte, cvc_byte)| session_key_byte ^ cvc_byte)
            .collect();

        let change_command = ChangeCommand::new(xnew_cvc, epubkey, xcvc);
        let change_response: ChangeResponse = transmit(self.transport(), &change_command).await?;

        self.set_card_nonce(change_response.card_nonce);
        Ok(())
    }

    async fn xpub(&mut self, master: bool, cvc: &str) -> Result<Xpub, XpubError> {
        let (_, epubkey, xcvc) = self.calc_ekeys_xcvc(cvc, XpubCommand::name());
        let xpub_command = XpubCommand::new(master, epubkey, xcvc);
        let xpub_response: XpubResponse = transmit(self.transport(), &xpub_command).await?;
        self.set_card_nonce(xpub_response.card_nonce);
        let xpub = Xpub::decode(xpub_response.xpub.as_slice())?;
        Ok(xpub)
    }
}

#[async_trait]
impl Nfc for TapSigner {}

impl TapSignerShared for TapSigner {}

impl TapSigner {
    pub fn try_from_status(
        transport: Arc<dyn CkTransport>,
        status_response: StatusResponse,
    ) -> Result<Self, StatusError> {
        let pubkey = status_response.pubkey.as_slice();
        let pubkey = PublicKey::from_slice(pubkey)?;

        Ok(TapSigner {
            transport,
            secp: Secp256k1::new(),
            proto: status_response.proto,
            ver: status_response.ver,
            birth: status_response.birth,
            path: status_response.path,
            num_backups: status_response.num_backups,
            pubkey,
            card_nonce: status_response.card_nonce,
            auth_delay: status_response.auth_delay,
        })
    }

    /// Backup the current card, the backup is encrypted with the "Backup Password" on the back of the card
    pub async fn backup(&mut self, cvc: &str) -> Result<Vec<u8>, ChangeError> {
        let (_, epubkey, xcvc) = self.calc_ekeys_xcvc(cvc, "backup");

        let backup_command = BackupCommand::new(epubkey, xcvc);
        let backup_response: BackupResponse = transmit(self.transport(), &backup_command).await?;

        self.card_nonce = backup_response.card_nonce;
        Ok(backup_response.data)
    }
}

#[async_trait]
impl Wait for TapSigner {}

#[async_trait]
impl Read for TapSigner {
    fn requires_auth(&self) -> bool {
        true
    }

    fn slot(&self) -> Option<u8> {
        None
    }
}

#[async_trait]
impl Certificate for TapSigner {
    async fn slot_pubkey(&mut self) -> Result<Option<PublicKey>, ReadError> {
        Ok(None)
    }
}

impl core::fmt::Debug for TapSigner {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("TapSigner")
            .field("proto", &self.proto)
            .field("ver", &self.ver)
            .field("birth", &self.birth)
            .field("path", &self.path)
            .field("num_backups", &self.num_backups)
            .field("pubkey", &self.pubkey)
            .field("card_nonce", &self.card_nonce.to_lower_hex_string())
            .field("auth_delay", &self.auth_delay)
            .finish()
    }
}

#[cfg(feature = "emulator")]
#[cfg(test)]
mod test {
    use crate::emulator::find_emulator;
    use crate::emulator::test::{CardTypeOption, EcardSubprocess};
    use crate::tap_signer::TapSignerShared;
    use crate::{CkTapCard, rand_chaincode};
    use std::path::Path;

    // verify the xpub command works
    #[tokio::test]
    async fn test_tap_signer_xpub() {
        let card_type = CardTypeOption::TapSigner;
        let pipe_path = "/tmp/test-tapsigner-xpub-pipe";
        let pipe_path = Path::new(&pipe_path);
        let python = EcardSubprocess::new(pipe_path, &card_type).unwrap();
        let emulator = find_emulator(pipe_path).await.unwrap();
        if let CkTapCard::TapSigner(mut ts) = emulator {
            ts.init(rand_chaincode(), "123456").await.unwrap();
            let xpub = ts.xpub(false, "123456").await.unwrap();
            assert_eq!(xpub.depth, 3);
            let master_xpub = ts.xpub(true, "123456").await.unwrap();
            assert_eq!(master_xpub.depth, 0);
        }
        drop(python);
    }
}