bhwi-async 0.1.0

Async HWI trait over the bhwi sans-IO Bitcoin hardware wallet interpreters
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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
//! Asynchronous hardware-wallet operations over caller-provided transports.
//!
//! [`HWI`] preserves backend error types; [`HWIDevice`] provides an object-safe,
//! error-erased interface. The asynchronous traits use `async_trait(?Send)`:
//! their futures need not be `Send`, and no particular async runtime is required.
//!
//! Shared command types are reexported from `bhwi::common`, including typed
//! host requests and responses for [`HostInteraction`] implementations.

/// BitBox02 sessions and pairing.
#[cfg(feature = "bitbox")]
pub mod bitbox;
/// Coldcard sessions.
#[cfg(feature = "coldcard")]
pub mod coldcard;
/// Standard descriptor and keypool construction.
pub mod descriptors;
/// Device discovery, selection, and cached metadata.
pub mod device;
/// Address-display requests prepared from descriptors.
pub mod display_address;
/// Jade sessions and PIN-server integration.
#[cfg(feature = "jade")]
pub mod jade;
/// KeepKey sessions and host interaction.
#[cfg(feature = "keepkey")]
pub mod keepkey;
/// Ledger sessions.
#[cfg(feature = "ledger")]
pub mod ledger;
/// Device-specific setup, recovery, and PIN contexts.
#[cfg(any(feature = "bitbox", feature = "keepkey", feature = "trezor"))]
pub mod management;
/// PSBT input cleanup and signature accumulation.
pub mod psbt;
/// Message-signature encoding and device-specific signing preparation.
pub mod signing;
#[cfg(feature = "specter")]
pub mod specter;
/// Framing adapters over caller-provided I/O channels.
pub mod transport;
/// Trezor sessions and host interaction.
#[cfg(feature = "trezor")]
pub mod trezor;

use std::{error::Error as StdError, fmt::Debug, str::FromStr};

use async_trait::async_trait;
pub use bhwi::common::DeviceBackup;
pub use bhwi::common::DeviceContext;
pub use bhwi::common::DisplayAddress;
pub use bhwi::common::ErrorKind;
pub use bhwi::common::Info;
pub use bhwi::common::RestoreOptions;
pub use bhwi::common::SetupOptions;
pub use bhwi::common::WalletRegistration;
pub use bhwi::common::{Error as HostError, HostRequest, HostResponse, PinMatrixRequestKind};
pub use bhwi::common::{MultisigAddressType, MultisigDisplayAddress};
use bhwi::miniscript::descriptor::WalletPolicy;
#[cfg(any(feature = "trezor", feature = "keepkey"))]
pub use bhwi::passphrase::HostPassphrase;
use bhwi::{
    Interpreter,
    bitcoin::{
        Network,
        bip32::{DerivationPath, Fingerprint, Xpub},
        psbt::Psbt,
        secp256k1::ecdsa::Signature,
    },
    common::{self},
};
/// The asynchronous Jade client.
#[cfg(feature = "jade")]
pub use jade::Jade;
/// The asynchronous KeepKey client.
#[cfg(feature = "keepkey")]
pub use keepkey::KeepKey;
/// The asynchronous Ledger client.
#[cfg(feature = "ledger")]
pub use ledger::Ledger;
/// The asynchronous Specter-DIY client.
#[cfg(feature = "specter")]
pub use specter::Specter;
/// The asynchronous Trezor client.
#[cfg(feature = "trezor")]
pub use trezor::Trezor;

/// Exchanges already-encoded interpreter payloads with a device.
///
/// Implementations provide framing and I/O, not interpreter-level encryption.
/// Futures need not be `Send`; the trait does not select an async runtime.
#[async_trait(?Send)]
pub trait Transport {
    /// An I/O or framing failure.
    type Error: Debug;
    /// Sends a payload and returns the device response.
    ///
    /// `encrypted` identifies an already-encrypted payload for framing purposes;
    /// it is not a request to encrypt `command` again.
    async fn exchange(&mut self, command: &[u8], encrypted: bool) -> Result<Vec<u8>, Self::Error>;

    /// Returns whether the backend classifies `error` as a post-write read failure.
    ///
    /// Commands that allow a final disconnect may treat this classification as
    /// success. BitBox includes timeouts and short reports; `true` proves neither
    /// that the device disconnected nor that its action completed.
    fn is_post_write_disconnect(&self, _error: &Self::Error) -> bool {
        false
    }

    /// Returns the kind of `error`; defaults to [`ErrorKind::Transport`].
    fn error_kind(&self, _error: &Self::Error) -> ErrorKind {
        ErrorKind::Transport
    }
}

/// Sends interpreter PIN-server requests using caller-provided HTTP I/O.
///
/// Futures need not be `Send`; the trait does not select an async runtime.
#[async_trait(?Send)]
pub trait HttpClient {
    /// An HTTP request failure.
    type Error: Debug;
    /// Sends the encoded request to `url` and returns the encoded response body.
    async fn request(&self, url: &str, request: &[u8]) -> Result<Vec<u8>, Self::Error>;

    /// Returns the kind of `error`; defaults to [`ErrorKind::Transport`].
    fn error_kind(&self, _error: &Self::Error) -> ErrorKind {
        ErrorKind::Transport
    }
}

/// Collects typed user input requested by a device.
///
/// Futures need not be `Send`. The command driver validates and encodes returned
/// responses with [`HostResponse::into_bytes_for`].
#[async_trait(?Send)]
pub trait HostInteraction {
    /// Returns input for `request`, or an error such as cancellation.
    async fn respond(
        &mut self,
        request: &common::HostRequest,
    ) -> Result<common::HostResponse, common::Error>;
}

/// Asynchronous wallet operations with a backend-specific error type.
///
/// Support and required [`DeviceContext`] values vary by device. Unsupported
/// operations and missing context return errors. Futures need not be `Send`,
/// and the trait does not select an async runtime.
#[async_trait(?Send)]
pub trait HWI {
    /// A device, transport, or host-interaction failure.
    type Error: Debug;
    /// Requests a device backup, which need not produce a downloadable file.
    async fn backup_device(&mut self) -> Result<DeviceBackup, Self::Error>;
    /// Initializes a device using options and any required caller-supplied context.
    async fn setup_device(
        &mut self,
        options: SetupOptions,
        context: Option<DeviceContext>,
    ) -> Result<bool, Self::Error>;
    /// Requests a device wipe and returns the reported or assumed outcome.
    ///
    /// Qualifying post-write read errors are treated as `Ok(true)` without
    /// confirming the wipe. On BitBox this includes timeouts and short reports,
    /// even on the initial information query before the wipe request is sent.
    async fn wipe_device(&mut self) -> Result<bool, Self::Error>;
    /// Restores a device using options and any required recovery context.
    async fn restore_device(
        &mut self,
        options: RestoreOptions,
        context: Option<DeviceContext>,
    ) -> Result<bool, Self::Error>;
    /// Requests a passphrase-protection toggle and returns the backend's outcome.
    ///
    /// KeepKey can return `true` while awaiting PIN entry, before the setting changes.
    async fn toggle_passphrase(&mut self) -> Result<bool, Self::Error>;
    /// Requests PIN entry and returns whether the prompt was accepted.
    async fn prompt_pin(&mut self) -> Result<bool, Self::Error>;
    /// Submits scrambled keypad positions supplied in the device-specific context.
    async fn send_pin(&mut self, context: Option<DeviceContext>) -> Result<bool, Self::Error>;
    /// Runs the backend's initialization or unlock handshake for `network`.
    ///
    /// This may involve device or host interaction. Trezor and KeepKey initialization
    /// can succeed while a PIN is still required; `Ok(())` does not guarantee an
    /// unlocked wallet.
    async fn unlock(&mut self, network: Network) -> Result<(), Self::Error>;
    /// Returns device information, which may be synthesized by the backend.
    async fn get_info(&mut self) -> Result<Info, Self::Error>;
    /// Returns the master fingerprint of the currently selected wallet.
    async fn get_master_fingerprint(&mut self) -> Result<Fingerprint, Self::Error>;
    /// Derives an extended public key, optionally requesting on-device display.
    async fn get_extended_pubkey(
        &mut self,
        path: DerivationPath,
        display: bool,
    ) -> Result<Xpub, Self::Error>;
    /// Signs a message and returns the backend's header and ECDSA signature.
    async fn sign_message(
        &mut self,
        message: &[u8],
        path: DerivationPath,
    ) -> Result<(u8, Signature), Self::Error>;
    /// Displays an address using any backend-required policy or registration context.
    async fn display_address(
        &mut self,
        address: common::DisplayAddress,
        context: Option<common::DeviceContext>,
    ) -> Result<String, Self::Error>;
    /// Registers a named wallet policy, possibly leaving user confirmation pending.
    async fn register_wallet(
        &mut self,
        name: &str,
        policy: &str,
    ) -> Result<WalletRegistration, Self::Error>;
    /// Signs a PSBT using any backend-required context and returns the updated PSBT.
    ///
    /// Ledger requires a Ledger context even for a policy without a registration HMAC.
    async fn sign_tx(
        &mut self,
        psbt: Psbt,
        context: Option<common::DeviceContext>,
    ) -> Result<Psbt, Self::Error>;
}

// TODO: this will become a pain to maintain, but we can have a proc-macro
// generate this trait by putting it over HWI's definition and then also
// generate the blanket impl which will map the errors to HWIDeviceError
/// Object-safe wallet operations with errors erased into [`HWIDeviceError`].
///
/// The blanket implementation delegates to [`HWI`]. Support and context
/// requirements remain backend-specific; futures need not be `Send`.
#[async_trait(?Send)]
pub trait HWIDevice {
    /// Requests a device backup, which need not produce a downloadable file.
    async fn backup_device(&mut self) -> Result<DeviceBackup, HWIDeviceError>;
    /// Initializes a device using options and any required caller-supplied context.
    async fn setup_device(
        &mut self,
        options: SetupOptions,
        context: Option<DeviceContext>,
    ) -> Result<bool, HWIDeviceError>;
    /// Requests a device wipe and returns the reported or assumed outcome.
    ///
    /// Qualifying post-write read errors are treated as `Ok(true)` without
    /// confirming the wipe. On BitBox this includes timeouts and short reports,
    /// even on the initial information query before the wipe request is sent.
    async fn wipe_device(&mut self) -> Result<bool, HWIDeviceError>;
    /// Restores a device using options and any required recovery context.
    async fn restore_device(
        &mut self,
        options: RestoreOptions,
        context: Option<DeviceContext>,
    ) -> Result<bool, HWIDeviceError>;
    /// Requests a passphrase-protection toggle and returns the backend's outcome.
    ///
    /// KeepKey can return `true` while awaiting PIN entry, before the setting changes.
    async fn toggle_passphrase(&mut self) -> Result<bool, HWIDeviceError>;
    /// Requests PIN entry and returns whether the prompt was accepted.
    async fn prompt_pin(&mut self) -> Result<bool, HWIDeviceError>;
    /// Submits scrambled keypad positions supplied in the device-specific context.
    async fn send_pin(&mut self, context: Option<DeviceContext>) -> Result<bool, HWIDeviceError>;
    /// Runs the backend's initialization or unlock handshake for `network`.
    ///
    /// This may involve device or host interaction. Trezor and KeepKey initialization
    /// can succeed while a PIN is still required; `Ok(())` does not guarantee an
    /// unlocked wallet.
    async fn unlock(&mut self, network: Network) -> Result<(), HWIDeviceError>;
    /// Returns device information, which may be synthesized by the backend.
    async fn get_info(&mut self) -> Result<Info, HWIDeviceError>;
    /// Returns the master fingerprint of the currently selected wallet.
    async fn get_master_fingerprint(&mut self) -> Result<Fingerprint, HWIDeviceError>;
    /// Derives an extended public key, optionally requesting on-device display.
    async fn get_extended_pubkey(
        &mut self,
        path: DerivationPath,
        display: bool,
    ) -> Result<Xpub, HWIDeviceError>;
    /// Signs a message and returns the backend's header and ECDSA signature.
    async fn sign_message(
        &mut self,
        message: &[u8],
        path: DerivationPath,
    ) -> Result<(u8, Signature), HWIDeviceError>;
    /// Displays an address using any backend-required policy or registration context.
    async fn display_address(
        &mut self,
        address: common::DisplayAddress,
        context: Option<common::DeviceContext>,
    ) -> Result<String, HWIDeviceError>;
    /// Registers a named wallet policy, possibly leaving user confirmation pending.
    async fn register_wallet(
        &mut self,
        name: &str,
        policy: &str,
    ) -> Result<WalletRegistration, HWIDeviceError>;
    /// Signs a PSBT using any backend-required context and returns the updated PSBT.
    ///
    /// Ledger requires a Ledger context even for a policy without a registration HMAC.
    async fn sign_tx(
        &mut self,
        psbt: Psbt,
        context: Option<common::DeviceContext>,
    ) -> Result<Psbt, HWIDeviceError>;
}

/// An erased wallet-operation error that retains the original error as its source.
#[derive(Debug, thiserror::Error)]
#[error("{error}")]
pub struct HWIDeviceError {
    #[source]
    error: Box<dyn StdError + Send + Sync + 'static>,
    kind: Option<ErrorKind>,
}

impl HWIDeviceError {
    /// Wraps a backend error while retaining its error chain.
    pub fn new(error: impl StdError + Send + Sync + 'static) -> Self {
        Self {
            error: Box::new(error),
            kind: None,
        }
    }

    /// Creates an erased error with an explicit kind.
    pub fn with_kind(error: impl StdError + Send + Sync + 'static, kind: ErrorKind) -> Self {
        Self {
            kind: Some(kind),
            ..Self::new(error)
        }
    }

    /// Returns the kind set on this error, or else the first one in its source chain.
    pub fn kind(&self) -> Option<ErrorKind> {
        if self.kind.is_some() {
            return self.kind;
        }
        let mut source: Option<&(dyn StdError + 'static)> = Some(self.error.as_ref());
        while let Some(current) = source {
            if let Some(error) = current.downcast_ref::<common::Error>() {
                return Some(error.kind());
            }
            if let Some(kind) = current
                .downcast_ref::<HWIDeviceError>()
                .and_then(Self::kind)
            {
                return Some(kind);
            }
            source = current.source();
        }
        None
    }
}

impl From<Box<dyn StdError + Send + Sync + 'static>> for HWIDeviceError {
    fn from(error: Box<dyn StdError + Send + Sync + 'static>) -> Self {
        Self { error, kind: None }
    }
}

/// An error that reports its [`ErrorKind`].
pub trait ErrorKindOf {
    /// Returns the error's kind.
    fn error_kind(&self) -> ErrorKind;
}

impl ErrorKindOf for common::Error {
    fn error_kind(&self) -> ErrorKind {
        self.kind()
    }
}

/// A failure while driving a common command through its interpreter and I/O.
#[derive(Debug, thiserror::Error)]
pub enum Error<E, F> {
    /// A device transport failure.
    #[error("transport error: {error}")]
    Transport {
        /// The failure kind.
        kind: ErrorKind,
        /// The transport's error.
        error: E,
    },

    /// A PIN-server HTTP failure.
    #[error("http client error: {error}")]
    HttpClient {
        /// The failure kind.
        kind: ErrorKind,
        /// The HTTP client's error.
        error: F,
    },

    /// An interpreter or host-interaction failure.
    #[error("{0}")]
    Interpreter(
        /// The command or input error.
        #[from]
        common::Error,
    ),
}

impl<E, F> Error<E, F> {
    /// Returns the failure kind from whichever layer failed.
    pub fn kind(&self) -> ErrorKind {
        match self {
            Self::Transport { kind, .. } | Self::HttpClient { kind, .. } => *kind,
            Self::Interpreter(error) => error.kind(),
        }
    }
}

impl<E, F> ErrorKindOf for Error<E, F> {
    fn error_kind(&self) -> ErrorKind {
        self.kind()
    }
}

fn kinded<E>(error: E) -> HWIDeviceError
where
    E: ErrorKindOf + StdError + Send + Sync + 'static,
{
    let kind = error.error_kind();
    HWIDeviceError::with_kind(error, kind)
}

#[async_trait(?Send)]
impl<D> HWI for D
where
    D: CommonInterface<common::Command, common::Transmit, common::Response, common::Error>
        + OnUnlock,
{
    type Error = Error<D::TransportError, D::HttpClientError>;
    async fn backup_device(&mut self) -> Result<DeviceBackup, Self::Error> {
        if let common::Response::Backup(backup) = run_command(self, common::Command::Backup).await?
        {
            Ok(backup)
        } else {
            Err(
                common::Error::new(ErrorKind::UnexpectedResponse, "no error or result returned")
                    .into(),
            )
        }
    }

    async fn setup_device(
        &mut self,
        options: SetupOptions,
        context: Option<DeviceContext>,
    ) -> Result<bool, Self::Error> {
        if let common::Response::DeviceAction(success) =
            run_command(self, common::Command::Setup(options, context)).await?
        {
            Ok(success)
        } else {
            Err(
                common::Error::new(ErrorKind::UnexpectedResponse, "no error or result returned")
                    .into(),
            )
        }
    }

    async fn wipe_device(&mut self) -> Result<bool, Self::Error> {
        if let common::Response::DeviceAction(success) =
            run_command_allowing_final_disconnect(self, common::Command::Wipe).await?
        {
            Ok(success)
        } else {
            Err(
                common::Error::new(ErrorKind::UnexpectedResponse, "no error or result returned")
                    .into(),
            )
        }
    }

    async fn restore_device(
        &mut self,
        options: RestoreOptions,
        context: Option<DeviceContext>,
    ) -> Result<bool, Self::Error> {
        if let common::Response::DeviceAction(success) =
            run_command(self, common::Command::Restore(options, context)).await?
        {
            Ok(success)
        } else {
            Err(
                common::Error::new(ErrorKind::UnexpectedResponse, "no error or result returned")
                    .into(),
            )
        }
    }

    async fn toggle_passphrase(&mut self) -> Result<bool, Self::Error> {
        if let common::Response::DeviceAction(success) =
            run_command(self, common::Command::TogglePassphrase).await?
        {
            Ok(success)
        } else {
            Err(
                common::Error::new(ErrorKind::UnexpectedResponse, "no error or result returned")
                    .into(),
            )
        }
    }

    async fn prompt_pin(&mut self) -> Result<bool, Self::Error> {
        if let common::Response::DeviceAction(success) =
            run_command(self, common::Command::PromptPin).await?
        {
            Ok(success)
        } else {
            Err(
                common::Error::new(ErrorKind::UnexpectedResponse, "no error or result returned")
                    .into(),
            )
        }
    }

    async fn send_pin(&mut self, context: Option<DeviceContext>) -> Result<bool, Self::Error> {
        if let common::Response::DeviceAction(success) =
            run_command(self, common::Command::SendPin(context)).await?
        {
            Ok(success)
        } else {
            Err(
                common::Error::new(ErrorKind::UnexpectedResponse, "no error or result returned")
                    .into(),
            )
        }
    }

    async fn unlock(&mut self, network: Network) -> Result<(), Self::Error> {
        let res = run_command(
            self,
            common::Command::Unlock {
                options: common::UnlockOptions {
                    network: Some(network),
                },
            },
        )
        .await?;
        self.on_unlock(res)?;
        Ok(())
    }

    async fn get_info(&mut self) -> Result<Info, Self::Error> {
        if let common::Response::Info(version) =
            run_command(self, common::Command::GetVersion).await?
        {
            Ok(version)
        } else {
            Err(
                common::Error::new(ErrorKind::UnexpectedResponse, "no error or result returned")
                    .into(),
            )
        }
    }

    async fn get_master_fingerprint(&mut self) -> Result<Fingerprint, Self::Error> {
        if let common::Response::MasterFingerprint(fg) =
            run_command(self, common::Command::GetMasterFingerprint).await?
        {
            Ok(fg)
        } else {
            Err(
                common::Error::new(ErrorKind::UnexpectedResponse, "no error or result returned")
                    .into(),
            )
        }
    }

    async fn get_extended_pubkey(
        &mut self,
        path: DerivationPath,
        display: bool,
    ) -> Result<Xpub, Self::Error> {
        if let common::Response::Xpub(xpub) =
            run_command(self, common::Command::GetXpub { path, display }).await?
        {
            Ok(xpub)
        } else {
            Err(
                common::Error::new(ErrorKind::UnexpectedResponse, "no error or result returned")
                    .into(),
            )
        }
    }

    async fn sign_message(
        &mut self,
        message: &[u8],
        path: DerivationPath,
    ) -> Result<(u8, Signature), Self::Error> {
        if let common::Response::Signature(header, signature) = run_command(
            self,
            common::Command::SignMessage {
                message: message.to_vec(),
                path,
            },
        )
        .await?
        {
            Ok((header, signature))
        } else {
            Err(
                common::Error::new(ErrorKind::UnexpectedResponse, "no error or result returned")
                    .into(),
            )
        }
    }

    async fn display_address(
        &mut self,
        address: common::DisplayAddress,
        context: Option<common::DeviceContext>,
    ) -> Result<String, Self::Error> {
        if let common::Response::Address(addr) =
            run_command(self, common::Command::DisplayAddress(address, context)).await?
        {
            Ok(addr)
        } else {
            Err(
                common::Error::new(ErrorKind::UnexpectedResponse, "no error or result returned")
                    .into(),
            )
        }
    }

    async fn register_wallet(
        &mut self,
        name: &str,
        policy: &str,
    ) -> Result<WalletRegistration, Self::Error> {
        let wallet_policy = WalletPolicy::from_str(policy)
            .map_err(|e| common::Error::new(ErrorKind::Serialization, e.to_string()))?;
        if let common::Response::WalletRegistration(registration) = run_command(
            self,
            common::Command::RegisterWallet {
                name: name.to_string(),
                policy: wallet_policy,
            },
        )
        .await?
        {
            Ok(registration)
        } else {
            Err(
                common::Error::new(ErrorKind::UnexpectedResponse, "no error or result returned")
                    .into(),
            )
        }
    }

    async fn sign_tx(
        &mut self,
        psbt: Psbt,
        context: Option<common::DeviceContext>,
    ) -> Result<Psbt, Self::Error> {
        if let common::Response::SignedPsbt(psbt) =
            run_command(self, common::Command::SignTx(psbt, context)).await?
        {
            Ok(psbt)
        } else {
            Err(
                common::Error::new(ErrorKind::UnexpectedResponse, "no error or result returned")
                    .into(),
            )
        }
    }
}

#[async_trait(?Send)]
impl<T> HWIDevice for T
where
    T: HWI,
    T::Error: ErrorKindOf + StdError + Send + Sync + 'static,
{
    async fn backup_device(&mut self) -> Result<DeviceBackup, HWIDeviceError> {
        HWI::backup_device(self).await.map_err(kinded)
    }

    async fn setup_device(
        &mut self,
        options: SetupOptions,
        context: Option<DeviceContext>,
    ) -> Result<bool, HWIDeviceError> {
        HWI::setup_device(self, options, context)
            .await
            .map_err(kinded)
    }

    async fn wipe_device(&mut self) -> Result<bool, HWIDeviceError> {
        HWI::wipe_device(self).await.map_err(kinded)
    }

    async fn restore_device(
        &mut self,
        options: RestoreOptions,
        context: Option<DeviceContext>,
    ) -> Result<bool, HWIDeviceError> {
        HWI::restore_device(self, options, context)
            .await
            .map_err(kinded)
    }

    async fn toggle_passphrase(&mut self) -> Result<bool, HWIDeviceError> {
        HWI::toggle_passphrase(self).await.map_err(kinded)
    }

    async fn prompt_pin(&mut self) -> Result<bool, HWIDeviceError> {
        HWI::prompt_pin(self).await.map_err(kinded)
    }

    async fn send_pin(&mut self, context: Option<DeviceContext>) -> Result<bool, HWIDeviceError> {
        HWI::send_pin(self, context).await.map_err(kinded)
    }

    async fn unlock(&mut self, network: Network) -> Result<(), HWIDeviceError> {
        HWI::unlock(self, network).await.map_err(kinded)
    }

    async fn get_info(&mut self) -> Result<Info, HWIDeviceError> {
        HWI::get_info(self).await.map_err(kinded)
    }

    async fn get_master_fingerprint(&mut self) -> Result<Fingerprint, HWIDeviceError> {
        HWI::get_master_fingerprint(self).await.map_err(kinded)
    }

    async fn get_extended_pubkey(
        &mut self,
        path: DerivationPath,
        display: bool,
    ) -> Result<Xpub, HWIDeviceError> {
        HWI::get_extended_pubkey(self, path, display)
            .await
            .map_err(kinded)
    }

    async fn sign_message(
        &mut self,
        message: &[u8],
        path: DerivationPath,
    ) -> Result<(u8, Signature), HWIDeviceError> {
        HWI::sign_message(self, message, path).await.map_err(kinded)
    }

    async fn display_address(
        &mut self,
        address: DisplayAddress,
        context: Option<DeviceContext>,
    ) -> Result<String, HWIDeviceError> {
        HWI::display_address(self, address, context)
            .await
            .map_err(kinded)
    }

    async fn register_wallet(
        &mut self,
        name: &str,
        policy: &str,
    ) -> Result<WalletRegistration, HWIDeviceError> {
        HWI::register_wallet(self, name, policy)
            .await
            .map_err(kinded)
    }

    async fn sign_tx(
        &mut self,
        psbt: Psbt,
        context: Option<common::DeviceContext>,
    ) -> Result<Psbt, HWIDeviceError> {
        HWI::sign_tx(self, psbt, context).await.map_err(kinded)
    }
}

/// Updates persistent client state after the unlock interpreter completes.
pub trait OnUnlock {
    /// Applies the unlock response, or returns an error if it cannot be accepted.
    fn on_unlock(&mut self, _response: common::Response) -> Result<(), common::Error>;
}

/// Supplies the I/O endpoints and interpreter used to drive a command.
pub trait CommonInterface<C, T, R, E> {
    /// The device transport's error type.
    type TransportError: Debug;
    /// The PIN-server client's error type.
    type HttpClientError: Debug;

    /// Borrows the transport, HTTP client, optional host input, and a fresh interpreter.
    ///
    /// Session state may be borrowed by the interpreter and retained between commands.
    #[allow(clippy::type_complexity)]
    fn components(
        &mut self,
    ) -> (
        &mut (dyn Transport<Error = Self::TransportError> + '_),
        &(dyn HttpClient<Error = Self::HttpClientError> + '_),
        Option<&mut (dyn HostInteraction + 'static)>,
        impl Interpreter<Command = C, Transmit = T, Response = R, Error = E>,
    );
}

async fn run_command<D, C, E, F>(
    device: &mut D,
    command: C,
) -> Result<common::Response, Error<E, F>>
where
    E: Debug,
    F: Debug,
    D: CommonInterface<
            common::Command,
            common::Transmit,
            common::Response,
            common::Error,
            TransportError = E,
            HttpClientError = F,
        >,
    C: Into<common::Command>,
{
    run_command_inner(device, command, false).await
}

async fn run_command_allowing_final_disconnect<D, C, E, F>(
    device: &mut D,
    command: C,
) -> Result<common::Response, Error<E, F>>
where
    E: Debug,
    F: Debug,
    D: CommonInterface<
            common::Command,
            common::Transmit,
            common::Response,
            common::Error,
            TransportError = E,
            HttpClientError = F,
        >,
    C: Into<common::Command>,
{
    run_command_inner(device, command, true).await
}

async fn run_command_inner<D, C, E, F>(
    device: &mut D,
    command: C,
    allow_final_disconnect: bool,
) -> Result<common::Response, Error<E, F>>
where
    E: Debug,
    F: Debug,
    D: CommonInterface<
            common::Command,
            common::Transmit,
            common::Response,
            common::Error,
            TransportError = E,
            HttpClientError = F,
        >,
    C: Into<common::Command>,
{
    let (transport, http_client, mut host_interaction, mut intpr) = device.components();
    let mut transmit = Some(intpr.start(command.into())?);
    while let Some(t) = &transmit {
        match &t.recipient {
            common::Recipient::PinServer { url } => {
                let res = http_client
                    .request(url, &t.payload)
                    .await
                    .map_err(|error| Error::HttpClient {
                        kind: http_client.error_kind(&error),
                        error,
                    })?;
                transmit = intpr.exchange(res)?;
            }
            common::Recipient::Host(request) => {
                let interaction = host_interaction.as_deref_mut().ok_or_else(|| {
                    common::Error::new(ErrorKind::Unsupported, "host interaction required")
                })?;
                let response = interaction.respond(request).await?;
                transmit = intpr.exchange(response.into_bytes_for(request)?)?;
            }
            common::Recipient::Device => {
                let exchange = match transport.exchange(&t.payload, t.encrypted).await {
                    Ok(exchange) => exchange,
                    Err(error)
                        if allow_final_disconnect && transport.is_post_write_disconnect(&error) =>
                    {
                        return Ok(common::Response::DeviceAction(true));
                    }
                    Err(error) => {
                        return Err(Error::Transport {
                            kind: transport.error_kind(&error),
                            error,
                        });
                    }
                };
                transmit = intpr.exchange(exchange)?;
            }
        }
    }
    intpr.end().map_err(|e| e.into())
}

#[cfg(test)]
mod tests {
    use super::*;
    use futures::executor::block_on;
    use std::{
        cell::{Cell, RefCell},
        convert::Infallible,
        rc::Rc,
    };

    struct ScriptedTransport {
        calls: usize,
        events: Rc<RefCell<Vec<&'static str>>>,
    }

    #[async_trait(?Send)]
    impl Transport for ScriptedTransport {
        type Error = Infallible;

        async fn exchange(
            &mut self,
            command: &[u8],
            encrypted: bool,
        ) -> Result<Vec<u8>, Self::Error> {
            self.events.borrow_mut().push("device");
            self.calls += 1;
            assert_eq!(command, &[0x01]);
            assert!(!encrypted);
            Ok(vec![0x02])
        }
    }

    struct NoHttp;

    #[async_trait(?Send)]
    impl HttpClient for NoHttp {
        type Error = Infallible;

        async fn request(&self, _url: &str, _request: &[u8]) -> Result<Vec<u8>, Self::Error> {
            panic!("host interaction must not use HTTP")
        }
    }

    struct ScriptedHost {
        response: Option<common::HostResponse>,
        calls: Rc<Cell<usize>>,
        events: Rc<RefCell<Vec<&'static str>>>,
    }

    #[async_trait(?Send)]
    impl HostInteraction for ScriptedHost {
        async fn respond(
            &mut self,
            request: &common::HostRequest,
        ) -> Result<common::HostResponse, common::Error> {
            assert_eq!(
                request,
                &common::HostRequest::PinMatrix {
                    kind: common::PinMatrixRequestKind::Current,
                }
            );
            self.events.borrow_mut().push("host");
            self.calls.set(self.calls.get() + 1);
            Ok(self.response.take().expect("one scripted host response"))
        }
    }

    #[derive(Default)]
    struct ScriptedInterpreter {
        state: u8,
        host_first: bool,
    }

    impl Interpreter for ScriptedInterpreter {
        type Command = common::Command;
        type Transmit = common::Transmit;
        type Response = common::Response;
        type Error = common::Error;

        fn start(&mut self, command: Self::Command) -> Result<Self::Transmit, Self::Error> {
            assert!(matches!(command, common::Command::GetVersion));
            assert_eq!(self.state, 0);
            self.state = 1;
            if self.host_first {
                Ok(common::HostRequest::PinMatrix {
                    kind: common::PinMatrixRequestKind::Current,
                }
                .into())
            } else {
                Ok(vec![0x01].into())
            }
        }

        fn exchange(&mut self, data: Vec<u8>) -> Result<Option<Self::Transmit>, Self::Error> {
            match (self.host_first, self.state) {
                (false, 1) => {
                    assert_eq!(data.as_slice(), &[0x02]);
                    self.state = 2;
                    Ok(Some(
                        common::HostRequest::PinMatrix {
                            kind: common::PinMatrixRequestKind::Current,
                        }
                        .into(),
                    ))
                }
                (false, 2) => {
                    assert_eq!(data.as_slice(), b"123");
                    self.state = 3;
                    Ok(None)
                }
                (true, 1) => {
                    assert_eq!(data.as_slice(), b"123");
                    self.state = 2;
                    Ok(Some(vec![0x01].into()))
                }
                (true, 2) => {
                    assert_eq!(data.as_slice(), &[0x02]);
                    self.state = 3;
                    Ok(None)
                }
                _ => panic!("unexpected scripted interpreter state"),
            }
        }

        fn end(self) -> Result<Self::Response, Self::Error> {
            assert_eq!(self.state, 3);
            Ok(common::Response::DeviceAction(true))
        }
    }

    struct ScriptedDevice {
        transport: ScriptedTransport,
        host_interaction: Option<Box<dyn HostInteraction>>,
        host_first: bool,
    }

    impl CommonInterface<common::Command, common::Transmit, common::Response, common::Error>
        for ScriptedDevice
    {
        type TransportError = Infallible;
        type HttpClientError = Infallible;

        fn components(
            &mut self,
        ) -> (
            &mut (dyn Transport<Error = Self::TransportError> + '_),
            &(dyn HttpClient<Error = Self::HttpClientError> + '_),
            Option<&mut (dyn HostInteraction + 'static)>,
            impl Interpreter<
                Command = common::Command,
                Transmit = common::Transmit,
                Response = common::Response,
                Error = common::Error,
            >,
        ) {
            (
                &mut self.transport,
                &NoHttp,
                self.host_interaction.as_deref_mut(),
                ScriptedInterpreter {
                    host_first: self.host_first,
                    ..Default::default()
                },
            )
        }
    }

    fn scripted_device(
        response: Option<common::HostResponse>,
    ) -> (ScriptedDevice, Rc<Cell<usize>>) {
        scripted_device_with_sequence(response, false)
    }

    fn scripted_device_with_sequence(
        response: Option<common::HostResponse>,
        host_first: bool,
    ) -> (ScriptedDevice, Rc<Cell<usize>>) {
        let calls = Rc::new(Cell::new(0));
        let events = Rc::new(RefCell::new(Vec::new()));
        let host_interaction = response.map(|response| {
            Box::new(ScriptedHost {
                response: Some(response),
                calls: Rc::clone(&calls),
                events: Rc::clone(&events),
            }) as Box<dyn HostInteraction>
        });
        (
            ScriptedDevice {
                transport: ScriptedTransport { calls: 0, events },
                host_interaction,
                host_first,
            },
            calls,
        )
    }

    #[test]
    fn initial_host_recipient_uses_provider_before_device_exchange() {
        let (mut device, host_calls) = scripted_device_with_sequence(
            Some(common::HostResponse::PinPositions("123".into())),
            true,
        );

        let response = block_on(run_command_inner(
            &mut device,
            common::Command::GetVersion,
            false,
        ))
        .expect("scripted command succeeds");

        assert!(matches!(response, common::Response::DeviceAction(true)));
        assert_eq!(host_calls.get(), 1);
        assert_eq!(device.transport.calls, 1);
        assert_eq!(
            device.transport.events.borrow().as_slice(),
            &["host", "device"]
        );
    }

    #[test]
    fn host_recipient_uses_provider_without_second_device_exchange() {
        let (mut device, host_calls) =
            scripted_device(Some(common::HostResponse::PinPositions("123".into())));

        let response = block_on(run_command_inner(
            &mut device,
            common::Command::GetVersion,
            false,
        ))
        .expect("scripted command succeeds");

        assert!(matches!(response, common::Response::DeviceAction(true)));
        assert_eq!(device.transport.calls, 1);
        assert_eq!(host_calls.get(), 1);
    }

    #[test]
    fn host_recipient_requires_provider_without_second_device_exchange() {
        let (mut device, host_calls) = scripted_device(None);

        let error = match block_on(run_command_inner(
            &mut device,
            common::Command::GetVersion,
            false,
        )) {
            Err(error) => error,
            Ok(_) => panic!("missing host provider succeeds"),
        };

        assert!(matches!(
            error,
            Error::Interpreter(e) if e.kind() == common::ErrorKind::Unsupported
                && e.message() == "host interaction required"
        ));
        assert_eq!(device.transport.calls, 1);
        assert_eq!(host_calls.get(), 0);
    }

    #[test]
    fn host_recipient_validates_response_without_second_device_exchange() {
        let (mut device, host_calls) =
            scripted_device(Some(common::HostResponse::RecoveryCharacter('a')));

        let error = match block_on(run_command_inner(
            &mut device,
            common::Command::GetVersion,
            false,
        )) {
            Err(error) => error,
            Ok(_) => panic!("mismatched host response succeeds"),
        };

        assert!(matches!(
            error,
            Error::Interpreter(e) if e.kind() == common::ErrorKind::InvalidInput
                && e.message() == "host response does not match request"
        ));
        assert_eq!(device.transport.calls, 1);
        assert_eq!(host_calls.get(), 1);
    }
}