From 3a54af1a6bec094a09753371ff9ad60de8b0ceb8 Mon Sep 17 00:00:00 2001 From: Hyun Seungmin Date: Mon, 27 Nov 2023 18:19:26 +0900 Subject: [PATCH] Merge pull request #3688 from planetarium/fix/private-key-error fix: private key hex with zero paddings (cherry picked from commit dda7ec43a579bae0648de7c1d750768317a33c92) --- .../Nekoyume/Editor/HeadlessTool.cs | 3 ++- .../Nekoyume/Editor/LibplanetEditor.cs | 3 ++- .../Extensions/PrivateKeyExtensionsTest.cs | 23 +++++++++++++++++++ .../PrivateKeyExtensionsTest.cs.meta | 3 +++ .../Extensions/PrivateKeyExtensions.cs | 14 +++++++++++ .../Extensions/PrivateKeyExtensions.cs.meta | 3 +++ nekoyume/Assets/_Scripts/Game/Game.cs | 14 +++++------ .../Assets/_Scripts/Helper/HeadlessHelper.cs | 2 +- .../_Scripts/UI/Widget/Popup/SettingPopup.cs | 11 +++++---- 9 files changed, 61 insertions(+), 15 deletions(-) create mode 100644 nekoyume/Assets/Tests/EditMode/Extensions/PrivateKeyExtensionsTest.cs create mode 100644 nekoyume/Assets/Tests/EditMode/Extensions/PrivateKeyExtensionsTest.cs.meta create mode 100644 nekoyume/Assets/_Scripts/Extensions/PrivateKeyExtensions.cs create mode 100644 nekoyume/Assets/_Scripts/Extensions/PrivateKeyExtensions.cs.meta diff --git a/nekoyume/Assets/Planetarium/Nekoyume/Editor/HeadlessTool.cs b/nekoyume/Assets/Planetarium/Nekoyume/Editor/HeadlessTool.cs index 5220700d717..95e3347d0d1 100644 --- a/nekoyume/Assets/Planetarium/Nekoyume/Editor/HeadlessTool.cs +++ b/nekoyume/Assets/Planetarium/Nekoyume/Editor/HeadlessTool.cs @@ -5,6 +5,7 @@ using System.IO; using System.Runtime.InteropServices; using Libplanet.Common; +using Nekoyume; using Nekoyume.Blockchain; using UnityEditor; using UnityEngine; @@ -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"; diff --git a/nekoyume/Assets/Planetarium/Nekoyume/Editor/LibplanetEditor.cs b/nekoyume/Assets/Planetarium/Nekoyume/Editor/LibplanetEditor.cs index 39d9997f520..6b32213e237 100644 --- a/nekoyume/Assets/Planetarium/Nekoyume/Editor/LibplanetEditor.cs +++ b/nekoyume/Assets/Planetarium/Nekoyume/Editor/LibplanetEditor.cs @@ -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; @@ -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; } diff --git a/nekoyume/Assets/Tests/EditMode/Extensions/PrivateKeyExtensionsTest.cs b/nekoyume/Assets/Tests/EditMode/Extensions/PrivateKeyExtensionsTest.cs new file mode 100644 index 00000000000..439c0988339 --- /dev/null +++ b/nekoyume/Assets/Tests/EditMode/Extensions/PrivateKeyExtensionsTest.cs @@ -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(() => new PrivateKey(ByteUtil.Hex(privateKey.ByteArray))); + Assert.AreEqual(hexWithZeroPaddings, privateKey.ToHexWithZeroPaddings()); + Assert.DoesNotThrow(() => new PrivateKey(privateKey.ToHexWithZeroPaddings())); + } + } +} diff --git a/nekoyume/Assets/Tests/EditMode/Extensions/PrivateKeyExtensionsTest.cs.meta b/nekoyume/Assets/Tests/EditMode/Extensions/PrivateKeyExtensionsTest.cs.meta new file mode 100644 index 00000000000..07f26a7ce7d --- /dev/null +++ b/nekoyume/Assets/Tests/EditMode/Extensions/PrivateKeyExtensionsTest.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: b35e5ce4973b4cf4acd1f5f926f4ceb5 +timeCreated: 1701069872 \ No newline at end of file diff --git a/nekoyume/Assets/_Scripts/Extensions/PrivateKeyExtensions.cs b/nekoyume/Assets/_Scripts/Extensions/PrivateKeyExtensions.cs new file mode 100644 index 00000000000..e487450ae8c --- /dev/null +++ b/nekoyume/Assets/_Scripts/Extensions/PrivateKeyExtensions.cs @@ -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'); + } + } +} diff --git a/nekoyume/Assets/_Scripts/Extensions/PrivateKeyExtensions.cs.meta b/nekoyume/Assets/_Scripts/Extensions/PrivateKeyExtensions.cs.meta new file mode 100644 index 00000000000..ef3651de1f0 --- /dev/null +++ b/nekoyume/Assets/_Scripts/Extensions/PrivateKeyExtensions.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: c0634000fa1c45dfbdc18efd7fa25086 +timeCreated: 1701069559 \ No newline at end of file diff --git a/nekoyume/Assets/_Scripts/Game/Game.cs b/nekoyume/Assets/_Scripts/Game/Game.cs index dbf9afb3458..c38e46171db 100644 --- a/nekoyume/Assets/_Scripts/Game/Game.cs +++ b/nekoyume/Assets/_Scripts/Game/Game.cs @@ -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 @@ -1508,7 +1508,7 @@ private IEnumerator CoLogin(PlanetContext planetContext, Action 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()})."); } @@ -1547,7 +1547,7 @@ private IEnumerator CoLogin(PlanetContext planetContext, Action 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()})."); @@ -1571,7 +1571,7 @@ private IEnumerator CoLogin(PlanetContext planetContext, Action callback) introScreen.SetData( _commandLineOptions.KeyStorePath, - ByteUtil.Hex(pk.ByteArray), + pk.ToHexWithZeroPaddings(), planetContext); } else @@ -1592,7 +1592,7 @@ private IEnumerator CoLogin(PlanetContext planetContext, Action callback) var pk = loginSystem.GetPrivateKey(); introScreen.Show( _commandLineOptions.KeyStorePath, - ByteUtil.Hex(pk.ByteArray), + pk.ToHexWithZeroPaddings(), planetContext); Debug.Log("[Game] CoLogin()... WaitUntil introScreen.OnClickStart."); @@ -1600,7 +1600,7 @@ private IEnumerator CoLogin(PlanetContext planetContext, Action callback) 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()})."); @@ -1809,7 +1809,7 @@ private IEnumerator CoLogin(PlanetContext planetContext, Action 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()})."); diff --git a/nekoyume/Assets/_Scripts/Helper/HeadlessHelper.cs b/nekoyume/Assets/_Scripts/Helper/HeadlessHelper.cs index f885012ab3c..0b23669d622 100644 --- a/nekoyume/Assets/_Scripts/Helper/HeadlessHelper.cs +++ b/nekoyume/Assets/_Scripts/Helper/HeadlessHelper.cs @@ -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")); diff --git a/nekoyume/Assets/_Scripts/UI/Widget/Popup/SettingPopup.cs b/nekoyume/Assets/_Scripts/UI/Widget/Popup/SettingPopup.cs index b1b53149f25..8bba922b234 100644 --- a/nekoyume/Assets/_Scripts/UI/Widget/Popup/SettingPopup.cs +++ b/nekoyume/Assets/_Scripts/UI/Widget/Popup/SettingPopup.cs @@ -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(); } }