Skip to content
Draft
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
21 changes: 21 additions & 0 deletions Libplanet.sln
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Libplanet.Node.Executable",
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Libplanet.Node.Tests", "sdk\node\Libplanet.Node.Tests\Libplanet.Node.Tests.csproj", "{C050C5F0-8A40-4CB1-9715-A55EBF94FBF2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Libplanet.Crypto.Nethereum", "src\Libplanet.Crypto.Nethereum\Libplanet.Crypto.Nethereum.csproj", "{42801CB6-D452-408C-B0A5-970D8D997B38}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -697,6 +699,24 @@ Global
{C050C5F0-8A40-4CB1-9715-A55EBF94FBF2}.Release|x64.Build.0 = Release|Any CPU
{C050C5F0-8A40-4CB1-9715-A55EBF94FBF2}.Release|x86.ActiveCfg = Release|Any CPU
{C050C5F0-8A40-4CB1-9715-A55EBF94FBF2}.Release|x86.Build.0 = Release|Any CPU
{42801CB6-D452-408C-B0A5-970D8D997B38}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{42801CB6-D452-408C-B0A5-970D8D997B38}.Debug|Any CPU.Build.0 = Debug|Any CPU
{42801CB6-D452-408C-B0A5-970D8D997B38}.Debug|x64.ActiveCfg = Debug|Any CPU
{42801CB6-D452-408C-B0A5-970D8D997B38}.Debug|x64.Build.0 = Debug|Any CPU
{42801CB6-D452-408C-B0A5-970D8D997B38}.Debug|x86.ActiveCfg = Debug|Any CPU
{42801CB6-D452-408C-B0A5-970D8D997B38}.Debug|x86.Build.0 = Debug|Any CPU
{42801CB6-D452-408C-B0A5-970D8D997B38}.ReleaseMono|Any CPU.ActiveCfg = Debug|Any CPU
{42801CB6-D452-408C-B0A5-970D8D997B38}.ReleaseMono|Any CPU.Build.0 = Debug|Any CPU
{42801CB6-D452-408C-B0A5-970D8D997B38}.ReleaseMono|x64.ActiveCfg = Debug|Any CPU
{42801CB6-D452-408C-B0A5-970D8D997B38}.ReleaseMono|x64.Build.0 = Debug|Any CPU
{42801CB6-D452-408C-B0A5-970D8D997B38}.ReleaseMono|x86.ActiveCfg = Debug|Any CPU
{42801CB6-D452-408C-B0A5-970D8D997B38}.ReleaseMono|x86.Build.0 = Debug|Any CPU
{42801CB6-D452-408C-B0A5-970D8D997B38}.Release|Any CPU.ActiveCfg = Release|Any CPU
{42801CB6-D452-408C-B0A5-970D8D997B38}.Release|Any CPU.Build.0 = Release|Any CPU
{42801CB6-D452-408C-B0A5-970D8D997B38}.Release|x64.ActiveCfg = Release|Any CPU
{42801CB6-D452-408C-B0A5-970D8D997B38}.Release|x64.Build.0 = Release|Any CPU
{42801CB6-D452-408C-B0A5-970D8D997B38}.Release|x86.ActiveCfg = Release|Any CPU
{42801CB6-D452-408C-B0A5-970D8D997B38}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -737,6 +757,7 @@ Global
{704BA731-9C70-4CBE-A607-1A2E1FB73753} = {8CA69CC9-3415-4484-9342-88D495AE2FF6}
{A0EAD8F0-B7A3-4112-9F3F-2D9922A500BA} = {8CA69CC9-3415-4484-9342-88D495AE2FF6}
{C050C5F0-8A40-4CB1-9715-A55EBF94FBF2} = {8CA69CC9-3415-4484-9342-88D495AE2FF6}
{42801CB6-D452-408C-B0A5-970D8D997B38} = {AC908E33-B856-4E23-9F81-B7F7C97A07F9}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {DB552D2A-94E1-4A1C-9F3E-E0097C6158CD}
Expand Down
22 changes: 22 additions & 0 deletions src/Libplanet.Crypto.Nethereum/Libplanet.Crypto.Nethereum.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<LangVersion>10</LangVersion>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<NoWarn>$(NoWarn);NU5104</NoWarn>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Nethereum.Hex" Version="4.29.0" />
<PackageReference Include="Nethereum.Util" Version="4.29.0" />
<PackageReference Include="Nethereum.Signer" Version="4.29.0" />
</ItemGroup>

<ItemGroup Condition="'$(SkipSonar)' != 'true'">
<PackageReference Remove="SonarAnalyzer.CSharp" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Libplanet\Libplanet.csproj" />
</ItemGroup>
</Project>
44 changes: 44 additions & 0 deletions src/Libplanet.Crypto.Nethereum/NethereumCryptoBackend.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Security.Cryptography;
using Libplanet;
using Libplanet.Common;
using Libplanet.Crypto;
using EthECKey = Nethereum.Signer.EthECKey;
using EthereumMessageSigner = Nethereum.Signer.EthereumMessageSigner;

namespace Libplanet.Crypto.Nethereum;

public class NethereumCryptoBackend<T> : ICryptoBackend<T>
where T : HashAlgorithm
{
private readonly EthereumMessageSigner _signer = new EthereumMessageSigner();
private readonly object _instanceLock = new object();

public byte[] Sign(HashDigest<T> messageHash, PrivateKey privateKey)
{
lock (_instanceLock)
{
var key = new EthECKey(privateKey.ToByteArray(), isPrivate: true);
var signature = _signer.Sign(messageHash.ToByteArray(), key);
if (signature.StartsWith("0x"))
{
signature = signature.Substring(2);
}

return ByteUtil.ParseHex(signature);
}
}

public bool Verify(
HashDigest<T> messageHash,
byte[] signature,
PublicKey publicKey)
{
lock (_instanceLock)
{
var address = _signer.EcRecover(messageHash.ToByteArray(), ByteUtil.Hex(signature));
return publicKey.Address.Equals(new Address(address));
}
}
}
Loading