Skip to content

Commit

Permalink
Merge pull request #1572 from dahlia/0.19-maintenance
Browse files Browse the repository at this point in the history
Merge 0.18.4 into 0.19-maintenance (to be 0.19.2)
  • Loading branch information
dahlia committed Nov 3, 2021
2 parents 7db61f0 + f28a5ea commit f89db86
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 8 deletions.
20 changes: 20 additions & 0 deletions CHANGES.md
Expand Up @@ -6,6 +6,13 @@ Version 0.19.2

To be released.

- `PrivateKey(IReadOnlyList<byte>)` overloaded constructor no more accepts
a list shorter or longer than 32 bytes. [[#1571], [#1572]]
- `PrivateKey.FromString()` method no more accept a hexadecimal digits
shorter or longer than 64 characters. [[#1571], [#1572]]

[#1572]: https://github.com/planetarium/libplanet/pull/1572


Version 0.19.1
--------------
Expand Down Expand Up @@ -108,6 +115,19 @@ Released on October 27, 2021.
[#1557]: https://github.com/planetarium/libplanet/pull/1557


Version 0.18.4
--------------

Released on November 2, 2021.

- `PrivateKey(IReadOnlyList<byte>)` overloaded constructor no more accepts
a list shorter or longer than 32 bytes. [[#1571]]
- `PrivateKey.FromString()` method no more accept a hexadecimal digits
shorter or longer than 64 characters. [[#1571]]

[#1571]: https://github.com/planetarium/libplanet/pull/1571


Version 0.18.3
--------------

Expand Down
33 changes: 32 additions & 1 deletion Libplanet.Tests/Crypto/PrivateKeyTest.cs
Expand Up @@ -15,6 +15,17 @@ public void FromString()
{
Assert.Throws<ArgumentOutOfRangeException>(() => PrivateKey.FromString(string.Empty));
Assert.Throws<ArgumentOutOfRangeException>(() => PrivateKey.FromString("a"));
Assert.Throws<ArgumentOutOfRangeException>(() => PrivateKey.FromString("870912"));
Assert.Throws<ArgumentOutOfRangeException>(() =>
PrivateKey.FromString(
"00000000000000000000000000000000000000000000000000000000870912"
)
);
Assert.Throws<ArgumentOutOfRangeException>(() =>
PrivateKey.FromString(
"000000000000000000000000000000000000000000000000000000000000870912"
)
);
Assert.Throws<FormatException>(() => PrivateKey.FromString("zz"));
PrivateKey actual = PrivateKey.FromString(
"e07107ca4b0d19147fa1152a0f2c7884705d59cbb6318e2f901bd28dd9ff78e3"
Expand Down Expand Up @@ -49,7 +60,27 @@ public void BytesTest()
[Fact]
public void BytesSanityCheckTest()
{
var bs = new byte[]
Assert.Throws<ArgumentOutOfRangeException>(
() => new PrivateKey(new byte[] { 0x87, 0x09, 0x12 })
);
Assert.Throws<ArgumentOutOfRangeException>(() =>
new PrivateKey(new byte[31]
{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0x87, 0x09, 0x12,
})
);
Assert.Throws<ArgumentOutOfRangeException>(() =>
new PrivateKey(new byte[33]
{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0x87, 0x09, 0x12,
})
);

var bs = new byte[20]
{
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
Expand Down
39 changes: 33 additions & 6 deletions Libplanet/Crypto/PrivateKey.cs
Expand Up @@ -42,6 +42,7 @@ namespace Libplanet.Crypto
[Equals]
public class PrivateKey
{
private const int KeyByteSize = 32;
private PublicKey? _publicKey;

/// <summary>
Expand All @@ -60,12 +61,35 @@ public PrivateKey()
/// </summary>
/// <param name="privateKey">A valid <see cref="byte"/>s that encodes an ECDSA private key.
/// </param>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the given
/// <paramref name="privateKey"/> is too short or too long.</exception>
/// <remarks>A valid <see cref="byte"/> array for a <see cref="PrivateKey"/> can be encoded
/// using <see cref="ByteArray"/> property.</remarks>
/// <seealso cref="ByteArray"/>
public PrivateKey(IReadOnlyList<byte> privateKey)
: this(GenerateKeyFromBytes(privateKey is byte[] ba ? ba : privateKey.ToArray()))
: this(privateKey is byte[] ba ? ba : privateKey.ToArray(), informedConsent: true)
{
if (privateKey.Count != KeyByteSize)
{
throw new ArgumentOutOfRangeException(
nameof(privateKey),
$"The key must be {KeyByteSize} bytes."
);
}
}

internal PrivateKey(byte[] unverifiedKey, bool informedConsent)
: this(GenerateKeyFromBytes(unverifiedKey))
{
// The `informedConsent` parameter mainly purposes to prevent this overload from
// being chosen instead of PrivatKey(IReadOnly<byte>) by mistake.
if (!informedConsent)
{
throw new ArgumentException(
nameof(informedConsent),
"The caller should ensure the key is valid and safe enough."
);
}
}

private PrivateKey(ECPrivateKeyParameters keyParam)
Expand Down Expand Up @@ -131,19 +155,22 @@ public PublicKey PublicKey
/// <exception cref="ArgumentNullException">Thrown when the given <paramref name="hex"/>
/// string is <c>null</c>.</exception>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the length of the given
/// <paramref name="hex"/> string is an odd number, or it is empty.</exception>
/// <paramref name="hex"/> string is too short or too long.</exception>
/// <exception cref="FormatException">Thrown when the given <paramref name="hex"/> string is
/// not a valid hexadecimal string.</exception>
[Pure]
public static PrivateKey FromString(string hex)
{
if (!hex.Any())
byte[] bytes = ByteUtil.ParseHex(hex);
if (bytes.Length != KeyByteSize)
{
throw new ArgumentOutOfRangeException(nameof(hex), "Argument must not be empty.");
throw new ArgumentOutOfRangeException(
nameof(hex),
$"Expected {KeyByteSize * 2} hexadecimal digits."
);
}

byte[] bytes = ByteUtil.ParseHex(hex);
return new PrivateKey(bytes);
return new PrivateKey(unverifiedKey: bytes, informedConsent: true);
}

/// <summary>
Expand Down
5 changes: 4 additions & 1 deletion Libplanet/KeyStore/ProtectedPrivateKey.cs
Expand Up @@ -320,7 +320,10 @@ public PrivateKey Unprotect(string passphrase)
ImmutableArray<byte> encKey = MakeEncryptionKey(derivedKey);
ImmutableArray<byte> plaintext = Cipher.Decrypt(encKey, Ciphertext);

var key = new PrivateKey(plaintext);
var key = new PrivateKey(
unverifiedKey: plaintext.ToBuilder().ToArray(),
informedConsent: true
);
Address actualAddress = key.ToAddress();
if (!Address.Equals(actualAddress))
{
Expand Down

0 comments on commit f89db86

Please sign in to comment.