Skip to content

Commit

Permalink
Make error messages more clear
Browse files Browse the repository at this point in the history
  • Loading branch information
Coding-Enthusiast committed Feb 21, 2020
1 parent 0d506e6 commit e949a71
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 14 deletions.
5 changes: 2 additions & 3 deletions Src/FinderOuter/Backend/Constants.cs
Expand Up @@ -15,8 +15,7 @@ public struct Constants
public const char UncompPrivKeyChar = '5';
public const int CompPrivKeyLen = 52;
public const int UncompPrivKeyLen = 51;
public const byte CompPrivKeyFirstByte = 0x80;
public const byte UncompPrivKeyFirstByte = 0x80;
public const byte UncompPrivKeyLastByte = 1;
public const byte PrivKeyFirstByte = 0x80;
public const byte CompPrivKeyLastByte = 1;
}
}
4 changes: 2 additions & 2 deletions Src/FinderOuter/Backend/Encoders/Base58.cs
Expand Up @@ -50,7 +50,7 @@ public bool IsValid(string encoded)
}


private bool HasValidChars(string val)
internal bool HasValidChars(string val)
{
if (val == null) // We consider empty string to be valid (white space will be caught in next condition)
{
Expand All @@ -65,7 +65,7 @@ private bool HasValidChars(string val)
return true;
}

private bool HasValidCheckSum(string val)
internal bool HasValidCheckSum(string val)
{
byte[] data = DecodeWithoutValidation(val);
if (data.Length < CheckSumSize)
Expand Down
37 changes: 30 additions & 7 deletions Src/FinderOuter/Services/Base58Sevice.cs
Expand Up @@ -605,24 +605,47 @@ public async Task<bool> Find(string key, char missingChar)
{
AddMessage("No character is missing, checking validity of the key itself.");
// TODO: use Backend.KeyPairs.PrivateKey instead
if (!encoder.IsValid(key))
if (!encoder.HasValidChars(key))
{
return Fail("The given key is not a valid base-58 encoded string.");
return Fail("The given key contains invalid base-58 characters.");
}
if (!encoder.HasValidCheckSum(key))
{
return Fail("The given key has an invalid checksum.");
}

byte[] keyBa = encoder.DecodeWithCheckSum(key);
if (keyBa.Length == 33 && keyBa[0] == Constants.CompPrivKeyFirstByte)
if (keyBa[0] != Constants.PrivKeyFirstByte)
{
return Pass("The given key is a valid compressed private key.");
return Fail($"Invalid first key byte (actual={keyBa[0]}, expected={Constants.PrivKeyFirstByte})");
}
else if (keyBa.Length == 34 &&
keyBa[0] == Constants.UncompPrivKeyFirstByte && keyBa[33] == Constants.UncompPrivKeyLastByte)
if (keyBa.Length == 33)
{
if (!inputService.IsPrivateKeyInRange(keyBa.SubArray(1)))
{
return Fail("Invalid key integer value (outside of the range defined by secp256k1 curve).");
}

return Pass("The given key is a valid uncompressed private key.");
}
else if (keyBa.Length == 34)
{
if (keyBa[^1] != Constants.CompPrivKeyLastByte)
{
return Fail($"Invalid compressed key last byte (actual={keyBa[^1]}, " +
$"expected={Constants.CompPrivKeyLastByte})");
}
if (!inputService.IsPrivateKeyInRange(keyBa.SubArray(1, 32)))
{
return Fail("Invalid key integer value (outside of the range defined by secp256k1 curve).");
}

return Pass("The given key is a valid compressed private key.");
}
else
{
return Fail("The given key is not a valid private key.");
return Fail($"The given key length is invalid. actual = {keyBa.Length}, " +
$"expected = 33 (uncomp) or 34 (comp))");
}
}

Expand Down
15 changes: 13 additions & 2 deletions Src/FinderOuter/Services/InputService.cs
Expand Up @@ -5,8 +5,7 @@

using FinderOuter.Backend;
using FinderOuter.Models;
using System;
using System.Collections.Generic;
using System.Numerics;
using System.Text;

namespace FinderOuter.Services
Expand All @@ -29,6 +28,18 @@ public bool CanBePrivateKey(string key)
}


public bool IsPrivateKeyInRange(byte[] key)
{
if (key.Length > 32)
{
return false;
}
BigInteger val = key.ToBigInt(true, true);
BigInteger max = BigInteger.Parse("115792089237316195423570985008687907852837564279074904382605163141518161494336");
return val >= BigInteger.One && val <= max;
}


public bool NormalizeNFKD(string s, out string norm)
{
norm = s.Normalize(NormalizationForm.FormKD);
Expand Down

0 comments on commit e949a71

Please sign in to comment.