Skip to content

Commit 6ee41ed

Browse files
Wi1l-B0tshargonvncoelho
authored
[Fix]: ISigner Only sign consensus messages (#3939)
* Fix: make the signer sign specific data * Fix: make the signer sign specific data --------- Co-authored-by: Shargon <[email protected]> Co-authored-by: Vitor Nazário Coelho <[email protected]>
1 parent bfc70e9 commit 6ee41ed

File tree

8 files changed

+116
-70
lines changed

8 files changed

+116
-70
lines changed

src/Neo.CLI/CLI/MainService.Wallet.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ private void OnCloseWalletCommand()
7373

7474
if (CurrentWallet is not null)
7575
{
76-
SignerFactory.UnregisterSigner(CurrentWallet.Name);
76+
SignerManager.UnregisterSigner(CurrentWallet.Name);
7777
}
7878

7979
CurrentWallet = null;

src/Neo.CLI/CLI/MainService.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public void CreateWallet(string path, string password, bool createDefaultAccount
161161
wallet.Save();
162162

163163
CurrentWallet = wallet;
164-
SignerFactory.RegisterSigner(wallet.Name, wallet);
164+
SignerManager.RegisterSigner(wallet.Name, wallet);
165165
}
166166

167167
private bool NoWallet()
@@ -296,10 +296,10 @@ public void OpenWallet(string path, string password)
296296
throw new FileNotFoundException($"Wallet file \"{path}\" not found.");
297297
}
298298

299-
if (CurrentWallet is not null) SignerFactory.UnregisterSigner(CurrentWallet.Name);
299+
if (CurrentWallet is not null) SignerManager.UnregisterSigner(CurrentWallet.Name);
300300

301301
CurrentWallet = Wallet.Open(path, password, NeoSystem.Settings) ?? throw new NotSupportedException();
302-
SignerFactory.RegisterSigner(CurrentWallet.Name, CurrentWallet);
302+
SignerManager.RegisterSigner(CurrentWallet.Name, CurrentWallet);
303303
}
304304

305305
public async void Start(CommandLineOptions options)

src/Neo/Sign/ISigner.cs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
// modifications are permitted.
1111

1212
using Neo.Cryptography.ECC;
13-
using Neo.SmartContract;
13+
using Neo.Network.P2P.Payloads;
14+
using Neo.Persistence;
1415
using System;
1516

1617
namespace Neo.Sign
@@ -21,22 +22,27 @@ namespace Neo.Sign
2122
public interface ISigner
2223
{
2324
/// <summary>
24-
/// Signs the <see cref="ContractParametersContext"/> with the wallet.
25+
/// Signs the <see cref="ExtensiblePayload"/> with the wallet.
2526
/// </summary>
26-
/// <param name="context">The <see cref="ContractParametersContext"/> to be used.</param>
27-
/// <returns>
28-
/// <see langword="true"/> if any signature is successfully added to the context;
29-
/// otherwise, <see langword="false"/>.
30-
/// </returns>
31-
bool Sign(ContractParametersContext context);
27+
/// <param name="payload">The <see cref="ExtensiblePayload"/> to be used.</param>
28+
/// <param name="snapshot">The snapshot.</param>
29+
/// <param name="network">The network.</param>
30+
/// <returns>The witness.</returns>
31+
/// <exception cref="ArgumentNullException">Thrown when the payload is null.</exception>
32+
Witness SignExtensiblePayload(ExtensiblePayload payload, DataCache snapshot, uint network);
3233

3334
/// <summary>
3435
/// Signs the specified data with the corresponding private key of the specified public key.
3536
/// </summary>
36-
/// <param name="signData">The data to sign.</param>
37+
/// <param name="block">The block to sign.</param>
3738
/// <param name="publicKey">The public key.</param>
39+
/// <param name="network">The network.</param>
3840
/// <returns>The signature.</returns>
39-
ReadOnlyMemory<byte> Sign(byte[] signData, ECPoint publicKey);
41+
/// <exception cref="ArgumentNullException">Thrown when the block or public key is null.</exception>
42+
/// <exception cref="SignException">
43+
/// Thrown when the account is not found or not signable, or the network is not matching.
44+
/// </exception>
45+
ReadOnlyMemory<byte> SignBlock(Block block, ECPoint publicKey, uint network);
4046

4147
/// <summary>
4248
/// Checks if the wallet contains an account(has private key and is not locked) with the specified public key.

src/Neo/Sign/SignerFactory.cs renamed to src/Neo/Sign/SignerManager.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Copyright (C) 2015-2025 The Neo Project.
22
//
3-
// SignerFactory.cs file belongs to the neo project and is free
3+
// SignerManager.cs file belongs to the neo project and is free
44
// software distributed under the MIT software license, see the
55
// accompanying file LICENSE in the main directory of the
66
// repository or http://www.opensource.org/licenses/mit-license.php
@@ -15,7 +15,7 @@
1515

1616
namespace Neo.Sign
1717
{
18-
public static class SignerFactory
18+
public static class SignerManager
1919
{
2020
private static readonly ConcurrentDictionary<string, ISigner> s_signers = new();
2121

src/Neo/Wallets/Wallet.cs

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
using Neo.Cryptography;
1313
using Neo.Extensions;
14+
using Neo.Network.P2P;
1415
using Neo.Network.P2P.Payloads;
1516
using Neo.Persistence;
1617
using Neo.Sign;
@@ -664,11 +665,42 @@ public bool Sign(ContractParametersContext context)
664665
return fSuccess;
665666
}
666667

667-
/// <inheritdoc/>
668-
public ReadOnlyMemory<byte> Sign(byte[] signData, ECPoint publicKey)
668+
/// <summary>
669+
/// Signs the specified extensible payload with the wallet.
670+
/// </summary>
671+
/// <param name="payload">The extensible payload to sign.</param>
672+
/// <param name="snapshot">The snapshot.</param>
673+
/// <param name="network">The network.</param>
674+
/// <returns>The signature.</returns>
675+
/// <exception cref="ArgumentNullException">Thrown when the payload is null.</exception>
676+
public Witness SignExtensiblePayload(ExtensiblePayload payload, DataCache snapshot, uint network)
677+
{
678+
if (payload is null) throw new ArgumentNullException(nameof(payload));
679+
680+
var context = new ContractParametersContext(snapshot, payload, network);
681+
Sign(context);
682+
683+
return context.GetWitnesses()[0];
684+
}
685+
686+
/// <summary>
687+
/// Signs the specified block with the specified public key.
688+
/// </summary>
689+
/// <param name="block">The block to sign.</param>
690+
/// <param name="publicKey">The public key.</param>
691+
/// <param name="network">The network.</param>
692+
/// <returns>The signature.</returns>
693+
/// <exception cref="ArgumentNullException">Thrown when the block or public key is null.</exception>
694+
/// <exception cref="SignException">
695+
/// Thrown when the account is not found, the private key is not found, the account is locked,
696+
/// or the network is not matching.
697+
/// </exception>
698+
public ReadOnlyMemory<byte> SignBlock(Block block, ECPoint publicKey, uint network)
669699
{
670-
if (signData is null) throw new ArgumentNullException(nameof(signData));
700+
if (block is null) throw new ArgumentNullException(nameof(block));
671701
if (publicKey is null) throw new ArgumentNullException(nameof(publicKey));
702+
if (network != ProtocolSettings.Network)
703+
throw new SignException($"Network is not matching({ProtocolSettings.Network} != {network})");
672704

673705
var account = GetAccount(publicKey);
674706
if (account is null)
@@ -681,10 +713,18 @@ public ReadOnlyMemory<byte> Sign(byte[] signData, ECPoint publicKey)
681713
if (account.Lock)
682714
throw new SignException("Account is locked");
683715

716+
var signData = block.GetSignData(network);
684717
return Crypto.Sign(signData, privateKey);
685718
}
686719

687-
/// <inheritdoc/>
720+
/// <summary>
721+
/// Checks if the wallet contains an account with the specified public key.
722+
/// </summary>
723+
/// <param name="publicKey">The public key.</param>
724+
/// <returns>
725+
/// <see langword="true"/> if the account is found and has a private key and is not locked;
726+
/// otherwise, <see langword="false"/>.
727+
/// </returns>
688728
public bool ContainsSignable(ECPoint publicKey)
689729
{
690730
var account = GetAccount(publicKey);

src/Plugins/DBFTPlugin/Consensus/ConsensusContext.MakePayload.cs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@
1111

1212
using Neo.Extensions;
1313
using Neo.Ledger;
14-
using Neo.Network.P2P;
1514
using Neo.Network.P2P.Payloads;
1615
using Neo.Plugins.DBFTPlugin.Messages;
1716
using Neo.Plugins.DBFTPlugin.Types;
18-
using Neo.SmartContract;
17+
using Neo.Sign;
1918
using System;
2019
using System.Buffers.Binary;
2120
using System.Collections.Generic;
@@ -40,10 +39,10 @@ public ExtensiblePayload MakeCommit()
4039
if (CommitPayloads[MyIndex] is not null)
4140
return CommitPayloads[MyIndex];
4241

43-
var signData = EnsureHeader().GetSignData(dbftSettings.Network);
42+
var block = EnsureHeader();
4443
CommitPayloads[MyIndex] = MakeSignedPayload(new Commit
4544
{
46-
Signature = _signer.Sign(signData, _myPublicKey)
45+
Signature = _signer.SignBlock(block, _myPublicKey, dbftSettings.Network)
4746
});
4847
return CommitPayloads[MyIndex];
4948
}
@@ -60,18 +59,15 @@ private ExtensiblePayload MakeSignedPayload(ConsensusMessage message)
6059

6160
private void SignPayload(ExtensiblePayload payload)
6261
{
63-
ContractParametersContext sc;
6462
try
6563
{
66-
sc = new ContractParametersContext(neoSystem.StoreView, payload, dbftSettings.Network);
67-
_signer.Sign(sc);
64+
payload.Witness = _signer.SignExtensiblePayload(payload, Snapshot, dbftSettings.Network);
6865
}
69-
catch (InvalidOperationException exception)
66+
catch (InvalidOperationException ex)
7067
{
71-
Utility.Log(nameof(ConsensusContext), LogLevel.Debug, exception.ToString());
68+
Utility.Log(nameof(ConsensusContext), LogLevel.Debug, ex.ToString());
7269
return;
7370
}
74-
payload.Witness = sc.GetWitnesses()[0];
7571
}
7672

7773
/// <summary>

src/Plugins/DBFTPlugin/DBFTPlugin.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ void IWalletChangedHandler.IWalletProvider_WalletChanged_Handler(object sender,
8787
[ConsoleCommand("start consensus", Category = "Consensus", Description = "Start consensus service (dBFT)")]
8888
private void OnStart(string signerName = "")
8989
{
90-
var signer = SignerFactory.GetSignerOrDefault(signerName);
90+
var signer = SignerManager.GetSignerOrDefault(signerName);
9191
Start(signer ?? walletProvider.GetWallet());
9292
}
9393

0 commit comments

Comments
 (0)