Skip to content

Commit e439a88

Browse files
committed
wip
1 parent 3d92231 commit e439a88

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+314
-677
lines changed

Docs/docfx.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"src/Libplanet.Action/Libplanet.Action.csproj",
99
"src/Libplanet.Common/Libplanet.Common.csproj",
1010
"src/Libplanet.Crypto/Libplanet.Crypto.csproj",
11-
"src/Libplanet.Crypto.Secp256k1/Libplanet.Crypto.Secp256k1.csproj",
11+
"src/Libplanet.Crypto.Nethereum/Libplanet.Crypto.Nethereum.csproj",
1212
"tools/Libplanet.Explorer.Cocona/Libplanet.Explorer.Cocona.csproj",
1313
"tools/Libplanet.Extensions.Cocona/Libplanet.Extensions.Cocona.csproj",
1414
"test/Libplanet.Mocks/Libplanet.Mocks.csproj",

Libplanet.sln

-42
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Libplanet.Net", "src\Libpla
3737
EndProject
3838
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Libplanet.Net.Tests", "test\Libplanet.Net.Tests\Libplanet.Net.Tests.csproj", "{6D7A63C9-16AB-4B7E-B9C0-0956E1E02610}"
3939
EndProject
40-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Libplanet.Crypto.Secp256k1", "src\Libplanet.Crypto.Secp256k1\Libplanet.Crypto.Secp256k1.csproj", "{9F88C871-CB37-4A88-BACF-540AC253C202}"
41-
EndProject
42-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Libplanet.Crypto.Secp256k1.Tests", "test\Libplanet.Crypto.Secp256k1.Tests\Libplanet.Crypto.Secp256k1.Tests.csproj", "{B1A38DDE-5534-4625-A3F2-A585BA7A1198}"
43-
EndProject
4440
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Libplanet.Explorer.Cocona", "tools\Libplanet.Explorer.Cocona\Libplanet.Explorer.Cocona.csproj", "{8698E0C2-1A82-43E6-8A26-3D9A825CF574}"
4541
EndProject
4642
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Libplanet.Explorer.Cocona.Tests", "test\Libplanet.Explorer.Cocona.Tests\Libplanet.Explorer.Cocona.Tests.csproj", "{F782BC86-9CE6-4F69-8F77-710A399CB54F}"
@@ -393,42 +389,6 @@ Global
393389
{6D7A63C9-16AB-4B7E-B9C0-0956E1E02610}.Release|x64.Build.0 = Release|Any CPU
394390
{6D7A63C9-16AB-4B7E-B9C0-0956E1E02610}.Release|x86.ActiveCfg = Release|Any CPU
395391
{6D7A63C9-16AB-4B7E-B9C0-0956E1E02610}.Release|x86.Build.0 = Release|Any CPU
396-
{9F88C871-CB37-4A88-BACF-540AC253C202}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
397-
{9F88C871-CB37-4A88-BACF-540AC253C202}.Debug|Any CPU.Build.0 = Debug|Any CPU
398-
{9F88C871-CB37-4A88-BACF-540AC253C202}.Debug|x64.ActiveCfg = Debug|Any CPU
399-
{9F88C871-CB37-4A88-BACF-540AC253C202}.Debug|x64.Build.0 = Debug|Any CPU
400-
{9F88C871-CB37-4A88-BACF-540AC253C202}.Debug|x86.ActiveCfg = Debug|Any CPU
401-
{9F88C871-CB37-4A88-BACF-540AC253C202}.Debug|x86.Build.0 = Debug|Any CPU
402-
{9F88C871-CB37-4A88-BACF-540AC253C202}.ReleaseMono|Any CPU.ActiveCfg = Debug|Any CPU
403-
{9F88C871-CB37-4A88-BACF-540AC253C202}.ReleaseMono|Any CPU.Build.0 = Debug|Any CPU
404-
{9F88C871-CB37-4A88-BACF-540AC253C202}.ReleaseMono|x64.ActiveCfg = Debug|Any CPU
405-
{9F88C871-CB37-4A88-BACF-540AC253C202}.ReleaseMono|x64.Build.0 = Debug|Any CPU
406-
{9F88C871-CB37-4A88-BACF-540AC253C202}.ReleaseMono|x86.ActiveCfg = Debug|Any CPU
407-
{9F88C871-CB37-4A88-BACF-540AC253C202}.ReleaseMono|x86.Build.0 = Debug|Any CPU
408-
{9F88C871-CB37-4A88-BACF-540AC253C202}.Release|Any CPU.ActiveCfg = Release|Any CPU
409-
{9F88C871-CB37-4A88-BACF-540AC253C202}.Release|Any CPU.Build.0 = Release|Any CPU
410-
{9F88C871-CB37-4A88-BACF-540AC253C202}.Release|x64.ActiveCfg = Release|Any CPU
411-
{9F88C871-CB37-4A88-BACF-540AC253C202}.Release|x64.Build.0 = Release|Any CPU
412-
{9F88C871-CB37-4A88-BACF-540AC253C202}.Release|x86.ActiveCfg = Release|Any CPU
413-
{9F88C871-CB37-4A88-BACF-540AC253C202}.Release|x86.Build.0 = Release|Any CPU
414-
{B1A38DDE-5534-4625-A3F2-A585BA7A1198}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
415-
{B1A38DDE-5534-4625-A3F2-A585BA7A1198}.Debug|Any CPU.Build.0 = Debug|Any CPU
416-
{B1A38DDE-5534-4625-A3F2-A585BA7A1198}.Debug|x64.ActiveCfg = Debug|Any CPU
417-
{B1A38DDE-5534-4625-A3F2-A585BA7A1198}.Debug|x64.Build.0 = Debug|Any CPU
418-
{B1A38DDE-5534-4625-A3F2-A585BA7A1198}.Debug|x86.ActiveCfg = Debug|Any CPU
419-
{B1A38DDE-5534-4625-A3F2-A585BA7A1198}.Debug|x86.Build.0 = Debug|Any CPU
420-
{B1A38DDE-5534-4625-A3F2-A585BA7A1198}.ReleaseMono|Any CPU.ActiveCfg = Debug|Any CPU
421-
{B1A38DDE-5534-4625-A3F2-A585BA7A1198}.ReleaseMono|Any CPU.Build.0 = Debug|Any CPU
422-
{B1A38DDE-5534-4625-A3F2-A585BA7A1198}.ReleaseMono|x64.ActiveCfg = Debug|Any CPU
423-
{B1A38DDE-5534-4625-A3F2-A585BA7A1198}.ReleaseMono|x64.Build.0 = Debug|Any CPU
424-
{B1A38DDE-5534-4625-A3F2-A585BA7A1198}.ReleaseMono|x86.ActiveCfg = Debug|Any CPU
425-
{B1A38DDE-5534-4625-A3F2-A585BA7A1198}.ReleaseMono|x86.Build.0 = Debug|Any CPU
426-
{B1A38DDE-5534-4625-A3F2-A585BA7A1198}.Release|Any CPU.ActiveCfg = Release|Any CPU
427-
{B1A38DDE-5534-4625-A3F2-A585BA7A1198}.Release|Any CPU.Build.0 = Release|Any CPU
428-
{B1A38DDE-5534-4625-A3F2-A585BA7A1198}.Release|x64.ActiveCfg = Release|Any CPU
429-
{B1A38DDE-5534-4625-A3F2-A585BA7A1198}.Release|x64.Build.0 = Release|Any CPU
430-
{B1A38DDE-5534-4625-A3F2-A585BA7A1198}.Release|x86.ActiveCfg = Release|Any CPU
431-
{B1A38DDE-5534-4625-A3F2-A585BA7A1198}.Release|x86.Build.0 = Release|Any CPU
432392
{8698E0C2-1A82-43E6-8A26-3D9A825CF574}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
433393
{8698E0C2-1A82-43E6-8A26-3D9A825CF574}.Debug|Any CPU.Build.0 = Debug|Any CPU
434394
{8698E0C2-1A82-43E6-8A26-3D9A825CF574}.Debug|x64.ActiveCfg = Debug|Any CPU
@@ -739,8 +699,6 @@ Global
739699
{A43E44E5-F9C1-44BD-A593-419EC113117B} = {B9C00FAF-36CF-463A-83FA-43E6B974AE2E}
740700
{2E17A091-DC29-41FB-ABC8-767BD75FFB07} = {AC908E33-B856-4E23-9F81-B7F7C97A07F9}
741701
{6D7A63C9-16AB-4B7E-B9C0-0956E1E02610} = {B9C00FAF-36CF-463A-83FA-43E6B974AE2E}
742-
{9F88C871-CB37-4A88-BACF-540AC253C202} = {AC908E33-B856-4E23-9F81-B7F7C97A07F9}
743-
{B1A38DDE-5534-4625-A3F2-A585BA7A1198} = {B9C00FAF-36CF-463A-83FA-43E6B974AE2E}
744702
{8698E0C2-1A82-43E6-8A26-3D9A825CF574} = {88E7FAF4-CEEC-48B6-9114-71CFE3FC0F50}
745703
{F782BC86-9CE6-4F69-8F77-710A399CB54F} = {B9C00FAF-36CF-463A-83FA-43E6B974AE2E}
746704
{763BAD3E-3244-4E8F-8182-2BF35774262A} = {AC908E33-B856-4E23-9F81-B7F7C97A07F9}

sdk/node/Libplanet.Node.Executable/Libplanet.Node.Executable.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
<ItemGroup>
2929
<ProjectReference Include="..\..\..\tools\Libplanet.Explorer.Executable\Libplanet.Explorer.Executable.csproj" />
30-
<ProjectReference Include="..\..\..\src\Libplanet.Crypto.Secp256k1\Libplanet.Crypto.Secp256k1.csproj" />
30+
<ProjectReference Include="..\..\..\src\Libplanet.Crypto.Nethereum\Libplanet.Crypto.Nethereum.csproj" />
3131
<ProjectReference Include="..\Libplanet.Node.Extensions\Libplanet.Node.Extensions.csproj" />
3232
<ProjectReference Include="..\Libplanet.Node\Libplanet.Node.csproj" />
3333
</ItemGroup>

src/Libplanet.Crypto.Nethereum/Libplanet.Crypto.Nethereum.csproj

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
<ItemGroup>
1010
<PackageReference Include="Nethereum.Hex" Version="4.29.0" />
11-
<PackageReference Include="Nethereum.Util" Version="4.29.0" />
1211
<PackageReference Include="Nethereum.Signer" Version="4.29.0" />
1312
</ItemGroup>
1413

src/Libplanet.Crypto.Nethereum/NethereumCryptoBackend.cs

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
using System.Diagnostics.CodeAnalysis;
2-
using System.Linq;
31
using System.Security.Cryptography;
4-
using Libplanet;
52
using Libplanet.Common;
6-
using Libplanet.Crypto;
73
using EthECKey = Nethereum.Signer.EthECKey;
84
using EthereumMessageSigner = Nethereum.Signer.EthereumMessageSigner;
95

@@ -33,12 +29,12 @@ public byte[] Sign(HashDigest<T> messageHash, PrivateKey privateKey)
3329
public bool Verify(
3430
HashDigest<T> messageHash,
3531
byte[] signature,
36-
PublicKey publicKey)
32+
Address signer)
3733
{
3834
lock (_instanceLock)
3935
{
4036
var address = _signer.EcRecover(messageHash.ToByteArray(), ByteUtil.Hex(signature));
41-
return publicKey.Address.Equals(new Address(address));
37+
return signer.Equals(new Address(address));
4238
}
4339
}
4440
}

src/Libplanet.Crypto.Secp256k1/Libplanet.Crypto.Secp256k1.csproj

-21
This file was deleted.

src/Libplanet.Crypto.Secp256k1/Secp256k1CryptoBackend.cs

-69
This file was deleted.

src/Libplanet.Crypto/Address.cs

+37-21
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
using System.Diagnostics.Contracts;
66
using System.Globalization;
77
using System.Linq;
8+
using System.Security.Cryptography;
89
using System.Text;
910
using System.Text.Json;
1011
using System.Text.Json.Serialization;
1112
using Bencodex;
1213
using Bencodex.Types;
1314
using Libplanet.Common;
14-
using Org.BouncyCastle.Crypto.Digests;
1515

1616
namespace Libplanet.Crypto
1717
{
@@ -259,35 +259,51 @@ public int CompareTo(object? obj) => obj is Address other
259259

260260
private static string ToChecksumAddress(string hex)
261261
{
262-
byte[] bytes = Encoding.ASCII.GetBytes(hex);
263-
byte[] hash = CalculateHash(bytes);
264-
string hashHex = ByteUtil.Hex(hash);
265-
string address = string.Empty;
266-
267-
for (var i = 0; i < hex.Length; i++)
262+
var value = new Nethereum.Util.AddressUtil().ConvertToChecksumAddress(hex);
263+
if (value.StartsWith("0x"))
268264
{
269-
char c = hex[i];
270-
address += (hashHex[i] >= '8') ? char.ToUpper(c, CultureInfo.InvariantCulture) : c;
265+
return value.Substring(2);
271266
}
272267

273-
return address;
268+
return value;
274269
}
275270

276-
private static byte[] CalculateHash(byte[] value)
277-
{
278-
var digest = new KeccakDigest(256);
279-
var output = new byte[digest.GetDigestSize()];
280-
digest.BlockUpdate(value, 0, value.Length);
281-
digest.DoFinal(output, 0);
282-
return output;
283-
}
271+
// private static byte[] CalculateHash(byte[] value)
272+
// {
273+
// var bytes = address.ToByteArray();
274+
// byte[] hashed;
275+
276+
// using (var hmac = new HMACSHA1(key))
277+
// {
278+
// hashed = hmac.ComputeHash(bytes);
279+
// }
280+
281+
// return new Address(hashed);
282+
283+
// var digest = new KeccakDigest(256);
284+
// var output = new byte[digest.GetDigestSize()];
285+
// digest.BlockUpdate(value, 0, value.Length);
286+
// digest.DoFinal(output, 0);
287+
// return output;
288+
// }
284289

285290
private static ImmutableArray<byte> DeriveAddress(PublicKey key)
286291
{
287-
byte[] hashPayload = key.Format(false).Skip(1).ToArray();
288-
var output = CalculateHash(hashPayload);
292+
var initaddr = new Nethereum.Util.Sha3Keccack().CalculateHash(
293+
GetPubKeyNoPrefix(key, false));
294+
var addr = new byte[initaddr.Length - 12];
295+
Array.Copy(initaddr, 12, addr, 0, initaddr.Length - 12);
296+
var address = ToChecksumAddress(
297+
Nethereum.Hex.HexConvertors.Extensions.HexByteConvertorExtensions.ToHex(addr));
298+
return Convert.FromHexString(address).ToImmutableArray();
299+
}
289300

290-
return output.Skip(output.Length - Size).ToImmutableArray();
301+
private static byte[] GetPubKeyNoPrefix(PublicKey publicKey, bool compressed = false)
302+
{
303+
var pubKey = publicKey.Format(compressed);
304+
var arr = new byte[pubKey.Length - 1];
305+
Array.Copy(pubKey, 1, arr, 0, arr.Length);
306+
return arr;
291307
}
292308

293309
private static ImmutableArray<byte> DeriveAddress(string hex)

src/Libplanet.Crypto/DefaultCryptoBackend.cs

+24-45
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,47 @@
1-
using System.IO;
2-
using System.Security.Cryptography;
31
using Libplanet.Common;
4-
using Org.BouncyCastle.Asn1;
5-
using Org.BouncyCastle.Crypto.Digests;
6-
using Org.BouncyCastle.Crypto.Signers;
7-
using Org.BouncyCastle.Math;
2+
using Secp256k1Net;
83

94
namespace Libplanet.Crypto
105
{
116
public class DefaultCryptoBackend<T> : ICryptoBackend<T>
12-
where T : HashAlgorithm
7+
where T : System.Security.Cryptography.HashAlgorithm
138
{
9+
private readonly Secp256k1 _instance = new Secp256k1();
10+
private readonly object _instanceLock = new object();
11+
1412
public byte[] Sign(HashDigest<T> messageHash, PrivateKey privateKey)
1513
{
16-
var h = new Sha256Digest();
17-
var kCalculator = new HMacDsaKCalculator(h);
18-
var signer = new ECDsaSigner(kCalculator);
19-
signer.Init(true, privateKey.KeyParam);
20-
BigInteger[] rs = signer.GenerateSignature(messageHash.ToByteArray());
21-
var r = rs[0];
22-
var s = rs[1];
23-
24-
BigInteger otherS = privateKey.KeyParam.Parameters.N.Subtract(s);
25-
if (s.CompareTo(otherS) == 1)
14+
lock (_instanceLock)
2615
{
27-
s = otherS;
28-
}
16+
var signature = new byte[Secp256k1.UNSERIALIZED_SIGNATURE_SIZE];
17+
var privateKeyBytes = privateKey.ToByteArray();
2918

30-
using var bos = new MemoryStream(72);
31-
using (var seq = new DerSequenceGenerator(bos))
32-
{
33-
seq.AddObject(new DerInteger(r));
34-
seq.AddObject(new DerInteger(s));
35-
}
19+
_instance.SignRecoverable(signature, messageHash.ToByteArray(), privateKeyBytes);
3620

37-
return bos.ToArray();
21+
return signature;
22+
}
3823
}
3924

4025
public bool Verify(
4126
HashDigest<T> messageHash,
4227
byte[] signature,
43-
PublicKey publicKey)
28+
PublicKey publicKey) => Verify(messageHash, signature, publicKey.Address);
29+
30+
public bool Verify(
31+
HashDigest<T> messageHash,
32+
byte[] signature,
33+
Address signer)
4434
{
45-
try
35+
lock (_instanceLock)
4636
{
47-
Asn1Sequence asn1Sequence = (Asn1Sequence)Asn1Object.FromByteArray(signature);
37+
var publicKeyOutput = new byte[Secp256k1.PUBKEY_LENGTH];
38+
var serializedKey = new byte[Secp256k1.SERIALIZED_UNCOMPRESSED_PUBKEY_LENGTH];
39+
var h = ByteUtil.Hex(messageHash.ToByteArray());
4840

49-
var rs = new[]
50-
{
51-
((DerInteger)asn1Sequence[0]).Value,
52-
((DerInteger)asn1Sequence[1]).Value,
53-
};
54-
var verifier = new ECDsaSigner();
55-
verifier.Init(false, publicKey.KeyParam);
41+
_instance.Recover(publicKeyOutput, signature, messageHash.ToByteArray());
42+
_instance.PublicKeySerialize(serializedKey, publicKeyOutput);
5643

57-
return verifier.VerifySignature(messageHash.ToByteArray(), rs[0], rs[1]);
58-
}
59-
catch (IOException)
60-
{
61-
return false;
62-
}
63-
catch (Asn1ParsingException)
64-
{
65-
return false;
44+
return signer.Equals(new PublicKey(serializedKey).Address);
6645
}
6746
}
6847
}

0 commit comments

Comments
 (0)