-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathPeer.cs
179 lines (161 loc) · 5.71 KB
/
Peer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Ipfs
{
/// <summary>
/// A daemon node on the IPFS network.
/// </summary>
/// <remarks>
/// Equality is based solely on the peer's <see cref="Id"/>.
/// </remarks>
public class Peer : IEquatable<Peer>
{
static MultiAddress[] noAddress = new MultiAddress[0];
const string unknown = "unknown/0.0";
/// <summary>
/// Universally unique identifier.
/// </summary>
/// <value>
/// This is the <see cref="MultiHash"/> of the peer's protobuf encoded
/// <see cref="PublicKey"/>.
/// </value>
/// <seealso href="https://github.com/libp2p/specs/pull/100"/>
public MultiHash Id { get; set; }
/// <summary>
/// The public key of the node.
/// </summary>
/// <value>
/// The base 64 encoding of the node's public key. The default is <b>null</b>
/// </value>
/// <remarks>
/// The IPFS public key is the base-64 encoding of a protobuf encoding containing
/// a type and the DER encoding of the PKCS Subject Public Key Info.
/// </remarks>
/// <seealso href="https://tools.ietf.org/html/rfc5280#section-4.1.2.7"/>
public string PublicKey { get; set; }
/// <summary>
/// The multiple addresses of the node.
/// </summary>
/// <value>
/// Where the peer can be found. The default is an empty sequence.
/// </value>
public IEnumerable<MultiAddress> Addresses { get; set; } = noAddress;
/// <summary>
/// The name and version of the IPFS software.
/// </summary>
/// <value>
/// For example "go-ipfs/0.4.17/".
/// </value>
/// <remarks>
/// There is no specification that describes the agent version string. The default
/// is "unknown/0.0".
/// </remarks>
public string AgentVersion { get; set; } = unknown;
/// <summary>
/// The name and version of the supported IPFS protocol.
/// </summary>
/// <value>
/// For example "ipfs/0.1.0".
/// </value>
/// <remarks>
/// There is no specification that describes the protocol version string. The default
/// is "unknown/0.0".
/// </remarks>
public string ProtocolVersion { get; set; } = unknown;
/// <summary>
/// The <see cref="MultiAddress"/> that the peer is connected on.
/// </summary>
/// <value>
/// <b>null</b> when the peer is not connected to.
/// </value>
public MultiAddress ConnectedAddress { get; set; }
/// <summary>
/// The round-trip time it takes to get data from the peer.
/// </summary>
public TimeSpan? Latency { get; set; }
/// <summary>
/// Determines if the information on the peer is valid.
/// </summary>
/// <returns>
/// <b>true</b> if all validation rules pass; otherwise <b>false</b>.
/// </returns>
/// <remarks>
/// Verifies that
/// <list type="bullet">
/// <item><description>The <see cref="Id"/> is defined</description></item>
/// <item><description>The <see cref="Id"/> is a hash of the <see cref="PublicKey"/></description></item>
/// </list>
/// </remarks>
public bool IsValid()
{
if (Id == null)
return false;
if (PublicKey != null && !Id.Matches(Convert.FromBase64String(PublicKey)))
return false;
return true;
}
/// <inheritdoc />
public override int GetHashCode()
{
return ToString().GetHashCode();
}
/// <inheritdoc />
public override bool Equals(object obj)
{
var that = obj as Peer;
return (that == null)
? false
: this.Equals(that);
}
/// <inheritdoc />
public bool Equals(Peer that)
{
return this.Id == that.Id;
}
/// <summary>
/// Value equality.
/// </summary>
public static bool operator ==(Peer a, Peer b)
{
if (object.ReferenceEquals(a, b)) return true;
if (a is null) return false;
if (b is null) return false;
return a.Equals(b);
}
/// <summary>
/// Value inequality.
/// </summary>
public static bool operator !=(Peer a, Peer b)
{
return !(a == b);
}
/// <summary>
/// Returns the <see cref="Base58"/> encoding of the <see cref="Id"/>.
/// </summary>
/// <returns>
/// A Base58 representaton of the peer.
/// </returns>
public override string ToString()
{
return Id == null ? string.Empty : Id.ToBase58();
}
/// <summary>
/// Implicit casting of a <see cref="string"/> to a <see cref="Peer"/>.
/// </summary>
/// <param name="s">
/// A <see cref="Base58"/> encoded <see cref="Id"/>.
/// </param>
/// <returns>
/// A new <see cref="Peer"/>.
/// </returns>
/// <remarks>
/// Equivalent to <code>new Peer { Id = s }</code>
/// </remarks>
static public implicit operator Peer(string s)
{
return new Peer { Id = s };
}
}
}