Add byte read method to signer helper
MODIFY
WalletWasabi/Helpers/WasabiSignerHelpers.cs
WalletWasabi/Helpers/WasabiSignerHelpers.cs
@@ -3,6 +3,7 @@ using NBitcoin;
using System.Threading.Tasks;
using System.IO;
using System.Security.Cryptography;
+using System.Threading;
namespace WalletWasabi.Helpers;
@@ -10,9 +11,7 @@ public class WasabiSignerHelpers
{
public static async Task SignSha256SumsFileAsync(string sha256SumsAscFilePath, Key wasabiPrivateKey)
{
- var bytes = await File.ReadAllBytesAsync(sha256SumsAscFilePath).ConfigureAwait(false);
- using SHA256 sha = SHA256.Create();
- byte[] computedHash = sha.ComputeHash(bytes);
+ var computedHash = await GetShaComputedBytesOfFileAsync(sha256SumsAscFilePath).ConfigureAwait(false);
ECDSASignature signature = wasabiPrivateKey.Sign(new uint256(computedHash));
@@ -25,9 +24,7 @@ public class WasabiSignerHelpers
public static async Task VerifySha256SumsFileAsync(string sha256SumsAscFilePath)
{
// Read the content file
- var bytes = await File.ReadAllBytesAsync(sha256SumsAscFilePath).ConfigureAwait(false);
- using SHA256 sha = SHA256.Create();
- byte[] hash = sha.ComputeHash(bytes);
+ byte[] hash = await GetShaComputedBytesOfFileAsync(sha256SumsAscFilePath).ConfigureAwait(false);
// Read the signature file
var wasabiSignatureFilePath = Path.ChangeExtension(sha256SumsAscFilePath, "wasabisig");
@@ -63,4 +60,24 @@ public class WasabiSignerHelpers
BitcoinSecret secret = new(keyFileContent, Network.Main);
return secret.PrivateKey;
}
+
+ /// <summary>
+ /// This function returns a SHA256 computed byte array of a file on the provided file path.
+ /// Used to generate uint256 hashes.
+ /// </summary>
+ /// <exception cref="FileNotFoundException"></exception>
+ public static async Task<byte[]> GetShaComputedBytesOfFileAsync(string filePath, CancellationToken cancellationToken = default)
+ {
+ if (File.Exists(filePath))
+ {
+ byte[] bytes = await File.ReadAllBytesAsync(filePath, cancellationToken).ConfigureAwait(false);
+ using SHA256 sha = SHA256.Create();
+ byte[] computedHash = sha.ComputeHash(bytes);
+ return computedHash;
+ }
+ else
+ {
+ throw new FileNotFoundException($"Couldn't find file at {filePath}");
+ }
+ }
}

Add byte read method to signer helper