Skip to content

Commit 2ec343a

Browse files
Change how the no missing word cases are handled
1 parent 4b4188f commit 2ec343a

File tree

2 files changed

+20
-58
lines changed

2 files changed

+20
-58
lines changed

Src/FinderOuter/Services/MnemonicSevice.cs

Lines changed: 11 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1425,67 +1425,30 @@ public async void FindMissing(MnemonicSearchSpace ss, string pass, string path,
14251425
return;
14261426
}
14271427

1428-
try
1429-
{
1430-
this.path = new BIP0032Path(path);
1431-
}
1432-
catch (Exception ex)
1433-
{
1434-
report.Fail($"Invalid path ({ex.Message}).");
1435-
this.path = null;
1436-
}
1437-
14381428
if (!InputService.TryGetCompareService(compType, comp, out comparer))
14391429
{
14401430
report.Fail($"Invalid extra input or input type {compType}.");
14411431
comparer = null;
14421432
}
14431433

1434+
this.path = MnemonicSearchSpace.ProcessPath(path, out string pathError);
1435+
14441436
if (ss.MissCount == 0)
14451437
{
1446-
try
1447-
{
1448-
BIP0032 temp = ss.mnType switch
1449-
{
1450-
MnemonicTypes.BIP39 => new BIP0039(ss.Input, ss.wl, pass),
1451-
MnemonicTypes.Electrum => new ElectrumMnemonic(ss.Input, ss.wl, pass),
1452-
_ => throw new ArgumentException("Undefined mnemonic type.")
1453-
};
1454-
1455-
report.Pass($"Given input is a valid {ss.mnType} mnemonic.");
1456-
1457-
if (path is null || comparer is null)
1458-
{
1459-
report.AddMessageSafe("Path or compare input is not set correctly to check the derived key.");
1460-
return;
1461-
}
1462-
1463-
uint startIndex = this.path.Indexes[^1];
1464-
uint[] indices = new uint[this.path.Indexes.Length - 1];
1465-
Array.Copy(this.path.Indexes, 0, indices, 0, indices.Length);
1466-
BIP0032Path newPath = new(indices);
1467-
1468-
PrivateKey[] keys = temp.GetPrivateKeys(newPath, 1, startIndex);
1469-
if (comparer.Compare(keys[0].ToBytes()))
1470-
{
1471-
report.Pass($"The given child key is derived from this mnemonic at {this.path} path.");
1472-
}
1473-
else
1474-
{
1475-
report.Fail($"The given child key is not derived from this mnemonic or not at {this.path} path.");
1476-
}
1477-
}
1478-
catch (Exception ex)
1479-
{
1480-
report.Fail($"Mnemonic is not missing any characters but is invalid. Error: {ex.Message}");
1481-
}
1438+
report.FoundAnyResult = ss.ProcessNoMissing(comparer, pass, this.path, out string message);
1439+
report.AddMessageSafe(message);
1440+
return;
1441+
}
14821442

1443+
if (path is null)
1444+
{
1445+
report.Fail($"Could not parse the given derivation path (error message: {pathError}).");
14831446
return;
14841447
}
14851448

1486-
if (path is null || comparer is null)
1449+
if (comparer is null || !comparer.IsInitialized)
14871450
{
1488-
report.AddMessageSafe("Path or compare input is not set correctly to check the derived key.");
1451+
report.Fail("Set the compare value correctly to verify the derived key/address.");
14891452
return;
14901453
}
14911454

Src/FinderOuter/Services/SearchSpaces/MnemonicSearchSpace.cs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public bool Process(string mnemonic, char missChar, MnemonicTypes mnType, BIP003
125125
}
126126

127127

128-
private BIP0032Path ProcessPath(string path, out string error)
128+
public static BIP0032Path ProcessPath(string path, out string error)
129129
{
130130
BIP0032Path result;
131131
try
@@ -142,7 +142,7 @@ private BIP0032Path ProcessPath(string path, out string error)
142142
return result;
143143
}
144144

145-
public bool ProcessNoMissing(ICompareService comparer, string pass, string path, out string message)
145+
public bool ProcessNoMissing(ICompareService comparer, string pass, BIP0032Path path, out string message)
146146
{
147147
if (MissCount != 0)
148148
{
@@ -161,11 +161,10 @@ public bool ProcessNoMissing(ICompareService comparer, string pass, string path,
161161

162162
message = $"Given input is a valid {mnType} mnemonic.";
163163

164-
BIP0032Path bipPath = ProcessPath(path, out string error);
165-
if (bipPath is null)
164+
if (path is null)
166165
{
167166
message += Environment.NewLine;
168-
message += $"Could not set derivation path ({error}).";
167+
message += "Derivation path is not set.";
169168
return true; // The mnemonic is valid, we just can't derive child keys to do extra checks
170169
}
171170

@@ -175,22 +174,22 @@ public bool ProcessNoMissing(ICompareService comparer, string pass, string path,
175174
return true; // Same as above
176175
}
177176

178-
uint startIndex = bipPath.Indexes[^1];
179-
uint[] indices = new uint[bipPath.Indexes.Length - 1];
180-
Array.Copy(bipPath.Indexes, 0, indices, 0, indices.Length);
177+
uint startIndex = path.Indexes[^1];
178+
uint[] indices = new uint[path.Indexes.Length - 1];
179+
Array.Copy(path.Indexes, 0, indices, 0, indices.Length);
181180
BIP0032Path newPath = new(indices);
182181

183182
PrivateKey[] keys = temp.GetPrivateKeys(newPath, 1, startIndex);
184183
if (comparer.Compare(keys[0].ToBytes()))
185184
{
186185
message += Environment.NewLine;
187-
message += $"The given child key is correctly derived from this mnemonic at {bipPath} path.";
186+
message += $"The given child key is correctly derived from this mnemonic at {path} path.";
188187
return true;
189188
}
190189
else
191190
{
192191
message += Environment.NewLine;
193-
message += $"The given child key is not derived from this mnemonic or not at {bipPath} path.";
192+
message += $"The given child key is not derived from this mnemonic or not at {path} path.";
194193
message += $"List of all address types that can be derived from this mnemonic at the given path:" +
195194
$"{Environment.NewLine}" +
196195
$"{AddressService.GetAllAddresses(keys[0].ToPublicKey(comparer.Calc))}";

0 commit comments

Comments
 (0)