Skip to content

Commit a8e0e8a

Browse files
committed
byte key representation as ImmutableArray<byte>
1 parent 5d85f73 commit a8e0e8a

7 files changed

+16
-15
lines changed

Libplanet/Crypto/BlsPrivateKey.cs

+5-4
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ namespace Libplanet.Crypto
3030
public class BlsPrivateKey : IEquatable<BlsPrivateKey>, IPrivateKey
3131
{
3232
private const int KeyByteSize = BLS.SECRETKEY_SERIALIZE_SIZE;
33-
private readonly IReadOnlyList<byte> _privateKey;
33+
private readonly ImmutableArray<byte> _privateKey;
3434
private BlsPublicKey? _publicKey;
3535
private BlsSignature? _proofOfPossession;
3636

@@ -40,7 +40,8 @@ public class BlsPrivateKey : IEquatable<BlsPrivateKey>, IPrivateKey
4040
/// </summary>
4141
public BlsPrivateKey()
4242
{
43-
_privateKey = CryptoConfig.ConsensusCryptoBackend.GeneratePrivateKey();
43+
_privateKey = CryptoConfig.ConsensusCryptoBackend.GeneratePrivateKey()
44+
.ToImmutableArray();
4445
_proofOfPossession = CryptoConfig.ConsensusCryptoBackend.GetProofOfPossession(this);
4546
}
4647

@@ -81,7 +82,7 @@ public BlsPrivateKey(IReadOnlyList<byte> privateKey)
8182
nameof(privateKey));
8283
}
8384

84-
_privateKey = privateKey;
85+
_privateKey = privateKey.ToImmutableArray();
8586
_ = CryptoConfig.ConsensusCryptoBackend.ValidateGetNativePrivateKey(this);
8687
_proofOfPossession = CryptoConfig.ConsensusCryptoBackend.GetProofOfPossession(this);
8788
}
@@ -113,7 +114,7 @@ public BlsPublicKey PublicKey
113114

114115
IPublicKey IPrivateKey.PublicKey => PublicKey;
115116

116-
IReadOnlyList<byte> IPrivateKey.KeyBytes => ByteArray;
117+
ImmutableArray<byte> IPrivateKey.KeyBytes => ByteArray;
117118

118119
/// <summary>
119120
/// Returns the corresponding Proof of Possession. Public key can be aggregated, some public

Libplanet/Crypto/BlsPublicKey.cs

+3-4
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class BlsPublicKey : IEquatable<BlsPublicKey>, IPublicKey
3535
{
3636
private const int KeyByteSize = BLS.PUBLICKEY_SERIALIZE_SIZE;
3737

38-
private readonly IReadOnlyList<byte> _publicKey;
38+
private readonly ImmutableArray<byte> _publicKey;
3939

4040
/// <summary>
4141
/// Creates a <see cref="BlsPublicKey"/> instance from the given
@@ -59,12 +59,11 @@ public BlsPublicKey(IReadOnlyList<byte> publicKey)
5959
);
6060
}
6161

62-
_publicKey = publicKey;
63-
62+
_publicKey = publicKey.ToImmutableArray();
6463
_ = CryptoConfig.ConsensusCryptoBackend.ValidateGetNativePublicKey(this);
6564
}
6665

67-
public IReadOnlyList<byte> KeyBytes => ToImmutableArray();
66+
public ImmutableArray<byte> KeyBytes => ToImmutableArray();
6867

6968
public static bool operator ==(BlsPublicKey left, BlsPublicKey right) =>
7069
left.Equals(right);

Libplanet/Crypto/BlsSignature.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ namespace Libplanet.Crypto
3434
public sealed class BlsSignature : IEquatable<BlsSignature>
3535
{
3636
private const int KeyByteSize = BLS.SIGNATURE_SERIALIZE_SIZE;
37-
private readonly IReadOnlyList<byte> _signature;
37+
private readonly ImmutableArray<byte> _signature;
3838

3939
/// <summary>
4040
/// Instantiates a new <see cref="BlsSignature"/> by given <see cref="byte"/>
@@ -65,7 +65,7 @@ public BlsSignature(IReadOnlyList<byte> signature)
6565
);
6666
}
6767

68-
_signature = signature;
68+
_signature = signature.ToImmutableArray();
6969
_ = CryptoConfig.ConsensusCryptoBackend.ValidateGetNativeSignature(ToByteArray());
7070
}
7171

Libplanet/Crypto/IPrivateKey.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Collections.Generic;
1+
using System.Collections.Immutable;
22

33
namespace Libplanet.Crypto
44
{
@@ -11,7 +11,7 @@ public interface IPrivateKey
1111
/// <summary>
1212
/// The <see cref="byte"/> representation of private key.
1313
/// </summary>
14-
IReadOnlyList<byte> KeyBytes { get; }
14+
ImmutableArray<byte> KeyBytes { get; }
1515

1616
/// <summary>
1717
/// The public key pair, <see cref="IPublicKey"/> of private key.

Libplanet/Crypto/IPublicKey.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Generic;
2+
using System.Collections.Immutable;
23

34
namespace Libplanet.Crypto
45
{
@@ -11,7 +12,7 @@ public interface IPublicKey
1112
/// <summary>
1213
/// The <see cref="byte"/> representation of public key.
1314
/// </summary>
14-
IReadOnlyList<byte> KeyBytes { get; }
15+
ImmutableArray<byte> KeyBytes { get; }
1516

1617
/// <summary>
1718
/// Verifies message with base public key method. See base verify method for detailed

Libplanet/Crypto/PrivateKey.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ public PublicKey PublicKey
155155
[Pure]
156156
public ImmutableArray<byte> ByteArray => ToByteArray().ToImmutableArray();
157157

158-
public IReadOnlyList<byte> KeyBytes => ByteArray;
158+
public ImmutableArray<byte> KeyBytes => ByteArray;
159159

160160
internal ECPrivateKeyParameters KeyParam { get; }
161161

Libplanet/Crypto/PublicKey.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ internal PublicKey(ECPublicKeyParameters keyParam)
5252
KeyParam = keyParam;
5353
}
5454

55-
public IReadOnlyList<byte> KeyBytes => Format(false);
55+
public ImmutableArray<byte> KeyBytes => Format(false).ToImmutableArray();
5656

5757
internal ECPublicKeyParameters KeyParam { get; }
5858

0 commit comments

Comments
 (0)