Replaced ifs with switch expression in HwiProcesssBridgeMock
MODIFY
WalletWasabi.Tests/UnitTests/Hwi/HwiProcessBridgeMock.cs
WalletWasabi.Tests/UnitTests/Hwi/HwiProcessBridgeMock.cs
@@ -28,35 +28,25 @@ namespace WalletWasabi.Tests.UnitTests.Hwi
string model;
string rawPath;
- if (Model == HardwareWalletModels.Trezor_T)
- {
- model = "trezor_t";
- rawPath = "webusb: 001:4";
- }
- else if (Model == HardwareWalletModels.Trezor_1)
- {
- model = "trezor_1";
- rawPath = "hid:\\\\\\\\?\\\\hid#vid_534c&pid_0001&mi_00#7&6f0b727&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}";
- }
- else if (Model == HardwareWalletModels.Coldcard)
- {
- model = "coldcard";
- rawPath = @"\\\\?\\hid#vid_d13e&pid_cc10&mi_00#7&1b239988&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}";
- }
- else if (Model == HardwareWalletModels.Ledger_Nano_S)
- {
- model = "ledger_nano_s";
- rawPath = "\\\\\\\\?\\\\hid#vid_2c97&pid_0001&mi_00#7&e45ae20&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}";
- }
- else if (Model == HardwareWalletModels.Ledger_Nano_X)
- {
- model = "ledger_nano_x";
- rawPath = "\\\\\\\\?\\\\hid#vid_2c97&pid_0001&mi_00#7&e45ae20&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}";
- }
- else
- {
- throw new NotImplementedException("Mock missing.");
- }
+ model = Model switch
+ {
+ HardwareWalletModels.Trezor_T => "trezor_t",
+ HardwareWalletModels.Trezor_1 => "trezor_1",
+ HardwareWalletModels.Coldcard => "coldcard",
+ HardwareWalletModels.Ledger_Nano_S => "ledger_nano_s",
+ HardwareWalletModels.Ledger_Nano_X => "ledger_nano_x",
+ _ => throw new NotImplementedException("Mock missing.")
+ };
+
+ rawPath = Model switch
+ {
+ HardwareWalletModels.Trezor_T => "webusb: 001:4",
+ HardwareWalletModels.Trezor_1 => "hid:\\\\\\\\?\\\\hid#vid_534c&pid_0001&mi_00#7&6f0b727&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}",
+ HardwareWalletModels.Coldcard => @"\\\\?\\hid#vid_d13e&pid_cc10&mi_00#7&1b239988&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}",
+ HardwareWalletModels.Ledger_Nano_S => "\\\\\\\\?\\\\hid#vid_2c97&pid_0001&mi_00#7&e45ae20&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}",
+ HardwareWalletModels.Ledger_Nano_X => "\\\\\\\\?\\\\hid#vid_2c97&pid_0001&mi_00#7&e45ae20&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}",
+ _ => throw new NotImplementedException("Mock missing.")
+ };
string path = HwiParser.NormalizeRawDevicePath(rawPath);
string devicePathAndTypeArgumentString = $"--device-path \"{path}\" --device-type \"{model}\"";
@@ -68,140 +58,81 @@ namespace WalletWasabi.Tests.UnitTests.Hwi
if (CompareArguments(arguments, "enumerate"))
{
- if (Model == HardwareWalletModels.Trezor_T)
- {
- response = $"[{{\"model\": \"{model}\", \"path\": \"{rawPath}\", \"needs_pin_sent\": false, \"needs_passphrase_sent\": false, \"error\": \"Not initialized\"}}]";
- }
- else if (Model == HardwareWalletModels.Trezor_1)
- {
- response = $"[{{\"model\": \"{model}\", \"path\": \"{rawPath}\", \"needs_pin_sent\": true, \"needs_passphrase_sent\": false, \"error\": \"Could not open client or get fingerprint information: Trezor is locked. Unlock by using 'promptpin' and then 'sendpin'.\", \"code\": -12}}]\r\n";
- }
- else if (Model == HardwareWalletModels.Coldcard)
- {
- response = $"[{{\"model\": \"{model}\", \"path\": \"{rawPath}\", \"needs_passphrase\": false, \"fingerprint\": \"a3d0d797\"}}]\r\n";
- }
- else if (Model == HardwareWalletModels.Ledger_Nano_S)
- {
- response = $"[{{\"model\": \"{model}\", \"path\": \"{rawPath}\", \"fingerprint\": \"4054d6f6\", \"needs_pin_sent\": false, \"needs_passphrase_sent\": false}}]\r\n";
- }
- else if (Model == HardwareWalletModels.Ledger_Nano_X)
+ response = Model switch
{
- response = $"[{{\"model\": \"{model}\", \"path\": \"{rawPath}\", \"fingerprint\": \"4054d6f6\", \"needs_pin_sent\": false, \"needs_passphrase_sent\": false}}]\r\n";
- }
+ HardwareWalletModels.Trezor_T => $"[{{\"model\": \"{model}\", \"path\": \"{rawPath}\", \"needs_pin_sent\": false, \"needs_passphrase_sent\": false, \"error\": \"Not initialized\"}}]",
+ HardwareWalletModels.Trezor_1 => $"[{{\"model\": \"{model}\", \"path\": \"{rawPath}\", \"needs_pin_sent\": true, \"needs_passphrase_sent\": false, \"error\": \"Could not open client or get fingerprint information: Trezor is locked. Unlock by using 'promptpin' and then 'sendpin'.\", \"code\": -12}}]\r\n",
+ HardwareWalletModels.Coldcard => $"[{{\"model\": \"{model}\", \"path\": \"{rawPath}\", \"needs_passphrase\": false, \"fingerprint\": \"a3d0d797\"}}]\r\n",
+ HardwareWalletModels.Ledger_Nano_S => $"[{{\"model\": \"{model}\", \"path\": \"{rawPath}\", \"fingerprint\": \"4054d6f6\", \"needs_pin_sent\": false, \"needs_passphrase_sent\": false}}]\r\n",
+ HardwareWalletModels.Ledger_Nano_X => $"[{{\"model\": \"{model}\", \"path\": \"{rawPath}\", \"fingerprint\": \"4054d6f6\", \"needs_pin_sent\": false, \"needs_passphrase_sent\": false}}]\r\n",
+ _ => throw new NotImplementedException($"Mock missing for {model}")
+ };
}
else if (CompareArguments(arguments, $"{devicePathAndTypeArgumentString} wipe"))
{
- if (Model is HardwareWalletModels.Trezor_T or HardwareWalletModels.Trezor_1)
- {
- response = SuccessTrueResponse;
- }
- else if (Model == HardwareWalletModels.Coldcard)
- {
- response = "{\"error\": \"The Coldcard does not support wiping via software\", \"code\": -9}\r\n";
- }
- else if (Model == HardwareWalletModels.Ledger_Nano_S)
- {
- response = "{\"error\": \"The Ledger Nano S does not support wiping via software\", \"code\": -9}\r\n";
- }
- else if (Model == HardwareWalletModels.Ledger_Nano_X)
+ response = Model switch
{
- response = "{\"error\": \"The Ledger Nano X does not support wiping via software\", \"code\": -9}\r\n";
- }
+ HardwareWalletModels.Trezor_T or HardwareWalletModels.Trezor_1 => SuccessTrueResponse,
+ HardwareWalletModels.Coldcard => "{\"error\": \"The Coldcard does not support wiping via software\", \"code\": -9}\r\n",
+ HardwareWalletModels.Ledger_Nano_S => "{\"error\": \"The Ledger Nano S does not support wiping via software\", \"code\": -9}\r\n",
+ HardwareWalletModels.Ledger_Nano_X => "{\"error\": \"The Ledger Nano X does not support wiping via software\", \"code\": -9}\r\n",
+ _ => throw new NotImplementedException("Mock missing.")
+ };
}
else if (CompareArguments(arguments, $"{devicePathAndTypeArgumentString} setup"))
{
- if (Model is HardwareWalletModels.Trezor_T or HardwareWalletModels.Trezor_1)
- {
- response = "{\"error\": \"setup requires interactive mode\", \"code\": -9}";
- }
- else if (Model == HardwareWalletModels.Coldcard)
- {
- response = "{\"error\": \"The Coldcard does not support software setup\", \"code\": -9}\r\n";
- }
- else if (Model == HardwareWalletModels.Ledger_Nano_S)
- {
- response = "{\"error\": \"The Ledger Nano S does not support software setup\", \"code\": -9}\r\n";
- }
- else if (Model == HardwareWalletModels.Ledger_Nano_X)
+ response = Model switch
{
- response = "{\"error\": \"The Ledger Nano X does not support software setup\", \"code\": -9}\r\n";
- }
+ HardwareWalletModels.Trezor_T or HardwareWalletModels.Trezor_1 => "{\"error\": \"setup requires interactive mode\", \"code\": -9}",
+ HardwareWalletModels.Coldcard => "{\"error\": \"The Coldcard does not support software setup\", \"code\": -9}\r\n",
+ HardwareWalletModels.Ledger_Nano_S => "{\"error\": \"The Ledger Nano S does not support software setup\", \"code\": -9}\r\n",
+ HardwareWalletModels.Ledger_Nano_X => "{\"error\": \"The Ledger Nano X does not support software setup\", \"code\": -9}\r\n",
+ _ => throw new NotImplementedException("Mock missing.")
+ };
}
else if (CompareArguments(arguments, $"{devicePathAndTypeArgumentString} --interactive setup"))
{
- if (Model is HardwareWalletModels.Trezor_T or HardwareWalletModels.Trezor_1)
- {
- response = SuccessTrueResponse;
- }
- else if (Model == HardwareWalletModels.Coldcard)
- {
- response = "{\"error\": \"The Coldcard does not support software setup\", \"code\": -9}\r\n";
- }
- else if (Model == HardwareWalletModels.Ledger_Nano_S)
- {
- response = "{\"error\": \"The Ledger Nano S does not support software setup\", \"code\": -9}\r\n";
- }
- else if (Model == HardwareWalletModels.Ledger_Nano_X)
+ response = Model switch
{
- response = "{\"error\": \"The Ledger Nano X does not support software setup\", \"code\": -9}\r\n";
- }
+ HardwareWalletModels.Trezor_T or HardwareWalletModels.Trezor_1 => SuccessTrueResponse,
+ HardwareWalletModels.Coldcard => "{\"error\": \"The Coldcard does not support software setup\", \"code\": -9}\r\n",
+ HardwareWalletModels.Ledger_Nano_S => "{\"error\": \"The Ledger Nano S does not support software setup\", \"code\": -9}\r\n",
+ HardwareWalletModels.Ledger_Nano_X => "{\"error\": \"The Ledger Nano X does not support software setup\", \"code\": -9}\r\n",
+ _ => throw new NotImplementedException("Mock missing.")
+ };
}
else if (CompareArguments(arguments, $"{devicePathAndTypeArgumentString} --interactive restore"))
{
- if (Model is HardwareWalletModels.Trezor_T or HardwareWalletModels.Trezor_1)
- {
- response = SuccessTrueResponse;
- }
- else if (Model == HardwareWalletModels.Coldcard)
- {
- response = "{\"error\": \"The Coldcard does not support restoring via software\", \"code\": -9}\r\n";
- }
- else if (Model == HardwareWalletModels.Ledger_Nano_S)
- {
- response = "{\"error\": \"The Ledger Nano S does not support restoring via software\", \"code\": -9}\r\n";
- }
- else if (Model == HardwareWalletModels.Ledger_Nano_X)
+ response = Model switch
{
- response = "{\"error\": \"The Ledger Nano X does not support restoring via software\", \"code\": -9}\r\n";
- }
+ HardwareWalletModels.Trezor_T or HardwareWalletModels.Trezor_1 => SuccessTrueResponse,
+ HardwareWalletModels.Coldcard => "{\"error\": \"The Coldcard does not support restoring via software\", \"code\": -9}\r\n",
+ HardwareWalletModels.Ledger_Nano_S => "{\"error\": \"The Ledger Nano S does not support restoring via software\", \"code\": -9}\r\n",
+ HardwareWalletModels.Ledger_Nano_X => "{\"error\": \"The Ledger Nano X does not support restoring via software\", \"code\": -9}\r\n",
+ _ => throw new NotImplementedException("Mock missing.")
+ };
}
else if (CompareArguments(arguments, $"{devicePathAndTypeArgumentString} promptpin"))
{
- if (Model is HardwareWalletModels.Trezor_T or HardwareWalletModels.Trezor_1)
- {
- response = "{\"error\": \"The PIN has already been sent to this device\", \"code\": -11}\r\n";
- }
- else if (Model == HardwareWalletModels.Coldcard)
- {
- response = "{\"error\": \"The Coldcard does not need a PIN sent from the host\", \"code\": -9}\r\n";
- }
- else if (Model == HardwareWalletModels.Ledger_Nano_S)
- {
- response = "{\"error\": \"The Ledger Nano S does not need a PIN sent from the host\", \"code\": -9}\r\n";
- }
- else if (Model == HardwareWalletModels.Ledger_Nano_X)
+ response = Model switch
{
- response = "{\"error\": \"The Ledger Nano X does not need a PIN sent from the host\", \"code\": -9}\r\n";
- }
+ HardwareWalletModels.Trezor_T or HardwareWalletModels.Trezor_1 => "{\"error\": \"The PIN has already been sent to this device\", \"code\": -11}",
+ HardwareWalletModels.Coldcard => "{\"error\": \"The Coldcard does not need a PIN sent from the host\", \"code\": -9}\r\n",
+ HardwareWalletModels.Ledger_Nano_S => "{\"error\": \"The Ledger Nano S does not need a PIN sent from the host\", \"code\": -9}\r\n",
+ HardwareWalletModels.Ledger_Nano_X => "{\"error\": \"The Ledger Nano X does not need a PIN sent from the host\", \"code\": -9}\r\n",
+ _ => throw new NotImplementedException("Mock missing.")
+ };
}
else if (CompareArguments(arguments, $"{devicePathAndTypeArgumentString} sendpin", true))
{
- if (Model is HardwareWalletModels.Trezor_T or HardwareWalletModels.Trezor_1)
- {
- response = "{\"error\": \"The PIN has already been sent to this device\", \"code\": -11}";
- }
- else if (Model == HardwareWalletModels.Coldcard)
- {
- response = "{\"error\": \"The Coldcard does not need a PIN sent from the host\", \"code\": -9}\r\n";
- }
- else if (Model == HardwareWalletModels.Ledger_Nano_S)
- {
- response = "{\"error\": \"The Ledger Nano S does not need a PIN sent from the host\", \"code\": -9}\r\n";
- }
- else if (Model == HardwareWalletModels.Ledger_Nano_X)
+ response = Model switch
{
- response = "{\"error\": \"The Ledger Nano X does not need a PIN sent from the host\", \"code\": -9}\r\n";
- }
+ HardwareWalletModels.Trezor_T or HardwareWalletModels.Trezor_1 => "{\"error\": \"The PIN has already been sent to this device\", \"code\": -11}",
+ HardwareWalletModels.Coldcard => "{\"error\": \"The Coldcard does not need a PIN sent from the host\", \"code\": -9}\r\n",
+ HardwareWalletModels.Ledger_Nano_S => "{\"error\": \"The Ledger Nano S does not need a PIN sent from the host\", \"code\": -9}\r\n",
+ HardwareWalletModels.Ledger_Nano_X => "{\"error\": \"The Ledger Nano X does not need a PIN sent from the host\", \"code\": -9}\r\n",
+ _ => throw new NotImplementedException("Mock missing.")
+ };
}
else if (CompareGetXbpubArguments(arguments, out string? xpub))
{

Replaced ifs with switch expression in HwiProcesssBridgeMock