implement hwi varsion parse correctly
MODIFY
WalletWasabi.Tests/UnitTests/Hwi/DefaultResponseTests.cs
WalletWasabi.Tests/UnitTests/Hwi/DefaultResponseTests.cs
@@ -159,8 +159,12 @@ namespace WalletWasabi.Tests.UnitTests.Hwi
{
Version result = new Version(1, 1, 2);
Assert.Equal(accepted, HwiParser.TryParseVersion(versionResponse, out Version v1));
+
+ if (accepted)
+ {
Assert.Equal(result, v1);
}
+ }
#endregion Tests

MODIFY
WalletWasabi/Hwi/Parsers/HwiParser.cs
WalletWasabi/Hwi/Parsers/HwiParser.cs
@@ -287,81 +287,47 @@ namespace WalletWasabi.Hwi.Parsers
return rawPath.Replace(@"\\", @"\");
}
- public static bool TryParseVersion(string hwiResponse, string substringFrom, out Version version)
- {
- var setterIndex = 0;
- var versionStart = false;
- var versionStartIndex = 0;
- var versionEndIndex = 0;
-
- for (int i = 0; i < hwiResponse.Length; i++)
- {
- var chr = hwiResponse[i];
- if (!versionStart & char.IsDigit(chr))
- {
- versionStartIndex = i;
- versionStart = true;
- }
- else if (versionStart && chr == '.')
- {
- setterIndex++;
- }
- else if (versionStart && !char.IsDigit(chr) & setterIndex >= 2)
- {
- versionEndIndex = i;
- break;
- }
- }
-
- if (versionEndIndex - versionStartIndex <= 5) // 5 because `0.0.0` is the mininum valid semver
+ public static bool TryParseVersion(string hwiResponse, out Version version)
{
version = null;
- return false;
- }
-
- var verString = hwiResponse[versionStartIndex..versionEndIndex];
-
- if (Version.TryParse(verString, out var res))
+ try
{
- version = res;
+ version = ParseVersion(hwiResponse);
return true;
}
- else
+ catch (Exception)
{
- version = null;
return false;
}
}
- public static bool TryParseVersion(string hwiResponse, out Version version)
+ public static Version ParseVersion(string hwiResponse)
{
- version = null;
+ const string WinPrefix = "hwi.exe";
+ const string Prefix = "hwi";
// Order matters! https://github.com/zkSNACKs/WalletWasabi/pull/1905/commits/cecefcc50af140cc06cb93961cda86f9b21db11b
+ string prefixToTrim = hwiResponse.StartsWith(WinPrefix) ?
+ WinPrefix :
+ hwiResponse.StartsWith(Prefix) ?
+ Prefix :
+ null;
- // Example output: hwi.exe 1.0.1
- if (TryParseVersion(hwiResponse, "hwi.exe", out Version v2))
+ if (prefixToTrim is null)
{
- version = v2;
+ throw new FormatException("Hwi prefix is missing from the version response");
}
- // Example output: hwi 1.0.1
- if (TryParseVersion(hwiResponse, "hwi", out Version v1))
- {
- version = v1;
- }
+ hwiResponse = hwiResponse.TrimStart(prefixToTrim, StringComparison.InvariantCultureIgnoreCase).TrimEnd();
- return version != null;
- }
+ var onlyVersion = hwiResponse.Split("-")[0];
- public static Version ParseVersion(string hwiResponse)
- {
- if (TryParseVersion(hwiResponse, out Version version))
+ if (onlyVersion.Split('.').Length != 3)
{
- return version;
+ throw new FormatException("Version must contain major.minor.build numbers.");
}
- throw new FormatException($"Cannot parse version from HWI's response. Response: {hwiResponse}.");
+ return Version.Parse(onlyVersion);
}
public static string ToArgumentString(Network network, IEnumerable<HwiOption> options, HwiCommands? command, string commandArguments)

implement hwi varsion parse correctly