Skip to content

Commit 1e7817b

Browse files
Update UI more frequently with reports
1 parent b7d347c commit 1e7817b

File tree

7 files changed

+57
-32
lines changed

7 files changed

+57
-32
lines changed

Src/FinderOuter/Services/Base16Sevice.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ public async Task<bool> Find(string key, char missingChar, string AdditionalInpu
186186
AddQueue($"Elapsed time: {watch.Elapsed}");
187187
AddQueue(GetKeyPerSec(total, watch.Elapsed.TotalSeconds));
188188

189-
return CopyQueueToMessage(success);
189+
return FinishReport(success);
190190
}
191191
}
192192
}

Src/FinderOuter/Services/Base58Sevice.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ private async Task<bool> FindPrivateKey(string key, char missingChar)
446446
if (key.Contains(missingChar)) // Length must be correct then
447447
{
448448
missCount = key.Count(c => c == missingChar);
449-
if (inputService.CanBePrivateKey(key))
449+
if (inputService.CanBePrivateKey(key, out string error))
450450
{
451451
missingIndexes = new int[missCount];
452452
bool isComp = key.Length == Constants.PrivKeyCompWifLen;
@@ -477,6 +477,10 @@ private async Task<bool> FindPrivateKey(string key, char missingChar)
477477
AddQueue($"Elapsed time: {watch.Elapsed}");
478478
AddQueue(GetKeyPerSec(GetTotalCount(missCount), watch.Elapsed.TotalSeconds));
479479
}
480+
else
481+
{
482+
AddQueue(error);
483+
}
480484

481485
if (success)
482486
{
@@ -566,7 +570,7 @@ public async Task<bool> Find(string key, char missingChar, InputType t)
566570
return Fail("Given input type is not defined.");
567571
}
568572

569-
return CopyQueueToMessage(success);
573+
return FinishReport(success);
570574
}
571575
}
572576
}

Src/FinderOuter/Services/InputService.cs

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,40 @@ public class InputService
1717
private readonly Bech32 b32Enc = new Bech32();
1818

1919

20-
public bool CanBePrivateKey(string key)
20+
public bool CanBePrivateKey(string key, out string error)
2121
{
22-
return
23-
(key.Length == Constants.PrivKeyCompWifLen &&
24-
(key[0] == Constants.PrivKeyCompChar1 || key[0] == Constants.PrivKeyCompChar2))
25-
||
26-
(key.Length == Constants.PrivKeyUncompWifLen &&
27-
(key[0] == Constants.PrivKeyUncompChar));
22+
if (key.Length == Constants.PrivKeyCompWifLen)
23+
{
24+
if (key[0] == Constants.PrivKeyCompChar1 || key[0] == Constants.PrivKeyCompChar2)
25+
{
26+
error = null;
27+
return true;
28+
}
29+
else
30+
{
31+
error = $"A key with {key.Length} length is expected to start with {Constants.PrivKeyCompChar1} " +
32+
$"or {Constants.PrivKeyCompChar2}.";
33+
return false;
34+
}
35+
}
36+
else if (key.Length == Constants.PrivKeyUncompWifLen)
37+
{
38+
if (key[0] == Constants.PrivKeyUncompChar)
39+
{
40+
error = null;
41+
return true;
42+
}
43+
else
44+
{
45+
error = $"A key with {key.Length} length is expected to start with {Constants.PrivKeyUncompChar}.";
46+
return false;
47+
}
48+
}
49+
else
50+
{
51+
error = "Given key has an invalid length";
52+
return false;
53+
}
2854
}
2955

3056
public string CheckPrivateKey(string key)

Src/FinderOuter/Services/MessageSignatureService.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public MessageSignatureService(Report rep) : base(rep)
3030
private readonly Sha256 hash = new Sha256(true);
3131
private readonly EllipticCurveCalculator calc;
3232
private readonly Address addressBuilder;
33-
private InputService inputService;
33+
private readonly InputService inputService;
3434

3535

3636
private bool CheckAddress(string addr, out Address.AddressType addrType)
@@ -154,8 +154,7 @@ public bool Validate(string message, string address, string signature)
154154

155155
Signature sig = new Signature(sigBa, Signature.SigEncoding.WithRecId);
156156
bool success = CheckPubkeys(sig, toSign, address, addrType);
157-
CopyQueueToMessage(success);
158-
return success;
157+
return FinishReport(success);
159158
}
160159

161160

@@ -340,7 +339,7 @@ public async Task<bool> TryFindProblem(string message, string address, string si
340339
}
341340
);
342341

343-
return CopyQueueToMessage(success);
342+
return FinishReport(success);
344343
}
345344
}
346345
}

Src/FinderOuter/Services/MnemonicSevice.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ public async Task<bool> FindMissing(string mnemonic, char missingChar, MnemonicT
511511
AddQueue($"Found {Final.Count:n0} correct mnemonics.");
512512
Final.Clear();
513513
}
514-
return CopyQueueToMessage(success);
514+
return FinishReport(success);
515515
}
516516

517517

Src/FinderOuter/Services/ServiceBase.cs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
// Distributed under the MIT software license, see the accompanying
44
// file LICENCE or http://www.opensource.org/licenses/mit-license.php.
55

6+
using Avalonia.Threading;
67
using FinderOuter.Models;
78
using System;
89
using System.Numerics;
9-
using System.Text;
1010

1111
namespace FinderOuter.Services
1212
{
@@ -21,16 +21,19 @@ public ServiceBase(Report rep)
2121
}
2222

2323

24-
2524
private readonly Report report;
26-
private readonly StringBuilder queue = new StringBuilder();
2725

2826

2927
protected void InitReport()
3028
{
3129
report.CurrentState = State.Working;
3230
report.Message = string.Empty;
33-
queue.Clear();
31+
}
32+
33+
protected bool FinishReport(bool success)
34+
{
35+
report.CurrentState = success ? State.FinishedSuccess : State.FinishedFail;
36+
return success;
3437
}
3538

3639
protected void AddMessage(string msg)
@@ -54,15 +57,8 @@ protected bool Pass(string msg)
5457

5558
protected void AddQueue(string msg)
5659
{
57-
queue.AppendLine(msg);
58-
}
59-
60-
protected bool CopyQueueToMessage(bool hasPassed)
61-
{
62-
report.CurrentState = hasPassed ? State.FinishedSuccess : State.FinishedFail;
63-
AddMessage(queue.ToString());
64-
65-
return hasPassed;
60+
Dispatcher.UIThread.InvokeAsync(() =>
61+
report.Message += string.IsNullOrEmpty(report.Message) ? msg : $"{Environment.NewLine}{msg}");
6662
}
6763

6864
protected string GetKeyPerSec(BigInteger total, double totalSecond)
@@ -76,6 +72,5 @@ protected string GetKeyPerSec(BigInteger total, double totalSecond)
7672
return $"k/s= {total / new BigInteger(totalSecond):n0}";
7773
}
7874
}
79-
8075
}
8176
}

Src/Tests/Services/InputServiceTests.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ public class InputServiceTests
2424
public void CanBePrivateKeyTest(string key)
2525
{
2626
InputService serv = new InputService();
27-
bool actual = serv.CanBePrivateKey(key);
28-
Assert.True(actual);
27+
bool actual = serv.CanBePrivateKey(key, out string error);
28+
Assert.True(actual, error);
29+
Assert.Null(error);
2930
}
3031

3132
[Theory]
@@ -41,7 +42,7 @@ public void CanBePrivateKeyTest(string key)
4142
public void CanBePrivateKey_FalseTest(string key)
4243
{
4344
InputService serv = new InputService();
44-
bool actual = serv.CanBePrivateKey(key);
45+
bool actual = serv.CanBePrivateKey(key, out _);
4546
Assert.False(actual);
4647
}
4748

0 commit comments

Comments
 (0)