Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

♻️ Refactor crypto #3486

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ To be released.

### Backward-incompatible API changes

- (Libplanet.Crypto) Removed `ToAddress()` extension method for
`PrivateKey` and `PublicKey`. [[#3486]]
- (Libplanet.Crypto) Added `Address` property to `PrivateKey` and `PublicKey`.
[[#3486]]

### Backward-incompatible network protocol changes

### Backward-incompatible storage format changes
Expand All @@ -24,6 +29,8 @@ To be released.

### CLI tools

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


Version 3.7.0
-------------
Expand Down
2 changes: 1 addition & 1 deletion Libplanet.Action.Tests/ActionEvaluationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public ActionEvaluationTest(ITestOutputHelper output)
public void Constructor()
{
var txid = new System.Random().NextTxId();
Address address = new PrivateKey().ToAddress();
Address address = new PrivateKey().Address;
var evaluation = new ActionEvaluation(
new DumbAction(address, "item"),
new ActionContext(
Expand Down
6 changes: 3 additions & 3 deletions Libplanet.Action.Tests/Loader/IndexedActionLoaderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ public void LoadAction()
var loader1 = new SingleActionLoader(typeof(DumbAction));
var loader2 = new SingleActionLoader(typeof(Attack));
var loader3 = new SingleActionLoader(typeof(RandomAction));
var action1 = new DumbAction(new PrivateKey().PublicKey.ToAddress(), "foo");
var action1 = new DumbAction(new PrivateKey().Address, "foo");
var action2 = new Attack();
action2.LoadPlainValue(Dictionary.Empty
.Add("type_id", "attack")
.Add("values", Dictionary.Empty
.Add("weapon", "sword")
.Add("target", "dummy")
.Add("target_address", new PrivateKey().PublicKey.ToAddress().Bencoded)));
var action3 = new RandomAction(new PrivateKey().PublicKey.ToAddress());
.Add("target_address", new PrivateKey().Address.Bencoded)));
var action3 = new RandomAction(new PrivateKey().Address);

var loader = new IndexedActionLoader(
new List<(long, IActionLoader)>
Expand Down
4 changes: 2 additions & 2 deletions Libplanet.Action.Tests/State/KeyConvertersTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public KeyConvertersTest()
[Fact]
public void ToKeysSpec()
{
var address = new PrivateKey().ToAddress();
var currency = Currency.Uncapped("Foo", 2, new PrivateKey().ToAddress());
var address = new PrivateKey().Address;
var currency = Currency.Uncapped("Foo", 2, new PrivateKey().Address);

Assert.Equal(
new KeyBytes(ByteUtil.Hex(address.ByteArray)),
Expand Down
4 changes: 2 additions & 2 deletions Libplanet.Benchmarks/AppendBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void PrepareAppendMakeTenTransactionsNoAction()
public void PrepareAppendMakeOneTransactionWithActions()
{
var privateKey = new PrivateKey();
var address = privateKey.ToAddress();
var address = privateKey.Address;
var actions = new[]
{
new DumbAction(address, "foo"),
Expand All @@ -74,7 +74,7 @@ public void PrepareAppendMakeTenTransactionsWithActions()
for (var i = 0; i < 10; i++)
{
var privateKey = new PrivateKey();
var address = privateKey.ToAddress();
var address = privateKey.Address;
var actions = new[]
{
new DumbAction(address, "foo"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public LeafModel()
BigDict = Enumerable
.Range(0, 1000)
.Select(_ => new KeyValuePair<Address, string>(
new PrivateKey().ToAddress(),
new PrivateKey().ToAddress().ToString()))
new PrivateKey().Address,
new PrivateKey().Address.ToString()))
.ToImmutableDictionary();
}

Expand Down
4 changes: 2 additions & 2 deletions Libplanet.Benchmarks/DataModel/DataModelBenchmark.MidModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public MidModel()
BigDict = Enumerable
.Range(0, 1000)
.Select(_ => new KeyValuePair<Address, string>(
new PrivateKey().ToAddress(),
new PrivateKey().ToAddress().ToString()))
new PrivateKey().Address,
new PrivateKey().Address.ToString()))
.ToImmutableDictionary();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public RootModel()
BigDict = Enumerable
.Range(0, 1000)
.Select(_ => new KeyValuePair<Address, string>(
new PrivateKey().ToAddress(),
new PrivateKey().ToAddress().ToString()))
new PrivateKey().Address,
new PrivateKey().Address.ToString()))
.ToImmutableDictionary();
}

Expand Down
4 changes: 2 additions & 2 deletions Libplanet.Benchmarks/ProposeBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public void MakeTenTransactionsNoAction()
public void MakeOneTransactionWithActions()
{
var privateKey = new PrivateKey();
var address = privateKey.ToAddress();
var address = privateKey.Address;
var actions = new[]
{
new DumbAction(address, "foo"),
Expand All @@ -95,7 +95,7 @@ public void MakeTenTransactionsWithActions()
for (var i = 0; i < 10; i++)
{
var privateKey = new PrivateKey();
var address = privateKey.ToAddress();
var address = privateKey.Address;
var actions = new[]
{
new DumbAction(address, "foo"),
Expand Down
2 changes: 1 addition & 1 deletion Libplanet.Crypto/Address.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public Address(byte[] address)
/// cref="PublicKey"/>.
/// <para>Note that there is an equivalent extension method
/// <see cref="AddressExtensions.ToAddress(PublicKey)"/>, which enables
/// a code like <c>publicKey.ToAddress()</c> instead of
/// a code like <c>publicKey.Address</c> instead of
/// <c>new Address(publicKey)</c>, for convenience.</para>
/// </summary>
/// <param name="publicKey">A <see cref="PublicKey"/> to derive
Expand Down
41 changes: 0 additions & 41 deletions Libplanet.Crypto/AddressExtensions.cs

This file was deleted.

6 changes: 6 additions & 0 deletions Libplanet.Crypto/PrivateKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@ public PublicKey PublicKey
}
}

/// <summary>
/// The corresponding <see cref="Crypto.Address"/> derived from a <see cref="PrivateKey"/>.
/// This is the same as the one derived from <see cref="PublicKey"/>.
/// </summary>
public Address Address => new Address(PublicKey);

/// <summary>
/// An encoded <see cref="byte"/> array representation.
/// </summary>
Expand Down
5 changes: 5 additions & 0 deletions Libplanet.Crypto/PublicKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ internal PublicKey(ECPublicKeyParameters keyParam)
KeyParam = keyParam;
}

/// <summary>
/// The corresponding <see cref="Crypto.Address"/> derived from a <see cref="PublicKey"/>.
/// </summary>
public Address Address => new Address(this);

internal ECPublicKeyParameters KeyParam { get; }

public static bool operator ==(PublicKey left, PublicKey right) => left.Equals(right);
Expand Down
22 changes: 11 additions & 11 deletions Libplanet.Explorer.Tests/GeneratedBlockChainFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,23 +58,23 @@ public GeneratedBlockChainFixture(
.Empty,
(dict, pk) =>
dict.SetItem(
pk.ToAddress(),
pk.Address,
ImmutableArray<Block>.Empty));
SignedTxs = PrivateKeys.Aggregate(
ImmutableDictionary<
Address,
ImmutableArray<Transaction>>.Empty,
(dict, pk) =>
dict.SetItem(
pk.ToAddress(),
pk.Address,
ImmutableArray<Transaction>.Empty));
InvolvedTxs = PrivateKeys.Aggregate(
ImmutableDictionary<
Address,
ImmutableArray<Transaction>>.Empty,
(dict, pk) =>
dict.SetItem(
pk.ToAddress(),
pk.Address,
ImmutableArray<Transaction>.Empty));

var privateKey = new PrivateKey();
Expand All @@ -90,7 +90,7 @@ public GeneratedBlockChainFixture(
Block genesisBlock = BlockChain.ProposeGenesisBlock(
actionEvaluator,
transactions: PrivateKeys
.OrderBy(pk => pk.ToAddress().ToHex())
.OrderBy(pk => pk.Address.ToHex())
.Select(
(pk, i) => Transaction.Create(
nonce: i,
Expand Down Expand Up @@ -126,7 +126,7 @@ public GeneratedBlockChainFixture(
random.Next(),
actionsForTransactions.Select(actions =>
Transaction.Create(
Chain.GetNextTxNonce(pk.ToAddress()),
Chain.GetNextTxNonce(pk.Address),
pk,
Chain.Genesis.Hash,
actions.ToPlainValues()))
Expand All @@ -150,7 +150,7 @@ public GeneratedBlockChainFixture(
random.Next(),
actionsForTransactions.Select(actions =>
Transaction.Create(
Chain.GetNextTxNonce(pk.ToAddress()),
Chain.GetNextTxNonce(pk.Address),
pk,
Chain.Genesis.Hash,
actions.ToPlainValues()))
Expand All @@ -172,7 +172,7 @@ private ImmutableArray<Transaction> GetRandomTransactions(
var pk = PrivateKeys[random.Next(PrivateKeys.Length)];
if (!nonces.TryGetValue(pk, out var nonce))
{
nonce = Chain.GetNextTxNonce(pk.ToAddress());
nonce = Chain.GetNextTxNonce(pk.Address);
}

nonces = nonces.SetItem(pk, nonce + 1);
Expand All @@ -187,7 +187,7 @@ private Transaction
GetRandomTransaction(int seed, PrivateKey pk, long nonce)
{
var random = new System.Random(seed);
var addr = pk.ToAddress();
var addr = pk.Address;
var bal = (int)(Chain.GetBalance(addr, TestCurrency).MajorUnit & int.MaxValue);
return Transaction.Create(
nonce,
Expand Down Expand Up @@ -216,7 +216,7 @@ private IImmutableSet<Address> GetRandomAddresses(int seed)
return Enumerable.Range(0, random.Next(PrivateKeys.Length - 1) + 1)
.Aggregate(
ImmutableHashSet<Address>.Empty,
(arr, _) => arr.Add(PrivateKeys[random.Next(PrivateKeys.Length)].ToAddress()));
(arr, _) => arr.Add(PrivateKeys[random.Next(PrivateKeys.Length)].Address));
}

private void AddBlock(
Expand All @@ -243,7 +243,7 @@ private void AddBlock(
0,
block.Hash,
PrivateKeys
.OrderBy(pk => pk.ToAddress().ToHex())
.OrderBy(pk => pk.Address.ToHex())
.Select(pk => new VoteMetadata(
Chain.Tip.Index + 1,
0,
Expand All @@ -252,7 +252,7 @@ private void AddBlock(
pk.PublicKey,
VoteFlag.PreCommit).Sign(pk)).ToImmutableArray()));
MinedBlocks =
MinedBlocks.SetItem(pk.ToAddress(), MinedBlocks[pk.ToAddress()].Add(block));
MinedBlocks.SetItem(pk.Address, MinedBlocks[pk.Address].Add(block));
SignedTxs = transactions.Aggregate(
SignedTxs,
(dict, tx) =>
Expand Down
8 changes: 4 additions & 4 deletions Libplanet.Explorer.Tests/Indexing/BlockChainIndexTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ await index.SynchronizeAsync(
0,
divergentBlock.Hash,
ChainFx.PrivateKeys
.OrderBy(pk => pk.ToAddress().ToHex())
.OrderBy(pk => pk.Address.ToHex())
.Select(pk => new VoteMetadata(
forkedChain.Tip.Index + 1,
0,
Expand Down Expand Up @@ -85,7 +85,7 @@ public async Task GetLastNonceByAddress()
{
foreach (var pk in ChainFx.PrivateKeys)
{
var address = pk.ToAddress();
var address = pk.Address;
Assert.Equal(
ChainFx.Chain.GetNextTxNonce(address) - 1,
// ReSharper disable once MethodHasAsyncOverload
Expand Down Expand Up @@ -251,7 +251,7 @@ public async Task GetBlockHashesByMiner(bool fromHalfway, bool throughHalfway, b
{
foreach (var pk in ChainFx.PrivateKeys)
{
var address = pk.ToAddress();
var address = pk.Address;
var inChain = ChainFx.MinedBlocks[address].ToArray();
inChain = desc ? inChain.Reverse().ToArray() : inChain;
int? fromHeight = fromHalfway ? inChain.Length / 4 : null;
Expand Down Expand Up @@ -305,7 +305,7 @@ public async Task GetSignedTxIdsByAddress(bool fromHalfway, bool throughHalfway,
{
foreach (var pk in ChainFx.PrivateKeys)
{
var address = pk.ToAddress();
var address = pk.Address;
var inChain = ChainFx.SignedTxs[address].ToArray();
inChain = desc ? inChain.Reverse().ToArray() : inChain;
int? fromNonce = fromHalfway ? inChain.Length / 4 : null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public async Task TransactionResult()
var successTx = successBlock.Transactions.First();
var pk = Fx.PrivateKeys[0];
var stagingTx = Transaction.Create(
Fx.Chain.GetNextTxNonce(pk.ToAddress()),
Fx.Chain.GetNextTxNonce(pk.Address),
pk,
Fx.Chain.Genesis.Hash,
ImmutableArray<SimpleAction>.Empty.Add(new SimpleAction1()).ToPlainValues());
Expand Down Expand Up @@ -89,11 +89,11 @@ public virtual async Task Transactions()
{
var allBlocks = Fx.Chain.IterateBlocks().ToImmutableArray();
await AssertTransactionsQueryPermutation(allBlocks, null, null);
foreach (var signer in Fx.PrivateKeys.Select(pk => pk.ToAddress()))
foreach (var signer in Fx.PrivateKeys.Select(pk => pk.Address))
{
await AssertTransactionsQueryPermutation(allBlocks, signer, null);
await AssertTransactionsQueryPermutation(allBlocks, null, signer);
foreach (var involved in Fx.PrivateKeys.Select(pk => pk.ToAddress()))
foreach (var involved in Fx.PrivateKeys.Select(pk => pk.Address))
{
await AssertTransactionsQueryPermutation(allBlocks, signer, involved);
}
Expand Down
Loading
Loading