ctaphid_types/transaction.rs
1// Copyright (C) 2021 Robin Krahl <robin.krahl@ireas.org>
2// SPDX-License-Identifier: Apache-2.0 or MIT
3
4use crate::{channel::Channel, command::Command, message::Message};
5
6/// A CTAPHID transaction.
7///
8/// See [ยง 11.2.2 of the CTAP specification][spec].
9///
10/// [spec]: https://fidoalliance.org/specs/fido-v2.1-ps-20210615/fido-client-to-authenticator-protocol-v2.1-ps-20210615.html#usb-protocol-and-framing
11#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
12pub struct Transaction<T: AsRef<[u8]>, S: AsRef<[u8]>> {
13 /// The channel used for this transaction.
14 pub channel: Channel,
15 /// The command that is executed in this transaction.
16 pub command: Command,
17 /// The request payload.
18 pub request: T,
19 /// The response payload.
20 pub response: S,
21}
22
23impl<T: AsRef<[u8]>, S: AsRef<[u8]>> Transaction<T, S> {
24 /// Returns the request message of this transaction.
25 pub fn request_message(&self) -> Message<&[u8]> {
26 Message {
27 channel: self.channel,
28 command: self.command,
29 data: self.request.as_ref(),
30 }
31 }
32
33 /// Returns the response message of this transaction.
34 pub fn response_message(&self) -> Message<&[u8]> {
35 Message {
36 channel: self.channel,
37 command: self.command,
38 data: self.response.as_ref(),
39 }
40 }
41}