Skip to content

Commit 7ee7771

Browse files
feat(PeerNode): determine if the information is valid
1 parent 956fa12 commit 7ee7771

File tree

2 files changed

+92
-4
lines changed

2 files changed

+92
-4
lines changed

src/PeerNode.cs

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,79 @@
66
namespace Ipfs.Api
77
{
88
/// <summary>
9-
/// Information about a peer node.
9+
/// An IPFS node.
1010
/// </summary>
1111
public class PeerNode
1212
{
1313
/// <summary>
1414
/// Unique identifier (multihash)
1515
/// </summary>
16+
/// <value>
17+
/// This is the <see cref="MultiHash"/> of the peer's <see cref="PublicKey"/>.
18+
/// </value>
1619
public string Id { get; set; }
1720

1821
/// <summary>
1922
/// The public key of the node.
2023
/// </summary>
24+
/// <value>
25+
/// The base 64 encoding of the node's public key.
26+
/// </value>
2127
public string PublicKey { get; set; }
2228

2329
/// <summary>
24-
/// The multi addresses of the node.
30+
/// The multiple addresses of the node.
2531
/// </summary>
32+
/// <value>
33+
/// Where the peer can be found.
34+
/// </value>
2635
public IEnumerable<MultiAddress> Addresses { get; set; }
2736

2837
/// <summary>
29-
/// TODO
38+
/// The name and version of the IPFS software.
3039
/// </summary>
40+
/// <value>
41+
/// For example "go-ipfs/0.4.7/".
42+
/// </value>
43+
/// <remarks>
44+
/// There is no specification that describes the agent version string.
45+
/// </remarks>
3146
public string AgentVersion { get; set; }
3247

3348
/// <summary>
34-
/// TODO
49+
/// The name and version of the supported IPFS protocol.
3550
/// </summary>
51+
/// <value>
52+
/// For example "go-ipfs/0.4.7/".
53+
/// </value>
54+
/// <remarks>
55+
/// There is no specification that describes the protocol version string.
56+
/// </remarks>
3657
public string ProtocolVersion { get; set; }
58+
59+
/// <summary>
60+
/// Determines if the information on the peer is valid.
61+
/// </summary>
62+
/// <returns>
63+
/// <b>true</b> if all validation rules pass; otherwise <b>false</b>.
64+
/// </returns>
65+
/// <remarks>
66+
/// Verifies that
67+
/// <list type="bullet">
68+
/// <item>The <see cref="Id"/> is a hash of the <see cref="PublicKey"/></item>
69+
/// <item>All <see cref="Addresses"/> point to the node</item>
70+
/// </list>
71+
/// </remarks>
72+
public bool IsValid()
73+
{
74+
var mh = new MultiHash(Id);
75+
if (!mh.Matches(Convert.FromBase64String(PublicKey)))
76+
return false;
77+
if (!Addresses.All(a =>
78+
a.Protocols.Last().Name == "ipfs" &&
79+
a.Protocols.Last().Value == Id))
80+
return false;
81+
return true;
82+
}
3783
}
3884
}

test/PeerNodeTest.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using System;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
6+
namespace Ipfs.Api
7+
{
8+
[TestClass]
9+
public class PeerNodeTest
10+
{
11+
const string mars = "QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3";
12+
13+
[TestMethod]
14+
public async Task Valid_Node()
15+
{
16+
var ipfs = TestFixture.Ipfs;
17+
var localNode = await ipfs.IdAsync();
18+
Assert.IsTrue(localNode.IsValid());
19+
}
20+
21+
[TestMethod]
22+
public async Task Invalid_Node_Address()
23+
{
24+
var ipfs = TestFixture.Ipfs;
25+
var localNode = await ipfs.IdAsync();
26+
var addresses = localNode.Addresses.ToList();
27+
addresses.Add(new MultiAddress($"/ip6/::1/tcp/4001/ipfs/{mars}"));
28+
localNode.Addresses = addresses;
29+
Assert.IsFalse(localNode.IsValid());
30+
}
31+
32+
[TestMethod]
33+
public async Task Invalid_Node_Id()
34+
{
35+
var ipfs = TestFixture.Ipfs;
36+
var localNode = await ipfs.IdAsync();
37+
localNode.Id = mars;
38+
Assert.IsFalse(localNode.IsValid());
39+
}
40+
41+
}
42+
}

0 commit comments

Comments
 (0)