-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathCodec.cs
159 lines (147 loc) · 5.4 KB
/
Codec.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
using System;
using System.Collections.Generic;
namespace Ipfs.Registry
{
/// <summary>
/// Metadata for IPFS multi-codec.
/// </summary>
/// <remarks>
/// IPFS assigns a unique <see cref="Name"/> and <see cref="Code"/> to codecs.
/// See <see href="https://github.com/multiformats/multicodec/blob/master/table.csv">table.csv</see>
/// for the currently defined multi-codecs.
/// </remarks>
/// <seealso href="https://github.com/multiformats/multicodec"/>
public class Codec
{
internal static Dictionary<string, Codec> Names = new(StringComparer.Ordinal);
internal static Dictionary<int, Codec> Codes = new();
/// <summary>
/// Register the standard multi-codecs for IPFS.
/// </summary>
/// <seealso href="https://github.com/multiformats/multicodec/blob/master/table.csv"/>
static Codec()
{
Register("raw", 0x55);
Register("cms", 0x57); // Not official yet, https://github.com/multiformats/multicodec/pull/69
Register("cbor", 0x51);
Register("protobuf", 0x50);
Register("rlp", 0x60);
Register("bencode", 0x63);
Register("multicodec", 0x30);
Register("multihash", 0x31);
Register("multiaddr", 0x32);
Register("multibase", 0x33);
Register("dag-pb", 0x70);
Register("dag-cbor", 0x71);
Register("libp2p-key", 0x72);
Register("git-raw", 0x78);
Register("eth-block", 0x90);
Register("eth-block-list", 0x91);
Register("eth-tx-trie", 0x92);
Register("eth-tx", 0x93);
Register("eth-tx-receipt-trie", 0x94);
Register("eth-tx-receipt", 0x95);
Register("eth-state-trie", 0x96);
Register("eth-account-snapshot", 0x97);
Register("eth-storage-trie", 0x98);
Register("bitcoin-block", 0xb0);
Register("bitcoin-tx", 0xb1);
Register("zcash-block", 0xc0);
Register("zcash-tx", 0xc1);
Register("stellar-block", 0xd0);
Register("stellar-tx", 0xd1);
Register("torrent-info", 0x7b);
Register("torrent-file", 0x7c);
Register("ed25519-pub", 0xed);
}
/// <summary>
/// The name of the codec.
/// </summary>
/// <value>
/// A unique name.
/// </value>
public string Name { get; private set; } = string.Empty;
/// <summary>
/// The IPFS code assigned to the codec.
/// </summary>
/// <value>
/// Valid codes at <see href="https://github.com/multiformats/multicodec/blob/master/table.csv"/>.
/// </value>
public int Code { get; private set; }
/// <summary>
/// Use <see cref="Register"/> to create a new instance of a <see cref="Codec"/>.
/// </summary>
private Codec()
{
}
/// <summary>
/// The <see cref="Name"/> of the codec.
/// </summary>
/// <value>
/// The <see cref="Name"/> of the codec.
/// </value>
public override string ToString()
{
return Name;
}
/// <summary>
/// Register a new IPFS codec.
/// </summary>
/// <param name="name">
/// The name of the codec.
/// </param>
/// <param name="code">
/// The IPFS code assigned to the codec.
/// </param>
/// <returns>
/// A new <see cref="Codec"/>.
/// </returns>
/// <exception cref="ArgumentException">
/// When the <paramref name="name"/> or <paramref name="code"/> is already defined.
/// </exception>
/// <exception cref="ArgumentNullException">
/// When the <paramref name="name"/> is null or empty.
/// </exception>
public static Codec Register(string name, int code)
{
if (string.IsNullOrWhiteSpace(name))
{
throw new ArgumentNullException(nameof(name));
}
if (Names.ContainsKey(name))
{
throw new ArgumentException(string.Format("The IPFS codec name '{0}' is already defined.", name));
}
if (Codes.ContainsKey(code))
{
throw new ArgumentException(string.Format("The IPFS codec code '{0}' is already defined.", code));
}
var a = new Codec
{
Name = name,
Code = code
};
Names[name] = a;
Codes[code] = a;
return a;
}
/// <summary>
/// Remove an IPFS codec from the registry.
/// </summary>
/// <param name="codec">
/// The <see cref="Codec"/> to remove.
/// </param>
public static void Deregister(Codec codec)
{
Names.Remove(codec.Name);
Codes.Remove(codec.Code);
}
/// <summary>
/// A sequence consisting of all codecs.
/// </summary>
/// <value>
/// All the registered codecs.
/// </value>
public static IEnumerable<Codec> All => Names.Values;
}
}