-
Notifications
You must be signed in to change notification settings - Fork 463
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
Feature external signer #6780
Feature external signer #6780
Changes from 30 commits
18e9182
8a812ff
3f38c73
0fc08ed
1e5a569
c535c1b
b15fe43
5bf7c60
64becce
14284f2
64dcddf
aee1fca
5b69110
fedde62
1ab457c
3294716
045a515
a245145
473927e
1c2f94f
6b60f76
2fbf68e
bb05c1b
1012dd3
48dcd4c
2a34f39
54afa02
c99c5fa
d06892a
79801f5
18f01ef
50fee7a
ece7b6f
821117f
8d07596
206a61b
536c290
7ebf203
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// SPDX-FileCopyrightText: 2022 Demerzel Solutions Limited | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
|
||
using System; | ||
using System.Threading.Tasks; | ||
using FluentAssertions; | ||
using Nethermind.Core; | ||
using Nethermind.Core.Crypto; | ||
using Nethermind.Core.Extensions; | ||
using Nethermind.Core.Test.Builders; | ||
using Nethermind.JsonRpc; | ||
using Nethermind.JsonRpc.Client; | ||
using NSubstitute; | ||
using NUnit.Framework; | ||
|
||
namespace Nethermind.Blockchain.Test.Consensus | ||
{ | ||
[TestFixture] | ||
public class ClefSignerTests | ||
{ | ||
[Test] | ||
public async Task Sign_SigningHash_RequestHasCorrectParameters() | ||
{ | ||
IJsonRpcClient client = Substitute.For<IJsonRpcClient>(); | ||
client.Post<string[]>("account_list").Returns(Task.FromResult<string[]?>([TestItem.AddressA!.ToString()])); | ||
Task<string?> postMethod = client.Post<string>("account_signData", "text/plain", Arg.Any<string>(), Keccak.Zero); | ||
var returnValue = (new byte[65]).ToHexString(); | ||
postMethod.Returns(returnValue); | ||
ClefSigner sut = await ClefSigner.Create(client); | ||
|
||
var result = sut.Sign(Keccak.Zero); | ||
|
||
Assert.That(new Signature(returnValue).Bytes, Is.EqualTo(result.Bytes)); | ||
} | ||
|
||
[Test] | ||
public async Task Sign_SigningCliqueHeader_PassingCorrectClefParametersForRequest() | ||
{ | ||
IJsonRpcClient client = Substitute.For<IJsonRpcClient>(); | ||
client.Post<string[]>("account_list").Returns(Task.FromResult<string[]?>([TestItem.AddressA!.ToString()])); | ||
Task<string?> postMethod = client.Post<string>(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>()); | ||
var returnValue = (new byte[65]).ToHexString(); | ||
postMethod.Returns(returnValue); | ||
BlockHeader blockHeader = Build.A.BlockHeader.TestObject; | ||
ClefSigner sut = await ClefSigner.Create(client); | ||
|
||
sut.Sign(blockHeader); | ||
|
||
await client.Received().Post<string>("account_signData", "application/x-clique-header", Arg.Any<string>(), Arg.Any<string>()); | ||
} | ||
|
||
|
||
[TestCase(0, 27)] | ||
[TestCase(1, 28)] | ||
public async Task Sign_RecoveryIdIsSetToCliqueValues_RecoveryIdIsAdjusted(byte recId, byte expected) | ||
{ | ||
IJsonRpcClient client = Substitute.For<IJsonRpcClient>(); | ||
client.Post<string[]>("account_list").Returns(Task.FromResult<string[]?>([TestItem.AddressA!.ToString()])); | ||
Task<string?> postMethod = client.Post<string>("account_signData", "application/x-clique-header", Arg.Any<string>(), Arg.Any<string>()); | ||
var returnValue = (new byte[65]); | ||
returnValue[64] = recId; | ||
postMethod.Returns(returnValue.ToHexString()); | ||
BlockHeader blockHeader = Build.A.BlockHeader.TestObject; | ||
ClefSigner sut = await ClefSigner.Create(client); | ||
|
||
var result = sut.Sign(blockHeader); | ||
|
||
Assert.That(result.V, Is.EqualTo(expected)); | ||
} | ||
|
||
[Test] | ||
public async Task Create_SignerAddressSpecified_CorrectAddressIsSet() | ||
{ | ||
IJsonRpcClient client = Substitute.For<IJsonRpcClient>(); | ||
client.Post<string[]>("account_list").Returns(Task.FromResult<string[]?>([TestItem.AddressA!.ToString(), TestItem.AddressB!.ToString()])); | ||
|
||
ClefSigner sut = await ClefSigner.Create(client, TestItem.AddressB); | ||
|
||
Assert.That(sut.Address, Is.EqualTo(TestItem.AddressB)); | ||
} | ||
|
||
[Test] | ||
public void Create_SignerAddressDoesNotExists_ThrowInvalidOperationException() | ||
{ | ||
IJsonRpcClient client = Substitute.For<IJsonRpcClient>(); | ||
client.Post<string[]>("account_list").Returns(Task.FromResult<string[]?>([TestItem.AddressA!.ToString(), TestItem.AddressB!.ToString()])); | ||
|
||
Assert.That(async () => await ClefSigner.Create(client, TestItem.AddressC), Throws.InstanceOf<InvalidOperationException>()); | ||
} | ||
|
||
[Test] | ||
public async Task SetSigner_TryingToASigner_ThrowInvalidOperationException() | ||
{ | ||
IJsonRpcClient client = Substitute.For<IJsonRpcClient>(); | ||
client.Post<string[]>("account_list").Returns(Task.FromResult<string[]?>([TestItem.AddressA!.ToString()])); | ||
ClefSigner sut = await ClefSigner.Create(client); | ||
|
||
Assert.That(() => sut.SetSigner(Build.A.PrivateKey.TestObject), Throws.InstanceOf<InvalidOperationException>()); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,4 +70,6 @@ public IBlocksConfig? BlocksConfig | |
return _blocksConfig; | ||
} | ||
} | ||
|
||
public string Signer { get; set; } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe it is not possible, but could we make it an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not possible with the current JsonSerializer, but a custom one could be made. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we should write one, is easy - here with JSON.NET: https://stackoverflow.com/a/8087049/1187056 |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similarly a lot of duplicate in setup