Skip to content

Commit

Permalink
Merge pull request #3692 from planetarium/cherry-pick-3688
Browse files Browse the repository at this point in the history
[cherry pick]fix: private key hex with zero paddings
  • Loading branch information
jonny-jeahyunchoi authored Nov 27, 2023
2 parents 2dd9872 + 3a54af1 commit f5d2bfd
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 15 deletions.
3 changes: 2 additions & 1 deletion nekoyume/Assets/Planetarium/Nekoyume/Editor/HeadlessTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.IO;
using System.Runtime.InteropServices;
using Libplanet.Common;
using Nekoyume;
using Nekoyume.Blockchain;
using UnityEditor;
using UnityEngine;
Expand Down Expand Up @@ -195,7 +196,7 @@ public static void Initialize()
$"run -c DevEx --project NineChronicles.Headless.Executable -C appsettings.local.json --genesis-block-path {Path.Combine(_genesisPath, "genesis-block")} --store-path {Path.Combine(_docsRoot, "planetarium", _storeName)} --store-type memory",
};

var pkHex = ByteUtil.Hex(Agent.ProposerKey.ByteArray);
var pkHex = Agent.ProposerKey.ToHexWithZeroPaddings();
startInfo.Arguments +=
$" --miner-private-key {pkHex} --consensus-private-key {pkHex} --consensus-seed {Agent.ProposerKey.PublicKey},localhost,60000";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.IO;
using Libplanet.Common;
using Libplanet.Crypto;
using Nekoyume;
using Nekoyume.Blockchain;
using Nekoyume.Model;
using Nekoyume.Model.State;
Expand All @@ -15,7 +16,7 @@ public static class LibplanetEditor
private static PublicKey GetOrCreateInitialValidator()
{
var pk = Agent.ProposerKey;
Debug.Log($"Private Key of initialValidator: {ByteUtil.Hex(pk.ByteArray)}");
Debug.Log($"Private Key of initialValidator: {pk.ToHexWithZeroPaddings()}");
Debug.Log($"Public Key of initialValidator: {pk.PublicKey}");
return pk.PublicKey;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using Libplanet.Common;
using Libplanet.Crypto;
using Nekoyume;
using NUnit.Framework;

namespace Tests.EditMode.Extensions
{
public class PrivateKeyExtensionsTest
{
[Test]
public void ToHexWithZeroPaddings()
{
const string hexWithZeroPaddings =
"00000102030405060708090a0102030405060708090b0102030405060708090c";
var privateKey = new PrivateKey(hexWithZeroPaddings);
Assert.AreNotEqual(hexWithZeroPaddings, ByteUtil.Hex(privateKey.ByteArray));
Assert.Throws<ArgumentOutOfRangeException>(() => new PrivateKey(ByteUtil.Hex(privateKey.ByteArray)));
Assert.AreEqual(hexWithZeroPaddings, privateKey.ToHexWithZeroPaddings());
Assert.DoesNotThrow(() => new PrivateKey(privateKey.ToHexWithZeroPaddings()));
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions nekoyume/Assets/_Scripts/Extensions/PrivateKeyExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Libplanet.Common;
using Libplanet.Crypto;

namespace Nekoyume
{
public static class PrivateKeyExtensions
{
public static string ToHexWithZeroPaddings(this PrivateKey privateKey)
{
var hex = ByteUtil.Hex(privateKey.ByteArray);
return hex.PadLeft(64, '0');
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions nekoyume/Assets/_Scripts/Game/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ private IEnumerator Start()
{
Debug.Log("[Game] Start()... CommandLineOptions.PrivateKey is empty." +
" Set local private key instead.");
_commandLineOptions.PrivateKey = ByteUtil.Hex(loginSystem.GetPrivateKey().ByteArray);
_commandLineOptions.PrivateKey = loginSystem.GetPrivateKey().ToHexWithZeroPaddings();
}
}
#endif
Expand Down Expand Up @@ -1508,7 +1508,7 @@ private IEnumerator CoLogin(PlanetContext planetContext, Action<bool> callback)
Debug.Log("[Game] CoLogin()... WaitUntil LoginPopup.Login. Done.");

// NOTE: Update CommandlineOptions.PrivateKey finally.
_commandLineOptions.PrivateKey = ByteUtil.Hex(loginSystem.GetPrivateKey().ByteArray);
_commandLineOptions.PrivateKey = loginSystem.GetPrivateKey().ToHexWithZeroPaddings();
Debug.Log("[Game] CoLogin()... CommandLineOptions.PrivateKey finally updated" +
$" to ({loginSystem.GetPrivateKey().ToAddress()}).");
}
Expand Down Expand Up @@ -1547,7 +1547,7 @@ private IEnumerator CoLogin(PlanetContext planetContext, Action<bool> callback)
var pk = loginSystem.GetPrivateKey();

// NOTE: Update CommandlineOptions.PrivateKey.
_commandLineOptions.PrivateKey = ByteUtil.Hex(pk.ByteArray);
_commandLineOptions.PrivateKey = pk.ToHexWithZeroPaddings();
Debug.Log("[Game] CoLogin()... CommandLineOptions.PrivateKey updated" +
$" to ({pk.ToAddress()}).");

Expand All @@ -1571,7 +1571,7 @@ private IEnumerator CoLogin(PlanetContext planetContext, Action<bool> callback)

introScreen.SetData(
_commandLineOptions.KeyStorePath,
ByteUtil.Hex(pk.ByteArray),
pk.ToHexWithZeroPaddings(),
planetContext);
}
else
Expand All @@ -1592,15 +1592,15 @@ private IEnumerator CoLogin(PlanetContext planetContext, Action<bool> callback)
var pk = loginSystem.GetPrivateKey();
introScreen.Show(
_commandLineOptions.KeyStorePath,
ByteUtil.Hex(pk.ByteArray),
pk.ToHexWithZeroPaddings(),
planetContext);

Debug.Log("[Game] CoLogin()... WaitUntil introScreen.OnClickStart.");
yield return introScreen.OnClickStart.AsObservable().First().StartAsCoroutine();
Debug.Log("[Game] CoLogin()... WaitUntil introScreen.OnClickStart. Done.");

// NOTE: Update CommandlineOptions.PrivateKey finally.
_commandLineOptions.PrivateKey = ByteUtil.Hex(pk.ByteArray);
_commandLineOptions.PrivateKey = pk.ToHexWithZeroPaddings();
Debug.Log("[Game] CoLogin()... CommandLineOptions.PrivateKey finally updated" +
$" to ({pk.ToAddress()}).");

Expand Down Expand Up @@ -1809,7 +1809,7 @@ private IEnumerator CoLogin(PlanetContext planetContext, Action<bool> callback)
Debug.Log("[Game] CoLogin()... WaitUntil loginPopup.Login. Done.");

// NOTE: Update CommandlineOptions.PrivateKey finally.
_commandLineOptions.PrivateKey = ByteUtil.Hex(loginSystem.GetPrivateKey().ByteArray);
_commandLineOptions.PrivateKey = loginSystem.GetPrivateKey().ToHexWithZeroPaddings();
Debug.Log("[Game] CoLogin()... CommandLineOptions.PrivateKey finally updated" +
$" to ({loginSystem.GetPrivateKey().ToAddress()}).");

Expand Down
2 changes: 1 addition & 1 deletion nekoyume/Assets/_Scripts/Helper/HeadlessHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static bool CheckHeadlessSettings()

public static void RunLocalHeadless()
{
var pkHex = ByteUtil.Hex(Agent.ProposerKey.ByteArray);
var pkHex = Agent.ProposerKey.ToHexWithZeroPaddings();
try
{
Debug.Log(Path.Combine(_genesisPath, "genesis-block"));
Expand Down
11 changes: 6 additions & 5 deletions nekoyume/Assets/_Scripts/UI/Widget/Popup/SettingPopup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,20 +215,21 @@ public override void Show(bool ignoreStartAnimation = false)
if (!(_privateKey is null))
{
addressContentInputField.text = _privateKey.ToAddress().ToString();
privateKeyContentInputField.text = ByteUtil.Hex(_privateKey.ByteArray);
privateKeyContentInputField.text = _privateKey.ToHexWithZeroPaddings();
}
else
{
if (Game.Game.instance.Agent.PrivateKey is null)
var agent = Game.Game.instance.Agent;
if (agent?.PrivateKey is null)
{
addressContentInputField.text = string.Empty;
privateKeyContentInputField.text = string.Empty;
}
else
{
addressContentInputField.text = Game.Game.instance.Agent.Address.ToString();
privateKeyContentInputField.text =
ByteUtil.Hex(Game.Game.instance.Agent.PrivateKey.ByteArray);

addressContentInputField.text = agent.Address.ToString();
privateKeyContentInputField.text = agent.PrivateKey.ToHexWithZeroPaddings();
}
}

Expand Down

0 comments on commit f5d2bfd

Please sign in to comment.