Skip to content

Commit

Permalink
Address.Size constant
Browse files Browse the repository at this point in the history
  • Loading branch information
dahlia committed Mar 14, 2019
1 parent 625fddd commit 7571090
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Expand Up @@ -30,6 +30,7 @@ To be released.
[[#120], [#123] by Yang Chun Ung, [#126], [#127]]
- `Address` and `TxId` are now serializable.
[[#99], [#124] by Qria]
- Added `Address.Size` constant, which is fixed to the `Int32` 20.
- `Swarm.AddPeersAsync()` was fixed so that unreachable `Peer`s are ignored.
[[#128]]

Expand Down
12 changes: 9 additions & 3 deletions Libplanet/Address.cs
Expand Up @@ -43,6 +43,12 @@ namespace Libplanet
public partial struct Address : ISerializable
#pragma warning restore CS0282
{
/// <summary>
/// The <see cref="byte"/>s size that each <see cref="Address"/> takes.
/// <para>It is 20 <see cref="byte"/>s.</para>
/// </summary>
public const int Size = 20;

private ImmutableArray<byte> _byteArray;

/// <summary>
Expand All @@ -67,7 +73,7 @@ public Address(byte[] address)
throw new NullReferenceException("address must not be null");
}

if (address.Length != 20)
if (address.Length != Size)
{
throw new ArgumentException("address must be 20 bytes");
}
Expand Down Expand Up @@ -142,7 +148,7 @@ public ImmutableArray<byte> ByteArray
{
if (_byteArray.IsDefault)
{
_byteArray = new byte[20].ToImmutableArray();
_byteArray = new byte[Size].ToImmutableArray();
}

return _byteArray;
Expand Down Expand Up @@ -245,7 +251,7 @@ private static byte[] DeriveAddress(PublicKey key)
byte[] hashPayload = key.Format(false).Skip(1).ToArray();
var output = CalculateHash(hashPayload);

return output.Skip(output.Length - 20).ToArray();
return output.Skip(output.Length - Size).ToArray();
}

private static byte[] DeriveAddress(string hex)
Expand Down

0 comments on commit 7571090

Please sign in to comment.