Implement GetVersionAsync
MODIFY
WalletWasabi.Tests/HwiTests/HwiClientTests.cs
WalletWasabi.Tests/HwiTests/HwiClientTests.cs
@@ -2,6 +2,8 @@ using NBitcoin;
using System;
using System.Collections.Generic;
using System.Text;
+using System.Threading;
+using System.Threading.Tasks;
using WalletWasabi.Hwi2;
using Xunit;
@@ -10,19 +12,44 @@ namespace WalletWasabi.Tests.HwiTests
public class HwiClientTests
{
[Theory]
- [InlineData("Main")]
- [InlineData("TestNet")]
- [InlineData("RegTest")]
- public void CanCreateHwiClient(string networkString)
+ [MemberData(nameof(GetDifferentNetworkValues))]
+ public void CanCreate(Network network)
{
- var network = Network.GetNetwork(networkString);
new HwiClient(network);
}
+ public static IEnumerable<object[]> GetDifferentNetworkValues()
+ {
+ var networks = new List<Network>
+ {
+ Network.Main,
+ Network.TestNet,
+ Network.RegTest
+ };
+
+ foreach (Network network in networks)
+ {
+ yield return new object[] { network };
+ }
+ }
+
[Fact]
- public void HwiClientConstructorThrowsArgumentNullException()
+ public void ConstructorThrowsArgumentNullException()
{
Assert.Throws<ArgumentNullException>(() => new HwiClient(null));
}
+
+ [Theory]
+ [MemberData(nameof(GetDifferentNetworkValues))]
+ public async Task CanGetVersionAsync(Network network)
+ {
+ var client = new HwiClient(network);
+
+ using (var cts = new CancellationTokenSource(3000))
+ {
+ Version version = await client.GetVersionAsync(cts.Token);
+ Assert.Equal(new Version("1.0.1"), version);
+ }
+ }
}
}

ADD
WalletWasabi/Hwi2/Binaries/hwi-win64/hwi.exe
WalletWasabi/Hwi2/Binaries/hwi-win64/hwi.exe
Binary files /dev/null and b/WalletWasabi/Hwi2/Binaries/hwi-win64/hwi.exe differ

MODIFY
WalletWasabi/Hwi2/HwiClient.cs
WalletWasabi/Hwi2/HwiClient.cs
using NBitcoin;
using System;
using System.Collections.Generic;
+using System.Diagnostics;
+using System.IO;
using System.Text;
+using System.Threading;
+using System.Threading.Tasks;
using WalletWasabi.Helpers;
namespace WalletWasabi.Hwi2
@@ -14,5 +18,62 @@ namespace WalletWasabi.Hwi2
{
Network = Guard.NotNull(nameof(network), network);
}
+
+ public async Task<Version> GetVersionAsync(CancellationToken cancel)
+ {
+ var fullBaseDirectory = Path.GetFullPath(AppContext.BaseDirectory);
+ var hwiPath = Path.Combine(fullBaseDirectory, "Hwi2", "Binaries", "hwi-win64", "hwi.exe");
+
+ string arguments = "--version";
+ using (var process = Process.Start(
+ new ProcessStartInfo
+ {
+ FileName = hwiPath,
+ Arguments = arguments,
+ RedirectStandardOutput = true,
+ UseShellExecute = false,
+ CreateNoWindow = true,
+ WindowStyle = ProcessWindowStyle.Hidden
+ }
+ ))
+ {
+ await process.WaitForExitAsync(cancel).ConfigureAwait(false);
+
+ if (process.ExitCode != 0)
+ {
+ throw new IOException($"'hwi {arguments}' exited with incorrect exit code.", process.ExitCode);
+ }
+
+ string responseString = await process.StandardOutput.ReadToEndAsync().ConfigureAwait(false);
+
+ // Example output: hwi 1.0.0
+ if (TryParseVersion(responseString, "hwi", out Version v1))
+ {
+ return v1;
+ }
+
+ // Example output: hwi.exe 1.0.0
+ if (TryParseVersion(responseString, "hwi.exe", out Version v2))
+ {
+ return v2;
+ }
+
+ throw new FormatException($"Cannot parse version from HWI's response. Response: {responseString}.");
+ }
+ }
+
+ public static bool TryParseVersion(string hwiResponse, string substringFrom, out Version version)
+ {
+ int startIndex = hwiResponse.IndexOf(substringFrom) + substringFrom.Length;
+ var versionString = hwiResponse.Substring(startIndex).Trim();
+ version = null;
+ if (Version.TryParse(versionString, out Version v))
+ {
+ version = v;
+ return true;
+ }
+
+ return false;
+ }
}
}

MODIFY
WalletWasabi/WalletWasabi.csproj
WalletWasabi/WalletWasabi.csproj
<PackageReference Include="Microsoft.Win32.Registry" Version="4.5.0" /> </ItemGroup>
<ItemGroup>
<Folder Include="Gma\QrCodeNet\Encoding\ReedSolomon\" />
+ <Folder Include="Hwi2\Binaries\hwi-win64\" />
</ItemGroup>
<ItemGroup>
+ <None Update="Hwi2\Binaries\hwi-win64\hwi.exe">
+ <CopyToOutputDirectory>Always</CopyToOutputDirectory>
+ </None>
<None Update="Hwi\Software\hwi-win64\hwi.exe">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>

Implement GetVersionAsync