/
TrustNodeExtensions.cs
48 lines (42 loc) · 1.46 KB
/
TrustNodeExtensions.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
// Copyright Bastian Eicher et al.
// Licensed under the GNU Lesser Public License
using ZeroInstall.Store.Trust;
namespace ZeroInstall.Store.ViewModel;
/// <summary>
/// Maps between <see cref="TrustDB"/> and <see cref="TrustNode"/>s.
/// </summary>
public static class TrustNodeExtensions
{
/// <summary>
/// Creates <see cref="TrustNode"/> representations for all entries in a <see cref="TrustDB"/>.
/// </summary>
public static NamedCollection<TrustNode> ToNodes(this TrustDB trustDB)
{
#region Sanity checks
if (trustDB == null) throw new ArgumentNullException(nameof(trustDB));
#endregion
var nodes = new NamedCollection<TrustNode>();
foreach (var key in trustDB.Keys)
{
foreach (var domain in key.Domains)
{
if (key.Fingerprint != null)
nodes.Add(new(key.Fingerprint, domain));
}
}
return nodes;
}
/// <summary>
/// Creates a <see cref="TrustDB"/> from <see cref="TrustNode"/>s.
/// </summary>
public static TrustDB ToTrustDB(this IEnumerable<TrustNode> nodes)
{
#region Sanity checks
if (nodes == null) throw new ArgumentNullException(nameof(nodes));
#endregion
var trustDB = new TrustDB();
foreach (var node in nodes)
trustDB.TrustKey(node.Fingerprint, node.Domain);
return trustDB;
}
}