Skip to content

Commit 2604d57

Browse files
Wi1l-B0tshargonNGDAdmincschuchardt88
committed
Add: SignClient Vsock support (neo-project#4002)
* Add: SignClient vsock support * optimize: add const string * optimize: use Uri parse endpoint --------- Co-authored-by: Shargon <[email protected]> Co-authored-by: NGD Admin <[email protected]> Co-authored-by: Christopher Schuchardt <[email protected]>
1 parent 49c8af6 commit 2604d57

File tree

5 files changed

+93
-12
lines changed

5 files changed

+93
-12
lines changed

src/Plugins/SignClient/Settings.cs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Copyright (C) 2015-2025 The Neo Project.
2+
//
3+
// Settings.cs file belongs to the neo project and is free
4+
// software distributed under the MIT software license, see the
5+
// accompanying file LICENSE in the main directory of the
6+
// repository or http://www.opensource.org/licenses/mit-license.php
7+
// for more details.
8+
//
9+
// Redistribution and use in source and binary forms with or without
10+
// modifications are permitted.
11+
12+
using Microsoft.Extensions.Configuration;
13+
14+
namespace Neo.Plugins.SignClient
15+
{
16+
public class Settings : PluginSettings
17+
{
18+
public const string SectionName = "PluginConfiguration";
19+
private const string DefaultEndpoint = "http://127.0.0.1:9991";
20+
21+
/// <summary>
22+
/// The name of the sign client(i.e. Signer).
23+
/// </summary>
24+
public readonly string Name;
25+
26+
/// <summary>
27+
/// The host of the sign client(i.e. Signer).
28+
/// The "Endpoint" should be "vsock://contextId:port" if use vsock.
29+
/// The "Endpoint" should be "http://host:port" or "https://host:port" if use tcp.
30+
/// </summary>
31+
public readonly string Endpoint;
32+
33+
/// <summary>
34+
/// Create a new settings instance from the configuration section.
35+
/// </summary>
36+
/// <param name="section">The configuration section.</param>
37+
/// <exception cref="FormatException">If the endpoint type or endpoint is invalid.</exception>
38+
public Settings(IConfigurationSection section) : base(section)
39+
{
40+
Name = section.GetValue("Name", "SignClient");
41+
Endpoint = section.GetValue("Endpoint", DefaultEndpoint); // Only support local host at present
42+
_ = GetVsockAddress(); // for check the endpoint is valid
43+
}
44+
45+
public static Settings Default
46+
{
47+
get
48+
{
49+
var section = new ConfigurationBuilder()
50+
.AddInMemoryCollection(new Dictionary<string, string?>
51+
{
52+
[SectionName + ":Name"] = "SignClient",
53+
[SectionName + ":Endpoint"] = DefaultEndpoint
54+
})
55+
.Build()
56+
.GetSection(SectionName);
57+
return new Settings(section);
58+
}
59+
}
60+
61+
/// <summary>
62+
/// Get the vsock address from the endpoint.
63+
/// </summary>
64+
/// <returns>The vsock address. If the endpoint type is not vsock, return null.</returns>
65+
/// <exception cref="FormatException">If the endpoint is invalid.</exception>
66+
internal VsockAddress? GetVsockAddress()
67+
{
68+
var uri = new Uri(Endpoint); // UriFormatException is a subclass of FormatException
69+
if (uri.Scheme != "vsock") return null;
70+
try
71+
{
72+
return new VsockAddress(int.Parse(uri.Host), uri.Port);
73+
}
74+
catch
75+
{
76+
throw new FormatException($"Invalid vsock endpoint: {Endpoint}");
77+
}
78+
}
79+
}
80+
}

src/Plugins/SignClient/SignClient.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public class SignClient : Plugin, ISigner
4646

4747
public SignClient() { }
4848

49-
public SignClient(SignSettings settings)
49+
public SignClient(Settings settings)
5050
{
5151
Reset(settings);
5252
}
@@ -66,7 +66,7 @@ private void Reset(string name, SecureSign.SecureSignClient? client)
6666
if (!string.IsNullOrEmpty(_name)) SignerManager.RegisterSigner(_name, this);
6767
}
6868

69-
private ServiceConfig GetServiceConfig(SignSettings settings)
69+
private ServiceConfig GetServiceConfig(Settings settings)
7070
{
7171
var methodConfig = new MethodConfig
7272
{
@@ -93,7 +93,7 @@ private ServiceConfig GetServiceConfig(SignSettings settings)
9393
return new ServiceConfig { MethodConfigs = { methodConfig } };
9494
}
9595

96-
private void Reset(SignSettings settings)
96+
private void Reset(Settings settings)
9797
{
9898
// _settings = settings;
9999
var serviceConfig = GetServiceConfig(settings);
@@ -332,7 +332,7 @@ public ReadOnlyMemory<byte> SignBlock(Block block, ECPoint publicKey, uint netwo
332332
protected override void Configure()
333333
{
334334
var config = GetConfiguration();
335-
if (config is not null) Reset(new SignSettings(config));
335+
if (config is not null) Reset(new Settings(config));
336336
}
337337

338338
/// <inheritdoc/>

src/Plugins/SignClient/SignClient.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
<PackageReference Include="Grpc.Net.Client" Version="2.71.0" />
2626
<!-- NOTE: Need install rosetta2 on macOS ARM64 -->
2727
<PackageReference Include="Grpc.Tools" Version="2.72.0" PrivateAssets="all" />
28+
<PackageReference Include="Ookii.VmSockets" Version="1.0.0" />
2829
</ItemGroup>
2930

3031
<ItemGroup>

tests/Neo.Plugins.SignClient.Tests/UT_SignClient.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,12 @@ private static SignClient NewClient(Block? block, ExtensiblePayload? payload)
5555
var section = new ConfigurationBuilder()
5656
.AddInMemoryCollection(new Dictionary<string, string?>
5757
{
58-
[SignSettings.SectionName + ":Name"] = "SignClient",
59-
[SignSettings.SectionName + ":Endpoint"] = endpoint,
58+
[Settings.SectionName + ":Name"] = "SignClient",
59+
[Settings.SectionName + ":Endpoint"] = endpoint,
6060
})
6161
.Build()
62-
.GetSection(SignSettings.SectionName);
63-
return new SignClient(new SignSettings(section));
62+
.GetSection(Settings.SectionName);
63+
return new SignClient(new Settings(section));
6464
}
6565

6666
var mockClient = new Mock<SecureSign.SecureSignClient>();

tests/Neo.Plugins.SignClient.Tests/UT_Vsock.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public void TestGetVsockAddress()
2929
.Build()
3030
.GetSection("PluginConfiguration");
3131

32-
var settings = new SignSettings(section);
32+
var settings = new Settings(section);
3333
Assert.AreEqual(address, settings.GetVsockAddress());
3434

3535
section = new ConfigurationBuilder()
@@ -39,7 +39,7 @@ public void TestGetVsockAddress()
3939
})
4040
.Build()
4141
.GetSection("PluginConfiguration");
42-
Assert.IsNull(new SignSettings(section).GetVsockAddress());
42+
Assert.IsNull(new Settings(section).GetVsockAddress());
4343
}
4444

4545
[TestMethod]
@@ -52,7 +52,7 @@ public void TestInvalidEndpoint()
5252
})
5353
.Build()
5454
.GetSection("PluginConfiguration");
55-
Assert.ThrowsExactly<FormatException>(() => _ = new SignSettings(section).GetVsockAddress());
55+
Assert.ThrowsExactly<FormatException>(() => _ = new Settings(section).GetVsockAddress());
5656

5757
section = new ConfigurationBuilder()
5858
.AddInMemoryCollection(new Dictionary<string, string?>
@@ -61,7 +61,7 @@ public void TestInvalidEndpoint()
6161
})
6262
.Build()
6363
.GetSection("PluginConfiguration");
64-
Assert.ThrowsExactly<UriFormatException>(() => _ = new SignSettings(section).GetVsockAddress());
64+
Assert.ThrowsExactly<UriFormatException>(() => _ = new Settings(section).GetVsockAddress());
6565
}
6666
}
6767
}

0 commit comments

Comments
 (0)