Skip to content

Commit 5b9ec5c

Browse files
committed
Validate device on constructor
1 parent a119b6e commit 5b9ec5c

File tree

5 files changed

+29
-11
lines changed

5 files changed

+29
-11
lines changed

include/tap_protocol/tap_protocol.h

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <vector>
66
#include <cstddef>
77
#include <string>
8+
#include <cstdint>
89

910
namespace tap_protocol {
1011
using Bytes = std::vector<unsigned char>;

src/satscard.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,12 @@ Satscard::Satscard(std::unique_ptr<Transport> transport)
125125
: CKTapCard(std::move(transport), false) {
126126
auto st = FirstLook();
127127

128+
if (st.tapsigner) {
129+
throw TapProtoException(
130+
TapProtoException::INVALID_DEVICE,
131+
"Incorrect device type detected. Please try again.");
132+
}
133+
128134
if (GetActiveSlotStatus() == SlotStatus::SEALED) {
129135
RenderActiveSlotAddress(st);
130136
}

src/tapsigner.cpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,12 @@ void from_json(const nlohmann::json& j, Tapsigner::BackupResponse& t) {
5858

5959
Tapsigner::Tapsigner(std::unique_ptr<Transport> transport)
6060
: CKTapCard(std::move(transport), false) {
61-
FirstLook();
61+
auto st = FirstLook();
62+
if (!st.tapsigner) {
63+
throw TapProtoException(
64+
TapProtoException::INVALID_DEVICE,
65+
"Incorrect device type detected. Please try again.");
66+
}
6267
CertificateCheck();
6368
}
6469

tests/connection_test.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,19 @@ TEST_CASE("type of card") {
5656
CHECK(st.proto == 1);
5757
}
5858
}
59+
60+
TEST_CASE("invalid card") {
61+
std::unique_ptr<tap_protocol::Transport> tp =
62+
std::make_unique<CardEmulator>();
63+
64+
tap_protocol::CKTapCard card(std::move(tp));
65+
if (card.IsTapsigner()) {
66+
CHECK_THROWS_AS(
67+
{ auto tapsigner = tap_protocol::ToSatscard(std::move(card)); },
68+
tap_protocol::TapProtoException);
69+
} else {
70+
CHECK_THROWS_AS(
71+
{ auto satscard = tap_protocol::ToTapsigner(std::move(card)); },
72+
tap_protocol::TapProtoException);
73+
}
74+
}

tests/tapsigner_test.cpp

-10
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,6 @@
1414
// $ ./ecard.py emulate -t
1515
// $ ./ecard.py emulate -t --no-init # fresh card
1616

17-
TEST_CASE("tapsigner to satscard") {
18-
std::unique_ptr<tap_protocol::Transport> tp =
19-
std::make_unique<CardEmulator>();
20-
tap_protocol::Tapsigner tapsigner(std::move(tp));
21-
if (!tapsigner.IsTapsigner()) {
22-
auto satscard = tap_protocol::ToSatscard(std::move(tapsigner));
23-
MESSAGE("satscard active slot: ", satscard->GetActiveSlotIndex());
24-
}
25-
}
26-
2717
TEST_SUITE_BEGIN("tapsigner" * doctest::skip([]() -> bool {
2818
std::unique_ptr<tap_protocol::Transport> tp =
2919
std::make_unique<CardEmulator>();

0 commit comments

Comments
 (0)